Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 1 | |
| 2 | var erpnext = {}; |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 3 | var wn = {}; |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 5 | // Add / update a new Lead / Communication |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 6 | // subject, sender, description |
| 7 | erpnext.send_message = function(opts) { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 8 | wn.call({ |
| 9 | type: "POST", |
| 10 | method: "website.helpers.contact.send_message", |
| 11 | args: opts, |
| 12 | callback: opts.callback |
| 13 | }) |
| 14 | } |
| 15 | |
| 16 | wn.call = function(opts) { |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 17 | if(opts.btn) { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 18 | var $spinner = $('<img src="lib/images/ui/button-load.gif">').appendTo($(opts.btn).parent()) |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 19 | $(opts.btn).attr("disabled", "disabled"); |
| 20 | } |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 21 | |
| 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 Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 43 | $.ajax({ |
Rushabh Mehta | fdc629b | 2013-01-31 22:05:39 +0530 | [diff] [blame] | 44 | type: "POST", |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 45 | url: "server.py", |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 46 | data: opts.args, |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 47 | dataType: "json", |
| 48 | success: function(data) { |
| 49 | if(opts.btn) { |
| 50 | $(opts.btn).attr("disabled", false); |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 51 | $spinner.remove(); |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 52 | } |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 53 | 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 Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 60 | opts.callback(data); |
| 61 | } |
| 62 | }); |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 63 | |
| 64 | return false; |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 65 | } |
| 66 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 67 | // Setup the user tools |
| 68 | // |
| 69 | $(document).ready(function() { |
| 70 | // update login |
| 71 | var full_name = getCookie("full_name"); |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 72 | if(full_name) { |
Rushabh Mehta | bed19ac | 2013-03-21 17:12:25 +0530 | [diff] [blame] | 73 | $("#user-tools").html(repl('<a href="profile" title="My Profile" id="user-full-name">%(full_name)s</a> | \ |
| 74 | <a href="account" title="My Account">My Account</a> | \ |
Rushabh Mehta | da512ba | 2013-03-22 12:45:44 +0530 | [diff] [blame] | 75 | <!--<a href="cart" title="Shopping Cart"><i class="icon-shopping-cart"></i> (%(count)s)</a> | -->\ |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 76 | <a href="server.py?cmd=web_logout" title="Sign Out"><i class="icon-signout"></i></a>', { |
| 77 | full_name: full_name, |
| 78 | count: getCookie("cart_count") || "0" |
| 79 | })); |
| 80 | $("#user-tools a").tooltip({"placement":"bottom"}); |
| 81 | } |
| 82 | }) |
| 83 | |
| 84 | // Utility functions |
| 85 | |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 86 | function valid_email(id) { |
| 87 | 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) |
| 88 | return 0; else return 1; } |
| 89 | |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 90 | var validate_email = valid_email; |
| 91 | |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 92 | function get_url_arg(name) { |
| 93 | name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); |
| 94 | var regexS = "[\\?&]"+name+"=([^&#]*)"; |
| 95 | var regex = new RegExp( regexS ); |
| 96 | var results = regex.exec( window.location.href ); |
| 97 | if(results == null) |
| 98 | return ""; |
| 99 | else |
| 100 | return decodeURIComponent(results[1]); |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | function repl(s, dict) { |
| 104 | if(s==null)return ''; |
| 105 | for(key in dict) { |
| 106 | s = s.split("%("+key+")s").join(dict[key]); |
| 107 | } |
| 108 | return s; |
| 109 | } |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 110 | |
Rushabh Mehta | da512ba | 2013-03-22 12:45:44 +0530 | [diff] [blame] | 111 | function replace_all(s, t1, t2) { |
| 112 | return s.split(t1).join(t2); |
| 113 | } |
| 114 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 115 | function getCookie(name) { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 116 | return getCookies()[name]; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | function getCookies() { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 120 | var c = document.cookie, v = 0, cookies = {}; |
| 121 | if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) { |
| 122 | c = RegExp.$1; |
| 123 | v = 1; |
| 124 | } |
| 125 | if (v === 0) { |
| 126 | c.split(/[,;]/).map(function(cookie) { |
| 127 | var parts = cookie.split(/=/, 2), |
| 128 | name = decodeURIComponent(parts[0].trimLeft()), |
| 129 | value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null; |
Nabin Hait | 6dcee5a | 2013-03-26 10:45:29 +0530 | [diff] [blame] | 130 | if(value && value.charAt(0)==='"') { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 131 | value = value.substr(1, value.length-2); |
| 132 | } |
| 133 | cookies[name] = value; |
| 134 | }); |
| 135 | } else { |
| 136 | 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) { |
| 137 | var name = $0, |
| 138 | value = $1.charAt(0) === '"' |
| 139 | ? $1.substr(1, -1).replace(/\\(.)/g, "$1") |
| 140 | : $1; |
| 141 | cookies[name] = value; |
| 142 | }); |
| 143 | } |
| 144 | return cookies; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if (typeof String.prototype.trimLeft !== "function") { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 148 | String.prototype.trimLeft = function() { |
| 149 | return this.replace(/^\s+/, ""); |
| 150 | }; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 151 | } |
| 152 | if (typeof String.prototype.trimRight !== "function") { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 153 | String.prototype.trimRight = function() { |
| 154 | return this.replace(/\s+$/, ""); |
| 155 | }; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 156 | } |
| 157 | if (typeof Array.prototype.map !== "function") { |
Rushabh Mehta | a75efa7 | 2013-03-19 17:59:49 +0530 | [diff] [blame] | 158 | Array.prototype.map = function(callback, thisArg) { |
| 159 | for (var i=0, n=this.length, a=[]; i<n; i++) { |
| 160 | if (i in this) a[i] = callback.call(thisArg, this[i]); |
| 161 | } |
| 162 | return a; |
| 163 | }; |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 164 | } |