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 'focus': -1 // currently selected indeks of document.links
16 var _validate = {}; // validation functions
19 box = document.getElementById('msgbox')
20 box.innerHTML= box.innerHTML+'<br>'+msg
23 function save_focus(e) {
24 _focus = e.name||e.id;
25 var h = document.getElementById('hints');
27 h.style.display = e.title && e.title.length ? 'inline' : 'none';
28 h.innerHTML = e.title ? e.title : '';
32 function _expand(tabobj) {
34 var ul = tabobj.parentNode.parentNode;
35 var alltabs=ul.getElementsByTagName("input");
36 var frm = tabobj.form;
38 if (ul.getAttribute("rel")){
39 for (var i=0; i<alltabs.length; i++){
40 alltabs[i].className = "ajaxbutton" //deselect all tabs
42 tabobj.className = "current";
43 JsHttpRequest.request(tabobj)
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]);
53 function _set_combo_input(e) {
54 e.setAttribute('_last', e.value);
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];
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';
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;
87 e.onkeydown = function(ev) {
88 ev = ev||window.event;
89 key = ev.keyCode||ev.which;
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];
104 box.value = byid ? opt.value : opt.text;
105 box.setAttribute('_last', box.value);
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')
118 if (this.selectedIndex != this.getAttribute('_last'))
121 e.onchange = function() {
123 this.setAttribute('_last', this.selectedIndex);
124 if(s.className=='combo')
126 if(s.selectedIndex>=0) {
127 var sname = '_'+s.name+'_update';
128 var update = document.getElementsByName(sname)[0];
130 JsHttpRequest.request(update);
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';
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
156 Behaviour definitions
159 'input': function(e) {
160 if(e.onfocus==undefined) {
161 e.onfocus = function() {
163 if (this.className == 'combo')
167 if (e.className == 'combo' || e.className == 'combo2') {
171 if(e.type == 'text' ) {
172 e.onkeydown = function(ev) {
173 ev = ev||window.event;
174 key = ev.keyCode||ev.which;
176 if(e.className == 'searchbox') e.onblur();
183 'input.combo2,input[aspect="fallback"]':
185 // this hides search button for js enabled browsers
186 e.style.display = 'none';
190 // this shows divs for js enabled browsers only
191 e.style.display = 'block';
193 // '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
194 'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton':
196 e.onclick = function() {
198 if (e.getAttribute('aspect') == 'process')
199 JsHttpRequest.request(this, null, 30000);
201 JsHttpRequest.request(this);
205 'button': function(e) {
207 var func = _validate[e.name];
210 if (typeof old != 'function') {
213 e.onclick = function() {
215 { old(); return true;}
223 '.amount': function(e) {
224 if(e.onblur==undefined) {
225 e.onblur = function() {
226 var dec = this.getAttribute("dec");
227 price_format(this.name, get_amount(this.name), dec);
231 '.searchbox': // emulated onchange event handling for text inputs
233 e.setAttribute('_last_val', e.value);
234 e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
235 e.onblur = function() {
236 var val = this.getAttribute('_last_val');
237 if (val != this.value) {
238 this.setAttribute('_last_val', this.value);
239 JsHttpRequest.request('_'+this.name+'_changed', this.form);
243 'select': function(e) {
244 if(e.onfocus==undefined) {
245 e.onfocus = function() {
249 if (c == 'combo' || c == 'combo2')
250 _set_combo_select(e);
253 'textarea,a': function(e) {
254 if(e.onfocus==undefined) {
255 e.onfocus = function() {
258 e.onmouseover = function(e) {
264 'a.printlink': function(l) {
265 l.onclick = function() {
267 JsHttpRequest.request(this);
271 'a': function(e) { // traverse menu
272 e.onkeydown = function(ev) {
273 ev = ev||window.event;
274 key = ev.keyCode||ev.which;
275 if(key==37 || key==38 || key==39 || key==40) {
276 move_focus(key, e, document.links);
277 ev.returnValue = false;
282 'ul.ajaxtabs': function(ul) {
283 var ulist=ul.getElementsByTagName("li");
284 for (var x=0; x<ulist.length; x++){ //loop through each LI e
285 var ulistlink=ulist[x].getElementsByTagName("input")[0];
286 if(ulistlink.onclick==undefined) {
287 // ? var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
288 var url = ulistlink.form.action
289 ulistlink.onclick=function(){
296 /* 'tr.editrow': function(e) {
297 e.onkeydown = function(ev) {
298 ev = ev||window.event;
299 key = ev.keyCode||ev.which;
301 // Find & click additem/update button
303 } else if(key == 27) {
309 */ '#msgbox': function(e) {
310 // this is to avoid changing div height after ajax update in IE7
311 e.style.display = e.innerHTML.length ? 'block' : 'none';
314 'a.date_picker': function(e) {
315 // this un-hides data picker for js enabled browsers
316 e.href = date_picker(this.getAttribute('rel'));
317 e.style.display = '';
318 e.tabindex = -1; // skip in tabbing order
322 function stopEv(ev) {
323 ev.returnValue = false;
324 ev.cancelBubble = true;
325 if(ev.preventDefault) ev.preventDefault();
329 Modified accesskey system. While Alt key is pressed letter keys moves
330 focus to next marked link. Alt key release activates focused link.
332 function setHotKeys() {
333 document.onkeydown = function(ev) {
334 ev = ev||window.event;
335 key = ev.keyCode||ev.which;
336 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
341 else if (_hotkeys.alt && ((key>47 && key<58) || (key>64 && key<91))) {
342 var n = _hotkeys.focus;
343 var l = document.links;
345 key = String.fromCharCode(key);
346 for (var i=0; i<cnt; i++) {
348 // check also if the link is visible
349 if (l[n].accessKey==key && l[n].scrollWidth) {
351 // The timeout is needed to prevent unpredictable behaviour on IE.
352 var tmp = function() {document.links[_hotkeys.focus].focus();};
359 if((ev.ctrlKey && key == 13) || key == 27) {
360 _hotkeys.alt = false; // cancel link selection
362 ev.cancelBubble = true;
363 if(ev.stopPropagation) ev.stopPropagation();
364 // activate submit/escape form
365 for(var j=0; j<this.forms.length; j++) {
366 var form = this.forms[j];
367 for (var i=0; i<form.elements.length; i++){
368 var el = form.elements[i];
369 var asp = el.getAttribute('aspect');
370 if ((asp=='default' && key==13)||(asp=='cancel' && key==27)) {
371 JsHttpRequest.request(el);
372 ev.returnValue = false;
377 ev.returnValue = false;
382 document.onkeyup = function(ev) {
383 if (_hotkeys.alt==true) {
384 ev = ev||window.event;
385 key = ev.keyCode||ev.which;
388 _hotkeys.alt = false;
389 if (_hotkeys.focus>=0) {
390 var link = document.links[_hotkeys.focus];
394 if (link.target=='_blank') {
395 window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
396 openWindow(link.href,'_blank');
398 window.location = link.href;
407 Behaviour.register(inserts);
409 Behaviour.addLoadEvent(setFocus);
410 Behaviour.addLoadEvent(setHotKeys);