Pictures!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. (function() {
  2. window.apiLogin = apiLogin;
  3. window.api = api;
  4. window.elem = elem;
  5. window.uploadEl = uploadEl;
  6. window.async = async;
  7. window.debounce = debounce;
  8. window.error = error;
  9. window.$$ = $$;
  10. function apiLogin(pass, cb) {
  11. var extraHeads = [ [ "Session-Pass", pass ] ];
  12. api("login", {}, (err, token) => {
  13. if (token) {
  14. document.cookie = "token = " + token;
  15. cb(true);
  16. } else {
  17. cb(false);
  18. }
  19. }, extraHeads);
  20. }
  21. function argstr(args) {
  22. return "?" + Object.keys(args).map(function(key) {
  23. return encodeURIComponent(key)+"="+encodeURIComponent(args[key]);
  24. }).join("&");
  25. }
  26. function api(method, args, cb, extraHeads) {
  27. var heads = new Headers();
  28. if (extraHeads)
  29. extraHeads.forEach(h => heads.append(h[0], h[1]));
  30. var opts = {
  31. method: "POST",
  32. headers: heads,
  33. credentials: "same-origin"
  34. };
  35. fetch("/admin/api/"+method+argstr(args), opts)
  36. .then(response => response.json())
  37. .then(res => {
  38. if (res.err && res.err === "EINVALTOKEN")
  39. setView("login");
  40. else
  41. cb(res.err, res.obj)
  42. });
  43. }
  44. function elem(tag, props, children) {
  45. var e;
  46. if (tag instanceof HTMLElement)
  47. e = tag;
  48. else
  49. e = document.createElement(tag);
  50. if (props) {
  51. for (var i in props) {
  52. e[i] = props[i];
  53. }
  54. }
  55. if (children) {
  56. for (var i in children) {
  57. e.appendChild(children[i]);
  58. }
  59. }
  60. e.appendTo = function(p) {
  61. p.appendChild(e);
  62. return e;
  63. }
  64. e.on = function(name, fn) {
  65. e.addEventListener(name, function(evt) {
  66. fn.call(e, evt);
  67. }, false);
  68. return e;
  69. }
  70. e.addClass = function(name) {
  71. if (e.className.indexOf(name) !== -1)
  72. return e;
  73. e.className += " "+name;
  74. return e;
  75. }
  76. e.removeClass = function(name) {
  77. e.className = e.className
  78. .replace(name, "")
  79. .trim()
  80. .replace(/ +/, "");
  81. return e;
  82. }
  83. e.clear = function() {
  84. var fc;
  85. while (fc = e.firstChild)
  86. e.removeChild(fc);
  87. return e;
  88. }
  89. return e;
  90. }
  91. var uploadElId = 0;
  92. function uploadEl(args, cb) {
  93. var id = uploadElId++;
  94. var frame;
  95. var form;
  96. var fileEl;
  97. return form = elem("form", {
  98. className: "uploader",
  99. action: "/admin/api/slide_file_upload"+argstr(args),
  100. method: "post",
  101. enctype: "multipart/form-data",
  102. target: "upload-form-"+id
  103. }, [
  104. frame = elem("iframe", {
  105. style: "display: none",
  106. name: "upload-form-"+id
  107. }).on("load", cb),
  108. fileEl = elem("input", {
  109. type: "file",
  110. style: "display: none",
  111. name: "file"
  112. }).on("change", () => {
  113. form.submit();
  114. }),
  115. elem("button", {
  116. innerHTML: "Upload",
  117. type: "button"
  118. }).on("click", () => {
  119. fileEl.click();
  120. })
  121. ]);
  122. }
  123. function async(n, cb) {
  124. var args = [];
  125. return function(arg) {
  126. args.push(arg);
  127. n -= 1;
  128. if (n === 0)
  129. cb(args);
  130. }
  131. }
  132. function debounce(fn, ms) {
  133. if (ms === undefined)
  134. ms = 300;
  135. var timeout;
  136. return function() {
  137. if (timeout)
  138. clearTimeout(timeout);
  139. timeout = setTimeout(function() {
  140. fn();
  141. timeout = null;
  142. }, ms);
  143. }
  144. }
  145. function error(msg) {
  146. alert(msg);
  147. }
  148. function $$() {
  149. return elem(document.querySelector.apply(document, arguments));
  150. }
  151. })();