linkit.js 4.2 KB

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