noalyss Version-9
managetable.js
Go to the documentation of this file.
1/*
2 * This file is part of NOALYSS.
3 *
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.
8 *
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.
13 *
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
17 */
18// Copyright Author Dany De Bontridder danydb@aevalys.eu
19
20/**
21 *@class ManageTable Javascript object to manage ajax calls to
22 * save , input or delete data row.
23 * The callback must
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
28 version.
29
30 Example of code
31 @code
32 // Version will manage the table "version"
33 var version=new ManageTable("version");
34
35 // the file ajax_my.php will be called
36 version.set_callback("ajax_my.php");
37
38 // Add supplemental parameter to this file
39 version.param_add ({"plugin_code":"OIC"};
40
41 // Set the id of dialog box , table and tr prefix
42 version.set_control("dialbox1");
43
44 @endcode
45
46
47 The answer from ajax must be like this template
48 @verbatim
49 <xml>
50 <ctl> id of the control to update for diag.box, row </ctl>
51 <status> OK , FAIL ...</status>
52 <html> Html to display</html>
53 </xml>
54 @endverbatim
55
56 List of function
57 - set_control(p_ctl_name)
58 - set_callback (p_new_callback)
59 - param_add (json object)
60 - parseXML (private function)
61 - save
62 - delete
63 - input
64
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
68 Example :
69 @code
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);
77 xhr.send(formData);
78}
79 </script>
80 @endcode
81 As a hidden parameter the Manage_Table:object_name must be set
82
83 Example :
84 @code
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) {
88 console.log(p_param);
89 console.log(this)
90 console.log(p_param.getAttribute(ctl_pk_id))
91}
92 @endcode
93
94 */
95var ManageTable = function (p_table_name)
96{
97 this.callback = "ajax.php"; //!< File to call
98 this.control = "dtr"; //<! Prefix Id of dialog box, table, row
99 this.mt_style={};
100 this.sort_column=0;
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;
106 };
107 /**
108 * Set the sort ,
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}
112 */
113 this.set_sort = function (p_column) {
114
115 this.sort_column=p_column;
116 };
117
118 this.set_dialog_box=function (p_dialog_box){
119 this.control=p_dialog_box;
120 }
121 /**
122 * Insert the row a the right location
123 * @param {type} p_element_row DOMElement TR
124 * @returns nothing
125 */
126 this.insertRow=function(p_table,p_element_row,sort_column) {
127 try {
128 // use the table
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;
136 return;
137 }
138 // loop for each row , compare the innerHTML of the column with the
139 // value if less than insert before
140 var i = 0;
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');
144
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);
149 }
150
151 if (table_value > element_value) {
152
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;
157 return;
158 }
159 }
160 p_table.appendChild(p_element_row);
161 } catch(e) {
162 console.log("insertRow failed with "+e.message);
163 throw e;
164 }
165
166 };
167 var answer = {};
168 /**
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
173 */
174 this.set_control = function (p_ctl_name) {
175 this.control = p_ctl_name;
176 };
177 /**
178 *@brief set the name of the callback file to
179 * call by default it is ajax.php
180 */
181 this.set_callback = function (p_new_callback) {
182 this.callback = p_new_callback;
183 };
184 /**
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
188 */
189 this.param_add = function (p_obj) {
190 var result = {};
191 for (var key in this.param) {
192 result[key] = this.param[key];
193 }
194 for (var key in p_obj) {
195 result[key] = p_obj[key];
196 }
197 this.param = result;
198 return this.param;
199 };
200 /**
201 @brief receive answer from ajax and fill up the
202 private object "answer"
203 @param req Ajax answer
204 */
205 this.parseXML = function (req) {
206 try {
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)
215 {
216 throw content[53] + req.responseText;
217
218 }
219 var answer=[];
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]);
225 return answer;
226 } catch (e) {
227 console.error("managetable:parseXML")
228 throw e;
229 }
230 };
231
232 /**
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)
236 *
237 *@example
238 tbl6030f6f8c336a.afterSaveFct=function() {
239 // if p_id == -1 then we are adding
240 if ( this.param.p_id != -1 ) { return;}
241 // retrive the id
242 var id=this.new_row.id.replace('tbl6030f6f8c336a_','');
243 // recall input ManageTable.input
244 this.input(id,'tbl6030f6f8c336a');
245
246}
247 <caption>when I introduce a new element I need to reopen it to complete the missing information. (this) contains
248 the current object
249 </caption>
250 *
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
254 * + *
255 * + * As a hidden parameter the Manage_Table:object_name must be set
256 *
257 */
258 this.save = function (form_id) {
259 var param_form={};
260 waiting_box();
261 try {
262 this.param['action'] = 'save';
263 var form = $(form_id).serialize(true);
264 param_form = json_concat(this.param,form);
265 var here=this;
266 } catch (e) {
267 alert(e.message);
268 return false;
269 }
270 new Ajax.Request(this.callback, {
271 parameters: param_form,
272 method: "post",
273 onSuccess: function (req) {
274 try {
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);
280 var new_row;
281
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']);
287 } else {
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']);
292 /**
293 * put the element at the right place
294 */
295 here.insertRow($("tb"+answer['ctl']) , new_row,here.sort_column);
296 }
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") {
303 try {
304 here.afterSaveFct.call(here,new_row,req);
305 } catch (e) {
306 console.error("FAIL253 afterSaveFct ");
307 console.error(e.message);
308 console.error (here.afterSaveFct);
309 }
310 }
311
312 } else {
313 remove_waiting_box();
314 smoke.alert(content[48]);
315 $(here.control).update(answer['html']);
316
317 }
318 }
319 catch (e) {
320 alert(e.message);
321 return false;
322 }
323 }
324
325
326 });
327 return false;
328 };
329 /**
330 *@brief call the ajax with action delete
331 *@param id (pk) of the data row
332 */
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;
337 var here=this;
338 $(p_ctl+"_"+p_id).addClassName("highlight");
339 smoke.confirm(content[47],
340 function (e)
341 {
342 if (e ) {
343 new Ajax.Request(here.callback, {
344 parameters: here.param,
345 method: "get",
346 onSuccess: function (req) {
347 var answer = here.parseXML(req);
348 if (answer['status'] == 'OK') {
349 var x=answer['ctl_row'];
350 $(x).remove();
351 alternate_row_color("tb"+answer['ctl']);
352 }else {
353 smoke.alert(answer['html']);
354 }
355 }
356 });
357 }
358 else {
359 $(p_ctl+"_"+p_id).removeClassName("highlight");
360 }
361 }) ;
362
363 };
364 /**
365 *@brief display a dialog box with the information
366 * of the data row
367 *@param id (pk) of the data row
368 *@param ctl name of the object
369 */
370 this.input = function (p_id, p_ctl) {
371 waiting_box();
372 this.param['p_id'] = p_id;
373 this.param['action'] = 'input';
374 this.param['ctl'] = p_ctl;
375 var control = this.control;
376 var here = this;
377
378 // display the form to enter data
379 new Ajax.Request(this.callback, {
380 parameters: this.param,
381 method: "get",
382 onSuccess: function (req) {
383
384 try {
385 var x = here.parseXML(req);
386 var obj = {id: control, "cssclass": here.cssclass, "html": loading()};
387 create_div(obj);
388 var pos = calcy(50);
389
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});
395 } catch (e) {
396 smoke.alert(content[48] + e.message);
397 }
398
399 }
400 });
401 };
402
403
404}
405