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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. (function() {
  2. var months = [
  3. "January",
  4. "February",
  5. "March",
  6. "April",
  7. "May",
  8. "June",
  9. "July",
  10. "August",
  11. "September",
  12. "October",
  13. "November",
  14. "December"
  15. ];
  16. window.util = {};
  17. util.notify = function notify(title, body) {
  18. var elem = $("#notify-box");
  19. elem.children(".title").html(title);
  20. elem.children(".body").html(body || "");
  21. elem.addClass("active");
  22. notify.timeout = setTimeout(function() {
  23. elem.removeClass("active");
  24. }, 5000);
  25. };
  26. $(document).ready(function() {
  27. $("#notify-box").on("mouseenter", function() {
  28. clearTimeout(util.notify.timeout);
  29. });
  30. });
  31. util.error = function(body) {
  32. util.notify("Error: "+body);
  33. };
  34. util.htmlEntities = function(str) {
  35. return str.replace(/&/g, "&")
  36. .replace(/</g, "&lt;")
  37. .replace(/>/g, "&gt;")
  38. .replace(/"/g, "&quot;");
  39. };
  40. util.api = function(name, data, cb, getXhr) {
  41. var fd = new FormData();
  42. for (var i in data) {
  43. fd.append(i, data[i]);
  44. }
  45. return $.ajax({
  46. method: "POST",
  47. url: "/api/"+name,
  48. data: fd,
  49. processData: false,
  50. contentType: false,
  51. xhr: function() {
  52. var xhr = new XMLHttpRequest();
  53. if (getXhr)
  54. getXhr(xhr);
  55. return xhr;
  56. },
  57. error: function(xhr, status, err) {
  58. cb(err);
  59. }
  60. }).done(function(res) {
  61. console.log("response from "+name+":");
  62. console.log(res);
  63. if (res.success)
  64. cb(null, res);
  65. else
  66. cb(res.error);
  67. });
  68. };
  69. util.async = function(n, cb) {
  70. if (typeof n !== "number")
  71. throw new Error("Expected number, got "+typeof n);
  72. if (n < 1)
  73. return cb();
  74. var res = {};
  75. return function(key, val, err) {
  76. if (key)
  77. res[key] = val;
  78. if (n === 1)
  79. cb(res);
  80. else
  81. n -= 1;
  82. };
  83. };
  84. util.pad = function(str, length, padChar) {
  85. var missing = (length - str.length) + 1;
  86. if (missing <= 0)
  87. return str;
  88. return new Array(missing).join(padChar) + str;
  89. };
  90. util.dateToString = function(date) {
  91. var day = util.pad(date.getDate().toString(), 2, "0");
  92. var month = months[date.getMonth()];
  93. return day+". of "+month+" "+
  94. date.getFullYear()+", "+
  95. util.pad(date.getHours().toString(), 2, "0")+":"+
  96. util.pad(date.getMinutes().toString(), 2, "0");
  97. };
  98. util.prevent = function(evt) {
  99. evt.preventDefault();
  100. evt.stopPropagation();
  101. };
  102. util.redirect = function(url, timeout) {
  103. setTimeout(function() {
  104. location.href = url;
  105. }, timeout || 1000);
  106. };
  107. window.display = {};
  108. display.loggedIn = function() {
  109. util.api("template?navbar-loggedin", {}, function(err, res) {
  110. if (err)
  111. return util.error(err);
  112. $("#navbar-profile-container").html(res.html);
  113. util.notify("Logged In", "You are now logged in.");
  114. });
  115. };
  116. display.logIn = function() {
  117. util.api("template?navbar-login", {}, function(err, res) {
  118. if (err)
  119. return util.error(err);
  120. $("#navbar-profile-container").html(res.html);
  121. util.notify("Logged Out", "You are now logged out.");
  122. });
  123. };
  124. $(document).ready(function() {
  125. $("#login-form").on("submit", function(evt) {
  126. util.prevent(evt);
  127. var username = $("#login-username").val();
  128. var password = $("#login-password").val();
  129. util.api("account_login", {
  130. username: username,
  131. password: password
  132. }, function(err, res) {
  133. if (err)
  134. util.error(err);
  135. else
  136. display.loggedIn();
  137. });
  138. });
  139. });
  140. })();