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.

website.js 5.9 KiB

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