]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/FilteringTreeSelect.js
no need for a store if there is no tree data (don't attempt to access dataList[0])
[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.dataList = [];
38                 var self = this;
39                 dojo.forEach(this.tree, function(node) { self._makeNodeList(node); });
40                 if(this.dataList.length > 0) {
41                     this.store = new dojo.data.ItemFileReadStore(
42                         {data:fieldmapper[this.dataList[0].classname].toStoreData(this.dataList)});
43                 }
44                 this.inherited(arguments);
45             },
46
47             // Compile the tree down to a depth-first list of nodes
48             _makeNodeList : function(node) {
49                 this.dataList.push(node);
50                 for(var i in node[this.childField]()) 
51                     this._makeNodeList(node[this.childField]()[i]);
52             },
53
54             // For each item, find the depth at display time by searching up the tree.
55             _getMenuLabelFromItem : function(item) {
56                 var pad = -this.defaultPad;
57                 var self = this;
58
59                 function processItem(list) {
60                     if(!list.length) return;
61                     var pitem = list[0];
62                     pad += self.defaultPad;
63                     var parentId = self.store.getValue(pitem, self.parentField);
64                     self.store.fetch({onComplete:processItem, query:{id:''+parentId}});
65                 }
66                 processItem([item]);
67
68                 return {
69                     html: true,
70                     label: '<div style="padding-left:'+pad+'px;">' +
71                         this.store.getValue(item, this.labelAttr) + '</div>'
72                 }
73             }
74         }
75     );
76 }