Advertisement
Guest User

Untitled

a guest
May 5th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1.  
  2. jQuery.extend({
  3. annotation: function (node, re, nodeName, className, settings) {
  4. if (node.nodeType === 3) {
  5. var match = node.data.match(re);
  6. if (match) {
  7. var annotation = document.createElement(nodeName || 'span');
  8. annotation.className = className || 'annotation';
  9. var wordNode = node.splitText(match.index);
  10. wordNode.splitText(match[0].length);
  11. var wordClone = wordNode.cloneNode(true);
  12. annotation.appendChild(wordClone);
  13. wordNode.parentNode.replaceChild(annotation, wordNode);
  14. console.log(wordNode);
  15. $(annotation).webuiPopover({
  16. html: true,
  17. trigger: 'click',
  18. placement: 'auto',
  19. arrow: false,
  20. closeable: true,
  21. title: settings.title,
  22. content: settings.content});
  23. //TODO: fix strange behavior
  24. return 1; //skip added node in parent
  25. }
  26. } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
  27. !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
  28. !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already annotationed
  29. for (var i = 0; i < node.childNodes.length; i++) {
  30. i += jQuery.annotation(node.childNodes[i], re, nodeName, className);
  31. }
  32. }
  33. return 0;
  34. }
  35. });
  36.  
  37. jQuery.fn.unannotation = function (options) {
  38. var settings = { className: 'annotation', element: 'span' };
  39. jQuery.extend(settings, options);
  40.  
  41. return this.find(settings.element + "." + settings.className).each(function () {
  42. var parent = this.parentNode;
  43. parent.replaceChild(this.firstChild, this);
  44. parent.normalize();
  45. }).end();
  46. };
  47.  
  48. jQuery.fn.annotation = function (words, options) {
  49. var settings = { className: 'annotation', element: 'span',
  50. caseSensitive: false, wordsOnly: false };
  51. jQuery.extend(settings, options);
  52.  
  53.  
  54. if (words.constructor === String) {
  55. words = [words];
  56. }
  57. words = jQuery.grep(words, function(word, i){
  58. return word != '';
  59. });
  60. words = jQuery.map(words, function(word, i) {
  61. return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  62. });
  63. if (words.length == 0) { return this; };
  64.  
  65. var flag = settings.caseSensitive ? "" : "i";
  66. var pattern = "(" + words.join("|") + ")";
  67. if (settings.wordsOnly) {
  68. pattern = "\\b" + pattern + "\\b";
  69. }
  70. var re = new RegExp(pattern, flag);
  71.  
  72. return this.each(function () {
  73. jQuery.annotation(this, re, settings.element, settings.className, settings);
  74. });
  75. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement