jquerylistadecompras 1.0.1 → 3.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +149 -133
- package/{src/jquery.listadecompras.css → jquery.listadecompras.css} +329 -329
- package/{src/jquery.listadecompras.js → jquery.listadecompras.js} +461 -461
- package/package.json +18 -12
- package/.wakatime +0 -1
- package/LICENSE +0 -201
|
@@ -1,462 +1,462 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Project: Lista de Compras
|
|
3
|
-
* Description: Get a buy list based on a index and a client key in the Lista de Compras platform
|
|
4
|
-
* Author: Guilherme Branco Stracini
|
|
5
|
-
* License:
|
|
6
|
-
*/
|
|
7
|
-
// the semi-colon before function invocation is a safety net against concatenated
|
|
8
|
-
// scripts and/or other plugins which may not be closed properly.
|
|
9
|
-
;
|
|
10
|
-
(function($, window, document, undefined) {
|
|
11
|
-
|
|
12
|
-
// undefined is used here as the undefined global variable in ECMAScript 3 is
|
|
13
|
-
// mutable (ie. it can be changed by someone else). undefined isn't really being
|
|
14
|
-
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
|
|
15
|
-
// can no longer be modified.
|
|
16
|
-
|
|
17
|
-
// window is passed through as local variable rather than global
|
|
18
|
-
// as this (slightly) quickens the resolution process and can be more efficiently
|
|
19
|
-
// minified (especially when both are regularly referenced in your plugin).
|
|
20
|
-
|
|
21
|
-
var pluginName = "listaDeCompras",
|
|
22
|
-
pluginVersion = 2.1,
|
|
23
|
-
dataPlugin = "plugin_" + pluginName,
|
|
24
|
-
productionEndPoint = "https://api.listadecompras.editorainovacao.com.br/List/",
|
|
25
|
-
homologationEndPoint = "https://api.listadecomprastest.editorainovacao.com.br/List/",
|
|
26
|
-
defaults = {
|
|
27
|
-
useProduction: true,
|
|
28
|
-
readKey: null,
|
|
29
|
-
type: "table",
|
|
30
|
-
showPrices: true,
|
|
31
|
-
listLoadSuccessfullyCallback: null,
|
|
32
|
-
listLoadErrorCallback: null,
|
|
33
|
-
debug: false,
|
|
34
|
-
maxProducts: -1,
|
|
35
|
-
theme: 'default',
|
|
36
|
-
customCampaign: null,
|
|
37
|
-
headerText: null,
|
|
38
|
-
css: {
|
|
39
|
-
itemQuantity: "quantity",
|
|
40
|
-
itemQuantityPlus: "plus",
|
|
41
|
-
itemQuantityLess: "less",
|
|
42
|
-
itemAvailability: "availability",
|
|
43
|
-
itemAvailabilityIcon: "stockIcon",
|
|
44
|
-
buyButton: "buy",
|
|
45
|
-
cartTotal: "amount"
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
function debug(message, lc) {
|
|
50
|
-
if (!lc.options.debug)
|
|
51
|
-
return;
|
|
52
|
-
if (typeof message === "string")
|
|
53
|
-
console.log(message);
|
|
54
|
-
else if (typeof message === "object")
|
|
55
|
-
console.log(JSON.stringify(message));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function processList(lc) {
|
|
59
|
-
var footer;
|
|
60
|
-
var data = lc.privateData.list;
|
|
61
|
-
var id = pluginName + "_l_" + data.ListId + "_s_" + data.StoreId;
|
|
62
|
-
var listLength = data.Products.length;
|
|
63
|
-
if (lc.options.maxProducts != -1)
|
|
64
|
-
listLength = listLength > lc.options.maxProducts ? lc.options.maxProducts : listLength;
|
|
65
|
-
lc.element.empty();
|
|
66
|
-
switch (lc.options.type) {
|
|
67
|
-
case "table":
|
|
68
|
-
var table = $("<table id='" + id + "' width='100%' border='0' cellspacing='0' cellpadding='0'></table>");
|
|
69
|
-
lc.element.append(table);
|
|
70
|
-
var thead = $("<thead>" + (lc.options.headerText != null ? "<tr class='header'><th colspan='" + (lc.options.showPrices ? 5 : 4) + "'>" + lc.options.headerText + "</th></tr>" : "") + "<tr><th width='100'> </th><th>Produto</th>" + (lc.options.showPrices ? "<th>Preço</th>" : "") + "<th></th><th>Quant.</th></tr></thead>")
|
|
71
|
-
table.append(thead);
|
|
72
|
-
var tbody = $("<tbody></tbody>");
|
|
73
|
-
table.append(tbody);
|
|
74
|
-
for (var i = 0; i < listLength; i++) {
|
|
75
|
-
var product = data.Products[i];
|
|
76
|
-
if(product.Stock > 0)
|
|
77
|
-
addToCart(product, lc);
|
|
78
|
-
var tr = $("<tr>");
|
|
79
|
-
tbody.append(tr);
|
|
80
|
-
|
|
81
|
-
var tdPhoto = $("<td>");
|
|
82
|
-
tr.append(tdPhoto);
|
|
83
|
-
tdPhoto.append($("<img />").prop("src", product.PhotoUrl).prop("alt","Foto do produto " + product.Name));
|
|
84
|
-
|
|
85
|
-
var tdName = $("<td>");
|
|
86
|
-
tr.append(tdName);
|
|
87
|
-
tdName.append($("<h6>").text(product.Name + " " + product.Complement));
|
|
88
|
-
|
|
89
|
-
if(lc.options.showPrices)
|
|
90
|
-
{
|
|
91
|
-
var tdPrice = $("<td>");
|
|
92
|
-
tr.append(tdPrice);
|
|
93
|
-
tdPrice.append($("<span>").text(product.Price.toFixed(2).replace(".",",")));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
var tdAvailability = $("<td>");
|
|
97
|
-
tr.append(tdAvailability);
|
|
98
|
-
tdAvailability.append($("<input />").addClass(lc.options.css.itemAvailability).prop("type","checkbox").prop("name", lc.options.css.itemAvailability + "_l_" + data.ListId + "_p_" + product.SalesChannelId).val(product.Stock > 0 ? 1 : 0));
|
|
99
|
-
tdAvailability.append($("<label>").addClass(lc.options.css.itemAvailabilityIcon));
|
|
100
|
-
|
|
101
|
-
var tdCart = $("<td>");
|
|
102
|
-
tr.append(tdCart);
|
|
103
|
-
|
|
104
|
-
var divCart = $("<div>").addClass("cart");
|
|
105
|
-
tdCart.append(divCart);
|
|
106
|
-
divCart.append($("<input />").prop("type","text").prop("id",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).prop("name",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).addClass(lc.options.css.itemQuantity).val(product.Stock > 0 ? product.Quantity : ""));
|
|
107
|
-
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityPlus).text("+"));
|
|
108
|
-
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityLess).text("-"));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
var tfoot = $("<tfoot>");
|
|
112
|
-
table.append(tfoot);
|
|
113
|
-
|
|
114
|
-
var trFoot = $("<tr>");
|
|
115
|
-
tfoot.append(trFoot)
|
|
116
|
-
|
|
117
|
-
var tdInstructionsFoot = $("<td>").prop("colspan", 2);
|
|
118
|
-
trFoot.append(tdInstructionsFoot);
|
|
119
|
-
tdInstructionsFoot.append($("<p>").text("Selecione os produtos que deseja na Lista de Compras e clique em \"Comprar\" para ir até o carrinho."));
|
|
120
|
-
|
|
121
|
-
footer = $("<td>").prop("colspan",3);
|
|
122
|
-
trFoot.append(footer);
|
|
123
|
-
break;
|
|
124
|
-
|
|
125
|
-
case "list":
|
|
126
|
-
if(lc.options.headerText != null)
|
|
127
|
-
lc.element.append($("<div>").addClass("header").html(headerText));
|
|
128
|
-
var ul = $("<ul id='" + id + "></ul>");
|
|
129
|
-
lc.element.append(ul);
|
|
130
|
-
for(var i = 0; i < listLength; i++){
|
|
131
|
-
var product = data.Products[i];
|
|
132
|
-
if(product.Stock > 0)
|
|
133
|
-
addToCart(product, lc);
|
|
134
|
-
|
|
135
|
-
var li = $("<li>");
|
|
136
|
-
ul.append(li);
|
|
137
|
-
|
|
138
|
-
var divPhoto = $("<div>").addClass("photo");
|
|
139
|
-
li.append(divPhoto);
|
|
140
|
-
divPhoto.append($("<img />").prop("src", product.PhotoUrl).prop("alt","Foto do produto " + productName));
|
|
141
|
-
|
|
142
|
-
li.append($("<span>").text(product.Name + " " + product.Complement));
|
|
143
|
-
|
|
144
|
-
if(lc.options.showPrices)
|
|
145
|
-
{
|
|
146
|
-
var divPrice = $("<div>");
|
|
147
|
-
li.append(divPrice);
|
|
148
|
-
liPrice.append($("<span>").text(product.Price.toFixed(2).replace(".",",")));
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
var divStock = $("<div>");
|
|
152
|
-
li.append(divStock);
|
|
153
|
-
divStock.append($("<input />").prop("type","checkbox").addClass(lc.options.css.itemAvaiability).prop("name",lc.options.css.itemAvaiability + "_l_" + data.ListId + "_p_" + product.SalesChannelId).val(product.Stock > 0 ? 1 : 0));
|
|
154
|
-
divStock.append($("<label>").addClass(lc.options.css.itemAvailabilityIcon));
|
|
155
|
-
|
|
156
|
-
var divCart = $("<div>").addClass("cart");
|
|
157
|
-
li.append(divCart);
|
|
158
|
-
divCart.append($("<input />").prop("type","text").prop("id",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).prop("name",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).addClass(lc.options.css.itemQuantity).val(product.Stock > 0 ? product.Quantity : ""));
|
|
159
|
-
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityPlus).text("+"));
|
|
160
|
-
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityLess).text("-"));
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
footer = $("<li>");
|
|
164
|
-
ul.append(footer);
|
|
165
|
-
break;
|
|
166
|
-
|
|
167
|
-
default:
|
|
168
|
-
throw lc.options.type + " is not a valid display mode for jQUery." + pluginName + " plugin";
|
|
169
|
-
}
|
|
170
|
-
var divAmount = $("<div>").addClass(lc.options.css.cartTotal);
|
|
171
|
-
footer.append(divAmount);
|
|
172
|
-
if(lc.options.showPrices)
|
|
173
|
-
divAmount.append($("<span>").html("Total: <b>R$<font>0,00</font></b>"));
|
|
174
|
-
divAmount.append($("<div>").addClass(lc.options.css.buyButton).text("Comprar"));
|
|
175
|
-
|
|
176
|
-
$(lc.element).find("input." + lc.options.css.itemAvailability + "[value=0]").parents(lc.options.type === "table" ? "tr" : "li").find("." + lc.options.css.itemQuantity).prop("disabled", true);
|
|
177
|
-
updateCartTotal(lc);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function onBuyClick(lc) {
|
|
181
|
-
debug("Buy button clicked", lc);
|
|
182
|
-
window.open(lc.privateData.buyLink);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function onLoadList(data, lc) {
|
|
186
|
-
if (typeof data === "undefined")
|
|
187
|
-
return;
|
|
188
|
-
debug("List #" + data.ListId + " loaded sucesfully with " + data.Products.length + " products! Showing in " + lc.options.type + " format with" + (lc.options.showPrices ? "" : "out") + " prices", lc);
|
|
189
|
-
lc.privateData.storeId = data.StoreId;
|
|
190
|
-
lc.privateData.storeUrl = data.StoreUrl;
|
|
191
|
-
lc.privateData.clientMedium = data.ClientMedium;
|
|
192
|
-
lc.privateData.clientCampaign = lc.options.customCampaign == null ? data.ListId : lc.options.customCampaign;
|
|
193
|
-
lc.privateData.list = data;
|
|
194
|
-
clearCart(lc);
|
|
195
|
-
if (lc.options.listLoadSuccessfullyCallback != null && typeof lc.options.listLoadSuccessfullyCallback === "function")
|
|
196
|
-
return lc.options.listLoadSuccessfullyCallback(data, lc);
|
|
197
|
-
processList(lc);
|
|
198
|
-
addEventListeners(lc);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function onLoadError(x, t, e, lc) {
|
|
202
|
-
if (lc.options.listLoadErrorCallback != null && typeof lc.options.listLoadErrorCallback === "function")
|
|
203
|
-
return lc.options.listLoadErrorCallback(x, t, e, lc);
|
|
204
|
-
if(x.status == 404){
|
|
205
|
-
debug("List #" + lc.options.listId + " don't exists!", lc);
|
|
206
|
-
lc.element.data(dataPlugin, null);
|
|
207
|
-
lc.element.remove();
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
debug("A error ocurred when try to load List #" + lc.options.listId + ". Error Code: " + x.status + " | Error Message: " + e, lc);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function addEventListeners(lc){
|
|
214
|
-
debug("Addding event listeners on List #" + lc.privateData.list.ListId, lc);
|
|
215
|
-
$(lc.element).on("change","input." + lc.options.css.itemQuantity, function(){
|
|
216
|
-
var skuId = $(this).prop("id").replace(lc.options.css.itemQuantity + "_l_" + lc.privateData.list.ListId + "_p_", "");
|
|
217
|
-
if(isNaN($(this).val()))
|
|
218
|
-
$(this).val("0");
|
|
219
|
-
updateQuantity(skuId, parseInt($(this).val()), lc);
|
|
220
|
-
});
|
|
221
|
-
$(lc.element).on("click","button." + lc.options.css.itemQuantity, function(){
|
|
222
|
-
var input = $(this).parent().find("input." + lc.options.css.itemQuantity);
|
|
223
|
-
var skuId = input.prop("id").replace(lc.options.css.itemQuantity + "_l_" + lc.privateData.list.ListId + "_p_", "");
|
|
224
|
-
var cartQuantity = parseInt(input.val());
|
|
225
|
-
if($(this).hasClass("plus"))
|
|
226
|
-
cartQuantity++;
|
|
227
|
-
else
|
|
228
|
-
cartQuantity--;
|
|
229
|
-
updateQuantity(skuId, cartQuantity, lc);
|
|
230
|
-
})
|
|
231
|
-
$(lc.element).on("click", "." + lc.options.css.buyButton,function(){
|
|
232
|
-
onBuyClick(lc);
|
|
233
|
-
})
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function removeEventListeners(lc){
|
|
237
|
-
debug("Removing event listeners on List #" + lc.privateData.list.ListId, lc);
|
|
238
|
-
$(lc.element).off("change","input." + lc.options.css.itemQuantity);
|
|
239
|
-
$(lc.element).off("click","button." + lc.options.css.itemQuantity);
|
|
240
|
-
$(lc.element).off("click",".buy" + lc.options.css.buyButton);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function updateCartTotal(lc) {
|
|
244
|
-
debug("Updating cart total and buy link for List #" + lc.privateData.list.ListId, lc);
|
|
245
|
-
var linkEnd = "utm_source=Lista+de+Compras+[Editora+Inovação]&utm_medium=" + encodeURI(lc.privateData.clientMedium) + "&utm_campaign=" + encodeURI(lc.privateData.clientCampaign);
|
|
246
|
-
var total = 0;
|
|
247
|
-
var complement = "";
|
|
248
|
-
lc.privateData.buyLink = lc.privateData.storeUrl + "/checkout/cart/add?";
|
|
249
|
-
for (var i = 0; i < lc.privateData.cart.length; i++){
|
|
250
|
-
total += lc.privateData.cart[i].price * lc.privateData.cart[i].quantity;
|
|
251
|
-
if(lc.privateData.cart[i].quantity == 0)
|
|
252
|
-
continue;
|
|
253
|
-
lc.privateData.buyLink += complement + "sku=" + lc.privateData.cart[i].skuid + "&qty=" + lc.privateData.cart[i].quantity + "&seller=1";
|
|
254
|
-
complement = "&";
|
|
255
|
-
}
|
|
256
|
-
lc.privateData.buyLink += "&redirect=true&sc=" + lc.privateData.storeId + "&" + linkEnd;
|
|
257
|
-
if(total == 0)
|
|
258
|
-
lc.privateData.buyLink = lc.privateData.storeUrl + "?" + linkEnd;
|
|
259
|
-
$("." + lc.options.css.cartTotal + " > span > b > font").text(total.toFixed(2).toString().replace(".",","));
|
|
260
|
-
lc.privateData.cartTotal = total;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function updateQuantity(skuId, quantity, lc) {
|
|
264
|
-
if(quantity < 0)
|
|
265
|
-
quantity = 0;
|
|
266
|
-
var listId = lc.privateData.list.ListId;
|
|
267
|
-
debug("Updating quantity of SKU " + skuId + " to " + quantity + " in List #" + listId, lc)
|
|
268
|
-
var input = $("#quantity_l_" + listId + "_p_" + skuId);
|
|
269
|
-
if(input.length == 0){
|
|
270
|
-
debug("SKU with id " + skuId + " was not found in list " + listId,lc);
|
|
271
|
-
return false;
|
|
272
|
-
}
|
|
273
|
-
input.val(quantity);
|
|
274
|
-
var stockIcon = input.parents("tr").find("." + lc.options.css.itemAvailabilityIcon)
|
|
275
|
-
if(quantity == 0)
|
|
276
|
-
stockIcon.addClass("quantityZero");
|
|
277
|
-
else
|
|
278
|
-
stockIcon.removeClass("quantityZero");
|
|
279
|
-
var any = false;
|
|
280
|
-
for (var x = 0; x < lc.privateData.cart.length; x++) {
|
|
281
|
-
if (lc.privateData.cart[x].skuid == skuId) {
|
|
282
|
-
if(lc.privateData.cart[x].stock <= 0){
|
|
283
|
-
debug("SKU " + skuId + " has no available stock!");
|
|
284
|
-
}
|
|
285
|
-
lc.privateData.cart[x].quantity = quantity;
|
|
286
|
-
any = true;
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
updateCartTotal(lc);
|
|
291
|
-
return any;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
function addToCart(product, lc) {
|
|
295
|
-
debug("Adding product " + product.SalesChannelId + " to cart of List #" + lc.privateData.list.ListId, lc)
|
|
296
|
-
lc.privateData.cart.push({
|
|
297
|
-
skuid: product.SalesChannelId,
|
|
298
|
-
quantity: product.Stock > 0 ? product.Quantity : 0,
|
|
299
|
-
stock: product.Stock,
|
|
300
|
-
price: product.Price
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
function clearCart(lc){
|
|
305
|
-
debug("Clearing cart of List #" + lc.privateData.list.ListId,lc);
|
|
306
|
-
lc.privateData.cart = new Array();
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function ListaDeCompras() {
|
|
310
|
-
this.options = $.extend({}, defaults);
|
|
311
|
-
this.privateData = {};
|
|
312
|
-
this.initialized = false;
|
|
313
|
-
this.loaded = false;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
ListaDeCompras.prototype = {
|
|
317
|
-
init: function(options) {
|
|
318
|
-
if(this.initialized)
|
|
319
|
-
$.error("Plugin " + pluginName + " are already initialized in the following elements: " + this.element.selector);
|
|
320
|
-
debug("Initializing plugin " + pluginName + "[Version:" + pluginVersion + "] in the following elements " + this.element.selector, this);
|
|
321
|
-
$.extend(this.options, options);
|
|
322
|
-
this.initialized = true;
|
|
323
|
-
this.element.addClass(pluginName + " " + this.options.theme + "Theme");
|
|
324
|
-
if (typeof this.options.listId === "number")
|
|
325
|
-
this.load(this.options.listId);
|
|
326
|
-
return this;
|
|
327
|
-
},
|
|
328
|
-
destroy: function(keepList) {
|
|
329
|
-
if(!this.initialized)
|
|
330
|
-
$.error("Plugin " + pluginName + " aren't initialized in the following elements: " + this.element.selector);
|
|
331
|
-
debug("Destroying plugin " + pluginName + " in the following elements: " + this.element.selector, this);
|
|
332
|
-
if(typeof keepList === "undefined" || !keepList){
|
|
333
|
-
removeEventListeners(this);
|
|
334
|
-
this.element.empty();
|
|
335
|
-
this.element.removeClass(pluginName).removeClass(this.options.theme + "Theme");
|
|
336
|
-
}
|
|
337
|
-
this.initialized = false;
|
|
338
|
-
return true;
|
|
339
|
-
},
|
|
340
|
-
load: function(listId) {
|
|
341
|
-
if (!this.initialized)
|
|
342
|
-
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
343
|
-
if (this.options.readKey == null)
|
|
344
|
-
$.error("Configure your access key before call the load method. Use 'init' method in jQuery." + pluginName + " to initialize the plugin or the method 'set' to set an option of the plugin.");
|
|
345
|
-
this.options.listId = listId;
|
|
346
|
-
debug("Loading list " + listId + " in the following elements: " + this.element.selector, this);
|
|
347
|
-
var self = this;
|
|
348
|
-
$.ajax({
|
|
349
|
-
url: (self.options.useProduction ? productionEndPoint : homologationEndPoint) + listId,
|
|
350
|
-
type: "GET",
|
|
351
|
-
dataType: "json",
|
|
352
|
-
beforeSend: function(xhr) {
|
|
353
|
-
xhr.setRequestHeader("X-LISTADECOMPRAS-READ-KEY", self.options.readKey);
|
|
354
|
-
},
|
|
355
|
-
error: function(x, t, e) {
|
|
356
|
-
return onLoadError(x, t, e, self);
|
|
357
|
-
},
|
|
358
|
-
success: function(data){
|
|
359
|
-
self.loaded = true;
|
|
360
|
-
return onLoadList(data,self);
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
return true;
|
|
364
|
-
},
|
|
365
|
-
updateQuantity: function(skuId, quantity) {
|
|
366
|
-
if (!this.initialized)
|
|
367
|
-
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
368
|
-
if(!this.loaded)
|
|
369
|
-
$.error("Load a list before call this method");
|
|
370
|
-
return updateQuantity(skuId, quantity, this);
|
|
371
|
-
},
|
|
372
|
-
addToCart: function(product){
|
|
373
|
-
if(!this.initialized)
|
|
374
|
-
$.error("Initialize the plugin " + pluginName + " before call the addToCart method.");
|
|
375
|
-
if(!this.loaded)
|
|
376
|
-
$.error("Load a list before call this method");
|
|
377
|
-
return addToCart(product, this);
|
|
378
|
-
},
|
|
379
|
-
clearCart: function(){
|
|
380
|
-
if(!this.initialized)
|
|
381
|
-
$.error("Initialize the plugin " + pluginName + " before call the clearCart method.");
|
|
382
|
-
return clearCart(this);
|
|
383
|
-
},
|
|
384
|
-
addEventListeners: function(){
|
|
385
|
-
if(!this.initialized)
|
|
386
|
-
$.error("Initialize the plugin " + pluginName + " before call the addEventListeners method.");
|
|
387
|
-
if(!this.loaded)
|
|
388
|
-
$.error("Load a list before call this method");
|
|
389
|
-
return addEventListeners(this);
|
|
390
|
-
},
|
|
391
|
-
getBuyLink: function(){
|
|
392
|
-
if(!this.initialized)
|
|
393
|
-
$.error("Initialize the plugin " + pluginName + " before call the getBuyLink method.");
|
|
394
|
-
if(!this.loaded)
|
|
395
|
-
$.error("Load a list before call this method");
|
|
396
|
-
debug("Getting current buy link", this);
|
|
397
|
-
return this.privateData.buyLink;
|
|
398
|
-
},
|
|
399
|
-
getCartTotal: function(){
|
|
400
|
-
if(!this.initialized)
|
|
401
|
-
$.error("Initialize the plugin " + pluginName + " before call the getCartTotal method.");
|
|
402
|
-
if(!this.loaded)
|
|
403
|
-
$.error("Load a list before call this method");
|
|
404
|
-
return this.privateData.cartTotal;
|
|
405
|
-
},
|
|
406
|
-
updateCartTotal: function(){
|
|
407
|
-
if(!this.initialized)
|
|
408
|
-
$.error("Initialize the plugin " + pluginName + " before call the updateCartTotal method.");
|
|
409
|
-
if(!this.loaded)
|
|
410
|
-
$.error("Load a list before call this method");
|
|
411
|
-
return updateCartTotal(this);
|
|
412
|
-
},
|
|
413
|
-
set: function(option, value) {
|
|
414
|
-
if (!this.initialized)
|
|
415
|
-
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
416
|
-
debug("Setting " + option + " with value: " + value, this);
|
|
417
|
-
if (option === "theme") {
|
|
418
|
-
this.element.removeClass(this.options.theme + "Theme");
|
|
419
|
-
this.element.addClass(value + "Theme");
|
|
420
|
-
}
|
|
421
|
-
this.options[option] = value;
|
|
422
|
-
if(this.loaded)
|
|
423
|
-
processList(this);
|
|
424
|
-
return true;
|
|
425
|
-
},
|
|
426
|
-
get: function(option) {
|
|
427
|
-
if (!this.initialized)
|
|
428
|
-
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
429
|
-
debug("Getting " + option + " - Current value: " + this.options[option], this);
|
|
430
|
-
return this.options[option];
|
|
431
|
-
},
|
|
432
|
-
debug: function(){
|
|
433
|
-
debug("Debugging plugin", this);
|
|
434
|
-
return console.log(this);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
$.fn[pluginName] = function(arg) {
|
|
439
|
-
var args, instance;
|
|
440
|
-
if(this.length <= 0)
|
|
441
|
-
$.error("No elements with the selector '" + this.selector + "' was found");
|
|
442
|
-
if (!(this.data(dataPlugin) instanceof ListaDeCompras))
|
|
443
|
-
this.data(dataPlugin, new ListaDeCompras());
|
|
444
|
-
|
|
445
|
-
instance = this.data(dataPlugin);
|
|
446
|
-
instance.element = this;
|
|
447
|
-
|
|
448
|
-
if ((typeof arg === "undefined" || typeof arg === "object") && typeof instance["init"] === "function")
|
|
449
|
-
return instance.init(arg);
|
|
450
|
-
else if (typeof arg === "string" && typeof instance[arg] === "function") {
|
|
451
|
-
args = Array.prototype.slice.call(arguments, 1);
|
|
452
|
-
var returnValue = instance[arg].apply(instance, args);
|
|
453
|
-
if(arg !== "destroy")
|
|
454
|
-
return returnValue;
|
|
455
|
-
this.removeData(dataPlugin);
|
|
456
|
-
instance = null;
|
|
457
|
-
return returnValue;
|
|
458
|
-
|
|
459
|
-
} else
|
|
460
|
-
$.error("Method " + arg + " does not exist on jQuery." + pluginName + " plugin");
|
|
461
|
-
};
|
|
1
|
+
/*
|
|
2
|
+
* Project: Lista de Compras
|
|
3
|
+
* Description: Get a buy list based on a index and a client key in the Lista de Compras platform
|
|
4
|
+
* Author: Guilherme Branco Stracini
|
|
5
|
+
* License:
|
|
6
|
+
*/
|
|
7
|
+
// the semi-colon before function invocation is a safety net against concatenated
|
|
8
|
+
// scripts and/or other plugins which may not be closed properly.
|
|
9
|
+
;
|
|
10
|
+
(function($, window, document, undefined) {
|
|
11
|
+
|
|
12
|
+
// undefined is used here as the undefined global variable in ECMAScript 3 is
|
|
13
|
+
// mutable (ie. it can be changed by someone else). undefined isn't really being
|
|
14
|
+
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
|
|
15
|
+
// can no longer be modified.
|
|
16
|
+
|
|
17
|
+
// window is passed through as local variable rather than global
|
|
18
|
+
// as this (slightly) quickens the resolution process and can be more efficiently
|
|
19
|
+
// minified (especially when both are regularly referenced in your plugin).
|
|
20
|
+
|
|
21
|
+
var pluginName = "listaDeCompras",
|
|
22
|
+
pluginVersion = 2.1,
|
|
23
|
+
dataPlugin = "plugin_" + pluginName,
|
|
24
|
+
productionEndPoint = "https://api.listadecompras.editorainovacao.com.br/List/",
|
|
25
|
+
homologationEndPoint = "https://api.listadecomprastest.editorainovacao.com.br/List/",
|
|
26
|
+
defaults = {
|
|
27
|
+
useProduction: true,
|
|
28
|
+
readKey: null,
|
|
29
|
+
type: "table",
|
|
30
|
+
showPrices: true,
|
|
31
|
+
listLoadSuccessfullyCallback: null,
|
|
32
|
+
listLoadErrorCallback: null,
|
|
33
|
+
debug: false,
|
|
34
|
+
maxProducts: -1,
|
|
35
|
+
theme: 'default',
|
|
36
|
+
customCampaign: null,
|
|
37
|
+
headerText: null,
|
|
38
|
+
css: {
|
|
39
|
+
itemQuantity: "quantity",
|
|
40
|
+
itemQuantityPlus: "plus",
|
|
41
|
+
itemQuantityLess: "less",
|
|
42
|
+
itemAvailability: "availability",
|
|
43
|
+
itemAvailabilityIcon: "stockIcon",
|
|
44
|
+
buyButton: "buy",
|
|
45
|
+
cartTotal: "amount"
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function debug(message, lc) {
|
|
50
|
+
if (!lc.options.debug)
|
|
51
|
+
return;
|
|
52
|
+
if (typeof message === "string")
|
|
53
|
+
console.log(message);
|
|
54
|
+
else if (typeof message === "object")
|
|
55
|
+
console.log(JSON.stringify(message));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function processList(lc) {
|
|
59
|
+
var footer;
|
|
60
|
+
var data = lc.privateData.list;
|
|
61
|
+
var id = pluginName + "_l_" + data.ListId + "_s_" + data.StoreId;
|
|
62
|
+
var listLength = data.Products.length;
|
|
63
|
+
if (lc.options.maxProducts != -1)
|
|
64
|
+
listLength = listLength > lc.options.maxProducts ? lc.options.maxProducts : listLength;
|
|
65
|
+
lc.element.empty();
|
|
66
|
+
switch (lc.options.type) {
|
|
67
|
+
case "table":
|
|
68
|
+
var table = $("<table id='" + id + "' width='100%' border='0' cellspacing='0' cellpadding='0'></table>");
|
|
69
|
+
lc.element.append(table);
|
|
70
|
+
var thead = $("<thead>" + (lc.options.headerText != null ? "<tr class='header'><th colspan='" + (lc.options.showPrices ? 5 : 4) + "'>" + lc.options.headerText + "</th></tr>" : "") + "<tr><th width='100'> </th><th>Produto</th>" + (lc.options.showPrices ? "<th>Preço</th>" : "") + "<th></th><th>Quant.</th></tr></thead>")
|
|
71
|
+
table.append(thead);
|
|
72
|
+
var tbody = $("<tbody></tbody>");
|
|
73
|
+
table.append(tbody);
|
|
74
|
+
for (var i = 0; i < listLength; i++) {
|
|
75
|
+
var product = data.Products[i];
|
|
76
|
+
if(product.Stock > 0)
|
|
77
|
+
addToCart(product, lc);
|
|
78
|
+
var tr = $("<tr>");
|
|
79
|
+
tbody.append(tr);
|
|
80
|
+
|
|
81
|
+
var tdPhoto = $("<td>");
|
|
82
|
+
tr.append(tdPhoto);
|
|
83
|
+
tdPhoto.append($("<img />").prop("src", product.PhotoUrl).prop("alt","Foto do produto " + product.Name));
|
|
84
|
+
|
|
85
|
+
var tdName = $("<td>");
|
|
86
|
+
tr.append(tdName);
|
|
87
|
+
tdName.append($("<h6>").text(product.Name + " " + product.Complement));
|
|
88
|
+
|
|
89
|
+
if(lc.options.showPrices)
|
|
90
|
+
{
|
|
91
|
+
var tdPrice = $("<td>");
|
|
92
|
+
tr.append(tdPrice);
|
|
93
|
+
tdPrice.append($("<span>").text(product.Price.toFixed(2).replace(".",",")));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
var tdAvailability = $("<td>");
|
|
97
|
+
tr.append(tdAvailability);
|
|
98
|
+
tdAvailability.append($("<input />").addClass(lc.options.css.itemAvailability).prop("type","checkbox").prop("name", lc.options.css.itemAvailability + "_l_" + data.ListId + "_p_" + product.SalesChannelId).val(product.Stock > 0 ? 1 : 0));
|
|
99
|
+
tdAvailability.append($("<label>").addClass(lc.options.css.itemAvailabilityIcon));
|
|
100
|
+
|
|
101
|
+
var tdCart = $("<td>");
|
|
102
|
+
tr.append(tdCart);
|
|
103
|
+
|
|
104
|
+
var divCart = $("<div>").addClass("cart");
|
|
105
|
+
tdCart.append(divCart);
|
|
106
|
+
divCart.append($("<input />").prop("type","text").prop("id",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).prop("name",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).addClass(lc.options.css.itemQuantity).val(product.Stock > 0 ? product.Quantity : ""));
|
|
107
|
+
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityPlus).text("+"));
|
|
108
|
+
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityLess).text("-"));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
var tfoot = $("<tfoot>");
|
|
112
|
+
table.append(tfoot);
|
|
113
|
+
|
|
114
|
+
var trFoot = $("<tr>");
|
|
115
|
+
tfoot.append(trFoot)
|
|
116
|
+
|
|
117
|
+
var tdInstructionsFoot = $("<td>").prop("colspan", 2);
|
|
118
|
+
trFoot.append(tdInstructionsFoot);
|
|
119
|
+
tdInstructionsFoot.append($("<p>").text("Selecione os produtos que deseja na Lista de Compras e clique em \"Comprar\" para ir até o carrinho."));
|
|
120
|
+
|
|
121
|
+
footer = $("<td>").prop("colspan",3);
|
|
122
|
+
trFoot.append(footer);
|
|
123
|
+
break;
|
|
124
|
+
|
|
125
|
+
case "list":
|
|
126
|
+
if(lc.options.headerText != null)
|
|
127
|
+
lc.element.append($("<div>").addClass("header").html(headerText));
|
|
128
|
+
var ul = $("<ul id='" + id + "></ul>");
|
|
129
|
+
lc.element.append(ul);
|
|
130
|
+
for(var i = 0; i < listLength; i++){
|
|
131
|
+
var product = data.Products[i];
|
|
132
|
+
if(product.Stock > 0)
|
|
133
|
+
addToCart(product, lc);
|
|
134
|
+
|
|
135
|
+
var li = $("<li>");
|
|
136
|
+
ul.append(li);
|
|
137
|
+
|
|
138
|
+
var divPhoto = $("<div>").addClass("photo");
|
|
139
|
+
li.append(divPhoto);
|
|
140
|
+
divPhoto.append($("<img />").prop("src", product.PhotoUrl).prop("alt","Foto do produto " + productName));
|
|
141
|
+
|
|
142
|
+
li.append($("<span>").text(product.Name + " " + product.Complement));
|
|
143
|
+
|
|
144
|
+
if(lc.options.showPrices)
|
|
145
|
+
{
|
|
146
|
+
var divPrice = $("<div>");
|
|
147
|
+
li.append(divPrice);
|
|
148
|
+
liPrice.append($("<span>").text(product.Price.toFixed(2).replace(".",",")));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
var divStock = $("<div>");
|
|
152
|
+
li.append(divStock);
|
|
153
|
+
divStock.append($("<input />").prop("type","checkbox").addClass(lc.options.css.itemAvaiability).prop("name",lc.options.css.itemAvaiability + "_l_" + data.ListId + "_p_" + product.SalesChannelId).val(product.Stock > 0 ? 1 : 0));
|
|
154
|
+
divStock.append($("<label>").addClass(lc.options.css.itemAvailabilityIcon));
|
|
155
|
+
|
|
156
|
+
var divCart = $("<div>").addClass("cart");
|
|
157
|
+
li.append(divCart);
|
|
158
|
+
divCart.append($("<input />").prop("type","text").prop("id",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).prop("name",lc.options.css.itemQuantity + "_l_" + data.ListId + "_p_" + product.SalesChannelId).addClass(lc.options.css.itemQuantity).val(product.Stock > 0 ? product.Quantity : ""));
|
|
159
|
+
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityPlus).text("+"));
|
|
160
|
+
divCart.append($("<button />").prop("type","button").addClass(lc.options.css.itemQuantity + " " + lc.options.css.itemQuantityLess).text("-"));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
footer = $("<li>");
|
|
164
|
+
ul.append(footer);
|
|
165
|
+
break;
|
|
166
|
+
|
|
167
|
+
default:
|
|
168
|
+
throw lc.options.type + " is not a valid display mode for jQUery." + pluginName + " plugin";
|
|
169
|
+
}
|
|
170
|
+
var divAmount = $("<div>").addClass(lc.options.css.cartTotal);
|
|
171
|
+
footer.append(divAmount);
|
|
172
|
+
if(lc.options.showPrices)
|
|
173
|
+
divAmount.append($("<span>").html("Total: <b>R$<font>0,00</font></b>"));
|
|
174
|
+
divAmount.append($("<div>").addClass(lc.options.css.buyButton).text("Comprar"));
|
|
175
|
+
|
|
176
|
+
$(lc.element).find("input." + lc.options.css.itemAvailability + "[value=0]").parents(lc.options.type === "table" ? "tr" : "li").find("." + lc.options.css.itemQuantity).prop("disabled", true);
|
|
177
|
+
updateCartTotal(lc);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function onBuyClick(lc) {
|
|
181
|
+
debug("Buy button clicked", lc);
|
|
182
|
+
window.open(lc.privateData.buyLink);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function onLoadList(data, lc) {
|
|
186
|
+
if (typeof data === "undefined")
|
|
187
|
+
return;
|
|
188
|
+
debug("List #" + data.ListId + " loaded sucesfully with " + data.Products.length + " products! Showing in " + lc.options.type + " format with" + (lc.options.showPrices ? "" : "out") + " prices", lc);
|
|
189
|
+
lc.privateData.storeId = data.StoreId;
|
|
190
|
+
lc.privateData.storeUrl = data.StoreUrl;
|
|
191
|
+
lc.privateData.clientMedium = data.ClientMedium;
|
|
192
|
+
lc.privateData.clientCampaign = lc.options.customCampaign == null ? data.ListId : lc.options.customCampaign;
|
|
193
|
+
lc.privateData.list = data;
|
|
194
|
+
clearCart(lc);
|
|
195
|
+
if (lc.options.listLoadSuccessfullyCallback != null && typeof lc.options.listLoadSuccessfullyCallback === "function")
|
|
196
|
+
return lc.options.listLoadSuccessfullyCallback(data, lc);
|
|
197
|
+
processList(lc);
|
|
198
|
+
addEventListeners(lc);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function onLoadError(x, t, e, lc) {
|
|
202
|
+
if (lc.options.listLoadErrorCallback != null && typeof lc.options.listLoadErrorCallback === "function")
|
|
203
|
+
return lc.options.listLoadErrorCallback(x, t, e, lc);
|
|
204
|
+
if(x.status == 404){
|
|
205
|
+
debug("List #" + lc.options.listId + " don't exists!", lc);
|
|
206
|
+
lc.element.data(dataPlugin, null);
|
|
207
|
+
lc.element.remove();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
debug("A error ocurred when try to load List #" + lc.options.listId + ". Error Code: " + x.status + " | Error Message: " + e, lc);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function addEventListeners(lc){
|
|
214
|
+
debug("Addding event listeners on List #" + lc.privateData.list.ListId, lc);
|
|
215
|
+
$(lc.element).on("change","input." + lc.options.css.itemQuantity, function(){
|
|
216
|
+
var skuId = $(this).prop("id").replace(lc.options.css.itemQuantity + "_l_" + lc.privateData.list.ListId + "_p_", "");
|
|
217
|
+
if(isNaN($(this).val()))
|
|
218
|
+
$(this).val("0");
|
|
219
|
+
updateQuantity(skuId, parseInt($(this).val()), lc);
|
|
220
|
+
});
|
|
221
|
+
$(lc.element).on("click","button." + lc.options.css.itemQuantity, function(){
|
|
222
|
+
var input = $(this).parent().find("input." + lc.options.css.itemQuantity);
|
|
223
|
+
var skuId = input.prop("id").replace(lc.options.css.itemQuantity + "_l_" + lc.privateData.list.ListId + "_p_", "");
|
|
224
|
+
var cartQuantity = parseInt(input.val());
|
|
225
|
+
if($(this).hasClass("plus"))
|
|
226
|
+
cartQuantity++;
|
|
227
|
+
else
|
|
228
|
+
cartQuantity--;
|
|
229
|
+
updateQuantity(skuId, cartQuantity, lc);
|
|
230
|
+
})
|
|
231
|
+
$(lc.element).on("click", "." + lc.options.css.buyButton,function(){
|
|
232
|
+
onBuyClick(lc);
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function removeEventListeners(lc){
|
|
237
|
+
debug("Removing event listeners on List #" + lc.privateData.list.ListId, lc);
|
|
238
|
+
$(lc.element).off("change","input." + lc.options.css.itemQuantity);
|
|
239
|
+
$(lc.element).off("click","button." + lc.options.css.itemQuantity);
|
|
240
|
+
$(lc.element).off("click",".buy" + lc.options.css.buyButton);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function updateCartTotal(lc) {
|
|
244
|
+
debug("Updating cart total and buy link for List #" + lc.privateData.list.ListId, lc);
|
|
245
|
+
var linkEnd = "utm_source=Lista+de+Compras+[Editora+Inovação]&utm_medium=" + encodeURI(lc.privateData.clientMedium) + "&utm_campaign=" + encodeURI(lc.privateData.clientCampaign);
|
|
246
|
+
var total = 0;
|
|
247
|
+
var complement = "";
|
|
248
|
+
lc.privateData.buyLink = lc.privateData.storeUrl + "/checkout/cart/add?";
|
|
249
|
+
for (var i = 0; i < lc.privateData.cart.length; i++){
|
|
250
|
+
total += lc.privateData.cart[i].price * lc.privateData.cart[i].quantity;
|
|
251
|
+
if(lc.privateData.cart[i].quantity == 0)
|
|
252
|
+
continue;
|
|
253
|
+
lc.privateData.buyLink += complement + "sku=" + lc.privateData.cart[i].skuid + "&qty=" + lc.privateData.cart[i].quantity + "&seller=1";
|
|
254
|
+
complement = "&";
|
|
255
|
+
}
|
|
256
|
+
lc.privateData.buyLink += "&redirect=true&sc=" + lc.privateData.storeId + "&" + linkEnd;
|
|
257
|
+
if(total == 0)
|
|
258
|
+
lc.privateData.buyLink = lc.privateData.storeUrl + "?" + linkEnd;
|
|
259
|
+
$("." + lc.options.css.cartTotal + " > span > b > font").text(total.toFixed(2).toString().replace(".",","));
|
|
260
|
+
lc.privateData.cartTotal = total;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function updateQuantity(skuId, quantity, lc) {
|
|
264
|
+
if(quantity < 0)
|
|
265
|
+
quantity = 0;
|
|
266
|
+
var listId = lc.privateData.list.ListId;
|
|
267
|
+
debug("Updating quantity of SKU " + skuId + " to " + quantity + " in List #" + listId, lc)
|
|
268
|
+
var input = $("#quantity_l_" + listId + "_p_" + skuId);
|
|
269
|
+
if(input.length == 0){
|
|
270
|
+
debug("SKU with id " + skuId + " was not found in list " + listId,lc);
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
input.val(quantity);
|
|
274
|
+
var stockIcon = input.parents("tr").find("." + lc.options.css.itemAvailabilityIcon)
|
|
275
|
+
if(quantity == 0)
|
|
276
|
+
stockIcon.addClass("quantityZero");
|
|
277
|
+
else
|
|
278
|
+
stockIcon.removeClass("quantityZero");
|
|
279
|
+
var any = false;
|
|
280
|
+
for (var x = 0; x < lc.privateData.cart.length; x++) {
|
|
281
|
+
if (lc.privateData.cart[x].skuid == skuId) {
|
|
282
|
+
if(lc.privateData.cart[x].stock <= 0){
|
|
283
|
+
debug("SKU " + skuId + " has no available stock!");
|
|
284
|
+
}
|
|
285
|
+
lc.privateData.cart[x].quantity = quantity;
|
|
286
|
+
any = true;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
updateCartTotal(lc);
|
|
291
|
+
return any;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function addToCart(product, lc) {
|
|
295
|
+
debug("Adding product " + product.SalesChannelId + " to cart of List #" + lc.privateData.list.ListId, lc)
|
|
296
|
+
lc.privateData.cart.push({
|
|
297
|
+
skuid: product.SalesChannelId,
|
|
298
|
+
quantity: product.Stock > 0 ? product.Quantity : 0,
|
|
299
|
+
stock: product.Stock,
|
|
300
|
+
price: product.Price
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function clearCart(lc){
|
|
305
|
+
debug("Clearing cart of List #" + lc.privateData.list.ListId,lc);
|
|
306
|
+
lc.privateData.cart = new Array();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function ListaDeCompras() {
|
|
310
|
+
this.options = $.extend({}, defaults);
|
|
311
|
+
this.privateData = {};
|
|
312
|
+
this.initialized = false;
|
|
313
|
+
this.loaded = false;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
ListaDeCompras.prototype = {
|
|
317
|
+
init: function(options) {
|
|
318
|
+
if(this.initialized)
|
|
319
|
+
$.error("Plugin " + pluginName + " are already initialized in the following elements: " + this.element.selector);
|
|
320
|
+
debug("Initializing plugin " + pluginName + "[Version:" + pluginVersion + "] in the following elements " + this.element.selector, this);
|
|
321
|
+
$.extend(this.options, options);
|
|
322
|
+
this.initialized = true;
|
|
323
|
+
this.element.addClass(pluginName + " " + this.options.theme + "Theme");
|
|
324
|
+
if (typeof this.options.listId === "number")
|
|
325
|
+
this.load(this.options.listId);
|
|
326
|
+
return this;
|
|
327
|
+
},
|
|
328
|
+
destroy: function(keepList) {
|
|
329
|
+
if(!this.initialized)
|
|
330
|
+
$.error("Plugin " + pluginName + " aren't initialized in the following elements: " + this.element.selector);
|
|
331
|
+
debug("Destroying plugin " + pluginName + " in the following elements: " + this.element.selector, this);
|
|
332
|
+
if(typeof keepList === "undefined" || !keepList){
|
|
333
|
+
removeEventListeners(this);
|
|
334
|
+
this.element.empty();
|
|
335
|
+
this.element.removeClass(pluginName).removeClass(this.options.theme + "Theme");
|
|
336
|
+
}
|
|
337
|
+
this.initialized = false;
|
|
338
|
+
return true;
|
|
339
|
+
},
|
|
340
|
+
load: function(listId) {
|
|
341
|
+
if (!this.initialized)
|
|
342
|
+
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
343
|
+
if (this.options.readKey == null)
|
|
344
|
+
$.error("Configure your access key before call the load method. Use 'init' method in jQuery." + pluginName + " to initialize the plugin or the method 'set' to set an option of the plugin.");
|
|
345
|
+
this.options.listId = listId;
|
|
346
|
+
debug("Loading list " + listId + " in the following elements: " + this.element.selector, this);
|
|
347
|
+
var self = this;
|
|
348
|
+
$.ajax({
|
|
349
|
+
url: (self.options.useProduction ? productionEndPoint : homologationEndPoint) + listId,
|
|
350
|
+
type: "GET",
|
|
351
|
+
dataType: "json",
|
|
352
|
+
beforeSend: function(xhr) {
|
|
353
|
+
xhr.setRequestHeader("X-LISTADECOMPRAS-READ-KEY", self.options.readKey);
|
|
354
|
+
},
|
|
355
|
+
error: function(x, t, e) {
|
|
356
|
+
return onLoadError(x, t, e, self);
|
|
357
|
+
},
|
|
358
|
+
success: function(data){
|
|
359
|
+
self.loaded = true;
|
|
360
|
+
return onLoadList(data,self);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
return true;
|
|
364
|
+
},
|
|
365
|
+
updateQuantity: function(skuId, quantity) {
|
|
366
|
+
if (!this.initialized)
|
|
367
|
+
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
368
|
+
if(!this.loaded)
|
|
369
|
+
$.error("Load a list before call this method");
|
|
370
|
+
return updateQuantity(skuId, quantity, this);
|
|
371
|
+
},
|
|
372
|
+
addToCart: function(product){
|
|
373
|
+
if(!this.initialized)
|
|
374
|
+
$.error("Initialize the plugin " + pluginName + " before call the addToCart method.");
|
|
375
|
+
if(!this.loaded)
|
|
376
|
+
$.error("Load a list before call this method");
|
|
377
|
+
return addToCart(product, this);
|
|
378
|
+
},
|
|
379
|
+
clearCart: function(){
|
|
380
|
+
if(!this.initialized)
|
|
381
|
+
$.error("Initialize the plugin " + pluginName + " before call the clearCart method.");
|
|
382
|
+
return clearCart(this);
|
|
383
|
+
},
|
|
384
|
+
addEventListeners: function(){
|
|
385
|
+
if(!this.initialized)
|
|
386
|
+
$.error("Initialize the plugin " + pluginName + " before call the addEventListeners method.");
|
|
387
|
+
if(!this.loaded)
|
|
388
|
+
$.error("Load a list before call this method");
|
|
389
|
+
return addEventListeners(this);
|
|
390
|
+
},
|
|
391
|
+
getBuyLink: function(){
|
|
392
|
+
if(!this.initialized)
|
|
393
|
+
$.error("Initialize the plugin " + pluginName + " before call the getBuyLink method.");
|
|
394
|
+
if(!this.loaded)
|
|
395
|
+
$.error("Load a list before call this method");
|
|
396
|
+
debug("Getting current buy link", this);
|
|
397
|
+
return this.privateData.buyLink;
|
|
398
|
+
},
|
|
399
|
+
getCartTotal: function(){
|
|
400
|
+
if(!this.initialized)
|
|
401
|
+
$.error("Initialize the plugin " + pluginName + " before call the getCartTotal method.");
|
|
402
|
+
if(!this.loaded)
|
|
403
|
+
$.error("Load a list before call this method");
|
|
404
|
+
return this.privateData.cartTotal;
|
|
405
|
+
},
|
|
406
|
+
updateCartTotal: function(){
|
|
407
|
+
if(!this.initialized)
|
|
408
|
+
$.error("Initialize the plugin " + pluginName + " before call the updateCartTotal method.");
|
|
409
|
+
if(!this.loaded)
|
|
410
|
+
$.error("Load a list before call this method");
|
|
411
|
+
return updateCartTotal(this);
|
|
412
|
+
},
|
|
413
|
+
set: function(option, value) {
|
|
414
|
+
if (!this.initialized)
|
|
415
|
+
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
416
|
+
debug("Setting " + option + " with value: " + value, this);
|
|
417
|
+
if (option === "theme") {
|
|
418
|
+
this.element.removeClass(this.options.theme + "Theme");
|
|
419
|
+
this.element.addClass(value + "Theme");
|
|
420
|
+
}
|
|
421
|
+
this.options[option] = value;
|
|
422
|
+
if(this.loaded)
|
|
423
|
+
processList(this);
|
|
424
|
+
return true;
|
|
425
|
+
},
|
|
426
|
+
get: function(option) {
|
|
427
|
+
if (!this.initialized)
|
|
428
|
+
$.error("Initialize the plugin " + pluginName + " before call the load method.");
|
|
429
|
+
debug("Getting " + option + " - Current value: " + this.options[option], this);
|
|
430
|
+
return this.options[option];
|
|
431
|
+
},
|
|
432
|
+
debug: function(){
|
|
433
|
+
debug("Debugging plugin", this);
|
|
434
|
+
return console.log(this);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
$.fn[pluginName] = function(arg) {
|
|
439
|
+
var args, instance;
|
|
440
|
+
if(this.length <= 0)
|
|
441
|
+
$.error("No elements with the selector '" + this.selector + "' was found");
|
|
442
|
+
if (!(this.data(dataPlugin) instanceof ListaDeCompras))
|
|
443
|
+
this.data(dataPlugin, new ListaDeCompras());
|
|
444
|
+
|
|
445
|
+
instance = this.data(dataPlugin);
|
|
446
|
+
instance.element = this;
|
|
447
|
+
|
|
448
|
+
if ((typeof arg === "undefined" || typeof arg === "object") && typeof instance["init"] === "function")
|
|
449
|
+
return instance.init(arg);
|
|
450
|
+
else if (typeof arg === "string" && typeof instance[arg] === "function") {
|
|
451
|
+
args = Array.prototype.slice.call(arguments, 1);
|
|
452
|
+
var returnValue = instance[arg].apply(instance, args);
|
|
453
|
+
if(arg !== "destroy")
|
|
454
|
+
return returnValue;
|
|
455
|
+
this.removeData(dataPlugin);
|
|
456
|
+
instance = null;
|
|
457
|
+
return returnValue;
|
|
458
|
+
|
|
459
|
+
} else
|
|
460
|
+
$.error("Method " + arg + " does not exist on jQuery." + pluginName + " plugin");
|
|
461
|
+
};
|
|
462
462
|
}(jQuery, window, document));
|