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.
 
 
 
 
 
 

290 rader
7.4 KiB

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