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