25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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