| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- function api(method, args, cb) {
- var argstr = "?" + Object.keys(args).map(function(key) {
- return encodeURIComponent(key)+"="+encodeURIComponent(args[key]);
- }).join("&");
-
- fetch("/admin/api/"+method+argstr, { method: "POST" })
- .then(response => response.json())
- .then(res => cb(res.err, res.obj));
- }
-
- function elem(tag, props, children) {
- var e;
- if (tag instanceof HTMLElement)
- e = tag;
- else
- e = document.createElement(tag);
-
- if (props) {
- for (var i in props) {
- e[i] = props[i];
- }
- }
-
- if (children) {
- for (var i in children) {
- e.appendChild(children[i]);
- }
- }
-
- e.appendTo = function(p) {
- p.appendChild(e);
- return e;
- }
-
- e.on = function() {
- e.addEventListener.apply(e, arguments);
- return e;
- }
-
- e.addClass = function(name) {
- if (e.className.indexOf(name) !== -1)
- return e;
-
- e.className += " "+name;
- return e;
- }
-
- e.removeClass = function(name) {
- e.className = e.className
- .replace(name, "")
- .trim()
- .replace(/ +/, "");
- return e;
- }
-
- e.clear = function() {
- var fc;
- while (fc = e.firstChild)
- e.removeChild(fc);
- return e;
- }
-
- return e;
- }
-
- function uploadEl() {
- var fileEl;
- elem("div", { className: "uploader" }, [
- fileEl = elem("input", {
- type: "file",
- style: "display: none"
- });
-
- elem("button", {
- innerHTML: "Upload"
- }).on("click", () => {
- fileEl.click();
- });
- ]);
- }
-
- function async(n, cb) {
- var args = [];
- return function(arg) {
- args.push(arg);
-
- n -= 1;
- if (n === 0)
- cb(args);
- }
- }
-
- function debounce(fn, ms) {
- if (ms === undefined)
- ms = 100;
-
- var timeout;
- return function() {
-
- if (timeout)
- clearTimeout(timeout);
-
- timeout = setTimeout(function() {
- fn();
- timeout = null;
- }, ms);
- }
- }
-
- function error(msg) {
- alert(msg);
- }
-
- var $$ = function() {
- return elem(document.querySelector.apply(document, arguments));
- }
|