linkit.field.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file
  3. * Linkit field ui functions
  4. */
  5. (function($, behavior) {
  6. 'use strict';
  7. Drupal.behaviors[behavior] = {
  8. attach: function(context, settings) {
  9. // If there is no fields, just stop here.
  10. if (undefined === settings.linkit || null === settings.linkit.fields) {
  11. return false;
  12. }
  13. $.each(settings.linkit.fields, function(i, instance) {
  14. $('#' + instance.source, context).once(behavior, function() {
  15. var element = this;
  16. $('.linkit-field-' + instance.source).click(function(event) {
  17. event.preventDefault();
  18. // Only care about selection if the element is a textarea.
  19. if ('textarea' === element.nodeName.toLowerCase()) {
  20. instance.selection = Drupal.linkit.getDialogHelper('field').getSelection(element);
  21. }
  22. Drupal.settings.linkit.currentInstance = instance;
  23. Drupal.linkit.createModal();
  24. });
  25. });
  26. });
  27. }
  28. };
  29. /**
  30. * Linkit field dialog helper.
  31. */
  32. Drupal.linkit.registerDialogHelper('field', {
  33. afterInit: function() {},
  34. /**
  35. * Insert the link into the field.
  36. *
  37. * @param {Object} data
  38. * The link object.
  39. */
  40. insertLink: function(data) {
  41. var instance = Drupal.settings.linkit.currentInstance,
  42. // Call the insert plugin.
  43. link = Drupal.linkit.getInsertPlugin(instance.insertPlugin).insert(data, instance);
  44. if (instance.hasOwnProperty('selection')) {
  45. // Replace the selection and insert the link there.
  46. this.replaceSelection(instance.source, instance.selection, link);
  47. }
  48. else if (instance.hasOwnProperty('titleField')) {
  49. // The "linkContent" property will always be present when AJAX used.
  50. // Otherwise, if you use simple insert without autocomplete, then this
  51. // property will be undefined and title field should not be filled in.
  52. //
  53. // @see Drupal.behaviors.linkitSearch.attach
  54. if (instance.hasOwnProperty('linkContent')) {
  55. this.replaceFieldValue(instance.titleField, instance.linkContent);
  56. }
  57. // The "path" property will always be present after dialog was
  58. // opened and contain raw URL.
  59. //
  60. // @see Drupal.behaviors.linkitDashboard.attach
  61. this.replaceFieldValue(instance.source, data.path);
  62. }
  63. else {
  64. // Replace the field value.
  65. this.replaceFieldValue(instance.source, link);
  66. }
  67. },
  68. /**
  69. * Get field selection.
  70. */
  71. getSelection: function(element) {
  72. var object = {
  73. start: element.value.length,
  74. end: element.value.length,
  75. length: 0,
  76. text: ''
  77. };
  78. // Mozilla and DOM 3.0.
  79. if ('selectionStart' in element) {
  80. var length = element.selectionEnd - element.selectionStart;
  81. object = {
  82. start: element.selectionStart,
  83. end: element.selectionEnd,
  84. length: length,
  85. text: element.value.substr(element.selectionStart, length)
  86. };
  87. }
  88. // IE.
  89. else if (document.selection) {
  90. element.focus();
  91. var range = document.selection.createRange(),
  92. textRange = element.createTextRange(),
  93. textRangeDuplicate = textRange.duplicate();
  94. textRangeDuplicate.moveToBookmark(range.getBookmark());
  95. textRange.setEndPoint('EndToStart', textRangeDuplicate);
  96. if (!(range || textRange)) {
  97. return object;
  98. }
  99. // For some reason IE doesn't always count the \n and \r in the length.
  100. var text_part = range.text.replace(/[\r\n]/g, '.'),
  101. text_whole = element.value.replace(/[\r\n]/g, '.'),
  102. the_start = text_whole.indexOf(text_part, textRange.text.length);
  103. object = {
  104. start: the_start,
  105. end: the_start + text_part.length,
  106. length: text_part.length,
  107. text: range.text
  108. };
  109. }
  110. return object;
  111. },
  112. /**
  113. * Replace the field selection.
  114. */
  115. replaceSelection: function(id, selection, text) {
  116. var field = this.getField(id);
  117. field.value = field.value.substr(0, selection.start) + text + field.value.substr(selection.end, field.value.length);
  118. },
  119. /**
  120. * Replace the field value.
  121. */
  122. replaceFieldValue: function(id, text) {
  123. this.getField(id).value = text;
  124. },
  125. getField: function(id) {
  126. return document.getElementById(id);
  127. }
  128. });
  129. })(jQuery, 'linkitField');