linkit.dashboard.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @file
  3. * Linkit dashboard functions
  4. */
  5. (function ($) {
  6. Drupal.behaviors.linkitDashboard = {
  7. attach: function (context, settings) {
  8. // Bind the insert link button.
  9. $('.linkit-insert', context).once('linkit-insert', function() {
  10. $('.linkit-insert', context).click(function(event) {
  11. event.preventDefault();
  12. // Call the insertLink() function.
  13. Drupal.linkit.getDialogHelper(Drupal.settings.linkit.currentInstance.helper).insertLink(Drupal.linkit.getLink());
  14. // Close the dialog.
  15. Drupal.linkit.modalClose();
  16. });
  17. });
  18. // Bind the close link.
  19. $('#linkit-cancel', context).once('linkit-cancel', function() {
  20. $('#linkit-cancel', context).bind('click', Drupal.linkit.modalClose);
  21. });
  22. // Run the validation if the path field is populated directly.
  23. $('#edit-linkit-path', context).bind('keyup paste input propertychange', function(){
  24. Drupal.linkit.requiredFieldsValidation();
  25. });
  26. $(".ui-dialog-titlebar").show();
  27. // Run required field validation.
  28. Drupal.linkit.requiredFieldsValidation();
  29. if (!Drupal.settings.linkit.currentInstance.suppressProfileChanger) {
  30. // Make the profile changer
  31. Drupal.linkit.profileChanger(context);
  32. }
  33. if (Drupal.settings.linkit.IMCEurl && !$('#linkit-imce', context).length) {
  34. var $imceButton = $('<input />')
  35. .attr({type: 'button', id: 'linkit-imce', name: 'linkit-imce'})
  36. .addClass('form-submit')
  37. .val(Drupal.t('Open file browser'))
  38. .insertAfter($('.form-item-linkit-search', context))
  39. .click(function(e) {
  40. e.preventDefault();
  41. Drupal.linkit.openFileBrowser();
  42. });
  43. }
  44. }
  45. };
  46. /**
  47. * Check for mandatory fields in the form and disable for submissions
  48. * if any of the fields are empty.
  49. */
  50. Drupal.linkit.requiredFieldsValidation = function() {
  51. var allowed = true;
  52. $('#linkit-modal .required').each(function() {
  53. if (!$(this).val()) {
  54. allowed = false;
  55. return false;
  56. }
  57. });
  58. if (allowed) {
  59. $('#linkit-modal .linkit-insert')
  60. .removeAttr('disabled')
  61. .removeClass('form-button-disabled');
  62. }
  63. else {
  64. $('#linkit-modal .linkit-insert')
  65. .attr('disabled', 'disabled')
  66. .addClass('form-button-disabled');
  67. }
  68. };
  69. /**
  70. * Open the IMCE file browser
  71. */
  72. Drupal.linkit.openFileBrowser = function () {
  73. window.open(decodeURIComponent(Drupal.settings.linkit.IMCEurl), '', 'width=760,height=560,resizable=1');
  74. };
  75. /**
  76. * When a file is inserted through IMCE, this function is called.
  77. * See IMCE api for details.
  78. *
  79. * @param file
  80. * The file object that was selected inside IMCE
  81. * @param win
  82. * The IMCE window object
  83. */
  84. Drupal.linkit.IMCECallback = function(file, win) {
  85. Drupal.linkit.populateFields({
  86. path: win.imce.decode(Drupal.settings.basePath +
  87. Drupal.settings.linkit.publicFilesDirectory + '/' + file.relpath)
  88. });
  89. win.close();
  90. };
  91. /**
  92. * Populate fields on the dashboard.
  93. *
  94. * @param link
  95. * An object with the following properties (all are optional):
  96. * - path: The anchor's href.
  97. * - attributes: An object with additional attributes for the anchor element.
  98. */
  99. Drupal.linkit.populateFields = function(link) {
  100. link = link || {};
  101. link.attributes = link.attributes || {};
  102. $('#linkit-modal .linkit-path-element').val(link.path);
  103. $.each(link.attributes, function(name, value) {
  104. $('#linkit-modal .linkit-attributes .linkit-attribute-' + name).val(value);
  105. });
  106. // Run required field validation.
  107. Drupal.linkit.requiredFieldsValidation();
  108. };
  109. /**
  110. * Retrieve a link object by extracting values from the form.
  111. *
  112. * @return
  113. * The link object.
  114. */
  115. Drupal.linkit.getLink = function() {
  116. var link = {
  117. path: $('#linkit-modal .linkit-path-element ').val(),
  118. attributes: {}
  119. };
  120. $.each(Drupal.linkit.additionalAttributes(), function(f, name) {
  121. link.attributes[name] = $('#linkit-modal .linkit-attributes .linkit-attribute-' + name).val();
  122. });
  123. return link;
  124. };
  125. /**
  126. * Retrieve a list of the currently available additional attributes in the
  127. * dashboard. The attribute "href" is excluded.
  128. *
  129. * @return
  130. * An array with the names of the attributes.
  131. */
  132. Drupal.linkit.additionalAttributes = function() {
  133. var attributes = [];
  134. $('#linkit-modal .linkit-attributes .linkit-attribute').each(function() {
  135. // Remove the 'linkit_' prefix.
  136. attributes.push($(this).attr('name').substr(7));
  137. });
  138. return attributes;
  139. };
  140. Drupal.linkit.profileChanger = function(context) {
  141. $('#linkit-profile-changer > div.form-item', context).once('linkit-change-profile', function() {
  142. var target = $(this);
  143. var toggler = $('<div id="linkit-profile-changer-toggler"></div>')
  144. .html(Drupal.t('Change profile'))
  145. .click(function() {
  146. target.slideToggle();
  147. });
  148. $(this).after(toggler);
  149. });
  150. $('#linkit-profile-changer .form-radio', context).each(function() {
  151. var id = $(this).attr('id');
  152. var profile = $(this).val();
  153. if (typeof Drupal.ajax[id] != 'undefined') {
  154. // @TODO: Jquery 1.5 accept success setting to be an array of functions.
  155. // But we have to wait for jquery to get updated in Drupal core.
  156. // In the meantime we have to override it.
  157. Drupal.ajax[id].options.success = function (response, status) {
  158. if (typeof response == 'string') {
  159. response = $.parseJSON(response);
  160. }
  161. // Update the autocomplete url.
  162. Drupal.settings.linkit.currentInstance.autocompletePathParsed = Drupal.settings.linkit.autocompletePath.replace('___profile___', profile);
  163. // Call the ajax success method.
  164. Drupal.ajax[id].success(response, status);
  165. $('#linkit-profile-changer > div.form-item').slideToggle();
  166. };
  167. }
  168. });
  169. };
  170. Drupal.behaviors.linkitSearch = {
  171. attach: function(context, settings) {
  172. $('.linkit-search-element').once('linkit-search', function() {
  173. // Create a synonym for this to reduce code confusion.
  174. var searchElement = $('.linkit-search-element');
  175. var callbacks = {
  176. constructURL: function(path, search) {
  177. return path + encodeURIComponent(search);
  178. },
  179. insertSuggestionList: function($results, $input) {
  180. var top = $input.position().top + $input.outerHeight() - 5;
  181. $results.width($input.outerWidth()).css({
  182. position: 'absolute',
  183. // High value because of other overlays like
  184. // wysiwyg fullscreen (TinyMCE) mode.
  185. zIndex: 211000,
  186. maxHeight: $(window).height() - (top + 20)
  187. })
  188. .hide()
  189. .insertAfter($input);
  190. },
  191. select: function(result) {
  192. if (typeof result == 'undefined') {
  193. return false;
  194. }
  195. // Only change the link text if it is empty.
  196. if (typeof result.disabled != 'undefined' && result.disabled) {
  197. return false;
  198. }
  199. Drupal.linkit.populateFields({
  200. path: result.path
  201. });
  202. // Store the result title (Used when no selection is made by the user).
  203. Drupal.settings.linkit.currentInstance.linkContent = result.title;
  204. $('.linkit-path-element', context).focus();
  205. }
  206. };
  207. searchElement.betterAutocomplete('init', Drupal.settings.linkit.currentInstance.autocompletePathParsed, Drupal.settings.linkit.currentInstance.autocomplete, callbacks);
  208. });
  209. }
  210. };
  211. })(jQuery);