franck@0
|
1 // $Id: popups.js,v 1.9.8.12 2009/03/21 00:57:15 starbow Exp $ |
franck@0
|
2 |
franck@0
|
3 /** |
franck@0
|
4 * Popup Modal Dialog API |
franck@0
|
5 * |
franck@0
|
6 * Provide an API for building and displaying JavaScript, in-page, popups modal dialogs. |
franck@0
|
7 * Modality is provided by a fixed, semi-opaque div, positioned in front of the page contents. |
franck@0
|
8 * |
franck@0
|
9 */ |
franck@0
|
10 |
franck@0
|
11 /* |
franck@0
|
12 * TODO |
franck@0
|
13 * * Return key in add node form not working. |
franck@0
|
14 * * Tabledrag breaking after ahah reload. |
franck@0
|
15 */ |
franck@0
|
16 |
franck@0
|
17 // *************************************************************************** |
franck@0
|
18 // DRUPAL Namespace |
franck@0
|
19 // *************************************************************************** |
franck@0
|
20 |
franck@0
|
21 /** |
franck@0
|
22 * Attach the popups bevior to the all the requested links on the page. |
franck@0
|
23 * |
franck@0
|
24 * @param context |
franck@0
|
25 * The jQuery object to apply the behaviors to. |
franck@0
|
26 */ |
franck@0
|
27 |
franck@0
|
28 Drupal.behaviors.popups = function(context) { |
franck@0
|
29 Popups.saveSettings(); |
franck@0
|
30 |
franck@0
|
31 var $body = $('body'); |
franck@0
|
32 if(!$body.hasClass('popups-processed')) { |
franck@0
|
33 $body.addClass('popups-processed'); |
franck@0
|
34 $(document).bind('keydown', Popups.keyHandle); |
franck@0
|
35 var $popit = $('#popit'); |
franck@0
|
36 if ($popit.length) { |
franck@0
|
37 $popit.remove(); |
franck@0
|
38 Popups.message($popit.html()); |
franck@0
|
39 } |
franck@0
|
40 } |
franck@0
|
41 |
franck@0
|
42 // Add the popups-link-in-dialog behavior to links defined in Drupal.settings.popups.links array. |
franck@0
|
43 // Get these from current Drupal.settings, not Popups.originalSettings, as each page has it's own hooks. |
franck@0
|
44 if (Drupal.settings.popups && Drupal.settings.popups.links) { |
franck@0
|
45 jQuery.each(Drupal.settings.popups.links, function (link, options) { |
franck@0
|
46 Popups.attach(context, link, Popups.options(options)); |
franck@0
|
47 }); |
franck@0
|
48 } |
franck@0
|
49 |
franck@0
|
50 Popups.attach(context, '.popups', Popups.options({updateMethod: 'none'})); |
franck@0
|
51 Popups.attach(context, '.popups-form', Popups.options({updateMethod: 'ajax'})); // ajax reload. |
franck@0
|
52 Popups.attach(context, '.popups-form-reload', Popups.options({updateMethod: 'reload'})); // whole page reload. |
franck@0
|
53 Popups.attach(context, '.popups-form-noupdate', Popups.options({updateMethod: 'none'})); // no reload at all. |
franck@0
|
54 }; |
franck@0
|
55 |
franck@0
|
56 // *************************************************************************** |
franck@0
|
57 // Popups Namespace ********************************************************** |
franck@0
|
58 // *************************************************************************** |
franck@0
|
59 /** |
franck@0
|
60 * The Popups namespace contains: |
franck@0
|
61 * * An ordered stack of Popup objects, |
franck@0
|
62 * * The state of the original page, |
franck@0
|
63 * * Functions for managing both of the above. |
franck@0
|
64 */ |
franck@0
|
65 Popups = function(){}; |
franck@0
|
66 |
franck@0
|
67 /** |
franck@0
|
68 * Static variables in the Popups namespace. |
franck@0
|
69 */ |
franck@0
|
70 Popups.popupStack = []; |
franck@0
|
71 Popups.addedCSS = []; |
franck@0
|
72 Popups.addedJS = []; |
franck@0
|
73 Popups.originalSettings = null; // The initial popup options of the page. |
franck@0
|
74 /** |
franck@0
|
75 * Each popup object gets it's own set of options. |
franck@0
|
76 * These are the defaults. |
franck@0
|
77 */ |
franck@0
|
78 Popups.defaultOptions = { |
franck@0
|
79 doneTest: null, // null, *path*, *regexp*. how do we know when a multiform flow is done? |
franck@0
|
80 updateMethod: 'ajax', // none, ajax, reload, *callback* |
franck@0
|
81 updateSource: 'initial', // initial, final. Only used if updateMethod != none. |
franck@0
|
82 href: null, |
franck@0
|
83 width: null, // Override the width specified in the css. |
franck@0
|
84 targetSelectors: null, // Hash of jQuery selectors that define the content to be swapped out. |
franck@0
|
85 titleSelectors: null, // Array of jQuery selectors to place the new page title. |
franck@0
|
86 reloadOnError: false, // Force the entire page to reload if the popup href is unaccessable. |
franck@0
|
87 noMessage: false, // Don't show drupal_set_message messages. |
franck@0
|
88 skipDirtyCheck: false, // If true, this popup will not check for edits on the originating page. |
franck@0
|
89 hijackDestination: true // Use the destiination param to force a form submit to return to the originating page. |
franck@0
|
90 }; |
franck@0
|
91 |
franck@0
|
92 // *************************************************************************** |
franck@0
|
93 // Popups.Popup Object ******************************************************* |
franck@0
|
94 // *************************************************************************** |
franck@0
|
95 /** |
franck@0
|
96 * A Popup is a single modal dialog. |
franck@0
|
97 * The popup object encapslated all the info about a single popup. |
franck@0
|
98 */ |
franck@0
|
99 Popups.Popup = function() { |
franck@0
|
100 this.id = 'popups-' + Popups.nextCounter(); |
franck@0
|
101 |
franck@0
|
102 // These properties are needed if the popup contains a form that will be ajax submitted. |
franck@0
|
103 this.parent = null; // The popup that spawned this one. If parent is null, this popup was spawned by the original page. |
franck@0
|
104 this.path = null; // If popup is showing content from a url, this is that path. |
franck@0
|
105 this.element = null; // The DOM element that was clicked to launch this popup. |
franck@0
|
106 this.options = null; // An option array that control how the popup behaves. See Popups.defaultOptions for explainations. |
franck@0
|
107 }; |
franck@0
|
108 Popups.Popup.prototype.$popup = function() { |
franck@0
|
109 return $('#' + this.id); |
franck@0
|
110 }; |
franck@0
|
111 Popups.Popup.prototype.$popupBody = function() { |
franck@0
|
112 return $('#' + this.id + ' .popups-body'); |
franck@0
|
113 }; |
franck@0
|
114 Popups.Popup.prototype.$popupClose = function() { |
franck@0
|
115 return $('#' + this.id + ' .popups-close'); |
franck@0
|
116 }; |
franck@0
|
117 Popups.Popup.prototype.$popupTitle = function() { |
franck@0
|
118 return $('#' + this.id + ' .popups-title'); |
franck@0
|
119 }; |
franck@0
|
120 Popups.Popup.prototype.$popupButtons = function() { |
franck@0
|
121 return $('#' + this.id + ' .popups-buttons'); |
franck@0
|
122 }; |
franck@0
|
123 Popups.Popup.prototype.$popupFooter = function() { |
franck@0
|
124 return $('#' + this.id + ' .popups-footer'); |
franck@0
|
125 }; |
franck@0
|
126 |
franck@0
|
127 /** |
franck@0
|
128 * Create the jQuery wrapped html at the heart of the popup object. |
franck@0
|
129 * |
franck@0
|
130 * @param title |
franck@0
|
131 * String |
franck@0
|
132 * @param body |
franck@0
|
133 * String/HTML |
franck@0
|
134 * @param buttons |
franck@0
|
135 * Hash/Object |
franck@0
|
136 * @return |
franck@0
|
137 * The $popup. |
franck@0
|
138 */ |
franck@0
|
139 Popups.Popup.prototype.fill = function(title, body, buttons) { |
franck@0
|
140 return $(Drupal.theme('popupDialog', this.id, title, body, buttons)); |
franck@0
|
141 } |
franck@0
|
142 |
franck@0
|
143 /** |
franck@0
|
144 * Hide the popup by pushing it off to the side. |
franck@0
|
145 * Just making it display:none causes flash in FF2. |
franck@0
|
146 */ |
franck@0
|
147 Popups.Popup.prototype.hide = function() { |
franck@0
|
148 this.$popup().css('left', '-9999px'); |
franck@0
|
149 }; |
franck@0
|
150 |
franck@0
|
151 Popups.Popup.prototype.show = function() { |
franck@0
|
152 Popups.resizeAndCenter(this); |
franck@0
|
153 }; |
franck@0
|
154 |
franck@0
|
155 Popups.Popup.prototype.open = function(title, body, buttons, width){ |
franck@0
|
156 return Popups.open(this, title, body, buttons, width); |
franck@0
|
157 }; |
franck@0
|
158 |
franck@0
|
159 Popups.Popup.prototype.removePopup = function() { |
franck@0
|
160 Popups.removePopup(this); |
franck@0
|
161 }; |
franck@0
|
162 |
franck@0
|
163 /** |
franck@0
|
164 * Remove everything. |
franck@0
|
165 */ |
franck@0
|
166 Popups.Popup.prototype.close = function() { |
franck@0
|
167 return Popups.close(this); |
franck@0
|
168 }; |
franck@0
|
169 |
franck@0
|
170 /** |
franck@0
|
171 * Set the focus on the popups to the first visible, enabled form element, or the close link. |
franck@0
|
172 */ |
franck@0
|
173 Popups.Popup.prototype.refocus = function() { |
franck@0
|
174 // Select the first visible enabled input element. |
franck@0
|
175 var $popup = this.$popup(); |
franck@0
|
176 var $focus = $popup.find(':input:visible:enabled:first'); |
franck@0
|
177 if (!$focus.length) { |
franck@0
|
178 // There is no visible enabled input element, so select the close link. |
franck@0
|
179 $focus = $popup.find('.popups-close a'); |
franck@0
|
180 } |
franck@0
|
181 $focus.focus(); |
franck@0
|
182 }; |
franck@0
|
183 |
franck@0
|
184 /** |
franck@0
|
185 * Return a selector that will find target content on the layer that spawned this popup. |
franck@0
|
186 * This is needed for the popup to do ajax updates. |
franck@0
|
187 */ |
franck@0
|
188 Popups.Popup.prototype.targetLayerSelector = function() { |
franck@0
|
189 if (this.parent === null) { |
franck@0
|
190 return 'body'; // Select content in the original page. |
franck@0
|
191 } |
franck@0
|
192 else { |
franck@0
|
193 return '#' + this.parent.id; // Select content in the parent popup. |
franck@0
|
194 } |
franck@0
|
195 }; |
franck@0
|
196 |
franck@0
|
197 /** |
franck@0
|
198 * Determine if we are at an end point of a form flow, or just moving from one popups to another. |
franck@0
|
199 * |
franck@0
|
200 * @param path |
franck@0
|
201 * The path of the page that the form flow has moved to. |
franck@0
|
202 * This path is relative to the base_path. |
franck@0
|
203 * Ex: node/add/story, not http://localhost/drupal6/node/add/story or drupa6/node/add/story. |
franck@0
|
204 * @return bool |
franck@0
|
205 */ |
franck@0
|
206 Popups.Popup.prototype.isDone = function(path) { |
franck@0
|
207 // console.log("Doing isDone for popup: " + this.id + ", now at " + path ); |
franck@0
|
208 var done; |
franck@0
|
209 if (this.options.doneTest) { |
franck@0
|
210 // Test if we are at the path specified by doneTest. |
franck@0
|
211 done = (path === this.options.doneTest || path.match(this.options.doneTest)); |
franck@0
|
212 } |
franck@0
|
213 else { |
franck@0
|
214 if (this.parent) { |
franck@0
|
215 // Test if we are back to the parent popup's path. |
franck@0
|
216 done = (path === this.parent.path); |
franck@0
|
217 // console.log("Lookin at parent: " + this.parent.path + ". Done = " + done); |
franck@0
|
218 } |
franck@0
|
219 else { |
franck@0
|
220 // Test if we are back to the original page's path. |
franck@0
|
221 done = (path === Popups.originalSettings.popups.originalPath); |
franck@0
|
222 // console.log("Lookin at original page: " + Popups.originalSettings.popups.originalPath + ". Done = " + done); |
franck@0
|
223 } |
franck@0
|
224 }; |
franck@0
|
225 return done; |
franck@0
|
226 }; |
franck@0
|
227 |
franck@0
|
228 |
franck@0
|
229 // *************************************************************************** |
franck@0
|
230 // Popups Functions ********************************************************** |
franck@0
|
231 // *************************************************************************** |
franck@0
|
232 |
franck@0
|
233 /** |
franck@0
|
234 * Test if the param has been set. |
franck@0
|
235 * Used to distinguish between a value set to null or false and on not yet unset. |
franck@0
|
236 */ |
franck@0
|
237 Popups.isset = function(v) { |
franck@0
|
238 return (typeof(v) !== 'undefined'); |
franck@0
|
239 }; |
franck@0
|
240 |
franck@0
|
241 /** |
franck@0
|
242 * Get the currently active popup in the page. |
franck@0
|
243 * Currently it is the only one visible, but that could change. |
franck@0
|
244 */ |
franck@0
|
245 Popups.activePopup = function() { |
franck@0
|
246 if (Popups.popupStack.length) { |
franck@0
|
247 return Popups.popupStack[Popups.popupStack.length - 1]; // top of stack. |
franck@0
|
248 } |
franck@0
|
249 else { |
franck@0
|
250 return null; |
franck@0
|
251 } |
franck@0
|
252 }; |
franck@0
|
253 |
franck@0
|
254 /** |
franck@0
|
255 * Manage the page wide popupStack. |
franck@0
|
256 */ |
franck@0
|
257 Popups.push = function(popup) { |
franck@0
|
258 Popups.popupStack.push(popup); |
franck@0
|
259 }; |
franck@0
|
260 // Should I integrate this with popupRemove?? |
franck@0
|
261 Popups.pop = function(popup) { |
franck@0
|
262 return Popups.popupStack.pop(); |
franck@0
|
263 }; |
franck@0
|
264 |
franck@0
|
265 /** |
franck@0
|
266 * Build an options hash from defaults. |
franck@0
|
267 * |
franck@0
|
268 * @param overrides |
franck@0
|
269 * Hash of values to override the defaults. |
franck@0
|
270 */ |
franck@0
|
271 Popups.options = function(overrides) { |
franck@0
|
272 var defaults = Popups.defaultOptions; |
franck@0
|
273 return Popups.overrideOptions(defaults, overrides); |
franck@0
|
274 } |
franck@0
|
275 |
franck@0
|
276 /** |
franck@0
|
277 * Build an options hash. |
franck@0
|
278 * Also maps deprecated options to current options. |
franck@0
|
279 * |
franck@0
|
280 * @param defaults |
franck@0
|
281 * Hash of default values |
franck@0
|
282 * @param overrides |
franck@0
|
283 * Hash of values to override the defaults with. |
franck@0
|
284 */ |
franck@0
|
285 Popups.overrideOptions = function(defaults, overrides) { |
franck@0
|
286 var options = {}; |
franck@0
|
287 for(var option in defaults) { |
franck@0
|
288 var value; |
franck@0
|
289 if (Popups.isset(overrides[option])) { |
franck@0
|
290 options[option] = overrides[option]; |
franck@0
|
291 } |
franck@0
|
292 else { |
franck@0
|
293 options[option] = defaults[option]; |
franck@0
|
294 } |
franck@0
|
295 } |
franck@0
|
296 // Map deprecated options. |
franck@0
|
297 if (overrides['noReload'] || overrides['noUpdate']) { |
franck@0
|
298 options['updateMethod'] = 'none'; |
franck@0
|
299 } |
franck@0
|
300 if (overrides['reloadWhenDone']) { |
franck@0
|
301 options['updateMethod'] = 'reload'; |
franck@0
|
302 } |
franck@0
|
303 if (overrides['afterSubmit']) { |
franck@0
|
304 options['updateMethod'] = 'callback'; |
franck@0
|
305 options['onUpdate'] = overrides['afterSubmit']; |
franck@0
|
306 } |
franck@0
|
307 if (overrides['forceReturn']) { |
franck@0
|
308 options['doneTest'] = overrides['forceReturn']; |
franck@0
|
309 } |
franck@0
|
310 return options; |
franck@0
|
311 } |
franck@0
|
312 |
franck@0
|
313 /** |
franck@0
|
314 * Attach the popups behavior to all elements inside the context that match the selector. |
franck@0
|
315 * |
franck@0
|
316 * @param context |
franck@0
|
317 * Chunk of html to search. |
franck@0
|
318 * @param selector |
franck@0
|
319 * jQuery selector for elements to attach popups behavior to. |
franck@0
|
320 * @param options |
franck@0
|
321 * Hash of options associated with these links. |
franck@0
|
322 */ |
franck@0
|
323 Popups.attach = function(context, selector, options) { |
franck@0
|
324 // console.log(options); |
franck@0
|
325 $(selector, context).not('.popups-processed').each(function() { |
franck@0
|
326 var $element = $(this); |
franck@0
|
327 |
franck@0
|
328 // Mark the element as processed. |
franck@0
|
329 $element.addClass('popups-processed'); |
franck@0
|
330 |
franck@0
|
331 // Append note to link title. |
franck@0
|
332 var title = ''; |
franck@0
|
333 if ($element.attr('title')) { |
franck@0
|
334 title = $element.attr('title') + ' '; |
franck@0
|
335 } |
franck@0
|
336 title += Drupal.t('[Popup]'); |
franck@0
|
337 $element.attr('title', title); |
franck@0
|
338 |
franck@0
|
339 // Attach the on-click popup behavior to the element. |
franck@0
|
340 $element.click(function(event){ |
franck@0
|
341 return Popups.clickPopupElement(this, options); |
franck@0
|
342 }); |
franck@0
|
343 }); |
franck@0
|
344 }; |
franck@0
|
345 |
franck@0
|
346 /** |
franck@0
|
347 * Respond to click by opening a popup. |
franck@0
|
348 * |
franck@0
|
349 * @param element |
franck@0
|
350 * The element that was clicked. |
franck@0
|
351 * @param options |
franck@0
|
352 * Hash of options associated with the element. |
franck@0
|
353 */ |
franck@0
|
354 Popups.clickPopupElement = function(element, options) { |
franck@0
|
355 Popups.saveSettings(); |
franck@0
|
356 |
franck@0
|
357 // If the element contains a on-popups-options attribute, override default options param. |
franck@0
|
358 if ($(element).attr('on-popups-options')) { |
franck@0
|
359 var overrides = Drupal.parseJson($(element).attr('on-popups-options')); |
franck@0
|
360 options = Popups.overrideOptions(options, overrides); |
franck@0
|
361 } |
franck@0
|
362 |
franck@0
|
363 // The parent of the new popup is the currently active popup. |
franck@0
|
364 var parent = Popups.activePopup(); |
franck@0
|
365 |
franck@0
|
366 // If the option is distructive, check if the page is already modified, and offer to save. |
franck@0
|
367 var willModifyOriginal = !(options.updateMethod === 'none' || options.skipDirtyCheck); |
franck@0
|
368 if (willModifyOriginal && Popups.activeLayerIsEdited()) { |
franck@0
|
369 // The user will lose modifications, so show dialog offering to save current state. |
franck@0
|
370 Popups.offerToSave(element, options, parent); |
franck@0
|
371 } |
franck@0
|
372 else { |
franck@0
|
373 // Page is clean, or popup is safe, so just open it. |
franck@0
|
374 Popups.openPath(element, options, parent); |
franck@0
|
375 } |
franck@0
|
376 return false; |
franck@0
|
377 }; |
franck@0
|
378 |
franck@0
|
379 /** |
franck@0
|
380 * Test if the active layer been edited. |
franck@0
|
381 * Active layer is either the original page, or the active Popup. |
franck@0
|
382 */ |
franck@0
|
383 Popups.activeLayerIsEdited = function() { |
franck@0
|
384 var layer = Popups.activePopup(); |
franck@0
|
385 var $context = Popups.getLayerContext(layer); |
franck@0
|
386 // TODO: better test for edited page, maybe capture change event on :inputs. |
franck@0
|
387 var edited = $context.find('span.tabledrag-changed').length; |
franck@0
|
388 return edited; |
franck@0
|
389 } |
franck@0
|
390 |
franck@0
|
391 /** |
franck@0
|
392 * Show dialog offering to save form on parent layer. |
franck@0
|
393 * |
franck@0
|
394 * @param element |
franck@0
|
395 * The DOM element that was clicked. |
franck@0
|
396 * @param options |
franck@0
|
397 * The options associated with that element. |
franck@0
|
398 * @param parent |
franck@0
|
399 * The layer that has the unsaved edits. Null means the underlying page. |
franck@0
|
400 */ |
franck@0
|
401 Popups.offerToSave = function(element, options, parent) { |
franck@0
|
402 var popup = new Popups.Popup(); |
franck@0
|
403 var body = Drupal.t("There are unsaved changes in the form, which you will lose if you continue."); |
franck@0
|
404 var buttons = { |
franck@0
|
405 'popup_save': {title: Drupal.t('Save Changes'), func: function(){Popups.saveFormOnLayer(element, options, parent);}}, |
franck@0
|
406 'popup_submit': {title: Drupal.t('Continue'), func: function(){popup.removePopup(); Popups.openPath(element, options, parent);}}, |
franck@0
|
407 'popup_cancel': {title: Drupal.t('Cancel'), func: function(){popup.close();}} |
franck@0
|
408 }; |
franck@0
|
409 popup.open(Drupal.t('Warning: Please Confirm'), body, buttons); |
franck@0
|
410 }; |
franck@0
|
411 |
franck@0
|
412 /** |
franck@0
|
413 * Generic dialog builder. |
franck@0
|
414 * Adds the newly built popup into the DOM. |
franck@0
|
415 * |
franck@0
|
416 * TODO: capture the focus if it tabs out of the dialog. |
franck@0
|
417 * |
franck@0
|
418 * @param popup |
franck@0
|
419 * Popups.Popup object to fill with content, place in the DOM, and show on the screen. |
franck@0
|
420 * @param String title |
franck@0
|
421 * String: title of new dialog. |
franck@0
|
422 * @param body (optional) |
franck@0
|
423 * String: body of new dialog. |
franck@0
|
424 * @param buttons (optional) |
franck@0
|
425 * Hash of button parameters. |
franck@0
|
426 * @param width (optional) |
franck@0
|
427 * Width of new dialog. |
franck@0
|
428 * |
franck@0
|
429 * @return popup object |
franck@0
|
430 */ |
franck@0
|
431 Popups.open = function(popup, title, body, buttons, width){ |
franck@0
|
432 Popups.addOverlay(); |
franck@0
|
433 |
franck@0
|
434 if (Popups.activePopup()) { |
franck@0
|
435 // Hiding previously active popup. |
franck@0
|
436 Popups.activePopup().hide(); |
franck@0
|
437 } |
franck@0
|
438 |
franck@0
|
439 if (!popup) { |
franck@0
|
440 // Popup object was not handed in, so create a new one. |
franck@0
|
441 popup = new Popups.Popup(); |
franck@0
|
442 } |
franck@0
|
443 Popups.push(popup); // Put this popup at the top of the stack. |
franck@0
|
444 |
franck@0
|
445 // Create the jQuery wrapped html for the new popup. |
franck@0
|
446 var $popup = popup.fill(title, body, buttons); |
franck@0
|
447 popup.hide(); // Hide the new popup until it is finished and sized. |
franck@0
|
448 |
franck@0
|
449 if (width) { |
franck@0
|
450 $popup.css('width', width); |
franck@0
|
451 } |
franck@0
|
452 |
franck@0
|
453 // Add the new popup to the DOM. |
franck@0
|
454 $('body').append($popup); |
franck@0
|
455 |
franck@0
|
456 // Add button function callbacks. |
franck@0
|
457 if (buttons) { |
franck@0
|
458 jQuery.each(buttons, function(id, button){ |
franck@0
|
459 $('#' + id).click(button.func); |
franck@0
|
460 }); |
franck@0
|
461 } |
franck@0
|
462 |
franck@0
|
463 // Add the default click-to-close behavior. |
franck@0
|
464 popup.$popupClose().click(function(){ |
franck@0
|
465 return Popups.close(popup); |
franck@0
|
466 }); |
franck@0
|
467 |
franck@0
|
468 Popups.resizeAndCenter(popup); |
franck@0
|
469 |
franck@0
|
470 // Focus on the first input element in the popup window. |
franck@0
|
471 popup.refocus(); |
franck@0
|
472 |
franck@0
|
473 // TODO - this isn't the place for this - should mirror addLoading calls. |
franck@0
|
474 // Remove the loading image. |
franck@0
|
475 Popups.removeLoading(); |
franck@0
|
476 |
franck@0
|
477 return popup; |
franck@0
|
478 }; |
franck@0
|
479 |
franck@0
|
480 /** |
franck@0
|
481 * Adjust the popup's height to fit it's content. |
franck@0
|
482 * Move it to be centered on the screen. |
franck@0
|
483 * This undoes the effects of popup.hide(). |
franck@0
|
484 * |
franck@0
|
485 * @param popup |
franck@0
|
486 */ |
franck@0
|
487 Popups.resizeAndCenter = function(popup) { |
franck@0
|
488 var $popup = popup.$popup(); |
franck@0
|
489 |
franck@0
|
490 // center on the screen, adding in offsets if the window has been scrolled |
franck@0
|
491 var popupWidth = $popup.width(); |
franck@0
|
492 var windowWidth = Popups.windowWidth(); |
franck@0
|
493 var left = (windowWidth / 2) - (popupWidth / 2) + Popups.scrollLeft(); |
franck@0
|
494 |
franck@0
|
495 // Get popups's height on the page. |
franck@0
|
496 $popup.css('height', 'auto'); // Reset height. |
franck@0
|
497 var popupHeight = $popup.height(); |
franck@0
|
498 $popup.height(popupHeight); |
franck@0
|
499 var windowHeight = Popups.windowHeight(); |
franck@0
|
500 |
franck@0
|
501 if (popupHeight > (0.9 * windowHeight) ) { // Must fit in 90% of window. |
franck@0
|
502 popupHeight = 0.9 * windowHeight; |
franck@0
|
503 $popup.height(popupHeight); |
franck@0
|
504 } |
franck@0
|
505 var top = (windowHeight / 2) - (popupHeight / 2) + Popups.scrollTop(); |
franck@0
|
506 |
franck@0
|
507 $popup.css('top', top).css('left', left); // Position the popups to be visible. |
franck@0
|
508 }; |
franck@0
|
509 |
franck@0
|
510 |
franck@0
|
511 /** |
franck@0
|
512 * Create and show a simple popup dialog that functions like the browser's alert box. |
franck@0
|
513 */ |
franck@0
|
514 Popups.message = function(title, message) { |
franck@0
|
515 message = message || ''; |
franck@0
|
516 var popup = new Popups.Popup(); |
franck@0
|
517 var buttons = { |
franck@0
|
518 'popup_ok': {title: Drupal.t('OK'), func: function(){popup.close();}} |
franck@0
|
519 }; |
franck@0
|
520 popup.open(title, message, buttons); |
franck@0
|
521 return popup; |
franck@0
|
522 }; |
franck@0
|
523 |
franck@0
|
524 /** |
franck@0
|
525 * Handle any special keys when popups is active. |
franck@0
|
526 */ |
franck@0
|
527 Popups.keyHandle = function(e) { |
franck@0
|
528 if (!e) { |
franck@0
|
529 e = window.event; |
franck@0
|
530 } |
franck@0
|
531 switch (e.keyCode) { |
franck@0
|
532 case 27: // esc |
franck@0
|
533 Popups.close(); |
franck@0
|
534 break; |
franck@0
|
535 case 191: // '?' key, show help. |
franck@0
|
536 if (e.shiftKey && e.ctrlKey) { |
franck@0
|
537 var $help = $('a.popups.more-help'); |
franck@0
|
538 if ($help.size()) { |
franck@0
|
539 $help.click(); |
franck@0
|
540 } |
franck@0
|
541 else { |
franck@0
|
542 Popups.message(Drupal.t("Sorry, there is no additional help for this page")); |
franck@0
|
543 } |
franck@0
|
544 } |
franck@0
|
545 break; |
franck@0
|
546 } |
franck@0
|
547 }; |
franck@0
|
548 |
franck@0
|
549 /***************************************************************************** |
franck@0
|
550 * Appearence Functions (overlay, loading graphic, remove popups) ********* |
franck@0
|
551 *****************************************************************************/ |
franck@0
|
552 |
franck@0
|
553 /** |
franck@0
|
554 * Add full page div between the page and the dialog, to make the popup modal. |
franck@0
|
555 */ |
franck@0
|
556 Popups.addOverlay = function() { |
franck@0
|
557 var $overlay = $('#popups-overlay'); |
franck@0
|
558 if (!$overlay.length) { // Overlay does not already exist, so create it. |
franck@0
|
559 $overlay = $(Drupal.theme('popupOverlay')); |
franck@0
|
560 $overlay.css('opacity', '0.4'); // for ie6(?) |
franck@0
|
561 // Doing absolute positioning, so make overlay's size equal the entire body. |
franck@0
|
562 var $doc = $(document); |
franck@0
|
563 $overlay.width($doc.width()).height($doc.height()); |
franck@0
|
564 $overlay.click(function(){Popups.close();}); |
franck@0
|
565 $('body').prepend($overlay); |
franck@0
|
566 } |
franck@0
|
567 }; |
franck@0
|
568 |
franck@0
|
569 /** |
franck@0
|
570 * Remove overlay if popupStack is empty. |
franck@0
|
571 */ |
franck@0
|
572 Popups.removeOverlay = function() { |
franck@0
|
573 if (!Popups.popupStack.length) { |
franck@0
|
574 $('#popups-overlay').remove(); |
franck@0
|
575 } |
franck@0
|
576 }; |
franck@0
|
577 |
franck@0
|
578 /** |
franck@0
|
579 * Add a "Loading" message while we are waiting for the ajax response. |
franck@0
|
580 */ |
franck@0
|
581 Popups.addLoading = function() { |
franck@0
|
582 var $loading = $('#popups-loading'); |
franck@0
|
583 if (!$loading.length) { // Loading image does not already exist, so create it. |
franck@0
|
584 $loading = $(Drupal.theme('popupLoading')); |
franck@0
|
585 $('body').prepend($loading); // Loading div is initially display:none. |
franck@0
|
586 var width = $loading.width(); |
franck@0
|
587 var height = $loading.height(); |
franck@0
|
588 var left = (Popups.windowWidth() / 2) - (width / 2) + Popups.scrollLeft(); |
franck@0
|
589 var top = (Popups.windowHeight() / 2) - (height / 2) + Popups.scrollTop(); |
franck@0
|
590 $loading.css({'top': top, 'left': left, 'display': 'block'}); // Center it and make it visible. |
franck@0
|
591 } |
franck@0
|
592 }; |
franck@0
|
593 |
franck@0
|
594 Popups.removeLoading = function() { |
franck@0
|
595 $('#popups-loading').remove(); |
franck@0
|
596 }; |
franck@0
|
597 |
franck@0
|
598 // Should I fold this function into Popups.pop? |
franck@0
|
599 Popups.removePopup = function(popup) { |
franck@0
|
600 // console.log("Popups.removePopup: " + popup); |
franck@0
|
601 if (!Popups.isset(popup)) { |
franck@0
|
602 popup = Popups.activePopup(); |
franck@0
|
603 } |
franck@0
|
604 if (popup) { |
franck@0
|
605 // console.log('removing '+popup.id); |
franck@0
|
606 popup.$popup().remove(); |
franck@0
|
607 Popups.popupStack.splice(Popups.popupStack.indexOf(popup), 1); // Remove popup from stack. Probably should rework into .pop() |
franck@0
|
608 } |
franck@0
|
609 // else { |
franck@0
|
610 // console.log("Popups.removePopup - there is no popup to remove."); |
franck@0
|
611 // } |
franck@0
|
612 }; |
franck@0
|
613 |
franck@0
|
614 /** |
franck@0
|
615 * Remove everything. |
franck@0
|
616 */ |
franck@0
|
617 Popups.close = function(popup) { |
franck@0
|
618 if (!Popups.isset(popup)) { |
franck@0
|
619 popup = Popups.activePopup(); |
franck@0
|
620 } |
franck@0
|
621 Popups.removePopup(popup); // Should this be a pop?? |
franck@0
|
622 Popups.removeLoading(); |
franck@0
|
623 if (Popups.activePopup()) { |
franck@0
|
624 Popups.activePopup().show(); |
franck@0
|
625 Popups.activePopup().refocus(); |
franck@0
|
626 } |
franck@0
|
627 else { |
franck@0
|
628 Popups.removeOverlay(); |
franck@0
|
629 Popups.restorePage(); |
franck@0
|
630 } |
franck@0
|
631 return false; |
franck@0
|
632 }; |
franck@0
|
633 |
franck@0
|
634 /** |
franck@0
|
635 * Save the page's original Drupal.settings. |
franck@0
|
636 */ |
franck@0
|
637 Popups.saveSettings = function() { |
franck@0
|
638 if (!Popups.originalSettings) { |
franck@0
|
639 Popups.originalSettings = Drupal.settings; |
franck@0
|
640 } |
franck@0
|
641 }; |
franck@0
|
642 |
franck@0
|
643 /** |
franck@0
|
644 * Restore the page's original Drupal.settings. |
franck@0
|
645 */ |
franck@0
|
646 Popups.restoreSettings = function() { |
franck@0
|
647 Drupal.settings = Popups.originalSettings; |
franck@0
|
648 }; |
franck@0
|
649 |
franck@0
|
650 /** |
franck@0
|
651 * Remove as much of the effects of jit loading as possible. |
franck@0
|
652 */ |
franck@0
|
653 Popups.restorePage = function() { |
franck@0
|
654 Popups.restoreSettings(); |
franck@0
|
655 // Remove the CSS files that were jit loaded for popup. |
franck@0
|
656 for (var i in Popups.addedCSS) { |
franck@0
|
657 var link = Popups.addedCSS[i]; |
franck@0
|
658 $('link[href='+ $(link).attr('href') + ']').remove(); |
franck@0
|
659 } |
franck@0
|
660 Popups.addedCSS = []; |
franck@0
|
661 }; |
franck@0
|
662 |
franck@0
|
663 |
franck@0
|
664 /**************************************************************************** |
franck@0
|
665 * Utility Functions ****************************************************** |
franck@0
|
666 ****************************************************************************/ |
franck@0
|
667 |
franck@0
|
668 /** |
franck@0
|
669 * Get the position of the left side of the browser window. |
franck@0
|
670 */ |
franck@0
|
671 Popups.scrollLeft = function() { |
franck@0
|
672 return Math.max(document.documentElement.scrollLeft, document.body.scrollLeft); |
franck@0
|
673 }; |
franck@0
|
674 |
franck@0
|
675 /** |
franck@0
|
676 * Get the position of the top of the browser window. |
franck@0
|
677 */ |
franck@0
|
678 Popups.scrollTop = function() { |
franck@0
|
679 return Math.max(document.documentElement.scrollTop, document.body.scrollTop); |
franck@0
|
680 }; |
franck@0
|
681 |
franck@0
|
682 /** |
franck@0
|
683 * Get the height of the browser window. |
franck@0
|
684 * Fixes jQuery & Opera bug - http://drupal.org/node/366093 |
franck@0
|
685 */ |
franck@0
|
686 Popups.windowHeight = function() { |
franck@0
|
687 if ($.browser.opera && $.browser.version > "9.5" && $.fn.jquery <= "1.2.6") { |
franck@0
|
688 return document.documentElement.clientHeight; |
franck@0
|
689 } |
franck@0
|
690 return $(window).height(); |
franck@0
|
691 }; |
franck@0
|
692 |
franck@0
|
693 /** |
franck@0
|
694 * Get the height of the browser window. |
franck@0
|
695 * Fixes jQuery & Opera bug - http://drupal.org/node/366093 |
franck@0
|
696 */ |
franck@0
|
697 Popups.windowWidth = function() { |
franck@0
|
698 if ($.browser.opera && $.browser.version > "9.5" && $.fn.jquery <= "1.2.6") { |
franck@0
|
699 return document.documentElement.clientWidth; |
franck@0
|
700 } |
franck@0
|
701 return $(window).width(); |
franck@0
|
702 }; |
franck@0
|
703 |
franck@0
|
704 Popups.nextCounter = function() { |
franck@0
|
705 if (this.counter === undefined) { |
franck@0
|
706 this.counter = 0; |
franck@0
|
707 } |
franck@0
|
708 else { |
franck@0
|
709 this.counter++; |
franck@0
|
710 } |
franck@0
|
711 return this.counter; |
franck@0
|
712 }; |
franck@0
|
713 |
franck@0
|
714 /**************************************************************************** |
franck@0
|
715 * Ajax Functions ****************************************************** |
franck@0
|
716 ****************************************************************************/ |
franck@0
|
717 |
franck@0
|
718 /** |
franck@0
|
719 * Add additional CSS to the page. |
franck@0
|
720 */ |
franck@0
|
721 Popups.addCSS = function(css) { |
franck@0
|
722 Popups.addedCSS = []; |
franck@0
|
723 for (var type in css) { |
franck@0
|
724 for (var file in css[type]) { |
franck@0
|
725 var link = css[type][file]; |
franck@0
|
726 // Does the page already contain this stylesheet? |
franck@0
|
727 if (!$('link[href='+ $(link).attr('href') + ']').length) { |
franck@0
|
728 $('head').append(link); |
franck@0
|
729 Popups.addedCSS.push(link); // Keep a list, so we can remove them later. |
franck@0
|
730 } |
franck@0
|
731 } |
franck@0
|
732 } |
franck@0
|
733 }; |
franck@0
|
734 |
franck@0
|
735 /** |
franck@0
|
736 * Add additional Javascript to the page. |
franck@0
|
737 */ |
franck@0
|
738 Popups.addJS = function(js) { |
franck@0
|
739 // Parse the json info about the new context. |
franck@0
|
740 var scripts = []; |
franck@0
|
741 var inlines = []; |
franck@0
|
742 for (var type in js) { |
franck@0
|
743 if (type != 'setting') { |
franck@0
|
744 for (var file in js[type]) { |
franck@0
|
745 if (type == 'inline') { |
franck@0
|
746 inlines.push($(js[type][file]).text()); |
franck@0
|
747 } |
franck@0
|
748 else { |
franck@0
|
749 scripts.push($(js[type][file]).attr('src')); |
franck@0
|
750 } |
franck@0
|
751 } |
franck@0
|
752 } |
franck@0
|
753 } |
franck@0
|
754 |
franck@0
|
755 // Add new JS settings to the page, needed for #ahah properties to work. |
franck@0
|
756 Drupal.settings = js.setting; |
franck@0
|
757 // console.log('js.setting...'); |
franck@0
|
758 // console.log(js.setting); |
franck@0
|
759 |
franck@0
|
760 for (var i in scripts) { |
franck@0
|
761 var src = scripts[i]; |
franck@0
|
762 if (!$('script[src='+ src + ']').length && !Popups.addedJS[src]) { |
franck@0
|
763 // Get the script from the server and execute it. |
franck@0
|
764 $.ajax({ |
franck@0
|
765 type: 'GET', |
franck@0
|
766 url: src, |
franck@0
|
767 dataType: 'script', |
franck@0
|
768 async : false, |
franck@0
|
769 success: function(script) { |
franck@0
|
770 eval(script); |
franck@0
|
771 } |
franck@0
|
772 }); |
franck@0
|
773 // Mark the js as added to the underlying page. |
franck@0
|
774 Popups.addedJS[src] = true; |
franck@0
|
775 } |
franck@0
|
776 } |
franck@0
|
777 |
franck@0
|
778 return inlines; |
franck@0
|
779 }; |
franck@0
|
780 |
franck@0
|
781 /** |
franck@0
|
782 * Execute the jit loaded inline scripts. |
franck@0
|
783 * Q: Do we want to re-excute the ones already in the page? |
franck@0
|
784 * |
franck@0
|
785 * @param inlines |
franck@0
|
786 * Array of inline scripts. |
franck@0
|
787 */ |
franck@0
|
788 Popups.addInlineJS = function(inlines) { |
franck@0
|
789 // Load the inlines into the page. |
franck@0
|
790 for (var n in inlines) { |
franck@0
|
791 // If the script is not already in the page, execute it. |
franck@0
|
792 if (!$('script:not([src]):contains(' + inlines[n] + ')').length) { |
franck@0
|
793 eval(inlines[n]); |
franck@0
|
794 } |
franck@0
|
795 } |
franck@0
|
796 }; |
franck@0
|
797 |
franck@0
|
798 Popups.beforeSend = function(xhr) { |
franck@0
|
799 xhr.setRequestHeader("X-Drupal-Render-Mode", 'json/popups'); |
franck@0
|
800 }; |
franck@0
|
801 |
franck@0
|
802 /** |
franck@0
|
803 * Do before the form in the popups is submitted. |
franck@0
|
804 */ |
franck@0
|
805 Popups.beforeSubmit = function(formData, $form, options) { |
franck@0
|
806 Popups.removePopup(); // Remove just the dialog, but not the overlay. |
franck@0
|
807 Popups.addLoading(); |
franck@0
|
808 }; |
franck@0
|
809 |
franck@0
|
810 |
franck@0
|
811 /**************************************************************************** |
franck@0
|
812 * Page & Form in popups functions *** |
franck@0
|
813 ****************************************************************************/ |
franck@0
|
814 |
franck@0
|
815 /** |
franck@0
|
816 * Use Ajax to open a link in a popups window. |
franck@0
|
817 * |
franck@0
|
818 * @param element |
franck@0
|
819 * Element that was clicked to open the popups. |
franck@0
|
820 * @param options |
franck@0
|
821 * Hash of options controlling how the popups interacts with the underlying page. |
franck@0
|
822 * @param parent |
franck@0
|
823 * If path is being opened from inside another popup, that popup is the parent. |
franck@0
|
824 */ |
franck@0
|
825 Popups.openPath = function(element, options, parent) { |
franck@0
|
826 Popups.saveSettings(); |
franck@0
|
827 |
franck@0
|
828 // Let the user know something is happening. |
franck@0
|
829 $('body').css("cursor", "wait"); |
franck@0
|
830 |
franck@0
|
831 // TODO - get nonmodal working. |
franck@0
|
832 if (!options.nonModal) { |
franck@0
|
833 Popups.addOverlay(); |
franck@0
|
834 } |
franck@0
|
835 Popups.addLoading(); |
franck@0
|
836 |
franck@0
|
837 var href = options.href ? options.href : element.href; |
franck@0
|
838 $(document).trigger('popups_open_path', [element, href]); // Broadcast Popup Open Path event. |
franck@0
|
839 |
franck@0
|
840 var params = {}; |
franck@0
|
841 // Force the popups to return back to the orignal page when forms are done, unless hijackDestination option is set to FALSE. |
franck@0
|
842 if (options.hijackDestination) { |
franck@0
|
843 var returnPath; |
franck@0
|
844 if (parent) { |
franck@0
|
845 returnPath = parent.path; |
franck@0
|
846 // console.log('Popup parent is ...'); |
franck@0
|
847 // console.log(parent); |
franck@0
|
848 } |
franck@0
|
849 else { // No parent, so bring flow back to original page. |
franck@0
|
850 returnPath = Popups.originalSettings.popups.originalPath; |
franck@0
|
851 } |
franck@0
|
852 href = href.replace(/destination=[^;&]*[;&]?/, ''); // Strip out any existing destination param. |
franck@0
|
853 // console.log("Hijacking destination to " + returnPath); |
franck@0
|
854 params.destination = returnPath; // Set the destination to return to the parent's path. |
franck@0
|
855 } |
franck@0
|
856 |
franck@0
|
857 var ajaxOptions = { |
franck@0
|
858 url: href, |
franck@0
|
859 dataType: 'json', |
franck@0
|
860 data: params, |
franck@0
|
861 beforeSend: Popups.beforeSend, |
franck@0
|
862 success: function(json) { |
franck@0
|
863 // Add additional CSS to the page. |
franck@0
|
864 Popups.addCSS(json.css); |
franck@0
|
865 var inlines = Popups.addJS(json.js); |
franck@0
|
866 var popup = Popups.openPathContent(json.path, json.title, json.messages + json.content, element, options, parent); |
franck@0
|
867 Popups.addInlineJS(inlines); |
franck@0
|
868 // Broadcast an event that the path was opened. |
franck@0
|
869 $(document).trigger('popups_open_path_done', [element, href, popup]); |
franck@0
|
870 }, |
franck@0
|
871 complete: function() { |
franck@0
|
872 $('body').css("cursor", "auto"); // Return the cursor to normal state. |
franck@0
|
873 } |
franck@0
|
874 }; |
franck@0
|
875 |
franck@0
|
876 var ajaxOptions; |
franck@0
|
877 if (options.reloadOnError) { |
franck@0
|
878 ajaxOptions.error = function() { |
franck@0
|
879 location.reload(); // Reload on error. Is this working? |
franck@0
|
880 }; |
franck@0
|
881 } |
franck@0
|
882 else { |
franck@0
|
883 ajaxOptions.error = function() { |
franck@0
|
884 Popups.message("Unable to open: " + href); |
franck@0
|
885 }; |
franck@0
|
886 } |
franck@0
|
887 $.ajax(ajaxOptions); |
franck@0
|
888 |
franck@0
|
889 return false; |
franck@0
|
890 }; |
franck@0
|
891 |
franck@0
|
892 /** |
franck@0
|
893 * Open path's content in an ajax popups. |
franck@0
|
894 * |
franck@0
|
895 * @param title |
franck@0
|
896 * String title of the popups. |
franck@0
|
897 * @param content |
franck@0
|
898 * HTML to show in the popups. |
franck@0
|
899 * @param element |
franck@0
|
900 * A DOM object containing the element that was clicked to initiate the popup. |
franck@0
|
901 * @param options |
franck@0
|
902 * Hash of options controlling how the popups interacts with the underlying page. |
franck@0
|
903 * @param parent |
franck@0
|
904 * Spawning popup, or null if spawned from original page. |
franck@0
|
905 */ |
franck@0
|
906 Popups.openPathContent = function(path, title, content, element, options, parent) { |
franck@0
|
907 var popup = new Popups.Popup(); |
franck@0
|
908 Popups.open(popup, title, content, null, options.width); |
franck@0
|
909 |
franck@0
|
910 // Set properties on new popup. |
franck@0
|
911 popup.parent = parent; |
franck@0
|
912 popup.path = path; |
franck@0
|
913 // console.log("Setting popup " + popup.id + " originalPath to " + path); |
franck@0
|
914 popup.options = options; |
franck@0
|
915 popup.element = element; |
franck@0
|
916 |
franck@0
|
917 // Add behaviors to content in popups. |
franck@0
|
918 delete Drupal.behaviors.tableHeader; // Work-around for bug in tableheader.js (http://drupal.org/node/234377) |
franck@0
|
919 delete Drupal.behaviors.teaser; // Work-around for bug in teaser.js (sigh). |
franck@0
|
920 Drupal.attachBehaviors(popup.$popupBody()); |
franck@0
|
921 // Adding collapse moves focus. |
franck@0
|
922 popup.refocus(); |
franck@0
|
923 |
franck@0
|
924 // If the popups contains a form, capture submits. |
franck@0
|
925 var $form = $('form', popup.$popupBody()); |
franck@0
|
926 if ($form.length) { |
franck@0
|
927 $form.ajaxForm({ |
franck@0
|
928 dataType: 'json', |
franck@0
|
929 beforeSubmit: Popups.beforeSubmit, |
franck@0
|
930 beforeSend: Popups.beforeSend, |
franck@0
|
931 success: function(json, status) { |
franck@0
|
932 Popups.formSuccess(popup, json); |
franck@0
|
933 }, |
franck@0
|
934 error: function() { |
franck@0
|
935 Popups.message("Bad Response form submission"); |
franck@0
|
936 } |
franck@0
|
937 }); |
franck@0
|
938 } |
franck@0
|
939 return popup; |
franck@0
|
940 }; |
franck@0
|
941 |
franck@0
|
942 /** |
franck@0
|
943 * The form in the popups was successfully submitted |
franck@0
|
944 * Update the originating page. |
franck@0
|
945 * Show any messages in a popups. |
franck@0
|
946 * |
franck@0
|
947 * @param popup |
franck@0
|
948 * The popup object that contained the form that was just submitted. |
franck@0
|
949 * @param data |
franck@0
|
950 * JSON object from server with status of form submission. |
franck@0
|
951 */ |
franck@0
|
952 Popups.formSuccess = function(popup, data) { |
franck@0
|
953 // Determine if we are at an end point, or just moving from one popups to another. |
franck@0
|
954 var done = popup.isDone(data.path); |
franck@0
|
955 if (!done) { // Not done yet, so show new page in new popups. |
franck@0
|
956 Popups.removeLoading(); |
franck@0
|
957 Popups.openPathContent(data.path, data.title, data.messages + data.content, popup.element, popup.options, popup.parent); |
franck@0
|
958 } |
franck@0
|
959 else { // We are done with popup flow. |
franck@0
|
960 // Execute the onUpdate callback if available. |
franck@0
|
961 if (popup.options.updateMethod === 'callback' && popup.options.onUpdate) { |
franck@0
|
962 var result = eval(popup.options.onUpdate +'(data, popup.options, popup.element)'); |
franck@0
|
963 if (result === false) { // Give onUpdate callback a chance to skip normal processing. |
franck@0
|
964 return; |
franck@0
|
965 } |
franck@0
|
966 } |
franck@0
|
967 |
franck@0
|
968 if (popup.options.updateMethod === 'reload') { // Force a complete, non-ajax reload of the page. |
franck@0
|
969 if (popup.options.updateSource === 'final') { |
franck@0
|
970 location.href = Drupal.settings.basePath + data.path; // TODO: Need to test this. |
franck@0
|
971 } |
franck@0
|
972 else { // Reload originating page. |
franck@0
|
973 location.reload(); |
franck@0
|
974 } |
franck@0
|
975 } |
franck@0
|
976 else { // Normal, targeted ajax, reload behavior. |
franck@0
|
977 // Show messages in dialog and embed the results in the original page. |
franck@0
|
978 var showMessage = data.messages.length && !popup.options.noMessage; |
franck@0
|
979 if (showMessage) { |
franck@0
|
980 var messagePopup = Popups.message(data.messages); // Popup message. |
franck@0
|
981 if (Popups.originalSettings.popups.autoCloseFinalMessage) { |
franck@0
|
982 setTimeout(function(){Popups.close(messagePopup);}, 2500); // Autoclose the message box in 2.5 seconds. |
franck@0
|
983 } |
franck@0
|
984 |
franck@0
|
985 // Insert the message into the page above the content. |
franck@0
|
986 // Might not be the standard spot, but it is the easiest to find. |
franck@0
|
987 var $next; |
franck@0
|
988 if (popup.targetLayerSelector() === 'body') { |
franck@0
|
989 $next = $('body').find(Popups.originalSettings.popups.defaultTargetSelector); |
franck@0
|
990 } |
franck@0
|
991 else { |
franck@0
|
992 $next = $(popup.targetLayerSelector()).find('.popups-body'); |
franck@0
|
993 } |
franck@0
|
994 $next.parent().find('div.messages').remove(); // Remove the existing messages. |
franck@0
|
995 $next.before(data.messages); // Insert new messages. |
franck@0
|
996 } |
franck@0
|
997 |
franck@0
|
998 // Update the content area (defined by 'targetSelectors'). |
franck@0
|
999 if (popup.options.updateMethod !== 'none') { |
franck@0
|
1000 Popups.testContentSelector(); // Kick up warning message if selector is bad. |
franck@0
|
1001 |
franck@0
|
1002 Popups.restoreSettings(); // Need to restore original Drupal.settings.popups.links before running attachBehaviors. This probably has CSS side effects! |
franck@0
|
1003 if (popup.options.targetSelectors) { // Pick and choose what returned content goes where. |
franck@0
|
1004 jQuery.each(popup.options.targetSelectors, function(t_new, t_old) { |
franck@0
|
1005 if(!isNaN(t_new)) { |
franck@0
|
1006 t_new = t_old; // handle case where targetSelectors is an array, not a hash. |
franck@0
|
1007 } |
franck@0
|
1008 // console.log("Updating target " + t_old + ' with ' + t_new); |
franck@0
|
1009 var new_content = $(t_new, data.content); |
franck@0
|
1010 // console.log("new content... "); |
franck@0
|
1011 // console.log(new_content); |
franck@0
|
1012 var $c = $(popup.targetLayerSelector()).find(t_old).html(new_content); // Inject the new content into the original page. |
franck@0
|
1013 |
franck@0
|
1014 Drupal.attachBehaviors($c); |
franck@0
|
1015 }); |
franck@0
|
1016 } |
franck@0
|
1017 else { // Put the entire new content into default content area. |
franck@0
|
1018 var $c = $(popup.targetLayerSelector()).find(Popups.originalSettings.popups.defaultTargetSelector).html(data.content); |
franck@0
|
1019 // console.log("updating entire content area.") |
franck@0
|
1020 Drupal.attachBehaviors($c); |
franck@0
|
1021 } |
franck@0
|
1022 } |
franck@0
|
1023 |
franck@0
|
1024 // Update the title of the page. |
franck@0
|
1025 if (popup.options.titleSelectors) { |
franck@0
|
1026 jQuery.each(popup.options.titleSelectors, function() { |
franck@0
|
1027 $(''+this).html(data.title); |
franck@0
|
1028 }); |
franck@0
|
1029 } |
franck@0
|
1030 |
franck@0
|
1031 // Done with changes to the original page, remove effects. |
franck@0
|
1032 Popups.removeLoading(); |
franck@0
|
1033 if (!showMessage) { |
franck@0
|
1034 // If there is not a messages popups, close current layer. |
franck@0
|
1035 Popups.close(); |
franck@0
|
1036 } |
franck@0
|
1037 } |
franck@0
|
1038 |
franck@0
|
1039 // Broadcast an event that popup form was done and successful. |
franck@0
|
1040 $(document).trigger('popups_form_success', [popup]); |
franck@0
|
1041 |
franck@0
|
1042 } // End of updating spawning layer. |
franck@0
|
1043 }; |
franck@0
|
1044 |
franck@0
|
1045 |
franck@0
|
1046 /** |
franck@0
|
1047 * Get a jQuery object for the content of a layer. |
franck@0
|
1048 * @param layer |
franck@0
|
1049 * Either a popup, or null to signify the original page. |
franck@0
|
1050 */ |
franck@0
|
1051 Popups.getLayerContext = function(layer) { |
franck@0
|
1052 var $context; |
franck@0
|
1053 if (!layer) { |
franck@0
|
1054 $context = $('body').find(Popups.originalSettings.popups.defaultTargetSelector); |
franck@0
|
1055 } |
franck@0
|
1056 else { |
franck@0
|
1057 $context = layer.$popupBody(); |
franck@0
|
1058 } |
franck@0
|
1059 return $context; |
franck@0
|
1060 } |
franck@0
|
1061 |
franck@0
|
1062 /** |
franck@0
|
1063 * Submit the page and reload the results, before popping up the real dialog. |
franck@0
|
1064 * |
franck@0
|
1065 * @param element |
franck@0
|
1066 * Element that was clicked to open a new popup. |
franck@0
|
1067 * @param options |
franck@0
|
1068 * Hash of options controlling how the popups interacts with the underlying page. |
franck@0
|
1069 * @param layer |
franck@0
|
1070 * Popup with form to save, or null if form is on original page. |
franck@0
|
1071 */ |
franck@0
|
1072 Popups.saveFormOnLayer = function(element, options, layer) { |
franck@0
|
1073 var $context = Popups.getLayerContext(layer); |
franck@0
|
1074 var $form = $context.find('form'); |
franck@0
|
1075 var ajaxOptions = { |
franck@0
|
1076 dataType: 'json', |
franck@0
|
1077 beforeSubmit: Popups.beforeSubmit, |
franck@0
|
1078 beforeSend: Popups.beforeSend, |
franck@0
|
1079 success: function(response, status) { |
franck@0
|
1080 // Sync up the current page contents with the submit. |
franck@0
|
1081 var $c = $context.html(response.content); // Inject the new content into the page. |
franck@0
|
1082 Drupal.attachBehaviors($c); |
franck@0
|
1083 // The form has been saved, the page reloaded, now safe to show the triggering link in a popup. |
franck@0
|
1084 Popups.openPath(element, options, layer); |
franck@0
|
1085 } |
franck@0
|
1086 }; |
franck@0
|
1087 $form.ajaxSubmit(ajaxOptions); // Submit the form. |
franck@0
|
1088 }; |
franck@0
|
1089 |
franck@0
|
1090 /** |
franck@0
|
1091 * Warn the user if ajax updates will not work |
franck@0
|
1092 * due to mismatch between the theme and the theme's popup setting. |
franck@0
|
1093 */ |
franck@0
|
1094 Popups.testContentSelector = function() { |
franck@0
|
1095 var target = Popups.originalSettings.popups.defaultTargetSelector; |
franck@0
|
1096 var hits = $(target).length; |
franck@0
|
1097 if (hits !== 1) { // 1 is the corrent answer. |
franck@0
|
1098 var msg = Drupal.t('The popup content area for this theme is misconfigured.') + '\n'; |
franck@0
|
1099 if (hits === 0) { |
franck@0
|
1100 msg += Drupal.t('There is no element that matches ') + '"' + target + '"\n'; |
franck@0
|
1101 } |
franck@0
|
1102 else if (hits > 1) { |
franck@0
|
1103 msg += Drupal.t('There are multiple elements that match: ') + '"' + target + '"\n'; |
franck@0
|
1104 } |
franck@0
|
1105 msg += Drupal.t('Go to admin/build/themes/settings, select your theme, and edit the "Content Selector" field'); |
franck@0
|
1106 alert(msg); |
franck@0
|
1107 } |
franck@0
|
1108 }; |
franck@0
|
1109 |
franck@0
|
1110 |
franck@0
|
1111 // **************************************************************************** |
franck@0
|
1112 // * Theme Functions ******************************************************** |
franck@0
|
1113 // **************************************************************************** |
franck@0
|
1114 |
franck@0
|
1115 Drupal.theme.prototype.popupLoading = function() { |
franck@0
|
1116 var loading = '<div id="popups-loading">'; |
franck@0
|
1117 loading += '<img src="'+ Drupal.settings.basePath + Popups.originalSettings.popups.modulePath + '/ajax-loader.gif" />'; |
franck@0
|
1118 loading += '</div>'; |
franck@0
|
1119 return loading; |
franck@0
|
1120 }; |
franck@0
|
1121 |
franck@0
|
1122 Drupal.theme.prototype.popupOverlay = function() { |
franck@0
|
1123 return '<div id="popups-overlay"></div>'; |
franck@0
|
1124 }; |
franck@0
|
1125 |
franck@0
|
1126 Drupal.theme.prototype.popupButton = function(title, id) { |
franck@0
|
1127 return '<input type="button" value="'+ title +'" id="'+ id +'" />'; |
franck@0
|
1128 }; |
franck@0
|
1129 |
franck@0
|
1130 Drupal.theme.prototype.popupDialog = function(popupId, title, body, buttons) { |
franck@0
|
1131 var template = Drupal.theme('popupTemplate', popupId); |
franck@0
|
1132 var popups = template.replace('%title', title).replace('%body', body); |
franck@0
|
1133 |
franck@0
|
1134 var themedButtons = ''; |
franck@0
|
1135 if (buttons) { |
franck@0
|
1136 jQuery.each(buttons, function (id, button) { |
franck@0
|
1137 themedButtons += Drupal.theme('popupButton', button.title, id); |
franck@0
|
1138 }); |
franck@0
|
1139 } |
franck@0
|
1140 popups = popups.replace('%buttons', themedButtons); |
franck@0
|
1141 return popups; |
franck@0
|
1142 }; |
franck@0
|
1143 |
franck@0
|
1144 Drupal.theme.prototype.popupTemplate = function(popupId) { |
franck@0
|
1145 var template; |
franck@0
|
1146 template += '<div id="'+ popupId + '" class="popups-box">'; |
franck@0
|
1147 template += ' <div class="popups-title">'; |
franck@0
|
1148 template += ' <div class="popups-close"><a href="#">' + Drupal.t('Close') + '</a></div>'; |
franck@0
|
1149 template += ' <div class="title">%title</div>'; |
franck@0
|
1150 template += ' <div class="clear-block"></div>'; |
franck@0
|
1151 template += ' </div>'; |
franck@0
|
1152 template += ' <div class="popups-body">%body</div>'; |
franck@0
|
1153 template += ' <div class="popups-buttons">%buttons</div>'; |
franck@0
|
1154 template += ' <div class="popups-footer"></div>'; |
franck@0
|
1155 template += '</div>'; |
franck@0
|
1156 return template; |
franck@0
|
1157 }; |
franck@0
|
1158 |