/[webpac2]/Webpacus/root/js/search.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /Webpacus/root/js/search.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Mon Dec 26 22:02:55 2005 UTC (18 years, 4 months ago) by dpavlin
File MIME type: text/cpp
File size: 7022 byte(s)
 r12494@llin:  dpavlin | 2005-12-26 23:05:13 +0100
 try to re-use existing form fields

1 // WebPAC API
2 //
3 // Documented in this code only, I'm afraid. Beware of dragons!
4 // Dobrica Pavlinusic dpavlin(at)rot13(dot)org 2005-11-22
5
6 /*
7 load another template
8 */
9 function load_template(template_name) {
10
11 var old_template = $('template_name').value;
12
13 Logger.info('load_template: '+template_name+' (old: '+old_template+')');
14
15 if (template_name == old_template) {
16 Logger.error('why reload same template?');
17 return false;
18 }
19
20 $('template_name').value = template_name;
21 submit_results_form();
22 return false;
23 }
24
25 /*
26 pager
27 */
28 function load_results_page(page_nr) {
29
30 if (parseInt(page_nr) != page_nr) {
31 Logger.error("load_results_page can't parseInt "+page_nr);
32 return false;
33 }
34
35 var old_page = get_page_nr();
36
37 if (page_nr < 1) {
38 Logger.error("load_results_page called with '"+page_nr+"' < 1");
39 return false;
40 }
41
42 Logger.info('load_results_page: '+page_nr+' (from page: '+old_page+')');
43
44 if (page_nr == old_page) {
45 Logger.error('why reload same page?');
46 return false;
47 }
48
49 $('page_nr').value = page_nr;
50 submit_results_form();
51
52 return false;
53 }
54
55 function get_page_nr() {
56 var page_nr = parseInt( $('page_nr').value ) || 1;
57 return page_nr;
58 }
59
60 function reset_page_nr( nr ) {
61 if (! nr) nr = 1;
62 Logger.info('reset_page_nr to '+nr);
63 $('page_nr').value = nr;
64 }
65
66 /* syntaxtic sugar */
67 function set_page_nr(nr) {
68 return load_results_page(nr);
69 }
70
71 function next_page() {
72 load_results_page( get_page_nr() + 1 );
73 return false;
74 }
75
76 function prev_page() {
77 load_results_page( get_page_nr() - 1 );
78 return false;
79 }
80
81 /* toggle visibility and replace text with something else */
82 function toggleLinkAndElement ( link, elementID, text ) {
83
84 if ( link.innerHTML != text ) {
85 // change link text
86 link.oldInnerHTML = link.innerHTML;
87 link.innerHTML = text;
88 } else {
89 // change it back
90 link.innerHTML = link.oldInnerHTML;
91 }
92
93 Logger.debug('toggleLinkAndElement '+elementID+': '+Element.visible( elementID )+' text: '+text);
94
95 Element.toggle( elementID );
96
97 // why is this needed, Firefox 1.5?
98 if (Element.visible( elementID )) $( elementID ).style.display = 'block';
99
100 return false;
101 }
102
103
104 /*
105 submit results form using AJAX
106 */
107 function submit_results_form() {
108 var results_form = $('results_form');
109
110 if (results_form) {
111 var form_params = Form.serialize( results_form );
112
113 Logger.debug('Ajax.Updater(results,/results/ajax) '+form_params);
114
115 new Ajax.Updater( 'results', '/search/results/ajax', {
116 parameters: form_params,
117 asynchronous: 1,
118 onLoading: function(request) {
119 show_searching();
120 },
121 onLoaded: function(request) {
122 hide_searching();
123 }
124 } ) ;
125 return false;
126 } else {
127 Logger.debug('no results_form element');
128 return undef;
129 }
130 return false;
131 }
132
133 /*
134 helper functions for submit_results_form()
135 */
136
137 var search_active = 0;
138
139 function show_searching() {
140 Logger.debug('show_searching');
141 Element.show('searching');
142 // new Effect.Fade('results', { duration: 0.3 });
143 search_active = 1;
144 return false;
145 }
146
147 function hide_searching() {
148 Logger.debug('hide_searching');
149 Element.hide('searching');
150 // new Effect.Highlight('results');
151 // new Effect.Appear('results', { duration: 0.3 });
152 search_active = 0;
153 return false;
154 }
155
156 /*
157 load record nr in some template
158 */
159
160 function load_rec(record_uri, tmp_template_filename) {
161 if (! tmp_template_filename) tmp_template_filename = template_filename;
162 Logger.info('load_rec '+record_uri+' in '+tmp_template_filename);
163
164 var results_form = $('results_form');
165
166 if (results_form) {
167 var args = '?record_uri='+record_uri+'&template_filename='+tmp_template_filename;
168 Logger.debug('Ajax.Updater(results,/search/record) '+args);
169
170 new Ajax.Updater( 'results', '/search/record'+args, {
171 asynchronous: 1,
172 onLoading: function(request) {
173 show_searching();
174 },
175 onLoaded: function(request) {
176 hide_searching();
177 }
178 } ) ;
179 return false;
180 } else {
181 Logger.debug('no results_form element');
182 return undef;
183 }
184 return false;
185 }
186
187 /* click on checkbox */
188
189 function checkbox_click() {
190 if (search_active) return false;
191 submit_results_form();
192 Logger.debug('checkbox_click');
193 return true;
194 }
195
196 /* reset form fields in _reset hidden field to nothing */
197
198 function reset_form() {
199 Logger.info('reset '+$F('_reset'));
200
201 var names = new Array();
202 names = $F('_reset').split(',');
203 for(var i = 0; i < names.length; i++) {
204 Logger.debug('reset '+i+': '+names[i]);
205 $(names[i]).value='';
206 }
207
208 var t=$('search_table');
209 var max_p = t.rows.length;
210 for(var i = 0; i < attr_rows.length; i++) {
211 var p = attr_rows[i];
212 if (p != -1) {
213 if (p < max_p) {
214 Logger.debug('delete row '+p);
215 t.deleteRow( p );
216 } else {
217 Logger.error('skipping invalid delete row '+p);
218 }
219 }
220 }
221
222 attr_rows = new Array();
223
224 return false;
225 }
226
227 /* helper function for [% search($display, $search, $delimiter) %] */
228
229 function search_via_link(attr, val, template) {
230
231 if (! template) template = $('template_name').value;
232 Logger.info('search called from link for '+attr+'='+val+' in '+template);
233 save_search(attr, val);
234
235 $('template_name').value = template;
236 submit_results_form();
237
238 return false;
239 }
240
241 /* save search and add element for it in search form */
242
243 var attr_rows = new Array();
244
245 function save_search( attr, val ) {
246 Logger.info('save_search('+attr+','+val+')');
247
248 var t=$('search_table');
249 if (! t) {
250 Logger.error("can't find search_table");
251 return undef;
252 }
253 reset_form();
254
255 var form = $('results_form');
256 var inputs = form.getElementsByTagName('input');
257
258 for (var i = 0; i < inputs.length; i++) {
259 var input = inputs[i];
260 if (input.name != attr) continue;
261 Logger.debug('found '+name+'='+input.value);
262 input.value = unescape( val );
263 return false;
264 }
265
266 var t_row_nr = t.rows.length;
267 var tr = t.insertRow( t_row_nr );
268
269 if (! tr) {
270 Logger.error("can't create row "+t_row_nr);
271 return undef;
272 }
273
274 var row_nr = attr_rows.length;
275 attr_rows[ row_nr ] = t_row_nr;
276
277 Logger.debug('added row '+t_row_nr+' to table [offset: '+row_nr+']');
278
279 var td = tr.insertCell(0);
280 td.setAttribute('align', 'right');
281 td.appendChild(
282 document.createTextNode(attr)
283 );
284
285 td = tr.insertCell(1);
286 td.setAttribute('align', 'left');
287
288 var el = document.createElement('input');
289 el.setAttribute('type', 'text');
290 el.setAttribute('disabled', 'true');
291 el.setAttribute('name', attr);
292 el.setAttribute('id', '_attr_'+attr+'_id');
293 el.setAttribute('size', val.length < 60 ? 60: val.length);
294 el.setAttribute('value', unescape( val ) );
295 td.appendChild(el);
296
297 var el = document.createElement('input');
298 el.setAttribute('type', 'button');
299 el.setAttribute('value', '-');
300 el.onclick = function () {
301 if (attr_rows[row_nr] == -1) {
302 Logger.error('row '+row_nr+' allready deleted');
303 return false;
304 }
305 Logger.info('remove:' + attr + ' row:'+row_nr+' ['+attr_rows[row_nr]+']');
306 t.deleteRow( attr_rows[row_nr] );
307
308 attr_rows[row_nr] = -1;
309 for (var i=(row_nr + 1); i < attr_rows.length; i++) {
310 attr_rows[i]--;
311 Logger.debug('moved '+i+' to '+attr_rows[i]);
312 }
313 submit_results_form();
314 return false;
315 }
316 td.appendChild(el);
317
318 return true;
319 };

Properties

Name Value
svn:mime-type text/cpp

  ViewVC Help
Powered by ViewVC 1.1.26