|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- (function() {
-
- window.apiLogin = apiLogin;
- window.api = api;
- window.elem = elem;
- window.uploadEl = uploadEl;
- window.async = async;
- window.debounce = debounce;
- window.error = error;
- window.$$ = $$;
-
- function apiLogin(pass, cb) {
- var extraHeads = [ [ "Session-Pass", pass ] ];
-
- api("login", {}, (err, token) => {
- if (token) {
- document.cookie = "token = " + token;
- cb(true);
- } else {
- cb(false);
- }
- }, extraHeads);
- }
-
- function argstr(args) {
- return "?" + Object.keys(args).map(function(key) {
- return encodeURIComponent(key)+"="+encodeURIComponent(args[key]);
- }).join("&");
- }
-
- function api(method, args, cb, extraHeads) {
- var heads = new Headers();
- if (extraHeads)
- extraHeads.forEach(h => heads.append(h[0], h[1]));
-
- var opts = {
- method: "POST",
- headers: heads,
- credentials: "same-origin"
- };
- fetch("/admin/api/"+method+argstr(args), opts)
- .then(response => response.json())
- .then(res => {
- if (res.err && res.err === "EINVALTOKEN")
- setView("login");
- else
- 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(name, fn) {
- e.addEventListener(name, function(evt) {
- fn(evt, e);
- }, false);
- 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;
- }
-
- var uploadElId = 0;
- function uploadEl(args, cb) {
- var id = uploadElId++;
- var frame;
- var form;
- var fileEl;
- return form = elem("form", {
- className: "uploader",
- action: "/admin/api/slide_file_upload"+argstr(args),
- method: "post",
- enctype: "multipart/form-data",
- target: "upload-form-"+id
- }, [
- frame = elem("iframe", {
- style: "display: none",
- name: "upload-form-"+id
- }).on("load", cb),
-
- fileEl = elem("input", {
- type: "file",
- style: "display: none",
- name: "file"
- }).on("change", () => {
- form.submit();
- }),
-
- elem("button", {
- innerHTML: "Upload",
- type: "button"
- }).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 = 300;
-
- var timeout;
- return function() {
-
- if (timeout)
- clearTimeout(timeout);
-
- timeout = setTimeout(function() {
- fn();
- timeout = null;
- }, ms);
- }
- }
-
- function error(msg) {
- alert(msg);
- }
-
- function $$() {
- return elem(document.querySelector.apply(document, arguments));
- }
- })();
|