@leafer-in/state 1.0.2 → 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 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 hasFixedState(leaf) {
6
- return leaf.selected || leaf.disabled || core.State.isFocus(leaf);
7
- }
8
- function restoreStyle(leaf) {
9
- const style = leaf.__.normalStyle;
10
- if (style) {
11
- leaf.set(style);
12
- leaf.__.normalStyle = undefined;
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 setStateStyle(leaf, stateType, pointerState) {
17
- let style;
18
- const data = leaf.__;
19
- if (pointerState) {
20
- style = !hasFixedState(leaf) && data[stateType];
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
- else {
23
- switch (stateType) {
24
- case 'disabledStyle':
25
- style = data[stateType];
26
- break;
27
- case 'focusStyle':
28
- style = !leaf.disabled && data[stateType];
29
- break;
30
- case 'selectedStyle':
31
- style = !leaf.disabled && !core.State.isFocus(leaf) && data[stateType];
32
- break;
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 (style) {
36
- restoreStyle(leaf);
37
- leaf.__.normalStyle = leaf.get(style);
38
- leaf.set(style);
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
- function unsetStateStyle(leaf, _stateType, pointerState) {
43
- if (pointerState) {
44
- if (!hasFixedState(leaf)) {
45
- restoreStyle(leaf);
46
- if (core.State.isPress(leaf) && leaf.pressStyle) {
47
- setStateStyle(leaf, 'pressStyle', true);
48
- }
49
- else if (core.State.isHover(leaf) && leaf.hoverStyle) {
50
- setStateStyle(leaf, 'hoverStyle', true);
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
- restoreStyle(leaf);
56
- if (leaf.disabledStyle && leaf.disabled) {
57
- setStateStyle(leaf, 'disabledStyle');
58
- }
59
- else if (leaf.focusStyle && core.State.isFocus(leaf)) {
60
- setStateStyle(leaf, 'focusStyle');
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 if (leaf.selectedStyle && leaf.selected) {
63
- setStateStyle(leaf, 'selectedStyle');
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
- setStateStyle(leaf, 'hoverStyle', true);
229
+ setPointerState(leaf, 'hoverStyle');
72
230
  break;
73
231
  case core.PointerEvent.LEAVE:
74
- unsetStateStyle(leaf, 'hoverStyle', true);
232
+ unsetPointerState(leaf, 'hoverStyle');
75
233
  break;
76
234
  case core.PointerEvent.DOWN:
77
- setStateStyle(leaf, 'pressStyle', true);
235
+ setPointerState(leaf, 'pressStyle');
78
236
  break;
79
237
  case core.PointerEvent.UP:
80
- unsetStateStyle(leaf, 'pressStyle', true);
238
+ unsetPointerState(leaf, 'pressStyle');
81
239
  break;
82
240
  }
83
241
  }
84
242
 
85
- core.State.isHover = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isHover(leaf); };
86
- core.State.isPress = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isPress(leaf); };
87
- core.State.isFocus = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isFocus(leaf); };
88
- core.State.isDrag = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isDrag(leaf); };
89
- core.State.setStyle = function (leaf, stateType, value) { value ? setStateStyle(leaf, stateType) : unsetStateStyle(leaf); };
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.focus = function (value = true) {
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
- core.State.setStyle(this, 'focusStyle', value);
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;