-
Notifications
You must be signed in to change notification settings - Fork 71
/
product.js
436 lines (431 loc) · 15.7 KB
/
product.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* Easylife_Switcher extension
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* that is bundled with this package in the file LICENSE_EASYLIFE_SWITCHER.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/mit-license.php
*
* @category Easylife
* @package Easylife_Switcher
* @copyright 2013 - 2014 Marius Strajeru
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
/**
* extend Product.Config class to support image switchers and option labels
*
* @category Easylife
* @package Easylife_Switcher
*/
if(typeof Easylife=='undefined') {
var Easylife = {};
}
Easylife.Switcher = Class.create(Product.Config, {
currentValues: {},
rewritten: false,
/**
* rewrite initialize to transform dorpdowns and support default configuration
* @param $super
* @param config
*/
initialize: function($super, config){
$super(config);
this.rewritten = true;
this.transformDropdowns();
},
/**
* rewrite fillSelect to transform elements to labels
* @param $super
* @param element
*/
fillSelect: function($super, element){
$super(element);
//if (this.config.transform_dropdowns){
var transformed = this.transformDropdown(element);
if (!transformed && !this.getConfigValue(this.config, 'allow_no_stock_select', false)) {
var attributeId = element.id.replace(/[a-z]*/, '');
var options = this.getAttributeOptions(attributeId);
for (var i in options) {
if (options.hasOwnProperty(i)){
var optVal = options[i].id;
var inStock = this.isInStock(attributeId, optVal);
$(element).select('option').each (function(elem){
if ($(elem).value == optVal && !inStock) {
$(elem).disabled="disabled";
}
});
}
}
}
},
/**
* transform dropdowns to labels
*/
transformDropdowns: function(){
for (var i=0; i<this.settings.length;i++){
this.transformDropdown(this.settings[i]);
}
},
/**
* transform one dropdown to labels
* @param selectid
*/
transformDropdown: function(selectid){
var that = this;
var attributeId = $(selectid).id.replace(/[a-z]*/, '');
var transformed = false;
var switchConfig = this.getConfigValue(this.config, 'switch/' + attributeId, false);
if (switchConfig && typeof(switchConfig) == "object") {
transformed = true;
//transform dropdown
var newId = $(selectid).id +'_switchers';
//remove previous labels
if ($(newId)){
$(newId).remove();
}
//hide the select
//actually move it outside the visible area so the validation will still work
$(selectid).setStyle({
left:"-10000px",
position: "absolute"
});
$(selectid).insert({after: '<div class="switcher-field switcher-'+that.config.attributes[attributeId]['code']+'" id="' + newId + '"></div>'});
$(selectid).childElements().each(function(elem, index){
//skip first element "Choose..."
if (index == 0){
return;
}
var optVal = $(elem).value;
var title = $(elem).innerHTML;
var optText = that.getOptionText(elem, optVal, switchConfig);
var inStock = that.isInStock(attributeId, optVal);
var labelClass = that.getLabelClass(elem, attributeId, optVal, inStock);
$(newId).insert('<label class="switcher-label' + labelClass + '" id="' + $(selectid).id + '_' + optVal + '" value="' + optVal + '" title="' + title + '"' + that.getImageSizeHTML() + '>'+optText+'</label>');
//change the select value on click
that.bindClickEvent(selectid, optVal, inStock);
// Make IE 7 & 8 behave like real browsers - damn you IE
if (index == $(selectid).childElements().length - 1){
$(newId).insert('<div style="clear:both"></div>');
}
})
}
return transformed;
},
getImageSizeHTML: function() {
if (!this.config.image_size) {
return '';
}
var sizes = this.config.image_size;
return ' style="width:' + sizes[0] + 'px;height:' + sizes[1] + 'px;line-height:' + sizes[1] + 'px"';
},
/**
* bind click event on labels
*/
bindClickEvent: function(selectid, optVal, inStock) {
var that = this;
if (inStock || this.getConfigValue(this.config, 'allow_no_stock_select', false)){
Event.observe($($(selectid).id + '_' + optVal), 'click', function() {
that.selectValue(this, $(this).readAttribute('value'), selectid);
});
}
},
/**
* get the class name of the labels
*/
getLabelClass: function (elem, attributeId, optVal, inStock) {
var labelClass = '';
if ($(elem).selected){
labelClass = ' selected';
}
//check if the combination is in stock
if (!inStock){
labelClass += ' no-stock';
labelClass += this.getConfigValue(this.config, 'allow_no_stock_select', false) ? ' allow-select' : '';
}
return labelClass;
},
/**
* ge the option text of the label
*/
getOptionText: function(elem, value, config){
var text = $(elem).innerHTML;
var configType = this.getConfigValue(config, 'type', false);
if (!configType) {
return text;
}
switch (config.type) {
case 'custom_images':
var image = this.getConfigValue(config, 'images/' + value, false);
if (image) {
text = '<img src="' + image + '" alt="' + text +'" />';
}
break;
case 'product_images':
//get the images
var imageAttribute = this.getConfigValue(config, 'product_images', '');
var images = this.getConfigValue(this.config.images, imageAttribute, []);
//get first allowed product
var attrId = $(elem).parentNode.id.replace(/[a-z]*/, '');
var options = this.getConfigValue(this.config.attributes, attrId + '/options', false);
var productId = this.getFirstProductId(options, value);
if (productId && (image = this.getConfigValue(images, productId, false))) {
text = '<img src="' + image + '" alt="' + text +'" />';
}
break;
case 'colors':
var color = this.getConfigValue(config, 'color/' + value, false);
if (color) {
text = '<span class="switcher-hexacode" title="' + text + '" style="background-color:' + color +'"></span>'
}
break;
default:
text = this.handleCustomOptionText(text, elem, value, config);
break;
}
return text;
},
/**
* this can be overwritten in a custom class if more transformation types are added.
* by default it returns the current text
* @param text
* @param elem
* @param value
* @param config
* @returns {*}
*/
handleCustomOptionText: function(text, elem, value, config) {
return text;
},
/**
* get the first allowed product
* @param options
* @param value
* @returns {boolean}
*/
getFirstProductId: function(options, value) {
var productId = false;
//get first product in the list
for (var i in options) {
if (options.hasOwnProperty(i) && options[i].id == value) {
productId = this.getConfigValue(options[i], 'products/0', false);
}
}
return productId;
},
/**
* get values from an array or object. Similar to magento's getData for Varien_Object
* @param config
* @param path
* @param def
* @returns {*}
*/
getConfigValue: function(config, path, def) {
var parts = path.split('/');
var cloneConfig = config;
var i = 0;
while (i < parts.length && cloneConfig != -1){
var part = parts[i];
if (typeof cloneConfig[part] != "undefined") {
cloneConfig = cloneConfig[part];
}
else {
cloneConfig = -1;
}
i++;
}
if (cloneConfig == -1) {
return def;
}
return cloneConfig;
},
/**
* select a value when clicking a label
* @param elem
* @param value
* @param selectid
*/
selectValue: function(elem, value, selectid){
if ($(elem)){
$(elem).up(1).select('label').each (function(el){
$(el).removeClassName('selected');
});
$(elem).addClassName('selected');
this.simulateSelect(selectid, $(elem).readAttribute('value'));
}
},
/**
* simulate onchange event on select
* @param selectid
* @param value
*/
simulateSelect: function(selectid, value){
$(selectid).value = value;
$(selectid).simulate('change');
},
/**
* check if a combination is in stock
* @param attributeId
* @param value
*/
isInStock: function(attributeId, value){
var options = this.getConfigValue(this.config, 'attributes/' + attributeId + '/options', []);
var allowedProducts = [];
for ( var j=0; j<options.length;j++){
if (options[j].id == value){
allowedProducts = this.getConfigValue(options[j], 'allowedProducts', []);
break;
}
}
for (var i = 0; i<allowedProducts.length;i++){
var product = allowedProducts[i];
if (this.getConfigValue(this.config, 'stock/' + product, 0) == 1){
return 1;
}
}
return 0;
},
/**
* keep previously selected values
* @param element
*/
keepSelection: function(element) {
if (this.config.keep_values && element.nextSetting) {
var nextSettingId = $(element.nextSetting).id.replace(/[a-z]*/, '');
if (this.currentValues[nextSettingId]) {
$(element.nextSetting).value = this.currentValues[nextSettingId];
if (!$(element.nextSetting).value) {
delete this.currentValues[nextSettingId];
}
var label = $('attribute' + nextSettingId + '_' + this.currentValues[nextSettingId]);
if (label) {
$(label).simulate('click');
}
else {
$(element.nextSetting).simulate('change');
}
}
}
//recalculate current values
this.currentValues = {};
for (var i=0; i<this.settings.length;i++){
if ($(this.settings[i]).value) {
var id = $(this.settings[i]).id.replace(/[a-z]*/, '');
this.currentValues[id] = $(this.settings[i]).value;
}
}
},
/**
* change the main image of the product
* @param product
*/
changeMainImage: function(product) {
var productImage = this.getConfigValue(this.config, 'switch_images/' + product, false);
var image = eval(this.getConfigValue(this.config, 'main_image_selector', false));
if (productImage && image) {
//change the src
$(image).src = productImage['src'];
$(image).alt = productImage['alt'];
$(image).title = productImage['alt'];
//hack for default theme zoomer
//don't call the callback on the first page load
var callback = this.getConfigValue(this.config, 'switch_image_callback', false);
if (this.fullLoad && callback){
//callback - give it 0.1 seconds for the image src to be changed
//a small flicker might occur
//if you don't like it you can remove it at your own risk
eval.delay('0.1', callback)
}
}
},
/**
* change the media block for the product
* @param product
*/
changeMediaBlock: function(product) {
var mediaHtml = this.getConfigValue(this.config, 'switch_media/' + product, false);
var mediaEval = this.getConfigValue(this.config, 'switch_media_selector', false);
var media = mediaEval ? eval(mediaEval) : false;
if (media && mediaHtml){
//hack for default theme zoom-er that doesn't work if called twice.
//if it's not the page load
$(media).innerHTML = mediaHtml;
if (this.fullLoad){
var callback = this.getConfigValue(this.config, 'switch_media_callback', false);
if (callback){
//give it 0.1 seconds to settle in
//a small flicker might occur
//if you don't like it you can remove it at your own risk
eval.delay('0.1', callback)
}
}
}
},
/**
* rewrite configureElement to change the main image or media block
* @param $super
* @param element
*/
configureElement: function($super, element){
$super(element);
var attributeId = $(element).id.replace(/[a-z]*/, '');
var reset = false;
this.keepSelection(element);
var value = $(element).value;
//var options = this.config.attributes[attributeId].options;
//if we should not switch anything stop it here
var switchType = this.getConfigValue(this.config, 'switch_image_type', 0);
if (switchType == 0) {
return ;
}
var setAllowed = false;
var first = [];
var allowed = [];
for (var key in this.currentValues)
{
var options = this.getConfigValue(this.config, 'attributes/' + key + '/options', []);
for (var id in options){
if (options.hasOwnProperty(id) && options[id].id == this.currentValues[key]){
var products = options[id].allowedProducts;
if (!setAllowed) {
allowed = products;
first = allowed;
setAllowed = true;
} else {
var newProducts = [];
for (var i in products) {
if (allowed.indexOf(products[i]) != -1) {
newProducts.push(products[i]);
}
}
allowed = newProducts;
}
}
}
}
var product = allowed[0];
if (switchType == 1) {
this.changeMainImage(product);
}
else if(switchType == 2) {
this.changeMediaBlock(product);
}
},
/**
* rewrite configureForValues to avoid calling the switch callback on first load
* this may not be necessary if the default theme zoomer is not used
* @param $super
*/
configureForValues: function($super){
this.fullLoad = false;
$super();
this.fullLoad = true;
},
getOptionLabel : function($super, option, price) {
if (this.getConfigValue(this.config, 'show_added_prices', false)) {
return $super(option, price);
}
return option.label;
}
});