2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/client/ui/index.js

103 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
2018-01-12 12:25:07 +00:00
const Dropdown = require('./dropdown');
module.exports = {
2018-03-29 12:05:02 +00:00
create(tag, obj) {
if(!obj) {
2018-03-29 07:49:08 +00:00
let div = document.createElement('div');
div.innerHTML = tag.trim();
return div.firstChild;
}
2018-03-28 11:29:28 +00:00
let element = document.createElement(tag);
let $ = (expr, con) => {
return typeof expr === "string"
? (con || document).querySelector(expr)
: expr || null;
}
2018-03-29 12:05:02 +00:00
for (var i in obj) {
let val = obj[i];
2018-03-28 11:29:28 +00:00
if (i === "inside") {
$(val).appendChild(element);
}
else if (i === "around") {
let ref = $(val);
ref.parentNode.insertBefore(element, ref);
element.appendChild(ref);
} else if (i === "styles") {
if(typeof val === "object") {
Object.keys(val).map(prop => {
element.style[prop] = val[prop];
});
}
} else if (i in element ) {
element[i] = val;
}
else {
element.setAttribute(i, val);
}
}
return element;
},
2018-03-08 13:31:22 +00:00
add(tag, className, parent, textContent) {
2018-01-12 12:25:07 +00:00
let element = document.createElement(tag);
if (className) {
for (let c of className.split(' ')) {
2018-02-14 12:50:56 +00:00
this.addClass(element, c);
2018-01-12 12:25:07 +00:00
}
}
if (parent) {
parent.appendChild(element);
}
2018-03-08 13:31:22 +00:00
if (textContent) {
element.textContent = textContent;
}
2018-01-12 12:25:07 +00:00
return element;
},
remove(element) {
element.parentNode.removeChild(element);
},
2018-03-08 13:31:22 +00:00
empty(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
},
2018-02-14 12:50:56 +00:00
addClass(element, className) {
2018-01-12 12:25:07 +00:00
if (element.classList) {
element.classList.add(className);
} else {
element.className += " " + className;
}
},
2018-03-28 11:29:28 +00:00
removeClass(element, className) {
2018-01-12 12:25:07 +00:00
if (element.classList) {
element.classList.remove(className);
} else {
element.className = element.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
},
toggle(element, default_display = '') {
element.style.display = element.style.display === 'none' ? default_display : 'none';
},
make_dropdown(label, parent, btn_class = 'btn-secondary') {
return new Dropdown({parent: parent, label:label, btn_class:btn_class});
},
2018-01-12 12:25:07 +00:00
showAlert({message, color='yellow', timeout=4}) {
let alert = this.add('div', 'alert alert-warning bottom-right-float', document.body);
alert.innerHTML = `<span class='indicator ${color}'>${message}</span>`;
frappe.sleep(timeout).then(() => alert.remove());
return alert;
}
2018-01-12 12:25:07 +00:00
}