XML to JSON

This commit is contained in:
2023-10-23 13:40:20 +06:00
parent 4ddea79113
commit f3618efc7e
7 changed files with 11177 additions and 264 deletions

View File

@ -1,4 +1,4 @@
//Copyright (C) 2008 Ivanov I.M. ivanov.i@istt.kz
//Copyright (C) 2008 Igor
//For find non english chars: [^\x00-\x7F]+
function callWindow(uid,id,i)
@ -49,18 +49,14 @@ class SRec
applyReq(req,fn,node,xmldoc)
{
//alert2(trt('Alert'),getXMLNodeSerialisation(node));
this.hideProgressBar();
this.xmldoc=node.ownerDocument; //xmldoc;
if (fn==-1) //Information alert
{
let fullText = findFirstNode(node,'#cdata-section').nodeValue;
if(node.errorCode>0) {
let fullText = node.errorMessage;
let smallText = '';
let pos1=fullText.indexOf('[[');
let pos2=fullText.indexOf(']]');
if(pos1>=0 && pos2>=0 && pos1<pos2) smallText=fullText.substring(pos1+2, pos2);
if(fullText.indexOf("id456[[")>=0){ //Если есть идентификатор того что это перезапись
let okFunc=()=>{
this.setValue('seq',0);
@ -76,7 +72,11 @@ class SRec
else
alert2(trt('Alert'), fullText);
}
}else
return;
}
this.xmldoc=node.ownerDocument; //xmldoc;
if (fn==0)
{
//alert2(trt('Alert'),getXMLNodeSerialisation(node));
@ -1165,15 +1165,15 @@ class SRec
}
//Function to insert data into a table.
//node - A node with data.
//node - A JSON object with data.
//clear - Whether to clear all entries.
insertRows(node,clear)
{
let theTable = document.getElementById('thetable'+this.uid); //Data table
let nodeType=findFirstNode(node, "type");
let pagecount=nodeType.getAttribute("pc"); //total pages
if((nodeType.getAttribute("pp")!=null)&&(nodeType.getAttribute("pp")!=-1)) this.pagepos=nodeType.getAttribute("pp");
//let nodeType=findFirstNode(node, "type");
let pagecount=node.pc; //total pages
this.pagepos=node.pp;
if(clear || this.f_nodeData==null)
{ this.f_nodeData=node;
@ -1206,6 +1206,10 @@ class SRec
let newCell = newRow.insertCell(-1);
newCell.style.backgroundColor = bgColor;
newCell.style.cursor="pointer";
if (this.pagepos==i) {
newCell.style.fontWeight = "bold";
newCell.style.fontSize = "large";
}
newCell.appendChild(document.createTextNode(i+1));
//Upon clicking, a filter with old values is used only with a different page number.
newCell.onclick=function(obj,page){
@ -1227,123 +1231,115 @@ class SRec
}
}else
{
let tmpNR=findFirstNode(this.f_nodeData, "type");
let nodeRecord = nodeType.firstChild;
while (nodeRecord != null)
{
if(nodeRecord.nodeName=="record")
{
tmpNR.appendChild(nodeRecord.cloneNode(true) );
}
nodeRecord = nodeRecord.nextSibling;
for(let i=0;i<node.data.length;i++){
Object.assign(this.f_nodeData.data, node.data[i])
}
}
//Add new entries
let nColor=findNumNodeOnAttribute(findNodeOnPath(this.nodeMetadata,"type/objects-list"),"column","n","color");
let TypeName = nodeType.getAttribute("n");
let TypeName = node.n;
let i=0; //The position should be received from the server.
let nodeRecord = nodeType.firstChild;
while (nodeRecord != null)
{
if(nodeRecord.nodeName=="record")
for(let i=0;i<node.data.length;i++){
let bgColor='';
if (i%2==0) bgColor='var(--row-color-1)'; else bgColor='var(--row-color-2)';
//if(nColor>=0) //Color from Result
//{
// let bgColorT=findNodeOnNum(nodeRecord,"#cdata-section",nColor).nodeValue;
// if(bgColorT!="") bgColor=bgColorT;
//}
let id=node.data[i].id;
//add rows to an existing record table
let tr = document.createElement('tr');
tr.onmouseover=function(){ this.style.backgroundColor="var(--btn-color2)"; };
tr.onmouseout=function(val1,val2){return function(){val1.style.backgroundColor=val2;}}(tr,bgColor);
tr.setAttribute("id",id+'_'+this.uid);
tr.style.backgroundColor=bgColor;
//sequential record number
let td = document.createElement('td');
td.appendChild( document.createTextNode( i+1+100*this.pagepos ) );
tr.appendChild(td);
//CheckBuck to delete records
td = document.createElement('td');
td.style.cssText="text-align: center;";
if(!(this.f_pD!="1" || node.data[i].a.indexOf("d")==-1))
{
let bgColor='';
let i=theTable.rows.length; //number of rows in the table
if (i%2==0) bgColor='var(--row-color-1)'; else bgColor='var(--row-color-2)';
if(nColor>=0) //Color from Result
{
let bgColorT=findNodeOnNum(nodeRecord,"#cdata-section",nColor).nodeValue;
if(bgColorT!="") bgColor=bgColorT;
}
let id=nodeRecord.getAttribute("id");
//add rows to an existing record table
let tr = document.createElement('tr');
tr.onmouseover=function(){ this.style.backgroundColor="var(--btn-color2)"; };
tr.onmouseout=function(val1,val2){return function(){val1.style.backgroundColor=val2;}}(tr,bgColor);
tr.setAttribute("id",id+'_'+this.uid);
tr.style.backgroundColor=bgColor;
//sequential record number
let td = document.createElement('td');
td.appendChild( document.createTextNode( i+100*this.pagepos ) );
tr.appendChild(td);
//CheckBuck to delete records
td = document.createElement('td');
td.style.cssText="text-align: center;";
if(!(this.f_pD!="1" || nodeRecord.getAttribute("a").indexOf("d")==-1))
{
let checkbox = document.createElement('input');
checkbox.classList.add('DBMS');
this.masChBox.push(checkbox);
//checkbox.disabled=true;
checkbox.setAttribute("type","checkbox");
checkbox.setAttribute("value",id);
checkbox.style.cssText="font: normal normal normal 11px normal Arial;";
td.appendChild( checkbox );
}
tr.appendChild(td);
//for each column we make a column
let colN=0; //column number
let cdataNode = nodeRecord.firstChild;
while (cdataNode!=null)
{
if(cdataNode.nodeName=="#cdata-section")
{
if(this.masVis[colN])
{
td = document.createElement('td');
if((this.f_pU=="1")||(this.f_State=="1")){
td.style.cssText="cursor: pointer;";
}
let textNode=document.createTextNode(cdataNode.nodeValue);
td.setAttribute("id",id+this.masCL[colN].getAttribute("n")); //so that you can identify each record when you update
td.appendChild(textNode);
//if in the metadata for this column there is a reference object then add a link
if (this.masCT[colN]!=null)
{
//Opening a new window in the filter is passed to the current values of the row
td.style.cssText="cursor: pointer; background-color: var(--btn-color); font-weight: bold;";
td.onmouseover=function(){
this.style.backgroundColor="var(--btn-color2)";
};
td.onmouseout=function(){
this.style.backgroundColor="var(--btn-color)";
};
//when you click on a cell, the sent filter is prefilled with variables in accordance with the id of the pressed line
//in the function we pass the cell id and the column number
td.onclick=function(obj,val1,val2){
return function(){
obj.callWindow(val1,val2);
}
}(this,id,colN);
}else
{
if((this.f_pU=="1" && nodeRecord.getAttribute("a").indexOf("u")!=-1)||(this.f_State=="1"))
{
td.onclick=function(thiz,val1,val2){
return function(){thiz.updateRecord(val1);};
}(this,id);
}else
{
td.style.cssText="cursor: default;";
}
}
tr.appendChild(td);
}
colN++;
}
cdataNode = cdataNode.nextSibling;
}
theTable.tBodies[0].appendChild(tr);
let checkbox = document.createElement('input');
checkbox.classList.add('DBMS');
this.masChBox.push(checkbox);
//checkbox.disabled=true;
checkbox.setAttribute("type","checkbox");
checkbox.setAttribute("value",id);
checkbox.style.cssText="font: normal normal normal 11px normal Arial;";
td.appendChild( checkbox );
}
nodeRecord = nodeRecord.nextSibling;
tr.appendChild(td);
//for each column we make a column
let colN=0; //column number
//let cdataNode = nodeRecord.firstChild;
//while (cdataNode!=null)
//this.access[obj.data[i].row[0]]=obj.data[i].row[1];
for(let j=0;j<node.data[i].row.length;j++)
{
if(this.masVis[colN])
{
td = document.createElement('td');
if((this.f_pU=="1")||(this.f_State=="1")){
td.style.cssText="cursor: pointer;";
}
let textNode;
if(node.data[i].row[j]!=null) textNode=document.createTextNode(node.data[i].row[j]);
else textNode=document.createTextNode("");
//td.setAttribute("id",id+this.masCL[colN].getAttribute("n")); //so that you can identify each record when you update
td.setAttribute("id",id+this.masCL[colN].n); //so that you can identify each record when you update
td.appendChild(textNode);
//if in the metadata for this column there is a reference object then add a link
if (this.masCT[colN]!=null)
{
//Opening a new window in the filter is passed to the current values of the row
td.style.cssText="cursor: pointer; background-color: var(--btn-color); font-weight: bold;";
td.onmouseover=function(){
this.style.backgroundColor="var(--btn-color2)";
};
td.onmouseout=function(){
this.style.backgroundColor="var(--btn-color)";
};
//when you click on a cell, the sent filter is prefilled with variables in accordance with the id of the pressed line
//in the function we pass the cell id and the column number
td.onclick=function(obj,val1,val2){
return function(){
obj.callWindow(val1,val2);
}
}(this,id,colN);
}else
{
if((this.f_pU=="1" && node.data[i].a.indexOf("u")!=-1)||(this.f_State=="1"))
{
td.onclick=function(thiz,val1,val2){
return function(){thiz.updateRecord(val1);};
}(this,id);
}else
{
td.style.cssText="cursor: default;";
}
}
tr.appendChild(td);
}
colN++;
}
theTable.tBodies[0].appendChild(tr);
}
this.updateSize();
}
@ -1466,11 +1462,9 @@ class SRec
}
}
//find the node cdata in the data by the record id and column name
getDataC(id,col)
{
let i=0;
let b=false;
//Get column number by name
getColN(name){
let i=-1;
//determine the sequence number of the column
let node=findNodeOnPath(this.nodeMetadata,"type/objects-list");
let nodeCol = node.firstChild;
@ -1479,73 +1473,66 @@ class SRec
if(nodeCol.nodeName=="column")
{
if(nodeCol.getAttribute("n")==col){
b=true;
break;
}
i++;
}
nodeCol = nodeCol.nextSibling;
}
if(!b)return null;
//we search for value in result set
node=findFirstNode(this.f_nodeData, 'type');
let nodeRec=findNodeOnAttribute(node, 'record', 'id', id);
return findNodeOnNum(nodeRec,"#cdata-section",i);
return i;
}
//find the value in the result set by the id of the record and the name of the column
getData(id,col)
{
if(findNode(this.nodeMetadata,'type').getAttribute("ObjectID")==col) return id;
let cdt=this.getDataC(id,col);
if(cdt!=null) return cdt.nodeValue; else return '';
if(findNode(this.nodeMetadata,'type').getAttribute("ObjectID")==col)
return id;
let pos=this.getColN(col);
if(pos>=0){
for(let i=0;i<this.f_nodeData.data.length;i++){
if(this.f_nodeData.data[i].id=id){
return this.f_nodeData.data[i].row[pos];
}
}
}
return '';
}
//look for a string by id and update the values (TODO consecutively without hidden fields)
updateRows(node)
{
//We sort through the received records and update the values
let nodeRecord = findNode(node,"type").firstChild
while (nodeRecord != null)
for(let i=0;i<node.data.length;i++)
{
if(nodeRecord.nodeName=="record")
let id=node.data[i].id;
let tr=document.getElementById(id+'_'+this.uid);
if (tr==null)
{
let ii=0;
let jj=0;
let id=nodeRecord.getAttribute("id");
let tr=document.getElementById(id+'_'+this.uid);
if (tr==null)
this.insertRows(node,false); //If not then insert
}else //Update fields if editing
{
//Updating data in a data object
for(let ii=0;ii<this.f_nodeData.data.length;ii++){
if(this.f_nodeData.data[ii].id==id) {
this.f_nodeData.data[ii].row=node.data[i].row;
}
}
//Updating data in an HTML table
tr.style.textDecoration="underline";
for(let j=0;j<node.data[i].row.length;j++)
{
this.insertRows(node,false); //If not then insert
}else
{
tr.style.textDecoration="underline";
let cdataNode = nodeRecord.firstChild;
while (cdataNode!=null)
if(this.masCL[j].getAttribute("visible")!="0")
{
if (cdataNode.nodeName=="#cdata-section")
{
let cd=this.getDataC(id,this.masCL[ii].getAttribute("n"));
if(cd!=null) cd.nodeValue=cdataNode.nodeValue;
if(this.masCL[ii].getAttribute("visible")!="0")
{
while(tr.childNodes[jj+2].childNodes[0])
tr.childNodes[jj+2].removeChild(tr.childNodes[jj+2].childNodes[0]);
tr.childNodes[jj+2].appendChild(document.createTextNode(cdataNode.nodeValue));
jj++;
}
ii++;
while(tr.childNodes[j+2].childNodes[0]) {
tr.childNodes[j + 2].removeChild(tr.childNodes[j + 2].childNodes[0]);
}
cdataNode = cdataNode.nextSibling;
let textNode;
if(node.data[i].row[j]!=null) textNode=document.createTextNode(node.data[i].row[j]);
else textNode=document.createTextNode("");
tr.childNodes[j+2].appendChild(textNode);
}
}
}
nodeRecord = nodeRecord.nextSibling;
}
}
@ -1689,8 +1676,9 @@ class SRec
//Rebuild sequential numbering of rows (first column)
let theTable = document.getElementById('thetable'+this.uid); //data table
for(let i=1;i<theTable.rows.length;i++)
{while (theTable.rows[i].cells[0].childNodes[0]) //delete all childs
{theTable.rows[i].cells[0].removeChild(theTable.rows[i].cells[0].childNodes[0]);
{
while (theTable.rows[i].cells[0].childNodes[0]) //delete all childs
{ theTable.rows[i].cells[0].removeChild(theTable.rows[i].cells[0].childNodes[0]);
}
theTable.rows[i].cells[0].appendChild(document.createTextNode(i));
}