root / drupal7 / sites / all / modules / media / js / media.browser.js @ 6ae446a4
1 |
/**
|
---|---|
2 |
* @file
|
3 |
* Provides default functions for the media browser
|
4 |
*/
|
5 |
|
6 |
(function ($) { |
7 |
namespace('Drupal.media.browser');
|
8 |
|
9 |
Drupal.media.browser.selectedMedia = []; |
10 |
Drupal.media.browser.mediaAdded = function () {}; |
11 |
Drupal.media.browser.selectionFinalized = function (selectedMedia) { |
12 |
// This is intended to be overridden if a callee wants to be triggered
|
13 |
// when the media selection is finalized from inside the browser.
|
14 |
// This is used for the file upload form for instance.
|
15 |
}; |
16 |
|
17 |
Drupal.behaviors.MediaBrowser = { |
18 |
attach: function (context) { |
19 |
if (Drupal.settings.media && Drupal.settings.media.selectedMedia) {
|
20 |
Drupal.media.browser.selectMedia(Drupal.settings.media.selectedMedia); |
21 |
// Fire a confirmation of some sort.
|
22 |
Drupal.media.browser.finalizeSelection(); |
23 |
} |
24 |
|
25 |
// Instantiate the tabs.
|
26 |
var showFunc = function(event, ui) { |
27 |
// Store index of the tab being activated.
|
28 |
if (parent_iframe = Drupal.media.browser.getParentIframe(window)) {
|
29 |
$(parent_iframe).attr('current_tab', $('#media-tabs-wrapper > ul > li.ui-state-active').index()); |
30 |
} |
31 |
}; |
32 |
|
33 |
var activeTab = Drupal.media.browser.tabFromHash();
|
34 |
|
35 |
$('#media-browser-tabset').once('MediaBrowser').tabs({ |
36 |
selected: activeTab, // jquery < 1.9 |
37 |
active: activeTab, // jquery >= 1.9 |
38 |
show: showFunc, // jquery ui < 1.8 |
39 |
activate: showFunc // jquery ui >= 1.8 |
40 |
}); |
41 |
|
42 |
$('.media-browser-tab').each( Drupal.media.browser.validateButtons ); |
43 |
} |
44 |
// Wait for additional params to be passed in.
|
45 |
}; |
46 |
|
47 |
Drupal.media.browser.getParentIframe = function (window) { |
48 |
var arrFrames = parent.document.getElementsByTagName("IFRAME"); |
49 |
|
50 |
for (var i = 0; i < arrFrames.length; i++) { |
51 |
if (arrFrames[i].contentWindow === window) {
|
52 |
return arrFrames[i];
|
53 |
} |
54 |
} |
55 |
} |
56 |
|
57 |
/**
|
58 |
* Get index of the active tab from window.location.hash
|
59 |
*/
|
60 |
Drupal.media.browser.tabFromHash = function () { |
61 |
if (parent_iframe = Drupal.media.browser.getParentIframe(window)) {
|
62 |
return $(parent_iframe).attr('current_tab'); |
63 |
} |
64 |
|
65 |
return 0; |
66 |
}; |
67 |
|
68 |
Drupal.media.browser.launch = function () { |
69 |
|
70 |
}; |
71 |
|
72 |
Drupal.media.browser.validateButtons = function() { |
73 |
// The media browser runs in an IFRAME. The Drupal.media.popups.mediaBrowser()
|
74 |
// function sets up the IFRAME and an "OK" button that is outside of the
|
75 |
// IFRAME, so that its click handlers can destroy the IFRAME while retaining
|
76 |
// information about what media items were selected. However, Drupal UI
|
77 |
// convention is to place all action buttons on the same "line" at the bottom
|
78 |
// of the form, so if the form within the IFRAME contains a "Submit" button or
|
79 |
// other action buttons, then the "OK" button will appear below the IFRAME
|
80 |
// which breaks this convention and is confusing to the user. Therefore, we
|
81 |
// add a "Submit" button inside the IFRAME, and have its click action trigger
|
82 |
// the click action of the corresponding "OK" button that is outside the
|
83 |
// IFRAME. media.css contains CSS rules that hide the outside buttons.
|
84 |
|
85 |
// If a submit button is present, another round-trip to the server is needed
|
86 |
// before the user's selection is finalized. For these cases, when the form's
|
87 |
// real Submit button is clicked, the server either returns another form for
|
88 |
// the user to fill out, or else a completion page that contains or sets the
|
89 |
// Drupal.media.browser.selectedMedia variable. If the latter, then
|
90 |
// Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad() auto-triggers the
|
91 |
// "OK" button action to finalize the selection and remove the IFRAME.
|
92 |
|
93 |
// We need to check for the fake submit button that is used on non-form based
|
94 |
// pane content. On these items we need to bind the clicks so that media can
|
95 |
// be selected or the window can be closed. This is still a hacky approach,
|
96 |
// but it is a step in the right direction.
|
97 |
|
98 |
$('a.button.fake-submit', this).once().bind('click', Drupal.media.browser.submit); |
99 |
}; |
100 |
|
101 |
Drupal.media.browser.submit = function () { |
102 |
// @see Drupal.media.browser.validateButtons().
|
103 |
var buttons = $(parent.window.document.body).find('#mediaBrowser').parent('.ui-dialog').find('.ui-dialog-buttonpane button'); |
104 |
buttons[0].click();
|
105 |
|
106 |
// Return false to prevent the fake link "click" from continuing.
|
107 |
return false; |
108 |
} |
109 |
|
110 |
Drupal.media.browser.selectMedia = function (selectedMedia) { |
111 |
Drupal.media.browser.selectedMedia = selectedMedia; |
112 |
}; |
113 |
|
114 |
Drupal.media.browser.selectMediaAndSubmit = function (selectedMedia) { |
115 |
Drupal.media.browser.selectedMedia = selectedMedia; |
116 |
Drupal.media.browser.submit(); |
117 |
}; |
118 |
|
119 |
Drupal.media.browser.finalizeSelection = function () { |
120 |
if (!Drupal.media.browser.selectedMedia) {
|
121 |
throw new exception(Drupal.t('Cannot continue, nothing selected')); |
122 |
} |
123 |
else {
|
124 |
Drupal.media.browser.selectionFinalized(Drupal.media.browser.selectedMedia); |
125 |
} |
126 |
}; |
127 |
|
128 |
}(jQuery)); |