root / drupal7 / sites / all / modules / ctools / js / modal.js @ 219d19c4
1 |
/**
|
---|---|
2 |
* @file
|
3 |
*
|
4 |
* Implement a modal form.
|
5 |
*
|
6 |
* @see modal.inc for documentation.
|
7 |
*
|
8 |
* This javascript relies on the CTools ajax responder.
|
9 |
*/
|
10 |
|
11 |
(function ($) { |
12 |
// Make sure our objects are defined.
|
13 |
Drupal.CTools = Drupal.CTools || {}; |
14 |
Drupal.CTools.Modal = Drupal.CTools.Modal || {}; |
15 |
|
16 |
/**
|
17 |
* Display the modal
|
18 |
*
|
19 |
* @todo -- document the settings.
|
20 |
*/
|
21 |
Drupal.CTools.Modal.show = function(choice) { |
22 |
var opts = {};
|
23 |
|
24 |
if (choice && typeof choice == 'string' && Drupal.settings[choice]) { |
25 |
// This notation guarantees we are actually copying it.
|
26 |
$.extend(true, opts, Drupal.settings[choice]); |
27 |
} |
28 |
else if (choice) { |
29 |
$.extend(true, opts, choice); |
30 |
} |
31 |
|
32 |
var defaults = {
|
33 |
modalTheme: 'CToolsModalDialog', |
34 |
throbberTheme: 'CToolsModalThrobber', |
35 |
animation: 'show', |
36 |
animationSpeed: 'fast', |
37 |
modalSize: {
|
38 |
type: 'scale', |
39 |
width: .8, |
40 |
height: .8, |
41 |
addWidth: 0, |
42 |
addHeight: 0, |
43 |
// How much to remove from the inner content to make space for the
|
44 |
// theming.
|
45 |
contentRight: 25, |
46 |
contentBottom: 45 |
47 |
}, |
48 |
modalOptions: {
|
49 |
opacity: .55, |
50 |
background: '#fff' |
51 |
}, |
52 |
modalClass: 'default' |
53 |
}; |
54 |
|
55 |
var settings = {};
|
56 |
$.extend(true, settings, defaults, Drupal.settings.CToolsModal, opts); |
57 |
|
58 |
if (Drupal.CTools.Modal.currentSettings && Drupal.CTools.Modal.currentSettings != settings) {
|
59 |
Drupal.CTools.Modal.modal.remove(); |
60 |
Drupal.CTools.Modal.modal = null;
|
61 |
} |
62 |
|
63 |
Drupal.CTools.Modal.currentSettings = settings; |
64 |
|
65 |
var resize = function(e) { |
66 |
// When creating the modal, it actually exists only in a theoretical
|
67 |
// place that is not in the DOM. But once the modal exists, it is in the
|
68 |
// DOM so the context must be set appropriately.
|
69 |
var context = e ? document : Drupal.CTools.Modal.modal;
|
70 |
|
71 |
if (Drupal.CTools.Modal.currentSettings.modalSize.type == 'scale') { |
72 |
var width = $(window).width() * Drupal.CTools.Modal.currentSettings.modalSize.width; |
73 |
var height = $(window).height() * Drupal.CTools.Modal.currentSettings.modalSize.height; |
74 |
} |
75 |
else {
|
76 |
var width = Drupal.CTools.Modal.currentSettings.modalSize.width;
|
77 |
var height = Drupal.CTools.Modal.currentSettings.modalSize.height;
|
78 |
} |
79 |
|
80 |
// Use the additionol pixels for creating the width and height.
|
81 |
$('div.ctools-modal-content', context).css({ |
82 |
'width': width + Drupal.CTools.Modal.currentSettings.modalSize.addWidth + 'px', |
83 |
'height': height + Drupal.CTools.Modal.currentSettings.modalSize.addHeight + 'px' |
84 |
}); |
85 |
$('div.ctools-modal-content .modal-content', context).css({ |
86 |
'width': (width - Drupal.CTools.Modal.currentSettings.modalSize.contentRight) + 'px', |
87 |
'height': (height - Drupal.CTools.Modal.currentSettings.modalSize.contentBottom) + 'px' |
88 |
}); |
89 |
} |
90 |
|
91 |
if (!Drupal.CTools.Modal.modal) {
|
92 |
Drupal.CTools.Modal.modal = $(Drupal.theme(settings.modalTheme));
|
93 |
if (settings.modalSize.type == 'scale') { |
94 |
$(window).bind('resize', resize); |
95 |
} |
96 |
} |
97 |
|
98 |
resize(); |
99 |
|
100 |
$('span.modal-title', Drupal.CTools.Modal.modal).html(Drupal.CTools.Modal.currentSettings.loadingText); |
101 |
Drupal.CTools.Modal.modalContent(Drupal.CTools.Modal.modal, settings.modalOptions, settings.animation, settings.animationSpeed, settings.modalClass); |
102 |
$('#modalContent .modal-content').html(Drupal.theme(settings.throbberTheme)).addClass('ctools-modal-loading'); |
103 |
|
104 |
// Position autocomplete results based on the scroll position of the modal.
|
105 |
$('#modalContent .modal-content').delegate('input.form-autocomplete', 'keyup', function() { |
106 |
$('#autocomplete').css('top', $(this).position().top + $(this).outerHeight() + $(this).offsetParent().filter('#modal-content').scrollTop()); |
107 |
}); |
108 |
}; |
109 |
|
110 |
/**
|
111 |
* Hide the modal
|
112 |
*/
|
113 |
Drupal.CTools.Modal.dismiss = function() { |
114 |
if (Drupal.CTools.Modal.modal) {
|
115 |
Drupal.CTools.Modal.unmodalContent(Drupal.CTools.Modal.modal); |
116 |
} |
117 |
}; |
118 |
|
119 |
/**
|
120 |
* Provide the HTML to create the modal dialog.
|
121 |
*/
|
122 |
Drupal.theme.prototype.CToolsModalDialog = function () { |
123 |
var html = '' |
124 |
html += '<div id="ctools-modal">'
|
125 |
html += ' <div class="ctools-modal-content">' // panels-modal-content |
126 |
html += ' <div class="modal-header">';
|
127 |
html += ' <a class="close" href="#">';
|
128 |
html += Drupal.CTools.Modal.currentSettings.closeText + Drupal.CTools.Modal.currentSettings.closeImage; |
129 |
html += ' </a>';
|
130 |
html += ' <span id="modal-title" class="modal-title"> </span>';
|
131 |
html += ' </div>';
|
132 |
html += ' <div id="modal-content" class="modal-content">';
|
133 |
html += ' </div>';
|
134 |
html += ' </div>';
|
135 |
html += '</div>';
|
136 |
|
137 |
return html;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Provide the HTML to create the throbber.
|
142 |
*/
|
143 |
Drupal.theme.prototype.CToolsModalThrobber = function () { |
144 |
var html = ''; |
145 |
html += '<div id="modal-throbber">';
|
146 |
html += ' <div class="modal-throbber-wrapper">';
|
147 |
html += Drupal.CTools.Modal.currentSettings.throbber; |
148 |
html += ' </div>';
|
149 |
html += '</div>';
|
150 |
|
151 |
return html;
|
152 |
}; |
153 |
|
154 |
/**
|
155 |
* Figure out what settings string to use to display a modal.
|
156 |
*/
|
157 |
Drupal.CTools.Modal.getSettings = function (object) { |
158 |
var match = $(object).attr('class').match(/ctools-modal-(\S+)/); |
159 |
if (match) {
|
160 |
return match[1]; |
161 |
} |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Click function for modals that can be cached.
|
166 |
*/
|
167 |
Drupal.CTools.Modal.clickAjaxCacheLink = function () { |
168 |
Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this));
|
169 |
return Drupal.CTools.AJAX.clickAJAXCacheLink.apply(this); |
170 |
}; |
171 |
|
172 |
/**
|
173 |
* Handler to prepare the modal for the response
|
174 |
*/
|
175 |
Drupal.CTools.Modal.clickAjaxLink = function () { |
176 |
Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this));
|
177 |
return false; |
178 |
}; |
179 |
|
180 |
/**
|
181 |
* Submit responder to do an AJAX submit on all modal forms.
|
182 |
*/
|
183 |
Drupal.CTools.Modal.submitAjaxForm = function(e) { |
184 |
var $form = $(this); |
185 |
var url = $form.attr('action'); |
186 |
|
187 |
setTimeout(function() { Drupal.CTools.AJAX.ajaxSubmit($form, url); }, 1); |
188 |
return false; |
189 |
} |
190 |
|
191 |
/**
|
192 |
* Bind links that will open modals to the appropriate function.
|
193 |
*/
|
194 |
Drupal.behaviors.ZZCToolsModal = { |
195 |
attach: function(context) { |
196 |
// Bind links
|
197 |
// Note that doing so in this order means that the two classes can be
|
198 |
// used together safely.
|
199 |
/*
|
200 |
* @todo remimplement the warm caching feature
|
201 |
$('a.ctools-use-modal-cache', context).once('ctools-use-modal', function() {
|
202 |
$(this).click(Drupal.CTools.Modal.clickAjaxCacheLink);
|
203 |
Drupal.CTools.AJAX.warmCache.apply(this);
|
204 |
});
|
205 |
*/
|
206 |
|
207 |
$('area.ctools-use-modal, a.ctools-use-modal', context).once('ctools-use-modal', function() { |
208 |
var $this = $(this); |
209 |
$this.click(Drupal.CTools.Modal.clickAjaxLink);
|
210 |
// Create a drupal ajax object
|
211 |
var element_settings = {};
|
212 |
if ($this.attr('href')) { |
213 |
element_settings.url = $this.attr('href'); |
214 |
element_settings.event = 'click';
|
215 |
element_settings.progress = { type: 'throbber' }; |
216 |
} |
217 |
var base = $this.attr('href'); |
218 |
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); |
219 |
}); |
220 |
|
221 |
// Bind buttons
|
222 |
$('input.ctools-use-modal, button.ctools-use-modal', context).once('ctools-use-modal', function() { |
223 |
var $this = $(this); |
224 |
$this.click(Drupal.CTools.Modal.clickAjaxLink);
|
225 |
var button = this; |
226 |
var element_settings = {};
|
227 |
|
228 |
// AJAX submits specified in this manner automatically submit to the
|
229 |
// normal form action.
|
230 |
element_settings.url = Drupal.CTools.Modal.findURL(this);
|
231 |
if (element_settings.url == '') { |
232 |
element_settings.url = $(this).closest('form').attr('action'); |
233 |
} |
234 |
element_settings.event = 'click';
|
235 |
element_settings.setClick = true;
|
236 |
|
237 |
var base = $this.attr('id'); |
238 |
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); |
239 |
|
240 |
// Make sure changes to settings are reflected in the URL.
|
241 |
$('.' + $(button).attr('id') + '-url').change(function() { |
242 |
Drupal.ajax[base].options.url = Drupal.CTools.Modal.findURL(button); |
243 |
}); |
244 |
}); |
245 |
|
246 |
// Bind our custom event to the form submit
|
247 |
$('#modal-content form', context).once('ctools-use-modal', function() { |
248 |
var $this = $(this); |
249 |
var element_settings = {};
|
250 |
|
251 |
element_settings.url = $this.attr('action'); |
252 |
element_settings.event = 'submit';
|
253 |
element_settings.progress = { 'type': 'throbber' } |
254 |
var base = $this.attr('id'); |
255 |
|
256 |
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); |
257 |
Drupal.ajax[base].form = $this;
|
258 |
|
259 |
$('input[type=submit], button', this).click(function(event) { |
260 |
Drupal.ajax[base].element = this;
|
261 |
this.form.clk = this; |
262 |
// Stop autocomplete from submitting.
|
263 |
if (Drupal.autocompleteSubmit && !Drupal.autocompleteSubmit()) {
|
264 |
return false; |
265 |
} |
266 |
// An empty event means we were triggered via .click() and
|
267 |
// in jquery 1.4 this won't trigger a submit.
|
268 |
// We also have to check jQuery version to prevent
|
269 |
// IE8 + jQuery 1.4.4 to break on other events
|
270 |
// bound to the submit button.
|
271 |
if (jQuery.fn.jquery.substr(0, 3) === '1.4' && typeof event.bubbles === "undefined") { |
272 |
$(this.form).trigger('submit'); |
273 |
return false; |
274 |
} |
275 |
}); |
276 |
}); |
277 |
|
278 |
// Bind a click handler to allow elements with the 'ctools-close-modal'
|
279 |
// class to close the modal.
|
280 |
$('.ctools-close-modal', context).once('ctools-close-modal') |
281 |
.click(function() {
|
282 |
Drupal.CTools.Modal.dismiss(); |
283 |
return false; |
284 |
}); |
285 |
} |
286 |
}; |
287 |
|
288 |
// The following are implementations of AJAX responder commands.
|
289 |
|
290 |
/**
|
291 |
* AJAX responder command to place HTML within the modal.
|
292 |
*/
|
293 |
Drupal.CTools.Modal.modal_display = function(ajax, response, status) { |
294 |
if ($('#modalContent').length == 0) { |
295 |
Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(ajax.element)); |
296 |
} |
297 |
$('#modal-title').html(response.title); |
298 |
// Simulate an actual page load by scrolling to the top after adding the
|
299 |
// content. This is helpful for allowing users to see error messages at the
|
300 |
// top of a form, etc.
|
301 |
$('#modal-content').html(response.output).scrollTop(0); |
302 |
$(document).trigger('CToolsAttachBehaviors', $('#modalContent')); |
303 |
|
304 |
// Attach behaviors within a modal dialog.
|
305 |
var settings = response.settings || ajax.settings || Drupal.settings;
|
306 |
Drupal.attachBehaviors($('#modalContent'), settings); |
307 |
|
308 |
if ($('#modal-content').hasClass('ctools-modal-loading')) { |
309 |
$('#modal-content').removeClass('ctools-modal-loading'); |
310 |
} |
311 |
else {
|
312 |
// If the modal was already shown, and we are simply replacing its
|
313 |
// content, then focus on the first focusable element in the modal.
|
314 |
// (When first showing the modal, focus will be placed on the close
|
315 |
// button by the show() function called above.)
|
316 |
$('#modal-content :focusable:first').focus(); |
317 |
} |
318 |
} |
319 |
|
320 |
/**
|
321 |
* AJAX responder command to dismiss the modal.
|
322 |
*/
|
323 |
Drupal.CTools.Modal.modal_dismiss = function(command) { |
324 |
Drupal.CTools.Modal.dismiss(); |
325 |
$('link.ctools-temporary-css').remove(); |
326 |
} |
327 |
|
328 |
/**
|
329 |
* Display loading
|
330 |
*/
|
331 |
//Drupal.CTools.AJAX.commands.modal_loading = function(command) {
|
332 |
Drupal.CTools.Modal.modal_loading = function(command) { |
333 |
Drupal.CTools.Modal.modal_display({ |
334 |
output: Drupal.theme(Drupal.CTools.Modal.currentSettings.throbberTheme),
|
335 |
title: Drupal.CTools.Modal.currentSettings.loadingText
|
336 |
}); |
337 |
} |
338 |
|
339 |
/**
|
340 |
* Find a URL for an AJAX button.
|
341 |
*
|
342 |
* The URL for this gadget will be composed of the values of items by
|
343 |
* taking the ID of this item and adding -url and looking for that
|
344 |
* class. They need to be in the form in order since we will
|
345 |
* concat them all together using '/'.
|
346 |
*/
|
347 |
Drupal.CTools.Modal.findURL = function(item) { |
348 |
var url = ''; |
349 |
var url_class = '.' + $(item).attr('id') + '-url'; |
350 |
$(url_class).each(
|
351 |
function() {
|
352 |
var $this = $(this); |
353 |
if (url && $this.val()) { |
354 |
url += '/';
|
355 |
} |
356 |
url += $this.val();
|
357 |
}); |
358 |
return url;
|
359 |
}; |
360 |
|
361 |
|
362 |
/**
|
363 |
* modalContent
|
364 |
* @param content string to display in the content box
|
365 |
* @param css obj of css attributes
|
366 |
* @param animation (fadeIn, slideDown, show)
|
367 |
* @param speed (valid animation speeds slow, medium, fast or # in ms)
|
368 |
* @param modalClass class added to div#modalContent
|
369 |
*/
|
370 |
Drupal.CTools.Modal.modalContent = function(content, css, animation, speed, modalClass) { |
371 |
// If our animation isn't set, make it just show/pop
|
372 |
if (!animation) {
|
373 |
animation = 'show';
|
374 |
} |
375 |
else {
|
376 |
// If our animation isn't "fadeIn" or "slideDown" then it always is show
|
377 |
if (animation != 'fadeIn' && animation != 'slideDown') { |
378 |
animation = 'show';
|
379 |
} |
380 |
} |
381 |
|
382 |
if (!speed && 0 !== speed) { |
383 |
speed = 'fast';
|
384 |
} |
385 |
|
386 |
// Build our base attributes and allow them to be overriden
|
387 |
css = jQuery.extend({ |
388 |
position: 'absolute', |
389 |
left: '0px', |
390 |
margin: '0px', |
391 |
background: '#000', |
392 |
opacity: '.55' |
393 |
}, css); |
394 |
|
395 |
// Add opacity handling for IE.
|
396 |
css.filter = 'alpha(opacity=' + (100 * css.opacity) + ')'; |
397 |
content.hide(); |
398 |
|
399 |
// If we already have modalContent, remove it.
|
400 |
if ($('#modalBackdrop').length) $('#modalBackdrop').remove(); |
401 |
if ($('#modalContent').length) $('#modalContent').remove(); |
402 |
|
403 |
// position code lifted from http://www.quirksmode.org/viewport/compatibility.html
|
404 |
if (self.pageYOffset) { // all except Explorer |
405 |
var wt = self.pageYOffset;
|
406 |
} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict |
407 |
var wt = document.documentElement.scrollTop;
|
408 |
} else if (document.body) { // all other Explorers |
409 |
var wt = document.body.scrollTop;
|
410 |
} |
411 |
|
412 |
// Get our dimensions
|
413 |
|
414 |
// Get the docHeight and (ugly hack) add 50 pixels to make sure we dont have a *visible* border below our div
|
415 |
var docHeight = $(document).height() + 50; |
416 |
var docWidth = $(document).width(); |
417 |
var winHeight = $(window).height(); |
418 |
var winWidth = $(window).width(); |
419 |
if( docHeight < winHeight ) docHeight = winHeight;
|
420 |
|
421 |
// Create our divs
|
422 |
$('body').append('<div id="modalBackdrop" class="backdrop-' + modalClass + '" style="z-index: 1000; display: none;"></div><div id="modalContent" class="modal-' + modalClass + '" style="z-index: 1001; position: absolute;">' + $(content).html() + '</div>'); |
423 |
|
424 |
// Get a list of the tabbable elements in the modal content.
|
425 |
var getTabbableElements = function () { |
426 |
var tabbableElements = $('#modalContent :tabbable'), |
427 |
radioButtons = tabbableElements.filter('input[type="radio"]');
|
428 |
|
429 |
// The list of tabbable elements from jQuery is *almost* right. The
|
430 |
// exception is with groups of radio buttons. The list from jQuery will
|
431 |
// include all radio buttons, when in fact, only the selected radio button
|
432 |
// is tabbable, and if no radio buttons in a group are selected, then only
|
433 |
// the first is tabbable.
|
434 |
if (radioButtons.length > 0) { |
435 |
// First, build up an index of which groups have an item selected or not.
|
436 |
var anySelected = {};
|
437 |
radioButtons.each(function () {
|
438 |
var name = this.name; |
439 |
|
440 |
if (typeof anySelected[name] === 'undefined') { |
441 |
anySelected[name] = radioButtons.filter('input[name="' + name + '"]:checked').length !== 0; |
442 |
} |
443 |
}); |
444 |
|
445 |
// Next filter out the radio buttons that aren't really tabbable.
|
446 |
var found = {};
|
447 |
tabbableElements = tabbableElements.filter(function () {
|
448 |
var keep = true; |
449 |
|
450 |
if (this.type == 'radio') { |
451 |
if (anySelected[this.name]) { |
452 |
// Only keep the selected one.
|
453 |
keep = this.checked;
|
454 |
} |
455 |
else {
|
456 |
// Only keep the first one.
|
457 |
if (found[this.name]) { |
458 |
keep = false;
|
459 |
} |
460 |
found[this.name] = true; |
461 |
} |
462 |
} |
463 |
|
464 |
return keep;
|
465 |
}); |
466 |
} |
467 |
|
468 |
return tabbableElements.get();
|
469 |
}; |
470 |
|
471 |
// Keyboard and focus event handler ensures only modal elements gain focus.
|
472 |
modalEventHandler = function( event ) { |
473 |
target = null;
|
474 |
if ( event ) { //Mozilla |
475 |
target = event.target; |
476 |
} else { //IE |
477 |
event = window.event; |
478 |
target = event.srcElement; |
479 |
} |
480 |
|
481 |
var parents = $(target).parents().get(); |
482 |
for (var i = 0; i < parents.length; ++i) { |
483 |
var position = $(parents[i]).css('position'); |
484 |
if (position == 'absolute' || position == 'fixed') { |
485 |
return true; |
486 |
} |
487 |
} |
488 |
|
489 |
if ($(target).is('#modalContent, body') || $(target).filter('*:visible').parents('#modalContent').length) { |
490 |
// Allow the event only if target is a visible child node
|
491 |
// of #modalContent.
|
492 |
return true; |
493 |
} |
494 |
else {
|
495 |
getTabbableElements()[0].focus();
|
496 |
} |
497 |
|
498 |
event.preventDefault(); |
499 |
}; |
500 |
$('body').bind( 'focus', modalEventHandler ); |
501 |
$('body').bind( 'keypress', modalEventHandler ); |
502 |
|
503 |
// Keypress handler Ensures you can only TAB to elements within the modal.
|
504 |
// Based on the psuedo-code from WAI-ARIA 1.0 Authoring Practices section
|
505 |
// 3.3.1 "Trapping Focus".
|
506 |
modalTabTrapHandler = function (evt) { |
507 |
// We only care about the TAB key.
|
508 |
if (evt.which != 9) { |
509 |
return true; |
510 |
} |
511 |
|
512 |
var tabbableElements = getTabbableElements(),
|
513 |
firstTabbableElement = tabbableElements[0],
|
514 |
lastTabbableElement = tabbableElements[tabbableElements.length - 1],
|
515 |
singleTabbableElement = firstTabbableElement == lastTabbableElement, |
516 |
node = evt.target; |
517 |
|
518 |
// If this is the first element and the user wants to go backwards, then
|
519 |
// jump to the last element.
|
520 |
if (node == firstTabbableElement && evt.shiftKey) {
|
521 |
if (!singleTabbableElement) {
|
522 |
lastTabbableElement.focus(); |
523 |
} |
524 |
return false; |
525 |
} |
526 |
// If this is the last element and the user wants to go forwards, then
|
527 |
// jump to the first element.
|
528 |
else if (node == lastTabbableElement && !evt.shiftKey) { |
529 |
if (!singleTabbableElement) {
|
530 |
firstTabbableElement.focus(); |
531 |
} |
532 |
return false; |
533 |
} |
534 |
// If this element isn't in the dialog at all, then jump to the first
|
535 |
// or last element to get the user into the game.
|
536 |
else if ($.inArray(node, tabbableElements) == -1) { |
537 |
// Make sure the node isn't in another modal (ie. WYSIWYG modal).
|
538 |
var parents = $(node).parents().get(); |
539 |
for (var i = 0; i < parents.length; ++i) { |
540 |
var position = $(parents[i]).css('position'); |
541 |
if (position == 'absolute' || position == 'fixed') { |
542 |
return true; |
543 |
} |
544 |
} |
545 |
|
546 |
if (evt.shiftKey) {
|
547 |
lastTabbableElement.focus(); |
548 |
} |
549 |
else {
|
550 |
firstTabbableElement.focus(); |
551 |
} |
552 |
} |
553 |
}; |
554 |
$('body').bind('keydown', modalTabTrapHandler); |
555 |
|
556 |
// Create our content div, get the dimensions, and hide it
|
557 |
var modalContent = $('#modalContent').css('top','-1000px'); |
558 |
var $modalHeader = modalContent.find('.modal-header'); |
559 |
var mdcTop = wt + Math.max((winHeight / 2) - (modalContent.outerHeight() / 2), 0); |
560 |
var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2); |
561 |
$('#modalBackdrop').css(css).css('top', 0).css('height', docHeight + 'px').css('width', docWidth + 'px').show(); |
562 |
modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).hide()[animation](speed); |
563 |
|
564 |
// Bind a click for closing the modalContent
|
565 |
modalContentClose = function(){close(); return false;}; |
566 |
$('.close', $modalHeader).bind('click', modalContentClose); |
567 |
|
568 |
// Bind a keypress on escape for closing the modalContent
|
569 |
modalEventEscapeCloseHandler = function(event) { |
570 |
if (event.keyCode == 27) { |
571 |
close(); |
572 |
return false; |
573 |
} |
574 |
}; |
575 |
|
576 |
$(document).bind('keydown', modalEventEscapeCloseHandler); |
577 |
|
578 |
// Per WAI-ARIA 1.0 Authoring Practices, initial focus should be on the
|
579 |
// close button, but we should save the original focus to restore it after
|
580 |
// the dialog is closed.
|
581 |
var oldFocus = document.activeElement;
|
582 |
$('.close', $modalHeader).focus(); |
583 |
|
584 |
// Close the open modal content and backdrop
|
585 |
function close() { |
586 |
// Unbind the events
|
587 |
$(window).unbind('resize', modalContentResize); |
588 |
$('body').unbind( 'focus', modalEventHandler); |
589 |
$('body').unbind( 'keypress', modalEventHandler ); |
590 |
$('body').unbind( 'keydown', modalTabTrapHandler ); |
591 |
$('.close', $modalHeader).unbind('click', modalContentClose); |
592 |
$(document).unbind('keydown', modalEventEscapeCloseHandler); |
593 |
$(document).trigger('CToolsDetachBehaviors', $('#modalContent')); |
594 |
|
595 |
// Closing animation.
|
596 |
switch (animation) {
|
597 |
case 'fadeIn': |
598 |
modalContent.fadeOut(speed, modalContentRemove); |
599 |
break;
|
600 |
|
601 |
case 'slideDown': |
602 |
modalContent.slideUp(speed, modalContentRemove); |
603 |
break;
|
604 |
|
605 |
case 'show': |
606 |
modalContent.hide(speed, modalContentRemove); |
607 |
break;
|
608 |
} |
609 |
} |
610 |
|
611 |
// Remove the content.
|
612 |
modalContentRemove = function () { |
613 |
$('#modalContent').remove(); |
614 |
$('#modalBackdrop').remove(); |
615 |
|
616 |
// Restore focus to where it was before opening the dialog.
|
617 |
$(oldFocus).focus();
|
618 |
}; |
619 |
|
620 |
// Move and resize the modalBackdrop and modalContent on window resize.
|
621 |
modalContentResize = function () { |
622 |
// Reset the backdrop height/width to get accurate document size.
|
623 |
$('#modalBackdrop').css('height', '').css('width', ''); |
624 |
|
625 |
// Position code lifted from:
|
626 |
// http://www.quirksmode.org/viewport/compatibility.html
|
627 |
if (self.pageYOffset) { // all except Explorer |
628 |
var wt = self.pageYOffset;
|
629 |
} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict |
630 |
var wt = document.documentElement.scrollTop;
|
631 |
} else if (document.body) { // all other Explorers |
632 |
var wt = document.body.scrollTop;
|
633 |
} |
634 |
|
635 |
// Get our heights
|
636 |
var docHeight = $(document).height(); |
637 |
var docWidth = $(document).width(); |
638 |
var winHeight = $(window).height(); |
639 |
var winWidth = $(window).width(); |
640 |
if( docHeight < winHeight ) docHeight = winHeight;
|
641 |
|
642 |
// Get where we should move content to
|
643 |
var modalContent = $('#modalContent'); |
644 |
var mdcTop = wt + Math.max((winHeight / 2) - (modalContent.outerHeight() / 2), 0); |
645 |
var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2); |
646 |
|
647 |
// Apply the changes
|
648 |
$('#modalBackdrop').css('height', docHeight + 'px').css('width', docWidth + 'px').show(); |
649 |
modalContent.css('top', mdcTop + 'px').css('left', mdcLeft + 'px').show(); |
650 |
}; |
651 |
$(window).bind('resize', modalContentResize); |
652 |
}; |
653 |
|
654 |
/**
|
655 |
* unmodalContent
|
656 |
* @param content (The jQuery object to remove)
|
657 |
* @param animation (fadeOut, slideUp, show)
|
658 |
* @param speed (valid animation speeds slow, medium, fast or # in ms)
|
659 |
*/
|
660 |
Drupal.CTools.Modal.unmodalContent = function(content, animation, speed) |
661 |
{ |
662 |
// If our animation isn't set, make it just show/pop
|
663 |
if (!animation) { var animation = 'show'; } else { |
664 |
// If our animation isn't "fade" then it always is show
|
665 |
if (( animation != 'fadeOut' ) && ( animation != 'slideUp')) animation = 'show'; |
666 |
} |
667 |
// Set a speed if we dont have one
|
668 |
if ( !speed ) var speed = 'fast'; |
669 |
|
670 |
// Unbind the events we bound
|
671 |
$(window).unbind('resize', modalContentResize); |
672 |
$('body').unbind('focus', modalEventHandler); |
673 |
$('body').unbind('keypress', modalEventHandler); |
674 |
$('body').unbind( 'keydown', modalTabTrapHandler ); |
675 |
var $modalContent = $('#modalContent'); |
676 |
var $modalHeader = $modalContent.find('.modal-header'); |
677 |
$('.close', $modalHeader).unbind('click', modalContentClose); |
678 |
$('body').unbind('keypress', modalEventEscapeCloseHandler); |
679 |
$(document).trigger('CToolsDetachBehaviors', $modalContent); |
680 |
|
681 |
// jQuery magic loop through the instances and run the animations or removal.
|
682 |
content.each(function(){
|
683 |
if ( animation == 'fade' ) { |
684 |
$('#modalContent').fadeOut(speed, function() { |
685 |
$('#modalBackdrop').fadeOut(speed, function() { |
686 |
$(this).remove(); |
687 |
}); |
688 |
$(this).remove(); |
689 |
}); |
690 |
} else {
|
691 |
if ( animation == 'slide' ) { |
692 |
$('#modalContent').slideUp(speed,function() { |
693 |
$('#modalBackdrop').slideUp(speed, function() { |
694 |
$(this).remove(); |
695 |
}); |
696 |
$(this).remove(); |
697 |
}); |
698 |
} else {
|
699 |
$('#modalContent').remove(); |
700 |
$('#modalBackdrop').remove(); |
701 |
} |
702 |
} |
703 |
}); |
704 |
}; |
705 |
|
706 |
$(function() { |
707 |
Drupal.ajax.prototype.commands.modal_display = Drupal.CTools.Modal.modal_display; |
708 |
Drupal.ajax.prototype.commands.modal_dismiss = Drupal.CTools.Modal.modal_dismiss; |
709 |
}); |
710 |
|
711 |
})(jQuery); |