139573da5cc2e98bc2f7343b40cdae9e1c504911
[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         orgTreeSelector.openTo(  
50                 (newSearchLocation != null) ? parseInt(newSearchLocation) : getLocation(), true );
51         swapCanvas(G.ui.common.org_container);
52 }
53
54 function depthSelectorChanged() {
55         var i = _ds.selectedIndex;
56         if( i == _ds.options.length - 1 ) {
57                 setSelector( _ds, getDepth() );
58                 _opacHandleLocationTagClick();
59
60         } else {
61                 if(!isFrontPage && (findCurrentPage() != MYOPAC)) {
62                         searchBarSubmit();
63                 }
64         }
65
66 }
67
68 var chooseAnotherNode;
69 function buildLocationSelector(newLoc) {
70
71         var loc;
72         if(newLoc != null) loc = newLoc;
73         else loc = getLocation();
74
75         if( loc == globalOrgTree.id() ) return;
76
77         var selector = G.ui.searchbar.depth_selector
78         if(!chooseAnotherNode) 
79                 chooseAnotherNode = selector.removeChild(selector.getElementsByTagName("option")[0]);
80         var node = chooseAnotherNode;
81         removeChildren(selector);
82         
83         var location = findOrgUnit(loc);
84         var type = findOrgType(location.ou_type());
85
86         while( type && location ) {
87                 var n = node.cloneNode(true);   
88                 n.setAttribute("value", type.depth());
89                 removeChildren(n);
90                 n.appendChild(text(type.opac_label()));
91                 selector.appendChild(n);
92                 location = findOrgUnit(location.parent_ou());
93                 if(location) type = findOrgType(location.ou_type());
94                 else type = null;
95         }
96
97         selector.appendChild(node);
98 }
99
100 function updateLoc(location, depth) {
101         if( location != null )
102                 newSearchLocation = location;
103
104         if( depth != null ) {
105                 if(depth != 0 ){
106                         G.ui.searchbar.lib_sel_link.onclick = _opacHandleLocationTagClick;
107                         if( location == globalOrgTree.id() ) {
108                                 hideMe( G.ui.searchbar.depth_sel_span );
109                                 unHideMe( G.ui.searchbar.lib_sel_span );
110                         } else {
111                                 buildLocationSelector(location);
112                                 hideMe( G.ui.searchbar.lib_sel_span );
113                                 unHideMe( G.ui.searchbar.depth_sel_span );
114                         }
115                 }
116
117                 setSelector(G.ui.searchbar.depth_selector, depth);
118                 newSearchDepth = depth;
119         }
120
121         if(!isFrontPage && (findCurrentPage() != MYOPAC) 
122                                 && (newSearchLocation != getLocation()) ) {
123                 searchBarSubmit();
124         }
125 }
126
127
128 function searchBarSubmit() {
129
130         var text = G.ui.searchbar.text.value;
131         if(!text || text == "") return;
132
133         var args = {};
134         args.page                               = MRESULT;
135         args[PARAM_OFFSET]      = 0;
136         args[PARAM_TERM]                = text;
137         args[PARAM_STYPE]               = _ts.options[_ts.selectedIndex].value;
138         args[PARAM_LOCATION] = newSearchLocation;
139         args[PARAM_DEPTH]               = (newSearchDepth != null) ? newSearchDepth : parseInt(_ds.options[_ds.selectedIndex].value);
140         args[PARAM_FORM]                = _fs.options[_fs.selectedIndex].value;
141
142         goTo(buildOPACLink(args));
143 }
144
145