[open-ils-commits] r17205 - trunk/Open-ILS/web/js/dojo/openils (erickson)
svn at svn.open-ils.org
svn at svn.open-ils.org
Thu Aug 12 23:49:38 EDT 2010
Author: erickson
Date: 2010-08-12 23:49:35 -0400 (Thu, 12 Aug 2010)
New Revision: 17205
Modified:
trunk/Open-ILS/web/js/dojo/openils/Util.js
Log:
DOM text/pattern highlighter
openils.Util.hilightNode(
node,
[pattern1, pattern2, ...],
{classname:foo}
);
Modified: trunk/Open-ILS/web/js/dojo/openils/Util.js
===================================================================
--- trunk/Open-ILS/web/js/dojo/openils/Util.js 2010-08-13 02:39:49 UTC (rev 17204)
+++ trunk/Open-ILS/web/js/dojo/openils/Util.js 2010-08-13 03:49:35 UTC (rev 17205)
@@ -319,5 +319,46 @@
return openils.Util.objectProperties(o);
}
+ /**
+ * Highlight instances of each pattern in the given DOM node
+ * Inspired by the jquery plugin
+ * http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
+ */
+ openils.Util.hilightNode = function(node, patterns, args) {
+
+ var hclass = (args && args.classname) ? args.classname : 'oils-hightlight';
+
+ function _hilightNode(node, pat) {
+
+ if(node.nodeType == 3) {
+
+ pat = pat.toUpperCase();
+ var text = node.data.toUpperCase();
+ var pos = -1;
+
+ // find each instance of pat in the current node
+ while( (pos = text.indexOf(pat, pos + 1)) >= 0 ) {
+
+ var wrapper = dojo.create('span', {className : hclass});
+ var midnode = node.splitText(pos);
+ midnode.splitText(pat.length);
+ wrapper.appendChild(midnode.cloneNode(true));
+ midnode.parentNode.replaceChild(wrapper, midnode);
+ }
+
+ } else if(node.nodeType == 1 && node.childNodes[0]) {
+
+ // not a text node? have you checked the children?
+ dojo.forEach(
+ node.childNodes,
+ function(child) { _hilightNode(child, pat); }
+ );
+ }
+ }
+
+ // descend the tree for each pattern, since nodes are changed during highlighting
+ dojo.forEach(patterns, function(pat) { _hilightNode(node, pat); });
+ };
+
}
More information about the open-ils-commits
mailing list