root / drupal7 / sites / all / modules / media / js / plugins / media.views.js @ fc3d89c3
1 |
/**
|
---|---|
2 |
* @file
|
3 |
* Handles the JS for the views file browser.
|
4 |
*
|
5 |
* Note that this does not currently support multiple file selection
|
6 |
*/
|
7 |
|
8 |
(function ($) { |
9 |
|
10 |
namespace('Drupal.media.browser.views');
|
11 |
Drupal.behaviors.mediaViews = { |
12 |
attach: function (context, settings) { |
13 |
|
14 |
// Make sure when pressing enter on text inputs, the form isn't submitted
|
15 |
$('.ctools-auto-submit-full-form .views-exposed-form input:text, input:text.ctools-auto-submit', context) |
16 |
.filter(':not(.ctools-auto-submit-exclude)')
|
17 |
.bind('keydown keyup', function (e) { |
18 |
if(e.keyCode === 13) { |
19 |
e.stopImmediatePropagation(); |
20 |
e.preventDefault(); |
21 |
} |
22 |
}); |
23 |
// Disable the links on media items list
|
24 |
$('.view-content ul.media-list-thumbnails a').click(function() { |
25 |
return false; |
26 |
}); |
27 |
|
28 |
// Return focus to the correct part of the form.
|
29 |
$('.ctools-auto-submit-full-form .ctools-auto-submit-click', context).click(function () { |
30 |
settings.lastFocus = document.activeElement.id; |
31 |
|
32 |
// Add custom class to allow customize look and feel of the field while processing ajax
|
33 |
// This way user can have a better user expierence using the exposed filters
|
34 |
$(document.activeElement).addClass('media-ajaxing-disabled'); |
35 |
// Remove focus to the active element
|
36 |
$(document.activeElement).blur();
|
37 |
|
38 |
// Before go with ajax, suppress key events
|
39 |
$('body').bind('keydown keyup', suppressKeyEvents); |
40 |
}); |
41 |
if (settings.lastFocus) {
|
42 |
// Note, we just use each() so we can declare variables in a new scope.
|
43 |
$('#' + settings.lastFocus, context).each(function () { |
44 |
var $this = $(this), |
45 |
val = $this.val();
|
46 |
|
47 |
$this.focus();
|
48 |
|
49 |
// Clear and reset the value to put the cursor at the end.
|
50 |
$this.val(''); |
51 |
$this.val(val);
|
52 |
|
53 |
// After input recover focus, remove suppression of key events
|
54 |
$('body').unbind('keydown keyup', suppressKeyEvents); |
55 |
}); |
56 |
} |
57 |
|
58 |
// We loop through the views listed in Drupal.settings.media.browser.views
|
59 |
// and set them up individually.
|
60 |
var views_ids = [];
|
61 |
for(var key in Drupal.settings.media.browser.views){ |
62 |
views_ids.push(key); |
63 |
} |
64 |
|
65 |
for (var i = 0; i < views_ids.length; i++) { |
66 |
var views_id = views_ids[i];
|
67 |
for (var j= 0; j < Drupal.settings.media.browser.views[views_id].length; j++) { |
68 |
var views_display_id = Drupal.settings.media.browser.views[views_id][j],
|
69 |
view = $('.view-id-' + views_id + '.view-display-id-' + views_display_id); |
70 |
if (view.length) {
|
71 |
Drupal.media.browser.views.setup(view); |
72 |
} |
73 |
} |
74 |
} |
75 |
|
76 |
// Reset the state on tab-changes- bind on the 'select' event on the tabset
|
77 |
$('#media-browser-tabset').bind('tabsselect', function(event, ui) { |
78 |
var view = $('.view', ui.panel); |
79 |
if (view.length) {
|
80 |
Drupal.media.browser.views.select(view); |
81 |
} |
82 |
}); |
83 |
|
84 |
} |
85 |
} |
86 |
|
87 |
/**
|
88 |
* Event-function that is called with a view, when the tab containing that
|
89 |
* view is selected.
|
90 |
*/
|
91 |
Drupal.media.browser.views.select = function(view) { |
92 |
// Reset the list of selected files
|
93 |
Drupal.media.browser.selectMedia([]); |
94 |
|
95 |
// Reset all 'selected'-status.
|
96 |
$('.view-content .media-item', view).removeClass('selected'); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Setup function. Called once for every Media Browser view.
|
101 |
*
|
102 |
* Sets up event-handlers for selecting items in the view.
|
103 |
*/
|
104 |
Drupal.media.browser.views.setup = function(view) { |
105 |
// Ensure we only setup each view once..
|
106 |
if ($(view).hasClass('media-browser-views-processed')) { |
107 |
return;
|
108 |
} |
109 |
|
110 |
// Reset the list of selected files
|
111 |
Drupal.media.browser.selectMedia([]); |
112 |
|
113 |
// Catch double click to submit a single item.
|
114 |
$('.view-content .media-item', view).bind('dblclick', function () { |
115 |
var fid = $(this).closest('.media-item[data-fid]').data('fid'), |
116 |
selectedFiles = new Array();
|
117 |
|
118 |
// Remove all currently selected files
|
119 |
$('.view-content .media-item', view).removeClass('selected'); |
120 |
|
121 |
// Mark it as selected
|
122 |
$(this).addClass('selected'); |
123 |
|
124 |
// Because the files are added using drupal_add_js() and due to the fact
|
125 |
// that drupal_get_js() runs a drupal_array_merge_deep() which re-numbers
|
126 |
// numeric key values, we have to search in Drupal.settings.media.files
|
127 |
// for the matching file ID rather than referencing it directly.
|
128 |
for (index in Drupal.settings.media.files) { |
129 |
if (Drupal.settings.media.files[index].fid == fid) {
|
130 |
selectedFiles.push(Drupal.settings.media.files[index]); |
131 |
|
132 |
// If multiple tabs contains the same file, it will be present in the
|
133 |
// files-array multiple times, so we break out early so we don't have
|
134 |
// it in the selectedFiles array multiple times.
|
135 |
// This would interfer with multi-selection, so...
|
136 |
break;
|
137 |
} |
138 |
} |
139 |
Drupal.media.browser.selectMediaAndSubmit(selectedFiles); |
140 |
}); |
141 |
|
142 |
|
143 |
// Catch the click on a media item
|
144 |
$('.view-content .media-item', view).bind('click', function () { |
145 |
var fid = $(this).closest('.media-item[data-fid]').data('fid'), |
146 |
selectedFiles = new Array();
|
147 |
|
148 |
// Remove all currently selected files
|
149 |
$('.view-content .media-item', view).removeClass('selected'); |
150 |
|
151 |
// Mark it as selected
|
152 |
$(this).addClass('selected'); |
153 |
|
154 |
// Multiselect!
|
155 |
if (Drupal.settings.media.browser.params.multiselect) {
|
156 |
// Loop through the already selected files
|
157 |
for (index in Drupal.media.browser.selectedMedia) { |
158 |
var currentFid = Drupal.media.browser.selectedMedia[index].fid;
|
159 |
|
160 |
// If the current file exists in the list of already selected
|
161 |
// files, we deselect instead of selecting
|
162 |
if (currentFid == fid) {
|
163 |
$(this).removeClass('selected'); |
164 |
// If we change the fid, the later matching won't
|
165 |
// add it back again because it can't find it.
|
166 |
fid = NaN;
|
167 |
|
168 |
// The previously selected file wasn't clicked, so we retain it
|
169 |
// as an active file
|
170 |
} |
171 |
else {
|
172 |
// Add to list of already selected files
|
173 |
selectedFiles.push(Drupal.media.browser.selectedMedia[index]); |
174 |
|
175 |
// Mark it as selected
|
176 |
$('.view-content *[data-fid=' + currentFid + '].media-item', view).addClass('selected'); |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
// Because the files are added using drupal_add_js() and due to the fact
|
182 |
// that drupal_get_js() runs a drupal_array_merge_deep() which re-numbers
|
183 |
// numeric key values, we have to search in Drupal.settings.media.files
|
184 |
// for the matching file ID rather than referencing it directly.
|
185 |
for (index in Drupal.settings.media.files) { |
186 |
if (Drupal.settings.media.files[index].fid == fid) {
|
187 |
selectedFiles.push(Drupal.settings.media.files[index]); |
188 |
|
189 |
// If multiple tabs contains the same file, it will be present in the
|
190 |
// files-array multiple times, so we break out early so we don't have
|
191 |
// it in the selectedFiles array multiple times.
|
192 |
// This would interfer with multi-selection, so...
|
193 |
break;
|
194 |
} |
195 |
} |
196 |
Drupal.media.browser.selectMedia(selectedFiles); |
197 |
}); |
198 |
|
199 |
// Add the processed class, so we dont accidentally process the same element twice..
|
200 |
$(view).addClass('media-browser-views-processed'); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Helper callback to supress propagation and default behaviour of an event
|
205 |
*
|
206 |
* This function is used in this way to make private and accesible only for the current scope
|
207 |
*/
|
208 |
var suppressKeyEvents = function(e) { |
209 |
e.stopImmediatePropagation(); |
210 |
e.preventDefault(); |
211 |
} |
212 |
|
213 |
}(jQuery)); |