PageRenderTime 108ms CodeModel.GetById 33ms app.highlight 7ms RepoModel.GetById 51ms app.codeStats 0ms

/javascripts/lib/src/widgets/tree/TreeFilter.js

https://bitbucket.org/ksokmesa/sina-asian
JavaScript | 114 lines | 79 code | 4 blank | 31 comment | 26 complexity | c0629413d0e1a9881a6855ad67dac402 MD5 | raw file
Possible License(s): GPL-3.0
  1/*!
  2 * Ext JS Library 3.2.1
  3 * Copyright(c) 2006-2010 Ext JS, Inc.
  4 * licensing@extjs.com
  5 * http://www.extjs.com/license
  6 */
  7/**
  8 * @class Ext.tree.TreeFilter
  9 * Note this class is experimental and doesn't update the indent (lines) or expand collapse icons of the nodes
 10 * @param {TreePanel} tree
 11 * @param {Object} config (optional)
 12 */
 13Ext.tree.TreeFilter = function(tree, config){
 14    this.tree = tree;
 15    this.filtered = {};
 16    Ext.apply(this, config);
 17};
 18
 19Ext.tree.TreeFilter.prototype = {
 20    clearBlank:false,
 21    reverse:false,
 22    autoClear:false,
 23    remove:false,
 24
 25     /**
 26     * Filter the data by a specific attribute.
 27     * @param {String/RegExp} value Either string that the attribute value
 28     * should start with or a RegExp to test against the attribute
 29     * @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
 30     * @param {TreeNode} startNode (optional) The node to start the filter at.
 31     */
 32    filter : function(value, attr, startNode){
 33        attr = attr || "text";
 34        var f;
 35        if(typeof value == "string"){
 36            var vlen = value.length;
 37            // auto clear empty filter
 38            if(vlen == 0 && this.clearBlank){
 39                this.clear();
 40                return;
 41            }
 42            value = value.toLowerCase();
 43            f = function(n){
 44                return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
 45            };
 46        }else if(value.exec){ // regex?
 47            f = function(n){
 48                return value.test(n.attributes[attr]);
 49            };
 50        }else{
 51            throw 'Illegal filter type, must be string or regex';
 52        }
 53        this.filterBy(f, null, startNode);
 54	},
 55
 56    /**
 57     * Filter by a function. The passed function will be called with each
 58     * node in the tree (or from the startNode). If the function returns true, the node is kept
 59     * otherwise it is filtered. If a node is filtered, its children are also filtered.
 60     * @param {Function} fn The filter function
 61     * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
 62     */
 63    filterBy : function(fn, scope, startNode){
 64        startNode = startNode || this.tree.root;
 65        if(this.autoClear){
 66            this.clear();
 67        }
 68        var af = this.filtered, rv = this.reverse;
 69        var f = function(n){
 70            if(n == startNode){
 71                return true;
 72            }
 73            if(af[n.id]){
 74                return false;
 75            }
 76            var m = fn.call(scope || n, n);
 77            if(!m || rv){
 78                af[n.id] = n;
 79                n.ui.hide();
 80                return false;
 81            }
 82            return true;
 83        };
 84        startNode.cascade(f);
 85        if(this.remove){
 86           for(var id in af){
 87               if(typeof id != "function"){
 88                   var n = af[id];
 89                   if(n && n.parentNode){
 90                       n.parentNode.removeChild(n);
 91                   }
 92               }
 93           }
 94        }
 95    },
 96
 97    /**
 98     * Clears the current filter. Note: with the "remove" option
 99     * set a filter cannot be cleared.
100     */
101    clear : function(){
102        var t = this.tree;
103        var af = this.filtered;
104        for(var id in af){
105            if(typeof id != "function"){
106                var n = af[id];
107                if(n){
108                    n.ui.show();
109                }
110            }
111        }
112        this.filtered = {};
113    }
114};