rotate.js
3.06 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
import Range from './tools/range';
import Submenu from './submenuBase';
import templateHtml from './template/submenu/rotate';
import { toInteger, assignmentForDestroy } from '../util';
import { defaultRotateRangeValus } from '../consts';
const CLOCKWISE = 30;
const COUNTERCLOCKWISE = -30;
/**
* Rotate ui class
* @class
* @ignore
*/
class Rotate extends Submenu {
constructor(subMenuElement, { locale, makeSvgIcon, menuBarPosition, usageStatistics }) {
super(subMenuElement, {
locale,
name: 'rotate',
makeSvgIcon,
menuBarPosition,
templateHtml,
usageStatistics,
});
this._value = 0;
this._els = {
rotateButton: this.selector('.tie-retate-button'),
rotateRange: new Range(
{
slider: this.selector('.tie-rotate-range'),
input: this.selector('.tie-ratate-range-value'),
},
defaultRotateRangeValus
),
};
}
/**
* Destroys the instance.
*/
destroy() {
this._removeEvent();
this._els.rotateRange.destroy();
assignmentForDestroy(this);
}
setRangeBarAngle(type, angle) {
let resultAngle = angle;
if (type === 'rotate') {
resultAngle = parseInt(this._els.rotateRange.value, 10) + angle;
}
this._setRangeBarRatio(resultAngle);
}
_setRangeBarRatio(angle) {
this._els.rotateRange.value = angle;
}
/**
* Add event for rotate
* @param {Object} actions - actions for crop
* @param {Function} actions.rotate - rotate action
* @param {Function} actions.setAngle - set angle action
*/
addEvent(actions) {
this.eventHandler.rotationAngleChanged = this._changeRotateForButton.bind(this);
// {rotate, setAngle}
this.actions = actions;
this._els.rotateButton.addEventListener('click', this.eventHandler.rotationAngleChanged);
this._els.rotateRange.on('change', this._changeRotateForRange.bind(this));
}
/**
* Remove event
* @private
*/
_removeEvent() {
this._els.rotateButton.removeEventListener('click', this.eventHandler.rotationAngleChanged);
this._els.rotateRange.off();
}
/**
* Change rotate for range
* @param {number} value - angle value
* @param {boolean} isLast - Is last change
* @private
*/
_changeRotateForRange(value, isLast) {
const angle = toInteger(value);
this.actions.setAngle(angle, !isLast);
this._value = angle;
}
/**
* Change rotate for button
* @param {object} event - add button event object
* @private
*/
_changeRotateForButton(event) {
const button = event.target.closest('.tui-image-editor-button');
const angle = this._els.rotateRange.value;
if (button) {
const rotateType = this.getButtonType(button, ['counterclockwise', 'clockwise']);
const rotateAngle = {
clockwise: CLOCKWISE,
counterclockwise: COUNTERCLOCKWISE,
}[rotateType];
const newAngle = parseInt(angle, 10) + rotateAngle;
const isRotatable = newAngle >= -360 && newAngle <= 360;
if (isRotatable) {
this.actions.rotate(rotateAngle);
}
}
}
}
export default Rotate;