-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-lib-multiselect.patch
192 lines (182 loc) · 7.18 KB
/
media-lib-multiselect.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
diff --git a/js/media.js b/js/media.js
--- a/js/media.js 2013-09-09 15:29:17.778861898 +0100
+++ b/js/media.js 2013-09-09 15:11:56.601861915 +0100
@@ -33,7 +33,7 @@
}
// 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) {
@@ -48,17 +48,17 @@
previewField.html(mediaFile.preview);
previewField.addClass('preview-file-'+mediaFile.fid);
}, 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.
diff --git a/js/plugins/media.views.js b/js/plugins/media.views.js
--- a/js/plugins/media.views.js 2013-09-09 15:29:17.780861898 +0100
+++ b/js/plugins/media.views.js 2013-09-09 15:11:56.603861917 +0100
@@ -7,6 +7,7 @@
(function ($) {
+namespace('Drupal.media.browser.views');
Drupal.behaviors.mediaViews = {
attach: function (context, settings) {
@@ -15,28 +16,104 @@
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]);
+ // 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')
+ fid = NaN; // If we change the fid, the later matching won't
+ // add it back again because it can't find it.
+
+ // 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));
diff --git a/media.views.inc b/media.views.inc
--- a/media.views.inc 2013-09-09 15:29:17.790861898 +0100
+++ b/media.views.inc 2013-09-09 15:11:56.636861918 +0100
@@ -70,6 +70,20 @@
// 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;