Spaces:
Running
Running
File size: 3,486 Bytes
b456468 | 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 | import forEach from 'tui-code-snippet/collection/forEach';
import Submenu from '@/ui/submenuBase';
import templateHtml from '@/ui/template/submenu/crop';
import { assignmentForDestroy } from '@/util';
/**
* Crop ui class
* @class
* @ignore
*/
class Crop extends Submenu {
constructor(subMenuElement, { locale, makeSvgIcon, menuBarPosition, usageStatistics }) {
super(subMenuElement, {
locale,
name: 'crop',
makeSvgIcon,
menuBarPosition,
templateHtml,
usageStatistics,
});
this.status = 'active';
this._els = {
apply: this.selector('.tie-crop-button .apply'),
cancel: this.selector('.tie-crop-button .cancel'),
preset: this.selector('.tie-crop-preset-button'),
};
this.defaultPresetButton = this._els.preset.querySelector('.preset-none');
}
/**
* Destroys the instance.
*/
destroy() {
this._removeEvent();
assignmentForDestroy(this);
}
/**
* Add event for crop
* @param {Object} actions - actions for crop
* @param {Function} actions.crop - crop action
* @param {Function} actions.cancel - cancel action
* @param {Function} actions.preset - draw rectzone at a predefined ratio
*/
addEvent(actions) {
const apply = this._applyEventHandler.bind(this);
const cancel = this._cancelEventHandler.bind(this);
const cropzonePreset = this._cropzonePresetEventHandler.bind(this);
this.eventHandler = {
apply,
cancel,
cropzonePreset,
};
this.actions = actions;
this._els.apply.addEventListener('click', apply);
this._els.cancel.addEventListener('click', cancel);
this._els.preset.addEventListener('click', cropzonePreset);
}
/**
* Remove event
* @private
*/
_removeEvent() {
this._els.apply.removeEventListener('click', this.eventHandler.apply);
this._els.cancel.removeEventListener('click', this.eventHandler.cancel);
this._els.preset.removeEventListener('click', this.eventHandler.cropzonePreset);
}
_applyEventHandler() {
this.actions.crop();
this._els.apply.classList.remove('active');
}
_cancelEventHandler() {
this.actions.cancel();
this._els.apply.classList.remove('active');
}
_cropzonePresetEventHandler(event) {
const button = event.target.closest('.tui-image-editor-button.preset');
if (button) {
const [presetType] = button.className.match(/preset-[^\s]+/);
this._setPresetButtonActive(button);
this.actions.preset(presetType);
}
}
/**
* Executed when the menu starts.
*/
changeStartMode() {
this.actions.modeChange('crop');
}
/**
* Returns the menu to its default state.
*/
changeStandbyMode() {
this.actions.stopDrawingMode();
this._setPresetButtonActive();
}
/**
* Change apply button status
* @param {Boolean} enableStatus - apply button status
*/
changeApplyButtonStatus(enableStatus) {
if (enableStatus) {
this._els.apply.classList.add('active');
} else {
this._els.apply.classList.remove('active');
}
}
/**
* Set preset button to active status
* @param {HTMLElement} button - event target element
* @private
*/
_setPresetButtonActive(button = this.defaultPresetButton) {
forEach(this._els.preset.querySelectorAll('.preset'), (presetButton) => {
presetButton.classList.remove('active');
});
if (button) {
button.classList.add('active');
}
}
}
export default Crop;
|