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

add/table.min.js   F

Complexity

Total Complexity 110
Complexity/F 3.06

Size

Lines of Code 26
Function Count 36

Duplication

Duplicated Lines 20
Ratio 76.92 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 0
eloc 26
c 1
b 0
f 1
nc 1
dl 20
loc 26
rs 2
wmc 110
mnd 4
bc 93
fnc 36
bpm 2.5833
cpm 3.0555
noi 3

17 Functions

Rating   Name   Duplication   Size   Complexity  
A table.min.js ➔ TableHelperSetVisability 1 1 3
A table.min.js ➔ TableHelperProcessPaginationLinks 1 1 3
A table.min.js ➔ TableHelperDraw_SectionClear 0 1 4
A TableSingleton.getInstance 2 2 2
A table.min.js ➔ TableHelperDraw_SectionRow 1 2 5
A table.min.js ➔ TableHelperGoPageGetNo 1 2 4
A table.min.js ➔ TableHelperIePrior 1 3 4
A table.min.js ➔ TableHelperColumnHover 1 1 3
A table.min.js ➔ TableHelperDraw_SectionRowCellFromObject 1 1 5
A TableSingleton.constructor 10 10 1
A table.min.js ➔ TableHelperFilterGetTableId 0 2 3
A table.min.js ➔ TableHelperColumnHoverRelease 1 1 4
A table.min.js ➔ TableHelperDraw_Section 0 1 4
A table.min.js ➔ initInstance 8 8 1
A table.min.js ➔ TableHelperBuildRequest_Filter 8 8 3
A table.min.js ➔ TableHelperGetParent 1 2 3
A table.min.js ➔ TableHelperRequestToUrl 2 2 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like add/table.min.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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()