77913690201f933b9eba56556248359159d57a5e
[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     dojo.require("dojo.data.ItemFileWriteStore");
20
21     dojo.declare(
22         "openils.widget.FilteringTreeSelect", [dijit.form.FilteringSelect], {
23
24             defaultPad : 10,
25             parentField : 'parent',
26             labelAttr : 'name',
27             childField : 'children',
28             disableQuery : null,
29             tree : null,
30
31             startup : function() {
32                 this.tree = (typeof this.tree == 'string') ? 
33                         dojox.jsonPath.query(window, '$.' + this.tree, {evalType:"RESULT"}) : this.tree;
34                 if(!this.tree) {
35                     console.log("openils.widget.FilteringTreeSelect: Tree needed!");
36                     return;
37                 }
38                 if(!dojo.isArray(this.tree)) this.tree = [this.tree];
39                 this.className = this.tree[0].classname;
40                 this.dataList = [];
41                 var self = this;
42                 dojo.forEach(this.tree, function(node) { self._makeNodeList(node); });
43                 if(this.dataList.length > 0) {
44                     var storeData = fieldmapper[this.className].initStoreData();
45                     storeData.items = this.dataList;
46                     this.store = new dojo.data.ItemFileWriteStore({data:storeData});
47                 }
48                 this.inherited(arguments);
49
50                 if(this.dataList.length > 0 && this.disableQuery)  
51                     this._setDisabled();
52             },
53
54             _setDisabled : function() {
55
56                 // tag disabled items
57                 this.store.fetch({
58                     query : this.disableQuery,
59                     onItem : function(item) { item._disabled = 'true'; }
60                 });
61
62                 // disallow selecting of disabled items
63                 var self = this;
64                 dojo.connect(this, 'onChange', 
65                     function(ident) { 
66                         if(!ident) return;
67                         self.store.fetchItemByIdentity({
68                             identity : ident,
69                             onItem : function(item) {
70                                 if(item._disabled == 'true')
71                                     self.attr('value', '');
72                             }
73                         });
74                     }
75                 );
76             },
77
78             // Compile the tree down to a depth-first list of dojo data items
79             _makeNodeList : function(node, depth) {
80                 if(!depth) depth = 0;
81                 var storeItem = node.toStoreItem();
82                 storeItem._depth = depth++;
83                 this.dataList.push(storeItem);
84
85                 for(var i in node[this.childField]()) 
86                     this._makeNodeList(node[this.childField]()[i], depth);
87             },
88
89             // For each item, find the depth at display time by searching up the tree.
90             _getMenuLabelFromItem : function(item) {
91
92                 var style = 'padding-left:'+ (item._depth * this.defaultPad) +'px;';
93
94                 if(item._disabled == 'true') // TODO: external CSS
95                     style += 'background-color:#CCC;cursor:wait'; 
96
97                 return {
98                     html: true,
99                     label: '<div style="'+style+'">' + this.store.getValue(item, this.labelAttr) + '</div>'
100                 }
101             },
102         }
103     );
104 }