Passed
Branchmaster (910da3)
by Plamen
01:33
created

table.min.js ➔ ... ➔ Init   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 2

Duplication

Lines 2
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 2
loc 2
rs 10
1
var TableInitSetColumnsHoverEffect=function(tContainer,tableId){var tHcells=tContainer.rows[0].cells;for(var i=0;i<tHcells.length;i++){if(tHcells[i].firstChild.tagName==="A"){tHcells[i].firstChild.setAttribute("onmouseover","table.ColumnHover('"+tableId+"',"+i+");");tHcells[i].firstChild.setAttribute("onmouseout","table.ColumnHover('"+tableId+"');")}}};var TableHelperBuildRequest=function(rq,crntTableId,strDesc){rq.tableId=crntTableId;TableHelperBuildRequest_Sort(rq,strDesc);TableHelperBuildRequest_Filter(rq)};var TableHelperBuildRequest_Sort=function(rq,strDesc){function sortBySpan(span,i){var order=span.innerHTML;if(order.length===1){rq.colNo=i;rq.colOrd=order===strDesc?"desc":"asc"}
2
return rq.colNo===i}
3 View Code Duplication
var thTags=document.getElementById(rq.tableId).getElementsByTagName("thead")[0].getElementsByTagName("th");var length=thTags.length;for(var i=0;i<length;i++){var link=thTags[i].getElementsByTagName("a")[0];if(link){var span=link.getElementsByTagName("span")[0];if(span&&sortBySpan(span,i)){break}}}};var TableHelperBuildRequest_Filter=function(rq){function getFilterFieldsByTableID(tableID){var fields={filterBy:null,filter:null};var filterDiv=getFilterDivByTableIDOrNull(tableID);if(filterDiv!==null){setFilterBy(fields,filterDiv);setFilterValue(fields,filterDiv)}
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4
return fields}
5
function getFilterDivByTableIDOrNull(tableID){var res=null;if(document.getElementById(tableID).parentNode.getElementsByTagName("div").length>0){for(var i=0;i<document.getElementById(tableID).parentNode.getElementsByTagName("div").length;i++){if(document.getElementById(tableID).parentNode.getElementsByTagName("div")[i].getAttribute("class")==="filter"){return document.getElementById(tableID).parentNode.getElementsByTagName("div")[i]}}}
6
return res}
7
function setFilterBy(fields,filterDiv){var slctObj=filterDiv.getElementsByTagName("select")[0];if(slctObj&&slctObj.options[slctObj.selectedIndex].value!=="all"){fields.filterBy=slctObj.options[slctObj.selectedIndex].value}}
8
function setFilterValue(fields,filterDiv){var textObj=filterDiv.getElementsByTagName("input")[0];if(textObj&&textObj.value&&textObj.value.length!==0){fields.filter=encodeURIComponent(textObj.value.trim())}}
9
var r=getFilterFieldsByTableID(rq.tableId);if(r.filter!==null){rq.filter=r.filter}
10
if(r.filterBy!==null){rq.filterBy=r.filterBy}};var TableHelperColumnHover=function(tableContainer,index){var rows=document.getElementById(tableContainer).rows;var upto=rows.length-1;if(typeof index==="undefined"){TableHelperColumnHoverRelease(rows,upto)}else{for(var i=0;i<upto;i++){rows[i].cells[index].setAttribute("lang","col-hover")}}};var TableHelperColumnHoverRelease=function(rows,upto){for(var i=0;i<upto;i++){for(var j=0;j<rows[i].cells.length;j++){if(rows[i].cells[j].lang){rows[i].cells[j].removeAttribute("lang")}}}};var TableHelperSetVisability=function(tableContainer,flag){var tbl=document.getElementById(tableContainer);if(flag===!0){tbl.style.filter="none";tbl.style.opacity="1";tbl.style.cursor="auto"}else if(flag===!1){tbl.style.filter="blur(1px)";tbl.style.opacity="0.8";tbl.style.cursor="wait"}else{console.error("table error in the flag value")}};var TableHelperProcessPaginationLinks=function(tfoot){var pLinks=tfoot.querySelectorAll(".paging a");if(pLinks.length>0){for(var j=0;j<pLinks.length;j++){pLinks[j].setAttribute("href","javascript:void(0);");pLinks[j].setAttribute("onclick","return table.GoPage(this);")}}};var TableHelperGoPageGetNo=function(lnk,tableId){var jumpDir=lnk.innerHTML.trim().substr(0,1);if(jumpDir==="+"||jumpDir==="-"){var current=document.getElementById(tableId).querySelector("tfoot .paging .a").innerHTML;var jump=lnk.innerHTML.replace("K","000").replace("M","000000000");var jumpPage=(parseInt(current)+parseInt(jump));lnk.parentNode.setAttribute("data-page",jumpPage);lnk.style.transform="none"}
11
return lnk.parentNode.hasAttribute("data-page")?lnk.parentNode.getAttribute("data-page"):lnk.innerHTML};var TableHelperIePrior=function(v){var rv=!1;if(window.navigator.appName==='Microsoft Internet Explorer'){var ua=window.navigator.userAgent;var re=new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");if(re.exec(ua)!==null){rv=parseFloat(RegExp.$1)}
12
rv=rv<v?!0:!1}
13 View Code Duplication
return rv};var TableHelperRequestToUrl=function(rq){var url=location.pathname+".json"+location.search;if(typeof rq==="object"){var getUrlVarName={colNo:"col",colOrd:"ord",filter:"filter",filterBy:"filter-by",pageNo:"pg",exportType:"export",tableId:"table-id"};var flagFirst=location.search.length<1?!0:!1;for(var r in rq){var clue=flagFirst===!0?"?":"&";url+=clue+getUrlVarName[r]+"="+rq[r];flagFirst=!1}}
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
return url};var TableHelperGetParent=function(obj,objType){while(obj&&obj.tagName!==objType.toUpperCase()){obj=obj.parentNode}
15
return obj};var TableHelperFilterGetTableId=function(field){if(field.tagName.toLowerCase()!=="select"){return field.getAttribute("data-table-id")}
16
var f=field.parentNode.parentNode.getElementsByTagName("input")[0];return''===f.value?null:f.getAttribute("data-table-id")};var TableHelperDraw_Section=function(tableContainer,dt,tSection){var section=tSection==="tfoot"?"tfoot":"tbody";var tSec=document.getElementById(tableContainer).getElementsByTagName(section)[0];TableHelperDraw_SectionClear(tSec,TableHelperIePrior(9));for(var i=0;i<dt.length;i++){var row=dt[i];var tRow=document.createElement("tr");TableHelperDraw_SectionRow(row,tRow);tSec.appendChild(tRow);if(section==="tfoot"){TableHelperProcessPaginationLinks(tSec)}}};var TableHelperDraw_SectionClear=function(tSection,iePrior9){if(iePrior9){if(tSection.firstChild){while(tSection.firstChild){tSection.removeChild(tSection.firstChild)}}}else{tSection.innerHTML=""}};var TableHelperDraw_SectionRow=function(row,tRow){for(var cell in row){var tCell=document.createElement("td");if(typeof row[cell]==="string"||typeof row[cell]==="number"){tCell.innerHTML=row[cell]}else if(typeof row[cell]==="object"){TableHelperDraw_SectionRowCellFromObject(row,cell,tCell)}
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
17 View Code Duplication
tRow.appendChild(tCell)}};var TableHelperDraw_SectionRowCellFromObject=function(row,cell,tCell){for(var attr in row[cell]){if(typeof row[cell][attr]==="string"){tCell.innerHTML=row[cell][attr]}else if(typeof row[cell][attr]==="object"){for(var v in row[cell][attr]){tCell.setAttribute(v,row[cell][attr][v])}}}};var TableSingleton=(function(){var instance;function initInstance(){var tail=null;function LoadData(tableContainer,rq){if(tail!==null){tail.abort()}
1 ignored issue
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
18
TableHelperSetVisability(tableContainer,!1);var xmlhttp=window.XMLHttpRequest?new XMLHttpRequest():new window.ActiveXObject("Microsoft.XMLHTTP");xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState===4&&xmlhttp.status===200){Draw(tableContainer,JSON.parse(xmlhttp.responseText));TableHelperSetVisability(tableContainer,!0);instance.LoadEndCalback(tableContainer)}};xmlhttp.open("GET",RequestToUrl(rq),!0);xmlhttp.send();tail=xmlhttp}
19
function Init(tableId){var tContainer=document.getElementById(tableId);if(iePrior(9)){TableInitSetColumnsHoverEffect(tContainer,tableId)}
20
var tfoot=tContainer.getElementsByTagName("tfoot")[0];TableHelperProcessPaginationLinks(tfoot)}
21
function Draw(tableContainer,d){TableHelperDraw_Section(tableContainer,d.body);TableHelperDraw_Section(tableContainer,d.footer,"tfoot");if(instance.rq!==null){var hover=document.getElementById(instance.rq.tableId).getElementsByTagName("th")[instance.rq.colNo].lang;if(hover){instance.ColumnHover(tableContainer,instance.rq.colNo)}}}
22
var BuildRequest=TableHelperBuildRequest;var FilterGetTableId=TableHelperFilterGetTableId;var GoPageGetNo=TableHelperGoPageGetNo;var getParent=TableHelperGetParent;var iePrior=TableHelperIePrior;var RequestToUrl=TableHelperRequestToUrl;return{rq:null,strAsc:String.fromCharCode(9650),strDesc:String.fromCharCode(9660),ReloadData:function(tableId){var request={};BuildRequest(request,tableId,this.strDesc);LoadData(tableId,request)},Filter:function(field){var crntTableId=FilterGetTableId(field);if(crntTableId!==null){var request={};var exRq=this.rq;BuildRequest(request,crntTableId,this.strDesc);if(exRq===null||request.filter!==exRq.filter||request.filterBy!==exRq.filterBy){LoadData(crntTableId,request)}}},GoPage:function(lnk){var request={};var crntTableId=getParent(lnk,"table").getAttribute("id");BuildRequest(request,crntTableId,this.strDesc);request.pageNo=GoPageGetNo(lnk,crntTableId);LoadData(crntTableId,request);return!1},Export:function(lnk,eType){var request={};var crntTableId=getParent(lnk,"table").getAttribute("id");BuildRequest(request,crntTableId,this.strDesc);request.exportType=["CSV","Excel"].indexOf(eType)>=0?eType:"csv";window.open(RequestToUrl(request));return!1},Sort:function(colNo,lnk){var request={};var crntTableId=getParent(lnk,"table").getAttribute("id");BuildRequest(request,crntTableId,this.strDesc);if(Math.round(colNo)===request.colNo){request.colOrd=(request.colOrd==="asc"?"desc":"asc")}else{request.colNo=Math.round(colNo);request.colOrd="asc"}
23
LoadData(crntTableId,request);var headSpans=getParent(lnk,"thead").getElementsByTagName("span");var length=headSpans.length;for(var i=0;i<length;i++){headSpans[i].innerHTML=""}
24
lnk.getElementsByTagName("span")[0].innerHTML=(request.colOrd==="desc"?this.strDesc:this.strAsc)},ColumnHover:function(tableContainer,index){if(!iePrior(9)){TableHelperColumnHover.call(this,tableContainer,index)}},LoadEndCalback:function(){},init:Init}}
25
return{getInstance:function(){if(!instance){instance=initInstance()}
26
return instance}}})();var table=TableSingleton.getInstance()