| 1 | var strAsc = String.fromCharCode(9650); //▲ |
||
| 2 | var strDesc = String.fromCharCode(9660);//▼ |
||
| 3 | var xmlhttp; |
||
| 4 | var d; |
||
| 5 | |||
| 6 | var table = new function () { |
||
| 7 | this.rq = null; |
||
| 8 | this.tail = []; |
||
| 9 | |||
| 10 | this.ReloadData = function (tableId) { |
||
| 11 | var request = {}; |
||
| 12 | this.BuildRequest(request, tableId); |
||
| 13 | this.LoadData(tableId, request); |
||
| 14 | }; |
||
| 15 | |||
| 16 | this.BuildRequest = function (request, crntTableId, skipPropertyArray) { |
||
| 17 | this.rq = request; |
||
| 18 | this.checkSkip = function (skipProperty) { |
||
| 19 | var result = false; |
||
| 20 | if (skipPropertyArray && Object.prototype |
||
| 21 | .toString.call(skipPropertyArray) === '[object Array]') { |
||
| 22 | if (skipPropertyArray.indexOf(skipProperty) >= 0) { |
||
| 23 | result = true; |
||
| 24 | } |
||
| 25 | } |
||
| 26 | return result; |
||
| 27 | }; |
||
| 28 | this.getSort = function () { |
||
| 29 | var table = document.getElementById(crntTableId); |
||
| 30 | var thTags = table.getElementsByTagName("thead")[0] |
||
| 31 | .getElementsByTagName("th"); |
||
| 32 | for (var i = 0; i < thTags.length; i++) { |
||
| 33 | if (thTags[i].getElementsByTagName("a")[0] && thTags[i] |
||
| 34 | .getElementsByTagName("a")[0].getElementsByTagName("span") |
||
| 35 | .length === 1 |
||
| 36 | ) { |
||
| 37 | var order = thTags[i].getElementsByTagName("a")[0] |
||
| 38 | .getElementsByTagName("span")[0].innerHTML; |
||
| 39 | if (order.length === 1) { |
||
| 40 | this.rq.colNo = i; |
||
| 41 | this.rq.colOrd = (order === window.strDesc) ? |
||
| 42 | "desc" : "asc"; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 | }; |
||
| 47 | this.getFilter = function () { |
||
| 48 | var r = this.getFilterFieldsByTbaleID(crntTableId); |
||
| 49 | if (r.filter !== null) { |
||
| 50 | this.rq.filter = r.filter; |
||
| 51 | } |
||
| 52 | if (r.filterBy !== null) { |
||
| 53 | this.rq.filterBy = r.filterBy; |
||
| 54 | } |
||
| 55 | }; |
||
| 56 | |||
| 57 | /* Build request object */ |
||
| 58 | if (!this.checkSkip("sort")) { |
||
| 59 | this.getSort(); |
||
| 60 | } |
||
| 61 | if (!this.checkSkip("filter")) { |
||
| 62 | this.getFilter(); |
||
| 63 | } |
||
| 64 | |||
| 65 | this.rq.tableId = crntTableId; |
||
| 66 | return this.rq; |
||
| 67 | }; |
||
| 68 | |||
| 69 | this.RequestToUrl = function (rq) { |
||
| 70 | var url = location.pathname + ".json" + location.search; |
||
| 71 | if (typeof rq === "object") { |
||
| 72 | var getUrlVarName = { |
||
| 73 | colNo: "col", colOrd: "ord", filter: "filter", |
||
| 74 | filterBy: "filter-by", pageNo: "pg", exportType: "export", |
||
| 75 | tableId: "table-id" |
||
| 76 | }; |
||
| 77 | var flagFirst = location.search.length < 1 ? true : false; |
||
| 78 | for (var r in rq) { |
||
|
1 ignored issue
–
show
|
|||
| 79 | var clue = flagFirst === true ? "?" : "&"; |
||
| 80 | url += clue + getUrlVarName[r] + "=" + rq[r]; |
||
| 81 | flagFirst = false; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | return url; |
||
| 85 | }; |
||
| 86 | |||
| 87 | this.Filter = function (field) { |
||
| 88 | var crntTableId = this.FilterGetTableId(field); |
||
| 89 | if (crntTableId !== null) { |
||
| 90 | var request = {}; |
||
| 91 | var exRq = this.rq; |
||
| 92 | this.BuildRequest(request, crntTableId); |
||
| 93 | if (exRq === null || |
||
| 94 | request.filter !== exRq.filter || |
||
| 95 | request.filterBy !== exRq.filterBy |
||
| 96 | ) { |
||
| 97 | this.LoadData(crntTableId, request); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | }; |
||
| 101 | |||
| 102 | this.FilterGetTableId = function (field) { |
||
| 103 | if (field.tagName.toLowerCase() !== "select") { |
||
| 104 | return field.getAttribute("data-table-id"); |
||
| 105 | } else { |
||
| 106 | var f = field.parentNode.parentNode.getElementsByTagName("input")[0]; |
||
| 107 | return '' === f.value ? null : f.getAttribute("data-table-id"); |
||
| 108 | } |
||
| 109 | }; |
||
| 110 | |||
| 111 | this.GoPage = function (lnk) { |
||
| 112 | var request = {}; |
||
| 113 | var table = this.getParent(lnk, "table"); |
||
| 114 | var crntTableId = table.getAttribute("id"); |
||
| 115 | this.BuildRequest(request, crntTableId); |
||
| 116 | //check & serve pagination jump links |
||
| 117 | var jumpDir = lnk.innerHTML.trim().substr(0, 1); |
||
| 118 | if (jumpDir === "+" || jumpDir === "-") { |
||
| 119 | var current = table.querySelector("tfoot .paging .a").innerHTML; |
||
| 120 | var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000"); |
||
| 121 | var jumpPage = (parseInt(current) + parseInt(jump)); |
||
| 122 | lnk.parentNode.setAttribute("data-page", jumpPage); |
||
| 123 | lnk.style.transform = "none"; |
||
| 124 | } |
||
| 125 | request.pageNo = lnk.parentNode.hasAttribute("data-page") ? |
||
| 126 | lnk.parentNode.getAttribute("data-page") : |
||
| 127 | lnk.innerHTML; |
||
| 128 | this.LoadData(crntTableId, request); |
||
| 129 | return false; |
||
| 130 | }; |
||
| 131 | |||
| 132 | this.Export = function (lnk, eType) { |
||
| 133 | var request = {}; |
||
| 134 | var crntTableId = this.getParent(lnk, "table").getAttribute("id"); |
||
| 135 | this.BuildRequest(request, crntTableId); |
||
| 136 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? eType : "csv"; |
||
| 137 | window.open(this.RequestToUrl(request)); |
||
| 138 | return false; |
||
| 139 | }; |
||
| 140 | |||
| 141 | this.Sort = function (colNo, lnk) { |
||
| 142 | var request = {}; |
||
| 143 | var crntTableId = this.getParent(lnk, "table").getAttribute("id"); |
||
| 144 | this.BuildRequest(request, crntTableId); |
||
| 145 | if (Math.round(colNo) === request.colNo) { |
||
| 146 | request.colOrd = request.colOrd === "asc" ? "desc" : "asc"; |
||
| 147 | } else { |
||
| 148 | request.colNo = Math.round(colNo); |
||
| 149 | request.colOrd = "asc"; |
||
| 150 | } |
||
| 151 | this.LoadData(crntTableId, request); |
||
| 152 | /* Clear and add new sort arrow */ |
||
| 153 | var headSpans = this.getParent(lnk, "thead").getElementsByTagName("span"); |
||
| 154 | var length = headSpans.length; |
||
| 155 | for (var i = 0; i < length; i++) { |
||
| 156 | headSpans[i].innerHTML = ""; |
||
| 157 | } |
||
| 158 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? window.strDesc : window.strAsc); |
||
| 159 | }; |
||
| 160 | |||
| 161 | this.DrawSection = function (tableContainer, dt, tSection) { |
||
| 162 | var section = tSection === "tfoot" ? "tfoot" : "tbody"; |
||
| 163 | tSection = document.getElementById(tableContainer). |
||
| 164 | getElementsByTagName(section)[0]; |
||
| 165 | this.clearSection(tSection); |
||
| 166 | for (var i = 0; i < dt.length; i++) { |
||
| 167 | var row = dt[i]; |
||
| 168 | var tRow = document.createElement("tr"); |
||
| 169 | |||
| 170 | this.DrawRow(row, tRow); |
||
| 171 | |||
| 172 | tSection.appendChild(tRow); |
||
| 173 | if (section === "tfoot") { |
||
| 174 | this.footerProcessPaginationLinks(tSection); |
||
| 175 | } |
||
| 176 | this.AppendRowCalback(tableContainer); |
||
| 177 | } |
||
| 178 | }; |
||
| 179 | |||
| 180 | this.DrawRow = function(row, tRow){ |
||
| 181 | for(var cell in row){ |
||
|
1 ignored issue
–
show
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...
|
|||
| 182 | var tCell = document.createElement("td"); |
||
| 183 | if(typeof row[cell] === "string" || typeof row[cell] === "number"){ |
||
| 184 | tCell.innerHTML = row[cell]; |
||
| 185 | } else if(typeof row[cell] === "object"){ |
||
| 186 | this.DrawCellFromObject(row, cell, tCell); |
||
| 187 | } |
||
| 188 | tRow.appendChild(tCell); |
||
| 189 | } |
||
| 190 | }; |
||
| 191 | |||
| 192 | this.DrawCellFromObject = function (row, cell, tCell) { |
||
| 193 | for (var attr in row[cell]) { |
||
| 194 | if (typeof row[cell][attr] === "string") { |
||
| 195 | tCell.innerHTML = row[cell][attr]; |
||
| 196 | } else if (typeof row[cell][attr] === "object") { |
||
| 197 | for (var v in row[cell][attr]) { |
||
| 198 | tCell.setAttribute(v, row[cell][attr][v]); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | }; |
||
| 203 | |||
| 204 | this.footerProcessPaginationLinks = function (tSection) { |
||
| 205 | var pLinks = tSection.querySelectorAll(".paging a"); |
||
| 206 | if (pLinks.length > 0) { |
||
| 207 | for (var j = 0; j < pLinks.length; j++) { |
||
| 208 | pLinks[j].setAttribute("href", "javascript:void(0);"); |
||
| 209 | pLinks[j].setAttribute("onclick", "return table.GoPage(this);"); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | }; |
||
| 213 | |||
| 214 | this.clearSection = function (tSection) { |
||
| 215 | if (this.iePrior(9)) { |
||
| 216 | if (tSection.firstChild) { |
||
| 217 | while (tSection.firstChild) { |
||
| 218 | tSection.removeChild(tSection.firstChild); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } else { |
||
| 222 | tSection.innerHTML = ""; |
||
| 223 | } |
||
| 224 | }; |
||
| 225 | |||
| 226 | this.SetTheTableColumnsHoverEffect = function (tableContainer) { |
||
| 227 | if (this.iePrior(9)) { |
||
| 228 | return; |
||
| 229 | } |
||
| 230 | var tContainer = document.getElementById(tableContainer); |
||
| 231 | var tHcells = tContainer.rows[0].cells; |
||
| 232 | for (var i = 0; i < tHcells.length; i++) { |
||
| 233 | if (tHcells[i].firstChild.tagName === "A") { |
||
| 234 | tHcells[i].firstChild.setAttribute("onmouseover", "table.ColumnHover('" + tableContainer + "'," + i + ");"); |
||
| 235 | tHcells[i].firstChild.setAttribute("onmouseout", "table.ColumnHover('" + tableContainer + "');"); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | var pLinks = tContainer.querySelectorAll("tfoot .paging a"); |
||
| 239 | if (pLinks.length > 0) { |
||
| 240 | for (var j = 0; j < pLinks.length; j++) { |
||
| 241 | pLinks[j].setAttribute("href", "javascript:void(0);"); |
||
| 242 | pLinks[j].setAttribute("onclick", "return table.GoPage(this);"); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | }; |
||
| 246 | |||
| 247 | this.ColumnHover = function (tableContainer, index) { |
||
| 248 | if (this.iePrior(9)) { |
||
| 249 | return; |
||
| 250 | } |
||
| 251 | var tRow = document.getElementById(tableContainer).rows; |
||
| 252 | index = Math.round(index); |
||
| 253 | for (var i = 0; i < (tRow.length - 1); i++) { |
||
| 254 | if (index >= 0) { |
||
| 255 | tRow[i].cells[index].setAttribute("lang", "col-hover"); |
||
| 256 | } else { |
||
| 257 | for (var j = 0; j < tRow[i].cells.length; j++) { |
||
| 258 | if (tRow[i].cells[j].lang) { |
||
| 259 | tRow[i].cells[j].removeAttribute("lang"); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | }; |
||
| 265 | |||
| 266 | this.getFilterFieldsByTbaleID = function (tableID) { |
||
| 267 | var fields = {filterBy: null, filter: null}; |
||
| 268 | var filterDiv = this.getFilterDivByTableIDOrNull(tableID); |
||
| 269 | if (filterDiv !== null) { |
||
| 270 | var selectObj = filterDiv.getElementsByTagName("select")[0]; |
||
| 271 | var textObj = filterDiv.getElementsByTagName("input")[0]; |
||
| 272 | fields.filterBy = (selectObj === null || selectObj.options[selectObj.selectedIndex].value === "all") ? null : selectObj.options[selectObj.selectedIndex].value; |
||
| 273 | fields.filter = (textObj === null || textObj.value.length === 0) ? null : encodeURIComponent(textObj.value.trim()); |
||
| 274 | } |
||
| 275 | return fields; |
||
| 276 | }; |
||
| 277 | |||
| 278 | this.getFilterDivByTableIDOrNull = function (tableID) { |
||
| 279 | var res = null; |
||
| 280 | if (document.getElementById(tableID).parentNode.getElementsByTagName("div").length > 0) { |
||
| 281 | for (var i = 0; i < document.getElementById(tableID).parentNode.getElementsByTagName("div").length; i++) { |
||
| 282 | if (document.getElementById(tableID).parentNode.getElementsByTagName("div")[i].getAttribute("class") === "filter") { |
||
| 283 | return document.getElementById(tableID).parentNode.getElementsByTagName("div")[i]; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | } |
||
| 288 | return res; |
||
| 289 | }; |
||
| 290 | |||
| 291 | this.LoadData = function (tableContainer, rq) { |
||
| 292 | this.setVisability(tableContainer, false); |
||
| 293 | if (window.XMLHttpRequest) { |
||
| 294 | xmlhttp = new XMLHttpRequest();/* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
| 295 | } else { /** global: ActiveXObject */ |
||
| 296 | xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
| 297 | } |
||
| 298 | for (var i = 0; i < this.tail.length; i++) { |
||
| 299 | var ex_xmlhttp = this.tail.shift(); |
||
| 300 | ex_xmlhttp.abort(); |
||
| 301 | } |
||
| 302 | xmlhttp.onreadystatechange = function () { |
||
| 303 | if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { |
||
| 304 | d = JSON.parse(xmlhttp.responseText); |
||
| 305 | table.DrawSection(tableContainer, d.body); |
||
| 306 | table.DrawSection(tableContainer, d.footer, "tfoot"); |
||
| 307 | table.LoadEndCalback(tableContainer); |
||
| 308 | table.setVisability(tableContainer, true); |
||
| 309 | if (typeof rq === "object") { |
||
| 310 | table.ColumnHover(tableContainer, rq.colNo); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | }; |
||
| 314 | xmlhttp.open("GET", this.RequestToUrl(rq), true); |
||
| 315 | xmlhttp.send(); |
||
| 316 | this.tail.push(xmlhttp); //put in tail to may later abort any previous |
||
| 317 | }; |
||
| 318 | |||
| 319 | this.setVisability = function (tableContainer, rq) { |
||
| 320 | var tbl = document.getElementById(tableContainer); |
||
| 321 | if (rq === true) { |
||
| 322 | tbl.style.filter = "none"; |
||
| 323 | tbl.style.opacity = "1"; |
||
| 324 | tbl.style.cursor = "auto"; |
||
| 325 | } else if (rq === false) { |
||
| 326 | tbl.style.filter = "blur(1px)"; |
||
| 327 | tbl.style.opacity = "0.8"; |
||
| 328 | tbl.style.cursor = "wait"; |
||
| 329 | } else { |
||
| 330 | console.log("table error in the rq value"); /*Shows error*/ |
||
| 331 | } |
||
| 332 | }; |
||
| 333 | |||
| 334 | this.getParent = function (obj, objType) { |
||
| 335 | while (obj && obj.tagName !== objType.toUpperCase()) { |
||
| 336 | obj = obj.parentNode; |
||
| 337 | } |
||
| 338 | return obj; |
||
| 339 | }; |
||
| 340 | |||
| 341 | this.init = function (tableId) { |
||
| 342 | this.SetTheTableColumnsHoverEffect(tableId); |
||
| 343 | }; |
||
| 344 | |||
| 345 | this.iePrior = function (v) { |
||
| 346 | var rv = false; |
||
| 347 | if (/** global: navigator */ navigator.appName === 'Microsoft Internet Explorer') { |
||
| 348 | var ua = navigator.userAgent; |
||
| 349 | var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); |
||
| 350 | if (re.exec(ua) !== null) { |
||
| 351 | rv = parseFloat(RegExp.$1); |
||
| 352 | } |
||
| 353 | rv = rv < v ? true : false; |
||
| 354 | } |
||
| 355 | return rv; |
||
| 356 | }; |
||
| 357 | this.loadJS = function (src) { |
||
| 358 | var s = document.createElement('script'); |
||
| 359 | s.src = src; |
||
| 360 | document.getElementsByTagName('head')[0].appendChild(s); |
||
| 361 | }; |
||
| 362 | this.loadCSS = function (src) { |
||
| 363 | var s = document.createElement('link'); |
||
| 364 | s.href = src; |
||
| 365 | s.rel = "stylesheet"; |
||
| 366 | document.getElementsByTagName('head')[0].appendChild(s); |
||
| 367 | }; |
||
| 368 | |||
| 369 | this.LoadEndCalback = function (tableId) { |
||
| 370 | if (tableId) {/*Allows override*/ |
||
| 371 | } |
||
| 372 | }; |
||
| 373 | this.AppendRowCalback = function (tableId) { |
||
| 374 | if (tableId) {/*Allows override*/ |
||
| 375 | } |
||
| 376 | }; |
||
| 377 | }; |
||
| 378 | |||
| 379 | /** Moves custom created filter to the Table's filter |
||
| 380 | * @param {string} filterId |
||
| 381 | * @param {string} tableId |
||
| 382 | * @param {boolean} delay - needed in case the table is istill not executed */ |
||
| 383 | function moveSelectorToTheTableFilter(filterId, tableId, delay) { |
||
| 384 | if (delay === true) { |
||
| 385 | setTimeout(function () { |
||
| 386 | var filterDiv = document.getElementById(tableId) |
||
| 387 | .getElementsByTagName("div")[0]; |
||
| 388 | filterDiv.appendChild(document.getElementById(filterId)); |
||
| 389 | }, 500); |
||
| 390 | } else { |
||
| 391 | var filterDiv = document.getElementById(tableId) |
||
| 392 | .getElementsByTagName("div")[0]; |
||
| 393 | filterDiv.appendChild(document.getElementById(filterId)); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | function changeListCustomFilter(selectObj) { |
||
| 397 | var fId = selectObj.options[selectObj.selectedIndex].value; |
||
| 398 | var hasValue = fId !== "{!--Empty--!}"; |
||
| 399 | var varName = selectObj.getAttribute("name"); |
||
| 400 | var varPos = (document.URL.indexOf(varName) - (hasValue ? 0 : 1)); |
||
| 401 | if (varPos > 0) { |
||
| 402 | var url = document.URL.substring(0, varPos); |
||
| 403 | } else { |
||
| 404 | var separator = document.URL.indexOf("?") > 0 ? "&" : "?"; |
||
| 405 | url = document.URL + (hasValue ? separator : ""); |
||
| 406 | } |
||
| 407 | var newUrl = url + (hasValue ? (varName + "=" + fId) : ""); |
||
| 408 | location.assign(newUrl); |
||
| 409 | } |
||
| 410 | |||
| 411 | |||
| 412 | function tablesLoadData() { |
||
| 413 | var tables = document.getElementsByTagName("table"); |
||
| 414 | var envPrior9 = table.iePrior(9); |
||
| 415 | for (var i = 0; i < tables.length; i++) { |
||
| 416 | var isProcessable = envPrior9 ? |
||
| 417 | typeof tables[i]["data-table"] !== 'undefined' : |
||
| 418 | tables[i].hasAttribute("data-table"); |
||
| 419 | if (isProcessable && tables[i].getAttribute("data-table") === "js") { |
||
| 420 | table.LoadData(tables[i].id); |
||
| 421 | table.SetTheTableColumnsHoverEffect(tables[i].id); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | if (table.iePrior(10)) { |
||
| 425 | table.loadJS("/add/helpers/table/add/json2.js"); |
||
| 426 | } |
||
| 427 | |||
| 428 | /*if(table.iePrior(8)){ //can be used to add apropriate tables links modifications |
||
| 429 | // loadCSS("/add/helpers/table/add/ie7-and-down.css"); |
||
| 430 | }*/ |
||
| 431 | } |
||
| 432 | |||
| 433 | /*if(window.addEventListener){window.addEventListener('load',tablesLoadData,false);} else if(window.attachEvent){window.attachEvent('onload',tablesLoadData);}*/ |
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: