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.

преди 11 години
преди 11 години
преди 12 години
преди 12 години
преди 11 години
преди 11 години
преди 11 години
преди 12 години
преди 11 години
преди 12 години
преди 11 години
преди 11 години
преди 12 години
преди 11 години
преди 12 години
преди 11 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 11 години
преди 12 години
преди 12 години
преди 11 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 11 години
преди 12 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 11 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 12 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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,
  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. setup_push_state: function() {
  178. if(wn.supports_pjax()) {
  179. // hack for chrome's onload popstate call
  180. window.initial_href = window.location.href
  181. $(document).on("click", "#wrap a", wn.handle_click);
  182. $(window).on("popstate", function(event) {
  183. // hack for chrome's onload popstate call
  184. if(window.initial_href==location.href && window.previous_href==undefined) {
  185. wn.set_force_reload(true);
  186. return;
  187. }
  188. window.previous_href = location.href;
  189. var state = event.originalEvent.state;
  190. if(state) {
  191. wn.render_json(state);
  192. } else {
  193. console.log("state not found!");
  194. }
  195. });
  196. }
  197. },
  198. handle_click: function(event) {
  199. // taken from jquery pjax
  200. var link = event.currentTarget
  201. if (link.tagName.toUpperCase() !== 'A')
  202. throw "using pjax requires an anchor element"
  203. // Middle click, cmd click, and ctrl click should open
  204. // links in a new tab as normal.
  205. if ( event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey )
  206. return
  207. // Ignore cross origin links
  208. if ( location.protocol !== link.protocol || location.hostname !== link.hostname )
  209. return
  210. // Ignore anchors on the same page
  211. if (link.hash && link.href.replace(link.hash, '') ===
  212. location.href.replace(location.hash, ''))
  213. return
  214. // Ignore empty anchor "foo.html#"
  215. if (link.href === location.href + '#')
  216. return
  217. // our custom logic
  218. if (link.href.indexOf("cmd=")!==-1)
  219. return
  220. event.preventDefault()
  221. wn.load_via_ajax(link.href);
  222. },
  223. load_via_ajax: function(href) {
  224. // console.log("calling ajax");
  225. window.previous_href = href;
  226. history.pushState(null, null, href);
  227. $.ajax({ url: href, cache: false }).done(function(data) {
  228. history.replaceState(data, data.title, href);
  229. $("html, body").animate({ scrollTop: 0 }, "slow");
  230. wn.render_json(data);
  231. })
  232. },
  233. render_json: function(data) {
  234. if(data.reload) {
  235. window.location = location.href;
  236. } else {
  237. $('[data-html-block]').each(function(i, section) {
  238. var $section = $(section);
  239. if($section.attr("data-html-block")==="script") {
  240. $section.remove();
  241. $("<script data-html-block='script'></script>")
  242. .html(data[$section.attr("data-html-block")] || "")
  243. .appendTo("body");
  244. } else {
  245. $section.html(data[$section.attr("data-html-block")] || "");
  246. }
  247. });
  248. if(data.title) $("title").html(data.title);
  249. $(document).trigger("page_change");
  250. }
  251. },
  252. set_force_reload: function(reload) {
  253. // learned this from twitter's implementation
  254. window.history.replaceState({"reload": reload},
  255. window.document.title, location.href);
  256. },
  257. supports_pjax: function() {
  258. return (window.history && window.history.pushState && window.history.replaceState &&
  259. // pushState isn't reliable on iOS until 5.
  260. !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]|WebApps\/.+CFNetwork)/))
  261. }
  262. });
  263. // Utility functions
  264. function valid_email(id) {
  265. 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;
  266. }
  267. var validate_email = valid_email;
  268. function get_url_arg(name) {
  269. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  270. var regexS = "[\\?&]"+name+"=([^&#]*)";
  271. var regex = new RegExp( regexS );
  272. var results = regex.exec( window.location.href );
  273. if(results == null)
  274. return "";
  275. else
  276. return decodeURIComponent(results[1]);
  277. }
  278. function make_query_string(obj) {
  279. var query_params = [];
  280. $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
  281. return "?" + query_params.join("&");
  282. }
  283. function repl(s, dict) {
  284. if(s==null)return '';
  285. for(key in dict) {
  286. s = s.split("%("+key+")s").join(dict[key]);
  287. }
  288. return s;
  289. }
  290. function replace_all(s, t1, t2) {
  291. return s.split(t1).join(t2);
  292. }
  293. function getCookie(name) {
  294. return getCookies()[name];
  295. }
  296. wn.get_cookie = getCookie;
  297. function getCookies() {
  298. var c = document.cookie, v = 0, cookies = {};
  299. if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
  300. c = RegExp.$1;
  301. v = 1;
  302. }
  303. if (v === 0) {
  304. c.split(/[,;]/).map(function(cookie) {
  305. var parts = cookie.split(/=/, 2),
  306. name = decodeURIComponent(parts[0].trimLeft()),
  307. value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
  308. if(value && value.charAt(0)==='"') {
  309. value = value.substr(1, value.length-2);
  310. }
  311. cookies[name] = value;
  312. });
  313. } else {
  314. 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) {
  315. var name = $0,
  316. value = $1.charAt(0) === '"'
  317. ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
  318. : $1;
  319. cookies[name] = value;
  320. });
  321. }
  322. return cookies;
  323. }
  324. if (typeof String.prototype.trimLeft !== "function") {
  325. String.prototype.trimLeft = function() {
  326. return this.replace(/^\s+/, "");
  327. };
  328. }
  329. if (typeof String.prototype.trimRight !== "function") {
  330. String.prototype.trimRight = function() {
  331. return this.replace(/\s+$/, "");
  332. };
  333. }
  334. if (typeof Array.prototype.map !== "function") {
  335. Array.prototype.map = function(callback, thisArg) {
  336. for (var i=0, n=this.length, a=[]; i<n; i++) {
  337. if (i in this) a[i] = callback.call(thisArg, this[i]);
  338. }
  339. return a;
  340. };
  341. }
  342. function remove_script_and_style(txt) {
  343. return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
  344. $("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html();
  345. }
  346. function is_html(txt) {
  347. if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
  348. && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
  349. return false;
  350. }
  351. return true;
  352. }
  353. function ask_to_login() {
  354. if(!window.full_name) {
  355. if(localStorage) {
  356. localStorage.setItem("last_visited",
  357. window.location.href.replace(window.location.origin, ""));
  358. }
  359. window.location.href = "login";
  360. }
  361. }
  362. // check if logged in?
  363. $(document).ready(function() {
  364. window.full_name = getCookie("full_name");
  365. window.logged_in = getCookie("sid") && getCookie("sid")!=="Guest";
  366. $("#website-login").toggleClass("hide", logged_in ? true : false);
  367. $("#website-post-login").toggleClass("hide", logged_in ? false : true);
  368. $(".toggle-sidebar").on("click", function() {
  369. $(".page-sidebar").toggleClass("hidden-xs");
  370. $(".toggle-sidebar i").toggleClass("icon-rotate-180");
  371. });
  372. // switch to app link
  373. if(getCookie("system_user")==="yes") {
  374. $("#website-post-login .dropdown-menu").append('<li class="divider"></li>\
  375. <li><a href="app.html"><i class="icon-fixed-width icon-th-large"></i> Switch To App</a></li>');
  376. }
  377. wn.render_user();
  378. wn.setup_push_state()
  379. $(document).trigger("page_change");
  380. });
  381. $(document).on("page_change", function() {
  382. $(".page-header").toggleClass("hidden", !!!$("[data-html-block='header']").text().trim());
  383. $(".page-footer").toggleClass("hidden", !!!$(".page-footer").text().trim());
  384. $("[data-html-block='breadcrumbs'] .breadcrumb").toggleClass("hidden",
  385. $("[data-html-block='breadcrumbs']").text().trim()==$("[data-html-block='header']").text().trim());
  386. // add prive pages to sidebar
  387. if(website.private_pages && $(".page-sidebar").length) {
  388. $(data.private_pages).prependTo(".page-sidebar");
  389. }
  390. $(document).trigger("apply_permissions");
  391. wn.datetime.refresh_when();
  392. });