Pictures!
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

lib.js 2.7KB

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