2013-08-05 09:29:54 +00:00
|
|
|
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
|
|
|
|
// License: GNU General Public License v3. See license.txt
|
|
|
|
|
2012-12-14 11:09:27 +00:00
|
|
|
|
2013-08-30 12:53:50 +00:00
|
|
|
if(!window.erpnext) erpnext = {};
|
|
|
|
if(!window.wn) wn = {};
|
2012-12-14 11:09:27 +00:00
|
|
|
|
2013-03-19 05:42:22 +00:00
|
|
|
// Add / update a new Lead / Communication
|
2012-12-14 11:09:27 +00:00
|
|
|
// subject, sender, description
|
|
|
|
erpnext.send_message = function(opts) {
|
2013-07-29 14:00:39 +00:00
|
|
|
return wn.call({
|
2013-03-19 12:29:49 +00:00
|
|
|
type: "POST",
|
|
|
|
method: "website.helpers.contact.send_message",
|
|
|
|
args: opts,
|
|
|
|
callback: opts.callback
|
2013-07-02 06:10:16 +00:00
|
|
|
});
|
2013-03-19 12:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wn.call = function(opts) {
|
2012-12-14 11:09:27 +00:00
|
|
|
if(opts.btn) {
|
2013-08-30 12:52:58 +00:00
|
|
|
$(opts.btn).prop("disabled", true);
|
2012-12-14 11:09:27 +00:00
|
|
|
}
|
2013-03-19 12:29:49 +00:00
|
|
|
|
|
|
|
if(opts.msg) {
|
|
|
|
$(opts.msg).toggle(false);
|
|
|
|
}
|
|
|
|
|
2013-06-19 09:27:14 +00:00
|
|
|
if(!opts.args) opts.args = {};
|
|
|
|
|
2013-03-19 12:29:49 +00:00
|
|
|
// get or post?
|
|
|
|
if(!opts.args._type) {
|
|
|
|
opts.args._type = opts.type || "GET";
|
|
|
|
}
|
|
|
|
|
|
|
|
// method
|
|
|
|
if(opts.method) {
|
|
|
|
opts.args.cmd = opts.method;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringify
|
|
|
|
$.each(opts.args, function(key, val) {
|
|
|
|
if(typeof val != "string") {
|
|
|
|
opts.args[key] = JSON.stringify(val);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-12-14 11:09:27 +00:00
|
|
|
$.ajax({
|
2013-01-31 16:35:39 +00:00
|
|
|
type: "POST",
|
2012-12-14 11:09:27 +00:00
|
|
|
url: "server.py",
|
2013-03-19 12:29:49 +00:00
|
|
|
data: opts.args,
|
2012-12-14 11:09:27 +00:00
|
|
|
dataType: "json",
|
|
|
|
success: function(data) {
|
|
|
|
if(opts.btn) {
|
2013-08-30 12:52:58 +00:00
|
|
|
$(opts.btn).prop("disabled", false);
|
2013-03-19 12:29:49 +00:00
|
|
|
}
|
|
|
|
if(data.exc) {
|
2013-06-19 09:27:14 +00:00
|
|
|
if(opts.btn) {
|
|
|
|
$(opts.btn).addClass("btn-danger");
|
|
|
|
setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
|
|
|
|
}
|
2013-07-04 11:43:53 +00:00
|
|
|
try {
|
|
|
|
var err = JSON.parse(data.exc);
|
|
|
|
if($.isArray(err)) {
|
|
|
|
err = err.join("\n");
|
|
|
|
}
|
|
|
|
console.error ? console.error(err) : console.log(err);
|
|
|
|
} catch(e) {
|
|
|
|
console.log(data.exc);
|
|
|
|
}
|
2013-06-19 09:27:14 +00:00
|
|
|
} else{
|
|
|
|
if(opts.btn) {
|
|
|
|
$(opts.btn).addClass("btn-success");
|
|
|
|
setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
|
|
|
|
}
|
2012-12-14 11:09:27 +00:00
|
|
|
}
|
2013-03-19 12:29:49 +00:00
|
|
|
if(opts.msg && data.message) {
|
|
|
|
$(opts.msg).html(data.message).toggle(true);
|
|
|
|
}
|
|
|
|
if(opts.callback)
|
2012-12-14 11:09:27 +00:00
|
|
|
opts.callback(data);
|
2013-07-04 11:43:53 +00:00
|
|
|
},
|
|
|
|
error: function(response) {
|
|
|
|
console.error ? console.error(response) : console.log(response);
|
2012-12-14 11:09:27 +00:00
|
|
|
}
|
|
|
|
});
|
2013-03-19 12:29:49 +00:00
|
|
|
|
|
|
|
return false;
|
2012-12-14 11:09:27 +00:00
|
|
|
}
|
|
|
|
|
2013-03-19 05:42:22 +00:00
|
|
|
// Setup the user tools
|
|
|
|
//
|
|
|
|
$(document).ready(function() {
|
|
|
|
// update login
|
|
|
|
var full_name = getCookie("full_name");
|
|
|
|
if(full_name) {
|
2013-06-13 05:51:35 +00:00
|
|
|
$("#user-tools").addClass("hide");
|
|
|
|
$("#user-tools-post-login").removeClass("hide");
|
|
|
|
$("#user-full-name").text(full_name);
|
2013-03-19 05:42:22 +00:00
|
|
|
}
|
2013-06-13 05:51:35 +00:00
|
|
|
|
2013-07-10 15:19:44 +00:00
|
|
|
wn.cart.set_cart_count();
|
|
|
|
|
2013-06-13 05:51:35 +00:00
|
|
|
$("#user-tools a").tooltip({"placement":"bottom"});
|
|
|
|
$("#user-tools-post-login a").tooltip({"placement":"bottom"});
|
|
|
|
});
|
2013-03-19 05:42:22 +00:00
|
|
|
|
|
|
|
// Utility functions
|
|
|
|
|
2012-12-14 11:09:27 +00:00
|
|
|
function valid_email(id) {
|
|
|
|
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)
|
|
|
|
return 0; else return 1; }
|
|
|
|
|
2013-01-16 06:04:26 +00:00
|
|
|
var validate_email = valid_email;
|
|
|
|
|
2012-12-14 11:09:27 +00:00
|
|
|
function get_url_arg(name) {
|
|
|
|
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
|
|
|
|
var regexS = "[\\?&]"+name+"=([^&#]*)";
|
|
|
|
var regex = new RegExp( regexS );
|
|
|
|
var results = regex.exec( window.location.href );
|
|
|
|
if(results == null)
|
|
|
|
return "";
|
|
|
|
else
|
|
|
|
return decodeURIComponent(results[1]);
|
2012-12-17 07:22:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 09:27:14 +00:00
|
|
|
function make_query_string(obj) {
|
|
|
|
var query_params = [];
|
|
|
|
$.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
|
|
|
|
return "?" + query_params.join("&");
|
|
|
|
}
|
|
|
|
|
2012-12-17 07:22:43 +00:00
|
|
|
function repl(s, dict) {
|
|
|
|
if(s==null)return '';
|
|
|
|
for(key in dict) {
|
|
|
|
s = s.split("%("+key+")s").join(dict[key]);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2013-03-19 05:42:22 +00:00
|
|
|
|
2013-03-22 07:15:44 +00:00
|
|
|
function replace_all(s, t1, t2) {
|
|
|
|
return s.split(t1).join(t2);
|
|
|
|
}
|
|
|
|
|
2013-03-19 05:42:22 +00:00
|
|
|
function getCookie(name) {
|
2013-03-19 12:29:49 +00:00
|
|
|
return getCookies()[name];
|
2013-03-19 05:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCookies() {
|
2013-03-19 12:29:49 +00:00
|
|
|
var c = document.cookie, v = 0, cookies = {};
|
|
|
|
if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
|
|
|
|
c = RegExp.$1;
|
|
|
|
v = 1;
|
|
|
|
}
|
|
|
|
if (v === 0) {
|
|
|
|
c.split(/[,;]/).map(function(cookie) {
|
|
|
|
var parts = cookie.split(/=/, 2),
|
|
|
|
name = decodeURIComponent(parts[0].trimLeft()),
|
|
|
|
value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
|
2013-03-26 05:15:29 +00:00
|
|
|
if(value && value.charAt(0)==='"') {
|
2013-03-19 12:29:49 +00:00
|
|
|
value = value.substr(1, value.length-2);
|
|
|
|
}
|
|
|
|
cookies[name] = value;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
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) {
|
|
|
|
var name = $0,
|
|
|
|
value = $1.charAt(0) === '"'
|
|
|
|
? $1.substr(1, -1).replace(/\\(.)/g, "$1")
|
|
|
|
: $1;
|
|
|
|
cookies[name] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return cookies;
|
2013-03-19 05:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof String.prototype.trimLeft !== "function") {
|
2013-03-19 12:29:49 +00:00
|
|
|
String.prototype.trimLeft = function() {
|
|
|
|
return this.replace(/^\s+/, "");
|
|
|
|
};
|
2013-03-19 05:42:22 +00:00
|
|
|
}
|
|
|
|
if (typeof String.prototype.trimRight !== "function") {
|
2013-03-19 12:29:49 +00:00
|
|
|
String.prototype.trimRight = function() {
|
|
|
|
return this.replace(/\s+$/, "");
|
|
|
|
};
|
2013-03-19 05:42:22 +00:00
|
|
|
}
|
|
|
|
if (typeof Array.prototype.map !== "function") {
|
2013-03-19 12:29:49 +00:00
|
|
|
Array.prototype.map = function(callback, thisArg) {
|
|
|
|
for (var i=0, n=this.length, a=[]; i<n; i++) {
|
|
|
|
if (i in this) a[i] = callback.call(thisArg, this[i]);
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
};
|
2013-03-19 05:42:22 +00:00
|
|
|
}
|
2013-06-13 05:51:35 +00:00
|
|
|
|
|
|
|
// shopping cart
|
|
|
|
if(!wn.cart) wn.cart = {};
|
2013-06-19 09:27:14 +00:00
|
|
|
var full_name = getCookie("full_name");
|
|
|
|
|
2013-06-13 05:51:35 +00:00
|
|
|
$.extend(wn.cart, {
|
2013-06-19 09:27:14 +00:00
|
|
|
update_cart: function(opts) {
|
|
|
|
if(!full_name) {
|
|
|
|
if(localStorage) {
|
|
|
|
localStorage.setItem("last_visited", window.location.pathname.slice(1));
|
|
|
|
localStorage.setItem("pending_add_to_cart", opts.item_code);
|
|
|
|
}
|
|
|
|
window.location.href = "login";
|
|
|
|
} else {
|
2013-07-29 14:00:39 +00:00
|
|
|
return wn.call({
|
2013-06-19 09:27:14 +00:00
|
|
|
type: "POST",
|
|
|
|
method: "website.helpers.cart.update_cart",
|
|
|
|
args: {
|
|
|
|
item_code: opts.item_code,
|
2013-06-19 11:49:20 +00:00
|
|
|
qty: opts.qty,
|
|
|
|
with_doclist: opts.with_doclist
|
2013-06-19 09:27:14 +00:00
|
|
|
},
|
|
|
|
btn: opts.btn,
|
|
|
|
callback: function(r) {
|
|
|
|
if(opts.callback)
|
|
|
|
opts.callback(r);
|
2013-07-10 15:19:44 +00:00
|
|
|
|
|
|
|
wn.cart.set_cart_count();
|
2013-06-19 09:27:14 +00:00
|
|
|
}
|
|
|
|
});
|
2013-06-13 05:51:35 +00:00
|
|
|
}
|
|
|
|
},
|
2013-07-10 15:19:44 +00:00
|
|
|
|
|
|
|
set_cart_count: function() {
|
|
|
|
var cart_count = getCookie("cart_count");
|
|
|
|
if(cart_count)
|
|
|
|
$(".cart-count").html("( "+ cart_count +" )")
|
|
|
|
}
|
2013-08-30 12:53:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function remove_script_and_style(txt) {
|
|
|
|
return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
|
|
|
|
$("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html();
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_html(txt) {
|
|
|
|
if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
|
|
|
|
&& txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|