Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

335 рядки
8.6 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.provide("website");
  4. $.extend(wn, {
  5. _assets_loaded: [],
  6. require: function(url) {
  7. if(wn._assets_loaded.indexOf(url)!==-1) return;
  8. $.ajax({
  9. url: url, //+ "?q=" + Math.floor(Math.random() * 1000),
  10. async: false,
  11. dataType: "text",
  12. success: function(data) {
  13. if(url.split(".").splice(-1) == "js") {
  14. var el = document.createElement('script');
  15. } else {
  16. var el = document.createElement('style');
  17. }
  18. el.appendChild(document.createTextNode(data));
  19. document.getElementsByTagName('head')[0].appendChild(el);
  20. wn._assets_loaded.push(url);
  21. }
  22. });
  23. },
  24. hide_message: function(text) {
  25. $('.message-overlay').remove();
  26. },
  27. call: function(opts) {
  28. // opts = {"method": "PYTHON MODULE STRING", "args": {}, "callback": function(r) {}}
  29. wn.prepare_call(opts);
  30. return $.ajax({
  31. type: opts.type || "POST",
  32. url: "/",
  33. data: opts.args,
  34. dataType: "json",
  35. statusCode: {
  36. 404: function(xhr) {
  37. msgprint("Not Found");
  38. },
  39. 403: function(xhr) {
  40. msgprint("Not Permitted");
  41. },
  42. 200: function(data, xhr) {
  43. if(opts.callback)
  44. opts.callback(data);
  45. }
  46. }
  47. }).always(function(data) {
  48. // executed before statusCode functions
  49. if(data.responseText) {
  50. data = JSON.parse(data.responseText);
  51. }
  52. wn.process_response(opts, data);
  53. });
  54. },
  55. prepare_call: function(opts) {
  56. if(opts.btn) {
  57. $(opts.btn).prop("disabled", true);
  58. }
  59. if(opts.msg) {
  60. $(opts.msg).toggle(false);
  61. }
  62. if(!opts.args) opts.args = {};
  63. // get or post?
  64. if(!opts.args._type) {
  65. opts.args._type = opts.type || "GET";
  66. }
  67. // method
  68. if(opts.method) {
  69. opts.args.cmd = opts.method;
  70. }
  71. // stringify
  72. $.each(opts.args, function(key, val) {
  73. if(typeof val != "string") {
  74. opts.args[key] = JSON.stringify(val);
  75. }
  76. });
  77. if(!opts.no_spinner) {
  78. NProgress.start();
  79. }
  80. },
  81. process_response: function(opts, data) {
  82. if(!opts.no_spinner) NProgress.done();
  83. if(opts.btn) {
  84. $(opts.btn).prop("disabled", false);
  85. }
  86. if(data.exc) {
  87. if(opts.btn) {
  88. $(opts.btn).addClass("btn-danger");
  89. setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
  90. }
  91. try {
  92. var err = JSON.parse(data.exc);
  93. if($.isArray(err)) {
  94. err = err.join("\n");
  95. }
  96. console.error ? console.error(err) : console.log(err);
  97. } catch(e) {
  98. console.log(data.exc);
  99. }
  100. } else{
  101. if(opts.btn) {
  102. $(opts.btn).addClass("btn-success");
  103. setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
  104. }
  105. }
  106. if(opts.msg && data.message) {
  107. $(opts.msg).html(data.message).toggle(true);
  108. }
  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. render_user: function() {
  169. var sid = wn.get_cookie("sid");
  170. if(sid && sid!=="Guest") {
  171. $(".btn-login-area").toggle(false);
  172. $(".logged-in").toggle(true);
  173. $(".full-name").html(wn.get_cookie("full_name"));
  174. $(".user-picture").attr("src", wn.get_cookie("user_image"));
  175. }
  176. }
  177. });
  178. // Utility functions
  179. function valid_email(id) {
  180. return (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) ? 0 : 1;
  181. }
  182. var validate_email = valid_email;
  183. function get_url_arg(name) {
  184. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  185. var regexS = "[\\?&]"+name+"=([^&#]*)";
  186. var regex = new RegExp( regexS );
  187. var results = regex.exec( window.location.href );
  188. if(results == null)
  189. return "";
  190. else
  191. return decodeURIComponent(results[1]);
  192. }
  193. function make_query_string(obj) {
  194. var query_params = [];
  195. $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
  196. return "?" + query_params.join("&");
  197. }
  198. function repl(s, dict) {
  199. if(s==null)return '';
  200. for(key in dict) {
  201. s = s.split("%("+key+")s").join(dict[key]);
  202. }
  203. return s;
  204. }
  205. function replace_all(s, t1, t2) {
  206. return s.split(t1).join(t2);
  207. }
  208. function getCookie(name) {
  209. return getCookies()[name];
  210. }
  211. wn.get_cookie = getCookie;
  212. function getCookies() {
  213. var c = document.cookie, v = 0, cookies = {};
  214. if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
  215. c = RegExp.$1;
  216. v = 1;
  217. }
  218. if (v === 0) {
  219. c.split(/[,;]/).map(function(cookie) {
  220. var parts = cookie.split(/=/, 2),
  221. name = decodeURIComponent(parts[0].trimLeft()),
  222. value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
  223. if(value && value.charAt(0)==='"') {
  224. value = value.substr(1, value.length-2);
  225. }
  226. cookies[name] = value;
  227. });
  228. } else {
  229. 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) {
  230. var name = $0,
  231. value = $1.charAt(0) === '"'
  232. ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
  233. : $1;
  234. cookies[name] = value;
  235. });
  236. }
  237. return cookies;
  238. }
  239. if (typeof String.prototype.trimLeft !== "function") {
  240. String.prototype.trimLeft = function() {
  241. return this.replace(/^\s+/, "");
  242. };
  243. }
  244. if (typeof String.prototype.trimRight !== "function") {
  245. String.prototype.trimRight = function() {
  246. return this.replace(/\s+$/, "");
  247. };
  248. }
  249. if (typeof Array.prototype.map !== "function") {
  250. Array.prototype.map = function(callback, thisArg) {
  251. for (var i=0, n=this.length, a=[]; i<n; i++) {
  252. if (i in this) a[i] = callback.call(thisArg, this[i]);
  253. }
  254. return a;
  255. };
  256. }
  257. function remove_script_and_style(txt) {
  258. return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
  259. $("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html();
  260. }
  261. function is_html(txt) {
  262. if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
  263. && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
  264. return false;
  265. }
  266. return true;
  267. }
  268. function ask_to_login() {
  269. if(!window.full_name) {
  270. if(localStorage) {
  271. localStorage.setItem("last_visited",
  272. window.location.href.replace(window.location.origin, ""));
  273. }
  274. window.location.href = "login";
  275. }
  276. }
  277. // check if logged in?
  278. $(document).ready(function() {
  279. window.full_name = getCookie("full_name");
  280. window.logged_in = getCookie("sid") && getCookie("sid")!=="Guest";
  281. $("#website-login").toggleClass("hide", logged_in ? true : false);
  282. $("#website-post-login").toggleClass("hide", logged_in ? false : true);
  283. // switch to app link
  284. if(getCookie("system_user")==="yes") {
  285. $("#website-post-login .dropdown-menu").append('<li class="divider"></li>\
  286. <li><a href="app.html"><i class="icon-fixed-width icon-th-large"></i> Switch To App</a></li>');
  287. }
  288. wn.render_user();
  289. $(document).trigger("page_change");
  290. });
  291. $(document).on("page_change", function() {
  292. $(".page-header").toggleClass("hidden", !!!$(".page-header").text().trim());
  293. $(".page-footer").toggleClass("hidden", !!!$(".page-footer").text().trim());
  294. // add prive pages to sidebar
  295. if(website.private_pages && $(".page-sidebar").length) {
  296. $(data.private_pages).prependTo(".page-sidebar");
  297. }
  298. $(document).trigger("apply_permissions");
  299. wn.datetime.refresh_when();
  300. });