/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

  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. */
  13. Ext.tree.TreeFilter = function(tree, config){
  14. this.tree = tree;
  15. this.filtered = {};
  16. Ext.apply(this, config);
  17. };
  18. Ext.tree.TreeFilter.prototype = {
  19. clearBlank:false,
  20. reverse:false,
  21. autoClear:false,
  22. remove:false,
  23. /**
  24. * Filter the data by a specific attribute.
  25. * @param {String/RegExp} value Either string that the attribute value
  26. * should start with or a RegExp to test against the attribute
  27. * @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
  28. * @param {TreeNode} startNode (optional) The node to start the filter at.
  29. */
  30. filter : function(value, attr, startNode){
  31. attr = attr || "text";
  32. var f;
  33. if(typeof value == "string"){
  34. var vlen = value.length;
  35. // auto clear empty filter
  36. if(vlen == 0 && this.clearBlank){
  37. this.clear();
  38. return;
  39. }
  40. value = value.toLowerCase();
  41. f = function(n){
  42. return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
  43. };
  44. }else if(value.exec){ // regex?
  45. f = function(n){
  46. return value.test(n.attributes[attr]);
  47. };
  48. }else{
  49. throw 'Illegal filter type, must be string or regex';
  50. }
  51. this.filterBy(f, null, startNode);
  52. },
  53. /**
  54. * Filter by a function. The passed function will be called with each
  55. * node in the tree (or from the startNode). If the function returns true, the node is kept
  56. * otherwise it is filtered. If a node is filtered, its children are also filtered.
  57. * @param {Function} fn The filter function
  58. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
  59. */
  60. filterBy : function(fn, scope, startNode){
  61. startNode = startNode || this.tree.root;
  62. if(this.autoClear){
  63. this.clear();
  64. }
  65. var af = this.filtered, rv = this.reverse;
  66. var f = function(n){
  67. if(n == startNode){
  68. return true;
  69. }
  70. if(af[n.id]){
  71. return false;
  72. }
  73. var m = fn.call(scope || n, n);
  74. if(!m || rv){
  75. af[n.id] = n;
  76. n.ui.hide();
  77. return false;
  78. }
  79. return true;
  80. };
  81. startNode.cascade(f);
  82. if(this.remove){
  83. for(var id in af){
  84. if(typeof id != "function"){
  85. var n = af[id];
  86. if(n && n.parentNode){
  87. n.parentNode.removeChild(n);
  88. }
  89. }
  90. }
  91. }
  92. },
  93. /**
  94. * Clears the current filter. Note: with the "remove" option
  95. * set a filter cannot be cleared.
  96. */
  97. clear : function(){
  98. var t = this.tree;
  99. var af = this.filtered;
  100. for(var id in af){
  101. if(typeof id != "function"){
  102. var n = af[id];
  103. if(n){
  104. n.ui.show();
  105. }
  106. }
  107. }
  108. this.filtered = {};
  109. }
  110. };