/
mcf.productimages.js
178 lines (145 loc) · 5.03 KB
/
mcf.productimages.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
/*!
* File: mcf.productimage.js
* Part of MyCashFlow's default theme.
* Please don't edit this file unless necessary!
*/
/*jshint browser: true, jquery: true, laxbreak: true, curly: false, expr: true */
$(function() {
'use strict';
// Minimum variation image
// filter length.
var filterLengthLimit = 4;
var $productCurrentImage = $('#CurrentProductImage'),
$productThumbnails = $('#ProductThumbnails'),
$productBuyForm = $('.BuyForm');
// Create the spinning image loader.
var $imageLoader = $('<span id="ImgLoader">' + mcf.Lang.Loading + '</span>');
$imageLoader.prependTo($productCurrentImage);
$imageLoader.hide();
// Change the preview image when a thumbnail is clicked.
$productThumbnails.on('click', 'li a', function(evt, triggered) {
var $self = $(this);
var $oldImage = $('img', $productCurrentImage),
$newImage = $('img', $self),
imageText = $.trim($newImage.attr('alt')),
imageSize = $oldImage.attr('src').split('/')[2];
// Show the image loader.
$imageLoader.fadeIn(150);
// Hide the product image.
$oldImage.fadeOut(300, function() {
// Hide the image loader and
// show the new product image.
$imageLoader.fadeOut(300, function() {
$oldImage.attr('alt', $newImage.attr('alt'));
$oldImage.attr('src', $newImage.attr('src').replace(/[0-9]+x[0-9]+/, imageSize));
$oldImage.fadeIn(300);
});
});
$productCurrentImage
.attr('title', $(this).attr('title'))
.attr('href', $(this).attr('href'))
.data('eq', $(this).parent().index())
.next('#ProductImageCaption').text($self.attr('title'));
if (!triggered && imageText.length >= filterLengthLimit) {
var $bestMatch = null;
var $inputs = $('option, :radio', $productBuyForm).sort(function(a, b) {
var labelA = $(a).is(':radio')
? $(a).parent('label').text()
: $(a).find(':selected').text();
var labelB = $(b).is(':radio')
? $(b).parent('label').text()
: $(b).find(':selected').text();
return labelA.length > labelB.length;
});
$inputs.each(function() {
var inputValue = $(this).is(':radio')
? $.trim($(this).parent('label').text())
: $.trim($(this).text());
var priceRegex = /\, .?\d+\W\d+.+$/;
inputValue = inputValue.replace(priceRegex, '');
var matchA = inputValue.toLowerCase(),
matchB = imageText.toLowerCase(),
matches = matchA.length > matchB.length
? matchA.indexOf(matchB)
: matchB.indexOf(matchA);
if (matchA === matchB) {
$bestMatch = $(this);
return false;
}
if (matches > -1) {
$bestMatch = $(this);
}
});
if ($bestMatch) {
var $parent = $bestMatch.closest('.FormItem');
if ($bestMatch.is(':radio')) {
$(':radio', $parent).attr('checked', false);
$bestMatch.attr('checked', true);
} else if ($parent.hasClass('BuyFormQuantity') === false) {
$('option', $parent).attr('selected', false);
$bestMatch.attr('selected', true).parent("select").trigger("change", [true]);
}
}
}
evt.preventDefault();
});
// Change the preview image when a different product
// variant is selected. Matching is done by comparing
// the variant's name against the image titles.
$productBuyForm.on('change', function (evt, triggered) {
if (!triggered) {
var $changedEl = $(evt.target),
$bestMatch = null,
$parent = $changedEl.closest('.FormItem'),
inputValue = $changedEl.is(':radio')
? $.trim($changedEl.parent('label').text())
: $.trim($changedEl.find(':selected').text());
if (inputValue.length >= filterLengthLimit) {
var $thumbnails = $('li a', $productThumbnails).sort(function(a, b) {
var labelA = $(a).attr('title'),
labelB = $(b).attr('title');
return labelA.length > labelB.length;
});
$thumbnails.each(function() {
var matchA = $.trim($(this).attr('title').toLowerCase()),
matchB = inputValue.toLowerCase(),
matches = matchA.length > matchB.length
? matchA.indexOf(matchB)
: matchB.indexOf(matchA);
if (matchA === matchB) {
$bestMatch = $(this);
return false;
}
if (matches > -1) {
$bestMatch = $(this);
}
});
if ($bestMatch && $parent.hasClass('BuyFormQuantity') === false) {
$bestMatch.trigger('click', [true]);
}
}
}
});
// Open the current preview image into a modal window
// when clicked.
$productCurrentImage.on('click', function(evt) {
if ($productThumbnails.length) {
var $self = $(this);
// Get the index of the currently active image.
var index = $self.data('eq')
? $self.data('eq')
: 0;
// Initialize the ColorBox for product images.
$('li a', $productThumbnails).colorbox($.extend({}, { open: false }, mcf.colorboxOpts));
// Open the currently active product image.
$('li:eq(' + index + ') a', $productThumbnails).colorbox($.extend({}, { open: true }, mcf.colorboxOpts));
} else {
$(this).colorbox($.extend({}, { open: true }, mcf.colorboxOpts));
}
evt.preventDefault();
});
// Prevent Colorbox from opening when clicking thumbs.
$productThumbnails.on('click', function(evt) {
return false;
});
});