linkit.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @file
  3. * Linkit dialog functions.
  4. */
  5. (function ($) {
  6. // Create the Linkit namespaces.
  7. Drupal.linkit = Drupal.linkit || { 'excludeIdSelectors': {} };
  8. Drupal.linkit.currentInstance = Drupal.linkit.currentInstance || {};
  9. Drupal.linkit.dialogHelper = Drupal.linkit.dialogHelper || {};
  10. Drupal.linkit.insertPlugins = Drupal.linkit.insertPlugins || {};
  11. // Exclude ids from ajax_html_ids during AJAX requests.
  12. Drupal.linkit.excludeIdSelectors.ckeditor = ['[id^="cke_"]'];
  13. Drupal.linkit.excludeIdSelectors.tokens = ['[id^="token-"]'];
  14. /**
  15. * Create the modal dialog.
  16. */
  17. Drupal.linkit.createModal = function() {
  18. // Create the modal dialog element.
  19. Drupal.linkit.createModalElement()
  20. // Create jQuery UI Dialog.
  21. .dialog(Drupal.linkit.modalOptions())
  22. // Remove the title bar from the modal.
  23. .siblings(".ui-dialog-titlebar").hide();
  24. // Make the modal seem "fixed".
  25. $(window).bind("scroll resize", function() {
  26. $('#linkit-modal').dialog('option', 'position', ['center', 50]);
  27. });
  28. // Get modal content.
  29. Drupal.linkit.getDashboard();
  30. };
  31. /**
  32. * Create and append the modal element.
  33. */
  34. Drupal.linkit.createModalElement = function() {
  35. // Create a new div and give it an ID of linkit-modal.
  36. // This is the dashboard container.
  37. var linkitModal = $('<div id="linkit-modal"></div>');
  38. // Create a modal div in the <body>.
  39. $('body').append(linkitModal);
  40. return linkitModal;
  41. };
  42. /**
  43. * Default jQuery dialog options used when creating the Linkit modal.
  44. */
  45. Drupal.linkit.modalOptions = function() {
  46. return {
  47. dialogClass: 'linkit-wrapper',
  48. modal: true,
  49. draggable: false,
  50. resizable: false,
  51. width: 520,
  52. position: ['center', 50],
  53. minHeight: 0,
  54. zIndex: 210000,
  55. close: Drupal.linkit.modalClose,
  56. open: function (event, ui) {
  57. // Change the overlay style.
  58. $('.ui-widget-overlay').css({
  59. opacity: 0.5,
  60. filter: 'Alpha(Opacity=50)',
  61. backgroundColor: '#FFFFFF'
  62. });
  63. },
  64. title: 'Linkit'
  65. };
  66. };
  67. /**
  68. * Close the Linkit modal.
  69. */
  70. Drupal.linkit.modalClose = function (e) {
  71. $('#linkit-modal').dialog('destroy').remove();
  72. // Make sure the current intstance settings are removed when the modal is
  73. // closed.
  74. Drupal.settings.linkit.currentInstance = {};
  75. // The event object does not have a preventDefault member in
  76. // Internet Explorer prior to version 9.
  77. if (e && e.preventDefault) {
  78. e.preventDefault();
  79. }
  80. else {
  81. return false;
  82. }
  83. };
  84. /**
  85. *
  86. */
  87. Drupal.linkit.getDashboard = function () {
  88. // Create the AJAX object.
  89. var ajax_settings = {};
  90. ajax_settings.event = 'LinkitDashboard';
  91. ajax_settings.url = Drupal.settings.linkit.dashboardPath + Drupal.settings.linkit.currentInstance.profile;
  92. ajax_settings.progress = {
  93. type: 'throbber',
  94. message : Drupal.t('Loading Linkit dashboard...')
  95. };
  96. Drupal.ajax['linkit-modal'] = new Drupal.ajax('linkit-modal', $('#linkit-modal')[0], ajax_settings);
  97. // @TODO: Jquery 1.5 accept success setting to be an array of functions.
  98. // But we have to wait for jquery to get updated in Drupal core.
  99. // In the meantime we have to override it.
  100. Drupal.ajax['linkit-modal'].options.success = function (response, status) {
  101. if (typeof response == 'string') {
  102. response = $.parseJSON(response);
  103. }
  104. // Call the ajax success method.
  105. Drupal.ajax['linkit-modal'].success(response, status);
  106. // Run the afterInit function.
  107. var helper = Drupal.settings.linkit.currentInstance.helper;
  108. Drupal.linkit.getDialogHelper(helper).afterInit();
  109. // Set focus in the search field.
  110. $('#linkit-modal .linkit-search-element').focus();
  111. };
  112. // Update the autocomplete url.
  113. Drupal.settings.linkit.currentInstance.autocompletePathParsed =
  114. Drupal.settings.linkit.autocompletePath.replace('___profile___', Drupal.settings.linkit.currentInstance.profile);
  115. // Trigger the ajax event.
  116. $('#linkit-modal').trigger('LinkitDashboard');
  117. };
  118. /**
  119. * Register new dialog helper.
  120. */
  121. Drupal.linkit.registerDialogHelper = function(name, helper) {
  122. Drupal.linkit.dialogHelper[name] = helper;
  123. };
  124. /**
  125. * Get a dialog helper.
  126. *
  127. * @param {String} name
  128. * The name of helper.
  129. *
  130. * @return {Object}
  131. * Dialog helper object.
  132. */
  133. Drupal.linkit.getDialogHelper = function(name) {
  134. return Drupal.linkit.dialogHelper[name];
  135. };
  136. /**
  137. * Register new insert plugins.
  138. */
  139. Drupal.linkit.registerInsertPlugin = function(name, plugin) {
  140. Drupal.linkit.insertPlugins[name] = plugin;
  141. };
  142. /**
  143. * Get an insert plugin.
  144. */
  145. Drupal.linkit.getInsertPlugin = function(name) {
  146. return Drupal.linkit.insertPlugins[name];
  147. };
  148. var oldBeforeSerialize = (Drupal.ajax ? Drupal.ajax.prototype.beforeSerialize : false);
  149. if (oldBeforeSerialize) {
  150. /**
  151. * Filter the ajax_html_ids list sent in AJAX requests.
  152. *
  153. * This avoids hitting like max_input_vars, which defaults to 1000,
  154. * even with just a few active editor instances.
  155. */
  156. Drupal.ajax.prototype.beforeSerialize = function (element, options) {
  157. var ret = oldBeforeSerialize.call(this, element, options);
  158. var excludeSelectors = [];
  159. $.each(Drupal.linkit.excludeIdSelectors, function () {
  160. if ($.isArray(this)) {
  161. excludeSelectors = excludeSelectors.concat(this);
  162. }
  163. });
  164. if (excludeSelectors.length > 0) {
  165. options.data['ajax_html_ids[]'] = [];
  166. $('[id]:not(' + excludeSelectors.join(',') + ')').each(function () {
  167. options.data['ajax_html_ids[]'].push(this.id);
  168. });
  169. }
  170. return ret;
  171. }
  172. }
  173. })(jQuery);