blob: b5fea13bbe509fe03cc0f1ff9195831d9a0fc26c [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
Anand Doshi6c8ef772013-08-30 18:23:50 +05304if(!window.erpnext) erpnext = {};
5if(!window.wn) wn = {};
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05306
Rushabh Mehtaa54cb242013-03-19 11:12:22 +05307// Add / update a new Lead / Communication
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05308// subject, sender, description
9erpnext.send_message = function(opts) {
Anand Doshi1fac2a92013-07-29 19:30:39 +053010 return wn.call({
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053011 type: "POST",
Rushabh Mehtac59c4e02013-09-09 12:17:45 +053012 method: "selling.utils.contact.send_message",
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053013 args: opts,
14 callback: opts.callback
Anand Doshiedbf3e12013-07-02 11:40:16 +053015 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053016}
17
18wn.call = function(opts) {
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053019 if(opts.btn) {
Anand Doshic8e39b02013-08-30 18:22:58 +053020 $(opts.btn).prop("disabled", true);
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053021 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053022
23 if(opts.msg) {
24 $(opts.msg).toggle(false);
25 }
26
Anand Doshi3dceb842013-06-19 14:57:14 +053027 if(!opts.args) opts.args = {};
28
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053029 // 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 Mehta173a0fd2012-12-14 16:39:27 +053046 $.ajax({
Rushabh Mehtafdc629b2013-01-31 22:05:39 +053047 type: "POST",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053048 url: "server.py",
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053049 data: opts.args,
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053050 dataType: "json",
51 success: function(data) {
52 if(opts.btn) {
Anand Doshic8e39b02013-08-30 18:22:58 +053053 $(opts.btn).prop("disabled", false);
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053054 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053055 if(data.exc) {
Anand Doshi3dceb842013-06-19 14:57:14 +053056 if(opts.btn) {
57 $(opts.btn).addClass("btn-danger");
58 setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
59 }
Anand Doshi99100a42013-07-04 17:13:53 +053060 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 Doshi3dceb842013-06-19 14:57:14 +053069 } else{
70 if(opts.btn) {
71 $(opts.btn).addClass("btn-success");
72 setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
73 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053074 }
75 if(opts.msg && data.message) {
76 $(opts.msg).html(data.message).toggle(true);
77 }
78 if(opts.callback)
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053079 opts.callback(data);
Anand Doshi99100a42013-07-04 17:13:53 +053080 },
81 error: function(response) {
82 console.error ? console.error(response) : console.log(response);
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053083 }
84 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053085
86 return false;
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053087}
88
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053089// Setup the user tools
90//
91$(document).ready(function() {
92 // update login
93 var full_name = getCookie("full_name");
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053094 if(full_name) {
Anand Doshiab690292013-06-13 11:21:35 +053095 $("#user-tools").addClass("hide");
96 $("#user-tools-post-login").removeClass("hide");
97 $("#user-full-name").text(full_name);
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053098 }
Anand Doshiab690292013-06-13 11:21:35 +053099
Anand Doshi2ac0a832013-07-10 20:49:44 +0530100 wn.cart.set_cart_count();
101
Anand Doshiab690292013-06-13 11:21:35 +0530102 $("#user-tools a").tooltip({"placement":"bottom"});
103 $("#user-tools-post-login a").tooltip({"placement":"bottom"});
Anand Doshiab690292013-06-13 11:21:35 +0530104});
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530105
106// Utility functions
107
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530108function 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 Mehta2e5db352013-01-16 11:34:26 +0530112var validate_email = valid_email;
113
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530114function 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 Mehtafd6ad192012-12-17 12:52:43 +0530123}
124
Anand Doshi3dceb842013-06-19 14:57:14 +0530125function 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 Mehtafd6ad192012-12-17 12:52:43 +0530131function 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 Mehtaa54cb242013-03-19 11:12:22 +0530138
Rushabh Mehtada512ba2013-03-22 12:45:44 +0530139function replace_all(s, t1, t2) {
140 return s.split(t1).join(t2);
141}
142
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530143function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530144 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530145}
146
147function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530148 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 Hait6dcee5a2013-03-26 10:45:29 +0530158 if(value && value.charAt(0)==='"') {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530159 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 Mehtaa54cb242013-03-19 11:12:22 +0530173}
174
175if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530176 String.prototype.trimLeft = function() {
177 return this.replace(/^\s+/, "");
178 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530179}
180if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530181 String.prototype.trimRight = function() {
182 return this.replace(/\s+$/, "");
183 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530184}
185if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530186 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 Mehtaa54cb242013-03-19 11:12:22 +0530192}
Anand Doshiab690292013-06-13 11:21:35 +0530193
194// shopping cart
195if(!wn.cart) wn.cart = {};
Anand Doshi3dceb842013-06-19 14:57:14 +0530196var full_name = getCookie("full_name");
197
Anand Doshiab690292013-06-13 11:21:35 +0530198$.extend(wn.cart, {
Anand Doshi3dceb842013-06-19 14:57:14 +0530199 update_cart: function(opts) {
200 if(!full_name) {
201 if(localStorage) {
Anand Doshia9c0f5d2013-09-05 12:19:00 +0530202 localStorage.setItem("last_visited", window.location.href.split("/").slice(-1)[0]);
Anand Doshi3dceb842013-06-19 14:57:14 +0530203 localStorage.setItem("pending_add_to_cart", opts.item_code);
204 }
205 window.location.href = "login";
206 } else {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530207 return wn.call({
Anand Doshi3dceb842013-06-19 14:57:14 +0530208 type: "POST",
Rushabh Mehtac59c4e02013-09-09 12:17:45 +0530209 method: "selling.utils.cart.update_cart",
Anand Doshi3dceb842013-06-19 14:57:14 +0530210 args: {
211 item_code: opts.item_code,
Anand Doshic2a35272013-06-19 17:19:20 +0530212 qty: opts.qty,
213 with_doclist: opts.with_doclist
Anand Doshi3dceb842013-06-19 14:57:14 +0530214 },
215 btn: opts.btn,
216 callback: function(r) {
217 if(opts.callback)
218 opts.callback(r);
Anand Doshi2ac0a832013-07-10 20:49:44 +0530219
220 wn.cart.set_cart_count();
Anand Doshi3dceb842013-06-19 14:57:14 +0530221 }
222 });
Anand Doshiab690292013-06-13 11:21:35 +0530223 }
Anand Doshiab690292013-06-13 11:21:35 +0530224 },
Anand Doshi2ac0a832013-07-10 20:49:44 +0530225
226 set_cart_count: function() {
227 var cart_count = getCookie("cart_count");
228 if(cart_count)
229 $(".cart-count").html("( "+ cart_count +" )")
230 }
Anand Doshi6c8ef772013-08-30 18:23:50 +0530231});
232
233function 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
238function 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 Doshia9c0f5d2013-09-05 12:19:00 +0530244}
245
246function 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 Doshi6c8ef772013-08-30 18:23:50 +0530253}