jqgrid_utils 1.36.0 → 1.37.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.
@@ -1 +1,2118 @@
1
- !function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).Jqgrid_utils=e()}(function(){return function e(t,r,n){function a(o,s){if(!r[o]){if(!t[o]){var l="function"==typeof require&&require;if(!s&&l)return l(o,!0);if(i)return i(o,!0);var d=new Error("Cannot find module '"+o+"'");throw d.code="MODULE_NOT_FOUND",d}var c=r[o]={exports:{}};t[o][0].call(c.exports,function(e){return a(t[o][1][e]||e)},c,c.exports,e,t,r,n)}return r[o].exports}for(var i="function"==typeof require&&require,o=0;o<n.length;o++)a(n[o]);return a}({1:[function(e,t,r){"use strict";t.exports=class{constructor(e=!1){e&&e.hasOwnProperty("page")&&(this.page=e.page,localStorage.setItem("page",this.page))}async add_class(e,t,r){for(let n=0;n<e.length;n++)e[n].name===t&&(e[n].classes?e[n].classes+=" "+r:e[n].classes=r);return e}async _grid_substract_on(e,t=[],r=[],n,a=!1){return await this.grid_substract_on(e,t,r,n,a)}async grid_substract_on(e,t=[],r=[],n,a=!1){let i=jQuery(e),o=i.jqGrid("getGridParam","data"),s={invdate:"Total"},l=0,d=0;for(let e in t){let r=0;for(let n in o)if(o[n].hasOwnProperty(t[e])){let a=o[n][t[e]];"string"==typeof a&&is_digit(a)&&(a=parseFloat(a)),r+=a}r!=Math.floor(r)&&(r=r.toFixed(2)),s[t[e]]=r,l=r}for(let e in r){let t=0;for(let n in o)if(o[n].hasOwnProperty(r[e])){let a=o[n][r[e]];"string"==typeof a&&is_digit(a)&&(a=parseFloat(a)),t+=a}t!=Math.floor(t)&&(t=t.toFixed(2)),s[r[e]]=t,d=t}let c=l-d;return a&&c<0&&(c=0),s[n]=c,i.jqGrid("footerData","set",s),s}_grid_ratio_on(e,t,r,n){return this.grid_ratio_on(e,t,r,n)}async grid_ratio_on(e,t,r,n){jQuery("#grid").jqGrid("getGridParam","data");let a=jQuery(e),i=a.jqGrid("getGridParam","data"),o={invdate:"Total"};fraction_sum=0,denominator_sum=0,ratio_sum=0;for(let e in i)i[e].hasOwnProperty(t)&&(fraction_sum+=i[e][t]),i[e].hasOwnProperty(r)&&(denominator_sum+=i[e][r]);return o.qc_eta_ratio=(fraction_sum/denominator_sum).toFixed(2),a.jqGrid("footerData","set",o),o}is_html(e){let t=!1;try{const r=(new DOMParser).parseFromString(e,"text/html");Array.from(r.body.childNodes).some(e=>1===e.nodeType)&&(t=!0)}catch(e){}return t}async _grid_sum_on(e,t=[]){return await this.grid_sum_on(e,t)}async grid_sum_on(e,t=[]){let r=jQuery(e),n=r.jqGrid("getGridParam","data"),a={invdate:"Total"};for(let e in t){let r=0;for(let a in n){let i=0;if(n[a].hasOwnProperty(t[e])){let o=n[a][t[e]];if(o){if("string"==typeof o)if(this.is_html(o)){const e=(new DOMParser).parseFromString(o,"text/html").querySelectorAll("a"),t=Array.from(e).map(e=>e.text);if(t.length){const e=t[0].replace(",","");is_digit(e)&&(i=parseFloat(e))}}else is_digit(o)&&(i=parseFloat(o));else"number"==typeof o&&(i=o);r+=i}}}r!=Math.floor(r)&&(r=r.toFixed(2)),a[t[e]]=r}return r.jqGrid("footerData","set",a),a}_grid_avg_on(e,t=[]){return this.grid_avg_on(e,t)}async grid_avg_on(e,t=[]){let r=jQuery(e),n=r.jqGrid("getGridParam","data"),a=0,i={invdate:"Total"};for(let e in t){let r=0;for(let i in n)if(n[i].hasOwnProperty(t[e])){let o=n[i][t[e]];"string"==typeof o&&is_digit(o)&&(o=parseFloat(o)),r+=o,a++}r/=a,r!=Math.floor(r)&&(r=r.toFixed(2)),i[t[e]]=r}return r.jqGrid("footerData","set",i),i}_grid_percent_on(e,t){return grid_percent_on(e,t)}async grid_percent_on(e,t){let r=jQuery(e),n=r.jqGrid("getGridParam","data"),a={},i=t.id?t.id:"invate";footer[i]="Total";let o=t.total,s=JSON.parse(JSON.stringify(t.percent));s.push(o);for(let e in s){let t=s[e],r=0,i=0;for(let e in n)if(n[e].hasOwnProperty(t)){let a=n[e][t];"string"==typeof a?is_digit(a)&&(a=parseFloat(a),r+=a,i++):(r+=a,i++)}a[t]=r}for(let e in s){let t=s[e],r=a[t]/(a[o]/100);r=r.toFixed(2),footer[t]=r+"%"}r.jqGrid("footerData","set",footer)}async update_row_to_api(e,t="",r=["id"],n={},a){let i=this,o=[],s={},l={},d={};const c=e.jqGrid("getGridParam","record_data");if(""!=t&&Object.keys(n).length>0&&"edit"==n.inputData.oper){for(let e in c)if(c[e].id===n.rowid){for(let t in r)c[e].hasOwnProperty(r[t])&&(s[r[t]]=c[e][r[t]]);for(let t in n.inputData)"oper"!=t&&Object.keys(s).indexOf(t)<0&&n.inputData[t]!=c[e][t]&&(d[t]=n.inputData[t])}for(let e in d)if(Object.keys(s).indexOf(e)<0&&"oper"!=e){let r={};r[e]=d[e],a.ids=s,a.values=r,a.operator="edit";const n=await i.post_json(t,JSON.stringify(a));o.push(n)}}else if(""!=t&&Object.keys(n).length>0&&"add"==n.inputData.oper){for(let e in n.inputData)n.inputData[e]&&"id"!=e&&"oper"!=e&&(l[e]=n.inputData[e]);a.ids=s,a.values=l,a.operator="add";let e=await i.post_json(t,JSON.stringify(a));o.push(e)}return o}async delete_row_to_api(e,t="",r,n=[],a={}){let i={msg:"failed"},o=[],s={};const l=e.jqGrid("getGridParam","record_data");for(let e in l)if(l[e].id===r){for(let t in n)l[e].hasOwnProperty(n[t])&&(s[n[t]]=l[e][n[t]],o.push(n[t]));break}return""!=t&&Object.keys(s).length==o.length&&(a.ids=o,a.values=s,i=await this.adelete_api(t,JSON.stringify(a))),i}async append_seperator_link_column(e,t,r,n,a="",i){return t+="/",n.formatter=function(e,n){let o=r;if("object"==typeof i){let r="";for(let e in i){let t=e,a=i[e],o=n.rowData[a];o&&o&&(r+=""!=t?t+"/"+encodeURIComponent(o)+"/":encodeURIComponent(o))}r&&("&"!==r.slice(-1)&&"/"!==r.slice(-1)||(r=r.slice(0,-1)),e="<a "+a+'href="'+t+r+'"> '+o+"</a>")}return e},e.push(n),e}async add_edit(e,t,r,n){for(let a=0;a<e.length;a++)e[a].name===t&&(Object.assign(e[a],r),Object.assign(e[a],n));return e}async add_textarea(e,t,r='style="width:100%;height:100px"'){for(let n=0;n<e.length;n++)e[n].name===t&&(e[n].formatter=function(e){return"<textarea "+r+">"+e+"</textarea>"});return e}async get_col_model_from_data(e,t,r=[],n=[]){let a=[];for(let e in t){const r=Object.keys(t[e]);for(let e in r){const t=r[e];a.push(t)}}a=a.filter((e,t)=>a.indexOf(e)===t);let i=[];for(let e in n)i.push(n[e].name);let o=a.filter(e=>!i.includes(e));const s=new Set(r);o=o.filter(e=>!s.has(e)),o.sort();for(let e=0;e<o.length;e++)n.push({name:o[e],label:o[e]});return n}binary_replace(e,t="zero",r="one"){let n=t;return 1!=e&&0!=e||1==e&&(n=r),n}_date112_to_DMY(e,t="/"){let r=e;if(e.length>=8&&-1===e.indexOf(t)){let n=[];n.push(e.substr(6,2)),n.push(e.substr(4,2)),n.push(e.substr(0,4)),r=n.join(t)}return r}async date112_to_DMY(e,t,r="/"){for(let n=0;n<e.length;n++)e[n].name===t&&(e[n].formatter=function(e,t){if(e){let t=e=e.toString();if(e.length>=8&&-1===e.indexOf(r)){let n=[];n.push(e.substr(6,2)),n.push(e.substr(4,2)),n.push(e.substr(0,4)),t=n.join(r)}return t}return e});return e}async add_formatter(e,t,r){for(let n=0;n<e.length;n++)e[n].name===t&&r.hasOwnProperty("formatter")&&r.hasOwnProperty("formatoptions")&&(e[n].formatter=r.formatter,e[n].formatoptions=r.formatoptions,e[n].edittype=r.formatter,e[n].editoptions=r.formatoptions);return e}async natural_sort(e,t){for(let r=0;r<e.length;r++)e[r].name===t&&(e[r].sortfunc=function(e,t,r){void 0===r&&(r=1);var n,a,i=/(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,o=/(^[ ]*|[ ]*$)/g,s=/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,l=/^0x[0-9a-f]+$/i,d=/^0/,c=function(e){return self.insensitive&&(""+e).toLowerCase()||""+e},f=c(e).replace(o,"")||"",u=c(t).replace(o,"")||"",p=f.replace(i,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0"),_=u.replace(i,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0"),h=parseInt(f.match(l))||1!=p.length&&f.match(s)&&Date.parse(f),m=parseInt(u.match(l))||h&&u.match(s)&&Date.parse(u)||null;if(m){if(h<m)return-r;if(h>m)return r}for(var y=0,g=Math.max(p.length,_.length);y<g;y++){if(n=!(p[y]||"").match(d)&&parseFloat(p[y])||p[y]||0,a=!(_[y]||"").match(d)&&parseFloat(_[y])||_[y]||0,isNaN(n)!==isNaN(a))return isNaN(n)?r:-r;if(typeof n!=typeof a&&(n+="",a+=""),n<a)return-r;if(n>a)return r}return 0});return e}async add_html_formatter(e,t,r){for(let n=0;n<e.length;n++)e[n].name===t&&(e[n].formatter=function(e,t){return r});return e}async add_ok_button(e,t){let r=this;for(let n=0;n<e.length;n++)t.indexOf(e[n].name)>-1&&(e[n].formatter=function(e){return null!=e?r.__cell_format(e,"format_ok"):""});return e}async get_filled_cell_table_data(e,t=[]){let r=jQuery(e).jqGrid("getGridParam","data"),n=t,a=[];for(let e in r)if(r[e].hasOwnProperty("id")){let t=[r[e].id];for(let a in n)r[e].hasOwnProperty(n[a])?t.push(r[e][n[a]]):t.push("");var i=t.filter(function(e,t,r){return""!==e});Object.keys(i).length>1&&a.push(t)}return a}async get_filled_cell_data(e,t=[]){let r=jQuery(e).jqGrid("getGridParam","data"),n=t,a=[];for(let e in r)if(r[e].hasOwnProperty("id")){let t={id:r[e].id};for(let a in n)r[e].hasOwnProperty(n[a])&&""!=r[e][n[a]]&&(t[n[a]]=r[e][n[a]]);Object.keys(t).length>1&&a.push(t)}return a}async set_link(e,t,r,n=""){for(let a=0;a<e.length;a++)e[a].name===t&&(e[a].formatter=function(e,t){return'<a class="gl" '+n+'href="'+t.rowData[r]+'">'+e+"</a>"});return e}async hide_all_columns_except(e,t){for(let r=0;r<e.length;r++)t.indexOf(e[r].name)>-1?e[r].hidden=!1:e[r].hidden=!0;return e}async hide_column(e,t){for(let r=0;r<e.length;r++)e[r].name===t&&(e[r].hidden=!0);return e}s_grid_set_caption(e,t=[]){this.grid_set_captionn(e,[])}async grid_set_caption(e,t=[]){if(e){const r=jQuery(e);let n=0;n=0===t.length?r.jqGrid("getGridParam","records"):t.length;const a=/\d.*x/,i=r.jqGrid("getGridParam","caption").replace(a,"");r.jqGrid("setCaption",i+" "+n+"x")}}s_resize_saved_cell_width(e,t=!1,r=!1){this.grid_set_caption(e,t,r)}async resize_saved_cell_width(e,t=!1,r=!1){let n=t||this.page;n+=r?"-"+r+"-w-":"-grid-w-";for(let t=0;t<=e.length;t++)if(e[t]&&e[t].name){const r=e[t].name,a=localStorage.getItem(n+r);a&&(e[t].width=a)}return e}resize_cell(e,t,r=!1){const n=jQuery(this).jqGrid("getGridParam","colModel");if(n[t]&&n[t].name){const a=n[t].name;let i=(r||localStorage.getItem("page"))+"-"+this.id+"-w-"+a;localStorage.setItem(i,e),localStorage.getItem(i)}}async upsert_row(e,t,r={}){return e.rowid.startsWith("jqg")?await this.insert_row(e,t):await this.update_row(e,t)}async insert_row(e,t){let r={},n="";if(e.inputData.hasOwnProperty("id")){r._id="id",r._id_val=e.inputData.id;for(let t in e.inputData)r[t]=e.inputData[t];delete r.id,delete r.oper,n=await this.put_json(t,JSON.stringify(r))}return n}async update_row(e,t,r={}){let n="";r._id||(r._id="id"),r._id_val=e.inputData.id;for(let t in e.inputData)r[t]=e.inputData[t];return delete r.id,delete r.oper,n=await this.post_json(t,JSON.stringify(r)),n}async delete_row(e,t){let r="";return t.indexOf("?")>-1?t+="&_id="+encodeURIComponent(unescape(e)):t+="?_id="+encodeURIComponent(unescape(e)),r=JSON.parse(await this.adelete_api(t)),r.message}async adelete_api(e,t=!1){let r="application/x-www-form-urlencoded",n=null;return t&&(r="application/json;charset=UTF-8",n=t),new Promise((t,a)=>{let i=new XMLHttpRequest;i.open("DELETE",e),i.setRequestHeader("Content-type",r),i.onload=()=>t(i.responseText),i.onerror=()=>a(i.statusText),i.send(n)})}async post_json(e,t){return new Promise((r,n)=>{let a=new XMLHttpRequest;a.open("POST",e),a.setRequestHeader("Content-type","application/json"),a.onload=()=>r(a.responseText),a.onerror=()=>n(a.statusText),a.send(t)})}async put_json(e,t){return new Promise((r,n)=>{let a=new XMLHttpRequest;a.open("PUT",e),a.setRequestHeader("Content-type","application/json"),a.onload=()=>r(a.responseText),a.onerror=()=>n(a.statusText),a.send(t)})}s_hide_del_icon(){hide_del_icon()}async hide_del_icon(){jQuery(".ui-inline-del").each(function(e){jQuery(this).html("")})}async add_link_details_csv(e,t,r,n="",a,i,o=","){let s=this;t.indexOf("?")>-1?t+="&":t+="?";for(let l=0;l<e.length;l++)e[l].name===r&&(e[l].formatter=function(e,r){let l=e;const d=s.__cell_format(e,i),c=d.split(o);let f="";for(let i in c){const s=c[i].trim();if(s&&"object"==typeof a){let e="";for(let t in a){let n=t,i=a[t];l=r.rowData[i],l&&(e=-1!==n.indexOf("=")?e+""+n+encodeURIComponent(s)+"&":e+""+n+"="+encodeURIComponent(s)+"&")}"&"===e.slice(-1)&&(e=e.slice(0,-1)),f+="<a "+n+'href="'+t+e+'"> '+s+"</a>"+o+" "}(e=f.trim()).slice(-1)===o&&(e=e.slice(0,-1))}return e||d});return e}async compare(e,t,r,n){const a=jQuery(e).jqGrid("getGridParam","data");for(let i in a)a[i][t]!=a[i][r]&&(jQuery(e).jqGrid("setCell",a[i].id,t,"",n),jQuery(e).jqGrid("setCell",a[i].id,r,"",n))}async compare_smaller(e,t,r,n){const a=jQuery(e).jqGrid("getGridParam","data");for(let i in a)a[i][t]<a[i][r]&&(jQuery(e).jqGrid("setCell",a[i].id,t,"",n),jQuery(e).jqGrid("setCell",a[i].id,r,"",n))}async compare_bigger(e,t,r,n){const a=jQuery(e).jqGrid("getGridParam","data");for(let i in a)a[i][t]>a[i][r]&&(jQuery(e).jqGrid("setCell",a[i].id,t,"",n),jQuery(e).jqGrid("setCell",a[i].id,r,"",n))}async set_styles(e,t="styles"){const r=jQuery(e).jqGrid("getGridParam","data");for(let n in r)if(r[n][t]){const a=JSON.parse(r[n][t]);for(let t in a){const i=r[n].id,o=t;r[n].hasOwnProperty(o)&&jQuery(e).jqGrid("setCell",i,o,"",a[t])}}}async set_classes(e){const t=jQuery(e).getDataIDs();for(var r=0;r<t.length;r+=1){const a=jQuery(e).getRowData(t[r]);var n=jQuery("#"+t[r],jQuery(e));n.removeClass("ui-widget-content"),n.addClass(a.class)}}async add_link_details(e,t,r,n="",a,i){let o=this;t.indexOf("?")>-1?t+="&":t+="?";for(let s=0;s<e.length;s++)e[s].name===r&&(e[s].formatter=function(e,r){let s="";(e=String(e))&&(s=e);let l="";if(e&&(l=e.toString()),"object"==typeof a){let d="";for(let e in a){let t=e,n=a[e];s=r.rowData[n],s&&s&&(d=-1!==t.indexOf("=")?d+""+t+encodeURIComponent(s)+"&":d+""+t+"="+encodeURIComponent(s)+"&")}if(d){"&"===d.slice(-1)&&(d=d.slice(0,-1));const r=o.__cell_format(e,i);e=""!=l&&r&&l?"<a "+n+'href="'+t+d+'"> '+r+"</a>":""}}return l?e:""});return e}async add_link_details_separator(e,t,r,n="",a,i){t+="/";let o=this;for(let s=0;s<e.length;s++)e[s].name===r&&(e[s].formatter=function(e,r){let s=e;if("object"==typeof a){let l="";for(let e in a){let t=e,n=a[e];s=r.rowData[n],s&&s&&(l+=""!=t?t+"/"+encodeURIComponent(s)+"/":encodeURIComponent(s))}if(l){"&"!==l.slice(-1)&&"/"!==l.slice(-1)||(l=l.slice(0,-1));const r=o.__cell_format(e,i);e="<a "+n+'href="'+t+l+'"> '+r+"</a>"}}return e});return e}async add_link_separator(e,t,r,n,a=""){t+="/";for(let i=0;i<e.length;i++)e[i].name===r&&(e[i].formatter=function(e,r){let i="";for(let e in n)for(let t in n[e])if("field"===t&&(i+=r.rowData[n[e][t]]),"extension"===t&&(i+=n[e][t]),"fields"===t){i+="?";for(let a in n[e][t]){let o=r.rowData[n[e][t][a]];i=i+""+a+"="+encodeURIComponent(o)+"&"}}return i&&("&"!==i.slice(-1)&&"/"!==i.slice(-1)||(i=i.slice(0,-1)),e="<a "+a+'href="'+t+i+'"> '+e+"</a>"),e});return e}__cell_format(e,t){return"format_ok"==t&&(e=0==e||"fail"===e?'<i data-check="failed" class="fa fa-times-circle" aria-hidden="true" style="color:#ff0000;"></i>':'<i data-check="ok" class="fa fa-check-circle" aria-hidden="true" style="color:#008000;"></i>'),e}async subgrid(e,t,r,n,a=""){a=""!=a?a+" ":"",t?r+=t:t="";let i=jQuery("<table style='margin: 5px 0' class='"+e+"_t'></table>");i.appendTo("#"+jQuery.jgrid.jqID(e)),i.jqGrid({caption:a+t,colModel:n,datatype:"json",url:r,gridview:!0,rownumbers:!0,autoencode:!0,sortname:"c1",sortorder:"desc"})}async add_image(e,t,r,n=!1){void 0===r&&(r=60);for(let a=0;a<e.length;a++)e[a].name===t&&(e[a].picture=!0,e[a].width=r,e[a].height=r,e[a].formatter=function(e){const t=e.toLowerCase();return(t.startsWith("https://",0)||t.startsWith("http://",0))&&(t.includes(".png")||t.includes(".jpg")||t.includes(".jpeg")||t.includes(".gif")||t.includes(".svg")||t.includes(".svgz")||t.includes(".webp"))?n?'<a target="blank" href="'+e+'"><img src="'+e+'" alt="my image" width="'+r+'" /></a>':'<img src="'+e+'" alt="my image" width="'+r+'" />':e});return e}async set_filter(e,t,r,n="#filter"){jQuery(e).jqGrid("setGridParam",{fdata:t});let a=document.querySelector(n);for(const e in t)for(let n in r)r[n].push(t[e][n]);for(let e in r)r[e]=r[e].filter((e,t,r)=>r.indexOf(e)===t),r[e].sort();for(let t in r){let n=document.createElement("ul"),i=document.createElement("lh");i.innerHTML=t,n.appendChild(i);for(let a in r[t]){let i=document.createElement("li"),o=document.createElement("label");o.innerHTML=r[t][a];let s=document.createElement("input");s.setAttribute("type","checkbox"),s.setAttribute("class",t),s.setAttribute("id",t+"_"+r[t][a]),o.setAttribute("for",t+"_"+r[t][a]),s.value=r[t][a],s.onchange=async()=>{await this._filter(e,r)},i.appendChild(o),i.appendChild(s),n.appendChild(i)}a.appendChild(n)}}async _filter(e,t){let r=[],n=jQuery(e).jqGrid("getGridParam","fdata"),a=[];for(let e in t){let t=document.querySelectorAll("."+e);a[e]=[];for(let r in t)t[r].checked&&a[e].push(t[r].value)}for(let e in n){let i=!1;for(let r in t)-1!=a[r].indexOf(n[e][r])&&(i=!0);i&&r.push(n[e])}jQuery(e).jqGrid("clearGridData"),jQuery(e).jqGrid("setGridParam",{data:r}),jQuery(e).trigger("reloadGrid")}}},{}]},{},[1])(1)});
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
+ constructor(settings = false) {
11
+ if (settings) {
12
+ if (settings.hasOwnProperty("page")) {
13
+ this.page = settings["page"];
14
+ localStorage.setItem("page", this.page);
15
+ }
16
+ }
17
+ }
18
+
19
+ /**
20
+ * Add a Checkbox to the Model
21
+ @alias module:Jqgrid_utils
22
+ @param {array} - grid col_model
23
+ @param {string} - column_name
24
+ @example
25
+ var jqu = new Jqgrid_utils();
26
+ ol_model = await jqu.add_checkbox(col_model, "closed");
27
+ */
28
+ async add_checkbox(col_model, field_name) {
29
+ for (let i = 0; i < col_model.length; i++) {
30
+ if (col_model[i]["name"] === field_name) {
31
+ col_model[i]["formatter"] = "checkbox";
32
+ col_model[i]["formatoptions"] = { disabled: false };
33
+ col_model[i]["edittype"] = "checkbox";
34
+ col_model[i]["editoptions"] = { value: "Yes:No", defaultValue: "Yes" };
35
+ }
36
+ }
37
+ return col_model;
38
+ }
39
+
40
+ /**
41
+ * Add a Class to the Model
42
+ @alias module:Jqgrid_utils
43
+ @param {array} - grid col_model
44
+ @param {string} - column_name
45
+ @param {string} - class_name
46
+ @example
47
+ var jqu = new Jqgrid_utils();
48
+ col_model = await jqu.add_class(col_model,field_name,class_name)
49
+ */
50
+
51
+ async add_class(col_model, field_name, class_name) {
52
+ for (let i = 0; i < col_model.length; i++) {
53
+ if (col_model[i]["name"] === field_name) {
54
+ if (col_model[i]["classes"]) {
55
+ col_model[i]["classes"] += " " + class_name;
56
+ } else {
57
+ col_model[i]["classes"] = class_name;
58
+ }
59
+ }
60
+ }
61
+ return col_model;
62
+ }
63
+
64
+ /**
65
+ * Syncron Alias grid_sum_on
66
+ @alias module:Jqgrid_utils
67
+ */
68
+ async _grid_substract_on(
69
+ grid,
70
+ minuend = [],
71
+ subtrahend = [],
72
+ difference,
73
+ no_negative = false,
74
+ ) {
75
+ return await this.grid_substract_on(
76
+ grid,
77
+ minuend,
78
+ subtrahend,
79
+ difference,
80
+ no_negative,
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Sum the columns values together
86
+ @alias module:Jqgrid_utils
87
+ @param {object} - Grid Object (required)
88
+ @param {array} - string array list of field_names used as minuend(number from which the other number is subtracted)
89
+ @param {array} - string array list of field_names used as subtrahend(number which is to be subtracted from the minuend)
90
+ @param {string} - string field name for the difference(number which is to be subtracted from the minuend)
91
+ @param {bolen} - true or 1 to not show negative numbers
92
+
93
+ @example
94
+ var jqu = new Jqgrid_utils({page:page});
95
+ gridComplete: function () {
96
+ jqu._jqu._grid_sum_on(this, [
97
+ "qty_icollect",
98
+ "qty_ordered",
99
+ "need_for_qty_ordered",
100
+ "wait_icollect",
101
+ ]);
102
+ },
103
+ */
104
+ async grid_substract_on(
105
+ grid,
106
+ minuend = [],
107
+ subtrahend = [],
108
+ difference,
109
+ no_negative = false,
110
+ ) {
111
+ let $self = jQuery(grid);
112
+ let rows = $self.jqGrid("getGridParam", "data");
113
+ let footer = { invdate: "Total" };
114
+ let _minuend = 0.0;
115
+ let _subtrahend = 0.0;
116
+ for (let i in minuend) {
117
+ let sum = 0;
118
+ for (let r in rows) {
119
+ if (rows[r].hasOwnProperty(minuend[i])) {
120
+ let val = rows[r][minuend[i]];
121
+ if (typeof val === "string") {
122
+ if (is_digit(val)) {
123
+ val = parseFloat(val);
124
+ }
125
+ }
126
+ sum += val;
127
+ }
128
+ }
129
+ if (sum != Math.floor(sum)) {
130
+ sum = sum.toFixed(2);
131
+ }
132
+ footer[minuend[i]] = sum;
133
+ _minuend = sum;
134
+ }
135
+
136
+ for (let i in subtrahend) {
137
+ let sum = 0;
138
+ for (let r in rows) {
139
+ if (rows[r].hasOwnProperty(subtrahend[i])) {
140
+ let val = rows[r][subtrahend[i]];
141
+ if (typeof val === "string") {
142
+ if (is_digit(val)) {
143
+ val = parseFloat(val);
144
+ }
145
+ }
146
+ sum += val;
147
+ }
148
+ }
149
+ if (sum != Math.floor(sum)) {
150
+ sum = sum.toFixed(2);
151
+ }
152
+ footer[subtrahend[i]] = sum;
153
+ _subtrahend = sum;
154
+ }
155
+
156
+ let diff = _minuend - _subtrahend;
157
+ if (no_negative) {
158
+ if (diff < 0) {
159
+ diff = 0;
160
+ }
161
+ }
162
+ footer[difference] = diff;
163
+
164
+ $self.jqGrid("footerData", "set", footer);
165
+
166
+ return footer;
167
+ }
168
+
169
+ /**
170
+ * Syncron Alias grid_ratio_on
171
+ @alias module:Jqgrid_utils
172
+ */
173
+ _grid_ratio_on(grid, fraction_col, denominator_col, ratio_col) {
174
+ return this.grid_ratio_on(grid, fraction_col, denominator_col, ratio_col);
175
+ }
176
+
177
+ /**
178
+ * Get the ratio the columns values together
179
+ @alias module:Jqgrid_utils
180
+ @param {object} - Grid Object (required)
181
+ @param {string} - Column/Field Name for value 1
182
+ @param {string} - Column/Field Name for value 2
183
+ @param {string} - Column/Field Name to the set the target ratio
184
+ @example
185
+ var jqu = new Jqgrid_utils({page:page});
186
+ gridComplete: function()
187
+ {
188
+ jqu._grid_ratio_on(this, 'actual_days', 'plan_days', 'qc_eta_ratio');
189
+ },
190
+
191
+ */
192
+ async grid_ratio_on(grid, fraction_col, denominator_col, ratio_col) {
193
+ var allrows = jQuery("#grid").jqGrid("getGridParam", "data");
194
+ let $self = jQuery(grid);
195
+ let rows = $self.jqGrid("getGridParam", "data");
196
+ let footer = {
197
+ invdate: "Total",
198
+ };
199
+ let sum = 0;
200
+ fraction_sum = 0;
201
+ denominator_sum = 0;
202
+ ratio_sum = 0;
203
+ for (let r in rows) {
204
+ if (rows[r].hasOwnProperty(fraction_col)) {
205
+ fraction_sum += rows[r][fraction_col];
206
+ }
207
+ if (rows[r].hasOwnProperty(denominator_col)) {
208
+ denominator_sum += rows[r][denominator_col];
209
+ }
210
+ }
211
+ footer["qc_eta_ratio"] = (fraction_sum / denominator_sum).toFixed(2);
212
+ $self.jqGrid("footerData", "set", footer);
213
+ return footer;
214
+ }
215
+
216
+ /**
217
+ * Check if the string is html
218
+ @alias module:Jqgrid_utils
219
+ @param {string} - String of any kind
220
+ @returns {boolean} - true or false
221
+ */
222
+ is_html(str) {
223
+ let r = false;
224
+ try {
225
+ const doc = new DOMParser().parseFromString(str, "text/html");
226
+ const a = Array.from(doc.body.childNodes).some((n) => n.nodeType === 1);
227
+ if (a) {
228
+ r = true;
229
+ }
230
+ } catch (err) {}
231
+ return r;
232
+ }
233
+
234
+ /**
235
+ * Syncron Alias grid_sum_on
236
+ @alias module:Jqgrid_utils
237
+ */
238
+ async _grid_sum_on(grid, fields = []) {
239
+ return await this.grid_sum_on(grid, fields);
240
+ }
241
+
242
+ /**
243
+ * Sum the columns values together
244
+ @alias module:Jqgrid_utils
245
+ @param {object} - Grid Object (required)
246
+ @param {string} - Column/Field Name to sum
247
+ @example
248
+ var jqu = new Jqgrid_utils({page:page});
249
+ gridComplete: function () {
250
+ jqu._jqu._grid_sum_on(this, [
251
+ "qty_icollect",
252
+ "qty_ordered",
253
+ "need_for_qty_ordered",
254
+ "wait_icollect",
255
+ ]);
256
+ },
257
+ */
258
+ async grid_sum_on(grid, fields = []) {
259
+ let $self = jQuery(grid);
260
+ let rows = $self.jqGrid("getGridParam", "data");
261
+ let footer = {
262
+ invdate: "Total",
263
+ };
264
+ for (let i in fields) {
265
+ let sum = 0;
266
+ for (let r in rows) {
267
+ let _val = 0;
268
+ if (rows[r].hasOwnProperty(fields[i])) {
269
+ let val = rows[r][fields[i]];
270
+ if (val) {
271
+ if (typeof val === "string") {
272
+ if (this.is_html(val)) {
273
+ const doc = new DOMParser().parseFromString(val, "text/html");
274
+ const _a = doc.querySelectorAll("a");
275
+ const a = Array.from(_a).map((a) => a.text);
276
+ if (a.length) {
277
+ const n = a[0].replace(",", "");
278
+ if (is_digit(n)) {
279
+ _val = parseFloat(n);
280
+ }
281
+ }
282
+ } else {
283
+ if (is_digit(val)) {
284
+ _val = parseFloat(val);
285
+ }
286
+ }
287
+ } else if (typeof val === "number") {
288
+ _val = val;
289
+ }
290
+ sum += _val;
291
+ }
292
+ }
293
+ }
294
+ //let number = new Intl.NumberFormat('en-En', { style: 'currency', currency: 'USD' }).format(sum);
295
+ if (sum != Math.floor(sum)) {
296
+ sum = sum.toFixed(2);
297
+ }
298
+ footer[fields[i]] = sum;
299
+ }
300
+ $self.jqGrid("footerData", "set", footer);
301
+ return footer;
302
+ }
303
+
304
+ /**
305
+ * Syncron Alias grid_avg_on
306
+ @alias module:Jqgrid_utils
307
+ */
308
+ _grid_avg_on(grid, fields = []) {
309
+ return this.grid_avg_on(grid, fields);
310
+ }
311
+
312
+ /**
313
+ * Average the column values together
314
+ @alias module:Jqgrid_utils
315
+ @param {object} - Grid Object (required)
316
+ @param {array} - Column/Field Names to where the average of each column should be calculated
317
+ @example
318
+ var jqu = new Jqgrid_utils({page:page});
319
+
320
+ gridComplete: function()
321
+ {
322
+ jqu._grid_avg_on(this, ['diff_plan_to_actual', 'days_early', 'days_late']);
323
+ },
324
+
325
+
326
+ */
327
+
328
+ async grid_avg_on(grid, fields = []) {
329
+ let $self = jQuery(grid);
330
+ let rows = $self.jqGrid("getGridParam", "data");
331
+ let count = 0;
332
+ let footer = {
333
+ invdate: "Total",
334
+ };
335
+ for (let i in fields) {
336
+ let sum = 0;
337
+ for (let r in rows) {
338
+ if (rows[r].hasOwnProperty(fields[i])) {
339
+ let val = rows[r][fields[i]];
340
+ if (typeof val === "string") {
341
+ if (is_digit(val)) {
342
+ val = parseFloat(val);
343
+ }
344
+ }
345
+ sum += val;
346
+ count++;
347
+ }
348
+ }
349
+ sum = sum / count;
350
+ if (sum != Math.floor(sum)) {
351
+ sum = sum.toFixed(2);
352
+ }
353
+ footer[fields[i]] = sum;
354
+ }
355
+ $self.jqGrid("footerData", "set", footer);
356
+ return footer;
357
+ }
358
+
359
+ /**
360
+ * Syncron Alias grid_percent_on
361
+ @alias module:Jqgrid_utils
362
+ */
363
+ _grid_percent_on(grid, obj) {
364
+ return grid_percent_on(grid, obj);
365
+ }
366
+
367
+ /**
368
+ * Percent the columns values together
369
+ @alias module:Jqgrid_utils
370
+ @param {object} - Grid Object (required)
371
+ @param {string} - Column/Field Name to sum
372
+ @example
373
+ var jqu = new Jqgrid_utils({page:page});
374
+ gridComplete: function () {
375
+ jqu._jqu._grid_sum_on(this, [
376
+ "qty_icollect",
377
+ "qty_ordered",
378
+ "need_for_qty_ordered",
379
+ "wait_icollect",
380
+ ]);
381
+ },
382
+ */
383
+ async grid_percent_on(grid, obj) {
384
+ let $self = jQuery(grid);
385
+ let rows = $self.jqGrid("getGridParam", "data");
386
+ let footer_sum = {};
387
+ let id = obj["id"] ? obj["id"] : "invate";
388
+ footer[id] = "Total";
389
+ let total = obj["total"];
390
+ let sums = JSON.parse(JSON.stringify(obj["percent"]));
391
+ sums.push(total);
392
+ for (let i in sums) {
393
+ let field = sums[i];
394
+ let sum = 0;
395
+ let _rows = 0;
396
+ for (let r in rows) {
397
+ if (rows[r].hasOwnProperty(field)) {
398
+ let val = rows[r][field];
399
+ if (typeof val === "string") {
400
+ if (is_digit(val)) {
401
+ val = parseFloat(val);
402
+ sum += val;
403
+ _rows++;
404
+ }
405
+ } else {
406
+ sum += val;
407
+ _rows++;
408
+ }
409
+ }
410
+ }
411
+ footer_sum[field] = sum;
412
+ }
413
+
414
+ for (let i in sums) {
415
+ let field = sums[i];
416
+ let percent = footer_sum[field] / (footer_sum[total] / 100);
417
+ percent = percent.toFixed(2);
418
+ footer[field] = percent + "%";
419
+ }
420
+ $self.jqGrid("footerData", "set", footer);
421
+ }
422
+
423
+ /**
424
+ * Takes the updated columns data and send it to your API post server
425
+ * loadComplete: async function() for the old record needs to be called, see example !
426
+ @alias module:Jqgrid_utils
427
+ @param {object} - Grid Object (required)
428
+ @param {string} - API URL like https://foo.com (required)
429
+ @param {array} - id list, ids from the column header (required)
430
+ @param {object} - settings - extra key:value to send to your server
431
+
432
+ @example
433
+ var jqu = new Jqgrid_utils({page:page});
434
+
435
+ ....},
436
+ loadComplete: async function()
437
+ {
438
+ $grid.jqGrid('setGridParam',{record_data:$grid.jqGrid("getGridParam").data});
439
+ },
440
+ afterSetRow: async function(row)
441
+ {
442
+ const _api = await get_api_url('sapir');
443
+ const api = _api + '/column2';
444
+ let info = {"msg":"failed"};
445
+ var jqu = new Jqgrid_utils();
446
+ info = await jqu.update_row_to_api($grid, api,['id'],row,{server : '232',db : 'sl_h',table : 'kpi',});
447
+ for(let i in info)
448
+ {
449
+ log.info(info[i]);
450
+ }
451
+ },
452
+ */
453
+
454
+ async update_row_to_api(_self, api = "", _ids = ["id"], row = {}, data) {
455
+ let self = this;
456
+ let infos = [];
457
+ let ids = {};
458
+ let values = {};
459
+ let changed = {};
460
+ const rd = _self.jqGrid("getGridParam", "record_data");
461
+
462
+ if (
463
+ api != "" &&
464
+ Object.keys(row).length > 0 &&
465
+ row.inputData.oper == "edit"
466
+ ) {
467
+ for (let i in rd) {
468
+ if (rd[i]["id"] === row.rowid) {
469
+ for (let ii in _ids) {
470
+ if (rd[i].hasOwnProperty(_ids[ii])) {
471
+ ids[_ids[ii]] = rd[i][_ids[ii]];
472
+ }
473
+ }
474
+
475
+ for (let iii in row.inputData) {
476
+ if (iii != "oper") {
477
+ if (Object.keys(ids).indexOf(iii) < 0) {
478
+ //console.log(iii);
479
+ //console.log(row.inputData[iii]);
480
+ //console.log(rd[i][iii]);
481
+ if (row.inputData[iii] != rd[i][iii]) {
482
+ changed[iii] = row.inputData[iii];
483
+ }
484
+ }
485
+ }
486
+ }
487
+ }
488
+ }
489
+
490
+ for (let i in changed) {
491
+ if (Object.keys(ids).indexOf(i) < 0 && i != "oper") {
492
+ const col_name = i;
493
+ let col_value = {};
494
+ col_value[col_name] = changed[i];
495
+ data["ids"] = ids;
496
+ data["values"] = col_value;
497
+ data["operator"] = "edit";
498
+ //console.log(data)
499
+ const info = await self.post_json(api, JSON.stringify(data));
500
+ infos.push(info);
501
+ }
502
+ }
503
+ } else if (
504
+ api != "" &&
505
+ Object.keys(row).length > 0 &&
506
+ row.inputData.oper == "add"
507
+ ) {
508
+ //console.log("...add");
509
+ for (let i in row.inputData) {
510
+ if (row.inputData[i] && i != "id" && i != "oper") {
511
+ values[i] = row.inputData[i];
512
+ }
513
+ }
514
+ data["ids"] = ids;
515
+ data["values"] = values;
516
+ data["operator"] = "add";
517
+ //console.log(data);
518
+ let info = await self.post_json(api, JSON.stringify(data));
519
+ infos.push(info);
520
+ }
521
+
522
+ return infos;
523
+ }
524
+
525
+ /**
526
+ * After Delete a Grid Row send to and DELETE REST Request
527
+ * You need to define loadComplete and afterDelRow
528
+ * The Grid data needs to be saved as record within loadComplete
529
+ @alias module:Jqgrid_utils
530
+ @param {object} - Grid Object (required)
531
+ @param {string} - API URL like https://foo.com (required)
532
+ @param {string} - the row id value from afterDelRow (required)
533
+ @param {array} - id list, ids from the column header colmodel (required)
534
+ @param {object} - settings - extra key:value to send to your server
535
+ @example
536
+
537
+ loadComplete: async function()
538
+ {
539
+ $grid.jqGrid('setGridParam',{record_data:$grid.jqGrid("getGridParam").data});
540
+ },
541
+ afterDelRow: async function(rowid)
542
+ {
543
+ const _api = await get_api_url('sapir');
544
+ const api = _api + '/column2';
545
+ let info = {"msg":"failed"};
546
+ var jqu = new Jqgrid_utils();
547
+
548
+ info = await jqu.delete_row_to_api($grid,api,rowid,['id'],{
549
+ server : '232',
550
+ db : 'sl_h',
551
+ table : 'kpi',
552
+ operator: 'delete',
553
+ });
554
+ log.info(JSON.parse(info).msg);
555
+ },
556
+
557
+
558
+
559
+
560
+
561
+ */
562
+
563
+ async delete_row_to_api(_self, api = "", rowid, _ids = [], data = {}) {
564
+ let info = { msg: "failed" };
565
+ let self = this;
566
+ let ids = [];
567
+ let values = {};
568
+ const rd = _self.jqGrid("getGridParam", "record_data");
569
+ for (let i in rd) {
570
+ if (rd[i]["id"] === rowid) {
571
+ for (let ii in _ids) {
572
+ if (rd[i].hasOwnProperty(_ids[ii])) {
573
+ values[_ids[ii]] = rd[i][_ids[ii]];
574
+ ids.push(_ids[ii]);
575
+ }
576
+ }
577
+ break;
578
+ }
579
+ }
580
+
581
+ if (api != "" && Object.keys(values).length == ids.length) {
582
+ data["ids"] = ids;
583
+ data["values"] = values;
584
+ //console.log(data);
585
+ info = await self.adelete_api(api, JSON.stringify(data));
586
+ }
587
+ return info;
588
+ }
589
+
590
+ /**
591
+ *Append and sperator based link column to the end of a row
592
+ @alias module:Jqgrid_utils
593
+ @param {object} - col_model of the grid
594
+ @param {string} - URL string
595
+ @param {string} - field value - the visible text of the anchor
596
+ @param {object} - base row key value - like {"name":'wiki','label':"Wiki"}
597
+ @param {string} - URL Attributes
598
+ @param {object} - keys and fields value to use
599
+ @example
600
+ var jqu = new Jqgrid_utils();
601
+ 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"});
602
+
603
+ */
604
+
605
+ async append_seperator_link_column(
606
+ col_model,
607
+ url,
608
+ field_value,
609
+ base,
610
+ attr = "",
611
+ keys,
612
+ ) {
613
+ url = url + "/";
614
+ let self = this;
615
+ base["formatter"] = function (cell_val, obj) {
616
+ let _cell_val = field_value;
617
+ if (typeof keys === "object") {
618
+ let pref = "";
619
+ for (let i in keys) {
620
+ let key = i;
621
+ let v = keys[i];
622
+ let key_val = obj.rowData[v];
623
+ if (key_val) {
624
+ if (key_val) {
625
+ if (key != "") {
626
+ pref += key + "" + "/" + encodeURIComponent(key_val) + "/";
627
+ } else {
628
+ pref += encodeURIComponent(key_val);
629
+ }
630
+ }
631
+ }
632
+ }
633
+ if (pref) {
634
+ if (pref.slice(-1) === "&" || pref.slice(-1) === "/") {
635
+ pref = pref.slice(0, -1);
636
+ }
637
+ cell_val =
638
+ "<a " + attr + 'href="' + url + pref + '"> ' + _cell_val + "</a>";
639
+ }
640
+ }
641
+
642
+ return cell_val;
643
+ };
644
+
645
+ col_model.push(base);
646
+
647
+ return col_model;
648
+ }
649
+
650
+ /**
651
+ * add textarea
652
+ @alias module:Jqgrid_utils
653
+ @param {object} - edittype like
654
+ @example
655
+ let col_model = JSON.parse(await aget_api(url + "/model"));
656
+ col_model = await jqu.add_edit(col_model, 'mon',{ edittype:'textarea', editoptions:{rows:6,cols:100} });
657
+ see for other inputfields:
658
+ http://www.trirand.com/blog/phpjqgrid/doc/_2v80w6oam.htm
659
+ */
660
+ async add_edit(col_model, edit_field, edittype, editoptions) {
661
+ for (let i = 0; i < col_model.length; i++) {
662
+ if (col_model[i]["name"] === edit_field) {
663
+ Object.assign(col_model[i], edittype);
664
+ Object.assign(col_model[i], editoptions);
665
+ }
666
+ }
667
+ return col_model;
668
+ }
669
+
670
+ /**
671
+ * add textarea
672
+ @alias module:Jqgrid_utils
673
+ @param {string} - edit_filed
674
+ @param {string} - style of the textaread
675
+ @example
676
+ let col_model = JSON.parse(await aget_api(url + "/model"));
677
+ col_model = await jqu.add_textarea(col_model, 'worker','style="width:100%;height:100px"');
678
+ */
679
+ async add_textarea(
680
+ col_model,
681
+ edit_field,
682
+ style = 'style="width:100%;height:100px"',
683
+ ) {
684
+ for (let i = 0; i < col_model.length; i++) {
685
+ if (col_model[i]["name"] === edit_field) {
686
+ col_model[i]["formatter"] = function (cell_val) {
687
+ const txt = "<textarea " + style + ">" + cell_val + "</textarea>";
688
+ return txt;
689
+ };
690
+ }
691
+ }
692
+ return col_model;
693
+ }
694
+
695
+ /**
696
+ * Get basic colModel data from raw data
697
+ @alias module:Jqgrid_utils
698
+ @param {array} - grid object
699
+ @param {array} - raw data object from loadComplete
700
+ @param {array} - list of columns to exclude (optional)
701
+ @param {array} - existing colModel (optional)
702
+ @returns {array} - col_model
703
+ @example
704
+ var jqu = new Jqgrid_utils();
705
+ ,loadComplete: async function(data)
706
+ {
707
+ let col_model = jQuery(this).jqGrid('getGridParam',"colModel");
708
+ 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);
709
+ jQuery(this).jqGrid('setGridParam',{colModel:new_col_model});
710
+ },
711
+ */
712
+ async get_col_model_from_data(obj, data, exclude = [], col_model = []) {
713
+ let cols = [];
714
+ for (let i in data) {
715
+ const keys = Object.keys(data[i]);
716
+ for (let ii in keys) {
717
+ const key = keys[ii];
718
+ cols.push(key);
719
+ }
720
+ }
721
+ cols = cols.filter((item, pos) => cols.indexOf(item) === pos);
722
+ let mcols = [];
723
+ for (let i in col_model) {
724
+ mcols.push(col_model[i]["name"]);
725
+ }
726
+
727
+ let diff = cols.filter((x) => !mcols.includes(x));
728
+ const _exclude = new Set(exclude);
729
+ diff = diff.filter((name) => {
730
+ return !_exclude.has(name);
731
+ });
732
+ diff.sort();
733
+
734
+ for (let x = 0; x < diff.length; x++) {
735
+ col_model.push({ name: diff[x], label: diff[x] });
736
+ }
737
+ //console.log(col_model);
738
+ return col_model;
739
+ }
740
+
741
+ /**
742
+ * Replace a Binaery 0 or 1 to other given value
743
+ @alias module:Jqgrid_utils
744
+ @param {string} - cell value
745
+ @param {string} - string replacement for 0
746
+ @param {string} - string replacement for 1
747
+ @example
748
+ var jqu = new Jqgrid_utils();
749
+ let _data = jqu.binery_replace(0,'zero','one');
750
+ or for column formatter
751
+ download_formatter:"var jqu = new Jqgrid_utils();jqu.binary_replace({0},'zero','one')"});
752
+ */
753
+ binary_replace(cell_value, a = "zero", b = "one") {
754
+ let value = a;
755
+ if (cell_value == 1 || cell_value == 0) {
756
+ if (cell_value == 1) {
757
+ value = b;
758
+ }
759
+ }
760
+ return value;
761
+ }
762
+
763
+ /**
764
+ * Convert a 112 date string to a DMY format with sepertaor - sync function
765
+ @alias module:Jqgrid_utils
766
+ @param {string} - date string
767
+ @param {string} - seperator used
768
+ @example
769
+ var jqu = new Jqgrid_utils();
770
+ let _data = jqu._date112_to_DMY('20220104','/');
771
+ console.log(_data);
772
+ */
773
+ _date112_to_DMY(cell_value, seperator = "/") {
774
+ let value = cell_value;
775
+ if (cell_value.length >= 8 && cell_value.indexOf(seperator) === -1) {
776
+ let a = [];
777
+ a.push(cell_value.substr(6, 2));
778
+ a.push(cell_value.substr(4, 2));
779
+ a.push(cell_value.substr(0, 4));
780
+ value = a.join(seperator);
781
+ }
782
+ return value;
783
+ }
784
+
785
+ /**
786
+ * Convert a 112 date to a DMY format with sepertaor
787
+ @alias module:Jqgrid_utils
788
+ @param {object} - col_model of the grid
789
+ @param {string} - name of the date 112 column what should get converted
790
+ @param {string} - seperator used
791
+ @example
792
+ var jqu = new Jqgrid_utils();
793
+ let _data = await jqu.date112_to_DMY(this,'field','/');
794
+ console.log(_data);
795
+ */
796
+ async date112_to_DMY(col_model, edit_field, seperator = "/") {
797
+ for (let i = 0; i < col_model.length; i++) {
798
+ if (col_model[i]["name"] === edit_field) {
799
+ col_model[i]["formatter"] = function (cell_value, o) {
800
+ if (cell_value) {
801
+ cell_value = cell_value.toString();
802
+ let value = cell_value;
803
+ if (
804
+ cell_value.length >= 8 &&
805
+ cell_value.indexOf(seperator) === -1
806
+ ) {
807
+ let a = [];
808
+ a.push(cell_value.substr(6, 2));
809
+ a.push(cell_value.substr(4, 2));
810
+ a.push(cell_value.substr(0, 4));
811
+ value = a.join(seperator);
812
+ }
813
+ return value;
814
+ } else {
815
+ return cell_value;
816
+ }
817
+ };
818
+ }
819
+ }
820
+ return col_model;
821
+ }
822
+
823
+ /**
824
+ * Add Formatter
825
+ @alias module:Jqgrid_utils
826
+ @param {array} - grid col_model
827
+ @param {string} - string columns names what will be formatted
828
+ @param {object} - formatter object like { formatter: "select", formatoptions: {value: "1:ok;0:fail", defaultValue: "1" }}
829
+ @example
830
+ var jqu = new Jqgrid_utils();
831
+ col_model = await jqu.add_formatter(col_model,'select',{ formatter: "select", formatoptions: {value: "1:ok;0:fail", defaultValue: "1" }})
832
+ */
833
+
834
+ async add_formatter(col_model, edit_field, formatter) {
835
+ for (let i = 0; i < col_model.length; i++) {
836
+ if (col_model[i]["name"] === edit_field) {
837
+ if (
838
+ formatter.hasOwnProperty("formatter") &&
839
+ formatter.hasOwnProperty("formatoptions")
840
+ ) {
841
+ col_model[i]["formatter"] = formatter["formatter"];
842
+ col_model[i]["formatoptions"] = formatter["formatoptions"];
843
+ col_model[i]["edittype"] = formatter["formatter"];
844
+ col_model[i]["editoptions"] = formatter["formatoptions"];
845
+ }
846
+ }
847
+ }
848
+ return col_model;
849
+ }
850
+
851
+ /**
852
+ * Natural Sort Column
853
+ @alias module:Jqgrid_utils
854
+ @param {array} - grid col_model
855
+ @param {string} - string columns names for natural sorting
856
+ @returns {array} - col_model
857
+ @example
858
+ var jqu = new Jqgrid_utils();
859
+ col_model = await jqu.natural_sort(col_model,'colunmename');
860
+ */
861
+
862
+ async natural_sort(col_model, column_name) {
863
+ for (let i = 0; i < col_model.length; i++) {
864
+ if (col_model[i]["name"] === column_name) {
865
+ col_model[i]["sortfunc"] = function (a, b, d) {
866
+ if (d === undefined) {
867
+ d = 1;
868
+ }
869
+ var re =
870
+ /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
871
+ sre = /(^[ ]*|[ ]*$)/g,
872
+ dre =
873
+ /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
874
+ hre = /^0x[0-9a-f]+$/i,
875
+ ore = /^0/,
876
+ i = function (s) {
877
+ return (self.insensitive && ("" + s).toLowerCase()) || "" + s;
878
+ },
879
+ // convert all to strings strip whitespace
880
+ x = i(a).replace(sre, "") || "",
881
+ y = i(b).replace(sre, "") || "",
882
+ // chunk/tokenize
883
+ xN = x
884
+ .replace(re, "\0$1\0")
885
+ .replace(/\0$/, "")
886
+ .replace(/^\0/, "")
887
+ .split("\0"),
888
+ yN = y
889
+ .replace(re, "\0$1\0")
890
+ .replace(/\0$/, "")
891
+ .replace(/^\0/, "")
892
+ .split("\0"),
893
+ // numeric, hex or date detection
894
+ xD =
895
+ parseInt(x.match(hre)) ||
896
+ (xN.length != 1 && x.match(dre) && Date.parse(x)),
897
+ yD =
898
+ parseInt(y.match(hre)) ||
899
+ (xD && y.match(dre) && Date.parse(y)) ||
900
+ null,
901
+ oFxNcL,
902
+ oFyNcL;
903
+ // first try and sort Hex codes or Dates
904
+ if (yD)
905
+ if (xD < yD) return -d;
906
+ else if (xD > yD) return d;
907
+ // natural sorting through split numeric strings and default strings
908
+ for (
909
+ var cLoc = 0, numS = Math.max(xN.length, yN.length);
910
+ cLoc < numS;
911
+ cLoc++
912
+ ) {
913
+ // find floats not starting with '0', string or 0 if not defined (Clint Priest)
914
+ oFxNcL =
915
+ (!(xN[cLoc] || "").match(ore) && parseFloat(xN[cLoc])) ||
916
+ xN[cLoc] ||
917
+ 0;
918
+ oFyNcL =
919
+ (!(yN[cLoc] || "").match(ore) && parseFloat(yN[cLoc])) ||
920
+ yN[cLoc] ||
921
+ 0;
922
+ // handle numeric vs string comparison - number < string - (Kyle Adams)
923
+ if (isNaN(oFxNcL) !== isNaN(oFyNcL)) {
924
+ return isNaN(oFxNcL) ? d : -d;
925
+ }
926
+ // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
927
+ else if (typeof oFxNcL !== typeof oFyNcL) {
928
+ oFxNcL += "";
929
+ oFyNcL += "";
930
+ }
931
+ if (oFxNcL < oFyNcL) return -d;
932
+ if (oFxNcL > oFyNcL) return d;
933
+ }
934
+ return 0;
935
+ };
936
+ }
937
+ }
938
+ return col_model;
939
+ }
940
+
941
+ /**
942
+ * Add HTML Formatter
943
+ @alias module:Jqgrid_utils
944
+ @param {array} - grid col_model
945
+ @param {string} - string columns names what will be converted to ok buttons
946
+ @param {string} - html tag code
947
+ @returns {array} - col_model
948
+ @example
949
+ var jqu = new Jqgrid_utils();
950
+ col_model = await jqu.add_html_formatter(col_model,'process',"<button tabindex='0' class='cellbtn' type='button'>Process</button>");
951
+ */
952
+
953
+ async add_html_formatter(col_model, edit_field, html) {
954
+ for (let i = 0; i < col_model.length; i++) {
955
+ if (col_model[i]["name"] === edit_field) {
956
+ col_model[i]["formatter"] = function (cell_val, o) {
957
+ return html;
958
+ };
959
+ }
960
+ }
961
+ return col_model;
962
+ }
963
+
964
+ /**
965
+ * Add an OK Button
966
+ @alias module:Jqgrid_utils
967
+ @param {array} - grid col_model
968
+ @param {array} - list of columns names what will be converted to ok buttons
969
+ @returns {array} - col_model
970
+ @example
971
+ var jqu = new Jqgrid_utils();
972
+ col_model = await jqu.add_ok_button(col_model, ['checked']);
973
+ */
974
+
975
+ async add_ok_button(col_model, fields) {
976
+ let self = this;
977
+ for (let i = 0; i < col_model.length; i++) {
978
+ if (fields.indexOf(col_model[i]["name"]) > -1) {
979
+ col_model[i]["formatter"] = function (cell_val) {
980
+ if (cell_val != undefined) {
981
+ return self.__cell_format(cell_val, "format_ok");
982
+ } else {
983
+ return "";
984
+ }
985
+ };
986
+ }
987
+ }
988
+ return col_model;
989
+ }
990
+
991
+ /**
992
+ * Get the filled cell data
993
+ @alias module:Jqgrid_utils
994
+ @param {object} - the grid object or its name
995
+ @param {array} - list of columns names what will be collected
996
+ @returns {array} - table array
997
+ @example
998
+ var jqu = new Jqgrid_utils();
999
+ col_model = await jqu.get_filled_cell_table_dat("#gridname","fieldname");
1000
+
1001
+ */
1002
+ async get_filled_cell_table_data(_grid, fields = []) {
1003
+ let d = jQuery(_grid).jqGrid("getGridParam", "data");
1004
+ let keys = fields;
1005
+ let _data = [];
1006
+ for (let i in d) {
1007
+ if (d[i].hasOwnProperty("id")) {
1008
+ let row = [d[i]["id"]];
1009
+ for (let x in keys) {
1010
+ if (d[i].hasOwnProperty(keys[x])) {
1011
+ row.push(d[i][keys[x]]);
1012
+ } else {
1013
+ row.push("");
1014
+ }
1015
+ }
1016
+ var f = row.filter(function (value, index, arr) {
1017
+ return value !== "";
1018
+ });
1019
+ if (Object.keys(f).length > 1) {
1020
+ _data.push(row);
1021
+ }
1022
+ }
1023
+ }
1024
+ return _data;
1025
+ }
1026
+
1027
+ /**
1028
+ * Get the filled cell data
1029
+ @alias module:Jqgrid_utils
1030
+ @param {object} - the grid object or its name
1031
+ @param {array} - list of columns names what will be collected
1032
+ @returns {object} - json object of the colleted fields
1033
+ @example
1034
+ var jqu = new Jqgrid_utils();
1035
+ let _data = await jqu.get_filled_cell_data(this,["P-","bulk","wholesale"]);
1036
+ */
1037
+
1038
+ async get_filled_cell_data(_grid, fields = []) {
1039
+ let d = jQuery(_grid).jqGrid("getGridParam", "data");
1040
+ let keys = fields;
1041
+ let _data = [];
1042
+ for (let i in d) {
1043
+ if (d[i].hasOwnProperty("id")) {
1044
+ let row = { id: d[i]["id"] };
1045
+ for (let x in keys) {
1046
+ if (d[i].hasOwnProperty(keys[x])) {
1047
+ if (d[i][keys[x]] != "") {
1048
+ row[keys[x]] = d[i][keys[x]];
1049
+ }
1050
+ }
1051
+ }
1052
+ if (Object.keys(row).length > 1) {
1053
+ _data.push(row);
1054
+ }
1055
+ }
1056
+ }
1057
+ return _data;
1058
+ }
1059
+
1060
+ /**
1061
+ * Add an URL from the data to a specific cell/column
1062
+ @alias module:Jqgrid_utils
1063
+ @param {object} - col_model of the grid
1064
+ @param {string} - name of the column what should get convert to the url
1065
+ @param {string} - the used url of the data
1066
+ @returns {object} https://foo.bar.com/av0_code/bar
1067
+ @example
1068
+ var jqu = new Jqgrid_utils();
1069
+ col_model = await jqu.set_link(col_model,'field_get_url','field_with_the_url','target="blank"');
1070
+ console.log(_data);
1071
+ */
1072
+ async set_link(col_model, edit_field, url, attr = "") {
1073
+ for (let i = 0; i < col_model.length; i++) {
1074
+ if (col_model[i]["name"] === edit_field) {
1075
+ col_model[i]["formatter"] = function (cell_val, o) {
1076
+ return (
1077
+ '<a class="gl" ' +
1078
+ attr +
1079
+ 'href="' +
1080
+ o.rowData[url] +
1081
+ '">' +
1082
+ cell_val +
1083
+ "</a>"
1084
+ );
1085
+ };
1086
+ }
1087
+ }
1088
+ return col_model;
1089
+ }
1090
+
1091
+ /**
1092
+ * Hide all columns execpt column
1093
+ * @alias module:Jqgrid_utils
1094
+ * @param {object} - col_model of the grid
1095
+ * @param {array} - array of names to not to hide
1096
+ * @returns {object} col_model
1097
+ * @example
1098
+ col_model = await jqu.hide_all_columns_except(col_model,['supplier','customer']);
1099
+ */
1100
+ async hide_all_columns_except(col_model, fields) {
1101
+ for (let i = 0; i < col_model.length; i++) {
1102
+ if (fields.indexOf(col_model[i]["name"]) > -1) {
1103
+ col_model[i]["hidden"] = false;
1104
+ } else {
1105
+ col_model[i]["hidden"] = true;
1106
+ }
1107
+ }
1108
+ return col_model;
1109
+ }
1110
+
1111
+ /**
1112
+ * Hide a col_model column before load the grid
1113
+ * @alias module:Jqgrid_utils
1114
+ * @param {object} - col_model of the grid
1115
+ * @param {string} - name of the column to hide
1116
+ * @returns {object} col_model
1117
+ * @example
1118
+ col_model = await jqu.hide_column(col_model,'wholesale');
1119
+ col_model = await jqu.hide_column(col_model,'wholesale_formula');
1120
+ */
1121
+ async hide_column(col_model, field) {
1122
+ for (let i = 0; i < col_model.length; i++) {
1123
+ if (col_model[i]["name"] === field) {
1124
+ col_model[i]["hidden"] = true;
1125
+ }
1126
+ }
1127
+ return col_model;
1128
+ }
1129
+
1130
+ /**
1131
+ @alias module:Jqgrid_utils
1132
+ @param {object} - gridobject;
1133
+ @param {object} - grid data (optional);
1134
+ @example
1135
+ var jqu = new Jqgrid_utils();
1136
+ loadComplete: function(){
1137
+ jqu.grid_set_caption(this);
1138
+ },
1139
+ */
1140
+
1141
+ s_grid_set_caption(_grid, data = []) {
1142
+ this.grid_set_captionn(_grid, (data = []));
1143
+ }
1144
+
1145
+ /**
1146
+ Adding the row count number to the caption
1147
+ @alias module:Jqgrid_utils
1148
+ @param {object} - gridobject;
1149
+ @param {object} - grid data (optional);
1150
+ @example
1151
+ var jqu = new Jqgrid_utils();
1152
+ loadComplete: function(){
1153
+ await jqu.grid_set_caption(this);
1154
+ },
1155
+ */
1156
+ async grid_set_caption(_grid, data = []) {
1157
+ if (_grid) {
1158
+ const grid = jQuery(_grid);
1159
+ let count = 0;
1160
+ if (data.length === 0) {
1161
+ count = grid.jqGrid("getGridParam", "records");
1162
+ } else {
1163
+ count = data.length;
1164
+ }
1165
+ let caption = grid.jqGrid("getGridParam", "caption");
1166
+ const reg = /\d.*x/;
1167
+ const new_caption = caption.replace(reg, "");
1168
+ grid.jqGrid("setCaption", new_caption + " " + count + "x");
1169
+ }
1170
+ }
1171
+
1172
+ /**
1173
+ @alias module:Jqgrid_utils
1174
+ @param {object} - the col_model of the grid
1175
+ @param {string} - the name of the page(optional)
1176
+ @param {object} - the grid objec(optional)
1177
+ @example
1178
+ col_model = await jqu.resize_saved_cell_width(col_model);
1179
+ */
1180
+ s_resize_saved_cell_width(col_model, page = false, grid = false) {
1181
+ this.grid_set_caption(col_model, page, grid);
1182
+ }
1183
+
1184
+ /**
1185
+ @alias module:Jqgrid_utils
1186
+ @param {object} - the col_model of the grid
1187
+ @param {string} - the name of the page(optional)
1188
+ @param {object} - the grid objec(optional)
1189
+ @example
1190
+ col_model = await jqu.resize_saved_cell_width(col_model);
1191
+ */
1192
+ async resize_saved_cell_width(col_model, page = false, grid = false) {
1193
+ let key = page ? page : this.page;
1194
+ key += grid ? "-" + grid + "-w-" : "-grid-w-";
1195
+ for (let x = 0; x <= col_model.length; x++) {
1196
+ if (col_model[x]) {
1197
+ if (col_model[x]["name"]) {
1198
+ const name = col_model[x]["name"];
1199
+ const width = localStorage.getItem(key + name);
1200
+ if (width) {
1201
+ col_model[x]["width"] = width;
1202
+ }
1203
+ }
1204
+ }
1205
+ }
1206
+ return col_model;
1207
+ }
1208
+
1209
+ /**
1210
+ @alias module:Jqgrid_utils
1211
+ @param {string} the width of the resized column
1212
+ @param {string} column number what get resized
1213
+ @param {string} not in use yet
1214
+ @example
1215
+ * var jqu = new Jqgrid_utils({page:'mypage'});
1216
+ * resizeStop: jqu.resize_cell,
1217
+ */
1218
+ resize_cell(width, index, _page = false) {
1219
+ const col_model = jQuery(this).jqGrid("getGridParam", "colModel");
1220
+ if (col_model[index]) {
1221
+ if (col_model[index]["name"]) {
1222
+ const name = col_model[index]["name"];
1223
+ const page = _page ? _page : localStorage.getItem("page");
1224
+ const grid = this.id;
1225
+ let key = page + "-" + grid + "-w-" + name;
1226
+ localStorage.setItem(key, width);
1227
+ const cat = localStorage.getItem(key);
1228
+ }
1229
+ }
1230
+ }
1231
+
1232
+ /**
1233
+ * Upsert(insert or update) from the grid to an API
1234
+ @alias module:Jqgrid_utils
1235
+ @param {object} - row object
1236
+ @param {string} - url of the API
1237
+ @param {string} - data oject
1238
+ @returns {object} {update: 'ok'} or {update: 'failed'}
1239
+ @example
1240
+ var jqu = new Jqgrid_utils();
1241
+ afterSetRow: async function(row)
1242
+ {
1243
+ let r = await jqu.upsert_row(row, 'http://api.com',{'key':'value'});
1244
+ console.log(r);
1245
+ },
1246
+ */
1247
+ async upsert_row(row, url, req = {}) {
1248
+ if (row.rowid.startsWith("jqg")) {
1249
+ const r0 = await this.insert_row(row, url);
1250
+ return r0;
1251
+ } else {
1252
+ const r1 = await this.update_row(row, url);
1253
+ return r1;
1254
+ }
1255
+ }
1256
+
1257
+ /**
1258
+ * Insert from the grid to an API used by the upsert_row function
1259
+ @alias module:Jqgrid_utils
1260
+ @param {object} - row object
1261
+ @param {string} - URL of the API
1262
+ @returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
1263
+ @example
1264
+ var jqu = new Jqgrid_utils();
1265
+ afterSetRow: async function(row)
1266
+ {
1267
+ let r = await jqu.insert_row(row, 'http://api.com');
1268
+ console.log(r);
1269
+ },
1270
+ */
1271
+ async insert_row(row, url) {
1272
+ let req = {};
1273
+ let ret = "";
1274
+ if (row.inputData.hasOwnProperty("id")) {
1275
+ req["_id"] = "id";
1276
+ req["_id_val"] = row.inputData["id"];
1277
+ for (let i in row.inputData) {
1278
+ req[i] = row.inputData[i];
1279
+ }
1280
+ delete req["id"];
1281
+ delete req["oper"];
1282
+ //console.log(req);
1283
+ ret = await this.put_json(url, JSON.stringify(req));
1284
+ }
1285
+ return ret;
1286
+ }
1287
+
1288
+ /**
1289
+ * Update from the grid to an API used by the upsert_row function
1290
+ @alias module:Jqgrid_utils
1291
+ @param {object} - row object
1292
+ @param {string} - url of the API
1293
+ @param {string} - data oject
1294
+ @returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
1295
+ @example
1296
+ var jqu = new Jqgrid_utils();
1297
+ afterSetRow: async function(row)
1298
+ {
1299
+ let r = await jqu.update_row(row, 'http://api.com',{'key':value});
1300
+ console.log(r);
1301
+ },
1302
+ */
1303
+
1304
+ async update_row(row, url, req = {}) {
1305
+ let ret = "";
1306
+ {
1307
+ if (!req["_id"]) {
1308
+ req["_id"] = "id";
1309
+ }
1310
+ req["_id_val"] = row.inputData["id"];
1311
+ for (let i in row.inputData) {
1312
+ req[i] = row.inputData[i];
1313
+ }
1314
+ delete req["id"];
1315
+ delete req["oper"];
1316
+ ret = await this.post_json(url, JSON.stringify(req));
1317
+ }
1318
+ return ret;
1319
+ }
1320
+
1321
+ /**
1322
+ * Delete from the grid to an API
1323
+ @alias module:Jqgrid_utils
1324
+ @param {string} - row id
1325
+ @param {string} - url of the API
1326
+ @returns {object} @returns {object} Object from the the API like {delete: 'ok'} or {delete: 'failed'}
1327
+ @example
1328
+ var jqu = new Jqgrid_utils();
1329
+ afterDelRow: async function(row)
1330
+ {
1331
+ const r = await jqu.delete_row('id', 'http://api.com');
1332
+ console.log(r + ' : ' + row + ' - from API');
1333
+ },
1334
+ */
1335
+
1336
+ async delete_row(_id, url) {
1337
+ let ret = "";
1338
+ if (url.indexOf("?") > -1) {
1339
+ url += "&_id=" + encodeURIComponent(unescape(_id));
1340
+ } else {
1341
+ url += "?_id=" + encodeURIComponent(unescape(_id));
1342
+ }
1343
+
1344
+ ret = JSON.parse(await this.adelete_api(url));
1345
+ return ret["message"];
1346
+ }
1347
+
1348
+ /**
1349
+ * Async Delete request used by function delete_row
1350
+ @alias module:Jqgrid_utils
1351
+ @param {string} - url of the API
1352
+ @param {boalan} - header should be json type? default form type
1353
+ @returns {object} @returns {object} Object from the the API like {delete: 'ok'} or {delete: 'failed'}
1354
+ @example
1355
+ var jqu = new Jqgrid_utils();
1356
+ afterDelRow: async function(row)
1357
+ {
1358
+ ret = JSON.parse(await jqu.adelete_api(url));
1359
+ },
1360
+ */
1361
+
1362
+ async adelete_api(url, json = false) {
1363
+ let ctype = "application/x-www-form-urlencoded";
1364
+ let body = null;
1365
+ if (json) {
1366
+ ctype = "application/json;charset=UTF-8";
1367
+ body = json;
1368
+ }
1369
+ return new Promise((resolve, reject) => {
1370
+ let xhr = new XMLHttpRequest();
1371
+ xhr.open("DELETE", url);
1372
+ xhr.setRequestHeader("Content-type", ctype);
1373
+ xhr.onload = () => resolve(xhr.responseText);
1374
+ xhr.onerror = () => reject(xhr.statusText);
1375
+ xhr.send(body);
1376
+ });
1377
+ }
1378
+
1379
+ /**
1380
+ * Async Post request used by the update_row function
1381
+ @alias module:Jqgrid_utils
1382
+ @param {string} - url of the API
1383
+ @param {object} - json object
1384
+ @returns {object} @returns {object} Object from the the API like {update: 'ok'} or {update: 'failed'}
1385
+ @example
1386
+ var jqu = new Jqgrid_utils();
1387
+ ret = JSON.parse(await jqu.post_json(url,{'key':value,'key2':'value'}));
1388
+ */
1389
+
1390
+ async post_json(url, data) {
1391
+ return new Promise((resolve, reject) => {
1392
+ let xhr = new XMLHttpRequest();
1393
+ xhr.open("POST", url);
1394
+ xhr.setRequestHeader("Content-type", "application/json");
1395
+ xhr.onload = () => resolve(xhr.responseText);
1396
+ xhr.onerror = () => reject(xhr.statusText);
1397
+ xhr.send(data);
1398
+ });
1399
+ }
1400
+
1401
+ /**
1402
+ * Async Put request used by the insert_row function
1403
+ @alias module:Jqgrid_utils
1404
+ @param {string} - url of the API
1405
+ @param {object} - json object
1406
+ @returns {object} @returns {object} Object from the the API like {insert: 'ok'} or {insert: 'failed'}
1407
+ @example
1408
+ var jqu = new Jqgrid_utils();
1409
+ ret = JSON.parse(await jqu.put_json(url,{'key':value,'key2':'value2'}));
1410
+ */
1411
+ async put_json(url, data) {
1412
+ return new Promise((resolve, reject) => {
1413
+ let xhr = new XMLHttpRequest();
1414
+ xhr.open("PUT", url);
1415
+ xhr.setRequestHeader("Content-type", "application/json");
1416
+ xhr.onload = () => resolve(xhr.responseText);
1417
+ xhr.onerror = () => reject(xhr.statusText);
1418
+ xhr.send(data);
1419
+ });
1420
+ }
1421
+
1422
+ /**
1423
+ * Hide the del iconf rom the grid
1424
+ @alias module:Jqgrid_utils
1425
+ @example
1426
+ var jqu = new Jqgrid_utils();
1427
+ jqu.hide_del_icon();
1428
+ */
1429
+ s_hide_del_icon() {
1430
+ hide_del_icon();
1431
+ }
1432
+
1433
+ /**
1434
+ * Hide the del iconf rom the grid
1435
+ @alias module:Jqgrid_utils
1436
+ @example
1437
+ var jqu = new Jqgrid_utils();
1438
+ await jqu.hide_del_icon();
1439
+ */
1440
+ async hide_del_icon() {
1441
+ jQuery(".ui-inline-del").each(function (index) {
1442
+ jQuery(this).html("");
1443
+ });
1444
+ }
1445
+
1446
+ /**
1447
+ * Convert a cell into a link/url with data from another cell and spit the value by comma - CSV
1448
+ @alias module:Jqgrid_utils
1449
+ @param {object} - col_model of the grid
1450
+ @param {string} - URL string
1451
+ @param {string} - Column/Cell to use
1452
+ @param {string} - URL Attributes
1453
+ @param {object} - keys and fields value to use
1454
+ @param {object} - format info
1455
+ @param {string} - seperator of the cell value to split (default is comma)
1456
+ @example
1457
+ var jqu = new Jqgrid_utils();
1458
+ col_model = await jqu.add_link_details_csv(col_model, host + '/html/report.html' , 'tags','target="_blank"',{"tags":"tags"},',');
1459
+
1460
+ */
1461
+ async add_link_details_csv(
1462
+ col_model,
1463
+ url,
1464
+ edit_field,
1465
+ attr = "",
1466
+ keys,
1467
+ format,
1468
+ seperator = ",",
1469
+ ) {
1470
+ let self = this;
1471
+ if (url.indexOf("?") > -1) {
1472
+ url = url + "&";
1473
+ } else {
1474
+ url = url + "?";
1475
+ }
1476
+
1477
+ for (let i = 0; i < col_model.length; i++) {
1478
+ if (col_model[i]["name"] === edit_field) {
1479
+ col_model[i]["formatter"] = function (cell_val, obj) {
1480
+ let key_val = cell_val;
1481
+ const _cell_val = self.__cell_format(cell_val, format);
1482
+ const a = _cell_val.split(seperator);
1483
+ let cell_value = "";
1484
+ for (let x in a) {
1485
+ const x_value = a[x].trim();
1486
+ if (x_value) {
1487
+ if (typeof keys === "object") {
1488
+ let pref = "";
1489
+ for (let ii in keys) {
1490
+ let key = ii;
1491
+ let v = keys[ii];
1492
+ key_val = obj.rowData[v];
1493
+ if (key_val) {
1494
+ if (key.indexOf("=") !== -1) {
1495
+ pref =
1496
+ pref +
1497
+ "" +
1498
+ key +
1499
+ "" +
1500
+ encodeURIComponent(x_value) +
1501
+ "&";
1502
+ } else {
1503
+ pref =
1504
+ pref +
1505
+ "" +
1506
+ key +
1507
+ "=" +
1508
+ encodeURIComponent(x_value) +
1509
+ "&";
1510
+ }
1511
+ }
1512
+ }
1513
+ if (pref.slice(-1) === "&") {
1514
+ pref = pref.slice(0, -1);
1515
+ }
1516
+ cell_value +=
1517
+ "<a " +
1518
+ attr +
1519
+ 'href="' +
1520
+ url +
1521
+ pref +
1522
+ '"> ' +
1523
+ x_value +
1524
+ "</a>" +
1525
+ seperator +
1526
+ " ";
1527
+ }
1528
+ }
1529
+ cell_val = cell_value.trim();
1530
+ if (cell_val.slice(-1) === seperator) {
1531
+ //remove last seperator
1532
+ cell_val = cell_val.slice(0, -1);
1533
+ }
1534
+ }
1535
+
1536
+ if (cell_val) {
1537
+ return cell_val;
1538
+ } else {
1539
+ return _cell_val;
1540
+ }
1541
+ };
1542
+ }
1543
+ }
1544
+
1545
+ return col_model;
1546
+ }
1547
+
1548
+ /**
1549
+ * Compare 2 columns and give them a style class when they have different content
1550
+ * http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods
1551
+ @alias module:Jqgrid_utils
1552
+ @param {object} - grid object
1553
+ @param {string} - first column
1554
+ @param {string} - second column
1555
+ @param {string} - css class name
1556
+ @example
1557
+ loadComplete: async function()
1558
+ {
1559
+ await jqu.compare(this,'first_column','second_column','redlight');
1560
+ }
1561
+
1562
+ */
1563
+ async compare(obj, column1, column2, css_class) {
1564
+ const rows = jQuery(obj).jqGrid("getGridParam", "data");
1565
+ for (let i in rows) {
1566
+ if (rows[i][column1] != rows[i][column2]) {
1567
+ jQuery(obj).jqGrid("setCell", rows[i]["id"], column1, "", css_class);
1568
+ jQuery(obj).jqGrid("setCell", rows[i]["id"], column2, "", css_class);
1569
+ }
1570
+ }
1571
+ }
1572
+
1573
+ /**
1574
+ * Compare 2 columns for smaller and give them a style class
1575
+ * http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods
1576
+ @alias module:Jqgrid_utils
1577
+ @param {object} - grid object
1578
+ @param {string} - first column
1579
+ @param {string} - second column
1580
+ @param {string} - css class name
1581
+ @example
1582
+ loadComplete: async function()
1583
+ {
1584
+ await jqu.compare(this,'first_column','second_column','redlight');
1585
+ }
1586
+
1587
+ */
1588
+ async compare_smaller(obj, column1, column2, css_class) {
1589
+ const rows = jQuery(obj).jqGrid("getGridParam", "data");
1590
+ for (let i in rows) {
1591
+ if (rows[i][column1] < rows[i][column2]) {
1592
+ jQuery(obj).jqGrid("setCell", rows[i]["id"], column1, "", css_class);
1593
+ jQuery(obj).jqGrid("setCell", rows[i]["id"], column2, "", css_class);
1594
+ }
1595
+ }
1596
+ }
1597
+
1598
+ /**
1599
+ * Compare 2 columns for bigger and give them a style class
1600
+ * http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods
1601
+ @alias module:Jqgrid_utils
1602
+ @param {object} - grid object
1603
+ @param {string} - first column
1604
+ @param {string} - second column
1605
+ @param {string} - css class name
1606
+ @example
1607
+ loadComplete: async function()
1608
+ {
1609
+ await jqu.compare(this,'first_column','second_column','redlight');
1610
+ }
1611
+
1612
+ */
1613
+ async compare_bigger(obj, column1, column2, css_class) {
1614
+ const rows = jQuery(obj).jqGrid("getGridParam", "data");
1615
+ for (let i in rows) {
1616
+ if (rows[i][column1] > rows[i][column2]) {
1617
+ jQuery(obj).jqGrid("setCell", rows[i]["id"], column1, "", css_class);
1618
+ jQuery(obj).jqGrid("setCell", rows[i]["id"], column2, "", css_class);
1619
+ }
1620
+ }
1621
+ }
1622
+
1623
+ /**
1624
+ * Set styles to individual cells, what are defined in a dedicated column
1625
+ @alias module:Jqgrid_utils
1626
+ @param {object} - grid object
1627
+ @param {string} - name of the column what includes the style values what need to be in a strinify json format
1628
+ @example
1629
+ var jqu = new Jqgrid_utils();
1630
+ loadComplete: async function() {
1631
+ await jqu.set_styles(this);
1632
+ },
1633
+ */
1634
+ async set_styles(obj, style_column = "styles") {
1635
+ const rows = jQuery(obj).jqGrid("getGridParam", "data");
1636
+
1637
+ for (let i in rows) {
1638
+ if (rows[i][style_column]) {
1639
+ const styles = JSON.parse(rows[i][style_column]);
1640
+ for (let ii in styles) {
1641
+ const rowid = rows[i]["id"];
1642
+ const name = ii;
1643
+ if (rows[i].hasOwnProperty(name)) {
1644
+ jQuery(obj).jqGrid("setCell", rowid, name, "", styles[ii]);
1645
+ }
1646
+ }
1647
+ }
1648
+ }
1649
+ }
1650
+
1651
+ /**
1652
+ * Set a class to a row, what must be defined in a dedicated column called row.class
1653
+ * Once the grid is loaded, the functions adds extra class to the row element
1654
+ @alias module:Jqgrid_utils
1655
+ @param {object} - grid id like #grid
1656
+ @example
1657
+ var jqu = new Jqgrid_utils();
1658
+ loadComplete: async function() {
1659
+ await jqu.set_classes("#grid");
1660
+ },
1661
+ */
1662
+ async set_classes(grid_id, field) {
1663
+ const rows = jQuery(grid_id).getDataIDs();
1664
+ for (var i = 0; i < rows.length; i = i + 1) {
1665
+ const row = jQuery(grid_id).getRowData(rows[i]);
1666
+ var e = jQuery("#" + rows[i], jQuery(grid_id));
1667
+ e.removeClass("ui-widget-content");
1668
+ e.addClass(row.class);
1669
+ }
1670
+ }
1671
+
1672
+ /**
1673
+ * Convert a cell into a link/url with data from another cell
1674
+ @alias module:Jqgrid_utils
1675
+ @param {object} - col_model of the grid
1676
+ @param {string} - URL string
1677
+ @param {string} - Column/Cell to use
1678
+ @param {string} - URL Attributes
1679
+ @param {object} - keys and fields value to use
1680
+ @example
1681
+ var jqu = new Jqgrid_utils();
1682
+ col_model = await jqu.add_link_details(col_model,'http://foo.bar' , 'style','target="_blank"',{'key':'style'});
1683
+ col_model = await jqu.add_link_details(col_model, host + '/html/table_size.html' , 'database','target="_blank"',{"database":"database","server":"server"});
1684
+ */
1685
+ async add_link_details(col_model, url, edit_field, attr = "", keys, format) {
1686
+ let self = this;
1687
+ if (url.indexOf("?") > -1) {
1688
+ url = url + "&";
1689
+ } else {
1690
+ url = url + "?";
1691
+ }
1692
+
1693
+ for (let i = 0; i < col_model.length; i++) {
1694
+ if (col_model[i]["name"] === edit_field) {
1695
+ col_model[i]["formatter"] = function (cell_val, obj) {
1696
+ cell_val = String(cell_val);
1697
+ let key_val = "";
1698
+ if (cell_val) {
1699
+ key_val = cell_val;
1700
+ }
1701
+ let t = "";
1702
+ if (cell_val) {
1703
+ t = cell_val.toString();
1704
+ }
1705
+
1706
+ if (typeof keys === "object") {
1707
+ let pref = "";
1708
+ for (let ii in keys) {
1709
+ let key = ii;
1710
+ let v = keys[ii];
1711
+ key_val = obj.rowData[v];
1712
+ if (key_val) {
1713
+ if (key_val) {
1714
+ if (key.indexOf("=") !== -1) {
1715
+ pref =
1716
+ pref + "" + key + "" + encodeURIComponent(key_val) + "&";
1717
+ } else {
1718
+ pref =
1719
+ pref + "" + key + "=" + encodeURIComponent(key_val) + "&";
1720
+ }
1721
+ }
1722
+ }
1723
+ }
1724
+ if (pref) {
1725
+ if (pref.slice(-1) === "&") {
1726
+ pref = pref.slice(0, -1);
1727
+ }
1728
+ const _cell_val = self.__cell_format(cell_val, format);
1729
+
1730
+ if (t != "" && _cell_val && t) {
1731
+ cell_val =
1732
+ "<a " +
1733
+ attr +
1734
+ 'href="' +
1735
+ url +
1736
+ pref +
1737
+ '"> ' +
1738
+ _cell_val +
1739
+ "</a>";
1740
+ } else {
1741
+ cell_val = "";
1742
+ }
1743
+ }
1744
+ }
1745
+ if (t) {
1746
+ return cell_val;
1747
+ } else {
1748
+ return "";
1749
+ }
1750
+ };
1751
+ }
1752
+ }
1753
+
1754
+ return col_model;
1755
+ }
1756
+
1757
+ /**
1758
+ * Convert a cell into seperated based link/url like https://foo.bar.com/field/value/field/value
1759
+ @alias module:Jqgrid_utils
1760
+ @param {object} - col_model of the grid
1761
+ @param {string} - URL string
1762
+ @param {string} - Column/Cell to use
1763
+ @param {string} - URL Attributes
1764
+ @param {object} - keys and fields value to use
1765
+ @example
1766
+ var jqu = new Jqgrid_utils();
1767
+ col_model = await jqu.add_link_details_separator(col_model, url1 , 'style','target="_blank"',{"pricelist":"pricelist","style":"style"});
1768
+ col_model = await jqu.add_link_details_separator(col_model, 'https://foo.com' , 'target_column','target="_blank"',{"mykey":"myval"});
1769
+ */
1770
+ async add_link_details_separator(
1771
+ col_model,
1772
+ url,
1773
+ edit_field,
1774
+ attr = "",
1775
+ keys,
1776
+ format,
1777
+ ) {
1778
+ url = url + "/";
1779
+ let self = this;
1780
+ for (let i = 0; i < col_model.length; i++) {
1781
+ if (col_model[i]["name"] === edit_field) {
1782
+ col_model[i]["formatter"] = function (cell_val, obj) {
1783
+ let key_val = cell_val;
1784
+
1785
+ if (typeof keys === "object") {
1786
+ let pref = "";
1787
+ for (let ii in keys) {
1788
+ let key = ii;
1789
+ let v = keys[ii];
1790
+ key_val = obj.rowData[v];
1791
+ if (key_val) {
1792
+ if (key_val) {
1793
+ if (key != "") {
1794
+ pref += key + "" + "/" + encodeURIComponent(key_val) + "/";
1795
+ } else {
1796
+ pref += encodeURIComponent(key_val);
1797
+ }
1798
+ }
1799
+ }
1800
+ }
1801
+ if (pref) {
1802
+ if (pref.slice(-1) === "&" || pref.slice(-1) === "/") {
1803
+ pref = pref.slice(0, -1);
1804
+ }
1805
+ const _cell_val = self.__cell_format(cell_val, format);
1806
+ cell_val =
1807
+ "<a " +
1808
+ attr +
1809
+ 'href="' +
1810
+ url +
1811
+ pref +
1812
+ '"> ' +
1813
+ _cell_val +
1814
+ "</a>";
1815
+ }
1816
+ }
1817
+
1818
+ return cell_val;
1819
+ };
1820
+ }
1821
+ }
1822
+
1823
+ return col_model;
1824
+ }
1825
+
1826
+ /**
1827
+ * Convert a cell into seperated based link/url include parameter based url like https://foo.bar.com/field.html?k=v
1828
+ @alias module:Jqgrid_utils
1829
+ @param {object} - col_model of the grid
1830
+ @param {string} - URL string
1831
+ @param {array} - array of dict
1832
+ @param {string} - URL Attributes
1833
+ @example
1834
+ var jqu = new Jqgrid_utils();
1835
+ col_model = await jqu.add_link_separator(col_model, host + '/html' , 'style',[
1836
+ {
1837
+ 'field':'pricelist',
1838
+ 'extension':'.html',
1839
+ 'fields':{'style':'style'}
1840
+ }
1841
+ ]);
1842
+ //other example
1843
+ col_model = await jqu.add_link_separator(col_model, 'https://wiki.salamander-jewelry.net/index.php/grid_loss' , 'e',[{'field':'e'}],'target="_blank"');
1844
+
1845
+ */
1846
+ async add_link_separator(col_model, url, edit_field, fields, attr = "") {
1847
+ url = url + "/";
1848
+ let self = this;
1849
+ for (let i = 0; i < col_model.length; i++) {
1850
+ if (col_model[i]["name"] === edit_field) {
1851
+ col_model[i]["formatter"] = function (cell_val, obj) {
1852
+ let key_val = cell_val;
1853
+ let pref = "";
1854
+ for (let x in fields) {
1855
+ for (let xx in fields[x]) {
1856
+ if (xx === "field") {
1857
+ let field_value = obj.rowData[fields[x][xx]];
1858
+ pref += field_value;
1859
+ }
1860
+ if (xx === "extension") {
1861
+ pref += fields[x][xx];
1862
+ }
1863
+ if (xx === "fields") {
1864
+ pref += "?";
1865
+ for (let key in fields[x][xx]) {
1866
+ let val = obj.rowData[fields[x][xx][key]];
1867
+ pref = pref + "" + key + "=" + encodeURIComponent(val) + "&";
1868
+ }
1869
+ }
1870
+ }
1871
+ }
1872
+ if (pref) {
1873
+ if (pref.slice(-1) === "&" || pref.slice(-1) === "/") {
1874
+ pref = pref.slice(0, -1);
1875
+ }
1876
+ cell_val =
1877
+ "<a " + attr + 'href="' + url + pref + '"> ' + cell_val + "</a>";
1878
+ }
1879
+
1880
+ return cell_val;
1881
+ };
1882
+ }
1883
+ }
1884
+ return col_model;
1885
+ }
1886
+
1887
+ /**
1888
+ * Private Function
1889
+ @alias module:Jqgrid_utils
1890
+ */
1891
+ __cell_format(cell_value, format) {
1892
+ if (format == "format_ok") {
1893
+ if (cell_value == 0 || cell_value === "fail") {
1894
+ cell_value =
1895
+ '<i data-check="failed" class="fa fa-times-circle" aria-hidden="true" style="color:#ff0000;"></i>';
1896
+ } else {
1897
+ cell_value =
1898
+ '<i data-check="ok" class="fa fa-check-circle" aria-hidden="true" style="color:#008000;"></i>';
1899
+ }
1900
+ }
1901
+ return cell_value;
1902
+ }
1903
+
1904
+ /**
1905
+ @alias module:Jqgrid_utils
1906
+ @param {string} - row_id
1907
+ @param {string} - data id
1908
+ @param {string} - url to request
1909
+ @param {object} - col_model for the table
1910
+ @param {string} - Add to the caption of the subgrid
1911
+ @example
1912
+ subGrid: true,
1913
+ ,subGridRowExpanded: async function(_id, id) {
1914
+ let data_url2 = api + '/process_locations?f=data&process=';
1915
+ let col_model_url2 = api + '/process_locations?f=col_model';
1916
+ let col_model2 = JSON.parse(await vwu.aget_api(col_model_url2));
1917
+ await jqu.subgrid(_id, id, data_url2, col_model2,'Locations for Process');
1918
+
1919
+ let data_url = api + '/process_styles?f=data&process=';
1920
+ let col_model_url = api + '/process_styles?f=col_model';
1921
+ let col_model = JSON.parse(await vwu.aget_api(col_model_url));
1922
+ await jqu.subgrid(_id, id, data_url, col_model,'Styles for Process');
1923
+ },
1924
+
1925
+ or
1926
+
1927
+ subGrid: true,
1928
+ subGridRowExpanded: async function(_id, id) {
1929
+ let row_data = jQuery(this).jqGrid ('getRowData', id);
1930
+ let param={f:'data','style':row_data['style'],pricelist:'P-TENENGR1'};
1931
+ let data_url = api + '/order_ln';
1932
+ data_url = await add_parameters(data_url, param);
1933
+ let col_model_url2 = api + '/order_ln?f=col_model';
1934
+ let col_model2 = JSON.parse(await vwu.aget_api(col_model_url2));
1935
+ await jqu.subgrid(_id, false, data_url, col_model2,'Order Lines for ' + row_data['style']);
1936
+ },
1937
+
1938
+ */
1939
+ async subgrid(_id, id, url, col_model, caption = "") {
1940
+ caption = caption != "" ? caption + " " : "";
1941
+ if (id) {
1942
+ url += id;
1943
+ } else {
1944
+ id = "";
1945
+ }
1946
+ let $s1 = jQuery(
1947
+ "<table style='margin: 5px 0' class='" + _id + "_t'></table>",
1948
+ );
1949
+ $s1.appendTo("#" + jQuery.jgrid.jqID(_id));
1950
+ $s1.jqGrid({
1951
+ caption: caption + id,
1952
+ colModel: col_model,
1953
+ datatype: "json",
1954
+ url: url,
1955
+ gridview: true,
1956
+ rownumbers: true,
1957
+ autoencode: true,
1958
+ sortname: "c1",
1959
+ sortorder: "desc",
1960
+ });
1961
+ }
1962
+
1963
+ /**
1964
+ @alias module:Jqgrid_utils
1965
+ @param {object} - col_model for the grid
1966
+ @param {string} - field what include the image/picture href path like http://mypicture.png
1967
+ @param {int} - size of the picture
1968
+ @param {bolen} - image path should be a link
1969
+ @example
1970
+ col_model = await jqu.add_image(col_model, image_field, 60, false);
1971
+ */
1972
+ async add_image(col_model, edit_field, size, link = false) {
1973
+ if (size === undefined) {
1974
+ size = 60;
1975
+ }
1976
+ for (let i = 0; i < col_model.length; i++) {
1977
+ if (col_model[i]["name"] === edit_field) {
1978
+ col_model[i]["picture"] = true;
1979
+ col_model[i]["width"] = size;
1980
+ col_model[i]["height"] = size;
1981
+ col_model[i]["formatter"] = function (cell_val) {
1982
+ const cell_val2 = cell_val.toLowerCase();
1983
+
1984
+ if (
1985
+ cell_val2.startsWith("https://", 0) ||
1986
+ cell_val2.startsWith("http://", 0)
1987
+ ) {
1988
+ if (
1989
+ cell_val2.includes(".png") ||
1990
+ cell_val2.includes(".jpg") ||
1991
+ cell_val2.includes(".jpeg") ||
1992
+ cell_val2.includes(".gif") ||
1993
+ cell_val2.includes(".svg") ||
1994
+ cell_val2.includes(".svgz") ||
1995
+ cell_val2.includes(".webp")
1996
+ ) {
1997
+ if (link) {
1998
+ return (
1999
+ '<a target="blank" href="' +
2000
+ cell_val +
2001
+ '"><img src="' +
2002
+ cell_val +
2003
+ '" alt="my image" width="' +
2004
+ size +
2005
+ '" /></a>'
2006
+ );
2007
+ } else {
2008
+ return (
2009
+ '<img src="' +
2010
+ cell_val +
2011
+ '" alt="my image" width="' +
2012
+ size +
2013
+ '" />'
2014
+ );
2015
+ }
2016
+ }
2017
+ }
2018
+ return cell_val;
2019
+ };
2020
+ }
2021
+ }
2022
+ return col_model;
2023
+ }
2024
+
2025
+ /**
2026
+ * Add a filter to the website beside the grid
2027
+ @alias module:Jqgrid_utils
2028
+ @param {object} - grid object or grid string name
2029
+ @param {object} - the grid data object
2030
+ @param {object} - a dict with a array what should be filterd by the grid
2031
+ @param {string} - id name of the DOM oject where the filter should be appened
2032
+ @example
2033
+ var jqu = new Jqgrid_utils();
2034
+ var run_me_once = true;
2035
+ gridComplete: async function(){
2036
+ if(run_me_once)
2037
+ {
2038
+ await jqu.set_filter(this, data, {material:[],section:[]}, '#filter');
2039
+ run_me_once = false;
2040
+ }
2041
+ },
2042
+ */
2043
+ async set_filter(grid, data, fx, append_to = "#filter") {
2044
+ jQuery(grid).jqGrid("setGridParam", { fdata: data });
2045
+ let f = document.querySelector(append_to);
2046
+ for (const i in data) {
2047
+ for (let x in fx) {
2048
+ fx[x].push(data[i][x]);
2049
+ }
2050
+ }
2051
+
2052
+ for (let x in fx) {
2053
+ fx[x] = fx[x].filter((val, ind, arr) => arr.indexOf(val) === ind);
2054
+ fx[x].sort();
2055
+ }
2056
+
2057
+ for (let x in fx) {
2058
+ let ul = document.createElement("ul");
2059
+ let lh = document.createElement("lh");
2060
+ lh.innerHTML = x;
2061
+ ul.appendChild(lh);
2062
+ for (let i in fx[x]) {
2063
+ let li = document.createElement("li");
2064
+ let l = document.createElement("label");
2065
+ l.innerHTML = fx[x][i];
2066
+ let c = document.createElement("input");
2067
+ c.setAttribute("type", "checkbox");
2068
+ c.setAttribute("class", x);
2069
+ c.setAttribute("id", x + "_" + fx[x][i]);
2070
+ l.setAttribute("for", x + "_" + fx[x][i]);
2071
+ c.value = fx[x][i];
2072
+ c.onchange = async () => {
2073
+ await this._filter(grid, fx);
2074
+ };
2075
+ li.appendChild(l);
2076
+ li.appendChild(c);
2077
+ ul.appendChild(li);
2078
+ }
2079
+ f.appendChild(ul);
2080
+ }
2081
+ }
2082
+
2083
+ /**
2084
+ * private function of set_filter
2085
+ @alias module:Jqgrid_utils
2086
+ */
2087
+ async _filter(grid, fx) {
2088
+ let _data = [];
2089
+ let data = jQuery(grid).jqGrid("getGridParam", "fdata");
2090
+ let filter = [];
2091
+ for (let x in fx) {
2092
+ let m = document.querySelectorAll("." + x);
2093
+ filter[x] = [];
2094
+ for (let i in m) {
2095
+ if (m[i].checked) {
2096
+ filter[x].push(m[i].value);
2097
+ }
2098
+ }
2099
+ }
2100
+ for (let i in data) {
2101
+ let include = false;
2102
+ for (let x in fx) {
2103
+ if (filter[x].indexOf(data[i][x]) != -1) {
2104
+ include = true;
2105
+ }
2106
+ }
2107
+ if (include) {
2108
+ _data.push(data[i]);
2109
+ }
2110
+ }
2111
+ jQuery(grid).jqGrid("clearGridData");
2112
+ jQuery(grid).jqGrid("setGridParam", { data: _data });
2113
+ jQuery(grid).trigger("reloadGrid");
2114
+ }
2115
+ };
2116
+
2117
+ },{}]},{},[1])(1)
2118
+ });