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.
 
 
 
 
 
 

305 lines
7.8 KiB

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