Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

191 Zeilen
4.7 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. if(!window.wn) wn = {};
  4. wn.call = function(opts) {
  5. if(opts.btn) {
  6. $(opts.btn).prop("disabled", true);
  7. }
  8. if(opts.msg) {
  9. $(opts.msg).toggle(false);
  10. }
  11. if(!opts.args) opts.args = {};
  12. // get or post?
  13. if(!opts.args._type) {
  14. opts.args._type = opts.type || "GET";
  15. }
  16. // method
  17. if(opts.method) {
  18. opts.args.cmd = opts.method;
  19. }
  20. // stringify
  21. $.each(opts.args, function(key, val) {
  22. if(typeof val != "string") {
  23. opts.args[key] = JSON.stringify(val);
  24. }
  25. });
  26. $.ajax({
  27. type: "POST",
  28. url: "server.py",
  29. data: opts.args,
  30. dataType: "json",
  31. success: function(data) {
  32. if(opts.btn) {
  33. $(opts.btn).prop("disabled", false);
  34. }
  35. if(data.exc) {
  36. if(opts.btn) {
  37. $(opts.btn).addClass("btn-danger");
  38. setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
  39. }
  40. try {
  41. var err = JSON.parse(data.exc);
  42. if($.isArray(err)) {
  43. err = err.join("\n");
  44. }
  45. console.error ? console.error(err) : console.log(err);
  46. } catch(e) {
  47. console.log(data.exc);
  48. }
  49. } else{
  50. if(opts.btn) {
  51. $(opts.btn).addClass("btn-success");
  52. setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
  53. }
  54. }
  55. if(opts.msg && data.message) {
  56. $(opts.msg).html(data.message).toggle(true);
  57. }
  58. if(opts.callback)
  59. opts.callback(data);
  60. },
  61. error: function(response) {
  62. console.error ? console.error(response) : console.log(response);
  63. }
  64. });
  65. return false;
  66. }
  67. // Utility functions
  68. function valid_email(id) {
  69. if(id.toLowerCase().search("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")==-1)
  70. return 0; else return 1; }
  71. var validate_email = valid_email;
  72. function get_url_arg(name) {
  73. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  74. var regexS = "[\\?&]"+name+"=([^&#]*)";
  75. var regex = new RegExp( regexS );
  76. var results = regex.exec( window.location.href );
  77. if(results == null)
  78. return "";
  79. else
  80. return decodeURIComponent(results[1]);
  81. }
  82. function make_query_string(obj) {
  83. var query_params = [];
  84. $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
  85. return "?" + query_params.join("&");
  86. }
  87. function repl(s, dict) {
  88. if(s==null)return '';
  89. for(key in dict) {
  90. s = s.split("%("+key+")s").join(dict[key]);
  91. }
  92. return s;
  93. }
  94. function replace_all(s, t1, t2) {
  95. return s.split(t1).join(t2);
  96. }
  97. function getCookie(name) {
  98. return getCookies()[name];
  99. }
  100. function getCookies() {
  101. var c = document.cookie, v = 0, cookies = {};
  102. if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
  103. c = RegExp.$1;
  104. v = 1;
  105. }
  106. if (v === 0) {
  107. c.split(/[,;]/).map(function(cookie) {
  108. var parts = cookie.split(/=/, 2),
  109. name = decodeURIComponent(parts[0].trimLeft()),
  110. value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
  111. if(value && value.charAt(0)==='"') {
  112. value = value.substr(1, value.length-2);
  113. }
  114. cookies[name] = value;
  115. });
  116. } else {
  117. c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) {
  118. var name = $0,
  119. value = $1.charAt(0) === '"'
  120. ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
  121. : $1;
  122. cookies[name] = value;
  123. });
  124. }
  125. return cookies;
  126. }
  127. if (typeof String.prototype.trimLeft !== "function") {
  128. String.prototype.trimLeft = function() {
  129. return this.replace(/^\s+/, "");
  130. };
  131. }
  132. if (typeof String.prototype.trimRight !== "function") {
  133. String.prototype.trimRight = function() {
  134. return this.replace(/\s+$/, "");
  135. };
  136. }
  137. if (typeof Array.prototype.map !== "function") {
  138. Array.prototype.map = function(callback, thisArg) {
  139. for (var i=0, n=this.length, a=[]; i<n; i++) {
  140. if (i in this) a[i] = callback.call(thisArg, this[i]);
  141. }
  142. return a;
  143. };
  144. }
  145. function remove_script_and_style(txt) {
  146. return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
  147. $("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html();
  148. }
  149. function is_html(txt) {
  150. if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
  151. && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
  152. return false;
  153. }
  154. return true;
  155. }
  156. function ask_to_login() {
  157. if(!full_name) {
  158. if(localStorage) {
  159. localStorage.setItem("last_visited", window.location.href.split("/").slice(-1)[0]);
  160. }
  161. window.location.href = "login";
  162. }
  163. }
  164. // check if logged in?
  165. $(document).ready(function() {
  166. window.full_name = getCookie("full_name");
  167. $("#website-login").toggleClass("hide", full_name ? true : false);
  168. $("#website-post-login").toggleClass("hide", full_name ? false : true);
  169. });