@leafer-in/state 1.0.2 → 1.0.4

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