root / drupal7 / sites / all / modules / media / js / media.browser.js @ e4215af7
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 |
// Keep keyboard focus from going to the browser chrome.
|
45 |
$('body', context).once(function () { |
46 |
$(window).bind('keydown', function (event) { |
47 |
if (event.keyCode === 9) { |
48 |
var tabbables = $(':tabbable'), |
49 |
first = tabbables.filter(':first'),
|
50 |
last = tabbables.filter(':last'),
|
51 |
new_event; |
52 |
if ((event.target === last[0] && !event.shiftKey) || (event.target === first[0] && event.shiftKey)) { |
53 |
// If we're at the end of the tab list, then send a keyboard event
|
54 |
// to the parent iframe.
|
55 |
if (parent_iframe = Drupal.media.browser.getParentIframe(window)) {
|
56 |
$('.ui-dialog-titlebar-close', $(parent_iframe).closest('.ui-dialog')).focus(); |
57 |
event.preventDefault(); |
58 |
return false; |
59 |
} |
60 |
} |
61 |
} |
62 |
}); |
63 |
}); |
64 |
} |
65 |
// Wait for additional params to be passed in.
|
66 |
}; |
67 |
|
68 |
Drupal.media.browser.getParentIframe = function (window) { |
69 |
var arrFrames = parent.document.getElementsByTagName("IFRAME"); |
70 |
|
71 |
for (var i = 0; i < arrFrames.length; i++) { |
72 |
if (arrFrames[i].contentWindow === window) {
|
73 |
return arrFrames[i];
|
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
/**
|
79 |
* Get index of the active tab from window.location.hash
|
80 |
*/
|
81 |
Drupal.media.browser.tabFromHash = function () { |
82 |
if (parent_iframe = Drupal.media.browser.getParentIframe(window)) {
|
83 |
return $(parent_iframe).attr('current_tab'); |
84 |
} |
85 |
|
86 |
return 0; |
87 |
}; |
88 |
|
89 |
Drupal.media.browser.launch = function () { |
90 |
|
91 |
}; |
92 |
|
93 |
Drupal.media.browser.validateButtons = function() { |
94 |
// The media browser runs in an IFRAME. The Drupal.media.popups.mediaBrowser()
|
95 |
// function sets up the IFRAME and an "OK" button that is outside of the
|
96 |
// IFRAME, so that its click handlers can destroy the IFRAME while retaining
|
97 |
// information about what media items were selected. However, Drupal UI
|
98 |
// convention is to place all action buttons on the same "line" at the bottom
|
99 |
// of the form, so if the form within the IFRAME contains a "Submit" button or
|
100 |
// other action buttons, then the "OK" button will appear below the IFRAME
|
101 |
// which breaks this convention and is confusing to the user. Therefore, we
|
102 |
// add a "Submit" button inside the IFRAME, and have its click action trigger
|
103 |
// the click action of the corresponding "OK" button that is outside the
|
104 |
// IFRAME. media.css contains CSS rules that hide the outside buttons.
|
105 |
|
106 |
// If a submit button is present, another round-trip to the server is needed
|
107 |
// before the user's selection is finalized. For these cases, when the form's
|
108 |
// real Submit button is clicked, the server either returns another form for
|
109 |
// the user to fill out, or else a completion page that contains or sets the
|
110 |
// Drupal.media.browser.selectedMedia variable. If the latter, then
|
111 |
// Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad() auto-triggers the
|
112 |
// "OK" button action to finalize the selection and remove the IFRAME.
|
113 |
|
114 |
// We need to check for the fake submit button that is used on non-form based
|
115 |
// pane content. On these items we need to bind the clicks so that media can
|
116 |
// be selected or the window can be closed. This is still a hacky approach,
|
117 |
// but it is a step in the right direction.
|
118 |
|
119 |
$('a.button.fake-submit', this).once().bind('click', Drupal.media.browser.submit); |
120 |
}; |
121 |
|
122 |
Drupal.media.browser.submit = function () { |
123 |
// @see Drupal.media.browser.validateButtons().
|
124 |
var buttons = $(parent.window.document.body).find('#mediaBrowser').parent('.ui-dialog').find('.ui-dialog-buttonpane button'); |
125 |
buttons[0].click();
|
126 |
|
127 |
// Return false to prevent the fake link "click" from continuing.
|
128 |
return false; |
129 |
} |
130 |
|
131 |
Drupal.media.browser.selectMedia = function (selectedMedia) { |
132 |
Drupal.media.browser.selectedMedia = selectedMedia; |
133 |
}; |
134 |
|
135 |
Drupal.media.browser.selectMediaAndSubmit = function (selectedMedia) { |
136 |
Drupal.media.browser.selectedMedia = selectedMedia; |
137 |
Drupal.media.browser.submit(); |
138 |
}; |
139 |
|
140 |
Drupal.media.browser.finalizeSelection = function () { |
141 |
if (!Drupal.media.browser.selectedMedia) {
|
142 |
throw new exception(Drupal.t('Cannot continue, nothing selected')); |
143 |
} |
144 |
else {
|
145 |
Drupal.media.browser.selectionFinalized(Drupal.media.browser.selectedMedia); |
146 |
} |
147 |
}; |
148 |
|
149 |
}(jQuery)); |