PHP Classes

File: js/ajax_lib.js

Recommend this page to a friend!
  Classes of Francisco Caserio   cls_form   js/ajax_lib.js   Download  
File: js/ajax_lib.js
Role: Auxiliary script
Content type: text/plain
Description: ajax classes
Class: cls_form
Generate Web pages to manipulate database records
Author: By
Last change: role revision
Date: 16 years ago
Size: 21,884 bytes
 

Contents

Class file image Download
// ajax object versao 3.2 - 10/09/2008 release_submit = 1; function fieldFilter(obj_id, field_id, xmlpath) { this.obj_id = obj_id; this.field_id = field_id; this.xmlpath = xmlpath; this.default_value = ""; this.default_label = ""; this.readonly = 0; this.ck_mail = 0; this.keyword_limit = 3; this.xtra_field = new Array; this.xtra_field_ref = new Array(); this.xtra_action = new Array; this.reset_action = new Array; } fieldFilter.prototype.setDefault = function(id, label) { this.default_value = id; this.default_label = label; } fieldFilter.prototype.setReadOnly = function() { this.readonly = 1; } fieldFilter.prototype.setKeywordLimit = function(val) { this.keyword_limit = val; } fieldFilter.prototype.addField = function(field, xml_label) { if (!xml_label) xml_label = field; var ix = this.xtra_field.length; this.xtra_field[ix] = new Array(); this.xtra_field[ix]["field"] = field; this.xtra_field[ix]["xml_label"] = xml_label; } fieldFilter.prototype.addAction = function(action) { var ix = this.xtra_action.length; this.xtra_action[ix] = new Array(); this.xtra_action[ix]["action"] = action; } fieldFilter.prototype.addResetAction = function(action) { var ix = this.reset_action.length; this.reset_action[ix] = new Array(); this.reset_action[ix]["action"] = action; } fieldFilter.prototype.ckMail = function(action) { this.ck_mail = 1; } fieldFilter.prototype.setXtraField = function(ix,obj_pos) { for (var i=0; i<this.xtra_field.length; i++) { if (document.getElementById(this.xtra_field[i]["field"]+obj_pos)) { if (document.getElementById(this.xtra_field[i]["field"]+obj_pos).type == "checkbox") { document.getElementById(this.xtra_field[i]["field"]+obj_pos).checked = this.xtra_field_ref[this.obj_id][this.xtra_field[i]["field"]][ix] == 1 ? true : false; } else { document.getElementById(this.xtra_field[i]["field"]+obj_pos).value = this.xtra_field_ref[this.obj_id][this.xtra_field[i]["field"]][ix]; } } } } fieldFilter.prototype.applyAction = function(id) { this.id = id; for (var i=0; i<this.xtra_action.length; i++) { eval(this.xtra_action[i]["action"]); } } fieldFilter.prototype.build = function(size,width) { document.write(this.getObjStr(size,width)); } fieldFilter.prototype.getObjStr = function(size,width) { var str = ""; var onfocus_str = "temp=this.value;release_submit=0;"; var onblur_str = "release_submit=1;"; var onchange_str = "document.getElementById(this.id.replace('list','')).value=this.value;document.getElementById(this.id.replace('list','nome')).value=this[this.selectedIndex].text;"; onchange_str += this.obj_id + ".setXtraField(this.selectedIndex,this.id.replace('list"+this.field_id+"',''));"; onchange_str += this.obj_id + ".applyAction(this.id);"; //for (var i=0; i<this.xtra_action.length; i++) { // onchange_str += this.xtra_action[i]["action"] + ";"; //} str += "<table border=0 cellspacing=0 cellpadding=0><tr>\n"; str += "<td>\n"; str += "<input type=text name=nome"+this.field_id+" id=nome"+this.field_id+" value=\"" + this.default_label + "\" size="+size+" " + (this.readonly == 0 ? "onkeydown=" + this.obj_id + ".getFilter(event,this.id.replace('nome"+this.field_id+"','')) onKeyPress=\"return disableEnterKey(event)\"" : "readonly") + " onblur="+onblur_str+" onfocus="+onfocus_str+" class=formpeq>\n"; str += "<select name=list"+this.field_id+" id=list"+this.field_id+" class=formpeq onchange=" + onchange_str + " style=display:none" + (width ? ";width:"+width+"px" : "") + ">\n"; str += "</select>\n"; str += "<input type=hidden name=\""+this.field_id+"\" id=\""+this.field_id+"\" value=\"" + this.default_value + "\">\n"; str += "</td>\n"; if (this.readonly == 0) { str += "<td style=padding-left:5>\n"; str += "<input type=button id=filtro"+this.field_id+" value=\"Filtrar\" onclick=\"" + this.obj_id + ".getFilter(false,this.id.replace('filtro"+this.field_id+"',''))\" class=submit style=width:40px>\n"; str += "<input type=button id=reset"+this.field_id+" value=\"Limpar\" onclick=" + this.obj_id + ".resetFilter(this.id.replace('reset"+this.field_id+"','')) class=submit style=width:40px;display:none>\n"; str += "</td>\n"; } str += "</tr></table>\n"; return str; } fieldFilter.prototype.getFilter = function(ev,pos) { var th = this; var obj_pos = pos ? pos : ''; var filter_str = document.getElementById("nome" + this.field_id + obj_pos).value; if (!ev || ev.keyCode == 13) { if (ev && document.all) ev.keyCode = 9; // change enter behaviour for IE ER = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{2,64}(\.[a-z0-9-]{2,64})*\.[a-z]{2,4}$"); if (filter_str.length >= this.keyword_limit && (this.ck_mail == 0 || ER.test(filter_str))) { // limpa lista dropdown for (var i=document.getElementById("list" + this.field_id + obj_pos).length-1; i>= 0; i--) { document.getElementById("list" + this.field_id + obj_pos).remove(i); } // AJAX request using sarissa var xmlhttp = new XMLHttpRequest(); xmlpath = parseXmlPath(this.xmlpath, obj_pos); var url = xmlpath + (xmlpath.indexOf("?")>=0?"&":"?") + "pchave=" + filter_str; //window.open(url,'_blank') xmlhttp.open('POST', url, true); xmlhttp.onreadystatechange = function() { //if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4) { if (!document.all && xmlhttp.responseXML == null && confirm("Foi detectado um erro.\nClique OK para mais informações")) window.open(url, '_blank'); var objxml = xmlhttp.responseXML.getElementsByTagName('data'); if (objxml.length > 0) { th.xtra_field_ref[th.obj_id] = new Array(); for (var i=0; i<objxml.length; i++) { if (i == 0) { document.getElementById(th.field_id + obj_pos).value = getNodeValue(objxml[i],'id'); document.getElementById("nome" + th.field_id + obj_pos).value = getNodeValue(objxml[i],'text'); for (var j=0; j<th.xtra_field.length; j++) { th.xtra_field_ref[th.obj_id][th.xtra_field[j]["field"]] = new Array(); } } var oOption = document.createElement("OPTION"); document.getElementById("list" + th.field_id + obj_pos).options.add(oOption); oOption.value = getNodeValue(objxml[i],'id'); oOption.text = getNodeValue(objxml[i],'text'); oOption.title = getNodeValue(objxml[i],'text'); for (var j=0; j<th.xtra_field.length; j++) { th.xtra_field_ref[th.obj_id][th.xtra_field[j]["field"]][i] = getNodeValue(objxml[i], th.xtra_field[j]["xml_label"]); } } if (objxml.length > 0) th.setXtraField(0,obj_pos); } else if (objxml.length == 0) { var oOption = document.createElement("OPTION"); document.getElementById(th.field_id + obj_pos).value = ""; document.getElementById("list" + th.field_id + obj_pos).options.add(oOption); oOption.value = ""; oOption.text = "Não consta: " + filter_str; } for (var j=0; j<th.xtra_action.length; j++) { action = th.xtra_action[j]["action"]; while (action.indexOf('this.id') > 0) { action = action.replace('this.id','"'+th.field_id+obj_pos+'"'); } eval(action); } } } // function() xmlhttp.send(null); // end AJAX request using sarissa //var oOption = document.createElement("OPTION"); //document.getElementById("list"+id).options.add(oOption); //oOption.value = "RESET"; //oOption.text = "RESETAR"; document.getElementById("list" + this.field_id + obj_pos).style.display = ''; document.getElementById("reset" + this.field_id + obj_pos).style.display = ''; if (document.all) document.getElementById("list" + this.field_id + obj_pos).focus(); document.getElementById("nome" + this.field_id + obj_pos).style.display = 'none'; document.getElementById("filtro" + this.field_id + obj_pos).style.display = 'none'; } else if (filter_str.length < this.keyword_limit) { alert("Digite ao menos " + this.keyword_limit + " caracteres para filtrar a lista") } } } fieldFilter.prototype.resetFilter = function(pos) { if (!pos) pos = ""; document.getElementById(this.field_id + pos).value = ''; document.getElementById("nome" + this.field_id + pos).value = ''; document.getElementById("nome" + this.field_id + pos).style.display = ''; document.getElementById("list" + this.field_id + pos).style.display = 'none'; document.getElementById("filtro" + this.field_id + pos).style.display = ''; document.getElementById("reset" + this.field_id + pos).style.display = 'none'; document.getElementById("nome" + this.field_id + pos).focus(); for (var j=0; j<this.reset_action.length; j++) { action = this.reset_action[j]["action"].replace('this.id','"'+this.field_id+'"'); eval(action); } } // dropdownLoader function dropdownLoader(field_id, xmlpath) { //this.obj_id = obj_id; this.field_id = field_id; this.xmlpath = xmlpath; this.xtra_action = new Array; this.xtra_label = new Array; this.default_value = ""; this.ck_prompt = 1; this.ck_choose = 1; } dropdownLoader.prototype.setDefault = function(id) { this.default_value = id; } dropdownLoader.prototype.addAction = function(action) { var ix = this.xtra_action.length; this.xtra_action[ix] = new Array(); this.xtra_action[ix]["action"] = action; } dropdownLoader.prototype.addLabel = function(label, xml_label, field) { if (!xml_label) xml_label = label; var ix = this.xtra_label.length; this.xtra_label[ix] = new Array(); this.xtra_label[ix]["label"] = label; this.xtra_label[ix]["xml_label"] = xml_label; this.xtra_label[ix]["field"] = field; } dropdownLoader.prototype.removePrompt = function() { this.ck_prompt = 0; } dropdownLoader.prototype.removeChooser = function() { this.ck_choose = 0; } /* dropdownLoader.prototype.addField = function(field, xml_label) { this.addLabel(xml_label, xml_label, field); } */ dropdownLoader.prototype.load = function(id) { var th = this; // limpa dropdown de destino if (!document.getElementById(this.field_id)) return false; if (document.getElementById(this.field_id).value != "" && this.ck_prompt == 1 && !confirm("Esta alteração irá alterar a lista de opções de um campo que já está preenchido. Deseja prosseguir?")) { document.getElementById(this.field_id).form.reset(); return false; } for (var i=document.getElementById(this.field_id).length-1; i>=0; i--) { document.getElementById(this.field_id).remove(i); } // AJAX request using sarissa var xmlhttp = new XMLHttpRequest(); //xmlhttp.open('POST', this.xmlpath, true); var url = this.xmlpath + (this.xmlpath.indexOf("?")>=0?"&":"?") + "id_ref=" + id; //window.open(url,"_blank"); //alert(url) xmlhttp.open('POST', url, true); xmlhttp.onreadystatechange = function() { //if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4) { if (!document.all && xmlhttp.responseXML == null && confirm("Foi detectado um erro.\nClique OK para mais informações")) window.open(url, '_blank'); //alert("Processo de manutencao - identificando campo: " + th.field_id) var objxml = xmlhttp.responseXML.getElementsByTagName('data'); if (document.getElementById(th.field_id).size <= 1 && (th.ck_choose == 1 || objxml.length == 0) && //document.getElementById(th.field_id).length > 0 && //objxml.length > 1 && 1 == 1) { var oOption = document.createElement("OPTION"); document.getElementById(th.field_id).options.add(oOption); oOption.text = "-- Escolha --"; oOption.value = ""; } th.xtra_label_ref = new Array(); for (var i=0; i<objxml.length; i++) { var oOption = document.createElement("OPTION"); document.getElementById(th.field_id).options.add(oOption); id = getNodeValue(objxml[i], 'id'); str = getNodeValue(objxml[i], 'text'); oOption.text = document.getElementById(th.field_id).style.width ? str : str.substring(0,50) + (str.length > 50 ? "..." : ""); oOption.title = str; oOption.value = id; th.xtra_label_ref[id] = new Array(); for (var j=0; j<th.xtra_label.length; j++) { // homologado em IE e Firefox eval("oOption." + th.xtra_label[j]["label"] + " = " + getNodeValue(objxml[i], th.xtra_label[j]["xml_label"])); th.xtra_label_ref[id][th.xtra_label[j]["label"]] = getNodeValue(objxml[i], th.xtra_label[j]["xml_label"]); } } if (th.ck_prompt == 0 && document.getElementById(th.field_id).length > 0) document.getElementById(th.field_id).selectedIndex = 1; if (objxml.length > 0) document.getElementById(th.field_id).style.display = '' for (var i=0; i<th.xtra_action.length; i++) { action = th.xtra_action[i]["action"]; while (action.indexOf('this.id') > 0) { action = action.replace('this.id','"'+th.field_id+'"'); } eval(action); } if (th.default_value != "") document.getElementById(th.field_id).value = th.default_value; else document.getElementById(th.field_id).selectedIndex = 0; } } xmlhttp.send(null); } dropdownLoader.prototype.reset = function() { for (var i=document.getElementById(this.field_id).length-1; i>0; i--) { document.getElementById(this.field_id).remove(i); } if (document.getElementById(this.field_id).size == 0) document.getElementById(this.field_id)[0].text = "-- Escolha --"; else document.getElementById(this.field_id).remove(0); } // tableLoader function tableLoader(tr_id, xmlpath) { //this.obj_id = obj_id; this.tr_id = tr_id; this.xmlpath = xmlpath; this.xtra_action = new Array; this.label = new Array; this.ck_prompt = 0; this.css = null; this.count_id = null; } tableLoader.prototype.setClass = function(css) { this.css = css; } tableLoader.prototype.registerTotal = function(id) { this.count_id = id; } tableLoader.prototype.addLabel = function(xml_label, align) { var ix = this.label.length; this.label[ix] = new Array(); this.label[ix]["xml_label"] = xml_label; this.label[ix]["align"] = "left"; } tableLoader.prototype.addField = function(field, type, label) { var ix = this.label.length; this.label[ix] = new Array(); this.label[ix]["field"] = field; this.label[ix]["type"] = type; this.label[ix]["field_label"] = label; this.label[ix]["align"] = "left"; this.label[ix]["prop"] = new Array(); } tableLoader.prototype.addFieldProp = function(prop, val) { var ix = this.label.length - 1; this.label[ix]["prop"][prop] = (val ? val : false); } tableLoader.prototype.setLabelAlignment = function(align) { var ix = this.label.length - 1; this.label[ix]["align"] = align; } tableLoader.prototype.addAction = function(action) { var ix = this.xtra_action.length; this.xtra_action[ix] = new Array(); this.xtra_action[ix]["action"] = action; } tableLoader.prototype.load = function() { var th = this; // limpa dropdown de destino if (!document.getElementById(this.tr_id)) return false; if (document.getElementById(this.tr_id+"0") && this.ck_prompt == 1 && !confirm("Esta alteração irá alterar a lista de opções de um campo que já está preenchido. Deseja prosseguir?")) { document.getElementById(this.tr_id).form.reset(); return false; } this.reset(); // add loading warning if (document.getElementById(th.tr_id).tagName == "TABLE") { var tbody = document.getElementById(th.tr_id).getElementsByTagName("TBODY")[0]; var pos = -1; } else if (document.getElementById(th.tr_id).tagName == "TR") { var tbody = document.getElementById(th.tr_id).parentNode; var pos = document.getElementById(th.tr_id).rowIndex; } var row = tbody.insertRow(pos); row.id = this.tr_id + "loading"; var cell = document.createElement("TD"); cell.appendChild(document.createTextNode("Carregando...")); cell.style.textDecoration = "blink"; cell.colSpan = this.label.length; cell.className = this.css; row.appendChild(cell); // AJAX request using sarissa var xmlhttp = new XMLHttpRequest(); var url = parseXmlPath(this.xmlpath); //window.open(url,"_blank"); xmlhttp.open('POST', url, true); xmlhttp.onreadystatechange = function() { //if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4) { if (xmlhttp.responseXML == null && confirm("Foi detectado um erro.\nClique OK para mais informações")) window.open(url, '_blank'); // remove loading warning var remove = document.getElementById(th.tr_id+"loading"); remove.parentNode.removeChild(remove); // add lines var objxml = xmlhttp.responseXML.getElementsByTagName('data'); //alert(objxml.length) for (var i=0; i<objxml.length; i++) { //var row = document.createElement("TR"); if (pos >= 0) pos++; var row = tbody.insertRow(pos); row.id = th.tr_id + i; // nome do grupo for (var j=0; j<th.label.length; j++) { var cell = document.createElement("TD"); cell.className = th.css; cell.align = th.label[j]["align"]; if (th.label[j]["xml_label"]) { cell.appendChild(document.createTextNode(getNodeValue(objxml[i], th.label[j]["xml_label"]))); } else if (th.label[j]["field"]) { //alert("<INPUT TYPE='" + th.label[j]["type"] + "' NAME='" + th.label[j]["field"] + "' ID='" + th.label[j]["field"] + "' VALUE='" + getNodeValue(objxml[i], th.label[j]["field"]) + "'>"); var field = document.createElement("INPUT"); field.type = th.label[j]["type"]; field.id = th.label[j]["field"]+i; field.name = th.label[j]["field"]+i; //"[]"; field.value = getNodeValue(objxml[i], th.label[j]["field"]); for (key in th.label[j]["prop"]) { if (th.label[j]["prop"][key] && key.substring(0,2) == "on") { eval("field." + key + " = function() { " + th.label[j]["prop"][key] + " }"); } else if (th.label[j]["prop"][key]) { eval("field." + key + " = '" + th.label[j]["prop"][key] + "'"); } else if (getNodeValue(objxml[i], key) != "") { eval("field." + getNodeValue(objxml[i], key) + " = true"); } }; cell.appendChild(field); if (th.label[j]["field_label"]) { cell.noWrap = true; if (getNodeValue(objxml[i], th.label[j]["field_label"]) != "") cell.appendChild(document.createTextNode(getNodeValue(objxml[i], th.label[j]["field_label"]))); else cell.appendChild(document.createTextNode(th.label[j]["field_label"])); } } if (th.label[j]["type"] && th.label[j]["type"] == "hidden") row.appendChild(field); else row.appendChild(cell); } //tbody.appendChild(row); } if (th.count_id != null) { document.getElementById(th.count_id).value = objxml.length; } for (var i=0; i<th.xtra_action.length; i++) { action = th.xtra_action[i]["action"]; while (action.indexOf('this.id') > 0) { action = action.replace('this.id','"'+th.tr_id+'"'); } eval(action); } } } xmlhttp.send(null); } tableLoader.prototype.reset = function() { c = 0; while (document.getElementById(this.tr_id+c)) { var remove = document.getElementById(this.tr_id+c); remove.parentNode.removeChild(remove); c++; } } // other ajax calls function fn_get_list(dropdown_id,xmlpath,id,ck_noprompt,xtraaction) { temp = new dropdownLoader(dropdown_id, xmlpath); if (ck_noprompt && ck_noprompt == 1) temp.removePrompt(); if (xtraaction) temp.addAction(xtraaction); if (id == 0) temp.reset(); else temp.load(id); } function fn_set_comment(field_id, xmlpath) { // AJAX request using sarissa var xmlhttp = new XMLHttpRequest(); //xmlhttp.open('POST', xmlpath, true); xmlhttp.open('POST', xmlpath, true); //alert(xmlpath); xmlhttp.onreadystatechange = function() { //if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4) { var objxml = xmlhttp.responseXML.getElementsByTagName('data'); //alert(objxml.length); var str = "<br>"; for (var i=0; i<objxml.length; i++) { str += getNodeValue(objxml[i], 'text') + "\n"; } document.getElementById("comment_" + field_id).innerHTML = str; } } xmlhttp.send(null); } // support functions function disableEnterKey(e) { var key; if (window.event) key = window.event.keyCode; //IE else key = e.which; //firefox if (key == 13) return false; else return true; } function getNodeValue(obj,tag) { //if (!obj.getElementsByTagName(tag)[0].firstChild) alert(obj + ";" + tag) if (obj.getElementsByTagName(tag)[0]) { return obj.getElementsByTagName(tag)[0].firstChild.nodeValue; } else return ""; } function isArray(obj) { if (obj.constructor.toString().indexOf("Array") == -1) return false; else return true; } function list2array(list,sep) { if (!sep) sep = ","; var arr = new Array(); var temp = ""; for (var i=0; i<list.length; i++) { if (list.charAt(i) == sep) { arr[arr.length] = temp; temp = ""; } else temp += list.charAt(i); } arr[arr.length] = temp; return arr; } function parseXmlPath(xmlpath, obj_pos) { if (xmlpath.indexOf("[") >= 0) { var match = xmlpath.match(/\[([_A-Za-z0-9-]+)\]/g); //alert(match) for (var i=0; i<match.length; i++) { var ix = match[i].substring(1, match[i].length-1); if (obj_pos && document.getElementById(ix+obj_pos)) xmlpath = xmlpath.replace(match[i].toString(), document.getElementById(ix+obj_pos).value); else xmlpath = xmlpath.replace(match[i].toString(), document.getElementById(ix).value); } } return xmlpath; }