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 ***********************************************************************/
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
18 function validate(e) {
19 if (e.name && (typeof _validate[e.name] == 'function'))
20 return _validate[e.name](e);
22 var n = e.name.indexOf('[');
24 var key = e.name.substring(n+1, e.name.length-1);
25 if (key.length>1 && _validate[e.name.substring(0,n)])
26 return _validate[e.name.substring(0,n)][key](e);
32 function save_focus(e) {
33 _focus = e.name||e.id;
34 var h = document.getElementById('hints');
36 h.style.display = e.title && e.title.length ? 'inline' : 'none';
37 h.innerHTML = e.title ? e.title : '';
41 function _expand(tabobj) {
43 var ul = tabobj.parentNode.parentNode;
44 var alltabs=ul.getElementsByTagName("button");
45 var frm = tabobj.form;
47 if (ul.getAttribute("rel")){
48 for (var i=0; i<alltabs.length; i++){
49 alltabs[i].className = "ajaxbutton" //deselect all tabs
51 tabobj.className = "current";
52 JsHttpRequest.request(tabobj)
56 //interface for selecting a tab (plus expand corresponding content)
57 function expandtab(tabcontentid, tabnumber) {
58 var tabs = document.getElementById(tabcontentid);
59 _expand(tabs.getElementsByTagName("input")[tabnumber]);
62 function _set_combo_input(e) {
63 e.setAttribute('_last', e.value);
65 var but_name = this.name.substring(0, this.name.length-4)+'button';
66 var button = document.getElementsByName(but_name)[0];
67 var select = document.getElementsByName(this.getAttribute('rel'))[0];
69 // submit request if there is submit_on_change option set and
70 // search field has changed.
72 if (button && (this.value != this.getAttribute('_last'))) {
73 JsHttpRequest.request(button);
74 } else if(this.className=='combo2') {
75 this.style.display = 'none';
76 select.style.display = 'inline';
81 e.onkeyup = function(ev) {
82 var select = document.getElementsByName(this.getAttribute('rel'))[0];
83 if(select && select.selectedIndex>=0) {
84 var len = select.length;
85 var byid = this.className=='combo' || this.className=='combo3';
86 var ac = this.value.toUpperCase();
87 select.options[select.selectedIndex].selected = false;
88 for (i = 0; i < len; i++) {
89 var txt = byid ? select.options[i].value : select.options[i].text;
90 if (this.className=='combo3') {
91 if(txt.toUpperCase().indexOf(ac) == 0) {
92 select.options[i].selected = true;
96 if(txt.toUpperCase().indexOf(ac) >= 0) {
97 select.options[i].selected = true;
104 e.onkeydown = function(ev) {
105 ev = ev||window.event;
106 key = ev.keyCode||ev.which;
114 function _update_box(s) {
115 var byid = s.className=='combo' || s.className=='combo3';
116 var rel = s.getAttribute('rel');
117 var box = document.getElementsByName(rel)[0];
118 if(box && s.selectedIndex>=0) {
119 var opt = s.options[s.selectedIndex];
122 box.value = byid ? opt.value : opt.text;
123 box.setAttribute('_last', box.value);
124 return old != box.value
129 function _set_combo_select(e) {
130 // When combo position is changed via js (eg from searchbox)
131 // no onchange event is generated. To ensure proper change
132 // signaling we must track selectedIndex in onblur handler.
133 e.setAttribute('_last', e.selectedIndex);
134 e.onblur = function() {
135 var box = document.getElementsByName(this.getAttribute('rel'))[0];
136 // if(this.className=='combo')
137 // _update_box(this);
138 if ((this.selectedIndex != this.getAttribute('_last'))
139 ||((this.className=='combo' || this.className=='combo3') && _update_box(this))
143 e.onchange = function() {
145 this.setAttribute('_last', this.selectedIndex);
146 if(s.className=='combo' || this.className=='combo3')
148 if(s.selectedIndex>=0) {
149 var sname = '_'+s.name+'_update';
150 var update = document.getElementsByName(sname)[0];
152 JsHttpRequest.request(update);
157 e.onkeydown = function(event) {
158 event = event||window.event;
159 key = event.keyCode||event.which;
160 var box = document.getElementsByName(this.getAttribute('rel'))[0];
161 if(key == 8 || (key==37 && event.altKey)) {
162 event.returnValue = false;
165 if (box && (key == 32) && (this.className == 'combo2')) {
166 this.style.display = 'none';
167 box.style.display = 'inline';
172 if (key == 13 && !e.length) // prevent chrome issue (blocked cursor after CR on empty selector)
180 function callEditor(key) {
181 var el = document.getElementsByName(editors[key][1])[0];
182 if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
183 var left = (screen.width - editors[key][2]) / 2;
184 var top = (screen.height - editors[key][3]) / 2;
185 _w = open(editors[key][0]+el.value+'&popup=1',
186 "edit","scrollbars=yes,resizable=0,width="+editors[key][2]+",height="+editors[key][3]+",left="+left+",top="+top+",screenX="+left+",screenY="+top);
187 if (_w.opener == null)
189 editors._call = key; // store call point for passBack
193 function passBack(value) {
196 var back = o.editors[o.editors._call]; // form input bindings
197 var to = o.document.getElementsByName(back[1])[0];
199 if (to[0] != undefined)
200 to[0].value = value; // ugly hack to set selector to any value
202 // update page after item selection
203 o.JsHttpRequest.request('_'+to.name+'_update', to.form);
211 Normalize date format using previous input value to guess missing date elements.
212 Helps fast date input field change with only single or double numbers (for day or day-and-month fragments)
214 function fix_date(date, last)
216 var dat = last.split(user.datesep);
217 var cur = date.split(user.datesep);
218 var day, month, year;
220 // TODO: user.date as default?
221 // TODO: user.datesys
222 if (date == "" || date == last) // should we return an empty date or should we return last?
224 if (user.datefmt == 0 || user.datefmt == 3) // set defaults
226 day = dat[1]; month = dat[0]; year = dat[2];
227 } else if (user.datefmt == 1 || user.datefmt == 4){
228 day = dat[0]; month = dat[1]; year = dat[2];
230 day = dat[2]; month = dat[1]; year = dat[0];
232 if (cur[2] == undefined || cur[2] == "") // day or day-month
234 if (cur[0] != undefined && cur[0] != "" && cur[0].length < 3) // day entered
236 if (cur[1] != undefined && cur[1] != "" && cur[1].length < 3) // month entered
239 if (user.datefmt<3) {
240 if (day<10) day = '0'+parseInt(day, 10);
241 if (month<10) month = '0'+parseInt(month, 10);
243 if (year<100) year = year<60 ? (2000+parseInt(year,10)) : (1900+parseInt(year,10));
245 // console.info(day,month,year)
246 if (user.datefmt == 0 || user.datefmt==3)
247 return month+user.datesep+day+user.datesep+year;
248 if (user.datefmt == 1 || user.datefmt==4)
249 return day+user.datesep+month+user.datesep+year;
250 return year+user.datesep+month+user.datesep+day;
254 Behaviour definitions
257 'input': function(e) {
258 if(e.onfocus==undefined) {
259 e.onfocus = function() {
261 if (this.className == 'combo' || this.className == 'combo3')
265 if (e.className == 'combo' || e.className == 'combo2' || e.className == 'combo3') {
269 if(e.type == 'text' ) {
270 e.onkeydown = function(ev) {
271 ev = ev||window.event;
272 key = ev.keyCode||ev.which;
274 if(e.className == 'searchbox') e.onblur();
281 'input.combo2,input[aspect="fallback"]':
283 // this hides search button for js enabled browsers
284 e.style.display = 'none';
288 // this shows divs for js enabled browsers only
289 e.style.display = 'block';
291 'button': function(e) {
292 e.onclick = function(){
294 setTimeout(function() { var asp = e.getAttribute('aspect');
295 set_mark((asp && (asp.indexOf('process') !== -1)) ? 'progressbar.gif' : 'ajax-loader.gif');
300 e.onkeydown = function(ev) { // block unintentional page escape with 'history back' key pressed on buttons
301 ev = ev||window.event;
302 key = ev.keyCode||ev.which;
303 if(key == 8 || (key==37 && ev.altKey)) {
304 ev.returnValue = false;
310 // '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
311 'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton':
313 e.onclick = function() {
316 var asp = e.getAttribute('aspect')
317 if (asp && (asp.indexOf('process') !== -1))
318 JsHttpRequest.request(this, null, 600000); // ten minutes for backup
320 JsHttpRequest.request(this);
325 '.amount': function(e) {
326 if(e.onblur==undefined) {
327 e.onblur = function() {
328 var dec = this.getAttribute("dec");
329 price_format(this.name, get_amount(this.name), dec);
333 '.searchbox': // emulated onchange event handling for text inputs
335 e.setAttribute('_last_val', e.value);
336 e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
337 e.onblur = function() {
338 var val = this.getAttribute('_last_val');
339 if (val != this.value) {
340 this.setAttribute('_last_val', this.value);
341 JsHttpRequest.request('_'+this.name+'_changed', this.form);
347 e.setAttribute('_last_val', e.value);
348 e.setAttribute('autocomplete', 'off');
349 e.onblur = function() {
350 var val = this.getAttribute('_last_val');
351 if (val != this.value) {
352 this.value = fix_date(this.value, val);
353 this.setAttribute('_last_val', this.value);
354 if (e.className.match(/\bactive\b/))
355 JsHttpRequest.request('_'+this.name+'_changed', this.form);
359 'button[aspect*selector], button[aspect*abort], input[aspect*selector]': function(e) {
360 e.onclick = function() {
361 passBack(this.getAttribute('rel'));
365 'button[aspect=popup]': function(e) {
366 e.onclick = function() {
367 if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
368 var left = (screen.width - 800)/2;
369 var top = (screen.height - 600)/2;
370 _w = open(document.location+'popup=1',
371 "edit","Scrollbars=0,resizable=0,width=800,height=600, top="+top+",left="+left+",screenX="+left+",screenY="+top);
372 if (_w.opener == null)
374 // editors._call = key; // store call point for passBack
375 // _w.moveTo(50, 50);
380 'select': function(e) {
381 if(e.onfocus==undefined) {
382 e.onfocus = function() {
387 if (c == 'combo' || c == 'combo2' || c == 'combo3')
388 _set_combo_select(e);
390 e.onkeydown = function(ev) { // block unintentional page escape with 'history back' key pressed on buttons
391 ev = ev||window.event;
392 key = ev.keyCode||ev.which;
393 if(key == 8 || (key=37 && ev.altKey)) {
394 ev.returnValue = false;
400 'a.printlink': function(l) {
401 l.onclick = function() {
403 JsHttpRequest.request(this, null, 60000);
407 'a.repopts_link': function(l) {
408 l.onclick = function() {
410 var replinks = document.getElementsBySelector('a.repopts_link');
411 for(var i in replinks)
412 replinks[i].style.fontWeight = replinks[i]==this ? 'bold' : 'normal';
413 JsHttpRequest.request(this, null);
417 'a': function(e) { // traverse menu
418 e.onkeydown = function(ev) {
419 ev = ev||window.event;
420 key = ev.keyCode||ev.which;
421 if(key==37 || key==38 || key==39 || key==40) {
422 move_focus(key, e, document.links);
423 ev.returnValue = false;
427 // prevent unneeded transaction entry abortion
428 if (e.className == 'shortcut'
429 || e.className == 'menu_option'
430 || e.className == 'menu_tab'
431 || e.className == 'selected')
432 e.onclick = function(ev) {
433 if (_validate._processing
434 && _validate._modified
435 && !confirm(_validate._processing)) {
436 ev.returnValue = false;
439 window.location = e.href;
442 'ul.ajaxtabs': function(ul) {
443 var ulist=ul.getElementsByTagName("li");
444 for (var x=0; x<ulist.length; x++){ //loop through each LI e
445 var tab=ulist[x].getElementsByTagName("button")[0];
446 // if(tab.onclick==undefined) {
447 // ? var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
448 var url = tab.form.action
449 tab.onclick=function(){
456 'textarea': function(e) {
457 if(e.onfocus==undefined) {
458 e.onfocus = function() {
463 /* 'tr.editrow': function(e) {
464 e.onkeydown = function(ev) {
465 ev = ev||window.event;
466 key = ev.keyCode||ev.which;
468 // Find & click additem/update button
470 } else if(key == 27) {
476 *//* '#msgbox': function(e) {
477 // this is to avoid changing div height after ajax update in IE7
478 e.style.display = e.innerHTML.length ? 'block' : 'none';
481 'a.date_picker': function(e) {
482 // this un-hides data picker for js enabled browsers
483 e.href = date_picker(this.getAttribute('rel'));
484 e.style.display = '';
485 e.tabindex = -1; // skip in tabbing order
490 function stopEv(ev) {
491 if(ev.preventDefault) {
493 ev.stopPropagation();
495 ev.returnValue = false;
496 ev.cancelBubble = true;
502 Modified accesskey system. While Alt key is pressed letter keys moves
503 focus to next marked link. Alt key release activates focused link.
505 function setHotKeys() {
506 document.onkeydown = function(ev) {
507 ev = ev||window.event;
508 key = ev.keyCode||ev.which;
509 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
514 else if (ev.altKey && !ev.ctrlKey && ((key>47 && key<58) || (key>64 && key<91))) {
515 key = String.fromCharCode(key);
516 var n = _hotkeys.focus;
517 var l = document.getElementsBySelector('[accesskey='+key+']');
520 for (var i=0; i<cnt; i++) {
522 // check also if the link is visible
523 if (l[n].accessKey==key && l[n].scrollWidth) {
525 // The timeout is needed to prevent unpredictable behaviour on IE.
526 var tmp = function() {l[_hotkeys.focus].focus();};
533 if((ev.ctrlKey && key == 13) || key == 27) {
534 _hotkeys.alt = false; // cancel link selection
536 ev.cancelBubble = true;
537 if(ev.stopPropagation) ev.stopPropagation();
538 // activate submit/escape form
539 for(var j=0; j<this.forms.length; j++) {
540 var form = this.forms[j];
541 for (var i=0; i<form.elements.length; i++){
542 var el = form.elements[i];
543 var asp = el.getAttribute('aspect');
545 if (el.className!='editbutton' && (asp && asp.indexOf('selector') !== -1) && (key==13 || key==27)) {
546 passBack(key==13 ? el.getAttribute('rel') : false);
547 ev.returnValue = false;
550 if (((asp && asp.indexOf('default') !== -1) && key==13)||((asp && asp.indexOf('cancel') !== -1) && key==27)) {
552 if (asp.indexOf('nonajax') !== -1)
555 if (asp.indexOf('process') !== -1)
556 JsHttpRequest.request(el, null, 600000);
558 JsHttpRequest.request(el);
560 ev.returnValue = false;
565 ev.returnValue = false;
568 if (editors!=='undefined' && editors[key]) {
570 return stopEv(ev); // prevent default binding
574 document.onkeyup = function(ev) {
575 ev = ev||window.event;
576 key = ev.keyCode||ev.which;
578 if (_hotkeys.alt==true) {
580 _hotkeys.alt = false;
581 if (_hotkeys.focus >= 0) {
582 var link = _hotkeys.list[_hotkeys.focus];
586 if (link.target=='_blank') {
587 window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
588 openWindow(link.href,'_blank');
590 window.location = link.href;
599 Behaviour.register(inserts);
601 Behaviour.addLoadEvent(setFocus);
602 Behaviour.addLoadEvent(setHotKeys);