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