modal.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. };
  49. var settings = {};
  50. $.extend(true, settings, defaults, Drupal.settings.CToolsModal, opts);
  51. if (Drupal.CTools.Modal.currentSettings && Drupal.CTools.Modal.currentSettings != settings) {
  52. Drupal.CTools.Modal.modal.remove();
  53. Drupal.CTools.Modal.modal = null;
  54. }
  55. Drupal.CTools.Modal.currentSettings = settings;
  56. var resize = function(e) {
  57. // When creating the modal, it actually exists only in a theoretical
  58. // place that is not in the DOM. But once the modal exists, it is in the
  59. // DOM so the context must be set appropriately.
  60. var context = e ? document : Drupal.CTools.Modal.modal;
  61. if (Drupal.CTools.Modal.currentSettings.modalSize.type == 'scale') {
  62. var width = $(window).width() * Drupal.CTools.Modal.currentSettings.modalSize.width;
  63. var height = $(window).height() * Drupal.CTools.Modal.currentSettings.modalSize.height;
  64. }
  65. else {
  66. var width = Drupal.CTools.Modal.currentSettings.modalSize.width;
  67. var height = Drupal.CTools.Modal.currentSettings.modalSize.height;
  68. }
  69. // Use the additionol pixels for creating the width and height.
  70. $('div.ctools-modal-content', context).css({
  71. 'width': width + Drupal.CTools.Modal.currentSettings.modalSize.addWidth + 'px',
  72. 'height': height + Drupal.CTools.Modal.currentSettings.modalSize.addHeight + 'px'
  73. });
  74. $('div.ctools-modal-content .modal-content', context).css({
  75. 'width': (width - Drupal.CTools.Modal.currentSettings.modalSize.contentRight) + 'px',
  76. 'height': (height - Drupal.CTools.Modal.currentSettings.modalSize.contentBottom) + 'px'
  77. });
  78. }
  79. if (!Drupal.CTools.Modal.modal) {
  80. Drupal.CTools.Modal.modal = $(Drupal.theme(settings.modalTheme));
  81. if (settings.modalSize.type == 'scale') {
  82. $(window).bind('resize', resize);
  83. }
  84. }
  85. resize();
  86. $('span.modal-title', Drupal.CTools.Modal.modal).html(Drupal.CTools.Modal.currentSettings.loadingText);
  87. Drupal.CTools.Modal.modalContent(Drupal.CTools.Modal.modal, settings.modalOptions, settings.animation, settings.animationSpeed);
  88. $('#modalContent .modal-content').html(Drupal.theme(settings.throbberTheme));
  89. // Position autocomplete results based on the scroll position of the modal.
  90. $('#modalContent .modal-content').delegate('input.form-autocomplete', 'keyup', function() {
  91. $('#autocomplete').css('top', $(this).position().top + $(this).outerHeight() + $(this).offsetParent().filter('#modal-content').scrollTop());
  92. });
  93. };
  94. /**
  95. * Hide the modal
  96. */
  97. Drupal.CTools.Modal.dismiss = function() {
  98. if (Drupal.CTools.Modal.modal) {
  99. Drupal.CTools.Modal.unmodalContent(Drupal.CTools.Modal.modal);
  100. }
  101. };
  102. /**
  103. * Provide the HTML to create the modal dialog.
  104. */
  105. Drupal.theme.prototype.CToolsModalDialog = function () {
  106. var html = ''
  107. html += ' <div id="ctools-modal">'
  108. html += ' <div class="ctools-modal-content">' // panels-modal-content
  109. html += ' <div class="modal-header">';
  110. html += ' <a class="close" href="#">';
  111. html += Drupal.CTools.Modal.currentSettings.closeText + Drupal.CTools.Modal.currentSettings.closeImage;
  112. html += ' </a>';
  113. html += ' <span id="modal-title" class="modal-title">&nbsp;</span>';
  114. html += ' </div>';
  115. html += ' <div id="modal-content" class="modal-content">';
  116. html += ' </div>';
  117. html += ' </div>';
  118. html += ' </div>';
  119. return html;
  120. }
  121. /**
  122. * Provide the HTML to create the throbber.
  123. */
  124. Drupal.theme.prototype.CToolsModalThrobber = function () {
  125. var html = '';
  126. html += ' <div id="modal-throbber">';
  127. html += ' <div class="modal-throbber-wrapper">';
  128. html += Drupal.CTools.Modal.currentSettings.throbber;
  129. html += ' </div>';
  130. html += ' </div>';
  131. return html;
  132. };
  133. /**
  134. * Figure out what settings string to use to display a modal.
  135. */
  136. Drupal.CTools.Modal.getSettings = function (object) {
  137. var match = $(object).attr('class').match(/ctools-modal-(\S+)/);
  138. if (match) {
  139. return match[1];
  140. }
  141. }
  142. /**
  143. * Click function for modals that can be cached.
  144. */
  145. Drupal.CTools.Modal.clickAjaxCacheLink = function () {
  146. Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this));
  147. return Drupal.CTools.AJAX.clickAJAXCacheLink.apply(this);
  148. };
  149. /**
  150. * Handler to prepare the modal for the response
  151. */
  152. Drupal.CTools.Modal.clickAjaxLink = function () {
  153. Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this));
  154. return false;
  155. };
  156. /**
  157. * Submit responder to do an AJAX submit on all modal forms.
  158. */
  159. Drupal.CTools.Modal.submitAjaxForm = function(e) {
  160. var $form = $(this);
  161. var url = $form.attr('action');
  162. setTimeout(function() { Drupal.CTools.AJAX.ajaxSubmit($form, url); }, 1);
  163. return false;
  164. }
  165. /**
  166. * Bind links that will open modals to the appropriate function.
  167. */
  168. Drupal.behaviors.ZZCToolsModal = {
  169. attach: function(context) {
  170. // Bind links
  171. // Note that doing so in this order means that the two classes can be
  172. // used together safely.
  173. /*
  174. * @todo remimplement the warm caching feature
  175. $('a.ctools-use-modal-cache', context).once('ctools-use-modal', function() {
  176. $(this).click(Drupal.CTools.Modal.clickAjaxCacheLink);
  177. Drupal.CTools.AJAX.warmCache.apply(this);
  178. });
  179. */
  180. $('area.ctools-use-modal, a.ctools-use-modal', context).once('ctools-use-modal', function() {
  181. var $this = $(this);
  182. $this.click(Drupal.CTools.Modal.clickAjaxLink);
  183. // Create a drupal ajax object
  184. var element_settings = {};
  185. if ($this.attr('href')) {
  186. element_settings.url = $this.attr('href');
  187. element_settings.event = 'click';
  188. element_settings.progress = { type: 'throbber' };
  189. }
  190. var base = $this.attr('href');
  191. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  192. });
  193. // Bind buttons
  194. $('input.ctools-use-modal, button.ctools-use-modal', context).once('ctools-use-modal', function() {
  195. var $this = $(this);
  196. $this.click(Drupal.CTools.Modal.clickAjaxLink);
  197. var button = this;
  198. var element_settings = {};
  199. // AJAX submits specified in this manner automatically submit to the
  200. // normal form action.
  201. element_settings.url = Drupal.CTools.Modal.findURL(this);
  202. if (element_settings.url == '') {
  203. element_settings.url = $(this).closest('form').attr('action');
  204. }
  205. element_settings.event = 'click';
  206. element_settings.setClick = true;
  207. var base = $this.attr('id');
  208. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  209. // Make sure changes to settings are reflected in the URL.
  210. $('.' + $(button).attr('id') + '-url').change(function() {
  211. Drupal.ajax[base].options.url = Drupal.CTools.Modal.findURL(button);
  212. });
  213. });
  214. // Bind our custom event to the form submit
  215. $('#modal-content form', context).once('ctools-use-modal', function() {
  216. var $this = $(this);
  217. var element_settings = {};
  218. element_settings.url = $this.attr('action');
  219. element_settings.event = 'submit';
  220. element_settings.progress = { 'type': 'throbber' }
  221. var base = $this.attr('id');
  222. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  223. Drupal.ajax[base].form = $this;
  224. $('input[type=submit], button', this).click(function(event) {
  225. Drupal.ajax[base].element = this;
  226. this.form.clk = this;
  227. // Stop autocomplete from submitting.
  228. if (Drupal.autocompleteSubmit && !Drupal.autocompleteSubmit()) {
  229. return false;
  230. }
  231. // An empty event means we were triggered via .click() and
  232. // in jquery 1.4 this won't trigger a submit.
  233. if (event.bubbles == undefined) {
  234. $(this.form).trigger('submit');
  235. return false;
  236. }
  237. });
  238. });
  239. // Bind a click handler to allow elements with the 'ctools-close-modal'
  240. // class to close the modal.
  241. $('.ctools-close-modal', context).once('ctools-close-modal')
  242. .click(function() {
  243. Drupal.CTools.Modal.dismiss();
  244. return false;
  245. });
  246. }
  247. };
  248. // The following are implementations of AJAX responder commands.
  249. /**
  250. * AJAX responder command to place HTML within the modal.
  251. */
  252. Drupal.CTools.Modal.modal_display = function(ajax, response, status) {
  253. if ($('#modalContent').length == 0) {
  254. Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(ajax.element));
  255. }
  256. $('#modal-title').html(response.title);
  257. // Simulate an actual page load by scrolling to the top after adding the
  258. // content. This is helpful for allowing users to see error messages at the
  259. // top of a form, etc.
  260. $('#modal-content').html(response.output).scrollTop(0);
  261. // Attach behaviors within a modal dialog.
  262. var settings = response.settings || ajax.settings || Drupal.settings;
  263. Drupal.attachBehaviors('#modalContent', settings);
  264. }
  265. /**
  266. * AJAX responder command to dismiss the modal.
  267. */
  268. Drupal.CTools.Modal.modal_dismiss = function(command) {
  269. Drupal.CTools.Modal.dismiss();
  270. $('link.ctools-temporary-css').remove();
  271. }
  272. /**
  273. * Display loading
  274. */
  275. //Drupal.CTools.AJAX.commands.modal_loading = function(command) {
  276. Drupal.CTools.Modal.modal_loading = function(command) {
  277. Drupal.CTools.Modal.modal_display({
  278. output: Drupal.theme(Drupal.CTools.Modal.currentSettings.throbberTheme),
  279. title: Drupal.CTools.Modal.currentSettings.loadingText
  280. });
  281. }
  282. /**
  283. * Find a URL for an AJAX button.
  284. *
  285. * The URL for this gadget will be composed of the values of items by
  286. * taking the ID of this item and adding -url and looking for that
  287. * class. They need to be in the form in order since we will
  288. * concat them all together using '/'.
  289. */
  290. Drupal.CTools.Modal.findURL = function(item) {
  291. var url = '';
  292. var url_class = '.' + $(item).attr('id') + '-url';
  293. $(url_class).each(
  294. function() {
  295. var $this = $(this);
  296. if (url && $this.val()) {
  297. url += '/';
  298. }
  299. url += $this.val();
  300. });
  301. return url;
  302. };
  303. /**
  304. * modalContent
  305. * @param content string to display in the content box
  306. * @param css obj of css attributes
  307. * @param animation (fadeIn, slideDown, show)
  308. * @param speed (valid animation speeds slow, medium, fast or # in ms)
  309. */
  310. Drupal.CTools.Modal.modalContent = function(content, css, animation, speed) {
  311. // If our animation isn't set, make it just show/pop
  312. if (!animation) {
  313. animation = 'show';
  314. }
  315. else {
  316. // If our animation isn't "fadeIn" or "slideDown" then it always is show
  317. if (animation != 'fadeIn' && animation != 'slideDown') {
  318. animation = 'show';
  319. }
  320. }
  321. if (!speed) {
  322. speed = 'fast';
  323. }
  324. // Build our base attributes and allow them to be overriden
  325. css = jQuery.extend({
  326. position: 'absolute',
  327. left: '0px',
  328. margin: '0px',
  329. background: '#000',
  330. opacity: '.55'
  331. }, css);
  332. // Add opacity handling for IE.
  333. css.filter = 'alpha(opacity=' + (100 * css.opacity) + ')';
  334. content.hide();
  335. // If we already have modalContent, remove it.
  336. if ($('#modalBackdrop').length) $('#modalBackdrop').remove();
  337. if ($('#modalContent').length) $('#modalContent').remove();
  338. // position code lifted from http://www.quirksmode.org/viewport/compatibility.html
  339. if (self.pageYOffset) { // all except Explorer
  340. var wt = self.pageYOffset;
  341. } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
  342. var wt = document.documentElement.scrollTop;
  343. } else if (document.body) { // all other Explorers
  344. var wt = document.body.scrollTop;
  345. }
  346. // Get our dimensions
  347. // Get the docHeight and (ugly hack) add 50 pixels to make sure we dont have a *visible* border below our div
  348. var docHeight = $(document).height() + 50;
  349. var docWidth = $(document).width();
  350. var winHeight = $(window).height();
  351. var winWidth = $(window).width();
  352. if( docHeight < winHeight ) docHeight = winHeight;
  353. // Create our divs
  354. $('body').append('<div id="modalBackdrop" style="z-index: 1000; display: none;"></div><div id="modalContent" style="z-index: 1001; position: absolute;">' + $(content).html() + '</div>');
  355. // Keyboard and focus event handler ensures focus stays on modal elements only
  356. modalEventHandler = function( event ) {
  357. target = null;
  358. if ( event ) { //Mozilla
  359. target = event.target;
  360. } else { //IE
  361. event = window.event;
  362. target = event.srcElement;
  363. }
  364. var parents = $(target).parents().get();
  365. for (var i = 0; i < parents.length; ++i) {
  366. var position = $(parents[i]).css('position');
  367. if (position == 'absolute' || position == 'fixed') {
  368. return true;
  369. }
  370. }
  371. if ($(target).is('#modalContent, body') || $(target).filter('*:visible').parents('#modalContent').length) {
  372. // Allow the event only if target is a visible child node
  373. // of #modalContent.
  374. return true;
  375. }
  376. else {
  377. $('#modalContent').focus();
  378. }
  379. event.preventDefault();
  380. };
  381. $('body').bind( 'focus', modalEventHandler );
  382. $('body').bind( 'keypress', modalEventHandler );
  383. // Create our content div, get the dimensions, and hide it
  384. var modalContent = $('#modalContent').css('top','-1000px');
  385. var mdcTop = wt + ( winHeight / 2 ) - ( modalContent.outerHeight() / 2);
  386. var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
  387. $('#modalBackdrop').css(css).css('top', 0).css('height', docHeight + 'px').css('width', docWidth + 'px').show();
  388. modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).hide()[animation](speed);
  389. // Bind a click for closing the modalContent
  390. modalContentClose = function(){close(); return false;};
  391. $('.close').bind('click', modalContentClose);
  392. // Bind a keypress on escape for closing the modalContent
  393. modalEventEscapeCloseHandler = function(event) {
  394. if (event.keyCode == 27) {
  395. close();
  396. return false;
  397. }
  398. };
  399. $(document).bind('keydown', modalEventEscapeCloseHandler);
  400. // Close the open modal content and backdrop
  401. function close() {
  402. // Unbind the events
  403. $(window).unbind('resize', modalContentResize);
  404. $('body').unbind( 'focus', modalEventHandler);
  405. $('body').unbind( 'keypress', modalEventHandler );
  406. $('.close').unbind('click', modalContentClose);
  407. $('body').unbind('keypress', modalEventEscapeCloseHandler);
  408. $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
  409. // Set our animation parameters and use them
  410. if ( animation == 'fadeIn' ) animation = 'fadeOut';
  411. if ( animation == 'slideDown' ) animation = 'slideUp';
  412. if ( animation == 'show' ) animation = 'hide';
  413. // Close the content
  414. modalContent.hide()[animation](speed);
  415. // Remove the content
  416. $('#modalContent').remove();
  417. $('#modalBackdrop').remove();
  418. };
  419. // Move and resize the modalBackdrop and modalContent on resize of the window
  420. modalContentResize = function(){
  421. // position code lifted from http://www.quirksmode.org/viewport/compatibility.html
  422. if (self.pageYOffset) { // all except Explorer
  423. var wt = self.pageYOffset;
  424. } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
  425. var wt = document.documentElement.scrollTop;
  426. } else if (document.body) { // all other Explorers
  427. var wt = document.body.scrollTop;
  428. }
  429. // Get our heights
  430. var docHeight = $(document).height();
  431. var docWidth = $(document).width();
  432. var winHeight = $(window).height();
  433. var winWidth = $(window).width();
  434. if( docHeight < winHeight ) docHeight = winHeight;
  435. // Get where we should move content to
  436. var modalContent = $('#modalContent');
  437. var mdcTop = wt + ( winHeight / 2 ) - ( modalContent.outerHeight() / 2);
  438. var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
  439. // Apply the changes
  440. $('#modalBackdrop').css('height', docHeight + 'px').css('width', docWidth + 'px').show();
  441. modalContent.css('top', mdcTop + 'px').css('left', mdcLeft + 'px').show();
  442. };
  443. $(window).bind('resize', modalContentResize);
  444. $('#modalContent').focus();
  445. };
  446. /**
  447. * unmodalContent
  448. * @param content (The jQuery object to remove)
  449. * @param animation (fadeOut, slideUp, show)
  450. * @param speed (valid animation speeds slow, medium, fast or # in ms)
  451. */
  452. Drupal.CTools.Modal.unmodalContent = function(content, animation, speed)
  453. {
  454. // If our animation isn't set, make it just show/pop
  455. if (!animation) { var animation = 'show'; } else {
  456. // If our animation isn't "fade" then it always is show
  457. if (( animation != 'fadeOut' ) && ( animation != 'slideUp')) animation = 'show';
  458. }
  459. // Set a speed if we dont have one
  460. if ( !speed ) var speed = 'fast';
  461. // Unbind the events we bound
  462. $(window).unbind('resize', modalContentResize);
  463. $('body').unbind('focus', modalEventHandler);
  464. $('body').unbind('keypress', modalEventHandler);
  465. $('.close').unbind('click', modalContentClose);
  466. $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
  467. // jQuery magic loop through the instances and run the animations or removal.
  468. content.each(function(){
  469. if ( animation == 'fade' ) {
  470. $('#modalContent').fadeOut(speed, function() {
  471. $('#modalBackdrop').fadeOut(speed, function() {
  472. $(this).remove();
  473. });
  474. $(this).remove();
  475. });
  476. } else {
  477. if ( animation == 'slide' ) {
  478. $('#modalContent').slideUp(speed,function() {
  479. $('#modalBackdrop').slideUp(speed, function() {
  480. $(this).remove();
  481. });
  482. $(this).remove();
  483. });
  484. } else {
  485. $('#modalContent').remove();
  486. $('#modalBackdrop').remove();
  487. }
  488. }
  489. });
  490. };
  491. $(function() {
  492. Drupal.ajax.prototype.commands.modal_display = Drupal.CTools.Modal.modal_display;
  493. Drupal.ajax.prototype.commands.modal_dismiss = Drupal.CTools.Modal.modal_dismiss;
  494. });
  495. })(jQuery);