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