1
|
|
|
/** |
2
|
|
|
* Image Optimize plugin for Craft CMS |
3
|
|
|
* |
4
|
|
|
* OptimizedImages Field JS |
5
|
|
|
* |
6
|
|
|
* @author nystudio107 |
7
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
8
|
|
|
* @link https://nystudio107.com |
9
|
|
|
* @package ImageOptimize |
10
|
|
|
* @since 1.2.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
;(function ( $, window, document, undefined ) { |
|
|
|
|
14
|
|
|
|
15
|
|
|
var pluginName = "ImageOptimizeOptimizedImages", |
16
|
|
|
defaults = { |
17
|
|
|
}; |
18
|
|
|
|
19
|
|
|
// Plugin constructor |
|
|
|
|
20
|
|
|
function Plugin( element, options ) { |
|
|
|
|
21
|
|
|
this.element = element; |
|
|
|
|
22
|
|
|
|
23
|
|
|
this.options = $.extend( {}, defaults, options) ; |
|
|
|
|
24
|
|
|
|
25
|
|
|
this._defaults = defaults; |
|
|
|
|
26
|
|
|
this._name = pluginName; |
|
|
|
|
27
|
|
|
|
28
|
|
|
this.init(); |
|
|
|
|
29
|
|
|
} |
|
|
|
|
30
|
|
|
|
31
|
|
|
Plugin.prototype = { |
|
|
|
|
32
|
|
|
|
33
|
|
|
init: function(id) { |
|
|
|
|
34
|
|
|
var _this = this; |
|
|
|
|
35
|
|
|
|
36
|
|
|
$(function () { |
|
|
|
|
37
|
|
|
|
38
|
|
|
/* -- _this.options gives us access to the $jsonVars that our FieldType passed down to us */ |
|
|
|
|
39
|
|
|
|
40
|
|
|
}); |
|
|
|
|
41
|
|
|
} |
|
|
|
|
42
|
|
|
}; |
|
|
|
|
43
|
|
|
|
44
|
|
|
// A really lightweight plugin wrapper around the constructor, |
|
|
|
|
45
|
|
|
// preventing against multiple instantiations |
|
|
|
|
46
|
|
|
$.fn[pluginName] = function ( options ) { |
|
|
|
|
47
|
|
|
return this.each(function () { |
|
|
|
|
48
|
|
|
if (!$.data(this, "plugin_" + pluginName)) { |
|
|
|
|
49
|
|
|
$.data(this, "plugin_" + pluginName, |
|
|
|
|
50
|
|
|
new Plugin( this, options )); |
|
|
|
|
51
|
|
|
} |
|
|
|
|
52
|
|
|
}); |
|
|
|
|
53
|
|
|
}; |
|
|
|
|
54
|
|
|
|
55
|
|
|
})( jQuery, window, document ); |
|
|
|
|
56
|
|
|
|
57
|
|
|
Craft.OptimizedImagesInput = Garnish.Base.extend( |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
id: null, |
60
|
|
|
inputNamePrefix: null, |
61
|
|
|
inputIdPrefix: null, |
62
|
|
|
|
63
|
|
|
$container: null, |
64
|
|
|
$blockContainer: null, |
65
|
|
|
$addBlockBtnContainer: null, |
66
|
|
|
$addBlockBtnGroup: null, |
67
|
|
|
$addBlockBtnGroupBtns: null, |
68
|
|
|
|
69
|
|
|
blockSort: null, |
70
|
|
|
blockSelect: null, |
71
|
|
|
|
72
|
|
|
init: function(id, inputNamePrefix) { |
|
|
|
|
73
|
|
|
this.id = id; |
74
|
|
|
this.inputNamePrefix = inputNamePrefix; |
75
|
|
|
this.inputIdPrefix = Craft.formatInputId(this.inputNamePrefix); |
|
|
|
|
76
|
|
|
|
77
|
|
|
this.$container = $('#' + this.id); |
78
|
|
|
this.$blockContainer = this.$container.children('.variant-blocks'); |
79
|
|
|
this.$addBlockBtnContainer = this.$container.children('.buttons'); |
80
|
|
|
this.$addBlockBtnGroup = this.$addBlockBtnContainer.children('.btngroup'); |
81
|
|
|
this.$addBlockBtnGroupBtns = this.$addBlockBtnGroup.children('.btn'); |
82
|
|
|
|
83
|
|
|
// Create our action button menus |
84
|
|
|
var _this = this; |
85
|
|
|
this.$blockContainer.find('> > .actions > .settings').each(function (index, value) { |
|
|
|
|
86
|
|
|
var $value = $(value); |
87
|
|
|
var menuBtn; |
88
|
|
|
if ($value.data('menubtn')) { |
89
|
|
|
menuBtn = $value.data('menubtn'); |
90
|
|
|
} else { |
91
|
|
|
menuBtn = new Garnish.MenuBtn(value); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
menuBtn.menu.settings.onOptionSelect = $.proxy(function(option) { |
|
|
|
|
94
|
|
|
this.onMenuOptionSelect(option, menuBtn); |
95
|
|
|
}, _this); |
|
|
|
|
96
|
|
|
}); |
|
|
|
|
97
|
|
|
|
98
|
|
|
var $blocks = this.$blockContainer.children(); |
99
|
|
|
|
100
|
|
|
this.blockSort = new Garnish.DragSort($blocks, { |
|
|
|
|
101
|
|
|
handle: '> .actions > .move', |
102
|
|
|
axis: 'y', |
103
|
|
|
collapseDraggees: true, |
104
|
|
|
magnetStrength: 4, |
105
|
|
|
helperLagBase: 1.5, |
106
|
|
|
helperOpacity: 0.9, |
107
|
|
|
onSortChange: $.proxy(function() { |
|
|
|
|
108
|
|
|
this.resetVariantBlockOrder(); |
109
|
|
|
}, this) |
|
|
|
|
110
|
|
|
}); |
|
|
|
|
111
|
|
|
|
112
|
|
|
this.addListener(this.$addBlockBtnGroupBtns, 'click', function(ev) { |
|
|
|
|
113
|
|
|
var type = $(ev.target).data('type'); |
|
|
|
|
114
|
|
|
this.addVariantBlock(null); |
115
|
|
|
}); |
|
|
|
|
116
|
|
|
|
117
|
|
|
this.addAspectRatioHandlers(); |
118
|
|
|
this.reIndexVariants(); |
119
|
|
|
}, |
120
|
|
|
|
121
|
|
|
onMenuOptionSelect: function(option, menuBtn) { |
|
|
|
|
122
|
|
|
var $option = $(option); |
123
|
|
|
var container = menuBtn.$btn.closest('.matrixblock'); |
124
|
|
|
|
125
|
|
|
switch ($option.data('action')) { |
126
|
|
|
case 'add': { |
|
|
|
|
127
|
|
|
this.addVariantBlock(container); |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
case 'delete': { |
131
|
|
|
if (!$option.hasClass('disabled')) { |
132
|
|
|
this.deleteVariantBlock(container); |
133
|
|
|
} |
134
|
|
|
break; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
}, |
138
|
|
|
|
139
|
|
|
getHiddenBlockCss: function($block) { |
|
|
|
|
140
|
|
|
return { |
141
|
|
|
opacity: 0, |
142
|
|
|
marginBottom: -($block.outerHeight()) |
143
|
|
|
}; |
144
|
|
|
}, |
145
|
|
|
|
146
|
|
|
// Re-index all of the variant blocks |
147
|
|
|
reIndexVariants: function() { |
|
|
|
|
148
|
|
|
this.$blockContainer = this.$container.children('.variant-blocks'); |
149
|
|
|
var $blocks = this.$blockContainer.children(); |
150
|
|
|
$blocks.each(function (index, value) { |
|
|
|
|
151
|
|
|
var variantIndex = index; |
152
|
|
|
var $value = $(value); |
153
|
|
|
var elements = $value.find('div .field, label, input, select'); |
154
|
|
|
|
155
|
|
|
// Re-index all of the element attributes |
156
|
|
|
$(elements).each(function (index, value) { |
|
|
|
|
157
|
|
|
// id attributes |
158
|
|
|
var str = $(value).attr('id'); |
159
|
|
|
if (str) { |
160
|
|
|
str = str.replace(/\-([0-9]+)\-/g, "-" + variantIndex +"-"); |
161
|
|
|
$(value).attr('id', str); |
162
|
|
|
} |
163
|
|
|
// for attributes |
164
|
|
|
str = $(value).attr('for'); |
165
|
|
|
if (str) { |
166
|
|
|
str = str.replace(/\-([0-9]+)\-/g, "-" + variantIndex +"-"); |
167
|
|
|
$(value).attr('for', str); |
168
|
|
|
} |
169
|
|
|
// Name attributes |
170
|
|
|
str = $(value).attr('name'); |
171
|
|
|
if (str) { |
172
|
|
|
str = str.replace(/\[([0-9]+)\]/g, "[" + variantIndex +"]"); |
173
|
|
|
$(value).attr('name', str); |
174
|
|
|
} |
175
|
|
|
}); |
|
|
|
|
176
|
|
|
}); |
|
|
|
|
177
|
|
|
var disabledDeleteItem = false; |
178
|
|
|
// If we only have one block, don't allow it to be deleted |
179
|
|
|
if ($blocks.length == 1) { |
|
|
|
|
180
|
|
|
disabledDeleteItem = true; |
181
|
|
|
} |
182
|
|
|
$blocks.find('> .actions > .settings').each(function (index, value) { |
|
|
|
|
183
|
|
|
var $value = $(value); |
184
|
|
|
var menuBtn; |
185
|
|
|
if ($value.data('menubtn')) { |
186
|
|
|
menuBtn = $value.data('menubtn'); |
187
|
|
|
$menuItem = $(menuBtn.menu.$menuList[1]); |
|
|
|
|
188
|
|
|
if (disabledDeleteItem) { |
189
|
|
|
$menuItem.find("> li > a").addClass('disabled').disable(); |
190
|
|
|
} else { |
191
|
|
|
$menuItem.find("> li > a").removeClass('disabled').enable(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
}); |
|
|
|
|
195
|
|
|
|
196
|
|
|
}, |
197
|
|
|
|
198
|
|
|
addAspectRatioHandlers: function () { |
199
|
|
|
this.addListener($('.lightswitch'), 'click', function(ev) { |
|
|
|
|
200
|
|
|
var $target = $(ev.target); |
201
|
|
|
var $block = $target.closest('.matrixblock'); |
202
|
|
|
$block.find('.io-aspect-ratio-wrapper').slideToggle(); |
203
|
|
|
}); |
|
|
|
|
204
|
|
|
this.addListener($('.io-select-ar-box'), 'click', function(ev) { |
|
|
|
|
205
|
|
|
var $target = $(ev.target); |
206
|
|
|
var x = $(ev.target).data('x'), |
207
|
|
|
y = $(ev.target).data('y'), |
208
|
|
|
custom = $(ev.target).data('custom'), |
209
|
|
|
field, $block; |
210
|
|
|
// Select the appropriate aspect ratio |
211
|
|
|
$block = $target.closest('.matrixblock'); |
212
|
|
|
$block.find('.io-select-ar-box').each(function (index, value) { |
|
|
|
|
213
|
|
|
$(value).removeClass('io-selected-ar-box'); |
214
|
|
|
}); |
|
|
|
|
215
|
|
|
$target.addClass('io-selected-ar-box'); |
216
|
|
|
|
217
|
|
|
// Handle setting the actual field values |
218
|
|
|
if (custom) { |
219
|
|
|
$block.find('.io-custom-ar-wrapper').slideDown(); |
220
|
|
|
} else { |
221
|
|
|
$block.find('.io-custom-ar-wrapper').slideUp(); |
222
|
|
|
field = $block.find('input')[2]; |
223
|
|
|
$(field).val(x); |
224
|
|
|
field = $block.find('input')[3]; |
225
|
|
|
$(field).val(y); |
226
|
|
|
} |
227
|
|
|
}); |
|
|
|
|
228
|
|
|
}, |
229
|
|
|
|
230
|
|
|
addVariantBlock: function(container) { |
|
|
|
|
231
|
|
|
var _this = this; |
232
|
|
|
$block = $(this.$blockContainer.children()[0]).clone(); |
|
|
|
|
233
|
|
|
// Reset to default values |
234
|
|
|
$block.find('.io-select-ar-box').each(function (index, value) { |
|
|
|
|
235
|
|
|
if (index === 0) { |
236
|
|
|
$(value).addClass('io-selected-ar-box'); |
237
|
|
|
} else { |
238
|
|
|
$(value).removeClass('io-selected-ar-box'); |
239
|
|
|
} |
240
|
|
|
}); |
|
|
|
|
241
|
|
|
$block.find('.io-custom-ar-wrapper').hide(); |
242
|
|
|
field = $block.find('input')[0]; |
|
|
|
|
243
|
|
|
$(field).val(1200); |
244
|
|
|
field = $block.find('input')[1]; |
245
|
|
|
$(field).val(1); |
246
|
|
|
field = $block.find('input')[2]; |
247
|
|
|
$(field).val(16); |
248
|
|
|
field = $block.find('input')[3]; |
249
|
|
|
$(field).val(9); |
250
|
|
|
field = $block.find('select')[0]; |
251
|
|
|
$(field).val(82); |
252
|
|
|
field = $block.find('select')[1]; |
253
|
|
|
$(field).val('jpg'); |
254
|
|
|
$block.css(this.getHiddenBlockCss($block)).velocity({ |
|
|
|
|
255
|
|
|
opacity: 1, |
256
|
|
|
'margin-bottom': 10 |
257
|
|
|
}, 'fast', $.proxy(function() { |
|
|
|
|
258
|
|
|
|
259
|
|
|
// Insert the block in the right place |
260
|
|
|
if (container) { |
261
|
|
|
$block.insertBefore(container); |
262
|
|
|
} else { |
263
|
|
|
$block.appendTo(this.$blockContainer); |
264
|
|
|
} |
265
|
|
|
// Update the Garnish UI controls |
266
|
|
|
this.blockSort.addItems($block); |
267
|
|
|
this.addAspectRatioHandlers(); |
268
|
|
|
$block.find('.settings').each(function (index, value) { |
|
|
|
|
269
|
|
|
var $value = $(value), |
270
|
|
|
menuBtn, |
271
|
|
|
menu; |
272
|
|
|
|
273
|
|
|
menu = _this.$container.find('.io-menu-clone > .menu').clone(); |
274
|
|
|
$(menu).insertAfter($value); |
275
|
|
|
menuBtn = new Garnish.MenuBtn(value); |
|
|
|
|
276
|
|
|
|
277
|
|
|
menuBtn.menu.settings.onOptionSelect = $.proxy(function(option) { |
|
|
|
|
278
|
|
|
_this.onMenuOptionSelect(option, menuBtn); |
279
|
|
|
}, this); |
|
|
|
|
280
|
|
|
}); |
|
|
|
|
281
|
|
|
this.reIndexVariants(); |
282
|
|
|
}, this)); |
|
|
|
|
283
|
|
|
}, |
284
|
|
|
|
285
|
|
|
deleteVariantBlock: function(container) { |
|
|
|
|
286
|
|
|
var _this = this; |
287
|
|
|
container.velocity(this.getHiddenBlockCss(container), 'fast', $.proxy(function() { |
|
|
|
|
288
|
|
|
container.remove(); |
289
|
|
|
_this.reIndexVariants(); |
290
|
|
|
}, this)); |
|
|
|
|
291
|
|
|
}, |
292
|
|
|
|
293
|
|
|
resetVariantBlockOrder: function() { |
|
|
|
|
294
|
|
|
this.reIndexVariants(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
}); |
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.