]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/FilteringTreeSelect.js
make ndoe depth calculation more efficient and run at startup time to speed up displa...
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / FilteringTreeSelect.js
1 /* EXAMPLES:
2
3 <input jsId='ftree' dojoType="openils.widget.FilteringTreeSelect" searchAttr='shortname' labelAttr='shortname' tree='myTree'/>
4
5 --- OR --
6
7 var tree = new openils.widget.FilteringTreeSelect(null, parentDiv);
8 tree.searchAttr = 'shortname';
9 tree.labelAttr = 'shortname';
10 tree.parentField = 'parent_ou';
11 tree1.tree = fieldmapper.aou.globalOrgTree;
12 tree1.startup();
13
14 */
15
16 if(!dojo._hasResource["openils.widget.FilteringTreeSelect"]){
17     dojo.provide("openils.widget.FilteringTreeSelect");
18     dojo.require("dijit.form.FilteringSelect");
19
20     dojo.declare(
21         "openils.widget.FilteringTreeSelect", [dijit.form.FilteringSelect], {
22
23             defaultPad : 10,
24             parentField : 'parent',
25             labelAttr : 'name',
26             childField : 'children',
27             tree : null,
28
29             startup : function() {
30                 this.tree = (typeof this.tree == 'string') ? 
31                         dojox.jsonPath.query(window, '$.' + this.tree, {evalType:"RESULT"}) : this.tree;
32                 if(!this.tree) {
33                     console.log("openils.widget.FilteringTreeSelect: Tree needed!");
34                     return;
35                 }
36                 if(!dojo.isArray(this.tree)) this.tree = [this.tree];
37                 this.className = this.tree[0].classname;
38                 this.dataList = [];
39                 var self = this;
40                 dojo.forEach(this.tree, function(node) { self._makeNodeList(node); });
41                 if(this.dataList.length > 0) {
42                     var storeData = fieldmapper[this.className].initStoreData();
43                     storeData.items = this.dataList;
44                     this.store = new dojo.data.ItemFileReadStore({data:storeData});
45                 }
46                 this.inherited(arguments);
47             },
48
49             // Compile the tree down to a depth-first list of dojo data items
50             _makeNodeList : function(node, depth) {
51                 if(!depth) depth = 0;
52                 var storeItem = node.toStoreItem();
53                 storeItem._depth = depth++;
54                 this.dataList.push(storeItem);
55                 for(var i in node[this.childField]()) 
56                     this._makeNodeList(node[this.childField]()[i], depth);
57             },
58
59             // For each item, find the depth at display time by searching up the tree.
60             _getMenuLabelFromItem : function(item) {
61                 return {
62                     html: true,
63                     label: '<div style="padding-left:'+ (item._depth * this.defaultPad) +'px;">' +
64                         this.store.getValue(item, this.labelAttr) + '</div>'
65                 }
66             }
67         }
68     );
69 }