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