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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, "&lt;")
  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. }).done(function(res) {
  58. console.log("response from "+name+":");
  59. console.log(res);
  60. if (res.success)
  61. cb(null, res);
  62. else
  63. cb(res.error);
  64. });
  65. }
  66. util.async = function(n, cb) {
  67. if (typeof n !== "number")
  68. throw new Error("Expected number, got "+typeof n);
  69. if (n < 1)
  70. return cb();
  71. var res = {};
  72. return function(key, val, err) {
  73. if (key)
  74. res[key] = val;
  75. if (n === 1)
  76. cb(res);
  77. else
  78. n -= 1;
  79. }
  80. }
  81. util.pad = function(str, length, padChar) {
  82. var missing = (length - str.length) + 1;
  83. if (missing <= 0)
  84. return str;
  85. return new Array(missing).join(padChar) + str;
  86. }
  87. util.dateToString = function(date) {
  88. var day = util.pad(date.getDate().toString(), 2, "0");
  89. var month = months[date.getMonth()];
  90. return day+". of "+month+" "+
  91. date.getFullYear()+", "+
  92. util.pad(date.getHours().toString(), 2, "0")+":"+
  93. util.pad(date.getMinutes().toString(), 2, "0");
  94. }
  95. window.display = {};
  96. window.display.loggedIn = function() {
  97. util.api("template?navbar-loggedin", {}, function(err, res) {
  98. if (err)
  99. return util.error(err);
  100. $("#navbar-profile-container").html(res.html);
  101. util.notify("Logged In", "You are now logged in.");
  102. });
  103. }
  104. $(document).ready(function() {
  105. $("#login-form").on("submit", function(evt) {
  106. evt.stopPropagation();
  107. evt.preventDefault();
  108. var username = $("#login-username").val();
  109. var password = $("#login-password").val();
  110. util.api("account_login", {
  111. username: username,
  112. password: password
  113. }, function(err, res) {
  114. if (err)
  115. util.error(err);
  116. else
  117. display.loggedIn();
  118. });
  119. });
  120. });
  121. })();