source: products/quintagroup.plonetabs/branches/tests/quintagroup/plonetabs/tests/ecmaunits/js/nodeutilities.js @ 3402

Last change on this file since 3402 was 895, checked in by piv, 17 years ago

optimized PASMemberdataHandler handler

File size: 2.5 KB
Line 
1// These methods have all been deprecated in favor of using jquery.
2
3function wrapNode(node, wrappertype, wrapperclass){
4    /* utility function to wrap a node in an arbitrary element of type "wrappertype"
5     * with a class of "wrapperclass" */
6    jq(node).wrap('<' + wrappertype + '>').parent().addClass(wrapperclass);
7};
8
9function nodeContained(innernode, outernode){
10    // check if innernode is contained in outernode
11    return jq(innernode).parents()
12        .filter(function() { return this == outernode }).length > 0;
13};
14
15function findContainer(node, func) {
16    // Starting with the given node, find the nearest containing element
17    // for which the given function returns true.
18    p = jq(node).parents().filter(func);
19    return p.length ? p.get(0) : false;
20};
21
22function hasClassName(node, class_name) {
23    return jq(node).hasClass(class_name);
24};
25
26function addClassName(node, class_name) {
27    jq(node).addClass(class_name);
28};
29
30function removeClassName(node, class_name) {
31    jq(node).removeClass(class_name);
32};
33
34function replaceClassName(node, old_class, new_class, ignore_missing) {
35    if (ignore_missing || jq(node).hasClass(old_class))
36        jq(node).removeClass(old_class).addClass(new_class);
37};
38
39function walkTextNodes(node, func, data) {
40    // find all nodes, and call a function for all it's textnodes
41    jq(node).find('*').andSelf().contents().each(function() {
42        if (this.nodeType == 3) func(this, data);
43    });
44};
45
46function getInnerTextCompatible(node) {
47    return jq(node).text();
48};
49
50function getInnerTextFast(node) {
51    return jq(node).text();
52};
53
54/* This function reorder nodes in the DOM.
55 * fetch_func - the function which returns the value for comparison
56 * cmp_func - the compare function, if not provided then the string of the
57 * value returned by fetch_func is used.
58 */
59function sortNodes(nodes, fetch_func, cmp_func) {
60    // wrapper for sorting
61    var SortNodeWrapper = function(node) {
62        this.value = fetch_func(node);
63        this.cloned_node = node.cloneNode(true);
64    }
65    SortNodeWrapper.prototype.toString = function() {
66        return this.value.toString ? this.value.toString() : this.value;
67    }
68
69    // wrap nodes
70    var items = jq(nodes).map(function() { return new SortNodeWrapper(this); });
71
72    //sort
73    if (cmp_func) items.sort(cmp_func);
74    else          items.sort();
75
76    // reorder nodes
77    jq.each(items, function(i) { jq(nodes[i]).replace(this.cloned_node); });
78};
79
80function copyChildNodes(srcNode, dstNode) {
81    jq(srcNode).children().clone().appendTo(jq(dstNode));
82}
Note: See TracBrowser for help on using the repository browser.