New combo3 class searchable from start of the code (used in gl accounts list).
[fa-stable.git] / js / inserts.js
1 /**********************************************************************
2     Copyright (C) FrontAccounting, LLC.
3         Released under the terms of the GNU General Public License, GPL, 
4         as published by the Free Software Foundation, either version 3 
5         of the License, or (at your option) any later version.
6     This program is distributed in the hope that it will be useful,
7     but WITHOUT ANY WARRANTY; without even the implied warranty of
8     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
9     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ***********************************************************************/
11 var _focus;
12 var _hotkeys = {
13         'alt': false,   // whether is the Alt key pressed
14         'list': false, // list of all elements with hotkey used recently
15         'focus': -1             // currently selected list element
16 };
17
18 function save_focus(e) {
19   _focus = e.name||e.id;
20   var h = document.getElementById('hints');
21   if (h) {
22         h.style.display = e.title && e.title.length ? 'inline' : 'none';
23         h.innerHTML = e.title ? e.title : '';
24   }
25 }
26
27 function _expand(tabobj) {
28
29   var ul = tabobj.parentNode.parentNode;
30   var alltabs=ul.getElementsByTagName("button");
31   var frm = tabobj.form;
32
33   if (ul.getAttribute("rel")){
34         for (var i=0; i<alltabs.length; i++){
35           alltabs[i].className = "ajaxbutton"  //deselect all tabs
36         }
37         tabobj.className = "current";
38         JsHttpRequest.request(tabobj)
39   }
40 }
41
42 //interface for selecting a tab (plus expand corresponding content)
43 function expandtab(tabcontentid, tabnumber) {
44   var tabs = document.getElementById(tabcontentid);
45  _expand(tabs.getElementsByTagName("input")[tabnumber]);
46 }
47
48 function _set_combo_input(e) {
49                 e.setAttribute('_last', e.value);
50                 e.onblur=function() { 
51                   var but_name = this.name.substring(0, this.name.length-4)+'button';
52                   var button = document.getElementsByName(but_name)[0];
53                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
54                   save_focus(select);
55 // submit request if there is submit_on_change option set and 
56 // search field has changed.
57                 
58                   if (button && (this.value != this.getAttribute('_last'))) {
59                         JsHttpRequest.request(button);
60                   } else if(this.className=='combo2') {
61                                 this.style.display = 'none';
62                                 select.style.display = 'inline';
63                                 setFocus(select);
64                   }
65                   return false;
66                 };
67                 e.onkeyup = function(ev) {
68                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
69                         if(select && select.selectedIndex>=0) {
70                           var len = select.length;
71                           var byid = this.className=='combo' || this.className=='combo3';
72                           var ac = this.value.toUpperCase();
73                           select.options[select.selectedIndex].selected = false;
74                           for (i = 0; i < len; i++) {
75                                 var txt = byid ? select.options[i].value : select.options[i].text;
76                                 if (this.className=='combo3') {
77                                   if(txt.toUpperCase().indexOf(ac) == 0) {
78                                         select.options[i].selected = true;
79                                         break;
80                                   }
81                                 } else {
82                                   if(txt.toUpperCase().indexOf(ac) >= 0) {
83                                         select.options[i].selected = true;
84                                         break;
85                                   }
86                                 }
87                           }
88                         }
89                 };
90         e.onkeydown = function(ev) { 
91                         ev = ev||window.event;
92                         key = ev.keyCode||ev.which;
93                         if(key == 13) {
94                           this.blur();
95                           return false;
96                         }
97                 }
98 }
99
100 function _update_box(s) {
101         var byid = s.className=='combo' || s.className=='combo3';
102         var rel = s.getAttribute('rel');
103         var box = document.getElementsByName(rel)[0];
104                 if(box && s.selectedIndex>=0) {
105                           var opt = s.options[s.selectedIndex];
106                                 if(box) {
107                                   var old = box.value;
108                                   box.value = byid ? opt.value : opt.text;
109                                   box.setAttribute('_last', box.value);
110                                   return old != box.value
111                                 }
112                 }
113 }
114
115 function _set_combo_select(e) {
116                 // When combo position is changed via js (eg from searchbox)
117                 // no onchange event is generated. To ensure proper change 
118                 // signaling we must track selectedIndex in onblur handler.
119                 e.setAttribute('_last', e.selectedIndex);
120                 e.onblur = function() {
121                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
122 //                      if(this.className=='combo')
123 //                          _update_box(this);
124                         if ((this.selectedIndex != this.getAttribute('_last'))
125                                 ||((this.className=='combo' || this.className=='combo3') && _update_box(this))
126                                 )
127                                         this.onchange();
128                 }
129                 e.onchange = function() {
130                         var s = this;
131                         this.setAttribute('_last', this.selectedIndex);
132                         if(s.className=='combo' || this.className=='combo3')
133                             _update_box(s);
134                         if(s.selectedIndex>=0) {
135                                  var sname = '_'+s.name+'_update';
136                                  var update = document.getElementsByName(sname)[0];
137                                  if(update) {
138                                             JsHttpRequest.request(update);
139                                 } 
140                         }
141                         return true;
142                 }
143                 e.onkeydown = function(event) {
144                     event = event||window.event;
145                     key = event.keyCode||event.which;
146                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
147                     if (box && key == 32 && this.className == 'combo2') {
148                             this.style.display = 'none';
149                             box.style.display = 'inline';
150                                 box.value='';
151                                 setFocus(box);
152                             return false;
153                          }
154                 }
155 }               
156
157 var _w;
158
159 function callEditor(key) {
160   var el = document.getElementsByName(editors[key][1])[0]; 
161   if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
162   _w = open(editors[key][0]+el.value+'&popup=1',
163           "edit","Scrollbars=0,resizable=0,width=800,height=600");
164   if (_w.opener == null)
165           _w.opener = self;
166   editors._call = key; // store call point for passBack 
167   _w.focus();
168 }
169
170 function passBack(value) {
171         var o = opener;
172         if(value != false) {
173                 var back = o.editors[o.editors._call]; // form input bindings
174                 var to = o.document.getElementsByName(back[1])[0];
175                 if (to) {
176                         if (to[0] != undefined) 
177                                 to[0].value = value; // ugly hack to set selector to any value
178                         to.value = value;
179                         // update page after item selection
180                         o.JsHttpRequest.request('_'+to.name+'_update', to.form);
181                         o.setFocus(to.name);
182                 }
183         }
184         close();
185 }
186
187 /*
188  Behaviour definitions
189 */
190 var inserts = {
191         'input': function(e) {
192                 if(e.onfocus==undefined) {
193                         e.onfocus = function() {
194                             save_focus(this);
195                                 if (this.className == 'combo' || this.className == 'combo3') 
196                                         this.select();
197                         };
198                 }
199                 if (e.className == 'combo' || e.className == 'combo2' || e.className == 'combo3') {
200                                 _set_combo_input(e);
201                 } 
202                 else
203                 if(e.type == 'text' ) {
204                                 e.onkeydown = function(ev) { 
205                                         ev = ev||window.event;
206                                         key = ev.keyCode||ev.which;
207                                         if(key == 13) {
208                                                 if(e.className == 'searchbox') e.onblur();
209                                                 return false;
210                                         } 
211                                         return true;
212                                 }
213                         }
214         },
215         'input.combo2,input[aspect="fallback"]': 
216         function(e) {
217             // this hides search button for js enabled browsers
218             e.style.display = 'none';
219         },
220         'div.js_only': 
221         function(e) {
222             // this shows divs for js enabled browsers only
223             e.style.display = 'block';
224         },
225 //      '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
226         'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton': 
227         function(e) {
228                     e.onclick = function() {
229                             save_focus(e);
230                             var asp = e.getAttribute('aspect')
231                                 if (asp && asp.indexOf('process') !== -1)
232                                         JsHttpRequest.request(this, null, 60000);
233                                 else
234                                         JsHttpRequest.request(this);
235                                 return false;
236                     }
237         },
238         'button': function(e) {
239                 if (e.name) {
240                         var func = _validate[e.name];
241                         var old = e.onclick;
242                         if(func) { 
243                                 if (typeof old != 'function' || old == func) { // prevent multiply binding on ajax update
244                                         e.onclick = func;
245                                 } else {
246                                         e.onclick = function() {
247                                                 if(func()) 
248                                                         { old(); return true;}
249                                                 else
250                                                         return false;
251                                         }
252                                 }
253                         }
254                 }
255         },
256     '.amount': function(e) {
257                 if(e.onblur==undefined) {
258                   e.onblur = function() {
259                         var dec = this.getAttribute("dec");
260                         price_format(this.name, get_amount(this.name), dec);
261                   };
262                 }
263         },
264         '.searchbox': // emulated onchange event handling for text inputs
265                 function(e) {
266                         e.setAttribute('_last_val', e.value);
267                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
268                         e.onblur = function() {
269                                 var val = this.getAttribute('_last_val');
270                                 if (val != this.value) {
271                                         this.setAttribute('_last_val', this.value);
272                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
273                                 }
274                         }
275         },
276         'button[aspect*selector], button[aspect*abort], input[aspect*selector]': function(e) {
277                 e.onclick = function() {
278                         passBack(this.getAttribute('rel'));
279                         return false;
280                 }
281         },
282         'button[aspect=popup]': function(e) {
283                                                 var old = e.onclick
284                 e.onclick = function() {
285 //                      this.form.target = '_blank';
286 //                              old();
287 //                      return true;
288                         if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
289                           _w = open(document.location+'popup=1',
290                                   "edit","Scrollbars=0,resizable=0,width=800,height=600, top=50,left=50");
291                           if (_w.opener == null)
292                                   _w.opener = self;
293                         //  editors._call = key; // store call point for passBack 
294 //                        _w.moveTo(50, 50);
295                           _w.focus();
296                         return false;
297                 }
298         },
299         'select': function(e) {
300                 if(e.onfocus==undefined) {
301                         e.onfocus = function() {
302                             save_focus(this);
303                         };
304                   var c = e.className;
305                   if (c == 'combo' || c == 'combo2' || c == 'combo3')
306                         _set_combo_select(e);
307                 }
308         },
309         'a.printlink':  function(l) {
310                 l.onclick = function() {
311                     save_focus(this);
312                         JsHttpRequest.request(this, null, 60000);
313                         return false;
314                 }
315         },
316         'a': function(e) { // traverse menu
317                 e.onkeydown = function(ev) { 
318                         ev = ev||window.event;
319                         key = ev.keyCode||ev.which;
320                         if(key==37 || key==38 || key==39 || key==40) {
321                                         move_focus(key, e, document.links);
322                                         ev.returnValue = false;
323                                         return false;
324                         }
325                 }
326         },
327         'ul.ajaxtabs':  function(ul) {
328             var ulist=ul.getElementsByTagName("li");
329             for (var x=0; x<ulist.length; x++){ //loop through each LI e
330                 var tab=ulist[x].getElementsByTagName("button")[0];
331                 if(tab.onclick==undefined) {
332 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
333                     var url = tab.form.action
334                     tab.onclick=function(){
335                         _expand(this);
336                         return false;
337                     }
338                 }
339             }
340         }
341 /*      'tr.editrow': function(e) {
342                         e.onkeydown = function(ev) { 
343                         ev = ev||window.event;
344                         key = ev.keyCode||ev.which;
345                         if(key == 13) {
346                           // Find & click additem/update button
347                           
348                         } else  if(key == 27) {
349                           return false;
350                         }
351                 }
352
353         },
354 *//*    '#msgbox': function(e) {
355         // this is to avoid changing div height after ajax update in IE7
356           e.style.display = e.innerHTML.length ? 'block' : 'none';
357         }
358 *//* TODO
359         'a.date_picker':  function(e) {
360             // this un-hides data picker for js enabled browsers
361             e.href = date_picker(this.getAttribute('rel'));
362             e.style.display = '';
363             e.tabindex = -1; // skip in tabbing order
364         }
365 */
366 };
367
368 function stopEv(ev) {
369                         if(ev.preventDefault) {
370                                 ev.preventDefault();
371                                 ev.stopPropagation();
372                         } else {
373                                 ev.returnValue = false;
374                                 ev.cancelBubble = true;
375                                 window.keycode = 0;
376                         }
377                         return false;
378 }
379 /*
380         Modified accesskey system. While Alt key is pressed letter keys moves 
381         focus to next marked link. Alt key release activates focused link.
382 */
383 function setHotKeys() {
384         document.onkeydown = function(ev) {
385                 ev = ev||window.event;
386                 key = ev.keyCode||ev.which;
387                 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
388                         _hotkeys.alt = true;
389                         _hotkeys.focus = -1;
390                         return stopEv(ev);
391                 }
392                 else if (ev.altKey && !ev.ctrlKey && ((key>47 && key<58) || (key>64 && key<91))) {
393                         key = String.fromCharCode(key);
394                         var n = _hotkeys.focus;
395                         var l = document.getElementsBySelector('[accesskey='+key+']');
396                         var cnt = l.length;
397                         _hotkeys.list = l;
398                         for (var i=0; i<cnt; i++) { 
399                                 n = (n+1)%cnt;
400                                 // check also if the link is visible
401                                 if (l[n].accessKey==key && l[n].scrollWidth) {
402                                         _hotkeys.focus = n;
403             // The timeout is needed to prevent unpredictable behaviour on IE.
404                                         var tmp = function() {l[_hotkeys.focus].focus();};
405                                         setTimeout(tmp, 0);
406                                         break;
407                                 }
408                         }
409                         return stopEv(ev);
410                 }
411                 if((ev.ctrlKey && key == 13) || key == 27) {
412                         _hotkeys.alt = false; // cancel link selection
413                         _hotkeys.focus = -1;
414                         ev.cancelBubble = true;
415                         if(ev.stopPropagation) ev.stopPropagation();
416                         // activate submit/escape form
417                         for(var j=0; j<this.forms.length; j++) {
418                                 var form = this.forms[j];
419                                 for (var i=0; i<form.elements.length; i++){
420                                         var el = form.elements[i];
421                                         var asp = el.getAttribute('aspect');
422                                         
423
424                                         if (el.className!='editbutton' && (asp && asp.indexOf('selector') !== -1) && (key==13 || key==27)) {
425                                                 passBack(key==13 ? el.getAttribute('rel') : false);
426                                                 ev.returnValue = false;
427                                                 return false;
428                                         }
429                                         if (((asp && asp.indexOf('default') !== -1) && key==13)||((asp && asp.indexOf('cancel') !== -1) && key==27)) {
430
431                                                 if (asp.indexOf('process') !== -1)
432                                                         JsHttpRequest.request(el, null, 60000);
433                                                 else
434                                                         JsHttpRequest.request(el);
435                                                 ev.returnValue = false;
436                                                 return false;
437                                         }
438                                 }
439                         }
440                         ev.returnValue = false;
441                         return false;
442                 }
443                 if (editors && editors[key]) {
444                         callEditor(key);
445                         return stopEv(ev); // prevent default binding
446                 } 
447                 return true;
448         };
449         document.onkeyup = function(ev) {
450                 ev = ev||window.event;
451                 key = ev.keyCode||ev.which;
452
453                 if (_hotkeys.alt==true) {
454                         if (key == 18) {
455                                 _hotkeys.alt = false;
456                                 if (_hotkeys.focus>=0) {
457                                         var link = _hotkeys.list[_hotkeys.focus];
458                                         if(link.onclick) 
459                                                 link.onclick();
460                                         else
461                                                 if (link.target=='_blank') {
462                                                         window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
463                                                         openWindow(link.href,'_blank');
464                                                 } else
465                                                         window.location = link.href;
466                                 }
467                         return stopEv(ev);
468                         } 
469                 }
470                 return true;
471         }
472 }
473
474 Behaviour.register(inserts);
475
476 Behaviour.addLoadEvent(setFocus);
477 Behaviour.addLoadEvent(setHotKeys);