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 2.3KB

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