From: Janusz Dobrowolski Date: Sun, 15 Jun 2008 21:06:11 +0000 (+0000) Subject: Added searchbox class selector (onchange event in text input), text selection after... X-Git-Tag: v2.4.2~19^2~2016 X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;h=35f960e8c8b1941c5d841ac916ed1468b376ff17;p=fa-stable.git Added searchbox class selector (onchange event in text input), text selection after focus --- diff --git a/js/inserts.js b/js/inserts.js index 235de181..b535cd03 100644 --- a/js/inserts.js +++ b/js/inserts.js @@ -31,7 +31,7 @@ function _expand(tabobj) { alltabs[i].className = "ajaxbutton" //deselect all tabs } tabobj.className = "current"; - JsHttpRequest.request(tabobj.name) + JsHttpRequest.request(tabobj) } } @@ -42,14 +42,20 @@ function expandtab(tabcontentid, tabnumber) { } function _set_combo_input(e) { + e.setAttribute('_last', e.value); e.onblur=function() { var but_name = this.name.substring(0, this.name.length-4)+'button'; var button = document.getElementsByName(but_name)[0]; var select = document.getElementsByName(this.getAttribute('rel'))[0]; save_focus(select); -// this.style.display='none'; - if(button) { // if *_button set submit search request - JsHttpRequest.request(but_name); +// submit request if there is submit_on_change option set and +// search field has changed. + if (button && (this.value != this.getAttribute('_last'))) { + JsHttpRequest.request(button); + } else if(this.className=='combo2') { + this.style.display = 'none'; + select.style.display = 'inline'; + setFocus(select.name); } return false; }; @@ -85,7 +91,10 @@ function _update_box(s) { var box = document.getElementsByName(rel)[0]; if(box && s.selectedIndex>=0) { var opt = s.options[s.selectedIndex]; - if(box) box.value = byid ? opt.value : opt.text; + if(box) { + box.value = byid ? opt.value : opt.text; + box.setAttribute('_last', box.value); + } } } @@ -103,7 +112,7 @@ function _set_combo_select(e) { var sname = '_'+s.name+'_update'; var update = document.getElementsByName(sname)[0]; if(update) { - JsHttpRequest.request(sname); + JsHttpRequest.request(update); } } return true; @@ -146,7 +155,7 @@ var inserts = { 'input.ajaxsubmit,input.editbutton,input.navibutton': function(e) { e.onclick = function() { - JsHttpRequest.request(this.name); + JsHttpRequest.request(this); return false; } }, @@ -158,6 +167,26 @@ var inserts = { }; } }, + '.searchbox': // emulated onchange event handling for text inputs + function(e) { + e.setAttribute('_last_val', e.value); + e.setAttribute('autocomplete', 'off'); //must be off when calling onblur + e.onblur = function() { + var val = this.getAttribute('_last_val'); + if (val != this.value) { + this.setAttribute('_last_val', this.value); + JsHttpRequest.request('_'+this.name+'_changed', this.form); + } + } + e.onkeydown = function(ev) { + ev = ev||window.event; + key = ev.keyCode||ev.which; + if (key == 13 && (this.value != this.getAttribute('_last_val'))) { + this.blur(); + return false; + } + } + }, 'select': function(e) { if(e.onfocus==undefined) { e.onfocus = function() { diff --git a/js/utils.js b/js/utils.js index 2bd2cfa5..a2dc4ba1 100644 --- a/js/utils.js +++ b/js/utils.js @@ -1,14 +1,35 @@ // // JsHttpRequest class extensions. // - JsHttpRequest.request= function(submit) { - var url = window.location.toString(); - url = url.substring(0, url.indexOf('?')); +// Main functions for asynchronus form submitions +// Trigger is the source of request and can have following forms: +// - input object - all form values are also submited +// - arbitrary string - POST var trigger with value 1 is added to request; +// if form parameter exists also form values are submited, otherwise +// request is directed to current location +// + JsHttpRequest.request= function(trigger, form) { + + + var submitObj = typeof(trigger) == "string" ? + document.getElementsByName(trigger)[0] : trigger; + + form = form || (submitObj && submitObj.form); + + var url = form ? form.action : + window.location.toString(); + + if (!form) url = url.substring(0, url.indexOf('?')); + + var values = this.formValues(trigger, form); + if (!submitObj) + values[trigger] = 1; + // this is to avoid caching problems + values['_random'] = Math.random()*1234567; JsHttpRequest.query( 'POST '+url, // backend - this.formValues(submit), - + values, // Function is called when an answer arrives. function(result, errors) { // Write the answer. @@ -31,19 +52,19 @@ objElement.value = data; else objElement.innerHTML = data; // selector, div, span etc - } else if(cmd=='di') { // disable/enable element + } else if(cmd=='di') { // disable/enable element objElement.disabled = data; } else if(cmd=='fc') { // set focus _focus = data; } else if(cmd=='js') { // evaluate js code eval(data); } else if(cmd=='rd') { // client-side redirection - _page_reload = true; window.location = data; } else { errors = errors+'
Unknown ajax function: '+cmd; } } + // Write errors to the debug div. document.getElementById('msgbox').innerHTML = errors; @@ -60,19 +81,19 @@ } // returns input field values submitted when form button 'name' is pressed // - JsHttpRequest.formValues = function(inp) + JsHttpRequest.formValues = function(inp, objForm) { - var objForm; - var submitObj; + var submitObj = inp; var q = {}; + if (typeof(inp) == "string") submitObj = document.getElementsByName(inp)[0]; else submitObj = inp; - if(submitObj) { - objForm = submitObj.form; - } + + objForm = objForm || (submitObj && submitObj.form); + if (objForm) { var formElements = objForm.elements; @@ -83,7 +104,7 @@ if (el.type ) if( ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false) - || (el.type == 'submit' && el.name!=submitObj.name)) + || (el.type == 'submit' && (!submitObj || el.name!=submitObj.name))) continue; if (el.disabled && el.disabled == true) continue; @@ -105,8 +126,6 @@ } } } - // this is to avoid caching problems - q['_random'] = Math.random()*1234567; return q; } // @@ -171,7 +190,8 @@ function setFocus(name, byId) { if(el && el.focus) { // The timeout is needed to prevent unpredictable behaviour on IE & Gecko. // Using tmp var prevents crash on IE5 - var tmp = function() {el.focus()}; + + var tmp = function() {el.focus(); if (el.select) el.select();}; setTimeout(tmp, 0); } }