]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/FilteringTreeSelect.js
default searchAttr to valueField (assumes a markup-based implementation ...); add...
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / FilteringTreeSelect.js
1 /* EXAMPLES:
2
3 <div dojoType="openils.widget.FilteringTreeSelect" tree="orgTree" parentField="parent_ou" searchAttr="shortname"/>
4 <div dojoType="openils.widget.FilteringTreeSelect" tree="grpTree"/>
5
6 The tree attribute is expected to be a tree-shaped pile of OpenSRF objects.
7
8 */
9
10 if(!dojo._hasResource["openils.widget.FilteringTreeSelect"]){
11     dojo.provide("openils.widget.FilteringTreeSelect");
12     dojo.require("dijit.form.FilteringSelect");
13     dojo.require('dojo.data.ItemFileReadStore');
14     dojo.require('openils.Util');
15     dojo.require("dojox.jsonPath");
16
17     dojo.declare(
18         "openils.widget.FilteringTreeSelect", [dijit.form.ComboBox], 
19         {
20
21             defaultPad : 6,
22             childField : 'children',
23             parentField : 'parent',
24             valueField : '',
25             tree : "",
26             options : [],
27             values : [],
28
29             startup : function () {
30                 this.labelAttr = '_label'; // force it
31                 this.labelType = 'html'; // force it
32
33                 this._tree = (typeof this.tree == 'string') ? 
34                         dojox.jsonPath.query(window, '$.' + this.tree, {evalType:"RESULT"}) : this.tree;
35                 if (!dojo.isArray(this._tree)) this._tree = [ this._tree ];
36
37                 this._datalist = [];
38                 if (!this.valueField) this.valueField = this._tree[0].Identifier;
39                 if (!this.searchAttr) this.searchAttr = this.valueField;
40
41                 var self = this;
42                 this._tree.forEach( function (node) { self._add_items( node, 0 ); } );
43
44                 this.store = new dojo.data.ItemFileReadStore({
45                     data : {
46                         identifier : this.valueField,
47                         label : this.labelAttr,
48                         items : this._datalist
49                     }
50                 });
51
52                 this.inherited(arguments);
53             },
54
55             _add_items : function ( node, depth ) {
56                 var lpad = this.defaultPad * depth++;
57
58                 var data = node.toStoreItem();
59                 data._label = '<div style="padding-left:'+lpad+'px;">' + node[this.searchAttr]() + '</div>';
60
61                 this._datalist.push( data );
62
63                 var kids = node[this.childField]();
64                 for (var j in kids) {
65                     this._add_items( kids[j], depth );
66                 }
67
68                 return null;
69             }
70         }
71     );
72 }