jqgrid_utils 1.27.2 → 1.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1755 @@
1
+ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Jqgrid_utils = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2
+ /**
3
+ * A module for Jqgrid_utils
4
+ * @module Jqgrid_utils
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ module.exports = class Vanilla_website_utils
10
+ {
11
+
12
+ constructor(settings=false)
13
+ {
14
+ if(settings)
15
+ {
16
+ if(settings.hasOwnProperty('page'))
17
+ {
18
+ this.page = settings['page'];
19
+ localStorage.setItem('page', this.page);
20
+ }
21
+ }
22
+
23
+ }
24
+
25
+ /**
26
+ * Takes the updated columns data and send it to your API post server
27
+ * loadComplete: async function() for the old record needs to be called, see example !
28
+ @alias module:Jqgrid_utils
29
+ @param {object} - Grid Object (required)
30
+ @param {string} - API URL like https://foo.com (required)
31
+ @param {array} - id list, ids from the column header (required)
32
+ @param {object} - settings - extra key:value to send to your server
33
+
34
+ @example
35
+ var jqu = new Jqgrid_utils({page:page});
36
+
37
+ ....},
38
+ loadComplete: async function()
39
+ {
40
+ $grid.jqGrid('setGridParam',{record_data:$grid.jqGrid("getGridParam").data});
41
+ },
42
+ afterSetRow: async function(row)
43
+ {
44
+ const _api = await get_api_url('sapir');
45
+ const api = _api + '/column2';
46
+ let info = {"msg":"failed"};
47
+ var jqu = new Jqgrid_utils();
48
+ info = await jqu.update_row_to_api($grid, api,['id'],row,{server : '232',db : 'sl_h',table : 'kpi',});
49
+ for(let i in info)
50
+ {
51
+ log.info(info[i]);
52
+ }
53
+ },
54
+ */
55
+
56
+
57
+ async update_row_to_api(_self, api='',_ids=['id'],row={}, data)
58
+ {
59
+ let self = this;
60
+ let infos = [];
61
+ let ids = {};
62
+ let values = {};
63
+ let changed = {};
64
+ const rd = _self.jqGrid("getGridParam", "record_data");
65
+
66
+ if(api != '' && Object.keys(row).length > 0 && row.inputData.oper == 'edit')
67
+ {
68
+ for(let i in rd)
69
+ {
70
+ if (rd[i]['id'] === row.rowid)
71
+ {
72
+
73
+ for(let ii in _ids)
74
+ {
75
+ if(rd[i].hasOwnProperty(_ids[ii]))
76
+ {
77
+ ids[_ids[ii]] = rd[i][_ids[ii]];
78
+ }
79
+ }
80
+
81
+ for(let iii in row.inputData)
82
+ {
83
+ if(iii != 'oper')
84
+ {
85
+ if(Object.keys(ids).indexOf(iii) < 0)
86
+ {
87
+ //console.log(iii);
88
+ //console.log(row.inputData[iii]);
89
+ //console.log(rd[i][iii]);
90
+ if(row.inputData[iii] != rd[i][iii])
91
+ {
92
+ changed[iii] = row.inputData[iii];
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
98
+ }
99
+
100
+
101
+ for(let i in changed)
102
+ {
103
+ if( Object.keys(ids).indexOf(i) < 0 && i!= 'oper')
104
+ {
105
+ const col_name = i;
106
+ let col_value = {};
107
+ col_value[col_name] = changed[i];
108
+ data['ids'] = ids;
109
+ data['values'] = col_value;
110
+ data['operator'] = 'edit';
111
+ //console.log(data)
112
+ const info = await self.post_json(api ,JSON.stringify(data));
113
+ infos.push(info);
114
+ }
115
+ }
116
+
117
+
118
+
119
+
120
+ }
121
+ else if(api != '' && Object.keys(row).length > 0 && row.inputData.oper == 'add')
122
+ {
123
+ console.log("...add");
124
+ for(let i in row.inputData)
125
+ {
126
+ if(row.inputData[i] && i != 'id' && i != 'oper')
127
+ {
128
+ values[i] = row.inputData[i];
129
+ }
130
+ }
131
+ data['ids'] = ids;
132
+ data['values'] = values;
133
+ data['operator'] = 'add';
134
+ //console.log(data);
135
+ let info = await self.post_json(api ,JSON.stringify(data));
136
+ infos.push(info);
137
+
138
+ }
139
+
140
+ return infos;
141
+ }
142
+
143
+
144
+ /**
145
+ * After Delete a Grid Row send to and DELETE REST Request
146
+ * You need to define loadComplete and afterDelRow
147
+ * The Grid data needs to be saved as record within loadComplete
148
+ @alias module:Jqgrid_utils
149
+ @param {object} - Grid Object (required)
150
+ @param {string} - API URL like https://foo.com (required)
151
+ @param {string} - the row id value from afterDelRow (required)
152
+ @param {array} - id list, ids from the column header colmodel (required)
153
+ @param {object} - settings - extra key:value to send to your server
154
+ @example
155
+
156
+ loadComplete: async function()
157
+ {
158
+ $grid.jqGrid('setGridParam',{record_data:$grid.jqGrid("getGridParam").data});
159
+ },
160
+ afterDelRow: async function(rowid)
161
+ {
162
+ const _api = await get_api_url('sapir');
163
+ const api = _api + '/column2';
164
+ let info = {"msg":"failed"};
165
+ var jqu = new Jqgrid_utils();
166
+
167
+ info = await jqu.delete_row_to_api($grid,api,rowid,['id'],{
168
+ server : '232',
169
+ db : 'sl_h',
170
+ table : 'kpi',
171
+ operator: 'delete',
172
+ });
173
+ log.info(JSON.parse(info).msg);
174
+ },
175
+
176
+
177
+
178
+
179
+
180
+ */
181
+
182
+ async delete_row_to_api(_self, api='', rowid, _ids=[], data={})
183
+ {
184
+ let info = {"msg":"failed"};
185
+ let self = this;
186
+ let ids = [];
187
+ let values = {};
188
+ const rd = _self.jqGrid("getGridParam", "record_data");
189
+ for(let i in rd)
190
+ {
191
+ if (rd[i]['id'] === rowid)
192
+ {
193
+ for(let ii in _ids)
194
+ {
195
+ if(rd[i].hasOwnProperty(_ids[ii]))
196
+ {
197
+ values[_ids[ii]] = rd[i][_ids[ii]];
198
+ ids.push(_ids[ii]);
199
+ }
200
+ }
201
+ break;
202
+ }
203
+ }
204
+
205
+
206
+
207
+ if(api != '' && Object.keys(values).length == ids.length )
208
+ {
209
+ data['ids'] = ids;
210
+ data['values'] = values;
211
+ //console.log(data);
212
+ info = await self.adelete_api(api,JSON.stringify(data));
213
+ }
214
+ return info;
215
+ }
216
+
217
+
218
+
219
+
220
+ /**
221
+ *Append and sperator based link column to the end of a row
222
+ @alias module:Jqgrid_utils
223
+ @param {object} - col_model of the grid
224
+ @param {string} - URL string
225
+ @param {string} - field value - the visible text of the anchor
226
+ @param {object} - base row key value - like {"name":'wiki','label':"Wiki"}
227
+ @param {string} - URL Attributes
228
+ @param {object} - keys and fields value to use
229
+ @example
230
+ var jqu = new Jqgrid_utils();
231
+ col_model = await jqu.append_seperator_link_column(col_model, 'http://wiki.foo.com/index.php' ,'Wiki',{"name":'wiki','label':"Wiki","width":"65px","align":"center"} ,'target="_blank"',{"report_central":"name"});
232
+
233
+ */
234
+
235
+ async append_seperator_link_column(col_model, url, field_value, base, attr = '', keys)
236
+ {
237
+ url = url + '/';
238
+ let self = this;
239
+ base['formatter'] = function(cell_val, obj)
240
+ {
241
+ let _cell_val = field_value;
242
+ if (typeof keys === 'object')
243
+ {
244
+ let pref = '';
245
+ for (let i in keys)
246
+ {
247
+
248
+ let key = i;
249
+ let v = keys[i];
250
+ let key_val = obj.rowData[v];
251
+ if (key_val)
252
+ {
253
+ if (key_val)
254
+ {
255
+ if(key != '')
256
+ {
257
+ pref += key + '' + '/' + encodeURIComponent(key_val) + '/';
258
+ }
259
+ else
260
+ {
261
+ pref += encodeURIComponent(key_val);
262
+ }
263
+ }
264
+ }
265
+ }
266
+ if (pref)
267
+ {
268
+ if (pref.slice(-1) === '&' || pref.slice(-1) === '/' )
269
+ {
270
+ pref = pref.slice(0, -1);
271
+ }
272
+ cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + _cell_val + '</a>';
273
+ }
274
+
275
+ }
276
+
277
+ return cell_val;
278
+ }
279
+
280
+ col_model.push(base);
281
+
282
+ return col_model;
283
+ }
284
+
285
+
286
+ /**
287
+ * add textarea
288
+ @alias module:Jqgrid_utils
289
+ @param {object} - edittype like
290
+ @example
291
+ let col_model = JSON.parse(await aget_api(url + "/model"));
292
+ col_model = await jqu.add_edit(col_model, 'mon',{ edittype:'textarea', editoptions:{rows:6,cols:100} });
293
+ see for other inputfields:
294
+ http://www.trirand.com/blog/phpjqgrid/doc/_2v80w6oam.htm
295
+ */
296
+ async add_edit(col_model, edit_field, edittype, editoptions)
297
+ {
298
+ for (let i = 0; i < col_model.length; i++)
299
+ {
300
+ if (col_model[i]['name'] === edit_field)
301
+ {
302
+ Object.assign(col_model[i], edittype );
303
+ Object.assign(col_model[i], editoptions );
304
+ }
305
+ }
306
+ return col_model;
307
+ }
308
+
309
+
310
+ /**
311
+ * add textarea
312
+ @alias module:Jqgrid_utils
313
+ @param {string} - edit_filed
314
+ @param {string} - style of the textaread
315
+ @example
316
+ let col_model = JSON.parse(await aget_api(url + "/model"));
317
+ col_model = await jqu.add_textarea(col_model, 'worker','style="width:100%;height:100px"');
318
+ */
319
+ async add_textarea(col_model, edit_field, style = 'style="width:100%;height:100px"')
320
+ {
321
+ for (let i = 0; i < col_model.length; i++)
322
+ {
323
+ if (col_model[i]['name'] === edit_field)
324
+ {
325
+ col_model[i]['formatter'] = function(cell_val)
326
+ {
327
+ const txt = '<textarea '+ style +'>' + cell_val + '</textarea>' ;
328
+ return txt;
329
+ };
330
+ }
331
+ }
332
+ return col_model;
333
+ }
334
+
335
+
336
+
337
+ /**
338
+ * Get basic colModel data from raw data
339
+ @alias module:Jqgrid_utils
340
+ @param {array} - grid object
341
+ @param {array} - raw data object from loadComplete
342
+ @param {array} - list of columns to exclude (optional)
343
+ @param {array} - existing colModel (optional)
344
+ @returns {array} - col_model
345
+ @example
346
+ var jqu = new Jqgrid_utils();
347
+ ,loadComplete: async function(data)
348
+ {
349
+ let col_model = jQuery(this).jqGrid('getGridParam',"colModel");
350
+ const new_col_model= await update_col_model(this, data, ['id','cust_qty','waiting_supplier_orders','waiting_assemblies','pending_components','pending_customer_order',col_model);
351
+ jQuery(this).jqGrid('setGridParam',{colModel:new_col_model});
352
+ },
353
+ */
354
+ async get_col_model_from_data(obj, data, exclude=[],col_model=[])
355
+ {
356
+ let cols = [];
357
+ for(let i in data)
358
+ {
359
+ const keys = Object.keys(data[i]);
360
+ for(let ii in keys)
361
+ {
362
+ const key = keys[ii];
363
+ cols.push(key);
364
+ }
365
+ }
366
+ cols = cols.filter((item, pos) => cols.indexOf(item) === pos);
367
+ let mcols = [];
368
+ for(let i in col_model)
369
+ {
370
+ mcols.push(col_model[i]['name']);
371
+ }
372
+
373
+
374
+ let diff = cols.filter(x => !mcols.includes(x));
375
+ const _exclude = new Set(exclude);
376
+ diff = diff.filter((name) => {return !_exclude.has(name); });
377
+ diff.sort();
378
+
379
+ for(let x=0; x < diff.length; x++)
380
+ {
381
+ col_model.push({'name': diff[x],'label': diff[x]});
382
+ }
383
+ //console.log(col_model);
384
+ return col_model;
385
+ }
386
+
387
+ /**
388
+ * Replace a Binaery 0 or 1 to other given value
389
+ @alias module:Jqgrid_utils
390
+ @param {string} - cell value
391
+ @param {string} - string replacement for 0
392
+ @param {string} - string replacement for 1
393
+ @example
394
+ var jqu = new Jqgrid_utils();
395
+ let _data = jqu.binery_replace(0,'zero','one');
396
+ or for column formatter
397
+ download_formatter:"var jqu = new Jqgrid_utils();jqu.binary_replace({0},'zero','one')"});
398
+ */
399
+ binary_replace(cell_value, a='zero',b='one')
400
+ {
401
+ let value = a;
402
+ if(cell_value == 1 || cell_value == 0 )
403
+ {
404
+ if(cell_value == 1)
405
+ {
406
+ value = b;
407
+ }
408
+ }
409
+ return value;
410
+ }
411
+
412
+
413
+ /**
414
+ * Convert a 112 date string to a DMY format with sepertaor - sync function
415
+ @alias module:Jqgrid_utils
416
+ @param {string} - date string
417
+ @param {string} - seperator used
418
+ @example
419
+ var jqu = new Jqgrid_utils();
420
+ let _data = jqu._date112_to_DMY('20220104','/');
421
+ console.log(_data);
422
+ */
423
+ _date112_to_DMY(cell_value, seperator='/')
424
+ {
425
+ let value = cell_value;
426
+ if(cell_value.length >= 8 && cell_value.indexOf(seperator) === -1)
427
+ {
428
+ let a = [];
429
+ a.push(cell_value.substr(6, 2));
430
+ a.push(cell_value.substr(4, 2));
431
+ a.push(cell_value.substr(0, 4));
432
+ value = a.join(seperator);
433
+ }
434
+ return value;
435
+
436
+ }
437
+
438
+ /**
439
+ * Convert a 112 date to a DMY format with sepertaor
440
+ @alias module:Jqgrid_utils
441
+ @param {object} - col_model of the grid
442
+ @param {string} - name of the date 112 column what should get converted
443
+ @param {string} - seperator used
444
+ @example
445
+ var jqu = new Jqgrid_utils();
446
+ let _data = await jqu.date112_to_DMY(this,'field','/');
447
+ console.log(_data);
448
+ */
449
+ async date112_to_DMY(col_model, edit_field , seperator='/')
450
+ {
451
+ for(let i=0;i< col_model.length;i++)
452
+ {
453
+ if(col_model[i]['name'] === edit_field)
454
+ {
455
+ col_model[i]['formatter'] = function (cell_value, o)
456
+ {
457
+ if(cell_value)
458
+ {
459
+ cell_value = cell_value.toString();
460
+ let value = cell_value;
461
+ if(cell_value.length >= 8 && cell_value.indexOf(seperator) === -1)
462
+ {
463
+ let a = [];
464
+ a.push(cell_value.substr(6, 2));
465
+ a.push(cell_value.substr(4, 2));
466
+ a.push(cell_value.substr(0, 4));
467
+ value = a.join(seperator);
468
+ }
469
+ return value;
470
+ }
471
+ else
472
+ {
473
+ return cell_value;
474
+ }
475
+ };
476
+ }
477
+ }
478
+ return col_model;
479
+ }
480
+
481
+ /**
482
+ * Add Formatter
483
+ @alias module:Jqgrid_utils
484
+ @param {array} - grid col_model
485
+ @param {string} - string columns names what will be formatted
486
+ @param {object} - formatter object like { formatter: "select", formatoptions: {value: "1:ok;0:fail", defaultValue: "1" }}
487
+ @example
488
+ var jqu = new Jqgrid_utils();
489
+ col_model = await jqu.add_formatter(col_model,'select',{ formatter: "select", formatoptions: {value: "1:ok;0:fail", defaultValue: "1" }})
490
+ */
491
+
492
+ async add_formatter(col_model,edit_field, formatter)
493
+ {
494
+ for(let i=0;i< col_model.length;i++)
495
+ {
496
+ if(col_model[i]['name'] === edit_field)
497
+ {
498
+ if(formatter.hasOwnProperty('formatter') && formatter.hasOwnProperty('formatoptions'))
499
+ {
500
+ col_model[i]['formatter'] = formatter['formatter'];
501
+ col_model[i]['formatoptions'] = formatter['formatoptions'];
502
+ col_model[i]['edittype'] = formatter['formatter'];
503
+ col_model[i]['editoptions'] = formatter['formatoptions'];
504
+ }
505
+ }
506
+ }
507
+ return col_model;
508
+ }
509
+
510
+
511
+ /**
512
+ * Add HTML Formatter
513
+ @alias module:Jqgrid_utils
514
+ @param {array} - grid col_model
515
+ @param {string} - string columns names what will be converted to ok buttons
516
+ @param {string} - html tag code
517
+ @returns {array} - col_model
518
+ @example
519
+ var jqu = new Jqgrid_utils();
520
+ col_model = await jqu.add_html_formatter(col_model,'process',"<button tabindex='0' class='cellbtn' type='button'>Process</button>");
521
+ */
522
+
523
+ async add_html_formatter(col_model, edit_field, html)
524
+ {
525
+ for(let i=0;i< col_model.length;i++)
526
+ {
527
+ if(col_model[i]['name'] === edit_field)
528
+ {
529
+ col_model[i]['formatter'] = function (cell_val,o)
530
+ {
531
+ return html;
532
+ };
533
+ }
534
+ }
535
+ return col_model;
536
+ }
537
+
538
+ /**
539
+ * Add an OK Button
540
+ @alias module:Jqgrid_utils
541
+ @param {array} - grid col_model
542
+ @param {array} - list of columns names what will be converted to ok buttons
543
+ @returns {array} - col_model
544
+ @example
545
+ var jqu = new Jqgrid_utils();
546
+ col_model = await jqu.add_ok_button(col_model, ['checked']);
547
+ */
548
+
549
+ async add_ok_button(col_model, fields)
550
+ {
551
+ let self = this;
552
+ for (let i = 0; i < col_model.length; i++)
553
+ {
554
+ if (fields.indexOf(col_model[i]['name']) > -1)
555
+ {
556
+ col_model[i]['formatter'] = function(cell_val)
557
+ {
558
+ if (cell_val != undefined)
559
+ {
560
+ return self.__cell_format(cell_val, 'format_ok');
561
+ }
562
+ else
563
+ {
564
+ return '';
565
+ }
566
+ };
567
+ }
568
+ }
569
+ return col_model;
570
+ }
571
+
572
+
573
+
574
+ /**
575
+ * Get the filled cell data
576
+ @alias module:Jqgrid_utils
577
+ @param {object} - the grid object or its name
578
+ @param {array} - list of columns names what will be collected
579
+ @returns {array} - table array
580
+ @example
581
+ var jqu = new Jqgrid_utils();
582
+ col_model = await jqu.set_link(col_model,'av0_code','url_code','target="blank"');
583
+ */
584
+ async get_filled_cell_table_data(_grid, fields=[])
585
+ {
586
+ let d = jQuery(_grid).jqGrid('getGridParam','data');
587
+ let keys = fields;
588
+ let _data = [];
589
+ for(let i in d)
590
+ {
591
+ if(d[i].hasOwnProperty('id'))
592
+ {
593
+ let row = [d[i]['id']];
594
+ for(let x in keys)
595
+ {
596
+ if(d[i].hasOwnProperty(keys[x]))
597
+ {
598
+ row.push(d[i][keys[x]]);
599
+ }
600
+ else
601
+ {
602
+ row.push("");
603
+ }
604
+ }
605
+ var f = row.filter(function(value, index, arr){ return value !== "";});
606
+ if(Object.keys(f).length > 1)
607
+ {
608
+ _data.push(row);
609
+ }
610
+ }
611
+ }
612
+ return _data;
613
+ }
614
+
615
+
616
+ /**
617
+ * Get the filled cell data
618
+ @alias module:Jqgrid_utils
619
+ @param {object} - the grid object or its name
620
+ @param {array} - list of columns names what will be collected
621
+ @returns {object} - json object of the colleted fields
622
+ @example
623
+ var jqu = new Jqgrid_utils();
624
+ col_model = await jqu.set_link(col_model,'av0_code','url_code','target="blank"');
625
+ */
626
+
627
+ async get_filled_cell_data(_grid, fields=[])
628
+ {
629
+ let d = jQuery(_grid).jqGrid('getGridParam','data');
630
+ let keys = fields;
631
+ let _data = [];
632
+ for(let i in d)
633
+ {
634
+ if(d[i].hasOwnProperty('id'))
635
+ {
636
+ let row = {'id':d[i]['id']};
637
+ for(let x in keys)
638
+ {
639
+ if(d[i].hasOwnProperty(keys[x]))
640
+ {
641
+ if(d[i][keys[x]] != "")
642
+ {
643
+ row[keys[x]] = d[i][keys[x]];
644
+ }
645
+ }
646
+ }
647
+ if(Object.keys(row).length > 1)
648
+ {
649
+ _data.push(row);
650
+ }
651
+ }
652
+ }
653
+ return _data;
654
+ }
655
+
656
+ /**
657
+ * Add an URL from the data to a specific cell/column
658
+ @alias module:Jqgrid_utils
659
+ @param {object} - col_model of the grid
660
+ @param {string} - name of the column what should get convert to the url
661
+ @param {string} - the used url of the data
662
+ @returns {object} https://foo.bar.com/av0_code/bar
663
+ @example
664
+ var jqu = new Jqgrid_utils();
665
+ let _data = await jqu.get_filled_cell_data(this,["P-","bulk","wholesale"]);
666
+ console.log(_data);
667
+ */
668
+ async set_link(col_model, edit_field, url ,attr='')
669
+ {
670
+ for(let i=0;i< col_model.length;i++)
671
+ {
672
+ if(col_model[i]['name'] === edit_field)
673
+ {
674
+ col_model[i]['formatter'] = function (cell_val,o)
675
+ {
676
+ return '<a class="gl" ' + attr + 'href="' + o.rowData[url] + '">'+ cell_val +'</a>';
677
+ };
678
+ }
679
+ }
680
+ return col_model;
681
+ }
682
+
683
+
684
+
685
+ /**
686
+ * Hide all columns execpt column
687
+ * @alias module:Jqgrid_utils
688
+ * @param {object} - col_model of the grid
689
+ * @param {array} - array of names to not to hide
690
+ * @returns {object} col_model
691
+ * @example
692
+ col_model = await jqu.hide_all_columns_except(col_model,['supplier','customer']);
693
+ */
694
+ async hide_all_columns_except(col_model,fields)
695
+ {
696
+ for(let i=0;i< col_model.length;i++)
697
+ {
698
+ if( fields.indexOf(col_model[i]['name']) > -1)
699
+ {
700
+ col_model[i]['hidden'] = false;
701
+ }
702
+ else
703
+ {
704
+ col_model[i]['hidden'] = true;
705
+ }
706
+ }
707
+ return col_model;
708
+ }
709
+
710
+ /**
711
+ * Hide a col_model column before load the grid
712
+ * @alias module:Jqgrid_utils
713
+ * @param {object} - col_model of the grid
714
+ * @param {string} - name of the column to hide
715
+ * @returns {object} col_model
716
+ * @example
717
+ col_model = await jqu.hide_column(col_model,'wholesale');
718
+ col_model = await jqu.hide_column(col_model,'wholesale_formula');
719
+ */
720
+ async hide_column(col_model,field)
721
+ {
722
+ for(let i=0;i< col_model.length;i++)
723
+ {
724
+ if(col_model[i]['name'] === field)
725
+ {
726
+ col_model[i]['hidden'] = true;
727
+ }
728
+ }
729
+ return col_model;
730
+ }
731
+
732
+
733
+ /**
734
+ @alias module:Jqgrid_utils
735
+ @param {object} - gridobject;
736
+ @param {object} - grid data (optional);
737
+ @example
738
+ var jqu = new Jqgrid_utils();
739
+ loadComplete: function(){
740
+ jqu.grid_set_caption(this);
741
+ },
742
+ */
743
+
744
+ s_grid_set_caption(_grid, data=[])
745
+ {
746
+ this.grid_set_captionn(_grid, data=[]);
747
+ }
748
+
749
+ /**
750
+ Adding the row count number to the caption
751
+ @alias module:Jqgrid_utils
752
+ @param {object} - gridobject;
753
+ @param {object} - grid data (optional);
754
+ @example
755
+ var jqu = new Jqgrid_utils();
756
+ loadComplete: function(){
757
+ await jqu.grid_set_caption(this);
758
+ },
759
+ */
760
+ async grid_set_caption(_grid, data=[])
761
+ {
762
+ if(_grid)
763
+ {
764
+ const grid = jQuery(_grid);
765
+ let count = 0;
766
+ if(data.length === 0)
767
+ {
768
+ count = grid.jqGrid('getGridParam', 'records');
769
+ }
770
+ else
771
+ {
772
+ count = data.length;
773
+ }
774
+ let caption = grid.jqGrid("getGridParam", "caption");
775
+ const reg = /\d.*x/;
776
+ const new_caption = caption.replace(reg, "");
777
+ grid.jqGrid('setCaption', new_caption + " " + count + 'x');
778
+ }
779
+ }
780
+
781
+ /**
782
+ @alias module:Jqgrid_utils
783
+ @param {object} - the col_model of the grid
784
+ @param {string} - the name of the page(optional)
785
+ @param {object} - the grid objec(optional)
786
+ @example
787
+ col_model = await jqu.resize_saved_cell_width(col_model);
788
+ */
789
+ s_resize_saved_cell_width(col_model, page=false, grid=false)
790
+ {
791
+ this.grid_set_caption(col_model, page, grid);
792
+ }
793
+
794
+
795
+ /**
796
+ @alias module:Jqgrid_utils
797
+ @param {object} - the col_model of the grid
798
+ @param {string} - the name of the page(optional)
799
+ @param {object} - the grid objec(optional)
800
+ @example
801
+ col_model = await jqu.resize_saved_cell_width(col_model);
802
+ */
803
+ async resize_saved_cell_width(col_model,page=false, grid=false)
804
+ {
805
+ let key = page ? page : this.page;
806
+ key += grid ? '-' + grid + '-w-' : '-grid-w-';
807
+ for(let x = 0; x<= col_model.length; x++)
808
+ {
809
+ if(col_model[x])
810
+ {
811
+ if(col_model[x]['name'])
812
+ {
813
+ const name = col_model[x]['name'];
814
+ const width = localStorage.getItem(key + name);
815
+ if(width)
816
+ {
817
+ col_model[x]['width'] = width;
818
+ }
819
+ }
820
+ }
821
+ }
822
+ return col_model;
823
+ }
824
+
825
+ /**
826
+ @alias module:Jqgrid_utils
827
+ @param {string} the width of the resized column
828
+ @param {string} column number what get resized
829
+ @param {string} not in use yet
830
+ @example
831
+ * var jqu = new Jqgrid_utils({page:'mypage'});
832
+ * resizeStop: jqu.resize_cell,
833
+ */
834
+ resize_cell(width, index, _page=false)
835
+ {
836
+ const col_model = jQuery(this).jqGrid ('getGridParam', 'colModel');
837
+ if(col_model[index])
838
+ {
839
+ if(col_model[index]['name'])
840
+ {
841
+ const name = col_model[index]['name'];
842
+ const page = _page ? _page : localStorage.getItem('page');
843
+ const grid = this.id;
844
+ let key = page + '-' + grid + '-w-' + name;
845
+ localStorage.setItem(key, width);
846
+ const cat = localStorage.getItem(key);
847
+ }
848
+ }
849
+ }
850
+
851
+
852
+
853
+ /**
854
+ * Upsert(insert or update) from the grid to an API
855
+ @alias module:Jqgrid_utils
856
+ @param {object} - row object
857
+ @param {string} - url of the API
858
+ @param {string} - data oject
859
+ @returns {object} {update: 'ok'} or {update: 'failed'}
860
+ @example
861
+ var jqu = new Jqgrid_utils();
862
+ afterSetRow: async function(row)
863
+ {
864
+ let r = await jqu.upsert_row(row, 'http://api.com',{'key':'value'});
865
+ console.log(r);
866
+ },
867
+ */
868
+ async upsert_row(row, url, req = {})
869
+ {
870
+ if (row.rowid.startsWith('jqg'))
871
+ {
872
+ const r0 = await this.insert_row(row, url);
873
+ return r0;
874
+ }
875
+ else
876
+ {
877
+ const r1 = await this.update_row(row, url);
878
+ return r1;
879
+ }
880
+ }
881
+
882
+
883
+ /**
884
+ * Insert from the grid to an API used by the upsert_row function
885
+ @alias module:Jqgrid_utils
886
+ @param {object} - row object
887
+ @param {string} - URL of the API
888
+ @returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
889
+ @example
890
+ var jqu = new Jqgrid_utils();
891
+ afterSetRow: async function(row)
892
+ {
893
+ let r = await jqu.insert_row(row, 'http://api.com');
894
+ console.log(r);
895
+ },
896
+ */
897
+ async insert_row(row, url)
898
+ {
899
+ let req = {};
900
+ let ret = '';
901
+ if (row.inputData.hasOwnProperty('id'))
902
+ {
903
+ req['_id'] = 'id';
904
+ req['_id_val'] = row.inputData['id'];
905
+ for (let i in row.inputData)
906
+ {
907
+ req[i] = row.inputData[i];
908
+ }
909
+ delete req['id'];
910
+ delete req['oper'];
911
+ //console.log(req);
912
+ ret = await this.put_json(url, JSON.stringify(req));
913
+ }
914
+ return ret;
915
+ }
916
+
917
+
918
+ /**
919
+ * Update from the grid to an API used by the upsert_row function
920
+ @alias module:Jqgrid_utils
921
+ @param {object} - row object
922
+ @param {string} - url of the API
923
+ @param {string} - data oject
924
+ @returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
925
+ @example
926
+ var jqu = new Jqgrid_utils();
927
+ afterSetRow: async function(row)
928
+ {
929
+ let r = await jqu.update_row(row, 'http://api.com',{'key':value});
930
+ console.log(r);
931
+ },
932
+ */
933
+
934
+ async update_row(row, url, req = {})
935
+ {
936
+ let ret = '';
937
+ {
938
+ if(! req['_id'])
939
+ {
940
+ req['_id'] = 'id';
941
+ }
942
+ req['_id_val'] = row.inputData['id'];
943
+ for (let i in row.inputData)
944
+ {
945
+ req[i] = row.inputData[i];
946
+ }
947
+ delete req['id'];
948
+ delete req['oper'];
949
+ ret = await this.post_json(url, JSON.stringify(req));
950
+ }
951
+ return ret;
952
+ }
953
+
954
+ /**
955
+ * Delete from the grid to an API
956
+ @alias module:Jqgrid_utils
957
+ @param {string} - row id
958
+ @param {string} - url of the API
959
+ @returns {object} @returns {object} Object from the the API like {delete: 'ok'} or {delete: 'failed'}
960
+ @example
961
+ var jqu = new Jqgrid_utils();
962
+ afterDelRow: async function(row)
963
+ {
964
+ const r = await jqu.delete_row('id', 'http://api.com');
965
+ console.log(r + ' : ' + row + ' - from API');
966
+ },
967
+ */
968
+
969
+ async delete_row(_id, url)
970
+ {
971
+ let ret = '';
972
+ if (url.indexOf('?') > -1)
973
+ {
974
+ url += "&_id=" + encodeURIComponent(unescape(_id));
975
+ }
976
+ else
977
+ {
978
+ url += "?_id=" + encodeURIComponent(unescape(_id));
979
+ }
980
+
981
+ ret = JSON.parse(await this.adelete_api(url));
982
+ return ret['message'];
983
+ }
984
+
985
+
986
+ /**
987
+ * Async Delete request used by function delete_row
988
+ @alias module:Jqgrid_utils
989
+ @param {string} - url of the API
990
+ @param {boalan} - header should be json type? default form type
991
+ @returns {object} @returns {object} Object from the the API like {delete: 'ok'} or {delete: 'failed'}
992
+ @example
993
+ var jqu = new Jqgrid_utils();
994
+ afterDelRow: async function(row)
995
+ {
996
+ ret = JSON.parse(await jqu.adelete_api(url));
997
+ },
998
+ */
999
+
1000
+ async adelete_api(url, json = false)
1001
+ {
1002
+ let ctype = "application/x-www-form-urlencoded";
1003
+ let body = null;
1004
+ if(json)
1005
+ {
1006
+ ctype = "application/json;charset=UTF-8" ;
1007
+ body = json;
1008
+ }
1009
+ return new Promise((resolve, reject) =>
1010
+ {
1011
+ let xhr = new XMLHttpRequest();
1012
+ xhr.open("DELETE", url);
1013
+ xhr.setRequestHeader("Content-type", ctype);
1014
+ xhr.onload = () => resolve(xhr.responseText);
1015
+ xhr.onerror = () => reject(xhr.statusText);
1016
+ xhr.send(body);
1017
+ });
1018
+ }
1019
+
1020
+
1021
+ /**
1022
+ * Async Post request used by the update_row function
1023
+ @alias module:Jqgrid_utils
1024
+ @param {string} - url of the API
1025
+ @param {object} - json object
1026
+ @returns {object} @returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
1027
+ @example
1028
+ var jqu = new Jqgrid_utils();
1029
+ ret = JSON.parse(await jqu.post_json(url,{'key':value,'key2':'value'}));
1030
+ */
1031
+
1032
+ async post_json(url, data)
1033
+ {
1034
+ return new Promise((resolve, reject) =>
1035
+ {
1036
+ let xhr = new XMLHttpRequest();
1037
+ xhr.open("POST", url);
1038
+ xhr.setRequestHeader("Content-type", "application/json");
1039
+ xhr.onload = () => resolve(xhr.responseText);
1040
+ xhr.onerror = () => reject(xhr.statusText);
1041
+ xhr.send(data);
1042
+ });
1043
+ }
1044
+
1045
+ /**
1046
+ * Async Put request used by the insert_row function
1047
+ @alias module:Jqgrid_utils
1048
+ @param {string} - url of the API
1049
+ @param {object} - json object
1050
+ @returns {object} @returns {object} Object from the the API like {insert: 'ok'} or {insert: 'failed'}
1051
+ @example
1052
+ var jqu = new Jqgrid_utils();
1053
+ ret = JSON.parse(await jqu.put_json(url,{'key':value,'key2':'value2'}));
1054
+ */
1055
+ async put_json(url, data)
1056
+ {
1057
+ return new Promise((resolve, reject) =>
1058
+ {
1059
+ let xhr = new XMLHttpRequest();
1060
+ xhr.open("PUT", url);
1061
+ xhr.setRequestHeader("Content-type", "application/json");
1062
+ xhr.onload = () => resolve(xhr.responseText);
1063
+ xhr.onerror = () => reject(xhr.statusText);
1064
+ xhr.send(data);
1065
+ });
1066
+ }
1067
+
1068
+ /**
1069
+ * Hide the del iconf rom the grid
1070
+ @alias module:Jqgrid_utils
1071
+ @example
1072
+ var jqu = new Jqgrid_utils();
1073
+ jqu.hide_del_icon();
1074
+ */
1075
+ s_hide_del_icon()
1076
+ {
1077
+ hide_del_icon();
1078
+ }
1079
+
1080
+ /**
1081
+ * Hide the del iconf rom the grid
1082
+ @alias module:Jqgrid_utils
1083
+ @example
1084
+ var jqu = new Jqgrid_utils();
1085
+ await jqu.hide_del_icon();
1086
+ */
1087
+ async hide_del_icon()
1088
+ {
1089
+ jQuery('.ui-inline-del').each(function(index) {jQuery(this).html('');});
1090
+ }
1091
+
1092
+
1093
+ /**
1094
+ * Convert a cell into a link/url with data from another cell and spit the value by comma - CSV
1095
+ @alias module:Jqgrid_utils
1096
+ @param {object} - col_model of the grid
1097
+ @param {string} - URL string
1098
+ @param {string} - Column/Cell to use
1099
+ @param {string} - URL Attributes
1100
+ @param {object} - keys and fields value to use
1101
+ @param {object} - format info
1102
+ @param {string} - seperator of the cell value to split (default is comma)
1103
+ @example
1104
+ var jqu = new Jqgrid_utils();
1105
+ col_model = await jqu.add_link_details_csv(col_model, host + '/html/report.html' , 'tags','target="_blank"',{"tags":"tags"},',');
1106
+
1107
+ */
1108
+ async add_link_details_csv(col_model, url, edit_field, attr = '', keys, format, seperator=',')
1109
+ {
1110
+ let self = this;
1111
+ if (url.indexOf('?') > -1)
1112
+ {
1113
+ url = url + '&';
1114
+ }
1115
+ else
1116
+ {
1117
+ url = url + '?';
1118
+ }
1119
+
1120
+ for (let i = 0; i < col_model.length; i++)
1121
+ {
1122
+ if (col_model[i]['name'] === edit_field)
1123
+ {
1124
+ col_model[i]['formatter'] = function(cell_val, obj)
1125
+ {
1126
+ let key_val = cell_val;
1127
+ const _cell_val = self.__cell_format(cell_val, format);
1128
+ const a = _cell_val.split(seperator);
1129
+ let cell_value = '';
1130
+ for(let x in a)
1131
+ {
1132
+ const x_value = a[x].trim();
1133
+ if(x_value)
1134
+ {
1135
+ if (typeof keys === 'object')
1136
+ {
1137
+ let pref = '';
1138
+ for (let ii in keys)
1139
+ {
1140
+ let key = ii;
1141
+ let v = keys[ii];
1142
+ key_val = obj.rowData[v];
1143
+ if (key_val)
1144
+ {
1145
+ if (key.indexOf('=') !== -1)
1146
+ {
1147
+ pref = pref + '' + key + '' + encodeURIComponent(x_value) + '&';
1148
+ }
1149
+ else
1150
+ {
1151
+ pref = pref + '' + key + '=' + encodeURIComponent(x_value) + '&';
1152
+ }
1153
+ }
1154
+ }
1155
+ if (pref.slice(-1) === '&')
1156
+ {
1157
+ pref = pref.slice(0, -1);
1158
+ }
1159
+ cell_value += '<a ' + attr + 'href="' + url + pref + '"> ' + x_value + '</a>' + seperator + ' ';
1160
+ }
1161
+ }
1162
+ cell_val = cell_value.trim();
1163
+ if(cell_val.slice(-1) === seperator) //remove last seperator
1164
+ {
1165
+ cell_val = cell_val.slice(0, -1);
1166
+ }
1167
+
1168
+ }
1169
+
1170
+ if(cell_val)
1171
+ {
1172
+ return cell_val;
1173
+ }
1174
+ else
1175
+ {
1176
+ return _cell_val;
1177
+ }
1178
+ };
1179
+ }
1180
+
1181
+ }
1182
+
1183
+ return col_model;
1184
+ }
1185
+
1186
+
1187
+ /**
1188
+ * Compare 2 columns and give them a style class when they have different content
1189
+ * http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods
1190
+ @alias module:Jqgrid_utils
1191
+ @param {object} - grid object
1192
+ @param {string} - first column
1193
+ @param {string} - second column
1194
+ @param {string} - css class name
1195
+ @example
1196
+ loadComplete: async function()
1197
+ {
1198
+ await jqu.compare(this,'value','value_report','greenlight');
1199
+ }
1200
+
1201
+ */
1202
+ async compare(obj, column1, column2, style)
1203
+ {
1204
+ const rows = jQuery(obj).jqGrid('getGridParam','data');
1205
+ for(let i in rows) {
1206
+ if( rows[i][column1] != rows[i][column2] )
1207
+ {
1208
+ jQuery(obj).jqGrid('setCell',rows[i]['id'], column1, "" ,'greenlight');
1209
+ jQuery(obj).jqGrid('setCell',rows[i]['id'], column2, "" ,'greenlight');
1210
+ }
1211
+ }
1212
+ }
1213
+
1214
+ /**
1215
+ * Set styles to individual cells, what are defined in a dedicated column
1216
+ @alias module:Jqgrid_utils
1217
+ @param {object} - grid object
1218
+ @param {string} - name of the column what includes the style values what need to be in a strinify json format
1219
+ @example
1220
+ var jqu = new Jqgrid_utils();
1221
+ loadComplete: async function() {
1222
+ await jqu.set_styles(this);
1223
+ },
1224
+ */
1225
+ async set_styles(obj,style_column='styles')
1226
+ {
1227
+
1228
+ const rows = jQuery(obj).jqGrid('getGridParam','data');
1229
+ for(let i in rows) {
1230
+ if(rows[i][style_column])
1231
+ {
1232
+ const styles = JSON.parse(rows[i][style_column]);
1233
+ for(let ii in styles)
1234
+ {
1235
+ const rowid = rows[i]['id'];
1236
+ const name = ii;
1237
+ if(rows[i].hasOwnProperty(name))
1238
+ {
1239
+ jQuery("#grid").jqGrid('setCell',rowid,name,"",styles[ii]);
1240
+ }
1241
+ }
1242
+ }
1243
+ }
1244
+
1245
+ }
1246
+
1247
+
1248
+
1249
+ /**
1250
+ * Convert a cell into a link/url with data from another cell
1251
+ @alias module:Jqgrid_utils
1252
+ @param {object} - col_model of the grid
1253
+ @param {string} - URL string
1254
+ @param {string} - Column/Cell to use
1255
+ @param {string} - URL Attributes
1256
+ @param {object} - keys and fields value to use
1257
+ @example
1258
+ var jqu = new Jqgrid_utils();
1259
+ col_model = await jqu.add_link_details(col_model,'http://foo.bar' , 'style','target="_blank"',{'key':'style'});
1260
+ col_model = await jqu.add_link_details(col_model, host + '/html/table_size.html' , 'database','target="_blank"',{"database":"database","server":"server"});
1261
+ */
1262
+ async add_link_details(col_model, url, edit_field, attr = '', keys, format)
1263
+ {
1264
+ let self = this;
1265
+ if (url.indexOf('?') > -1)
1266
+ {
1267
+ url = url + '&';
1268
+ }
1269
+ else
1270
+ {
1271
+ url = url + '?';
1272
+ }
1273
+
1274
+ for (let i = 0; i < col_model.length; i++)
1275
+ {
1276
+ if (col_model[i]['name'] === edit_field)
1277
+ {
1278
+ col_model[i]['formatter'] = function(cell_val, obj)
1279
+ {
1280
+ cell_val = String(cell_val);
1281
+ let key_val = '';
1282
+ if(cell_val)
1283
+ {
1284
+ key_val = cell_val;
1285
+ }
1286
+ let t = '';
1287
+ if(cell_val) {
1288
+ t = cell_val.toString();
1289
+ }
1290
+
1291
+ if (typeof keys === 'object')
1292
+ {
1293
+ let pref = '';
1294
+ for (let ii in keys)
1295
+ {
1296
+ let key = ii;
1297
+ let v = keys[ii];
1298
+ key_val = obj.rowData[v];
1299
+ if (key_val)
1300
+ {
1301
+ if (key_val)
1302
+ {
1303
+ if (key.indexOf('=') !== -1)
1304
+ {
1305
+ pref = pref + '' + key + '' + encodeURIComponent(key_val) + '&';
1306
+ }
1307
+ else
1308
+ {
1309
+ pref = pref + '' + key + '=' + encodeURIComponent(key_val) + '&';
1310
+ }
1311
+ }
1312
+ }
1313
+ }
1314
+ if (pref)
1315
+ {
1316
+ if (pref.slice(-1) === '&')
1317
+ {
1318
+ pref = pref.slice(0, -1);
1319
+ }
1320
+ const _cell_val = self.__cell_format(cell_val, format);
1321
+
1322
+ if(t !='')
1323
+ {
1324
+ cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + _cell_val + '</a>';
1325
+ }
1326
+ else
1327
+ {
1328
+
1329
+ cell_val = '';
1330
+ }
1331
+ }
1332
+
1333
+ }
1334
+ if(t)
1335
+ {
1336
+ return cell_val;
1337
+ }
1338
+ else
1339
+ {
1340
+ return '';
1341
+ }
1342
+ };
1343
+ }
1344
+
1345
+ }
1346
+
1347
+ return col_model;
1348
+ }
1349
+
1350
+
1351
+
1352
+ /**
1353
+ * Convert a cell into seperated based link/url like https://foo.bar.com/field/value/field/value
1354
+ @alias module:Jqgrid_utils
1355
+ @param {object} - col_model of the grid
1356
+ @param {string} - URL string
1357
+ @param {string} - Column/Cell to use
1358
+ @param {string} - URL Attributes
1359
+ @param {object} - keys and fields value to use
1360
+ @example
1361
+ var jqu = new Jqgrid_utils();
1362
+ col_model = await jqu.add_link_details_separator(col_model, url1 , 'style','target="_blank"',{"pricelist":"pricelist","style":"style"});
1363
+ col_model = await jqu.add_link_details_separator(col_model, 'https://foo.com' , 'target_column','target="_blank"',{"mykey":"myval"});
1364
+ */
1365
+ async add_link_details_separator(col_model, url, edit_field, attr = '', keys, format)
1366
+ {
1367
+ url = url + '/';
1368
+ let self = this;
1369
+ for (let i = 0; i < col_model.length; i++)
1370
+ {
1371
+ if (col_model[i]['name'] === edit_field)
1372
+ {
1373
+ col_model[i]['formatter'] = function(cell_val, obj)
1374
+ {
1375
+ let key_val = cell_val;
1376
+
1377
+ if (typeof keys === 'object')
1378
+ {
1379
+ let pref = '';
1380
+ for (let ii in keys)
1381
+ {
1382
+
1383
+ let key = ii;
1384
+ let v = keys[ii];
1385
+ key_val = obj.rowData[v];
1386
+ if (key_val)
1387
+ {
1388
+ if (key_val)
1389
+ {
1390
+ if(key != '')
1391
+ {
1392
+ pref += key + '' + '/' + encodeURIComponent(key_val) + '/';
1393
+ }
1394
+ else
1395
+ {
1396
+ pref += encodeURIComponent(key_val);
1397
+ }
1398
+ }
1399
+ }
1400
+ }
1401
+ if (pref)
1402
+ {
1403
+ if (pref.slice(-1) === '&' || pref.slice(-1) === '/' )
1404
+ {
1405
+ pref = pref.slice(0, -1);
1406
+ }
1407
+ const _cell_val = self.__cell_format(cell_val, format);
1408
+ cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + _cell_val + '</a>';
1409
+ }
1410
+
1411
+ }
1412
+
1413
+ return cell_val;
1414
+ };
1415
+ }
1416
+
1417
+ }
1418
+
1419
+ return col_model;
1420
+ }
1421
+
1422
+
1423
+
1424
+ /**
1425
+ * Convert a cell into seperated based link/url include parameter based url like https://foo.bar.com/field.html?k=v
1426
+ @alias module:Jqgrid_utils
1427
+ @param {object} - col_model of the grid
1428
+ @param {string} - URL string
1429
+ @param {array} - array of dict
1430
+ @param {string} - URL Attributes
1431
+ @example
1432
+ var jqu = new Jqgrid_utils();
1433
+ col_model = await jqu.add_link_separator(col_model, host + '/html' , 'style',[
1434
+ {
1435
+ 'field':'pricelist',
1436
+ 'extension':'.html',
1437
+ 'fields':{'style':'style'}
1438
+ }
1439
+ ]);
1440
+ //other example
1441
+ col_model = await jqu.add_link_separator(col_model, 'https://wiki.salamander-jewelry.net/index.php/grid_loss' , 'e',[{'field':'e'}],'target="_blank"');
1442
+
1443
+ */
1444
+ async add_link_separator(col_model, url, edit_field, fields, attr='')
1445
+ {
1446
+ url = url + '/';
1447
+ let self = this;
1448
+ for (let i = 0; i < col_model.length; i++)
1449
+ {
1450
+ if (col_model[i]['name'] === edit_field)
1451
+ {
1452
+ col_model[i]['formatter'] = function(cell_val, obj)
1453
+ {
1454
+ let key_val = cell_val;
1455
+ let pref = '';
1456
+ for(let x in fields)
1457
+ {
1458
+ for(let xx in fields[x])
1459
+ {
1460
+ if(xx === 'field')
1461
+ {
1462
+ let field_value = obj.rowData[fields[x][xx]];
1463
+ pref += field_value;
1464
+ }
1465
+ if(xx === 'extension')
1466
+ {
1467
+ pref += fields[x][xx] ;
1468
+ }
1469
+ if(xx === 'fields')
1470
+ {
1471
+ pref += '?';
1472
+ for(let key in fields[x][xx])
1473
+ {
1474
+ let val = obj.rowData[fields[x][xx][key]];
1475
+ pref = pref + '' + key + '=' + encodeURIComponent(val) + '&';
1476
+ }
1477
+ }
1478
+
1479
+ }
1480
+ }
1481
+ if (pref)
1482
+ {
1483
+ if (pref.slice(-1) === '&' || pref.slice(-1) === '/' )
1484
+ {
1485
+ pref = pref.slice(0, -1);
1486
+ }
1487
+ cell_val = '<a ' + attr + 'href="' + url + pref + '"> ' + cell_val + '</a>';
1488
+ }
1489
+
1490
+ return cell_val;
1491
+ };
1492
+ }
1493
+ }
1494
+ return col_model;
1495
+ }
1496
+
1497
+ /**
1498
+ * Private Function
1499
+ @alias module:Jqgrid_utils
1500
+ */
1501
+ __cell_format(cell_value, format)
1502
+ {
1503
+ if (format == 'format_ok')
1504
+ {
1505
+ if (cell_value == 0 || cell_value === 'fail')
1506
+ {
1507
+ cell_value = '<i data-check="failed" class="fa fa-times-circle" aria-hidden="true" style="color:#ff0000;"></i>';
1508
+ }
1509
+ else
1510
+ {
1511
+ cell_value = '<i data-check="ok" class="fa fa-check-circle" aria-hidden="true" style="color:#008000;"></i>';
1512
+ }
1513
+ }
1514
+ return cell_value;
1515
+ }
1516
+
1517
+
1518
+
1519
+ /**
1520
+ @alias module:Jqgrid_utils
1521
+ @param {string} - row_id
1522
+ @param {string} - data id
1523
+ @param {string} - url to request
1524
+ @param {object} - col_model for the table
1525
+ @param {string} - Add to the caption of the subgrid
1526
+ @example
1527
+ subGrid: true,
1528
+ ,subGridRowExpanded: async function(_id, id) {
1529
+ let data_url2 = api + '/process_locations?f=data&process=';
1530
+ let col_model_url2 = api + '/process_locations?f=col_model';
1531
+ let col_model2 = JSON.parse(await vwu.aget_api(col_model_url2));
1532
+ await jqu.subgrid(_id, id, data_url2, col_model2,'Locations for Process');
1533
+
1534
+ let data_url = api + '/process_styles?f=data&process=';
1535
+ let col_model_url = api + '/process_styles?f=col_model';
1536
+ let col_model = JSON.parse(await vwu.aget_api(col_model_url));
1537
+ await jqu.subgrid(_id, id, data_url, col_model,'Styles for Process');
1538
+ },
1539
+
1540
+ or
1541
+
1542
+ subGrid: true,
1543
+ subGridRowExpanded: async function(_id, id) {
1544
+ let row_data = jQuery(this).jqGrid ('getRowData', id);
1545
+ let param={f:'data','style':row_data['style'],pricelist:'P-TENENGR1'};
1546
+ let data_url = api + '/order_ln';
1547
+ data_url = await add_parameters(data_url, param);
1548
+ let col_model_url2 = api + '/order_ln?f=col_model';
1549
+ let col_model2 = JSON.parse(await vwu.aget_api(col_model_url2));
1550
+ await jqu.subgrid(_id, false, data_url, col_model2,'Order Lines for ' + row_data['style']);
1551
+ },
1552
+
1553
+ */
1554
+ async subgrid(_id, id, url, col_model, caption='' )
1555
+ {
1556
+ caption = caption != '' ? caption + ' ' : '';
1557
+ if(id)
1558
+ {
1559
+ url += id;
1560
+ }
1561
+ else
1562
+ {
1563
+ id = '';
1564
+ }
1565
+ let $s1 = jQuery("<table style='margin: 5px 0' class='" + _id + "_t'></table>");
1566
+ $s1.appendTo("#" + jQuery.jgrid.jqID(_id));
1567
+ $s1.jqGrid({
1568
+ caption: caption + id,
1569
+ colModel: col_model,
1570
+ datatype: "json",
1571
+ url: url,
1572
+ gridview: true,
1573
+ rownumbers: true,
1574
+ autoencode: true,
1575
+ sortname: "c1",
1576
+ sortorder: "desc"
1577
+ });
1578
+ }
1579
+
1580
+
1581
+ /**
1582
+ @alias module:Jqgrid_utils
1583
+ @param {object} - col_model for the grid
1584
+ @param {string} - field what include the image/picture href path like http://mypicture.png
1585
+ @param {int} - size of the picture
1586
+ @param {bolen} - image path should be a link
1587
+ @example
1588
+ col_model = await jqu.add_image(col_model, image_field, 60, false);
1589
+ */
1590
+ async add_image(col_model, edit_field, size, link=false)
1591
+ {
1592
+ if (size === undefined)
1593
+ {
1594
+ size = 60;
1595
+ }
1596
+ for (let i = 0; i < col_model.length; i++)
1597
+ {
1598
+ if (col_model[i]['name'] === edit_field)
1599
+ {
1600
+ col_model[i]['picture'] = true;
1601
+ col_model[i]['width'] = size;
1602
+ col_model[i]['height'] = size;
1603
+ col_model[i]['formatter'] = function(cell_val)
1604
+ {
1605
+ const cell_val2 = cell_val.toLowerCase();
1606
+
1607
+ if(cell_val2.startsWith('https://', 0) || cell_val2.startsWith('http://', 0) )
1608
+ {
1609
+ if(cell_val2.includes(".png") ||
1610
+ cell_val2.includes(".jpg") ||
1611
+ cell_val2.includes(".jpeg") ||
1612
+ cell_val2.includes(".gif") ||
1613
+ cell_val2.includes(".svg") ||
1614
+ cell_val2.includes(".svgz") ||
1615
+ cell_val2.includes(".webp"))
1616
+ {
1617
+ if(link)
1618
+ {
1619
+ return '<a target="blank" href="' + cell_val + '"><img src="' + cell_val + '" alt="my image" width="' + size + '" /></a>';
1620
+ }
1621
+ else
1622
+ {
1623
+ return '<img src="' + cell_val + '" alt="my image" width="' + size + '" />';
1624
+ }
1625
+ }
1626
+ }
1627
+ return cell_val;
1628
+ };
1629
+ }
1630
+ }
1631
+ return col_model;
1632
+ }
1633
+
1634
+
1635
+
1636
+
1637
+
1638
+
1639
+
1640
+ /**
1641
+ * Add a filter to the website beside the grid
1642
+ @alias module:Jqgrid_utils
1643
+ @param {object} - grid object or grid string name
1644
+ @param {object} - the grid data object
1645
+ @param {object} - a dict with a array what should be filterd by the grid
1646
+ @param {string} - id name of the DOM oject where the filter should be appened
1647
+ @example
1648
+ var jqu = new Jqgrid_utils();
1649
+ var run_me_once = true;
1650
+ gridComplete: async function(){
1651
+ if(run_me_once)
1652
+ {
1653
+ await jqu.set_filter(this, data, {material:[],section:[]}, '#filter');
1654
+ run_me_once = false;
1655
+ }
1656
+ },
1657
+ */
1658
+ async set_filter(grid, data, fx, append_to="#filter")
1659
+ {
1660
+ jQuery(grid).jqGrid('setGridParam', { fdata: data });
1661
+ let f = document.querySelector(append_to);
1662
+ for(const i in data)
1663
+ {
1664
+ for(let x in fx)
1665
+ {
1666
+ fx[x].push(data[i][x]);
1667
+ }
1668
+ }
1669
+
1670
+ for(let x in fx)
1671
+ {
1672
+ fx[x]= fx[x].filter((val, ind, arr) => arr.indexOf(val) === ind);
1673
+ fx[x].sort();
1674
+ }
1675
+
1676
+ for(let x in fx)
1677
+ {
1678
+ let ul = document.createElement('ul');
1679
+ let lh = document.createElement('lh');
1680
+ lh.innerHTML = x;
1681
+ ul.appendChild(lh);
1682
+ for(let i in fx[x])
1683
+ {
1684
+ let li = document.createElement('li');
1685
+ let l = document.createElement('label');
1686
+ l.innerHTML = fx[x][i];
1687
+ let c = document.createElement('input');
1688
+ c.setAttribute('type','checkbox');
1689
+ c.setAttribute('class',x);
1690
+ c.setAttribute('id', x + '_' + fx[x][i]);
1691
+ l.setAttribute('for',x + '_' + fx[x][i]);
1692
+ c.value = fx[x][i];
1693
+ c.onchange = async () => { await this._filter(grid,fx);};
1694
+ li.appendChild(l);
1695
+ li.appendChild(c);
1696
+ ul.appendChild(li);
1697
+ }
1698
+ f.appendChild(ul);
1699
+ }
1700
+ }
1701
+
1702
+
1703
+ /**
1704
+ * private function of set_filter
1705
+ @alias module:Jqgrid_utils
1706
+ */
1707
+ async _filter(grid, fx)
1708
+ {
1709
+ let _data = [];
1710
+ let data = jQuery(grid).jqGrid('getGridParam','fdata');
1711
+ let filter = [];
1712
+ for(let x in fx)
1713
+ {
1714
+ let m = document.querySelectorAll("." + x);
1715
+ filter[x] = [];
1716
+ for(let i in m)
1717
+ {
1718
+ if(m[i].checked)
1719
+ {
1720
+ filter[x].push(m[i].value);
1721
+ }
1722
+ }
1723
+ }
1724
+ for(let i in data)
1725
+ {
1726
+ let include = false;
1727
+ for(let x in fx)
1728
+ {
1729
+ if(filter[x].indexOf(data[i][x]) != -1)
1730
+ {
1731
+ include = true;
1732
+ }
1733
+ }
1734
+ if(include)
1735
+ {
1736
+ _data.push(data[i]);
1737
+ }
1738
+ }
1739
+ jQuery(grid).jqGrid('clearGridData');
1740
+ jQuery(grid).jqGrid('setGridParam', {data: _data});
1741
+ jQuery(grid).trigger('reloadGrid');
1742
+ }
1743
+
1744
+
1745
+
1746
+
1747
+
1748
+
1749
+
1750
+ };
1751
+
1752
+
1753
+
1754
+ },{}]},{},[1])(1)
1755
+ });