new front page
[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                 function(evt) {if(userPressedEnter(evt)) searchBarSubmit();};
26
27         G.ui.searchbar.submit.onclick = searchBarSubmit;
28
29         _ds.onchange = depthSelectorChanged;
30
31         if( getLocation() == globalOrgTree.id() ) {
32                 unHideMe( G.ui.searchbar.lib_sel_span );
33                 G.ui.searchbar.lib_sel_link.onclick = _opacHandleLocationTagClick;
34         } else {
35                 unHideMe( G.ui.searchbar.depth_sel_span );
36                 buildLocationSelector();
37         }
38
39         /* set up the selector objects, etc */
40         G.ui.searchbar.text.value = (getTerm() != null) ? getTerm() : "";
41         setSelector(_ts,        getStype());
42         setSelector(_ds,        getDepth());
43         setSelector(_fs,        getForm());
44
45 }
46
47 function _opacHandleLocationTagClick() {
48         orgTreeSelector.openTo(  
49                 (newSearchLocation != null) ? parseInt(newSearchLocation) : getLocation(), true );
50         swapCanvas(G.ui.common.org_container);
51 }
52
53 function depthSelectorChanged() {
54         var i = _ds.selectedIndex;
55         if( i == _ds.options.length - 1 ) {
56                 setSelector( _ds, getDepth() );
57                 _opacHandleLocationTagClick();
58
59         } else {
60                 if(!isFrontPage)
61                         searchBarSubmit();
62         }
63
64 }
65
66 function buildLocationSelector(newLoc) {
67
68         var loc;
69         if(newLoc != null) loc = newLoc;
70         else loc = getLocation();
71
72         if( loc == globalOrgTree.id() ) return;
73
74         var selector = G.ui.searchbar.depth_selector
75         var node = selector.removeChild(selector.getElementsByTagName("option")[0]);
76         removeChildren(selector);
77         
78         var location = findOrgUnit(loc);
79         var type = findOrgType(location.ou_type());
80
81         while( type && location ) {
82                 var n = node.cloneNode(true);   
83                 n.setAttribute("value", type.depth());
84                 removeChildren(n);
85                 n.appendChild(text(type.opac_label()));
86                 selector.appendChild(n);
87                 location = findOrgUnit(location.parent_ou());
88                 if(location) type = findOrgType(location.ou_type());
89                 else type = null;
90         }
91
92         selector.appendChild(node);
93 }
94
95 function updateLoc(location, depth) {
96         if( location != null )
97                 newSearchLocation = location;
98
99         if( depth != null ) {
100                 if(depth != 0 ){
101                         G.ui.searchbar.lib_sel_link.onclick = _opacHandleLocationTagClick;
102                         if( location == globalOrgTree.id() ) {
103                                 hideMe( G.ui.searchbar.depth_sel_span );
104                                 unHideMe( G.ui.searchbar.lib_sel_span );
105                         } else {
106                                 buildLocationSelector(location);
107                                 hideMe( G.ui.searchbar.lib_sel_span );
108                                 unHideMe( G.ui.searchbar.depth_sel_span );
109                         }
110                 }
111
112                 setSelector(G.ui.searchbar.depth_selector, depth);
113                 newSearchDepth = depth;
114         }
115         if(!isFrontPage)
116                 searchBarSubmit();
117 }
118
119
120 function searchBarSubmit() {
121
122         var text = G.ui.searchbar.text.value;
123         if(!text || text == "") return;
124
125         var args = {};
126         args.page                               = MRESULT;
127         args[PARAM_OFFSET]      = 0;
128         args[PARAM_TERM]                = text;
129         args[PARAM_STYPE]               = _ts.options[_ts.selectedIndex].value;
130         args[PARAM_LOCATION] = newSearchLocation;
131         args[PARAM_DEPTH]               = (newSearchDepth != null) ? newSearchDepth : parseInt(_ds.options[_ds.selectedIndex].value);
132         args[PARAM_FORM]                = _fs.options[_fs.selectedIndex].value;
133
134         goTo(buildOPACLink(args));
135 }
136
137