2 * This file is part of NOALYSS.
4 * NOALYSS is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * NOALYSS is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with NOALYSS; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18// Copyright Author Dany De Bontridder danydb@aevalys.eu
21 * Javascript object to manage ajax calls to
22 * save , input or delete data row.
24 respond with a XML file , the tag status for the result
25 and data the HTML code to display
29 - set_control(p_ctl_name)
30 - set_callback (p_new_callback)
31 - param_add (json object)
32 - parseXML (private function)
36 How to call a function AFTER save ?
37 You set a function afterSaveFct like in the example, it will be trigger after you submit the FORM
38 a function named afterSaveFct(r) where r is the row in HTML to display, with a attribute ctl_pk_id which is the primary key and id of the row
41 document_attach_obj.afterSaveFct=function(e) {
42 var ctl_pk_id=e.getAttribute("ctl_pk_id");
43 var formData = new FormData();
44 var file_data=document.getElementById('da_file_name');
45 formData.append('da_file_name', file_data.files[0]);
46 var xhr = new XMLHttpRequest();
47 xhr.open("POST", "ajax.php?p_id="+ctl_pk_id+"&do=upload_document", true);
50 As a hidden parameter the Manage_Table:object_name must be set
52 // the object_name is tbl6030ee4ee519e , in the table each row (TR) has an attribute ctl_pk_id which is the id (primary key)
53 // <tr ctl_pk_id="" ...> </tr>
54 tbl6030ee4ee519e.afterSaveFct=function(p_param) {
57 console.log(p_param.getAttribute(ctl_pk_id))
63 * @param {string} p_table_name the data table on which we're working
64 in javascript , to create an object to manipulate the table
67// Version will manage the table "version"
68 var version=new ManageTable("version");
70 // the file ajax_my.php will be called
71 version.set_callback("ajax_my.php");
73 // Add supplemental parameter to this file
74 version.param_add ({"plugin_code":"OIC"};
76 // Set the id of dialog box , table and tr prefix
77 version.set_control("dialbox1");
79 //The answer from ajax must be like this template
82 <ctl> id of the control to update for diag.box, row </ctl>
83 <status> OK , FAIL ...</status>
84 <html> Html to display</html>
88var ManageTable = function (p_table_name)
90 this.callback = "ajax.php"; //!< File to call
91 this.control = "dtr"; //<! Prefix Id of dialog box, table, row
94 this.afterSaveFct=undefined; // function to call after "save"
95 this.cssclass="inner_box";
96 this.param = {"table": p_table_name, "ctl_id": this.control}; //<! default value to pass
97 this.set_style=function(p_json) {
102 * @param {string} p_column column number start from 0
103 * @param {string} p_type type of sort (string, numeric)
104 * @returns {ManageTable.set_sort}
106 this.set_sort = function (p_column) {
108 this.sort_column=p_column;
111 this.set_dialog_box=function (p_dialog_box){
112 this.control=p_dialog_box;
115 * Insert the row a the right location
116 * @param {type} p_element_row DOMElement TR
119 this.insertRow=function(p_table,p_element_row,sort_column) {
122 //compute the length of row
123 //if rows == 0 or the sort is not defined then append
124 if ( this.sort_column==-1 || p_table.rows.length < 2 || p_table.rows[1].cells[sort_column] == undefined || p_table.rows[1].cells[sort_column].getAttribute('sort_value') == undefined ) {
125 var row=p_table.insertRow(p_table.rows.length);
126 row.innerHTML=p_element_row.innerHTML;
127 row.id=p_element_row.id;
128 row.ctl_pk_id=row.id;
131 // loop for each row , compare the innerHTML of the column with the
132 // value if less than insert before
134 for (i = 1;i<p_table.rows.length;i++) {
135 var table_value=p_table.rows[i].cells[sort_column].getAttribute('sort_value') ;
136 var element_value=p_element_row.cells[sort_column].getAttribute('sort_value');
138 // if both are numeric so we force a numeric comparison
139 if ( ! isNaN(parseFloat(table_value)) && ! isNaN(parseFloat(element_value)) ) {
140 table_value=parseFloat(table_value);
141 element_value=parseFloat(element_value);
144 if (table_value > element_value) {
146 var row=p_table.insertRow(i);
147 row.innerHTML=p_element_row.innerHTML;
148 row.id=p_element_row.id;
149 row.ctl_pk_id=row.id;
153 p_table.appendChild(p_element_row);
155 console.log("insertRow failed with "+e.message);
162 *@fn ManageTable.set_control
163 *@brief Set the id of the control name , used as
164 * prefix for dialog box , table id and row
165 *@param string p_ctl_name id of dialog box
167 this.set_control = function (p_ctl_name) {
168 this.control = p_ctl_name;
171 *@brief set the name of the callback file to
172 * call by default it is ajax.php
174 this.set_callback = function (p_new_callback) {
175 this.callback = p_new_callback;
178 *@brief By default send the json param variable
179 * you can add a json object to it in order to
180 * send it to the callback function
182 this.param_add = function (p_obj) {
184 for (var key in this.param) {
185 result[key] = this.param[key];
187 for (var key in p_obj) {
188 result[key] = p_obj[key];
194 @brief receive answer from ajax and fill up the
195 private object "answer"
196 @param req Ajax answer
198 this.parseXML = function (req) {
200 if (req.responseText==='NOCONX') { reconnect();throw new Error("NOCONX") ;}
201 var xml = req.responseXML;
202 var status = xml.getElementsByTagName("status");
203 var ctl = xml.getElementsByTagName("ctl");
204 var html = xml.getElementsByTagName("html");
205 var ctl_row = xml.getElementsByTagName("ctl_row");
206 var ctl_pk_id=xml.getElementsByTagName("ctl_pk_id");
207 if (status.length == 0 || ctl.length == 0 || html.length == 0)
209 throw content[53] + req.responseText;
213 answer['status'] = getNodeText(status[0]);
214 answer['ctl'] = getNodeText(ctl[0]);
215 answer['ctl_row'] = getNodeText(ctl_row[0]);
216 answer['html'] = getNodeText(html[0]);
217 answer['ctl_pk_id'] = getNodeText(ctl_pk_id[0]);
220 console.error("managetable:parseXML")
226 *Call the ajax with the action save , it is possible to call a function after the save by setting
227 * a function to afterSaveFct.As a hidden parameter the Manage_Table:object_name must be set
228 * @param form_id string id of the FORM format ("frm"+object_name+"_"+p_id)
231 tbl6030f6f8c336a.afterSaveFct=function() {
232 // if p_id == -1 then we are adding
233 if ( this.param.p_id != -1 ) { return;}
235 var id=this.new_row.id.replace('tbl6030f6f8c336a_','');
236 // recall input ManageTable.input
237 this.input(id,'tbl6030f6f8c336a');
240 <caption>when I introduce a new element I need to reopen it to complete the missing information. (this) contains
244 * *Call the ajax with the action save , it is possible to call a function after the save by creating
245 * + * a function named afterSaveFct(r) where r is the row in HTML to display, with a attribute ctl_pk_id which is the
246 * + * primary key and id of the row
248 * + * As a hidden parameter the Manage_Table:object_name must be set
251 this.save = function (form_id) {
255 this.param['action'] = 'save';
256 var form = $(form_id).serialize(true);
257 param_form = json_concat(this.param,form);
263 new Ajax.Request(this.callback, {
264 parameters: param_form,
266 onSuccess: function (req) {
268 /// Display the result of the update
269 /// or add , the name of the row in the table has the
270 /// if p_ctl_row does not exist it means it is a new
271 /// row , otherwise an update
272 var answer=here.parseXML(req);
275 if (answer ['status'] == 'OK') {
276 if ($(answer['ctl_row']) ) {
277 new_row=$(answer['ctl_row']);
278 $(answer['ctl_row']).update(answer['html']);
279 new_row.setAttribute("ctl_pk_id",answer['ctl_pk_id']);
281 new_row = new Element("tr");
282 new_row.id = answer['ctl_row'];
283 new_row.innerHTML = answer['html'];
284 new_row.setAttribute("ctl_pk_id",answer['ctl_pk_id']);
286 * put the element at the right place
288 here.insertRow($("tb"+answer['ctl']) , new_row,here.sort_column);
290 new Effect.Highlight(answer['ctl_row'] ,{startcolor: '#FAD4D4',endcolor: '#F78082' });
291 alternate_row_color("tb"+answer['ctl']);
292 remove_waiting_box();
293 $(here.control).hide();
294 // if there is an afterSaveFct then call it
295 if (here.afterSaveFct != undefined && typeof here.afterSaveFct == "function") {
297 here.afterSaveFct.call(here,new_row,req);
299 console.error("FAIL253 afterSaveFct ");
300 console.error(e.message);
301 console.error (here.afterSaveFct);
306 remove_waiting_box();
307 smoke.alert(content[48]);
308 $(here.control).update(answer['html']);
323 *@brief call the ajax with action delete
324 *@param id (pk) of the data row
326 this.remove = function (p_id, p_ctl) {
327 this.param['p_id'] = p_id;
328 this.param['action'] = 'delete';
329 this.param['ctl'] = p_ctl;
331 $(p_ctl+"_"+p_id).addClassName("highlight");
332 smoke.confirm(content[47],
336 new Ajax.Request(here.callback, {
337 parameters: here.param,
339 onSuccess: function (req) {
340 var answer = here.parseXML(req);
341 if (answer['status'] == 'OK') {
342 var x=answer['ctl_row'];
344 alternate_row_color("tb"+answer['ctl']);
346 smoke.alert(answer['html']);
352 $(p_ctl+"_"+p_id).removeClassName("highlight");
358 *@brief display a dialog box with the information
360 *@param id (pk) of the data row
361 *@param ctl name of the object
363 this.input = function (p_id, p_ctl) {
365 this.param['p_id'] = p_id;
366 this.param['action'] = 'input';
367 this.param['ctl'] = p_ctl;
368 var control = this.control;
371 // display the form to enter data
372 new Ajax.Request(this.callback, {
373 parameters: this.param,
375 onSuccess: function (req) {
378 var x = here.parseXML(req);
379 var obj = {id: control, "cssclass": here.cssclass, "html": loading()};
383 here.mt_style["top"]=pos+"px";
384 $(obj.id).setStyle(here.mt_style);
385 remove_waiting_box();
386 $(obj.id).update(x['html']);
387 Effect.SlideDown(obj.id,{duration:0.3,scaleX:false,scaleY:true,scaleContent:false});
389 smoke.alert(content[48] + e.message);