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