ea865e7e7340403b3ffdc9f5d3017aa54deb29de
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / depth_selector.js
1
2 attachEvt( "common", "locationChanged", updateLoc );
3
4 var _ds;
5 var _libselspan;
6 var _libselslink;
7 var _dselspan;
8 var _newlocation = null;
9
10 function depthSelInit() {
11         _ds = $('depth_selector'); 
12         _ds.onchange = depthSelectorChanged;
13         _libselspan = $('lib_selector_span');
14         _libsellink = $('lib_selector_link');
15         _dselspan = $('depth_selector_span');
16
17         if( getLocation() == globalOrgTree.id() ) {
18                 unHideMe( _libselspan );
19                 _libsellink.onclick = _opacHandleLocationTagClick;
20         } else {
21                 unHideMe( _dselspan );
22                 buildLocationSelector();
23         }
24
25         setSelector(_ds,        getDepth());
26         _newlocation = getLocation();
27 }
28
29
30 var orgTreeIsBuilt = false;
31 function _opacHandleLocationTagClick() {
32
33         swapCanvas(G.ui.common.org_container);
34
35         if(!orgTreeIsBuilt) {
36                 drawOrgTree();
37                 orgTreeIsBuilt = true;
38         }
39
40 }
41
42 function depthSelGetDepth() {
43         var depth = parseInt(_ds.options[_ds.selectedIndex].value);
44         if(isNaN(depth)) depth = 0;
45         return depth;
46 }
47
48 function depthSelectorChanged() {
49         var i = _ds.selectedIndex;
50         if( i == _ds.options.length - 1 ) {
51                 setSelector( _ds, getDepth() );
52                 _opacHandleLocationTagClick();
53         } else { 
54                 /* this re-submits the search when they change the search range
55                         disabled for testing...
56                         */
57                 /*runEvt('common', 'depthChanged');*/ 
58         }
59 }
60
61 var chooseAnotherNode;
62 function buildLocationSelector(newLoc) {
63
64         var loc;
65         if(newLoc != null) loc = newLoc;
66         else loc = getLocation();
67
68         if( loc == globalOrgTree.id() ) return;
69
70         var selector = _ds;
71         if(!chooseAnotherNode) 
72                 chooseAnotherNode = selector.removeChild(
73                         selector.getElementsByTagName("option")[0]);
74         var node = chooseAnotherNode;
75         removeChildren(selector);
76         
77         var location = findOrgUnit(loc);
78         var type = findOrgType(location.ou_type());
79
80         while( type && location ) {
81                 var n = node.cloneNode(true);   
82                 n.setAttribute("value", type.depth());
83                 removeChildren(n);
84                 n.appendChild(text(type.opac_label()));
85                 selector.appendChild(n);
86                 location = findOrgUnit(location.parent_ou());
87                 if(location) type = findOrgType(location.ou_type());
88                 else type = null;
89         }
90
91         selector.appendChild(node);
92 }
93
94 function getNewSearchDepth() { return newSearchDepth; }
95 function getNewSearchLocation() { return (isNull(_newlocation)) ? LOCATION : _newlocation; }
96 function depthSelGetNewLoc() { return getNewSearchLocation(); }
97
98 function updateLoc(location, depth) {
99         if( depth != null ) {
100                 if(depth != 0 ){
101                         _libsellink.onclick = _opacHandleLocationTagClick;
102                         if( location == globalOrgTree.id() ) {
103                                 hideMe( _dselspan );
104                                 unHideMe( _libselspan );
105                         } else {
106                                 buildLocationSelector(location);
107                                 hideMe( _libselspan );
108                                 unHideMe( _dselspan );
109                         }
110                 }
111
112                 setSelector(_ds, depth);
113                 newSearchDepth = depth;
114         }
115
116         _newlocation = location;
117         runEvt('common','locationUpdated', location);
118 }
119
120
121