blob: 95cae1bbc61724b31a4b5c3a8fa6cbe76cce2408 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2// License: GNU General Public License v3. See license.txt
3
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05304
5var erpnext = {};
Rushabh Mehtaa75efa72013-03-19 17:59:49 +05306var wn = {};
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05307
Rushabh Mehtaa54cb242013-03-19 11:12:22 +05308// Add / update a new Lead / Communication
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05309// subject, sender, description
10erpnext.send_message = function(opts) {
Anand Doshi1fac2a92013-07-29 19:30:39 +053011 return wn.call({
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053012 type: "POST",
13 method: "website.helpers.contact.send_message",
14 args: opts,
15 callback: opts.callback
Anand Doshiedbf3e12013-07-02 11:40:16 +053016 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053017}
18
19wn.call = function(opts) {
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053020 if(opts.btn) {
21 $(opts.btn).attr("disabled", "disabled");
22 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053023
24 if(opts.msg) {
25 $(opts.msg).toggle(false);
26 }
27
Anand Doshi3dceb842013-06-19 14:57:14 +053028 if(!opts.args) opts.args = {};
29
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053030 // 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 Mehta173a0fd2012-12-14 16:39:27 +053047 $.ajax({
Rushabh Mehtafdc629b2013-01-31 22:05:39 +053048 type: "POST",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053049 url: "server.py",
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053050 data: opts.args,
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053051 dataType: "json",
52 success: function(data) {
53 if(opts.btn) {
54 $(opts.btn).attr("disabled", false);
55 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053056 if(data.exc) {
Anand Doshi3dceb842013-06-19 14:57:14 +053057 if(opts.btn) {
58 $(opts.btn).addClass("btn-danger");
59 setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
60 }
Anand Doshi99100a42013-07-04 17:13:53 +053061 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 Doshi3dceb842013-06-19 14:57:14 +053070 } else{
71 if(opts.btn) {
72 $(opts.btn).addClass("btn-success");
73 setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
74 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053075 }
76 if(opts.msg && data.message) {
77 $(opts.msg).html(data.message).toggle(true);
78 }
79 if(opts.callback)
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053080 opts.callback(data);
Anand Doshi99100a42013-07-04 17:13:53 +053081 },
82 error: function(response) {
83 console.error ? console.error(response) : console.log(response);
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053084 }
85 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053086
87 return false;
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053088}
89
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053090// Setup the user tools
91//
92$(document).ready(function() {
93 // update login
94 var full_name = getCookie("full_name");
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053095 if(full_name) {
Anand Doshiab690292013-06-13 11:21:35 +053096 $("#user-tools").addClass("hide");
97 $("#user-tools-post-login").removeClass("hide");
98 $("#user-full-name").text(full_name);
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053099 }
Anand Doshiab690292013-06-13 11:21:35 +0530100
Anand Doshi2ac0a832013-07-10 20:49:44 +0530101 wn.cart.set_cart_count();
102
Anand Doshiab690292013-06-13 11:21:35 +0530103 $("#user-tools a").tooltip({"placement":"bottom"});
104 $("#user-tools-post-login a").tooltip({"placement":"bottom"});
Anand Doshiab690292013-06-13 11:21:35 +0530105});
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530106
107// Utility functions
108
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530109function 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 Mehta2e5db352013-01-16 11:34:26 +0530113var validate_email = valid_email;
114
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530115function 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 Mehtafd6ad192012-12-17 12:52:43 +0530124}
125
Anand Doshi3dceb842013-06-19 14:57:14 +0530126function 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 Mehtafd6ad192012-12-17 12:52:43 +0530132function 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 Mehtaa54cb242013-03-19 11:12:22 +0530139
Rushabh Mehtada512ba2013-03-22 12:45:44 +0530140function replace_all(s, t1, t2) {
141 return s.split(t1).join(t2);
142}
143
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530144function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530145 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530146}
147
148function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530149 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 Hait6dcee5a2013-03-26 10:45:29 +0530159 if(value && value.charAt(0)==='"') {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530160 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 Mehtaa54cb242013-03-19 11:12:22 +0530174}
175
176if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530177 String.prototype.trimLeft = function() {
178 return this.replace(/^\s+/, "");
179 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530180}
181if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530182 String.prototype.trimRight = function() {
183 return this.replace(/\s+$/, "");
184 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530185}
186if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530187 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 Mehtaa54cb242013-03-19 11:12:22 +0530193}
Anand Doshiab690292013-06-13 11:21:35 +0530194
195// shopping cart
196if(!wn.cart) wn.cart = {};
Anand Doshi3dceb842013-06-19 14:57:14 +0530197var full_name = getCookie("full_name");
198
Anand Doshiab690292013-06-13 11:21:35 +0530199$.extend(wn.cart, {
Anand Doshi3dceb842013-06-19 14:57:14 +0530200 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 Doshi1fac2a92013-07-29 19:30:39 +0530208 return wn.call({
Anand Doshi3dceb842013-06-19 14:57:14 +0530209 type: "POST",
210 method: "website.helpers.cart.update_cart",
211 args: {
212 item_code: opts.item_code,
Anand Doshic2a35272013-06-19 17:19:20 +0530213 qty: opts.qty,
214 with_doclist: opts.with_doclist
Anand Doshi3dceb842013-06-19 14:57:14 +0530215 },
216 btn: opts.btn,
217 callback: function(r) {
218 if(opts.callback)
219 opts.callback(r);
Anand Doshi2ac0a832013-07-10 20:49:44 +0530220
221 wn.cart.set_cart_count();
Anand Doshi3dceb842013-06-19 14:57:14 +0530222 }
223 });
Anand Doshiab690292013-06-13 11:21:35 +0530224 }
Anand Doshiab690292013-06-13 11:21:35 +0530225 },
Anand Doshi2ac0a832013-07-10 20:49:44 +0530226
227 set_cart_count: function() {
228 var cart_count = getCookie("cart_count");
229 if(cart_count)
230 $(".cart-count").html("( "+ cart_count +" )")
231 }
Anand Doshiab690292013-06-13 11:21:35 +0530232});