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 *@class ManageTable 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
26 *@param {string} p_table_name the data table on which we're working
27 in javascript , to create an object to manipulate the table
32 // Version will manage the table "version"
33 var version=new ManageTable("version");
35 // the file ajax_my.php will be called
36 version.set_callback("ajax_my.php");
38 // Add supplemental parameter to this file
39 version.param_add ({"plugin_code":"OIC"};
41 // Set the id of dialog box , table and tr prefix
42 version.set_control("dialbox1");
47 The answer from ajax must be like this template
50 <ctl> id of the control to update for diag.box, row </ctl>
51 <status> OK , FAIL ...</status>
52 <html> Html to display</html>
57 - set_control(p_ctl_name)
58 - set_callback (p_new_callback)
59 - param_add (json object)
60 - parseXML (private function)
65 How to call a function AFTER save ?
66 You set a function afterSaveFct like in the example, it will be trigger after you submit the FORM
67 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
70 document_attach_obj.afterSaveFct=function(e) {
71 var ctl_pk_id=e.getAttribute("ctl_pk_id");
72 var formData = new FormData();
73 var file_data=document.getElementById('da_file_name');
74 formData.append('da_file_name', file_data.files[0]);
75 var xhr = new XMLHttpRequest();
76 xhr.open("POST", "ajax.php?p_id="+ctl_pk_id+"&do=upload_document", true);
81 As a hidden parameter the Manage_Table:object_name must be set
85 // the object_name is tbl6030ee4ee519e , in the table each row (TR) has an attribute ctl_pk_id which is the id (primary key)
86 // <tr ctl_pk_id="" ...> </tr>
87 tbl6030ee4ee519e.afterSaveFct=function(p_param) {
90 console.log(p_param.getAttribute(ctl_pk_id))
95var ManageTable = function (p_table_name)
97 this.callback = "ajax.php"; //!< File to call
98 this.control = "dtr"; //<! Prefix Id of dialog box, table, row
101 this.afterSaveFct=undefined; // function to call after "save"
102 this.cssclass="inner_box";
103 this.param = {"table": p_table_name, "ctl_id": this.control}; //<! default value to pass
104 this.set_style=function(p_json) {
105 this.mt_style=p_json;
109 * @param {string} p_column column number start from 0
110 * @param {string} p_type type of sort (string, numeric)
111 * @returns {ManageTable.set_sort}
113 this.set_sort = function (p_column) {
115 this.sort_column=p_column;
118 this.set_dialog_box=function (p_dialog_box){
119 this.control=p_dialog_box;
122 * Insert the row a the right location
123 * @param {type} p_element_row DOMElement TR
126 this.insertRow=function(p_table,p_element_row,sort_column) {
129 //compute the length of row
130 //if rows == 0 or the sort is not defined then append
131 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 ) {
132 var row=p_table.insertRow(p_table.rows.length);
133 row.innerHTML=p_element_row.innerHTML;
134 row.id=p_element_row.id;
135 row.ctl_pk_id=row.id;
138 // loop for each row , compare the innerHTML of the column with the
139 // value if less than insert before
141 for (i = 1;i<p_table.rows.length;i++) {
142 var table_value=p_table.rows[i].cells[sort_column].getAttribute('sort_value') ;
143 var element_value=p_element_row.cells[sort_column].getAttribute('sort_value');
145 // if both are numeric so we force a numeric comparison
146 if ( ! isNaN(parseFloat(table_value)) && ! isNaN(parseFloat(element_value)) ) {
147 table_value=parseFloat(table_value);
148 element_value=parseFloat(element_value);
151 if (table_value > element_value) {
153 var row=p_table.insertRow(i);
154 row.innerHTML=p_element_row.innerHTML;
155 row.id=p_element_row.id;
156 row.ctl_pk_id=row.id;
160 p_table.appendChild(p_element_row);
162 console.log("insertRow failed with "+e.message);
169 *@fn ManageTable.set_control
170 *@brief Set the id of the control name , used as
171 * prefix for dialog box , table id and row
172 *@param string p_ctl_name id of dialog box
174 this.set_control = function (p_ctl_name) {
175 this.control = p_ctl_name;
178 *@brief set the name of the callback file to
179 * call by default it is ajax.php
181 this.set_callback = function (p_new_callback) {
182 this.callback = p_new_callback;
185 *@brief By default send the json param variable
186 * you can add a json object to it in order to
187 * send it to the callback function
189 this.param_add = function (p_obj) {
191 for (var key in this.param) {
192 result[key] = this.param[key];
194 for (var key in p_obj) {
195 result[key] = p_obj[key];
201 @brief receive answer from ajax and fill up the
202 private object "answer"
203 @param req Ajax answer
205 this.parseXML = function (req) {
207 if (req.responseText==='NOCONX') { reconnect();throw new Error("NOCONX") ;}
208 var xml = req.responseXML;
209 var status = xml.getElementsByTagName("status");
210 var ctl = xml.getElementsByTagName("ctl");
211 var html = xml.getElementsByTagName("html");
212 var ctl_row = xml.getElementsByTagName("ctl_row");
213 var ctl_pk_id=xml.getElementsByTagName("ctl_pk_id");
214 if (status.length == 0 || ctl.length == 0 || html.length == 0)
216 throw content[53] + req.responseText;
220 answer['status'] = getNodeText(status[0]);
221 answer['ctl'] = getNodeText(ctl[0]);
222 answer['ctl_row'] = getNodeText(ctl_row[0]);
223 answer['html'] = getNodeText(html[0]);
224 answer['ctl_pk_id'] = getNodeText(ctl_pk_id[0]);
227 console.error("managetable:parseXML")
233 *Call the ajax with the action save , it is possible to call a function after the save by setting
234 * a function to afterSaveFct.As a hidden parameter the Manage_Table:object_name must be set
235 * @param form_id string id of the FORM format ("frm"+object_name+"_"+p_id)
238 tbl6030f6f8c336a.afterSaveFct=function() {
239 // if p_id == -1 then we are adding
240 if ( this.param.p_id != -1 ) { return;}
242 var id=this.new_row.id.replace('tbl6030f6f8c336a_','');
243 // recall input ManageTable.input
244 this.input(id,'tbl6030f6f8c336a');
247 <caption>when I introduce a new element I need to reopen it to complete the missing information. (this) contains
251 * *Call the ajax with the action save , it is possible to call a function after the save by creating
252 * + * a function named afterSaveFct(r) where r is the row in HTML to display, with a attribute ctl_pk_id which is the
253 * + * primary key and id of the row
255 * + * As a hidden parameter the Manage_Table:object_name must be set
258 this.save = function (form_id) {
262 this.param['action'] = 'save';
263 var form = $(form_id).serialize(true);
264 param_form = json_concat(this.param,form);
270 new Ajax.Request(this.callback, {
271 parameters: param_form,
273 onSuccess: function (req) {
275 /// Display the result of the update
276 /// or add , the name of the row in the table has the
277 /// if p_ctl_row does not exist it means it is a new
278 /// row , otherwise an update
279 var answer=here.parseXML(req);
282 if (answer ['status'] == 'OK') {
283 if ($(answer['ctl_row']) ) {
284 new_row=$(answer['ctl_row']);
285 $(answer['ctl_row']).update(answer['html']);
286 new_row.setAttribute("ctl_pk_id",answer['ctl_pk_id']);
288 new_row = new Element("tr");
289 new_row.id = answer['ctl_row'];
290 new_row.innerHTML = answer['html'];
291 new_row.setAttribute("ctl_pk_id",answer['ctl_pk_id']);
293 * put the element at the right place
295 here.insertRow($("tb"+answer['ctl']) , new_row,here.sort_column);
297 new Effect.Highlight(answer['ctl_row'] ,{startcolor: '#FAD4D4',endcolor: '#F78082' });
298 alternate_row_color("tb"+answer['ctl']);
299 remove_waiting_box();
300 $(here.control).hide();
301 // if there is an afterSaveFct then call it
302 if (here.afterSaveFct != undefined && typeof here.afterSaveFct == "function") {
304 here.afterSaveFct.call(here,new_row,req);
306 console.error("FAIL253 afterSaveFct ");
307 console.error(e.message);
308 console.error (here.afterSaveFct);
313 remove_waiting_box();
314 smoke.alert(content[48]);
315 $(here.control).update(answer['html']);
330 *@brief call the ajax with action delete
331 *@param id (pk) of the data row
333 this.remove = function (p_id, p_ctl) {
334 this.param['p_id'] = p_id;
335 this.param['action'] = 'delete';
336 this.param['ctl'] = p_ctl;
338 $(p_ctl+"_"+p_id).addClassName("highlight");
339 smoke.confirm(content[47],
343 new Ajax.Request(here.callback, {
344 parameters: here.param,
346 onSuccess: function (req) {
347 var answer = here.parseXML(req);
348 if (answer['status'] == 'OK') {
349 var x=answer['ctl_row'];
351 alternate_row_color("tb"+answer['ctl']);
353 smoke.alert(answer['html']);
359 $(p_ctl+"_"+p_id).removeClassName("highlight");
365 *@brief display a dialog box with the information
367 *@param id (pk) of the data row
368 *@param ctl name of the object
370 this.input = function (p_id, p_ctl) {
372 this.param['p_id'] = p_id;
373 this.param['action'] = 'input';
374 this.param['ctl'] = p_ctl;
375 var control = this.control;
378 // display the form to enter data
379 new Ajax.Request(this.callback, {
380 parameters: this.param,
382 onSuccess: function (req) {
385 var x = here.parseXML(req);
386 var obj = {id: control, "cssclass": here.cssclass, "html": loading()};
390 here.mt_style["top"]=pos+"px";
391 $(obj.id).setStyle(here.mt_style);
392 remove_waiting_box();
393 $(obj.id).update(x['html']);
394 Effect.SlideDown(obj.id,{duration:0.3,scaleX:false,scaleY:true,scaleContent:false});
396 smoke.alert(content[48] + e.message);