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