simple-ui-builder
Advanced tools
Comparing version 1.0.47 to 1.0.48
{ | ||
"name": "simple-ui-builder", | ||
"version": "1.0.47", | ||
"version": "1.0.48", | ||
"description": "Build user interface like Lego bricks. UI.input(), UI.button(cb), UI.select(arr) etc. For lazy guys as I am.", | ||
@@ -5,0 +5,0 @@ "main": "ui.js", |
1405
ui.js
@@ -8,109 +8,105 @@ /** | ||
(function() { | ||
;(function () { | ||
var UI = this.UI = {} | ||
var UI = this.UI = {}; | ||
UI.input = function (params, cb) { | ||
params = params || {} | ||
UI.input = function(params, cb) { | ||
cb = cb || params.cb || function (value) { | ||
console.log('you pressed enter and input value is >> ' + value + ' <<') | ||
} | ||
params = params || {}; | ||
if ((typeof params == 'function') && (typeof cb == 'object')) { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} | ||
cb = cb || params.cb || function(value) { | ||
console.log("you pressed enter and input value is >> " + value + " <<"); | ||
}; | ||
if ((typeof params == "function") && (typeof cb == "object")) { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
if (typeof params == 'string') params = { | ||
id: params | ||
} | ||
if (typeof params == "string") params = { | ||
id: params | ||
}; | ||
params.id = params.id || 'input' | ||
params.id = params.id || "input"; | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var input = document.createElement("input"); | ||
input.id = params.id; | ||
input.type = params.type || "text"; | ||
var input = document.createElement('input') | ||
input.id = params.id | ||
input.type = params.type || 'text' | ||
if (params.placeholder) input.placeholder = params.placeholder; | ||
input.name = params.name || params.id; | ||
if (params.placeholder) input.placeholder = params.placeholder | ||
input.name = params.name || params.id | ||
if ((params.class !== undefined) || (params.className !== undefined)) { | ||
input.className = params.class || params.className; | ||
} else input.className = "form-control"; //yes, I like bootstrap | ||
input.className = params.class || params.className | ||
} else input.className = 'form-control'; // yes, I like bootstrap | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
input.style[key] = val; | ||
var val = params.style[key] | ||
input.style[key] = val | ||
} | ||
} | ||
input.style.width = input.style.width || "300px"; | ||
input.style.marginTop = input.style.marginTop || "5px"; | ||
input.style.marginBottom = input.style.marginBottom || "5px"; | ||
input.style.width = input.style.width || '300px' | ||
input.style.marginTop = input.style.marginTop || '5px' | ||
input.style.marginBottom = input.style.marginBottom || '5px' | ||
if (params.value === undefined) { | ||
input.value = localStorage["input#" + params.id] || | ||
params.default || ""; | ||
} else input.value = params.value; | ||
input.value = localStorage['input#' + params.id] || | ||
params.default || '' | ||
} else input.value = params.value | ||
input.onkeyup = saveContents; | ||
input.onchange = saveContents; | ||
input.onkeyup = saveContents | ||
input.onchange = saveContents | ||
function saveContents(e) { | ||
localStorage["input#" + params.id] = input.value.trim(); | ||
function saveContents (e) { | ||
localStorage['input#' + params.id] = input.value.trim() | ||
if (e.keyCode === 13) { //checks whether the pressed key is "Enter" | ||
cb(input.value); | ||
if (e.keyCode === 13) { // checks whether the pressed key is "Enter" | ||
cb(input.value) | ||
} | ||
} | ||
params.parent.appendChild(input); | ||
}; | ||
params.parent.appendChild(input) | ||
} | ||
UI.button = function(cb, params) { | ||
if (typeof cb == "object") { | ||
if (typeof params == "function") { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
} else if (typeof params == "undefined") { | ||
params = cb; | ||
cb = console.log; | ||
UI.button = function (cb, params) { | ||
if (typeof cb == 'object') { | ||
if (typeof params == 'function') { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} else if (typeof params == 'undefined') { | ||
params = cb | ||
cb = console.log | ||
} | ||
} | ||
if (typeof cb == "string") { | ||
if (typeof params == "function") { | ||
var temp2 = params; | ||
if (typeof cb == 'string') { | ||
if (typeof params == 'function') { | ||
var temp2 = params | ||
params = { | ||
innerHTML: cb | ||
}; | ||
cb = temp2; | ||
} else if (typeof params == "undefined") { | ||
} | ||
cb = temp2 | ||
} else if (typeof params == 'undefined') { | ||
params = { | ||
innerHTML: cb | ||
}; | ||
cb = console.log; | ||
} | ||
cb = console.log | ||
} | ||
} | ||
params = params || {}; | ||
params = params || {} | ||
cb = cb || function(id) { | ||
console.log(id + " clicked"); | ||
}; | ||
cb = cb || function (id) { | ||
console.log(id + ' clicked') | ||
} | ||
@@ -121,69 +117,66 @@ params.className = (params.class !== undefined) ? | ||
params.className : | ||
"btn btn-default"); | ||
'btn btn-default') | ||
params.innerHTML = params.innerHTML || params.title || "Action"; | ||
params.innerHTML = params.innerHTML || params.title || 'Action' | ||
params.id = params.id || UI.slug(params.innerHTML); | ||
params.id = params.id || UI.slug(params.innerHTML) | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var button = document.createElement("button"); | ||
button.id = params.id; | ||
button.className = params.className; | ||
button.innerHTML = params.innerHTML; | ||
var button = document.createElement('button') | ||
button.id = params.id | ||
button.className = params.className | ||
button.innerHTML = params.innerHTML | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
button.style[key] = val; | ||
var val = params.style[key] | ||
button.style[key] = val | ||
} | ||
} | ||
button.style.margin = button.style.margin || params.margin || "10px"; | ||
button.onclick = function() { | ||
cb(button.id); | ||
}; | ||
params.parent.appendChild(button); | ||
}; | ||
button.style.margin = button.style.margin || params.margin || '10px' | ||
button.onclick = function () { | ||
cb(button.id) | ||
} | ||
params.parent.appendChild(button) | ||
} | ||
UI.buttons = function(arr, cb, params) { | ||
UI.buttons = function (arr, cb, params) { | ||
if (!arr || !arr.length) | ||
return console.warn("no array to build buttons!"); | ||
return console.warn('no array to build buttons!') | ||
if (typeof cb == "object") { | ||
if (typeof params == "function") { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
} else if (typeof params == "undefined") { | ||
params = cb; | ||
cb = console.log; | ||
if (typeof cb == 'object') { | ||
if (typeof params == 'function') { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} else if (typeof params == 'undefined') { | ||
params = cb | ||
cb = console.log | ||
} | ||
} | ||
params = params || {}; | ||
params = params || {} | ||
cb = cb || function(id) { | ||
console.log(id + " clicked"); | ||
}; | ||
cb = cb || function (id) { | ||
console.log(id + ' clicked') | ||
} | ||
if (typeof params == "string") params = { | ||
innerHTML: params, | ||
id: params | ||
}; | ||
if (typeof params == 'string') params = { | ||
innerHTML: params, | ||
id: params | ||
} | ||
var i = 0 | ||
var l = arr.length | ||
var i = 0; | ||
var l = arr.length; | ||
;(function next () { | ||
var item = arr[i] | ||
(function next() { | ||
var item = arr[i]; | ||
if (typeof item == "number") item = item.toString(); | ||
if (typeof item == 'number') item = item.toString() | ||
var buttonParams = { | ||
@@ -193,145 +186,139 @@ parent: params.parent, | ||
style: { | ||
margin: "2px" | ||
margin: '2px' | ||
} | ||
}; | ||
UI.button(cb, buttonParams); | ||
i++; | ||
if (i < l) next(); | ||
})(); | ||
}; | ||
} | ||
UI.button(cb, buttonParams) | ||
i++ | ||
if (i < l) next() | ||
})() | ||
} | ||
UI.br = function (params) { | ||
params = params || {} | ||
UI.br = function(params) { | ||
params = params || {}; | ||
if (typeof params == "string") { | ||
if (typeof params == 'string') { | ||
params = { | ||
parent: document.querySelector(params) | ||
}; | ||
} else if (typeof params == "number") { | ||
} | ||
} else if (typeof params == 'number') { | ||
params = { | ||
id: params.toString() | ||
}; | ||
} | ||
} else if (params instanceof HTMLElement) { | ||
params = { | ||
parent: params | ||
}; | ||
} | ||
} | ||
if (typeof params.parent == "string") | ||
params.parent = document.querySelector(params.parent); | ||
else params.parent = params.parent || document.querySelector("#ui") || | ||
document.body; | ||
if (typeof params.parent == 'string') | ||
params.parent = document.querySelector(params.parent) | ||
else params.parent = params.parent || document.querySelector('#ui') || | ||
document.body | ||
params.id = params.id || "br"; | ||
params.id = params.id || 'br' | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var br = document.createElement("br"); | ||
br.id = params.id; | ||
params.parent.appendChild(br); | ||
}; | ||
var br = document.createElement('br') | ||
br.id = params.id | ||
params.parent.appendChild(br) | ||
} | ||
UI.radio = function(arr, params, cb) { | ||
UI.radio = function (arr, params, cb) { | ||
if (!arr || !arr.length) | ||
return console.warn("no array to build radios!"); | ||
return console.warn('no array to build radios!') | ||
params = params || {}; | ||
params = params || {} | ||
cb = cb || params.cb || function(value) { | ||
console.log("you clicked radio >> " + value + " <<"); | ||
}; | ||
cb = cb || params.cb || function (value) { | ||
console.log('you clicked radio >> ' + value + ' <<') | ||
} | ||
if ((typeof params == "function") && (typeof cb == "object")) { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
if ((typeof params == 'function') && (typeof cb == 'object')) { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} | ||
if (typeof params == "string") params = { | ||
parent: document.querySelector(params) | ||
}; | ||
if (typeof params == 'string') params = { | ||
parent: document.querySelector(params) | ||
} | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
params.id = params.id || "radio"; | ||
params.id = params.id || 'radio' | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var i = 0; | ||
var l = arr.length; | ||
var i = 0 | ||
var l = arr.length | ||
createRadio(); | ||
createRadio() | ||
function createRadio() { | ||
var item = arr[i]; | ||
var radio = document.createElement("input"); | ||
radio.type = "radio"; | ||
radio.id = item; | ||
radio.name = params.id; | ||
function createRadio () { | ||
var item = arr[i] | ||
var radio = document.createElement('input') | ||
radio.type = 'radio' | ||
radio.id = item | ||
radio.name = params.id | ||
if (params.default && (params.default == item)) { | ||
radio.checked = true; | ||
radio.checked = true | ||
} | ||
radio.onclick = function() { | ||
cb(radio.id); | ||
}; | ||
radio.onclick = function () { | ||
cb(radio.id) | ||
} | ||
params.parent.appendChild(radio); | ||
params.parent.appendChild(document.createTextNode(item)); | ||
if (params.br) params.parent.appendChild(document.createElement("br")); | ||
i++; | ||
params.parent.appendChild(radio) | ||
params.parent.appendChild(document.createTextNode(item)) | ||
if (params.br) params.parent.appendChild(document.createElement('br')) | ||
i++ | ||
if (i < l) { | ||
createRadio(); | ||
createRadio() | ||
} | ||
} | ||
}; | ||
} | ||
UI.checkbox = function (params, cb) { | ||
params = params || {} | ||
UI.checkbox = function(params, cb) { | ||
cb = cb || params.cb || function (value) { | ||
console.log('you clicked checkbox >> ' + value + ' <<') | ||
} | ||
params = params || {}; | ||
if ((typeof params == 'function') && (typeof cb == 'object')) { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} | ||
cb = cb || params.cb || function(value) { | ||
console.log("you clicked checkbox >> " + value + " <<"); | ||
}; | ||
if ((typeof params == "function") && (typeof cb == "object")) { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
if (typeof params == 'string') params = { | ||
text: params, | ||
id: params | ||
} | ||
if (typeof params == "string") params = { | ||
text: params, | ||
id: params | ||
}; | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
params.id = params.id || "checkbox"; | ||
params.id = params.id || 'checkbox' | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var checkbox = document.createElement("input"); | ||
checkbox.type = "checkbox"; | ||
checkbox.id = params.id; | ||
checkbox.checked = Boolean(params.checked); | ||
checkbox.onclick = function() { | ||
cb(checkbox.id + ' ' + checkbox.checked); | ||
}; | ||
params.parent.appendChild(checkbox); | ||
var checkbox = document.createElement('input') | ||
checkbox.type = 'checkbox' | ||
checkbox.id = params.id | ||
checkbox.checked = Boolean(params.checked) | ||
checkbox.onclick = function () { | ||
cb(checkbox.id + ' ' + checkbox.checked) | ||
} | ||
params.parent.appendChild(checkbox) | ||
@@ -341,498 +328,474 @@ if (params.text) { | ||
parent: params.parent, | ||
id: UI.slug(params.id) + "-span", | ||
id: UI.slug(params.id) + '-span', | ||
innerHTML: params.text | ||
}; | ||
} | ||
var exSpanNode = document.getElementById(spanParams.id); | ||
if (exSpanNode) params.parent.removeChild(exSpanNode); | ||
UI.span(spanParams); | ||
var exSpanNode = document.getElementById(spanParams.id) | ||
if (exSpanNode) params.parent.removeChild(exSpanNode) | ||
UI.span(spanParams) | ||
} | ||
}; | ||
} | ||
UI.checkboxes = function(arr, cb, params) { | ||
UI.checkboxes = function (arr, cb, params) { | ||
if (!arr || !arr.length) | ||
return console.warn("no array to build buttons!"); | ||
return console.warn('no array to build buttons!') | ||
if (typeof cb == "object") { | ||
if (typeof params == "function") { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
} else if (typeof params == "undefined") { | ||
params = cb; | ||
cb = console.log; | ||
if (typeof cb == 'object') { | ||
if (typeof params == 'function') { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} else if (typeof params == 'undefined') { | ||
params = cb | ||
cb = console.log | ||
} | ||
} | ||
params = params || {}; | ||
params = params || {} | ||
cb = cb || function(id) { | ||
console.log(id + " checked"); | ||
}; | ||
cb = cb || function (id) { | ||
console.log(id + ' checked') | ||
} | ||
var i = 0; | ||
var l = arr.length; | ||
var i = 0 | ||
var l = arr.length | ||
(function next() { | ||
var item = arr[i]; | ||
;(function next () { | ||
var item = arr[i] | ||
if (typeof item == "number") item = item.toString(); | ||
if (typeof item == 'number') item = item.toString() | ||
var checkboxParams = { | ||
parent: params.parent, | ||
text: item, | ||
id: item.toLowerCase().replace(/[^\w\d]/g, "-"), | ||
id: item.toLowerCase().replace(/[^\w\d]/g, '-'), | ||
style: { | ||
margin: "2px" | ||
margin: '2px' | ||
} | ||
}; | ||
UI.checkbox(cb, checkboxParams); | ||
if (params.br) params.parent.appendChild(document.createElement("br")); | ||
} | ||
UI.checkbox(cb, checkboxParams) | ||
if (params.br) params.parent.appendChild(document.createElement('br')) | ||
i++; | ||
if (i < l) next(); | ||
})(); | ||
}; | ||
i++ | ||
if (i < l) next() | ||
})() | ||
} | ||
UI.fileReader = function (cb, params) { | ||
params = params || {} | ||
UI.fileReader = function(cb, params) { | ||
params = params || {}; | ||
if ((typeof params == "function") && (typeof cb == "object")) { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
if ((typeof params == 'function') && (typeof cb == 'object')) { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} | ||
cb = cb || params.cb || console.log; | ||
cb = cb || params.cb || console.log | ||
params.id = params.id || "file-reader"; | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
params.id = params.id || 'file-reader' | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var fileInput = document.createElement("input"); | ||
params.parent.appendChild(fileInput); | ||
fileInput.type = "file"; | ||
fileInput.id = params.id; | ||
var fileInput = document.createElement('input') | ||
params.parent.appendChild(fileInput) | ||
fileInput.type = 'file' | ||
fileInput.id = params.id | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
fileInput.style[key] = val; | ||
var val = params.style[key] | ||
fileInput.style[key] = val | ||
} | ||
} | ||
fileInput.onchange = function(evt) { | ||
var fileToRead = evt.target.files[0]; | ||
var fileType = fileToRead.name.split(/\./).pop(); | ||
fileInput.onchange = function (evt) { | ||
var fileToRead = evt.target.files[0] | ||
var fileType = fileToRead.name.split(/\./).pop() | ||
if (parent.bypassFileReader) return cb(fileToRead); | ||
var fileReader = new FileReader(); | ||
fileReader.onload = function(e) { | ||
if (parent.bypassFileReader) return cb(fileToRead) | ||
var fileReader = new FileReader() | ||
fileReader.onload = function (e) { | ||
var contents = e.target.result | ||
if (params.json && (['{', '['].indexOf(contents.slice(0, 1)) != -1)) | ||
contents = JSON.parse(contents) | ||
var contents = e.target.result; | ||
if (params.json && (["{", "["].indexOf(contents.slice(0, 1)) != -1)) | ||
contents = JSON.parse(contents); | ||
cb(contents, fileToRead) | ||
} | ||
cb(contents, fileToRead); | ||
}; | ||
if ((["zip", "kmz"].indexOf(fileType) != -1) || | ||
if ((['zip', 'kmz'].indexOf(fileType) != -1) || | ||
(params.readAsArrayBuffer)) { | ||
fileReader.readAsArrayBuffer(fileToRead); | ||
} else if ((["xls", "xlsx"].indexOf(fileType) != -1) || | ||
fileReader.readAsArrayBuffer(fileToRead) | ||
} else if ((['xls', 'xlsx'].indexOf(fileType) != -1) || | ||
(params.readAsBinaryString)) { | ||
fileReader.readAsBinaryString(fileToRead); | ||
fileReader.readAsBinaryString(fileToRead) | ||
} else { | ||
fileReader.readAsText(fileToRead, params.encoding); | ||
fileReader.readAsText(fileToRead, params.encoding) | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
UI.download = function (str, params) { | ||
params = params || {} | ||
UI.download = function(str, params) { | ||
if (!str) console.warn('nothing to save!') | ||
params = params || {}; | ||
if (typeof str == 'object') str = JSON.stringify(str, null, '\t') | ||
if (!str) console.warn("nothing to save!"); | ||
params.id = params.id || params.name || 'download-link' | ||
params.name = params.name || 'renameMe.json' | ||
params.type = params.type || 'application/json' | ||
if (typeof str == "object") str = JSON.stringify(str, null, "\t"); | ||
params.id = params.id || params.name || "download-link"; | ||
params.name = params.name || "renameMe.json"; | ||
params.type = params.type || "application/json"; | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var a = document.createElement('a'); | ||
var a = document.createElement('a') | ||
if (params.noBlob) { | ||
var data = params.type + encodeURIComponent(str); | ||
a.href = 'data:' + data; | ||
var data = params.type + encodeURIComponent(str) | ||
a.href = 'data:' + data | ||
} else { | ||
var blobObj = new Blob([str], { | ||
type: params.type | ||
}); | ||
var blobUrl = URL.createObjectURL(blobObj); | ||
a.href = blobUrl; | ||
}) | ||
var blobUrl = URL.createObjectURL(blobObj) | ||
a.href = blobUrl | ||
} | ||
a.download = params.name; | ||
a.textContent = params.name; | ||
a.id = params.id; | ||
a.download = params.name | ||
a.textContent = params.name | ||
a.id = params.id | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
a.style[key] = val; | ||
var val = params.style[key] | ||
a.style[key] = val | ||
} | ||
} | ||
params.parent.appendChild(a); | ||
}; | ||
params.parent.appendChild(a) | ||
} | ||
UI.span = function (params) { | ||
if (typeof params == 'string') params = { | ||
innerHTML: params | ||
} | ||
UI.span = function(params) { | ||
if (!params.innerHTML) return 'no innerHTML' | ||
if (typeof params == "string") params = { | ||
innerHTML: params, | ||
}; | ||
params.id = params.id || UI.slug(params.innerHTML) | ||
if (!params.innerHTML) return "no innerHTML"; | ||
params.id = params.id || UI.slug(params.innerHTML); | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var span = document.createElement("span"); | ||
span.innerHTML = params.innerHTML; | ||
span.id = params.id; | ||
if (params.className) span.className = params.className; | ||
var span = document.createElement('span') | ||
span.innerHTML = params.innerHTML | ||
span.id = params.id | ||
if (params.className) span.className = params.className | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
span.style[key] = val; | ||
var val = params.style[key] | ||
span.style[key] = val | ||
} | ||
} | ||
params.parent.appendChild(span); | ||
}; | ||
params.parent.appendChild(span) | ||
} | ||
UI.img = function (params) { | ||
if (typeof params == 'string') params = { | ||
src: params | ||
} | ||
UI.img = function(params) { | ||
if (!params.src) return 'no src' | ||
if (typeof params == "string") params = { | ||
src: params, | ||
}; | ||
params.id = params.id || 'img' | ||
if (!params.src) return "no src"; | ||
params.id = params.id || "img"; | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var img = document.createElement("img"); | ||
img.src = params.src; | ||
if (params.alt) img.alt = params.alt; | ||
img.id = params.id; | ||
if (params.className) img.className = params.className; | ||
var img = document.createElement('img') | ||
img.src = params.src | ||
if (params.alt) img.alt = params.alt | ||
img.id = params.id | ||
if (params.className) img.className = params.className | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
img.style[key] = val; | ||
var val = params.style[key] | ||
img.style[key] = val | ||
} | ||
} | ||
params.parent.appendChild(img); | ||
}; | ||
params.parent.appendChild(img) | ||
} | ||
UI.link = function (params) { | ||
if (typeof params == 'string') params = { | ||
href: params, | ||
title: params | ||
} | ||
UI.link = function(params) { | ||
if (!params.href) return 'no href' | ||
if (typeof params == "string") params = { | ||
href: params, | ||
title: params | ||
}; | ||
params.innerHTML = params.innerHTML || params.href | ||
if (!params.href) return "no href"; | ||
params.id = params.id || UI.slug(params.innerHTML) | ||
params.innerHTML = params.innerHTML || params.href; | ||
params.id = params.id || UI.slug(params.innerHTML); | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var a = document.createElement("a"); | ||
a.id = params.id; | ||
a.href = params.href; | ||
if (params.targetBlank) a.target = "_blank"; | ||
a.innerHTML = params.innerHTML; | ||
var a = document.createElement('a') | ||
a.id = params.id | ||
a.href = params.href | ||
if (params.targetBlank) a.target = '_blank' | ||
a.innerHTML = params.innerHTML | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
a.style[key] = val; | ||
var val = params.style[key] | ||
a.style[key] = val | ||
} | ||
} | ||
params.parent.appendChild(a); | ||
}; | ||
params.parent.appendChild(a) | ||
} | ||
UI.select = function(arr, cb, params) { | ||
UI.select = function (arr, cb, params) { | ||
if (!arr || !arr.length) | ||
return console.warn("no array to build select!"); | ||
return console.warn('no array to build select!') | ||
if ((typeof params == "function") && (typeof cb == "object")) { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
if ((typeof params == 'function') && (typeof cb == 'object')) { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} | ||
if (!params && (typeof cb == "object")) { | ||
params = cb; | ||
cb = console.log; | ||
if (!params && (typeof cb == 'object')) { | ||
params = cb | ||
cb = console.log | ||
} | ||
params = params || {}; | ||
params = params || {} | ||
params.id = params.id || ""; | ||
params.id = params.id || '' | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id + "Select"); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id + 'Select') | ||
if (exNode) params.parent.removeChild(exNode) | ||
cb = cb || console.log; | ||
cb = cb || console.log | ||
var select = document.createElement("select"); | ||
select.className = params.className || "selectpicker"; | ||
select.id = params.id + "Select"; | ||
var select = document.createElement('select') | ||
select.className = params.className || 'selectpicker' | ||
select.id = params.id + 'Select' | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
select.style[key] = val; | ||
var val = params.style[key] | ||
select.style[key] = val | ||
} | ||
} | ||
var firstOption = document.createElement("option"); | ||
firstOption.innerHTML = params.firstRowText || "select " + params.id; | ||
select.appendChild(firstOption); | ||
var firstOption = document.createElement('option') | ||
firstOption.innerHTML = params.firstRowText || 'select ' + params.id | ||
select.appendChild(firstOption) | ||
for (var i = 0; i < arr.length; i++) { | ||
var item = arr[i]; | ||
var item = arr[i] | ||
var option = document.createElement("option"); | ||
option.id = params.id + "Option"; | ||
option.innerHTML = params.innerHTML || item; | ||
option.value = params.value || item; | ||
var option = document.createElement('option') | ||
option.id = params.id + 'Option' | ||
option.innerHTML = params.innerHTML || item | ||
option.value = params.value || item | ||
if (params.default && params.default == item) { | ||
option.selected = true; | ||
option.selected = true | ||
} | ||
select.appendChild(option); | ||
select.appendChild(option) | ||
} | ||
params.parent.appendChild(select); | ||
params.parent.appendChild(select) | ||
select.onchange = function() { | ||
select.onchange = function () { | ||
var selectedOptionNode = | ||
document.querySelector("option#" + params.id + "Option" + ":checked"); | ||
if (selectedOptionNode) cb(selectedOptionNode.value); | ||
}; | ||
}; | ||
document.querySelector('option#' + params.id + 'Option' + ':checked') | ||
if (selectedOptionNode) cb(selectedOptionNode.value) | ||
} | ||
} | ||
UI.textarea = function (params) { | ||
params = params || {} | ||
params.id = params.id || 'textarea' | ||
params.cols = (params.cols || 60).toString() | ||
params.rows = (params.rows || 12).toString() | ||
params.fontSize = params.fontSize || '12px' | ||
UI.textarea = function(params) { | ||
params = params || {}; | ||
params.id = params.id || "textarea"; | ||
params.cols = (params.cols || 60).toString(); | ||
params.rows = (params.rows || 12).toString(); | ||
params.fontSize = params.fontSize || "12px"; | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var textarea = document.createElement("textarea"); | ||
textarea.cols = params.cols; | ||
textarea.rows = params.rows; | ||
textarea.id = params.id; | ||
textarea.className = params.className || params.class || "form-control"; | ||
var textarea = document.createElement('textarea') | ||
textarea.cols = params.cols | ||
textarea.rows = params.rows | ||
textarea.id = params.id | ||
textarea.className = params.className || params.class || 'form-control' | ||
if (params.style) { | ||
for (var key in params.style) { | ||
var val = params.style[key]; | ||
textarea.style[key] = val; | ||
var val = params.style[key] | ||
textarea.style[key] = val | ||
} | ||
} | ||
textarea.style.fontFamily = "monospace"; | ||
textarea.style.fontSize = params.fontSize; | ||
params.parent.appendChild(textarea); | ||
textarea.style.fontFamily = 'monospace' | ||
textarea.style.fontSize = params.fontSize | ||
params.parent.appendChild(textarea) | ||
textarea.value = localStorage["textarea#" + params.id] || | ||
params.value || ""; | ||
textarea.value = localStorage['textarea#' + params.id] || | ||
params.value || '' | ||
textarea.onkeyup = saveContents; | ||
textarea.onchange = saveContents; | ||
textarea.onkeyup = saveContents | ||
textarea.onchange = saveContents | ||
function saveContents(e) { | ||
localStorage["textarea#" + params.id] = textarea.value.trim(); | ||
function saveContents (e) { | ||
localStorage['textarea#' + params.id] = textarea.value.trim() | ||
} | ||
}; | ||
} | ||
UI.TB = function(cb, params) { | ||
if ((typeof params == "function") && (typeof cb == "object")) { | ||
var temp = params; | ||
params = cb; | ||
cb = temp; | ||
UI.TB = function (cb, params) { | ||
if ((typeof params == 'function') && (typeof cb == 'object')) { | ||
var temp = params | ||
params = cb | ||
cb = temp | ||
} | ||
cb = cb || console.log; | ||
cb = cb || console.log | ||
params = params || {}; | ||
params = params || {} | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
params.className = ""; | ||
UI.textarea(params); | ||
params.className = '' | ||
UI.textarea(params) | ||
params.parent.appendChild(document.createElement("br")); | ||
params.parent.appendChild(document.createElement('br')) | ||
var textareaId = params.id; | ||
var textareaNode = document.querySelector("textarea#" + params.id); | ||
var textareaId = params.id | ||
var textareaNode = document.querySelector('textarea#' + params.id) | ||
if (!params.noAction) { | ||
var actionParams = {}; | ||
actionParams.parent = params.parent; | ||
actionParams.id = params.id + "Action"; | ||
actionParams.innerHTML = params.buttonText || "Action"; | ||
actionParams.className = ""; | ||
var actionParams = {} | ||
actionParams.parent = params.parent | ||
actionParams.id = params.id + 'Action' | ||
actionParams.innerHTML = params.buttonText || 'Action' | ||
actionParams.className = '' | ||
actionParams.style = { | ||
margin: "0px" | ||
}; | ||
margin: '0px' | ||
} | ||
UI.button(actionParams, function() { | ||
UI.button(actionParams, function () { | ||
var textareaArr = textareaNode.value | ||
.trim() | ||
.split(/\n\r?/).filter(function(a) { | ||
return a; | ||
}); | ||
cb(textareaArr); | ||
}); | ||
.split(/\n\r?/).filter(function (a) { | ||
return a | ||
}) | ||
cb(textareaArr) | ||
}) | ||
} | ||
var clearParams = {}; | ||
clearParams.parent = params.parent; | ||
clearParams.id = params.id + "Clear"; | ||
clearParams.innerHTML = "Clear"; | ||
clearParams.className = ""; | ||
var clearParams = {} | ||
clearParams.parent = params.parent | ||
clearParams.id = params.id + 'Clear' | ||
clearParams.innerHTML = 'Clear' | ||
clearParams.className = '' | ||
clearParams.style = { | ||
margin: "0px", | ||
marginLeft: "5px" | ||
}; | ||
margin: '0px', | ||
marginLeft: '5px' | ||
} | ||
UI.button(clearParams, function() { | ||
textareaNode.value = ""; | ||
localStorage[textareaId] = ""; | ||
}); | ||
}; | ||
UI.button(clearParams, function () { | ||
textareaNode.value = '' | ||
localStorage[textareaId] = '' | ||
}) | ||
} | ||
UI.table = function(arr, params) { | ||
if ((!arr) && (typeof(arr[0]) != "object")) { | ||
alert("Argument is not an array with objects"); | ||
UI.table = function (arr, params) { | ||
if ((!arr) && (typeof (arr[0]) != 'object')) { | ||
alert('Argument is not an array with objects') | ||
} | ||
if (typeof arr[0] != "object") arr = arr.map(function(a) { | ||
return { | ||
value: a | ||
}; | ||
}); | ||
if (typeof arr[0] != 'object') arr = arr.map(function (a) { | ||
return { | ||
value: a | ||
} | ||
}) | ||
var reDateTimeJS = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/; | ||
var reDateTimeJS = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/ | ||
if (typeof params == "string") params = { | ||
parent: params | ||
}; | ||
if (typeof params == 'string') params = { | ||
parent: params | ||
} | ||
params = params || {}; | ||
if (typeof params.selectable != "boolean") params.selectable = false; | ||
params = params || {} | ||
if (typeof params.selectable != 'boolean') params.selectable = false | ||
params.tableId = params.tableId || params.id || "printedTable"; | ||
params.tableId = params.tableId || params.id || 'printedTable' | ||
params.showColumns = params.showColumns || | ||
params.columns || params.cols || []; | ||
params.columns || params.cols || [] | ||
params.hideColumns = params.hideColumns || []; | ||
params.hideColumns = params.hideColumns || [] | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
if (typeof params.hideHead != "boolean") params.hideHead = false; | ||
if (typeof params.sortColumns != "boolean") params.sortColumns = false; | ||
if (typeof params.hideHead != 'boolean') params.hideHead = false | ||
if (typeof params.sortColumns != 'boolean') params.sortColumns = false | ||
//console.log(params); | ||
var columns = []; | ||
var numCol = {}; | ||
var cell = ""; | ||
var chBoxCol; | ||
// console.log(params) | ||
var columns = [] | ||
var numCol = {} | ||
var cell = '' | ||
var chBoxCol | ||
if (params.showColumns.length !== 0) { | ||
columns = params.showColumns; | ||
columns = params.showColumns | ||
} else { | ||
for (var i = 0; i < arr.length; i++) { //собираем ключи со всех объектов | ||
for (var i = 0; i < arr.length; i++) { // собираем ключи со всех объектов | ||
for (var key in arr[i]) { | ||
if ((columns.indexOf(key) == -1) && | ||
(params.hideColumns.indexOf(key) == -1)) { | ||
columns.push(key); | ||
columns.push(key) | ||
var colCell = arr[i][key]; | ||
numCol[key] = (!isNaN(colCell) && (typeof colCell == "number")); | ||
var colCell = arr[i][key] | ||
numCol[key] = (!isNaN(colCell) && (typeof colCell == 'number')) | ||
} | ||
@@ -843,113 +806,109 @@ } | ||
if (params.sortColumns) columns.sort(); | ||
if (params.sortColumns) columns.sort() | ||
var exTable = document.getElementById(params.tableId); | ||
if (exTable) params.parent.removeChild(exTable); | ||
var exTable = document.getElementById(params.tableId) | ||
if (exTable) params.parent.removeChild(exTable) | ||
var table = document.createElement('table'); | ||
table.id = params.tableId; | ||
var table = document.createElement('table') | ||
table.id = params.tableId | ||
if (params.style) { | ||
for (var style in params.style) { | ||
var val = params.style[style]; | ||
table.style[style] = val; | ||
var val = params.style[style] | ||
table.style[style] = val | ||
} | ||
} | ||
table.style.margin = table.style.margin || "10px"; | ||
table.style.fontFamily = table.style.fontFamily || "monospace"; | ||
table.style.width = table.style.width || "auto 90%"; | ||
table.style.borderCollapse = table.style.borderCollapse || "collapse"; | ||
table.style.margin = table.style.margin || '10px' | ||
table.style.fontFamily = table.style.fontFamily || 'monospace' | ||
table.style.width = table.style.width || 'auto 90%' | ||
table.style.borderCollapse = table.style.borderCollapse || 'collapse' | ||
params.parent.appendChild(table); | ||
params.parent.appendChild(table) | ||
if (!params.hideHead) { | ||
var thead = table.createTHead(); | ||
var hRow = thead.insertRow(0); | ||
var thead = table.createTHead() | ||
var hRow = thead.insertRow(0) | ||
if (params.selectable) { | ||
var thh = document.createElement('th'); | ||
var thh = document.createElement('th') | ||
thh.style.background = "#f6f6f6"; | ||
thh.style.padding = "3px"; | ||
thh.style.border = "1px solid black"; | ||
thh.style.background = '#f6f6f6' | ||
thh.style.padding = '3px' | ||
thh.style.border = '1px solid black' | ||
chBoxCol = document.createElement("input"); | ||
chBoxCol.id = params.tableId + "ColCheckbox"; | ||
chBoxCol.type = "checkbox"; | ||
chBoxCol.checked = Boolean(params.checked); | ||
chBoxCol = document.createElement('input') | ||
chBoxCol.id = params.tableId + 'ColCheckbox' | ||
chBoxCol.type = 'checkbox' | ||
chBoxCol.checked = Boolean(params.checked) | ||
thh.appendChild(chBoxCol); | ||
hRow.appendChild(thh); | ||
thh.appendChild(chBoxCol) | ||
hRow.appendChild(thh) | ||
} | ||
for (var h = 0; h < columns.length; h++) { | ||
var th = document.createElement('th'); | ||
th.style.background = "#f6f6f6"; | ||
th.style.padding = "3px"; | ||
th.style.border = "1px solid black"; | ||
var th = document.createElement('th') | ||
th.style.background = '#f6f6f6' | ||
th.style.padding = '3px' | ||
th.style.border = '1px solid black' | ||
th.appendChild(document.createTextNode(columns[h])); | ||
hRow.appendChild(th); | ||
th.appendChild(document.createTextNode(columns[h])) | ||
hRow.appendChild(th) | ||
} | ||
} | ||
var tbody = document.createElement('tbody'); | ||
table.appendChild(tbody); | ||
var tbody = document.createElement('tbody') | ||
table.appendChild(tbody) | ||
if (arr.length !== 0) { | ||
for (var n = 0; n < arr.length; n++) { //собираем данные полей, чистим | ||
var dRow = tbody.insertRow(-1); | ||
dRow.id = n; | ||
var oneObj = arr[n]; | ||
for (var n = 0; n < arr.length; n++) { // собираем данные полей, чистим | ||
var dRow = tbody.insertRow(-1) | ||
dRow.id = n | ||
var oneObj = arr[n] | ||
if (params.selectable) { | ||
var tdch = document.createElement('td'); | ||
tdch.style.padding = "3px"; | ||
tdch.style.border = "1px solid black"; | ||
var tdch = document.createElement('td') | ||
tdch.style.padding = '3px' | ||
tdch.style.border = '1px solid black' | ||
var chBoxRow = document.createElement("input"); | ||
chBoxRow.className = params.tableId + "RowCheckbox"; | ||
chBoxRow.id = n; | ||
chBoxRow.type = "checkbox"; | ||
chBoxRow.checked = Boolean(params.checked); | ||
tdch.appendChild(chBoxRow); | ||
dRow.appendChild(tdch); | ||
var chBoxRow = document.createElement('input') | ||
chBoxRow.className = params.tableId + 'RowCheckbox' | ||
chBoxRow.id = n | ||
chBoxRow.type = 'checkbox' | ||
chBoxRow.checked = Boolean(params.checked) | ||
tdch.appendChild(chBoxRow) | ||
dRow.appendChild(tdch) | ||
} | ||
for (var l = 0; l < columns.length; l++) { | ||
cell = oneObj[columns[l]]; | ||
cell = (((cell !== undefined) && (cell !== null)) ? cell : ""); | ||
if (cell.constructor === Array) cell = cell.join(", "); | ||
if (cell.constructor === Object) cell = JSON.stringify(cell); | ||
cell = oneObj[columns[l]] | ||
cell = (((cell !== undefined) && (cell !== null)) ? cell : '') | ||
if (cell.constructor === Array) cell = cell.join(', ') | ||
if (cell.constructor === Object) cell = JSON.stringify(cell) | ||
if (typeof cell == "string") { | ||
cell = cell.replace(params.quotes, "'"); | ||
if (typeof cell == 'string') { | ||
cell = cell.replace(params.quotes, "'") | ||
if (reDateTimeJS.test(cell)) cell = | ||
new Date(cell).toLocaleDateString(); | ||
new Date(cell).toLocaleDateString() | ||
} | ||
var td = document.createElement('td'); | ||
var td = document.createElement('td') | ||
if (/<a.+<\/a>/.test(cell)) { | ||
td.appendChild(new DOMParser() | ||
.parseFromString(cell, "text/html") | ||
.querySelector("a")); | ||
.parseFromString(cell, 'text/html') | ||
.querySelector('a')) | ||
} else if (/<img.+?>/.test(cell)) { | ||
td.appendChild(new DOMParser() | ||
.parseFromString(cell, "text/html") | ||
.querySelector("img")); | ||
.parseFromString(cell, 'text/html') | ||
.querySelector('img')) | ||
} else if (/<input.+?>/.test(cell)) { | ||
td.appendChild(new DOMParser() | ||
.parseFromString(cell, "text/html") | ||
.querySelector("input")); | ||
.parseFromString(cell, 'text/html') | ||
.querySelector('input')) | ||
} else { | ||
td.appendChild(document.createTextNode(cell)); | ||
td.appendChild(document.createTextNode(cell)) | ||
} | ||
td.style.padding = "3px"; | ||
td.style.border = "1px solid black"; | ||
td.style.padding = '3px' | ||
td.style.border = '1px solid black' | ||
dRow.appendChild(td); | ||
dRow.appendChild(td) | ||
} | ||
@@ -960,178 +919,176 @@ } | ||
if (chBoxCol) { | ||
chBoxCol.onchange = function() { | ||
chBoxCol.onchange = function () { | ||
var checkboxes = | ||
document.querySelectorAll("." + params.tableId + "RowCheckbox"); | ||
document.querySelectorAll('.' + params.tableId + 'RowCheckbox') | ||
for (var i = 0; i < checkboxes.length; i++) { | ||
checkboxes[i].checked = !checkboxes[i].checked; | ||
checkboxes[i].checked = !checkboxes[i].checked | ||
} | ||
}; | ||
} | ||
} | ||
}; | ||
} | ||
UI.addRow = function (obj, params) { | ||
params = params || {} | ||
params.selectable = params.selectable || false | ||
params.mdl = params.mdl || false | ||
params.showColumns = params.showColumns || params.cols || params.columns || [] | ||
params.tableId = params.tableId || params.id || 'printedTable' | ||
UI.addRow = function(obj, params) { | ||
params = params || {}; | ||
params.selectable = params.selectable || false; | ||
params.mdl = params.mdl || false; | ||
params.showColumns = params.showColumns || params.cols || params.columns || []; | ||
params.tableId = params.tableId || params.id || "printedTable"; | ||
var tableExists = document.getElementById(params.tableId) | ||
if (!tableExists) Table.print([], params) | ||
var tableExists = document.getElementById(params.tableId); | ||
if (!tableExists) Table.print([], params); | ||
var reDateTimeJS = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/ | ||
var reDateTimeJS = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/; | ||
var columns = [] | ||
var cell = '' | ||
var columns = []; | ||
var cell = ""; | ||
if (params.showColumns.length !== 0) { | ||
columns = params.showColumns; | ||
columns = params.showColumns | ||
} else { | ||
columns = Object.keys(obj); | ||
columns = Object.keys(obj) | ||
} | ||
var tbody = document.querySelector("#" + params.tableId).querySelector("tbody"); | ||
var dRow = tbody.insertRow(-1); | ||
var tbody = document.querySelector('#' + params.tableId).querySelector('tbody') | ||
var dRow = tbody.insertRow(-1) | ||
if (params.selectable) { | ||
var tds = document.createElement('td'); | ||
var tds = document.createElement('td') | ||
if (!params.mdl) { | ||
tds.style.padding = "3px"; | ||
tds.style.border = "1px solid black"; | ||
tds.style.padding = '3px' | ||
tds.style.border = '1px solid black' | ||
} | ||
var chBoxRow = document.createElement("input"); | ||
chBoxRow.className = params.tableId + "RowCheckbox"; | ||
chBoxRow.type = "checkbox"; | ||
chBoxRow.checked = Boolean(params.checked); | ||
tds.appendChild(chBoxRow); | ||
dRow.appendChild(tds); | ||
var chBoxRow = document.createElement('input') | ||
chBoxRow.className = params.tableId + 'RowCheckbox' | ||
chBoxRow.type = 'checkbox' | ||
chBoxRow.checked = Boolean(params.checked) | ||
tds.appendChild(chBoxRow) | ||
dRow.appendChild(tds) | ||
} | ||
for (var i = 0; i < columns.length; i++) { | ||
var td = document.createElement('td'); | ||
var td = document.createElement('td') | ||
if (!params.mdl) { | ||
td.style.padding = "3px"; | ||
td.style.border = "1px solid black"; | ||
td.style.padding = '3px' | ||
td.style.border = '1px solid black' | ||
} | ||
cell = obj[columns[i]]; | ||
cell = (((cell !== undefined) && (cell !== null)) ? cell : ""); | ||
if (cell.constructor === Array) cell = cell.join(", "); | ||
else if (cell.constructor === Object) cell = JSON.stringify(cell); | ||
else if (typeof cell == "string") { | ||
cell = cell.replace(params.quotes, "'"); | ||
if (reDateTimeJS.test(cell)) cell = new Date(cell).toLocaleDateString(); | ||
cell = obj[columns[i]] | ||
cell = (((cell !== undefined) && (cell !== null)) ? cell : '') | ||
if (cell.constructor === Array) cell = cell.join(', ') | ||
else if (cell.constructor === Object) cell = JSON.stringify(cell) | ||
else if (typeof cell == 'string') { | ||
cell = cell.replace(params.quotes, "'") | ||
if (reDateTimeJS.test(cell)) cell = new Date(cell).toLocaleDateString() | ||
} | ||
if (/<a.+<\/a>/.test(cell)) { | ||
td.appendChild(new DOMParser().parseFromString(cell, "text/html").querySelector("a")); | ||
td.appendChild(new DOMParser().parseFromString(cell, 'text/html').querySelector('a')) | ||
} else if (/<img.+?>/.test(cell)) { | ||
td.appendChild(new DOMParser().parseFromString(cell, "text/html").querySelector("img")); | ||
td.appendChild(new DOMParser().parseFromString(cell, 'text/html').querySelector('img')) | ||
} else { | ||
td.appendChild(document.createTextNode(cell)); | ||
td.appendChild(document.createTextNode(cell)) | ||
} | ||
if (!params.mdl) { | ||
td.style.padding = "3px"; | ||
td.style.border = "1px solid black"; | ||
td.style.padding = '3px' | ||
td.style.border = '1px solid black' | ||
} else { | ||
if (!numCol[columns[l]]) { //select first col with non-num | ||
td.className = "mdl-data-table__cell--non-numeric"; | ||
if (!numCol[columns[l]]) { // select first col with non-num | ||
td.className = 'mdl-data-table__cell--non-numeric' | ||
} | ||
} | ||
dRow.appendChild(td); | ||
dRow.appendChild(td) | ||
} | ||
}; | ||
} | ||
UI.getTableSel = function(tableId) { | ||
var checkboxes = document.querySelectorAll("." + tableId + "RowCheckbox"); | ||
var checkedArr = []; | ||
UI.getTableSel = function (tableId) { | ||
var checkboxes = document.querySelectorAll('.' + tableId + 'RowCheckbox') | ||
var checkedArr = [] | ||
for (var i = 0; i < checkboxes.length; i++) { | ||
if (checkboxes[i].checked) checkedArr.push(i); | ||
if (checkboxes[i].checked) checkedArr.push(i) | ||
} | ||
return checkedArr; | ||
}; | ||
return checkedArr | ||
} | ||
UI.div = function (params) { | ||
params = params || {} | ||
UI.div = function(params) { | ||
params = params || {}; | ||
if (typeof params == "string") { | ||
if (typeof params == 'string') { | ||
params = { | ||
id: params | ||
}; | ||
} | ||
} | ||
if (typeof params.parent == "string") { | ||
params.parent = document.querySelector(params.parent); | ||
if (typeof params.parent == 'string') { | ||
params.parent = document.querySelector(params.parent) | ||
} else params.parent = params.parent || | ||
document.querySelector("#ui") || document.body; | ||
document.querySelector('#ui') || document.body | ||
var exNode = document.getElementById(params.id); | ||
if (exNode) params.parent.removeChild(exNode); | ||
var exNode = document.getElementById(params.id) | ||
if (exNode) params.parent.removeChild(exNode) | ||
var div = document.createElement("div"); | ||
var div = document.createElement('div') | ||
div.id = params.id || "div"; | ||
div.className = params.className; | ||
div.id = params.id || 'div' | ||
div.className = params.className | ||
if (params.attributes) { | ||
for (var attribute in params.attributes) { | ||
var val = params.attributes[attribute]; | ||
table.setAttribute(style, val); | ||
var val = params.attributes[attribute] | ||
table.setAttribute(style, val) | ||
} | ||
} | ||
params.parent.appendChild(div); | ||
}; | ||
params.parent.appendChild(div) | ||
} | ||
UI.appendModal = function (params) { | ||
// requires bootstrap | ||
UI.appendModal = function(params) { | ||
//requires bootstrap | ||
params = params || {} | ||
params.id = params.id || 'modal' | ||
params.title = params.title || 'Modal title' | ||
params = params || {}; | ||
params.id = params.id || "modal"; | ||
params.title = params.title || "Modal title"; | ||
var fadeDiv = document.createElement('div') | ||
fadeDiv.id = params.id | ||
fadeDiv.setAttribute('class', 'modal fade') | ||
fadeDiv.setAttribute('role', 'dialog') | ||
document.body.appendChild(fadeDiv) | ||
var fadeDiv = document.createElement("div"); | ||
fadeDiv.id = params.id; | ||
fadeDiv.setAttribute("class", "modal fade"); | ||
fadeDiv.setAttribute("role", "dialog"); | ||
document.body.appendChild(fadeDiv); | ||
var dialogDiv = document.createElement('div') | ||
dialogDiv.setAttribute('class', 'modal-dialog') | ||
fadeDiv.appendChild(dialogDiv) | ||
var dialogDiv = document.createElement("div"); | ||
dialogDiv.setAttribute("class", "modal-dialog"); | ||
fadeDiv.appendChild(dialogDiv); | ||
var contentDiv = document.createElement('div') | ||
contentDiv.setAttribute('class', 'modal-content') | ||
dialogDiv.appendChild(contentDiv) | ||
var contentDiv = document.createElement("div"); | ||
contentDiv.setAttribute("class", "modal-content"); | ||
dialogDiv.appendChild(contentDiv); | ||
var headerDiv = document.createElement('div') | ||
headerDiv.setAttribute('class', 'modal-header') | ||
headerDiv.style.padding = '10px' | ||
contentDiv.appendChild(headerDiv) | ||
var headerDiv = document.createElement("div"); | ||
headerDiv.setAttribute("class", "modal-header"); | ||
headerDiv.style.padding = "10px"; | ||
contentDiv.appendChild(headerDiv); | ||
var closeButton = document.createElement('button') | ||
closeButton.setAttribute('class', 'close') | ||
closeButton.setAttribute('data-dismiss', 'modal') | ||
closeButton.innerHTML = '×' | ||
headerDiv.appendChild(closeButton) | ||
var closeButton = document.createElement("button"); | ||
closeButton.setAttribute("class", "close"); | ||
closeButton.setAttribute("data-dismiss", "modal"); | ||
closeButton.innerHTML = "×"; | ||
headerDiv.appendChild(closeButton); | ||
var h4 = document.createElement('h4') | ||
h4.setAttribute('class', 'modal-title') | ||
h4.innerHTML = params.title | ||
headerDiv.appendChild(h4) | ||
var h4 = document.createElement("h4"); | ||
h4.setAttribute("class", "modal-title"); | ||
h4.innerHTML = params.title; | ||
headerDiv.appendChild(h4); | ||
var bodyDiv = document.createElement('div') | ||
bodyDiv.id = params.id + '-body' | ||
contentDiv.appendChild(bodyDiv) | ||
var bodyDiv = document.createElement("div"); | ||
bodyDiv.id = params.id + "-body"; | ||
contentDiv.appendChild(bodyDiv); | ||
var footerDiv = document.createElement('div') | ||
footerDiv.id = params.id + '-footer' | ||
contentDiv.appendChild(footerDiv) | ||
} | ||
var footerDiv = document.createElement("div"); | ||
footerDiv.id = params.id + "-footer"; | ||
contentDiv.appendChild(footerDiv); | ||
}; | ||
UI.slug = function (str) { | ||
if (!str) return | ||
if(typeof str === 'number') return str | ||
UI.slug = function(str) { | ||
var letterMap = { | ||
@@ -1177,20 +1134,19 @@ '/': '_', | ||
'ґ': 'g', | ||
'+': '-plus', | ||
}; | ||
'+': '-plus' | ||
} | ||
var reOtherSymbols = /[^a-z0-9\-_]/gi; | ||
var reOtherSymbols = /[^a-z0-9\-_]/gi | ||
var replLetters = str.split('').map(function(char) { | ||
char = char.toLowerCase(); | ||
return (letterMap[char] !== undefined) ? letterMap[char] : char; | ||
}).join(""); | ||
var replLetters = str.split('').map(function (char) { | ||
char = char.toLowerCase() | ||
return (letterMap[char] !== undefined) ? letterMap[char] : char | ||
}).join('') | ||
var replSymb = replLetters.replace(reOtherSymbols, '-'); | ||
var replSymb = replLetters.replace(reOtherSymbols, '-') | ||
var replUnnecDelims = removeUnnecessaryDelims(replSymb); | ||
var replUnnecDelims = removeUnnecessaryDelims(replSymb) | ||
return replUnnecDelims; | ||
return replUnnecDelims | ||
function removeUnnecessaryDelims(str) { | ||
function removeUnnecessaryDelims (str) { | ||
return str | ||
@@ -1200,6 +1156,5 @@ .replace(/\-{2,}/g, '-') | ||
.replace(/[\-\_]+$/g, '') | ||
.replace(/^[\-\_]+/g, ''); | ||
.replace(/^[\-\_]+/g, '') | ||
} | ||
}; | ||
})(); | ||
} | ||
})() |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
933
34788
1