Commit 9058a4c0 authored by fangel's avatar fangel Committed by Paris Liakos

Issue #1658452 Regression: Views Library does not support mutliselect

parent a7bff22a
......@@ -35,7 +35,7 @@ Drupal.behaviors.mediaElement = {
}
// When someone clicks the link to pick media (or clicks on an existing thumbnail)
$('.launcher', this).bind('click', function () {
$('.launcher', this).bind('click', function (e) {
// Launch the browser, providing the following callback function
// @TODO: This should not be an anomyous function.
Drupal.media.popups.mediaBrowser(function (mediaFiles) {
......@@ -49,17 +49,17 @@ Drupal.behaviors.mediaElement = {
// Set the preview field HTML.
previewField.html(mediaFile.preview);
}, globalOptions);
return false;
e.preventDefault();
});
// When someone clicks the Remove button.
$('.remove', this).bind('click', function () {
$('.remove', this).bind('click', function (e) {
// Set the value of the filefield fid (hidden) and trigger change.
fidField.val(0);
fidField.trigger('change');
// Set the preview field HTML.
previewField.html('');
return false;
e.preventDefault();
});
// Show or hide the edit/remove buttons if the field has a file or not.
......
......@@ -7,6 +7,7 @@
(function ($) {
namespace('Drupal.media.browser.views');
Drupal.behaviors.mediaViews = {
attach: function (context, settings) {
......@@ -15,34 +16,106 @@ Drupal.behaviors.mediaViews = {
return false;
});
// Catch the click on a media item
$('.view-content .media-item').bind('click', function () {
// Remove all currently selected files
$('.view-content .media-item').removeClass('selected');
// Set the current item to active
$(this).addClass('selected');
// Add this FID to the array of selected files
var fid = $(this).closest('a[data-fid]').data('fid');
// Because the files are added using drupal_add_js() and due to the fact
// that drupal_get_js() runs a drupal_array_merge_deep() which re-numbers
// numeric key values, we have to search in Drupal.settings.media.files
// for the matching file ID rather than referencing it directly.
var selectedFiles = new Array();
for (index in Drupal.settings.media.files) {
if (Drupal.settings.media.files[index].fid == fid) {
selectedFiles.push(Drupal.settings.media.files[index]);
// If multiple tabs contains the same file, it will be present in the
// files-array multiple times, so we break out early so we don't have
// it in the selectedFiles array multiple times.
// This would interfer with multi-selection, so...
break;
// We loop through the views listed in Drupal.settings.media.browser.views
// and set them up individually.
var views_ids = Object.keys(Drupal.settings.media.browser.views);
for (var i = 0; i < views_ids.length; i++) {
var views_id = views_ids[i];
for (var j= 0; j < Drupal.settings.media.browser.views[views_id].length; j++) {
var views_display_id = Drupal.settings.media.browser.views[views_id][j]
, view = $('.view-id-' + views_id + '.view-display-id-' + views_display_id);
if (view.length) {
Drupal.media.browser.views.setup(view);
}
}
Drupal.media.browser.selectMedia(selectedFiles);
});
}
// We want to be able to reset state on tab-changes, so we bind on the
// 'select' event on the tabset
$('#media-browser-tabset').tabs({
select: function(e, ui) {
var view = $('.view', ui.panel);
if (view.length) {
Drupal.media.browser.views.select(view);
}
}
})
}
}
/**
* Event-function that is called with a view, when the tab containing that
* view is selected.
*/
Drupal.media.browser.views.select = function(view) {
// Reset the list of selected files
Drupal.media.browser.selectMedia([]);
// Reset all 'selected'-status.
$('.view-content .media-item', view).removeClass('selected');
}
/**
* Setup function. Called once for every Media Browser view.
*
* Sets up event-handlers for selecting items in the view.
*/
Drupal.media.browser.views.setup = function(view) {
// Catch the click on a media item
$('.view-content .media-item', view).bind('click', function () {
var fid = $(this).closest('a[data-fid]').data('fid')
, selectedFiles = new Array();
// Remove all currently selected files
$('.view-content .media-item', view).removeClass('selected');
// Mark it as selected
$(this).addClass('selected');
// Multiselect!
if (Drupal.settings.media.browser.params.multiselect) {
// Loop through the already selected files
for (index in Drupal.media.browser.selectedMedia) {
var currentFid = Drupal.media.browser.selectedMedia[index].fid;
// If the current file exists in the list of already selected
// files, we deselect instead of selecting
if (currentFid == fid) {
$(this).removeClass('selected');
// If we change the fid, the later matching won't
// add it back again because it can't find it.
fid = NaN;
// The previously selected file wasn't clicked, so we retain it
// as an active file
}
else {
// Add to list of already selected files
selectedFiles.push(Drupal.media.browser.selectedMedia[index]);
// Mark it as selected
$('.view-content *[data-fid=' + currentFid + '] .media-item', view).addClass('selected');
}
}
}
// Because the files are added using drupal_add_js() and due to the fact
// that drupal_get_js() runs a drupal_array_merge_deep() which re-numbers
// numeric key values, we have to search in Drupal.settings.media.files
// for the matching file ID rather than referencing it directly.
for (index in Drupal.settings.media.files) {
if (Drupal.settings.media.files[index].fid == fid) {
selectedFiles.push(Drupal.settings.media.files[index]);
// If multiple tabs contains the same file, it will be present in the
// files-array multiple times, so we break out early so we don't have
// it in the selectedFiles array multiple times.
// This would interfer with multi-selection, so...
break;
}
}
Drupal.media.browser.selectMedia(selectedFiles);
});
}
}(jQuery));
......@@ -89,6 +89,20 @@ function template_preprocess_media_views_view_media_browser(&$vars) {
// Add the files to JS so that they are accessible inside the browser
drupal_add_js(array('media' => array('files' => array_values($files))), 'setting');
// Add the browser parameters to the settings and that this display exists
drupal_add_js(array(
'media' => array(
'browser' => array(
'params' => media_get_browser_params(),
'views' => array(
$vars['view']->name => array(
$vars['view']->current_display
),
),
),
),
), 'setting');
// Add classes and wrappers from the style plugin.
$handler = $vars['view']->style_plugin;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment