filter.js
14.7 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import snippet from 'tui-code-snippet';
import Colorpicker from './tools/colorpicker';
import Range from './tools/range';
import Submenu from './submenuBase';
import templateHtml from './template/submenu/filter';
import { toInteger, toCamelCase, assignmentForDestroy } from '../util';
import { defaultFilterRangeValus as FILTER_RANGE } from '../consts';
const PICKER_CONTROL_HEIGHT = '130px';
const BLEND_OPTIONS = ['add', 'diff', 'subtract', 'multiply', 'screen', 'lighten', 'darken'];
const FILTER_OPTIONS = [
'grayscale',
'invert',
'sepia',
'vintage',
'blur',
'sharpen',
'emboss',
'remove-white',
'brightness',
'noise',
'pixelate',
'color-filter',
'tint',
'multiply',
'blend',
];
const filterNameMap = {
grayscale: 'grayscale',
invert: 'invert',
sepia: 'sepia',
blur: 'blur',
sharpen: 'sharpen',
emboss: 'emboss',
removeWhite: 'removeColor',
brightness: 'brightness',
contrast: 'contrast',
saturation: 'saturation',
vintage: 'vintage',
polaroid: 'polaroid',
noise: 'noise',
pixelate: 'pixelate',
colorFilter: 'removeColor',
tint: 'blendColor',
multiply: 'blendColor',
blend: 'blendColor',
hue: 'hue',
gamma: 'gamma',
};
const RANGE_INSTANCE_NAMES = [
'removewhiteDistanceRange',
'colorfilterThresholeRange',
'pixelateRange',
'noiseRange',
'brightnessRange',
'tintOpacity',
];
const COLORPICKER_INSTANCE_NAMES = ['filterBlendColor', 'filterMultiplyColor', 'filterTintColor'];
/**
* Filter ui class
* @class
* @ignore
*/
class Filter extends Submenu {
constructor(subMenuElement, { locale, menuBarPosition, usageStatistics }) {
super(subMenuElement, {
locale,
name: 'filter',
menuBarPosition,
templateHtml,
usageStatistics,
});
this.selectBoxShow = false;
this.checkedMap = {};
this._makeControlElement();
}
/**
* Destroys the instance.
*/
destroy() {
this._removeEvent();
this._destroyToolInstance();
assignmentForDestroy(this);
}
/**
* Remove event for filter
*/
_removeEvent() {
snippet.forEach(FILTER_OPTIONS, (filter) => {
const filterCheckElement = this.selector(`.tie-${filter}`);
const filterNameCamelCase = toCamelCase(filter);
filterCheckElement.removeEventListener('change', this.eventHandler[filterNameCamelCase]);
});
snippet.forEach([...RANGE_INSTANCE_NAMES, ...COLORPICKER_INSTANCE_NAMES], (instanceName) => {
this._els[instanceName].off();
});
this._els.blendType.removeEventListener('change', this.eventHandler.changeBlendFilter);
this._els.blendType.removeEventListener('click', this.eventHandler.changeBlendFilter);
}
_destroyToolInstance() {
snippet.forEach([...RANGE_INSTANCE_NAMES, ...COLORPICKER_INSTANCE_NAMES], (instanceName) => {
this._els[instanceName].destroy();
});
}
/**
* Add event for filter
* @param {Object} actions - actions for crop
* @param {Function} actions.applyFilter - apply filter option
*/
addEvent({ applyFilter }) {
const changeFilterState = (filterName) =>
this._changeFilterState.bind(this, applyFilter, filterName);
const changeFilterStateForRange = (filterName) => (value, isLast) =>
this._changeFilterState(applyFilter, filterName, isLast);
this.eventHandler = {
changeBlendFilter: changeFilterState('blend'),
blandTypeClick: (event) => event.stopPropagation(),
};
snippet.forEach(FILTER_OPTIONS, (filter) => {
const filterCheckElement = this.selector(`.tie-${filter}`);
const filterNameCamelCase = toCamelCase(filter);
this.checkedMap[filterNameCamelCase] = filterCheckElement;
this.eventHandler[filterNameCamelCase] = changeFilterState(filterNameCamelCase);
filterCheckElement.addEventListener('change', this.eventHandler[filterNameCamelCase]);
});
this._els.removewhiteDistanceRange.on('change', changeFilterStateForRange('removeWhite'));
this._els.colorfilterThresholeRange.on('change', changeFilterStateForRange('colorFilter'));
this._els.pixelateRange.on('change', changeFilterStateForRange('pixelate'));
this._els.noiseRange.on('change', changeFilterStateForRange('noise'));
this._els.brightnessRange.on('change', changeFilterStateForRange('brightness'));
this._els.filterBlendColor.on('change', this.eventHandler.changeBlendFilter);
this._els.filterMultiplyColor.on('change', changeFilterState('multiply'));
this._els.filterTintColor.on('change', changeFilterState('tint'));
this._els.tintOpacity.on('change', changeFilterStateForRange('tint'));
this._els.filterMultiplyColor.on('changeShow', this.colorPickerChangeShow.bind(this));
this._els.filterTintColor.on('changeShow', this.colorPickerChangeShow.bind(this));
this._els.filterBlendColor.on('changeShow', this.colorPickerChangeShow.bind(this));
this._els.blendType.addEventListener('change', this.eventHandler.changeBlendFilter);
this._els.blendType.addEventListener('click', this.eventHandler.blandTypeClick);
}
/**
* Set filter for undo changed
* @param {Object} chagedFilterInfos - changed command infos
* @param {string} type - filter type
* @param {string} action - add or remove
* @param {Object} options - filter options
*/
setFilterState(chagedFilterInfos) {
const { type, options, action } = chagedFilterInfos;
const filterName = this._getFilterNameFromOptions(type, options);
const isRemove = action === 'remove';
if (!isRemove) {
this._setFilterState(filterName, options);
}
this.checkedMap[filterName].checked = !isRemove;
}
/**
* Set filter for undo changed
* @param {string} filterName - filter name
* @param {Object} options - filter options
* @private
*/
// eslint-disable-next-line complexity
_setFilterState(filterName, options) {
if (filterName === 'colorFilter') {
this._els.colorfilterThresholeRange.value = options.distance;
} else if (filterName === 'removeWhite') {
this._els.removewhiteDistanceRange.value = options.distance;
} else if (filterName === 'pixelate') {
this._els.pixelateRange.value = options.blocksize;
} else if (filterName === 'brightness') {
this._els.brightnessRange.value = options.brightness;
} else if (filterName === 'noise') {
this._els.noiseRange.value = options.noise;
} else if (filterName === 'tint') {
this._els.tintOpacity.value = options.alpha;
this._els.filterTintColor.color = options.color;
} else if (filterName === 'blend') {
this._els.filterBlendColor.color = options.color;
} else if (filterName === 'multiply') {
this._els.filterMultiplyColor.color = options.color;
}
}
/**
* Get filter name
* @param {string} type - filter type
* @param {Object} options - filter options
* @returns {string} filter name
* @private
*/
_getFilterNameFromOptions(type, options) {
let filterName = type;
if (type === 'removeColor') {
filterName = snippet.isExisty(options.useAlpha) ? 'removeWhite' : 'colorFilter';
} else if (type === 'blendColor') {
filterName = {
add: 'blend',
multiply: 'multiply',
tint: 'tint',
}[options.mode];
}
return filterName;
}
/**
* Add event for filter
* @param {Function} applyFilter - actions for firter
* @param {string} filterName - filter name
* @param {boolean} [isLast] - Is last change
*/
_changeFilterState(applyFilter, filterName, isLast = true) {
const apply = this.checkedMap[filterName].checked;
const type = filterNameMap[filterName];
const checkboxGroup = this.checkedMap[filterName].closest('.tui-image-editor-checkbox-group');
if (checkboxGroup) {
if (apply) {
checkboxGroup.classList.remove('tui-image-editor-disabled');
} else {
checkboxGroup.classList.add('tui-image-editor-disabled');
}
}
applyFilter(apply, type, this._getFilterOption(filterName), !isLast);
}
/**
* Get filter option
* @param {String} type - filter type
* @returns {Object} filter option object
* @private
*/
// eslint-disable-next-line complexity
_getFilterOption(type) {
const option = {};
switch (type) {
case 'removeWhite':
option.color = '#FFFFFF';
option.useAlpha = false;
option.distance = parseFloat(this._els.removewhiteDistanceRange.value);
break;
case 'colorFilter':
option.color = '#FFFFFF';
option.distance = parseFloat(this._els.colorfilterThresholeRange.value);
break;
case 'pixelate':
option.blocksize = toInteger(this._els.pixelateRange.value);
break;
case 'noise':
option.noise = toInteger(this._els.noiseRange.value);
break;
case 'brightness':
option.brightness = parseFloat(this._els.brightnessRange.value);
break;
case 'blend':
option.mode = 'add';
option.color = this._els.filterBlendColor.color;
option.mode = this._els.blendType.value;
break;
case 'multiply':
option.mode = 'multiply';
option.color = this._els.filterMultiplyColor.color;
break;
case 'tint':
option.mode = 'tint';
option.color = this._els.filterTintColor.color;
option.alpha = this._els.tintOpacity.value;
break;
case 'blur':
option.blur = this._els.blurRange.value;
break;
default:
break;
}
return option;
}
/**
* Make submenu range and colorpicker control
* @private
*/
_makeControlElement() {
this._els = {
removewhiteDistanceRange: new Range(
{ slider: this.selector('.tie-removewhite-distance-range') },
FILTER_RANGE.removewhiteDistanceRange
),
brightnessRange: new Range(
{ slider: this.selector('.tie-brightness-range') },
FILTER_RANGE.brightnessRange
),
noiseRange: new Range({ slider: this.selector('.tie-noise-range') }, FILTER_RANGE.noiseRange),
pixelateRange: new Range(
{ slider: this.selector('.tie-pixelate-range') },
FILTER_RANGE.pixelateRange
),
colorfilterThresholeRange: new Range(
{ slider: this.selector('.tie-colorfilter-threshole-range') },
FILTER_RANGE.colorfilterThresholeRange
),
filterTintColor: new Colorpicker(
this.selector('.tie-filter-tint-color'),
'#03bd9e',
this.toggleDirection,
this.usageStatistics
),
filterMultiplyColor: new Colorpicker(
this.selector('.tie-filter-multiply-color'),
'#515ce6',
this.toggleDirection,
this.usageStatistics
),
filterBlendColor: new Colorpicker(
this.selector('.tie-filter-blend-color'),
'#ffbb3b',
this.toggleDirection,
this.usageStatistics
),
blurRange: FILTER_RANGE.blurFilterRange,
};
this._els.tintOpacity = this._pickerWithRange(this._els.filterTintColor.pickerControl);
this._els.blendType = this._pickerWithSelectbox(this._els.filterBlendColor.pickerControl);
this.colorPickerControls.push(this._els.filterTintColor);
this.colorPickerControls.push(this._els.filterMultiplyColor);
this.colorPickerControls.push(this._els.filterBlendColor);
}
/**
* Make submenu control for picker & range mixin
* @param {HTMLElement} pickerControl - pickerControl dom element
* @returns {Range}
* @private
*/
_pickerWithRange(pickerControl) {
const rangeWrap = document.createElement('div');
const rangelabel = document.createElement('label');
const slider = document.createElement('div');
slider.id = 'tie-filter-tint-opacity';
rangelabel.innerHTML = 'Opacity';
rangeWrap.appendChild(rangelabel);
rangeWrap.appendChild(slider);
pickerControl.appendChild(rangeWrap);
pickerControl.style.height = PICKER_CONTROL_HEIGHT;
return new Range({ slider }, FILTER_RANGE.tintOpacityRange);
}
/**
* Make submenu control for picker & selectbox
* @param {HTMLElement} pickerControl - pickerControl dom element
* @returns {HTMLElement}
* @private
*/
_pickerWithSelectbox(pickerControl) {
const selectlistWrap = document.createElement('div');
const selectlist = document.createElement('select');
const optionlist = document.createElement('ul');
selectlistWrap.className = 'tui-image-editor-selectlist-wrap';
optionlist.className = 'tui-image-editor-selectlist';
selectlistWrap.appendChild(selectlist);
selectlistWrap.appendChild(optionlist);
this._makeSelectOptionList(selectlist);
pickerControl.appendChild(selectlistWrap);
pickerControl.style.height = PICKER_CONTROL_HEIGHT;
this._drawSelectOptionList(selectlist, optionlist);
this._pickerWithSelectboxForAddEvent(selectlist, optionlist);
return selectlist;
}
/**
* Make selectbox option list custom style
* @param {HTMLElement} selectlist - selectbox element
* @param {HTMLElement} optionlist - custom option list item element
* @private
*/
_drawSelectOptionList(selectlist, optionlist) {
const options = selectlist.querySelectorAll('option');
snippet.forEach(options, (option) => {
const optionElement = document.createElement('li');
optionElement.innerHTML = option.innerHTML;
optionElement.setAttribute('data-item', option.value);
optionlist.appendChild(optionElement);
});
}
/**
* custome selectbox custom event
* @param {HTMLElement} selectlist - selectbox element
* @param {HTMLElement} optionlist - custom option list item element
* @private
*/
_pickerWithSelectboxForAddEvent(selectlist, optionlist) {
optionlist.addEventListener('click', (event) => {
const optionValue = event.target.getAttribute('data-item');
const fireEvent = document.createEvent('HTMLEvents');
selectlist.querySelector(`[value="${optionValue}"]`).selected = true;
fireEvent.initEvent('change', true, true);
selectlist.dispatchEvent(fireEvent);
this.selectBoxShow = false;
optionlist.style.display = 'none';
});
selectlist.addEventListener('mousedown', (event) => {
event.preventDefault();
this.selectBoxShow = !this.selectBoxShow;
optionlist.style.display = this.selectBoxShow ? 'block' : 'none';
optionlist.setAttribute('data-selectitem', selectlist.value);
optionlist.querySelector(`[data-item='${selectlist.value}']`).classList.add('active');
});
}
/**
* Make option list for select control
* @param {HTMLElement} selectlist - blend option select list element
* @private
*/
_makeSelectOptionList(selectlist) {
snippet.forEach(BLEND_OPTIONS, (option) => {
const selectOption = document.createElement('option');
selectOption.setAttribute('value', option);
selectOption.innerHTML = option.replace(/^[a-z]/, ($0) => $0.toUpperCase());
selectlist.appendChild(selectOption);
});
}
}
export default Filter;