blob: 73fb04b1078a56f5ee8abb7becc37145f3dc1b91 [file] [log] [blame]
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05301
2var erpnext = {};
Rushabh Mehtaa75efa72013-03-19 17:59:49 +05303var wn = {};
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05304
Rushabh Mehtaa54cb242013-03-19 11:12:22 +05305// Add / update a new Lead / Communication
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05306// subject, sender, description
7erpnext.send_message = function(opts) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +05308 wn.call({
9 type: "POST",
10 method: "website.helpers.contact.send_message",
11 args: opts,
12 callback: opts.callback
13 })
14}
15
16wn.call = function(opts) {
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053017 if(opts.btn) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053018 var $spinner = $('<img src="lib/images/ui/button-load.gif">').appendTo($(opts.btn).parent())
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053019 $(opts.btn).attr("disabled", "disabled");
20 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053021
22 if(opts.msg) {
23 $(opts.msg).toggle(false);
24 }
25
26 // get or post?
27 if(!opts.args._type) {
28 opts.args._type = opts.type || "GET";
29 }
30
31 // method
32 if(opts.method) {
33 opts.args.cmd = opts.method;
34 }
35
36 // stringify
37 $.each(opts.args, function(key, val) {
38 if(typeof val != "string") {
39 opts.args[key] = JSON.stringify(val);
40 }
41 });
42
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053043 $.ajax({
Rushabh Mehtafdc629b2013-01-31 22:05:39 +053044 type: "POST",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053045 url: "server.py",
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053046 data: opts.args,
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053047 dataType: "json",
48 success: function(data) {
49 if(opts.btn) {
50 $(opts.btn).attr("disabled", false);
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053051 $spinner.remove();
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053052 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053053 if(data.exc) {
54 console.log(data.exc);
55 }
56 if(opts.msg && data.message) {
57 $(opts.msg).html(data.message).toggle(true);
58 }
59 if(opts.callback)
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053060 opts.callback(data);
61 }
62 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053063
64 return false;
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053065}
66
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053067// Setup the user tools
68//
69$(document).ready(function() {
70 // update login
71 var full_name = getCookie("full_name");
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053072 if(full_name) {
Anand Doshiab690292013-06-13 11:21:35 +053073 $("#user-tools").addClass("hide");
74 $("#user-tools-post-login").removeClass("hide");
75 $("#user-full-name").text(full_name);
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053076 }
Anand Doshiab690292013-06-13 11:21:35 +053077
78 wn.cart.update_display();
79 $("#user-tools a").tooltip({"placement":"bottom"});
80 $("#user-tools-post-login a").tooltip({"placement":"bottom"});
81
82 $(window).on("storage", function() { wn.cart.update_display(); });
83});
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053084
85// Utility functions
86
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053087function valid_email(id) {
88 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)
89 return 0; else return 1; }
90
Rushabh Mehta2e5db352013-01-16 11:34:26 +053091var validate_email = valid_email;
92
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053093function get_url_arg(name) {
94 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
95 var regexS = "[\\?&]"+name+"=([^&#]*)";
96 var regex = new RegExp( regexS );
97 var results = regex.exec( window.location.href );
98 if(results == null)
99 return "";
100 else
101 return decodeURIComponent(results[1]);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530102}
103
104function repl(s, dict) {
105 if(s==null)return '';
106 for(key in dict) {
107 s = s.split("%("+key+")s").join(dict[key]);
108 }
109 return s;
110}
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530111
Rushabh Mehtada512ba2013-03-22 12:45:44 +0530112function replace_all(s, t1, t2) {
113 return s.split(t1).join(t2);
114}
115
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530116function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530117 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530118}
119
120function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530121 var c = document.cookie, v = 0, cookies = {};
122 if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
123 c = RegExp.$1;
124 v = 1;
125 }
126 if (v === 0) {
127 c.split(/[,;]/).map(function(cookie) {
128 var parts = cookie.split(/=/, 2),
129 name = decodeURIComponent(parts[0].trimLeft()),
130 value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
Nabin Hait6dcee5a2013-03-26 10:45:29 +0530131 if(value && value.charAt(0)==='"') {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530132 value = value.substr(1, value.length-2);
133 }
134 cookies[name] = value;
135 });
136 } else {
137 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) {
138 var name = $0,
139 value = $1.charAt(0) === '"'
140 ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
141 : $1;
142 cookies[name] = value;
143 });
144 }
145 return cookies;
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530146}
147
148if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530149 String.prototype.trimLeft = function() {
150 return this.replace(/^\s+/, "");
151 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530152}
153if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530154 String.prototype.trimRight = function() {
155 return this.replace(/\s+$/, "");
156 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530157}
158if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530159 Array.prototype.map = function(callback, thisArg) {
160 for (var i=0, n=this.length, a=[]; i<n; i++) {
161 if (i in this) a[i] = callback.call(thisArg, this[i]);
162 }
163 return a;
164 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530165}
Anand Doshiab690292013-06-13 11:21:35 +0530166
167// shopping cart
168if(!wn.cart) wn.cart = {};
169$.extend(wn.cart, {
170 get_count: function() {
171 return Object.keys(this.get_cart()).length;
172 },
173
174 add_to_cart: function(itemprop) {
175 var cart = this.get_cart();
176 cart[itemprop.item_code] = $.extend(itemprop, {qty: 1});
177 this.set_cart(cart);
178 console.log(this.get_cart());
179 },
180
181 remove_from_cart: function(item_code) {
182 var cart = this.get_cart();
183 delete cart[item_code];
184 this.set_cart(cart);
185 console.log(this.get_cart());
186 },
187
188 get_cart: function() {
189 if( !("localStorage" in window) ) {
190 alert("Your browser seems to be ancient. Please use a modern browser.");
191 throw "ancient browser error";
192 }
193
194 return JSON.parse(localStorage.getItem("cart")) || {};
195 },
196
197 set_cart: function(cart) {
198 localStorage.setItem("cart", JSON.stringify(cart));
199 wn.cart.update_display();
200 },
201
202 update_display: function() {
203 $(".cart-count").text("( " + wn.cart.get_count() + " )");
204 }
205});