linkit.field.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @file
  3. * Linkit field ui functions
  4. */
  5. (function ($) {
  6. Drupal.behaviors.linkit_field = {
  7. attach : function(context, settings) {
  8. // If there is no fields, just stop here.
  9. if (settings.linkit == undefined || settings.linkit.fields == null) {
  10. return false;
  11. }
  12. $.each(settings.linkit.fields, function(field_name, field) {
  13. $('#' + field_name, context).once('linkit_field', function() {
  14. $('.linkit-field-' + field_name).click(function() {
  15. // Set profile.
  16. Drupal.settings.linkit.currentInstance.profile = Drupal.settings.linkit.fields[field_name].profile;
  17. // Set the name of the source field..
  18. Drupal.settings.linkit.currentInstance.source = field_name;
  19. // Set the source type.
  20. Drupal.settings.linkit.currentInstance.helper = 'field';
  21. // Only care about selection if the element is a textarea.
  22. if ($('textarea#' + field_name).length) {
  23. var selection = Drupal.linkit.getDialogHelper('field').getSelection($('#' + field_name).get(0));
  24. // Save the selection.
  25. Drupal.settings.linkit.currentInstance.selection = selection;
  26. }
  27. // Suppress profile changer.
  28. Drupal.settings.linkit.currentInstance.suppressProfileChanger = true;
  29. // Create the modal.
  30. Drupal.linkit.createModal();
  31. return false;
  32. });
  33. });
  34. });
  35. }
  36. };
  37. /**
  38. * Linkit field dialog helper.
  39. */
  40. Drupal.linkit.registerDialogHelper('field', {
  41. init : function() {},
  42. afterInit : function () {},
  43. /**
  44. * Insert the link into the field.
  45. *
  46. * @param {Object} link
  47. * The link object.
  48. */
  49. insertLink : function(data) {
  50. var source = $('#' + Drupal.settings.linkit.currentInstance.source),
  51. field_settings = Drupal.settings.linkit.fields[Drupal.settings.linkit.currentInstance.source],
  52. // Call the insert plugin.
  53. link = Drupal.linkit.getInsertPlugin(field_settings.insert_plugin).insert(data, field_settings);
  54. if (typeof Drupal.settings.linkit.currentInstance.selection != 'undefined') {
  55. // Replace the selection and insert the link there.
  56. this.replaceSelection(source.get(0), Drupal.settings.linkit.currentInstance.selection, link);
  57. }
  58. else {
  59. // Replace the field value.
  60. this.replaceFieldValue(source.get(0), link);
  61. }
  62. // Link field can have a title field. If they have, we populate the title
  63. // field with the search result title if any.
  64. if (typeof field_settings.title_field != 'undefined' && typeof Drupal.settings.linkit.currentInstance.linkContent != 'undefined') {
  65. this.replaceFieldValue($('#' + field_settings.title_field).get(0), Drupal.settings.linkit.currentInstance.linkContent);
  66. }
  67. },
  68. /**
  69. * Get field selection.
  70. */
  71. getSelection : function(e) {
  72. // Mozilla and DOM 3.0.
  73. if ('selectionStart' in e) {
  74. var l = e.selectionEnd - e.selectionStart;
  75. return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
  76. }
  77. // IE.
  78. else if(document.selection) {
  79. e.focus();
  80. var r = document.selection.createRange(),
  81. tr = e.createTextRange(),
  82. tr2 = tr.duplicate();
  83. tr2.moveToBookmark(r.getBookmark());
  84. tr.setEndPoint('EndToStart',tr2);
  85. if (r == null || tr == null) {
  86. return { start: e.value.length, end: e.value.length, length: 0, text: '' };
  87. }
  88. // For some reason IE doesn't always count the \n and \r in the length.
  89. var text_part = r.text.replace(/[\r\n]/g,'.'),
  90. text_whole = e.value.replace(/[\r\n]/g,'.'),
  91. the_start = text_whole.indexOf(text_part, tr.text.length);
  92. return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };
  93. }
  94. // Browser not supported.
  95. else {
  96. return { start: e.value.length, end: e.value.length, length: 0, text: '' };
  97. }
  98. },
  99. /**
  100. * Replace the field selection.
  101. */
  102. replaceSelection : function (e, selection, text) {
  103. var start_pos = selection.start;
  104. var end_pos = start_pos + text.length;
  105. e.value = e.value.substr(0, start_pos) + text + e.value.substr(selection.end, e.value.length);
  106. },
  107. /**
  108. * Replace the field value.
  109. */
  110. replaceFieldValue : function (e, text) {
  111. e.value = text;
  112. }
  113. });
  114. })(jQuery);