選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

258 行
6.5 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. if(!window.wn) wn = {};
  4. $.extend(wn, {
  5. show_message: function(text, icon) {
  6. if(!icon) icon="icon-refresh icon-spin";
  7. treemapper.hide_message();
  8. $('<div class="message-overlay"></div>')
  9. .html('<div class="content"><i class="'+icon+' text-muted"></i><br>'
  10. +text+'</div>').appendTo(document.body);
  11. },
  12. hide_message: function(text) {
  13. $('.message-overlay').remove();
  14. },
  15. call: function(opts) {
  16. wn.prepare_call(opts);
  17. $.ajax({
  18. type: "POST",
  19. url: "server.py",
  20. data: opts.args,
  21. dataType: "json",
  22. success: function(data) {
  23. wn.process_response(opts, data);
  24. },
  25. error: function(response) {
  26. NProgress.done();
  27. console.error ? console.error(response) : console.log(response);
  28. }
  29. });
  30. return false;
  31. },
  32. prepare_call: function(opts) {
  33. if(opts.btn) {
  34. $(opts.btn).prop("disabled", true);
  35. }
  36. if(opts.msg) {
  37. $(opts.msg).toggle(false);
  38. }
  39. if(!opts.args) opts.args = {};
  40. // get or post?
  41. if(!opts.args._type) {
  42. opts.args._type = opts.type || "GET";
  43. }
  44. // method
  45. if(opts.method) {
  46. opts.args.cmd = opts.method;
  47. }
  48. // stringify
  49. $.each(opts.args, function(key, val) {
  50. if(typeof val != "string") {
  51. opts.args[key] = JSON.stringify(val);
  52. }
  53. });
  54. NProgress.start();
  55. },
  56. process_response: function(opts, data) {
  57. NProgress.done();
  58. if(opts.btn) {
  59. $(opts.btn).prop("disabled", false);
  60. }
  61. if(data.exc) {
  62. if(opts.btn) {
  63. $(opts.btn).addClass("btn-danger");
  64. setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
  65. }
  66. try {
  67. var err = JSON.parse(data.exc);
  68. if($.isArray(err)) {
  69. err = err.join("\n");
  70. }
  71. console.error ? console.error(err) : console.log(err);
  72. } catch(e) {
  73. console.log(data.exc);
  74. }
  75. } else{
  76. if(opts.btn) {
  77. $(opts.btn).addClass("btn-success");
  78. setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
  79. }
  80. }
  81. if(opts.msg && data.message) {
  82. $(opts.msg).html(data.message).toggle(true);
  83. }
  84. if(opts.callback)
  85. opts.callback(data);
  86. },
  87. show_message: function(text, icon) {
  88. if(!icon) icon="icon-refresh icon-spin";
  89. wn.hide_message();
  90. $('<div class="message-overlay"></div>')
  91. .html('<div class="content"><i class="'+icon+' text-muted"></i><br>'
  92. +text+'</div>').appendTo(document.body);
  93. },
  94. hide_message: function(text) {
  95. $('.message-overlay').remove();
  96. },
  97. get_sid: function() {
  98. var sid = getCookie("sid");
  99. return sid && sid!=="Guest";
  100. },
  101. get_modal: function(title, body_html) {
  102. var modal = $('<div class="modal" style="overflow: auto;" tabindex="-1">\
  103. <div class="modal-dialog">\
  104. <div class="modal-content">\
  105. <div class="modal-header">\
  106. <a type="button" class="close"\
  107. data-dismiss="modal" aria-hidden="true">&times;</a>\
  108. <h4 class="modal-title">'+title+'</h4>\
  109. </div>\
  110. <div class="modal-body ui-front">'+body_html+'\
  111. </div>\
  112. </div>\
  113. </div>\
  114. </div>').appendTo(document.body);
  115. return modal;
  116. },
  117. msgprint: function(html, title) {
  118. return wn.get_modal(title || "Message", html).modal("show");
  119. },
  120. send_message: function(opts, btn) {
  121. return wn.call({
  122. type: "POST",
  123. method: "website.doctype.contact_us_settings.templates.pages.contact.send_message",
  124. btn: btn,
  125. args: opts,
  126. callback: opts.callback
  127. });
  128. },
  129. });
  130. // Utility functions
  131. function valid_email(id) {
  132. 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)
  133. return 0; else return 1; }
  134. var validate_email = valid_email;
  135. function get_url_arg(name) {
  136. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  137. var regexS = "[\\?&]"+name+"=([^&#]*)";
  138. var regex = new RegExp( regexS );
  139. var results = regex.exec( window.location.href );
  140. if(results == null)
  141. return "";
  142. else
  143. return decodeURIComponent(results[1]);
  144. }
  145. function make_query_string(obj) {
  146. var query_params = [];
  147. $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
  148. return "?" + query_params.join("&");
  149. }
  150. function repl(s, dict) {
  151. if(s==null)return '';
  152. for(key in dict) {
  153. s = s.split("%("+key+")s").join(dict[key]);
  154. }
  155. return s;
  156. }
  157. function replace_all(s, t1, t2) {
  158. return s.split(t1).join(t2);
  159. }
  160. function getCookie(name) {
  161. return getCookies()[name];
  162. }
  163. function getCookies() {
  164. var c = document.cookie, v = 0, cookies = {};
  165. if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
  166. c = RegExp.$1;
  167. v = 1;
  168. }
  169. if (v === 0) {
  170. c.split(/[,;]/).map(function(cookie) {
  171. var parts = cookie.split(/=/, 2),
  172. name = decodeURIComponent(parts[0].trimLeft()),
  173. value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
  174. if(value && value.charAt(0)==='"') {
  175. value = value.substr(1, value.length-2);
  176. }
  177. cookies[name] = value;
  178. });
  179. } else {
  180. 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) {
  181. var name = $0,
  182. value = $1.charAt(0) === '"'
  183. ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
  184. : $1;
  185. cookies[name] = value;
  186. });
  187. }
  188. return cookies;
  189. }
  190. if (typeof String.prototype.trimLeft !== "function") {
  191. String.prototype.trimLeft = function() {
  192. return this.replace(/^\s+/, "");
  193. };
  194. }
  195. if (typeof String.prototype.trimRight !== "function") {
  196. String.prototype.trimRight = function() {
  197. return this.replace(/\s+$/, "");
  198. };
  199. }
  200. if (typeof Array.prototype.map !== "function") {
  201. Array.prototype.map = function(callback, thisArg) {
  202. for (var i=0, n=this.length, a=[]; i<n; i++) {
  203. if (i in this) a[i] = callback.call(thisArg, this[i]);
  204. }
  205. return a;
  206. };
  207. }
  208. function remove_script_and_style(txt) {
  209. return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
  210. $("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html();
  211. }
  212. function is_html(txt) {
  213. if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
  214. && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
  215. return false;
  216. }
  217. return true;
  218. }
  219. function ask_to_login() {
  220. if(!full_name) {
  221. if(localStorage) {
  222. localStorage.setItem("last_visited", window.location.href.split("/").slice(-1)[0]);
  223. }
  224. window.location.href = "login";
  225. }
  226. }
  227. // check if logged in?
  228. $(document).ready(function() {
  229. window.full_name = getCookie("full_name");
  230. $("#website-login").toggleClass("hide", full_name ? true : false);
  231. $("#website-post-login").toggleClass("hide", full_name ? false : true);
  232. });