Simple image host.
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.

global.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. (function() {
  2. window.util = {};
  3. util.notify = function notify(title, body) {
  4. var elem = $("#notify-box");
  5. elem.children(".title").html(title);
  6. elem.children(".body").html(body || "");
  7. elem.addClass("active");
  8. notify.timeout = setTimeout(function() {
  9. elem.removeClass("active");
  10. }, 5000);
  11. }
  12. $(document).ready(function() {
  13. $("#notify-box").on("mouseenter", function() {
  14. clearTimeout(util.notify.timeout);
  15. });
  16. });
  17. util.error = function(body) {
  18. util.notify("Error: "+body);
  19. }
  20. util.htmlEntities = function(str) {
  21. return str.replace(/&/g, "&")
  22. .replace(/</g, "&lt;")
  23. .replace(/>/g, "&lt;")
  24. .replace(/"/g, "&quot");
  25. }
  26. util.api = function(name, data, cb, getXhr) {
  27. var fd = new FormData();
  28. for (var i in data) {
  29. fd.append(i, data[i]);
  30. }
  31. return $.ajax({
  32. method: "POST",
  33. url: "/api/"+name,
  34. data: fd,
  35. processData: false,
  36. contentType: false,
  37. xhr: function() {
  38. var xhr = new XMLHttpRequest();
  39. if (getXhr)
  40. getXhr(xhr);
  41. return xhr;
  42. }
  43. }).done(function(res) {
  44. console.log("response from "+name+":");
  45. console.log(res);
  46. if (res.success)
  47. cb(null, res);
  48. else
  49. cb(res.error);
  50. });
  51. }
  52. util.async = function(n, cb) {
  53. if (typeof n !== "number")
  54. throw new Error("Expected number, got "+typeof n);
  55. if (n < 1)
  56. return cb();
  57. var res = {};
  58. return function(key, val) {
  59. if (key !== undefined)
  60. res[key] = val;
  61. if (n === 1)
  62. cb(res);
  63. else
  64. n -= 1;
  65. }
  66. }
  67. window.display = {};
  68. window.display.loggedIn = function() {
  69. util.api("template?navbar-loggedin", {}, function(err, res) {
  70. if (err)
  71. return util.error(err);
  72. $("#navbar-profile-container").html(res.html);
  73. });
  74. }
  75. $(document).ready(function() {
  76. $("#login-form").on("submit", function(evt) {
  77. evt.stopPropagation();
  78. evt.preventDefault();
  79. var username = $("#login-username").val();
  80. var password = $("#login-password").val();
  81. util.api("account_login", {
  82. username: username,
  83. password: password
  84. }, function(err, res) {
  85. if (err)
  86. util.error(err);
  87. else
  88. display.loggedIn();
  89. });
  90. });
  91. });
  92. })();