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.
 
 
 
 
 
 

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