Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

250 rader
6.1 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. if(!window.wn) wn = {};
  4. wn = {
  5. call: function(opts) {
  6. wn.prepare_call(opts);
  7. $.ajax({
  8. type: "POST",
  9. url: "server.py",
  10. data: opts.args,
  11. dataType: "json",
  12. success: function(data) {
  13. wn.process_response(opts, data);
  14. },
  15. error: function(response) {
  16. NProgress.done();
  17. console.error ? console.error(response) : console.log(response);
  18. }
  19. });
  20. return false;
  21. },
  22. prepare_call: function(opts) {
  23. if(opts.btn) {
  24. $(opts.btn).prop("disabled", true);
  25. }
  26. if(opts.msg) {
  27. $(opts.msg).toggle(false);
  28. }
  29. if(!opts.args) opts.args = {};
  30. // get or post?
  31. if(!opts.args._type) {
  32. opts.args._type = opts.type || "GET";
  33. }
  34. // method
  35. if(opts.method) {
  36. opts.args.cmd = opts.method;
  37. }
  38. // stringify
  39. $.each(opts.args, function(key, val) {
  40. if(typeof val != "string") {
  41. opts.args[key] = JSON.stringify(val);
  42. }
  43. });
  44. NProgress.start();
  45. },
  46. process_response: function(opts, data) {
  47. NProgress.done();
  48. if(opts.btn) {
  49. $(opts.btn).prop("disabled", false);
  50. }
  51. if(data.exc) {
  52. if(opts.btn) {
  53. $(opts.btn).addClass("btn-danger");
  54. setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
  55. }
  56. try {
  57. var err = JSON.parse(data.exc);
  58. if($.isArray(err)) {
  59. err = err.join("\n");
  60. }
  61. console.error ? console.error(err) : console.log(err);
  62. } catch(e) {
  63. console.log(data.exc);
  64. }
  65. } else{
  66. if(opts.btn) {
  67. $(opts.btn).addClass("btn-success");
  68. setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
  69. }
  70. }
  71. if(opts.msg && data.message) {
  72. $(opts.msg).html(data.message).toggle(true);
  73. }
  74. if(opts.callback)
  75. opts.callback(data);
  76. },
  77. show_message: function(text, icon) {
  78. if(!icon) icon="icon-refresh icon-spin";
  79. wn.hide_message();
  80. $('<div class="message-overlay"></div>')
  81. .html('<div class="content"><i class="'+icon+' text-muted"></i><br>'
  82. +text+'</div>').appendTo(document.body);
  83. },
  84. hide_message: function(text) {
  85. $('.message-overlay').remove();
  86. },
  87. get_sid: function() {
  88. var sid = getCookie("sid");
  89. return sid && sid!=="Guest";
  90. },
  91. get_modal: function(title, body_html) {
  92. var modal = $('<div class="modal" style="overflow: auto;">\
  93. <div class="modal-dialog">\
  94. <div class="modal-content">\
  95. <div class="modal-header">\
  96. <a type="button" class="close" \
  97. data-dismiss="modal" aria-hidden="true">&times;</a>\
  98. <h4 class="modal-title">'+title+'</h4>\
  99. </div>\
  100. <div class="modal-body ui-front">'+body_html+'\
  101. </div>\
  102. </div>\
  103. </div>\
  104. </div>').appendTo(document.body);
  105. return modal;
  106. },
  107. msgprint: function(html, title) {
  108. return wn.get_modal(title || "Message", html).modal("show");
  109. }
  110. }
  111. // Utility functions
  112. function valid_email(id) {
  113. 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)
  114. return 0; else return 1; }
  115. var validate_email = valid_email;
  116. function get_url_arg(name) {
  117. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  118. var regexS = "[\\?&]"+name+"=([^&#]*)";
  119. var regex = new RegExp( regexS );
  120. var results = regex.exec( window.location.href );
  121. if(results == null)
  122. return "";
  123. else
  124. return decodeURIComponent(results[1]);
  125. }
  126. function make_query_string(obj) {
  127. var query_params = [];
  128. $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
  129. return "?" + query_params.join("&");
  130. }
  131. function repl(s, dict) {
  132. if(s==null)return '';
  133. for(key in dict) {
  134. s = s.split("%("+key+")s").join(dict[key]);
  135. }
  136. return s;
  137. }
  138. function replace_all(s, t1, t2) {
  139. return s.split(t1).join(t2);
  140. }
  141. function getCookie(name) {
  142. return getCookies()[name];
  143. }
  144. function getCookies() {
  145. var c = document.cookie, v = 0, cookies = {};
  146. if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
  147. c = RegExp.$1;
  148. v = 1;
  149. }
  150. if (v === 0) {
  151. c.split(/[,;]/).map(function(cookie) {
  152. var parts = cookie.split(/=/, 2),
  153. name = decodeURIComponent(parts[0].trimLeft()),
  154. value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
  155. if(value && value.charAt(0)==='"') {
  156. value = value.substr(1, value.length-2);
  157. }
  158. cookies[name] = value;
  159. });
  160. } else {
  161. 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) {
  162. var name = $0,
  163. value = $1.charAt(0) === '"'
  164. ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
  165. : $1;
  166. cookies[name] = value;
  167. });
  168. }
  169. return cookies;
  170. }
  171. if (typeof String.prototype.trimLeft !== "function") {
  172. String.prototype.trimLeft = function() {
  173. return this.replace(/^\s+/, "");
  174. };
  175. }
  176. if (typeof String.prototype.trimRight !== "function") {
  177. String.prototype.trimRight = function() {
  178. return this.replace(/\s+$/, "");
  179. };
  180. }
  181. if (typeof Array.prototype.map !== "function") {
  182. Array.prototype.map = function(callback, thisArg) {
  183. for (var i=0, n=this.length, a=[]; i<n; i++) {
  184. if (i in this) a[i] = callback.call(thisArg, this[i]);
  185. }
  186. return a;
  187. };
  188. }
  189. function remove_script_and_style(txt) {
  190. return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
  191. $("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html();
  192. }
  193. function is_html(txt) {
  194. if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
  195. && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
  196. return false;
  197. }
  198. return true;
  199. }
  200. function ask_to_login() {
  201. if(!full_name) {
  202. if(localStorage) {
  203. localStorage.setItem("last_visited", window.location.href.split("/").slice(-1)[0]);
  204. }
  205. window.location.href = "login";
  206. }
  207. }
  208. // check if logged in?
  209. $(document).ready(function() {
  210. window.full_name = getCookie("full_name");
  211. $("#website-login").toggleClass("hide", full_name ? true : false);
  212. $("#website-post-login").toggleClass("hide", full_name ? false : true);
  213. });
  214. wn.send_message = function(opts, btn) {
  215. return wn.call({
  216. type: "POST",
  217. method: "website.doctype.contact_us_settings.templates.pages.contact.send_message",
  218. btn: btn,
  219. args: opts,
  220. callback: opts.callback
  221. });
  222. }