@leafer-in/state 1.8.0 → 1.9.0

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,20 +1,18 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
- var core = require('@leafer-ui/core');
3
+ var core = require("@leafer-ui/core");
4
4
 
5
5
  function stateType(defaultValue, styleName) {
6
- return core.decorateLeafAttr(defaultValue, (key) => core.attr({
6
+ return core.decorateLeafAttr(defaultValue, key => core.attr({
7
7
  set(value) {
8
8
  this.__setAttr(key, value);
9
- if (this.leaferIsReady)
10
- styleName ? core.State.setStyleName(this, styleName, value) : core.State.set(this, value);
11
- else
12
- this.__layout.stateStyleChanged = true;
9
+ if (this.leaferIsReady) styleName ? core.State.setStyleName(this, styleName, value) : core.State.set(this, value); else this.__layout.stateStyleChanged = true;
13
10
  }
14
11
  }));
15
12
  }
13
+
16
14
  function stateStyleType(defaultValue) {
17
- return core.decorateLeafAttr(defaultValue, (key) => core.attr({
15
+ return core.decorateLeafAttr(defaultValue, key => core.attr({
18
16
  set(value) {
19
17
  this.__setAttr(key, value);
20
18
  this.__layout.stateStyleChanged = true;
@@ -23,14 +21,12 @@ function stateStyleType(defaultValue) {
23
21
  }
24
22
 
25
23
  function findParentButton(leaf, button) {
26
- if (button && button !== true)
27
- return button;
24
+ if (button && button !== true) return button;
28
25
  if (!leaf.button) {
29
- let { parent } = leaf;
26
+ let {parent: parent} = leaf;
30
27
  for (let i = 0; i < 2; i++) {
31
28
  if (parent) {
32
- if (parent.button)
33
- return parent;
29
+ if (parent.button) return parent;
34
30
  parent = parent.parent;
35
31
  }
36
32
  }
@@ -39,121 +35,106 @@ function findParentButton(leaf, button) {
39
35
  }
40
36
 
41
37
  function setStyle(leaf, style) {
42
- if (typeof style !== 'object')
43
- style = undefined;
44
- updateStyle(leaf, style, 'in');
38
+ if (!core.isObject(style)) style = undefined;
39
+ updateStyle(leaf, style, "in");
45
40
  }
41
+
46
42
  function unsetStyle(leaf, style) {
47
- const { normalStyle } = leaf;
48
- if (typeof style !== 'object')
49
- style = undefined;
43
+ const {normalStyle: normalStyle} = leaf;
44
+ if (!core.isObject(style)) style = undefined;
50
45
  if (normalStyle) {
51
- if (!style)
52
- style = normalStyle;
53
- updateStyle(leaf, style, 'out');
46
+ if (!style) style = normalStyle;
47
+ updateStyle(leaf, style, "out");
54
48
  }
55
49
  }
50
+
56
51
  const emprtyStyle = {};
52
+
57
53
  function updateStyle(leaf, style, type) {
58
- const { normalStyle } = leaf;
59
- if (!style)
60
- style = emprtyStyle;
54
+ const {normalStyle: normalStyle} = leaf;
55
+ if (!style) style = emprtyStyle;
61
56
  if (style.scale) {
62
57
  core.MathHelper.assignScale(style, style.scale);
63
58
  delete style.scale;
64
59
  }
65
- if (style === emprtyStyle || !core.State.canAnimate)
66
- type = null;
60
+ if (style === emprtyStyle || !core.State.canAnimate) type = null;
67
61
  let transition = type ? getTransition(type, style, leaf) : false;
68
62
  const fromStyle = transition ? getFromStyle(leaf, style) : undefined;
69
63
  const nextStyle = core.State.canAnimate && getStyle(leaf);
70
- if (nextStyle)
71
- leaf.killAnimate('transition');
72
- if (normalStyle)
73
- leaf.set(normalStyle, 'temp');
64
+ if (nextStyle) leaf.killAnimate("transition");
65
+ if (normalStyle) leaf.set(normalStyle, "temp");
74
66
  const statesStyle = getStyle(leaf);
75
67
  if (statesStyle) {
76
- const { animation } = statesStyle;
68
+ const {animation: animation} = statesStyle;
77
69
  if (animation) {
78
- const animate = leaf.animate(animation, undefined, 'animation', true);
70
+ const animate = leaf.animate(animation, undefined, "animation", true);
79
71
  Object.assign(statesStyle, animate.endingStyle);
80
- if (type !== 'in' || style.animation !== animation)
81
- animate.kill();
82
- else
83
- transition = false;
72
+ if (type !== "in" || style.animation !== animation) animate.kill(); else transition = false;
84
73
  delete statesStyle.animation;
85
74
  }
86
75
  leaf.normalStyle = filterStyle(statesStyle, leaf);
87
- leaf.set(statesStyle, 'temp');
88
- }
89
- else {
76
+ leaf.set(statesStyle, "temp");
77
+ } else {
90
78
  leaf.normalStyle = undefined;
91
79
  }
92
80
  if (transition) {
93
81
  const toStyle = filterStyle(fromStyle, leaf);
94
- leaf.set(fromStyle, 'temp');
95
- leaf.animate([fromStyle, toStyle], transition, 'transition', true);
82
+ leaf.set(fromStyle, "temp");
83
+ leaf.animate([ fromStyle, toStyle ], transition, "transition", true);
96
84
  }
97
85
  leaf.__layout.stateStyleChanged = false;
98
86
  }
87
+
99
88
  function getStyle(leaf) {
100
89
  let exist;
101
90
  const style = {}, button = findParentButton(leaf);
102
- const state = button ? (leaf.state || button.state) : leaf.state;
91
+ const state = button ? leaf.state || button.state : leaf.state;
103
92
  const stateStyle = state && leaf.states[state];
104
- if (stateStyle && core.State.isState(state, leaf, button))
105
- exist = assign(style, stateStyle);
93
+ if (stateStyle && core.State.isState(state, leaf, button)) exist = assign(style, stateStyle);
106
94
  const selectedStyle = style.selectedStyle || leaf.selectedStyle;
107
- if (selectedStyle && core.State.isSelected(leaf, button))
108
- exist = assign(style, selectedStyle);
95
+ if (selectedStyle && core.State.isSelected(leaf, button)) exist = assign(style, selectedStyle);
109
96
  const placeholderStyle = style.placeholderStyle || leaf.placeholderStyle;
110
- if (placeholderStyle && core.State.isPlacehold(leaf, button))
111
- exist = assign(style, placeholderStyle);
97
+ if (placeholderStyle && core.State.isPlacehold(leaf, button)) exist = assign(style, placeholderStyle);
112
98
  if (core.State.isDisabled(leaf, button)) {
113
99
  const disabledStyle = style.disabledStyle || leaf.disabledStyle;
114
- if (disabledStyle)
115
- exist = assign(style, disabledStyle);
116
- }
117
- else {
100
+ if (disabledStyle) exist = assign(style, disabledStyle);
101
+ } else {
118
102
  const focusStyle = style.focusStyle || leaf.focusStyle;
119
- if (focusStyle && core.State.isFocus(leaf, button))
120
- exist = assign(style, focusStyle);
103
+ if (focusStyle && core.State.isFocus(leaf, button)) exist = assign(style, focusStyle);
121
104
  const hoverStyle = style.hoverStyle || leaf.hoverStyle;
122
- if (hoverStyle && core.State.isHover(leaf, button))
123
- exist = assign(style, hoverStyle);
105
+ if (hoverStyle && core.State.isHover(leaf, button)) exist = assign(style, hoverStyle);
124
106
  const pressStyle = style.pressStyle || leaf.pressStyle;
125
- if (pressStyle && core.State.isPress(leaf, button))
126
- exist = assign(style, pressStyle);
107
+ if (pressStyle && core.State.isPress(leaf, button)) exist = assign(style, pressStyle);
127
108
  }
128
109
  return exist ? style : undefined;
129
110
  }
111
+
130
112
  function filterStyle(style, data, addStyle, useAnimateExcludes) {
131
113
  const to = addStyle ? style : {}, forStyle = addStyle || style;
132
114
  for (let key in forStyle) {
133
115
  if (useAnimateExcludes) {
134
- if (!core.State.animateExcludes[key])
135
- to[key] = data[key];
136
- }
137
- else
138
- to[key] = data[key];
116
+ if (!core.State.animateExcludes[key]) to[key] = data[key];
117
+ } else to[key] = data[key];
139
118
  }
140
119
  return to;
141
120
  }
121
+
142
122
  function filterAnimateStyle(style, data, addStyle) {
143
123
  return filterStyle(style, data, addStyle, true);
144
124
  }
125
+
145
126
  function getFromStyle(leaf, style) {
146
127
  const fromStyle = filterAnimateStyle(style, leaf), animate = leaf.animate();
147
- if (animate)
148
- filterAnimateStyle(fromStyle, leaf, animate.fromStyle);
128
+ if (animate) filterAnimateStyle(fromStyle, leaf, animate.fromStyle);
149
129
  return fromStyle;
150
130
  }
131
+
151
132
  function getTransition(type, style, data) {
152
- let name = type === 'in' ? 'transition' : 'transitionOut';
153
- if (type === 'out' && core.isNull(data[name]) && core.isNull(style[name]))
154
- name = 'transition';
133
+ let name = type === "in" ? "transition" : "transitionOut";
134
+ if (type === "out" && core.isNull(data[name]) && core.isNull(style[name])) name = "transition";
155
135
  return core.isNull(style[name]) ? data[name] : style[name];
156
136
  }
137
+
157
138
  function assign(style, stateStyle) {
158
139
  Object.assign(style, stateStyle);
159
140
  return true;
@@ -161,90 +142,79 @@ function assign(style, stateStyle) {
161
142
 
162
143
  function setPointerState(leaf, stateName) {
163
144
  const style = leaf[stateName];
164
- if (style)
165
- setStyle(leaf, style);
166
- if (leaf.button)
167
- setChildrenState(leaf.children, stateName);
145
+ if (style) setStyle(leaf, style);
146
+ if (leaf.button) setChildrenState(leaf.children, stateName);
168
147
  }
148
+
169
149
  function setState(leaf, stateName, stateStyle) {
170
- if (!stateStyle)
171
- stateStyle = leaf.states[stateName];
150
+ if (!stateStyle) stateStyle = leaf.states[stateName];
172
151
  setStyle(leaf, stateStyle);
173
- if (leaf.button)
174
- setChildrenState(leaf.children, null, stateName);
152
+ if (leaf.button) setChildrenState(leaf.children, null, stateName);
175
153
  }
154
+
176
155
  function setChildrenState(children, stateType, stateName) {
177
- if (!children)
178
- return;
156
+ if (!children) return;
179
157
  let leaf, update;
180
158
  for (let i = 0, len = children.length; i < len; i++) {
181
159
  leaf = children[i];
182
160
  if (stateType) {
183
161
  update = true;
184
162
  switch (stateType) {
185
- case 'hoverStyle':
186
- if (core.State.isHover(leaf))
187
- update = false;
188
- break;
189
- case 'pressStyle':
190
- if (core.State.isPress(leaf))
191
- update = false;
192
- break;
193
- case 'focusStyle':
194
- if (core.State.isFocus(leaf))
195
- update = false;
163
+ case "hoverStyle":
164
+ if (core.State.isHover(leaf)) update = false;
165
+ break;
166
+
167
+ case "pressStyle":
168
+ if (core.State.isPress(leaf)) update = false;
169
+ break;
170
+
171
+ case "focusStyle":
172
+ if (core.State.isFocus(leaf)) update = false;
196
173
  }
197
- if (update)
198
- setPointerState(leaf, stateType);
199
- }
200
- else if (stateName !== undefined)
201
- setState(leaf, stateName);
202
- if (leaf.isBranch)
203
- setChildrenState(leaf.children, stateType, stateName);
174
+ if (update) setPointerState(leaf, stateType);
175
+ } else if (!core.isUndefined(stateName)) setState(leaf, stateName);
176
+ if (leaf.isBranch) setChildrenState(leaf.children, stateType, stateName);
204
177
  }
205
178
  }
206
179
 
207
180
  function unsetPointerState(leaf, stateName) {
208
181
  const style = leaf[stateName];
209
- if (style)
210
- unsetStyle(leaf, style);
211
- if (leaf.button)
212
- unsetChildrenState(leaf.children, stateName);
182
+ if (style) unsetStyle(leaf, style);
183
+ if (leaf.button) unsetChildrenState(leaf.children, stateName);
213
184
  }
185
+
214
186
  function unsetState(leaf, stateName, stateStyle) {
215
187
  unsetStyle(leaf, stateStyle);
216
- if (leaf.button)
217
- unsetChildrenState(leaf.children, null, stateName);
188
+ if (leaf.button) unsetChildrenState(leaf.children, null, stateName);
218
189
  }
190
+
219
191
  function unsetChildrenState(children, stateType, stateName) {
220
- if (!children)
221
- return;
192
+ if (!children) return;
222
193
  let leaf;
223
194
  for (let i = 0, len = children.length; i < len; i++) {
224
195
  leaf = children[i];
225
- if (stateType)
226
- unsetPointerState(leaf, stateType);
227
- else if (stateName !== undefined)
228
- unsetState(leaf, stateName);
229
- if (leaf.isBranch)
230
- unsetChildrenState(leaf.children, stateType, stateName);
196
+ if (stateType) unsetPointerState(leaf, stateType); else if (!core.isUndefined(stateName)) unsetState(leaf, stateName);
197
+ if (leaf.isBranch) unsetChildrenState(leaf.children, stateType, stateName);
231
198
  }
232
199
  }
233
200
 
234
201
  function updateEventStyle(leaf, eventType) {
235
202
  switch (eventType) {
236
- case core.PointerEvent.ENTER:
237
- setPointerState(leaf, 'hoverStyle');
238
- break;
239
- case core.PointerEvent.LEAVE:
240
- unsetPointerState(leaf, 'hoverStyle');
241
- break;
242
- case core.PointerEvent.DOWN:
243
- setPointerState(leaf, 'pressStyle');
244
- break;
245
- case core.PointerEvent.UP:
246
- unsetPointerState(leaf, 'pressStyle');
247
- break;
203
+ case core.PointerEvent.ENTER:
204
+ setPointerState(leaf, "hoverStyle");
205
+ break;
206
+
207
+ case core.PointerEvent.LEAVE:
208
+ unsetPointerState(leaf, "hoverStyle");
209
+ break;
210
+
211
+ case core.PointerEvent.DOWN:
212
+ setPointerState(leaf, "pressStyle");
213
+ break;
214
+
215
+ case core.PointerEvent.UP:
216
+ unsetPointerState(leaf, "pressStyle");
217
+ break;
248
218
  }
249
219
  }
250
220
 
@@ -255,32 +225,32 @@ function checkPointerState(fnName, leaf, button) {
255
225
  find = interaction[fnName](leaf);
256
226
  if (!find && button) {
257
227
  const parentButton = findParentButton(leaf, button);
258
- if (parentButton)
259
- find = interaction[fnName](parentButton);
228
+ if (parentButton) find = interaction[fnName](parentButton);
260
229
  }
261
230
  }
262
231
  return find;
263
232
  }
233
+
264
234
  function checkFixedState(attrName, leaf, button) {
265
235
  let find = leaf[attrName];
266
236
  if (!find && button) {
267
237
  const parentButton = findParentButton(leaf, button);
268
- if (parentButton)
269
- find = parentButton[attrName];
238
+ if (parentButton) find = parentButton[attrName];
270
239
  }
271
240
  return find;
272
241
  }
242
+
273
243
  function checkState(stateName, leaf, button) {
274
244
  let find = leaf.states[stateName];
275
245
  if (!find && button) {
276
246
  const parentButton = findParentButton(leaf, button);
277
- if (parentButton)
278
- find = parentButton.states[stateName];
247
+ if (parentButton) find = parentButton.states[stateName];
279
248
  }
280
249
  return !!find;
281
250
  }
282
251
 
283
- core.Plugin.add('state');
252
+ core.Plugin.add("state");
253
+
284
254
  core.State.animateExcludes = {
285
255
  animation: 1,
286
256
  animationOut: 1,
@@ -297,58 +267,108 @@ core.State.animateExcludes = {
297
267
  disabledStyle: 1,
298
268
  placeholderStyle: 1
299
269
  };
300
- core.State.isState = function (state, leaf, button) { return checkState(state, leaf, button); };
301
- core.State.isSelected = function (leaf, button) { return checkFixedState('selected', leaf, button); };
302
- core.State.isDisabled = function (leaf, button) { return checkFixedState('disabled', leaf, button); };
303
- core.State.isFocus = function (leaf, button) { return checkPointerState('isFocus', leaf, button); };
304
- core.State.isHover = function (leaf, button) { return checkPointerState('isHover', leaf, button); };
305
- core.State.isPress = function (leaf, button) { return checkPointerState('isPress', leaf, button); };
306
- core.State.isPlacehold = function (leaf, _button) { return leaf.__.__isPlacehold; };
307
- core.State.isDrag = function (leaf, button) { return checkPointerState('isDrag', leaf, button); };
308
- core.State.setStyleName = function (leaf, stateType, value) { value ? setState(leaf, stateType, leaf[stateType]) : unsetState(leaf, stateType, leaf[stateType]); };
309
- core.State.set = function (leaf, stateName) { const style = leaf.states[stateName]; style ? setState(leaf, stateName, style) : unsetState(leaf, stateName, style); };
270
+
271
+ core.State.isState = function(state, leaf, button) {
272
+ return checkState(state, leaf, button);
273
+ };
274
+
275
+ core.State.isSelected = function(leaf, button) {
276
+ return checkFixedState("selected", leaf, button);
277
+ };
278
+
279
+ core.State.isDisabled = function(leaf, button) {
280
+ return checkFixedState("disabled", leaf, button);
281
+ };
282
+
283
+ core.State.isFocus = function(leaf, button) {
284
+ return checkPointerState("isFocus", leaf, button);
285
+ };
286
+
287
+ core.State.isHover = function(leaf, button) {
288
+ return checkPointerState("isHover", leaf, button);
289
+ };
290
+
291
+ core.State.isPress = function(leaf, button) {
292
+ return checkPointerState("isPress", leaf, button);
293
+ };
294
+
295
+ core.State.isPlacehold = function(leaf, _button) {
296
+ return leaf.__.__isPlacehold;
297
+ };
298
+
299
+ core.State.isDrag = function(leaf, button) {
300
+ return checkPointerState("isDrag", leaf, button);
301
+ };
302
+
303
+ core.State.setStyleName = function(leaf, stateType, value) {
304
+ value ? setState(leaf, stateType, leaf[stateType]) : unsetState(leaf, stateType, leaf[stateType]);
305
+ };
306
+
307
+ core.State.set = function(leaf, stateName) {
308
+ const style = leaf.states[stateName];
309
+ style ? setState(leaf, stateName, style) : unsetState(leaf, stateName, style);
310
+ };
311
+
310
312
  core.State.getStyle = getStyle;
313
+
311
314
  core.State.updateStyle = updateStyle;
315
+
312
316
  core.State.updateEventStyle = updateEventStyle;
317
+
313
318
  const ui = core.UI.prototype;
314
- core.UI.addAttr('selected', false, stateType, 'selectedStyle');
315
- core.UI.addAttr('disabled', false, stateType, 'disabledStyle');
316
- core.UI.addAttr('states', {}, stateStyleType);
317
- core.UI.addAttr('state', '', stateType);
318
- core.UI.addAttr('normalStyle', undefined, core.dataType);
319
- core.UI.addAttr('hoverStyle', undefined, stateStyleType);
320
- core.UI.addAttr('pressStyle', undefined, stateStyleType);
321
- core.UI.addAttr('focusStyle', undefined, stateStyleType);
322
- core.UI.addAttr('selectedStyle', undefined, stateStyleType);
323
- core.UI.addAttr('disabledStyle', undefined, stateStyleType);
324
- core.UI.addAttr('placeholderStyle', undefined, stateStyleType);
325
- core.UI.addAttr('button', false, core.dataType);
326
- ui.focus = function (value = true) {
319
+
320
+ core.UI.addAttr("selected", false, stateType, "selectedStyle");
321
+
322
+ core.UI.addAttr("disabled", false, stateType, "disabledStyle");
323
+
324
+ core.UI.addAttr("states", {}, stateStyleType);
325
+
326
+ core.UI.addAttr("state", "", stateType);
327
+
328
+ core.UI.addAttr("normalStyle", undefined, core.dataType);
329
+
330
+ core.UI.addAttr("hoverStyle", undefined, stateStyleType);
331
+
332
+ core.UI.addAttr("pressStyle", undefined, stateStyleType);
333
+
334
+ core.UI.addAttr("focusStyle", undefined, stateStyleType);
335
+
336
+ core.UI.addAttr("selectedStyle", undefined, stateStyleType);
337
+
338
+ core.UI.addAttr("disabledStyle", undefined, stateStyleType);
339
+
340
+ core.UI.addAttr("placeholderStyle", undefined, stateStyleType);
341
+
342
+ core.UI.addAttr("button", false, core.dataType);
343
+
344
+ ui.focus = function(value = true) {
327
345
  this.waitLeafer(() => {
328
- let { focusData } = this.app.interaction;
346
+ let {focusData: focusData} = this.app.interaction;
329
347
  if (value) {
330
- if (focusData)
331
- focusData.focus(false);
348
+ if (focusData) focusData.focus(false);
332
349
  focusData = this;
333
- }
334
- else
335
- focusData = null;
350
+ } else focusData = null;
336
351
  this.app.interaction.focusData = focusData;
337
- value ? setPointerState(this, 'focusStyle') : unsetPointerState(this, 'focusStyle');
352
+ value ? setPointerState(this, "focusStyle") : unsetPointerState(this, "focusStyle");
338
353
  });
339
354
  };
340
- ui.updateState = function () {
341
- core.State.updateStyle(this, undefined, 'in');
355
+
356
+ ui.updateState = function() {
357
+ core.State.updateStyle(this, undefined, "in");
342
358
  };
343
- const text = core.Text.prototype, textKey = 'text';
344
- core.defineKey(text, textKey, Object.assign(Object.assign({}, core.getDescriptor(text, textKey)), { set(value) {
359
+
360
+ const text = core.Text.prototype, textKey = "text";
361
+
362
+ core.defineKey(text, textKey, Object.assign(Object.assign({}, core.getDescriptor(text, textKey)), {
363
+ set(value) {
345
364
  const t = this, oldValue = t.text;
346
365
  if (t.__setAttr(textKey, value)) {
347
366
  core.doBoundsType(t);
348
- if (t.placeholderStyle && t.placeholder && (oldValue === '' || value === ''))
349
- t.__layout.stateStyleChanged = true;
367
+ if (t.placeholderStyle && t.placeholder && (oldValue === "" || value === "")) t.__layout.stateStyleChanged = true;
350
368
  }
351
- } }));
369
+ }
370
+ }));
352
371
 
353
372
  exports.stateStyleType = stateStyleType;
373
+
354
374
  exports.stateType = stateType;