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