modal.js 24 KB

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