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