@leafer-in/state 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/state.cjs +288 -61
- package/dist/state.esm.js +289 -63
- package/dist/state.esm.min.js +1 -1
- package/dist/state.js +292 -63
- package/dist/state.min.cjs +1 -1
- package/dist/state.min.js +1 -1
- package/package.json +4 -4
- package/src/check.ts +35 -0
- package/src/decorator.ts +20 -0
- package/src/event.ts +8 -8
- package/src/helper.ts +12 -11
- package/src/index.ts +68 -14
- package/src/set.ts +32 -27
- package/src/style.ts +136 -0
- package/src/unset.ts +19 -30
- package/types/index.d.ts +6 -1
package/dist/state.cjs
CHANGED
|
@@ -1,94 +1,316 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var draw = require('@leafer-ui/draw');
|
|
3
4
|
var core = require('@leafer-ui/core');
|
|
4
5
|
|
|
5
|
-
function
|
|
6
|
-
return
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
function stateType(defaultValue, styleName) {
|
|
7
|
+
return draw.decorateLeafAttr(defaultValue, (key) => draw.attr({
|
|
8
|
+
set(value) {
|
|
9
|
+
this.__setAttr(key, value);
|
|
10
|
+
this.waitLeafer(() => styleName ? draw.State.setStyleName(this, styleName, value) : draw.State.set(this, value));
|
|
11
|
+
}
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
function stateStyleType(defaultValue) {
|
|
15
|
+
return draw.decorateLeafAttr(defaultValue, (key) => draw.attr({
|
|
16
|
+
set(value) {
|
|
17
|
+
this.__setAttr(key, value);
|
|
18
|
+
this.__layout.stateStyleChanged = true;
|
|
19
|
+
}
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function findParentButton(leaf, button) {
|
|
24
|
+
if (button && button !== true)
|
|
25
|
+
return button;
|
|
26
|
+
if (!leaf.button) {
|
|
27
|
+
let { parent } = leaf;
|
|
28
|
+
for (let i = 0; i < 2; i++) {
|
|
29
|
+
if (parent) {
|
|
30
|
+
if (parent.button)
|
|
31
|
+
return parent;
|
|
32
|
+
parent = parent.parent;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
13
35
|
}
|
|
36
|
+
return null;
|
|
14
37
|
}
|
|
15
38
|
|
|
16
|
-
function
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
39
|
+
function setStyle(leaf, style) {
|
|
40
|
+
if (typeof style !== 'object')
|
|
41
|
+
style = undefined;
|
|
42
|
+
updateStyle(leaf, style, 'in');
|
|
43
|
+
}
|
|
44
|
+
function unsetStyle(leaf, style) {
|
|
45
|
+
const { normalStyle } = leaf;
|
|
46
|
+
if (typeof style !== 'object')
|
|
47
|
+
style = undefined;
|
|
48
|
+
if (normalStyle) {
|
|
49
|
+
if (!style)
|
|
50
|
+
style = normalStyle;
|
|
51
|
+
updateStyle(leaf, style, 'out');
|
|
21
52
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
53
|
+
}
|
|
54
|
+
const emprtyStyle = {};
|
|
55
|
+
function updateStyle(leaf, style, type) {
|
|
56
|
+
const { normalStyle } = leaf;
|
|
57
|
+
if (!style)
|
|
58
|
+
style = emprtyStyle;
|
|
59
|
+
if (style.scale) {
|
|
60
|
+
core.MathHelper.assignScale(style, style.scale);
|
|
61
|
+
delete style.scale;
|
|
62
|
+
}
|
|
63
|
+
if (style === emprtyStyle || !core.State.canAnimate)
|
|
64
|
+
type = null;
|
|
65
|
+
let transition = type ? getTransition(type, style, leaf) : false;
|
|
66
|
+
const fromStyle = transition ? getFromStyle(leaf, style) : undefined;
|
|
67
|
+
leaf.killAnimate('transition');
|
|
68
|
+
if (normalStyle)
|
|
69
|
+
leaf.set(normalStyle, true);
|
|
70
|
+
const statesStyle = getStyle(leaf);
|
|
71
|
+
if (statesStyle) {
|
|
72
|
+
const { animation } = statesStyle;
|
|
73
|
+
if (animation) {
|
|
74
|
+
const animate = leaf.animate(animation, undefined, 'animation', true);
|
|
75
|
+
Object.assign(statesStyle, animate.endingStyle);
|
|
76
|
+
if (type !== 'in' || style.animation !== animation)
|
|
77
|
+
animate.kill();
|
|
78
|
+
else
|
|
79
|
+
transition = false;
|
|
80
|
+
delete statesStyle.animation;
|
|
33
81
|
}
|
|
82
|
+
leaf.normalStyle = filterStyle(statesStyle, leaf);
|
|
83
|
+
leaf.set(statesStyle, true);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
leaf.normalStyle = undefined;
|
|
34
87
|
}
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
leaf.
|
|
38
|
-
leaf.
|
|
88
|
+
if (transition) {
|
|
89
|
+
const toStyle = filterStyle(fromStyle, leaf);
|
|
90
|
+
leaf.set(fromStyle, true);
|
|
91
|
+
leaf.animate([fromStyle, toStyle], transition, 'transition', true);
|
|
39
92
|
}
|
|
93
|
+
leaf.__layout.stateStyleChanged = false;
|
|
40
94
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
95
|
+
function getStyle(leaf) {
|
|
96
|
+
let exist;
|
|
97
|
+
const style = {}, { state } = leaf, button = findParentButton(leaf);
|
|
98
|
+
const stateStyle = state && leaf.states[state];
|
|
99
|
+
if (stateStyle && core.State.isState(state, leaf, button))
|
|
100
|
+
exist = assign(style, stateStyle);
|
|
101
|
+
const selectedStyle = style.selectedStyle || leaf.selectedStyle;
|
|
102
|
+
if (selectedStyle && core.State.isSelected(leaf, button))
|
|
103
|
+
exist = assign(style, selectedStyle);
|
|
104
|
+
if (core.State.isDisabled(leaf, button)) {
|
|
105
|
+
const disabledStyle = style.disabledStyle || leaf.disabledStyle;
|
|
106
|
+
if (disabledStyle)
|
|
107
|
+
exist = assign(style, disabledStyle);
|
|
53
108
|
}
|
|
54
109
|
else {
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
110
|
+
const focusStyle = style.focusStyle || leaf.focusStyle;
|
|
111
|
+
if (focusStyle && core.State.isFocus(leaf, button))
|
|
112
|
+
exist = assign(style, focusStyle);
|
|
113
|
+
const hoverStyle = style.hoverStyle || leaf.hoverStyle;
|
|
114
|
+
if (hoverStyle && core.State.isHover(leaf, button))
|
|
115
|
+
exist = assign(style, hoverStyle);
|
|
116
|
+
const pressStyle = style.pressStyle || leaf.pressStyle;
|
|
117
|
+
if (pressStyle && core.State.isPress(leaf, button))
|
|
118
|
+
exist = assign(style, pressStyle);
|
|
119
|
+
}
|
|
120
|
+
return exist ? style : undefined;
|
|
121
|
+
}
|
|
122
|
+
function filterStyle(style, data, addStyle, useAnimateExcludes) {
|
|
123
|
+
const to = addStyle ? style : {}, forStyle = addStyle || style;
|
|
124
|
+
for (let key in forStyle) {
|
|
125
|
+
if (useAnimateExcludes) {
|
|
126
|
+
if (!core.State.animateExcludes[key])
|
|
127
|
+
to[key] = data[key];
|
|
61
128
|
}
|
|
62
|
-
else
|
|
63
|
-
|
|
129
|
+
else
|
|
130
|
+
to[key] = data[key];
|
|
131
|
+
}
|
|
132
|
+
return to;
|
|
133
|
+
}
|
|
134
|
+
function filterAnimateStyle(style, data, addStyle) {
|
|
135
|
+
return filterStyle(style, data, addStyle, true);
|
|
136
|
+
}
|
|
137
|
+
function getFromStyle(leaf, style) {
|
|
138
|
+
const fromStyle = filterAnimateStyle(style, leaf), animate = leaf.animate();
|
|
139
|
+
if (animate)
|
|
140
|
+
filterAnimateStyle(fromStyle, leaf, animate.fromStyle);
|
|
141
|
+
return fromStyle;
|
|
142
|
+
}
|
|
143
|
+
function getTransition(type, style, data) {
|
|
144
|
+
let name = type === 'in' ? 'transition' : 'transitionOut';
|
|
145
|
+
if (type === 'out' && core.isNull(data[name]) && core.isNull(style[name]))
|
|
146
|
+
name = 'transition';
|
|
147
|
+
return core.isNull(style[name]) ? data[name] : style[name];
|
|
148
|
+
}
|
|
149
|
+
function assign(style, stateStyle) {
|
|
150
|
+
Object.assign(style, stateStyle);
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function setPointerState(leaf, stateName) {
|
|
155
|
+
const style = leaf[stateName];
|
|
156
|
+
if (style)
|
|
157
|
+
setStyle(leaf, style);
|
|
158
|
+
if (leaf.button)
|
|
159
|
+
setChildrenState(leaf.children, stateName);
|
|
160
|
+
}
|
|
161
|
+
function setState(leaf, stateName, stateStyle) {
|
|
162
|
+
if (!stateStyle)
|
|
163
|
+
stateStyle = leaf.states[stateName];
|
|
164
|
+
setStyle(leaf, stateStyle);
|
|
165
|
+
if (leaf.button)
|
|
166
|
+
setChildrenState(leaf.children, null, stateName);
|
|
167
|
+
}
|
|
168
|
+
function setChildrenState(children, stateType, state) {
|
|
169
|
+
if (!children)
|
|
170
|
+
return;
|
|
171
|
+
let leaf, update;
|
|
172
|
+
for (let i = 0, len = children.length; i < len; i++) {
|
|
173
|
+
leaf = children[i];
|
|
174
|
+
if (stateType) {
|
|
175
|
+
update = true;
|
|
176
|
+
switch (stateType) {
|
|
177
|
+
case 'hoverStyle':
|
|
178
|
+
if (core.State.isHover(leaf))
|
|
179
|
+
update = false;
|
|
180
|
+
break;
|
|
181
|
+
case 'pressStyle':
|
|
182
|
+
if (core.State.isPress(leaf))
|
|
183
|
+
update = false;
|
|
184
|
+
break;
|
|
185
|
+
case 'focusStyle':
|
|
186
|
+
if (core.State.isFocus(leaf))
|
|
187
|
+
update = false;
|
|
188
|
+
}
|
|
189
|
+
if (update)
|
|
190
|
+
setPointerState(leaf, stateType);
|
|
64
191
|
}
|
|
192
|
+
else if (state)
|
|
193
|
+
setState(leaf, state);
|
|
194
|
+
if (leaf.isBranch)
|
|
195
|
+
setChildrenState(leaf.children, stateType, state);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function unsetPointerState(leaf, stateName) {
|
|
200
|
+
const style = leaf[stateName];
|
|
201
|
+
if (style)
|
|
202
|
+
unsetStyle(leaf, style);
|
|
203
|
+
if (leaf.button)
|
|
204
|
+
unsetChildrenState(leaf.children, stateName);
|
|
205
|
+
}
|
|
206
|
+
function unsetState(leaf, stateName, stateStyle) {
|
|
207
|
+
unsetStyle(leaf, stateStyle);
|
|
208
|
+
if (leaf.button)
|
|
209
|
+
unsetChildrenState(leaf.children, null, stateName);
|
|
210
|
+
}
|
|
211
|
+
function unsetChildrenState(children, stateType, state) {
|
|
212
|
+
if (!children)
|
|
213
|
+
return;
|
|
214
|
+
let leaf;
|
|
215
|
+
for (let i = 0, len = children.length; i < len; i++) {
|
|
216
|
+
leaf = children[i];
|
|
217
|
+
if (stateType)
|
|
218
|
+
unsetPointerState(leaf, stateType);
|
|
219
|
+
else if (state)
|
|
220
|
+
unsetState(leaf, state);
|
|
221
|
+
if (leaf.isBranch)
|
|
222
|
+
unsetChildrenState(leaf.children, stateType, state);
|
|
65
223
|
}
|
|
66
224
|
}
|
|
67
225
|
|
|
68
226
|
function updateEventStyle(leaf, eventType) {
|
|
69
227
|
switch (eventType) {
|
|
70
228
|
case core.PointerEvent.ENTER:
|
|
71
|
-
|
|
229
|
+
setPointerState(leaf, 'hoverStyle');
|
|
72
230
|
break;
|
|
73
231
|
case core.PointerEvent.LEAVE:
|
|
74
|
-
|
|
232
|
+
unsetPointerState(leaf, 'hoverStyle');
|
|
75
233
|
break;
|
|
76
234
|
case core.PointerEvent.DOWN:
|
|
77
|
-
|
|
235
|
+
setPointerState(leaf, 'pressStyle');
|
|
78
236
|
break;
|
|
79
237
|
case core.PointerEvent.UP:
|
|
80
|
-
|
|
238
|
+
unsetPointerState(leaf, 'pressStyle');
|
|
81
239
|
break;
|
|
82
240
|
}
|
|
83
241
|
}
|
|
84
242
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
243
|
+
function checkPointerState(fnName, leaf, button) {
|
|
244
|
+
let find;
|
|
245
|
+
const interaction = leaf.leafer ? leaf.leafer.interaction : null;
|
|
246
|
+
if (interaction) {
|
|
247
|
+
find = interaction[fnName](leaf);
|
|
248
|
+
if (!find && button) {
|
|
249
|
+
const parentButton = findParentButton(leaf, button);
|
|
250
|
+
if (parentButton)
|
|
251
|
+
find = interaction[fnName](parentButton);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return find;
|
|
255
|
+
}
|
|
256
|
+
function checkFixedState(attrName, leaf, button) {
|
|
257
|
+
let find = leaf[attrName];
|
|
258
|
+
if (!find && button) {
|
|
259
|
+
const parentButton = findParentButton(leaf, button);
|
|
260
|
+
if (parentButton)
|
|
261
|
+
find = parentButton[attrName];
|
|
262
|
+
}
|
|
263
|
+
return find;
|
|
264
|
+
}
|
|
265
|
+
function checkState(stateName, leaf, button) {
|
|
266
|
+
let find = leaf.states[stateName];
|
|
267
|
+
if (!find && button) {
|
|
268
|
+
const parentButton = findParentButton(leaf, button);
|
|
269
|
+
if (parentButton)
|
|
270
|
+
find = parentButton.states[stateName];
|
|
271
|
+
}
|
|
272
|
+
return !!find;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
core.State.animateExcludes = {
|
|
276
|
+
animation: 1,
|
|
277
|
+
animationOut: 1,
|
|
278
|
+
transition: 1,
|
|
279
|
+
transitionOut: 1,
|
|
280
|
+
states: 1,
|
|
281
|
+
state: 1,
|
|
282
|
+
normalStyle: 1,
|
|
283
|
+
hoverStyle: 1,
|
|
284
|
+
pressStyle: 1,
|
|
285
|
+
focusStyle: 1,
|
|
286
|
+
selectedStyle: 1,
|
|
287
|
+
disabledStyle: 1
|
|
288
|
+
};
|
|
289
|
+
core.State.isState = function (state, leaf, button) { return checkState(state, leaf, button); };
|
|
290
|
+
core.State.isSelected = function (leaf, button) { return checkFixedState('selected', leaf, button); };
|
|
291
|
+
core.State.isDisabled = function (leaf, button) { return checkFixedState('disabled', leaf, button); };
|
|
292
|
+
core.State.isFocus = function (leaf, button) { return checkPointerState('isFocus', leaf, button); };
|
|
293
|
+
core.State.isHover = function (leaf, button) { return checkPointerState('isHover', leaf, button); };
|
|
294
|
+
core.State.isPress = function (leaf, button) { return checkPointerState('isPress', leaf, button); };
|
|
295
|
+
core.State.isDrag = function (leaf, button) { return checkPointerState('isDrag', leaf, button); };
|
|
296
|
+
core.State.setStyleName = function (leaf, stateType, value) { value ? setState(leaf, stateType, leaf[stateType]) : unsetState(leaf, stateType, leaf[stateType]); };
|
|
297
|
+
core.State.set = function (leaf, stateName) { const style = leaf.states[stateName]; style ? setState(leaf, stateName, style) : unsetState(leaf, stateName, style); };
|
|
298
|
+
core.State.getStyle = getStyle;
|
|
299
|
+
core.State.updateStyle = updateStyle;
|
|
90
300
|
core.State.updateEventStyle = updateEventStyle;
|
|
91
|
-
core.UI.prototype
|
|
301
|
+
const ui = core.UI.prototype;
|
|
302
|
+
stateType(false, 'selectedStyle')(ui, 'selected');
|
|
303
|
+
stateType(false, 'disabledStyle')(ui, 'disabled');
|
|
304
|
+
stateStyleType({})(ui, 'states');
|
|
305
|
+
stateType('')(ui, 'state');
|
|
306
|
+
core.dataType()(ui, 'normalStyle');
|
|
307
|
+
stateStyleType()(ui, 'hoverStyle');
|
|
308
|
+
stateStyleType()(ui, 'pressStyle');
|
|
309
|
+
stateStyleType()(ui, 'focusStyle');
|
|
310
|
+
stateStyleType()(ui, 'selectedStyle');
|
|
311
|
+
stateStyleType()(ui, 'disabledStyle');
|
|
312
|
+
core.dataType(false)(ui, 'button');
|
|
313
|
+
ui.focus = function (value = true) {
|
|
92
314
|
this.waitLeafer(() => {
|
|
93
315
|
let { focusData } = this.app.interaction;
|
|
94
316
|
if (value) {
|
|
@@ -96,10 +318,15 @@ core.UI.prototype.focus = function (value = true) {
|
|
|
96
318
|
focusData.focus(false);
|
|
97
319
|
focusData = this;
|
|
98
320
|
}
|
|
99
|
-
else
|
|
321
|
+
else
|
|
100
322
|
focusData = null;
|
|
101
|
-
}
|
|
102
323
|
this.app.interaction.focusData = focusData;
|
|
103
|
-
|
|
324
|
+
value ? setPointerState(this, 'focusStyle') : unsetPointerState(this, 'focusStyle');
|
|
104
325
|
});
|
|
105
326
|
};
|
|
327
|
+
ui.updateState = function () {
|
|
328
|
+
core.State.updateStyle(this, undefined, 'in');
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
exports.stateStyleType = stateStyleType;
|
|
332
|
+
exports.stateType = stateType;
|