4dfd061b533376a1c50b3be8a0ed6cd1098db6f3
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / search_bar.js
1 var searchBarExpanded = false;
2 /* our search selector boxes */
3 var _ts, _fs, _ds;
4
5 attachEvt( "common", "locationChanged", updateLoc );
6
7 var isFrontPage = false;
8
9
10 G.evt.common.init.push(searchBarInit);
11
12 /* if set by the org selector, this will be the location used the
13         next time the search is submitted */
14 var newSearchLocation; 
15 var newSearchDepth = null;
16
17 function searchBarInit() {
18
19         _ts = G.ui.searchbar.type_selector;
20         _ds = G.ui.searchbar.depth_selector;
21         _fs = G.ui.searchbar.form_selector;
22
23         G.ui.searchbar.text.focus();
24         //G.ui.searchbar.text.onkeypress = 
25         G.ui.searchbar.text.onkeydown = 
26                 function(evt) {if(userPressedEnter(evt)) { searchBarSubmit(); } };
27
28         G.ui.searchbar.submit.onclick = searchBarSubmit;
29
30         _ds.onchange = depthSelectorChanged;
31
32         if( getLocation() == globalOrgTree.id() ) {
33                 unHideMe( G.ui.searchbar.lib_sel_span );
34                 G.ui.searchbar.lib_sel_link.onclick = _opacHandleLocationTagClick;
35         } else {
36                 unHideMe( G.ui.searchbar.depth_sel_span );
37                 buildLocationSelector();
38         }
39
40         /* set up the selector objects, etc */
41         G.ui.searchbar.text.value = (getTerm() != null) ? getTerm() : "";
42         setSelector(_ts,        getStype());
43         setSelector(_ds,        getDepth());
44         setSelector(_fs,        getForm());
45
46 }
47
48 function _opacHandleLocationTagClick() {
49         /*
50         orgTreeSelector.openTo(  
51                 (newSearchLocation != null) ? parseInt(newSearchLocation) : getLocation(), true );
52                 */
53         swapCanvas(G.ui.common.org_container);
54 }
55
56 function depthSelectorChanged() {
57         var i = _ds.selectedIndex;
58         if( i == _ds.options.length - 1 ) {
59                 setSelector( _ds, getDepth() );
60                 _opacHandleLocationTagClick();
61
62         } else {
63                 if(!isFrontPage && (findCurrentPage() != MYOPAC)) {
64                         searchBarSubmit();
65                 }
66         }
67
68 }
69
70 var chooseAnotherNode;
71 function buildLocationSelector(newLoc) {
72
73         var loc;
74         if(newLoc != null) loc = newLoc;
75         else loc = getLocation();
76
77         if( loc == globalOrgTree.id() ) return;
78
79         var selector = G.ui.searchbar.depth_selector
80         if(!chooseAnotherNode) 
81                 chooseAnotherNode = selector.removeChild(selector.getElementsByTagName("option")[0]);
82         var node = chooseAnotherNode;
83         removeChildren(selector);
84         
85         var location = findOrgUnit(loc);
86         var type = findOrgType(location.ou_type());
87
88         while( type && location ) {
89                 var n = node.cloneNode(true);   
90                 n.setAttribute("value", type.depth());
91                 removeChildren(n);
92                 n.appendChild(text(type.opac_label()));
93                 selector.appendChild(n);
94                 location = findOrgUnit(location.parent_ou());
95                 if(location) type = findOrgType(location.ou_type());
96                 else type = null;
97         }
98
99         selector.appendChild(node);
100 }
101
102 function updateLoc(location, depth) {
103         if( location != null )
104                 newSearchLocation = location;
105
106         if( depth != null ) {
107                 if(depth != 0 ){
108                         G.ui.searchbar.lib_sel_link.onclick = _opacHandleLocationTagClick;
109                         if( location == globalOrgTree.id() ) {
110                                 hideMe( G.ui.searchbar.depth_sel_span );
111                                 unHideMe( G.ui.searchbar.lib_sel_span );
112                         } else {
113                                 buildLocationSelector(location);
114                                 hideMe( G.ui.searchbar.lib_sel_span );
115                                 unHideMe( G.ui.searchbar.depth_sel_span );
116                         }
117                 }
118
119                 setSelector(G.ui.searchbar.depth_selector, depth);
120                 newSearchDepth = depth;
121         }
122
123         if(!isFrontPage && (findCurrentPage() != MYOPAC) 
124                                 && (newSearchLocation != getLocation()) ) {
125                 searchBarSubmit();
126         }
127 }
128
129
130 function searchBarSubmit() {
131
132         var text = G.ui.searchbar.text.value;
133         if(!text || text == "") return;
134
135         var args = {};
136         args.page                               = MRESULT;
137         args[PARAM_OFFSET]      = 0;
138         args[PARAM_TERM]                = text;
139         args[PARAM_STYPE]               = _ts.options[_ts.selectedIndex].value;
140         args[PARAM_LOCATION] = newSearchLocation;
141         args[PARAM_DEPTH]               = (newSearchDepth != null) ? newSearchDepth : parseInt(_ds.options[_ds.selectedIndex].value);
142         args[PARAM_FORM]                = _fs.options[_fs.selectedIndex].value;
143
144         goTo(buildOPACLink(args));
145 }
146
147