modal.js 18 KB

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