671ed80ba8270ab23e98bd3ef4e6804d2615027a
[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
8 G.evt.common.init.push(searchBarInit);
9
10 /* if set by the org selector, this will be the location used the
11         next time the search is submitted */
12 var newSearchLocation; 
13 var newSearchDepth = null;
14
15 function searchBarInit() {
16
17         _ts = G.ui.searchbar.type_selector;
18         _ds = G.ui.searchbar.depth_selector;
19         _fs = G.ui.searchbar.form_selector;
20
21         G.ui.searchbar.text.focus();
22         G.ui.searchbar.text.onkeypress = 
23                 function(evt) {if(userPressedEnter(evt)) searchBarSubmit();};
24
25         G.ui.searchbar.submit.onclick = searchBarSubmit;
26
27         _ds.onchange = depthSelectorChanged;
28
29         if( getLocation() == globalOrgTree.id() ) {
30                 unHideMe( G.ui.searchbar.lib_sel_span );
31                 G.ui.searchbar.lib_sel_link.onclick = _opacHandleLocationTagClick;
32         } else {
33                 unHideMe( G.ui.searchbar.depth_sel_span );
34                 buildLocationSelector();
35         }
36
37         /* set up the selector objects, etc */
38         G.ui.searchbar.text.value = (getTerm() != null) ? getTerm() : "";
39         setSelector(_ts,        getStype());
40         setSelector(_ds,        getDepth());
41         setSelector(_fs,        getForm());
42
43 }
44
45 function _opacHandleLocationTagClick() {
46         orgTreeSelector.openTo(  
47                 (newSearchLocation != null) ? parseInt(newSearchLocation) : getLocation(), true );
48         swapCanvas(G.ui.common.org_container);
49 }
50
51 function depthSelectorChanged() {
52         var i = _ds.selectedIndex;
53         if( i == _ds.options.length - 1 ) {
54                 setSelector( _ds, getDepth() );
55                 _opacHandleLocationTagClick();
56
57         } else searchBarSubmit();
58
59 }
60
61 function buildLocationSelector() {
62
63         if( getLocation() == globalOrgTree.id() ) return;
64
65         var selector = G.ui.searchbar.depth_selector
66         var node = selector.removeChild(selector.getElementsByTagName("option")[0]);
67         
68         var location = findOrgUnit(getLocation());
69         var type = findOrgType(location.ou_type());
70
71         while( type && location ) {
72                 var n = node.cloneNode(true);   
73                 n.setAttribute("value", type.depth());
74                 removeChildren(n);
75                 n.appendChild(text(type.opac_label()));
76                 selector.appendChild(n);
77                 location = findOrgUnit(location.parent_ou());
78                 if(location) type = findOrgType(location.ou_type());
79                 else type = null;
80         }
81
82         selector.appendChild(node);
83 }
84
85 function updateLoc(location, depth) {
86         if( location != null )
87                 newSearchLocation = location;
88         if( depth != null ) {
89                 setSelector(G.ui.searchbar.depth_selector, depth);
90                 newSearchDepth = depth;
91         }
92         searchBarSubmit();
93 }
94
95
96 function searchBarSubmit() {
97
98         var text = G.ui.searchbar.text.value;
99         if(!text || text == "") return;
100
101         var args = {};
102         args.page                               = MRESULT;
103         args[PARAM_OFFSET]      = 0;
104         args[PARAM_TERM]                = text;
105         args[PARAM_STYPE]               = _ts.options[_ts.selectedIndex].value;
106         args[PARAM_LOCATION] = newSearchLocation;
107         args[PARAM_DEPTH]               = (newSearchDepth != null) ? newSearchDepth : parseInt(_ds.options[_ds.selectedIndex].value);
108         args[PARAM_FORM]                = _fs.options[_fs.selectedIndex].value;
109
110         goTo(buildOPACLink(args));
111 }
112
113