Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
Anand Doshi | 6c8ef77 | 2013-08-30 18:23:50 +0530 | [diff] [blame] | 4 | if(!window.erpnext) erpnext = {}; |
| 5 | if(!window.wn) wn = {}; |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 6 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 7 | // Add / update a new Lead / Communication |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 8 | // subject, sender, description |
| 9 | erpnext.send_message = function(opts) { |
Anand Doshi | 1fac2a9 | 2013-07-29 19:30:39 +0530 | [diff] [blame] | 10 | return wn.call({ |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 11 | type: "POST", |
| 12 | method: "website.helpers.contact.send_message", |
| 13 | args: opts, |
| 14 | callback: opts.callback |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 15 | }); |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | wn.call = function(opts) { |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 19 | if(opts.btn) { |
Anand Doshi | c8e39b0 | 2013-08-30 18:22:58 +0530 | [diff] [blame] | 20 | $(opts.btn).prop("disabled", true); |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 21 | } |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 22 | |
| 23 | if(opts.msg) { |
| 24 | $(opts.msg).toggle(false); |
| 25 | } |
| 26 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 27 | if(!opts.args) opts.args = {}; |
| 28 | |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 29 | // get or post? |
| 30 | if(!opts.args._type) { |
| 31 | opts.args._type = opts.type || "GET"; |
| 32 | } |
| 33 | |
| 34 | // method |
| 35 | if(opts.method) { |
| 36 | opts.args.cmd = opts.method; |
| 37 | } |
| 38 | |
| 39 | // stringify |
| 40 | $.each(opts.args, function(key, val) { |
| 41 | if(typeof val != "string") { |
| 42 | opts.args[key] = JSON.stringify(val); |
| 43 | } |
| 44 | }); |
| 45 | |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 46 | $.ajax({ |
Rushabh Mehta | fdc629b | 2013-01-31 22:05:39 +0530 | [diff] [blame] | 47 | type: "POST", |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 48 | url: "server.py", |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 49 | data: opts.args, |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 50 | dataType: "json", |
| 51 | success: function(data) { |
| 52 | if(opts.btn) { |
Anand Doshi | c8e39b0 | 2013-08-30 18:22:58 +0530 | [diff] [blame] | 53 | $(opts.btn).prop("disabled", false); |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 54 | } |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 55 | if(data.exc) { |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 56 | if(opts.btn) { |
| 57 | $(opts.btn).addClass("btn-danger"); |
| 58 | setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000); |
| 59 | } |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 60 | try { |
| 61 | var err = JSON.parse(data.exc); |
| 62 | if($.isArray(err)) { |
| 63 | err = err.join("\n"); |
| 64 | } |
| 65 | console.error ? console.error(err) : console.log(err); |
| 66 | } catch(e) { |
| 67 | console.log(data.exc); |
| 68 | } |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 69 | } else{ |
| 70 | if(opts.btn) { |
| 71 | $(opts.btn).addClass("btn-success"); |
| 72 | setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000); |
| 73 | } |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 74 | } |
| 75 | if(opts.msg && data.message) { |
| 76 | $(opts.msg).html(data.message).toggle(true); |
| 77 | } |
| 78 | if(opts.callback) |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 79 | opts.callback(data); |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 80 | }, |
| 81 | error: function(response) { |
| 82 | console.error ? console.error(response) : console.log(response); |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 83 | } |
| 84 | }); |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 85 | |
| 86 | return false; |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 87 | } |
| 88 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 89 | // Setup the user tools |
| 90 | // |
| 91 | $(document).ready(function() { |
| 92 | // update login |
| 93 | var full_name = getCookie("full_name"); |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 94 | if(full_name) { |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 95 | $("#user-tools").addClass("hide"); |
| 96 | $("#user-tools-post-login").removeClass("hide"); |
| 97 | $("#user-full-name").text(full_name); |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 98 | } |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 99 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 100 | wn.cart.set_cart_count(); |
| 101 | |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 102 | $("#user-tools a").tooltip({"placement":"bottom"}); |
| 103 | $("#user-tools-post-login a").tooltip({"placement":"bottom"}); |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 104 | }); |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 105 | |
| 106 | // Utility functions |
| 107 | |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 108 | function valid_email(id) { |
| 109 | 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) |
| 110 | return 0; else return 1; } |
| 111 | |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 112 | var validate_email = valid_email; |
| 113 | |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 114 | function get_url_arg(name) { |
| 115 | name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); |
| 116 | var regexS = "[\\?&]"+name+"=([^&#]*)"; |
| 117 | var regex = new RegExp( regexS ); |
| 118 | var results = regex.exec( window.location.href ); |
| 119 | if(results == null) |
| 120 | return ""; |
| 121 | else |
| 122 | return decodeURIComponent(results[1]); |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 123 | } |
| 124 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 125 | function make_query_string(obj) { |
| 126 | var query_params = []; |
| 127 | $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); }); |
| 128 | return "?" + query_params.join("&"); |
| 129 | } |
| 130 | |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 131 | function repl(s, dict) { |
| 132 | if(s==null)return ''; |
| 133 | for(key in dict) { |
| 134 | s = s.split("%("+key+")s").join(dict[key]); |
| 135 | } |
| 136 | return s; |
| 137 | } |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 138 | |
Rushabh Mehta | da512ba | 2013-03-22 12:45:44 +0530 | [diff] [blame] | 139 | function replace_all(s, t1, t2) { |
| 140 | return s.split(t1).join(t2); |
| 141 | } |
| 142 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 143 | function getCookie(name) { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 144 | return getCookies()[name]; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | function getCookies() { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 148 | var c = document.cookie, v = 0, cookies = {}; |
| 149 | if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) { |
| 150 | c = RegExp.$1; |
| 151 | v = 1; |
| 152 | } |
| 153 | if (v === 0) { |
| 154 | c.split(/[,;]/).map(function(cookie) { |
| 155 | var parts = cookie.split(/=/, 2), |
| 156 | name = decodeURIComponent(parts[0].trimLeft()), |
| 157 | value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null; |
Nabin Hait | 6dcee5a | 2013-03-26 10:45:29 +0530 | [diff] [blame] | 158 | if(value && value.charAt(0)==='"') { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 159 | value = value.substr(1, value.length-2); |
| 160 | } |
| 161 | cookies[name] = value; |
| 162 | }); |
| 163 | } else { |
| 164 | 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) { |
| 165 | var name = $0, |
| 166 | value = $1.charAt(0) === '"' |
| 167 | ? $1.substr(1, -1).replace(/\\(.)/g, "$1") |
| 168 | : $1; |
| 169 | cookies[name] = value; |
| 170 | }); |
| 171 | } |
| 172 | return cookies; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if (typeof String.prototype.trimLeft !== "function") { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 176 | String.prototype.trimLeft = function() { |
| 177 | return this.replace(/^\s+/, ""); |
| 178 | }; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 179 | } |
| 180 | if (typeof String.prototype.trimRight !== "function") { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 181 | String.prototype.trimRight = function() { |
| 182 | return this.replace(/\s+$/, ""); |
| 183 | }; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 184 | } |
| 185 | if (typeof Array.prototype.map !== "function") { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 186 | Array.prototype.map = function(callback, thisArg) { |
| 187 | for (var i=0, n=this.length, a=[]; i<n; i++) { |
| 188 | if (i in this) a[i] = callback.call(thisArg, this[i]); |
| 189 | } |
| 190 | return a; |
| 191 | }; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 192 | } |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 193 | |
| 194 | // shopping cart |
| 195 | if(!wn.cart) wn.cart = {}; |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 196 | var full_name = getCookie("full_name"); |
| 197 | |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 198 | $.extend(wn.cart, { |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 199 | update_cart: function(opts) { |
| 200 | if(!full_name) { |
| 201 | if(localStorage) { |
Anand Doshi | a9c0f5d | 2013-09-05 12:19:00 +0530 | [diff] [blame] | 202 | localStorage.setItem("last_visited", window.location.href.split("/").slice(-1)[0]); |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 203 | localStorage.setItem("pending_add_to_cart", opts.item_code); |
| 204 | } |
| 205 | window.location.href = "login"; |
| 206 | } else { |
Anand Doshi | 1fac2a9 | 2013-07-29 19:30:39 +0530 | [diff] [blame] | 207 | return wn.call({ |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 208 | type: "POST", |
| 209 | method: "website.helpers.cart.update_cart", |
| 210 | args: { |
| 211 | item_code: opts.item_code, |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 212 | qty: opts.qty, |
| 213 | with_doclist: opts.with_doclist |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 214 | }, |
| 215 | btn: opts.btn, |
| 216 | callback: function(r) { |
| 217 | if(opts.callback) |
| 218 | opts.callback(r); |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 219 | |
| 220 | wn.cart.set_cart_count(); |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 221 | } |
| 222 | }); |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 223 | } |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 224 | }, |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 225 | |
| 226 | set_cart_count: function() { |
| 227 | var cart_count = getCookie("cart_count"); |
| 228 | if(cart_count) |
| 229 | $(".cart-count").html("( "+ cart_count +" )") |
| 230 | } |
Anand Doshi | 6c8ef77 | 2013-08-30 18:23:50 +0530 | [diff] [blame] | 231 | }); |
| 232 | |
| 233 | function remove_script_and_style(txt) { |
| 234 | return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt : |
| 235 | $("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html(); |
| 236 | } |
| 237 | |
| 238 | function is_html(txt) { |
| 239 | if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1 |
| 240 | && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) { |
| 241 | return false; |
| 242 | } |
| 243 | return true; |
Anand Doshi | a9c0f5d | 2013-09-05 12:19:00 +0530 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | function ask_to_login() { |
| 247 | if(!full_name) { |
| 248 | if(localStorage) { |
| 249 | localStorage.setItem("last_visited", window.location.href.split("/").slice(-1)[0]); |
| 250 | } |
| 251 | window.location.href = "login"; |
| 252 | } |
Anand Doshi | 6c8ef77 | 2013-08-30 18:23:50 +0530 | [diff] [blame] | 253 | } |