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 humanFileSize(bytes, si=false, dp=1) { |
|
|
|
|
14
|
|
|
const thresh = si ? 1000 : 1024; |
15
|
|
|
|
16
|
|
|
if (Math.abs(bytes) < thresh) { |
17
|
|
|
return bytes + ' B'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
const units = si |
21
|
|
|
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] |
22
|
|
|
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; |
23
|
|
|
let u = -1; |
24
|
|
|
const r = 10**dp; |
25
|
|
|
|
26
|
|
|
do { |
27
|
|
|
bytes /= thresh; |
28
|
|
|
++u; |
29
|
|
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
return bytes.toFixed(dp) + ' ' + units[u]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
const images = document.querySelectorAll("img.io-prevew-image"); |
36
|
|
|
for (const image of images) { |
37
|
|
|
const url = image.src || image.href; |
38
|
|
|
if (url && url.length > 0) { |
39
|
|
|
const iTime = performance.getEntriesByName(url)[0]; |
|
|
|
|
40
|
|
|
if (iTime !== undefined) { |
41
|
|
|
const elem = image.parentNode.parentNode.parentNode.nextElementSibling.querySelector('.io-file-size'); |
42
|
|
|
if (elem) { |
43
|
|
|
elem.innerHTML = humanFileSize(iTime.decodedBodySize, true); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
;(function ( $, window, document, undefined ) { |
|
|
|
|
50
|
|
|
|
51
|
|
|
var pluginName = "ImageOptimizeOptimizedImages", |
52
|
|
|
defaults = { |
53
|
|
|
}; |
|
|
|
|
54
|
|
|
|
55
|
|
|
// Plugin constructor |
56
|
|
|
function Plugin( element, options ) { |
|
|
|
|
57
|
|
|
this.element = element; |
58
|
|
|
|
59
|
|
|
this.options = $.extend( {}, defaults, options) ; |
|
|
|
|
60
|
|
|
|
61
|
|
|
this._defaults = defaults; |
62
|
|
|
this._name = pluginName; |
63
|
|
|
|
64
|
|
|
this.init(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
Plugin.prototype = { |
68
|
|
|
|
69
|
|
|
init: function(id) { |
|
|
|
|
70
|
|
|
var _this = this; |
|
|
|
|
71
|
|
|
|
72
|
|
|
$(function () { |
|
|
|
|
73
|
|
|
|
74
|
|
|
/* -- _this.options gives us access to the $jsonVars that our FieldType passed down to us */ |
|
|
|
|
75
|
|
|
|
76
|
|
|
}); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
}; |
79
|
|
|
|
80
|
|
|
// A really lightweight plugin wrapper around the constructor, |
81
|
|
|
// preventing against multiple instantiations |
82
|
|
|
$.fn[pluginName] = function ( options ) { |
83
|
|
|
return this.each(function () { |
|
|
|
|
84
|
|
|
if (!$.data(this, "plugin_" + pluginName)) { |
85
|
|
|
$.data(this, "plugin_" + pluginName, |
|
|
|
|
86
|
|
|
new Plugin( this, options )); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
}); |
|
|
|
|
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
})( jQuery, window, document ); |
|
|
|
|
92
|
|
|
|
93
|
|
|
Craft.OptimizedImagesInput = Garnish.Base.extend( |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
id: null, |
96
|
|
|
inputNamePrefix: null, |
97
|
|
|
inputIdPrefix: null, |
98
|
|
|
|
99
|
|
|
$container: null, |
100
|
|
|
$blockContainer: null, |
101
|
|
|
$addBlockBtnContainer: null, |
102
|
|
|
$addBlockBtnGroup: null, |
103
|
|
|
$addBlockBtnGroupBtns: null, |
104
|
|
|
|
105
|
|
|
blockSort: null, |
106
|
|
|
blockSelect: null, |
107
|
|
|
|
108
|
|
|
init: function(id, inputNamePrefix, sizesWrapperId) { |
|
|
|
|
109
|
|
|
|
110
|
|
|
this.id = id; |
111
|
|
|
this.inputNamePrefix = inputNamePrefix; |
112
|
|
|
this.inputIdPrefix = Craft.formatInputId(this.inputNamePrefix); |
|
|
|
|
113
|
|
|
|
114
|
|
|
this.$container = $('#' + this.id); |
115
|
|
|
this.$blockContainer = this.$container.children('.variant-blocks'); |
116
|
|
|
this.$addBlockBtnContainer = this.$container.children('.buttons'); |
117
|
|
|
this.$addBlockBtnGroup = this.$addBlockBtnContainer.children('.btngroup'); |
118
|
|
|
this.$addBlockBtnGroupBtns = this.$addBlockBtnGroup.children('.btn'); |
119
|
|
|
|
120
|
|
|
// Create our action button menus |
121
|
|
|
var _this = this; |
122
|
|
|
this.$blockContainer.find('> > .actions > .settings').each(function (index, value) { |
|
|
|
|
123
|
|
|
var $value = $(value); |
124
|
|
|
var menuBtn; |
125
|
|
|
if ($value.data('menubtn')) { |
126
|
|
|
menuBtn = $value.data('menubtn'); |
127
|
|
|
} else { |
128
|
|
|
menuBtn = new Garnish.MenuBtn(value); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
menuBtn.menu.settings.onOptionSelect = $.proxy(function(option) { |
|
|
|
|
131
|
|
|
this.onMenuOptionSelect(option, menuBtn); |
132
|
|
|
}, _this); |
|
|
|
|
133
|
|
|
}); |
|
|
|
|
134
|
|
|
|
135
|
|
|
var $blocks = this.$blockContainer.children(); |
136
|
|
|
|
137
|
|
|
this.blockSort = new Garnish.DragSort($blocks, { |
|
|
|
|
138
|
|
|
handle: '> .actions > .move', |
139
|
|
|
axis: 'y', |
140
|
|
|
collapseDraggees: true, |
141
|
|
|
magnetStrength: 4, |
142
|
|
|
helperLagBase: 1.5, |
143
|
|
|
helperOpacity: 0.9, |
144
|
|
|
onSortChange: $.proxy(function() { |
|
|
|
|
145
|
|
|
this.resetVariantBlockOrder(); |
146
|
|
|
}, this) |
|
|
|
|
147
|
|
|
}); |
|
|
|
|
148
|
|
|
|
149
|
|
|
this.addListener(this.$addBlockBtnGroupBtns, 'click', function(ev) { |
|
|
|
|
150
|
|
|
var type = $(ev.target).data('type'); |
|
|
|
|
151
|
|
|
this.addVariantBlock(null); |
152
|
|
|
}); |
|
|
|
|
153
|
|
|
|
154
|
|
|
this.addAspectRatioHandlers(); |
155
|
|
|
this.reIndexVariants(); |
156
|
|
|
}, |
157
|
|
|
|
158
|
|
|
onMenuOptionSelect: function(option, menuBtn) { |
|
|
|
|
159
|
|
|
var $option = $(option); |
160
|
|
|
var container = menuBtn.$btn.closest('.matrixblock'); |
161
|
|
|
|
162
|
|
|
switch ($option.data('action')) { |
163
|
|
|
case 'add': { |
|
|
|
|
164
|
|
|
this.addVariantBlock(container); |
165
|
|
|
break; |
166
|
|
|
} |
167
|
|
|
case 'delete': { |
168
|
|
|
if (!$option.hasClass('disabled')) { |
169
|
|
|
this.deleteVariantBlock(container); |
170
|
|
|
} |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
}, |
175
|
|
|
|
176
|
|
|
getHiddenBlockCss: function($block) { |
|
|
|
|
177
|
|
|
return { |
178
|
|
|
opacity: 0, |
179
|
|
|
marginBottom: -($block.outerHeight()) |
180
|
|
|
}; |
181
|
|
|
}, |
182
|
|
|
|
183
|
|
|
// Re-index all of the variant blocks |
184
|
|
|
reIndexVariants: function() { |
|
|
|
|
185
|
|
|
this.$blockContainer = this.$container.children('.variant-blocks'); |
186
|
|
|
var $blocks = this.$blockContainer.children(); |
187
|
|
|
$blocks.each(function (index, value) { |
|
|
|
|
188
|
|
|
var variantIndex = index; |
189
|
|
|
var $value = $(value); |
190
|
|
|
var elements = $value.find('div .field, label, input, select'); |
191
|
|
|
|
192
|
|
|
// Re-index all of the element attributes |
193
|
|
|
$(elements).each(function (index, value) { |
|
|
|
|
194
|
|
|
// id attributes |
195
|
|
|
var str = $(value).attr('id'); |
196
|
|
|
if (str) { |
197
|
|
|
str = str.replace(/\-([0-9]+)\-/g, "-" + variantIndex +"-"); |
198
|
|
|
$(value).attr('id', str); |
199
|
|
|
} |
200
|
|
|
// for attributes |
201
|
|
|
str = $(value).attr('for'); |
202
|
|
|
if (str) { |
203
|
|
|
str = str.replace(/\-([0-9]+)\-/g, "-" + variantIndex +"-"); |
204
|
|
|
$(value).attr('for', str); |
205
|
|
|
} |
206
|
|
|
// Name attributes |
207
|
|
|
str = $(value).attr('name'); |
208
|
|
|
if (str) { |
209
|
|
|
str = str.replace(/\[([0-9]+)\]/g, "[" + variantIndex +"]"); |
210
|
|
|
$(value).attr('name', str); |
211
|
|
|
} |
212
|
|
|
}); |
|
|
|
|
213
|
|
|
}); |
|
|
|
|
214
|
|
|
var disabledDeleteItem = false; |
215
|
|
|
// If we only have one block, don't allow it to be deleted |
216
|
|
|
if ($blocks.length == 1) { |
|
|
|
|
217
|
|
|
disabledDeleteItem = true; |
218
|
|
|
} |
219
|
|
|
$blocks.find('> .actions > .settings').each(function (index, value) { |
|
|
|
|
220
|
|
|
var $value = $(value); |
221
|
|
|
var menuBtn; |
222
|
|
|
if ($value.data('menubtn')) { |
223
|
|
|
menuBtn = $value.data('menubtn'); |
224
|
|
|
$menuItem = $(menuBtn.menu.$menuList[1]); |
|
|
|
|
225
|
|
|
if (disabledDeleteItem) { |
226
|
|
|
$menuItem.find("> li > a").addClass('disabled').disable(); |
227
|
|
|
} else { |
228
|
|
|
$menuItem.find("> li > a").removeClass('disabled').enable(); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
}); |
|
|
|
|
232
|
|
|
|
233
|
|
|
}, |
234
|
|
|
|
235
|
|
|
addAspectRatioHandlers: function () { |
236
|
|
|
this.addListener($('.lightswitch'), 'click', function(ev) { |
|
|
|
|
237
|
|
|
var $target = $(ev.target); |
238
|
|
|
var $block = $target.closest('.matrixblock'); |
239
|
|
|
$block.find('.io-aspect-ratio-wrapper').slideToggle(); |
240
|
|
|
}); |
|
|
|
|
241
|
|
|
this.addListener($('.io-select-ar-box'), 'click', function(ev) { |
|
|
|
|
242
|
|
|
var $target = $(ev.target); |
243
|
|
|
var x = $(ev.target).data('x'), |
244
|
|
|
y = $(ev.target).data('y'), |
245
|
|
|
custom = $(ev.target).data('custom'), |
246
|
|
|
field, $block; |
247
|
|
|
// Select the appropriate aspect ratio |
248
|
|
|
$block = $target.closest('.matrixblock'); |
249
|
|
|
$block.find('.io-select-ar-box').each(function (index, value) { |
|
|
|
|
250
|
|
|
$(value).removeClass('io-selected-ar-box'); |
251
|
|
|
}); |
|
|
|
|
252
|
|
|
$target.addClass('io-selected-ar-box'); |
253
|
|
|
|
254
|
|
|
// Handle setting the actual field values |
255
|
|
|
if (custom) { |
256
|
|
|
$block.find('.io-custom-ar-wrapper').slideDown(); |
257
|
|
|
} else { |
258
|
|
|
$block.find('.io-custom-ar-wrapper').slideUp(); |
259
|
|
|
field = $block.find('input')[2]; |
260
|
|
|
$(field).val(x); |
261
|
|
|
field = $block.find('input')[3]; |
262
|
|
|
$(field).val(y); |
263
|
|
|
} |
264
|
|
|
}); |
|
|
|
|
265
|
|
|
}, |
266
|
|
|
|
267
|
|
|
addVariantBlock: function(container) { |
|
|
|
|
268
|
|
|
var _this = this; |
269
|
|
|
$block = $(this.$blockContainer.children()[0]).clone(); |
|
|
|
|
270
|
|
|
// Reset to default values |
271
|
|
|
$block.find('.io-select-ar-box').each(function (index, value) { |
|
|
|
|
272
|
|
|
if (index === 0) { |
273
|
|
|
$(value).addClass('io-selected-ar-box'); |
274
|
|
|
} else { |
275
|
|
|
$(value).removeClass('io-selected-ar-box'); |
276
|
|
|
} |
277
|
|
|
}); |
|
|
|
|
278
|
|
|
$block.find('.io-custom-ar-wrapper').hide(); |
279
|
|
|
field = $block.find('input')[0]; |
|
|
|
|
280
|
|
|
$(field).val(1200); |
281
|
|
|
field = $block.find('input')[1]; |
282
|
|
|
$(field).val(1); |
283
|
|
|
field = $block.find('input')[2]; |
284
|
|
|
$(field).val(16); |
285
|
|
|
field = $block.find('input')[3]; |
286
|
|
|
$(field).val(9); |
287
|
|
|
field = $block.find('select')[0]; |
288
|
|
|
$(field).val(82); |
289
|
|
|
field = $block.find('select')[1]; |
290
|
|
|
$(field).val('jpg'); |
291
|
|
|
$block.css(this.getHiddenBlockCss($block)).velocity({ |
|
|
|
|
292
|
|
|
opacity: 1, |
293
|
|
|
'margin-bottom': 10 |
294
|
|
|
}, 'fast', $.proxy(function() { |
|
|
|
|
295
|
|
|
|
296
|
|
|
// Insert the block in the right place |
297
|
|
|
if (container) { |
298
|
|
|
$block.insertBefore(container); |
299
|
|
|
} else { |
300
|
|
|
$block.appendTo(this.$blockContainer); |
301
|
|
|
} |
302
|
|
|
// Update the Garnish UI controls |
303
|
|
|
this.blockSort.addItems($block); |
304
|
|
|
this.addAspectRatioHandlers(); |
305
|
|
|
$block.find('.settings').each(function (index, value) { |
|
|
|
|
306
|
|
|
var $value = $(value), |
307
|
|
|
menuBtn, |
308
|
|
|
menu; |
309
|
|
|
|
310
|
|
|
menu = _this.$container.find('.io-menu-clone > .menu').clone(); |
311
|
|
|
$(menu).insertAfter($value); |
312
|
|
|
menuBtn = new Garnish.MenuBtn(value); |
|
|
|
|
313
|
|
|
|
314
|
|
|
menuBtn.menu.settings.onOptionSelect = $.proxy(function(option) { |
|
|
|
|
315
|
|
|
_this.onMenuOptionSelect(option, menuBtn); |
316
|
|
|
}, this); |
|
|
|
|
317
|
|
|
}); |
|
|
|
|
318
|
|
|
this.reIndexVariants(); |
319
|
|
|
}, this)); |
|
|
|
|
320
|
|
|
}, |
321
|
|
|
|
322
|
|
|
deleteVariantBlock: function(container) { |
|
|
|
|
323
|
|
|
var _this = this; |
324
|
|
|
container.velocity(this.getHiddenBlockCss(container), 'fast', $.proxy(function() { |
|
|
|
|
325
|
|
|
container.remove(); |
326
|
|
|
_this.reIndexVariants(); |
327
|
|
|
}, this)); |
|
|
|
|
328
|
|
|
}, |
329
|
|
|
|
330
|
|
|
resetVariantBlockOrder: function() { |
|
|
|
|
331
|
|
|
this.reIndexVariants(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
}); |
|
|
|
|
335
|
|
|
|