forked from 15001217168/mojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.babel.js
More file actions
401 lines (378 loc) · 11.5 KB
/
Copy pathmodule.babel.js
File metadata and controls
401 lines (378 loc) · 11.5 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import h from './h';
/*
Base class for module. Extends and parses defaults.
*/
class Module {
constructor ( o = {} ) {
// this._isIt = o.isIt;
// delete o.isIt;
this._o = o;
this._index = this._o.index || 0;
// map of props that should be
// parsed to arrays of values
this._arrayPropertyMap = {
strokeDashoffset: 1,
strokeDasharray: 1,
origin: 1
}
this._skipPropsDelta = {
timeline: 1,
prevChainModule: 1,
callbacksContext: 1
};
this._declareDefaults();
this._extendDefaults();
this._vars();
this._render();
}
/*
Method to declare defaults.
@private
*/
_declareDefaults () {
this._defaults = { };
}
/*
Method to declare module's variables.
@private
*/
_vars () {
this._progress = 0;
this._strokeDasharrayBuffer = [];
}
/*
Method to render on initialization.
@private
*/
_render () { }
/*
Method to set property on the module.
@private
@param {String, Object} Name of the property to set
or object with properties to set.
@param {Any} Value for the property to set. Could be
undefined if the first param is object.
*/
_setProp ( attr, value ) {
if ( typeof attr === 'object' ) {
for ( var key in attr ) { this._assignProp( key, attr[key] ); }
} else { this._assignProp( attr, value ); }
}
/*
Method to assign single property's value.
@private
@param {String} Property name.
@param {Any} Property value.
*/
_assignProp ( key, value ) {
this._props[key] = value;
}
/*
Method to show element.
@private
*/
_show () {
var p = this._props;
if ( !this.el ) { return; }
if ( p.isSoftHide ) {
// this.el.style.opacity = p.opacity;
this._showByTransform();
} else { this.el.style.display = 'block'; }
this._isShown = true;
}
/*
Method to hide element.
@private
*/
_hide () {
if ( !this.el ) { return; }
if ( this._props.isSoftHide ) {
// this.el.style.opacity = 0;
h.setPrefixedStyle( this.el, 'transform', 'scale(0)' );
} else { this.el.style.display = 'none'; }
this._isShown = false;
}
/*
Method to show element by applying transform back to normal.
@private
*/
_showByTransform () {}
/*
Method to parse option string.
Searches for stagger and rand values and parses them.
Leaves the value unattended otherwise.
@param {Any} Option value to parse.
@returns {Number} Parsed options value.
*/
_parseOptionString (value) {
if (typeof value === 'string') {
if (value.match(/stagger/)) {
value = h.parseStagger(value, this._index);
}
}
if (typeof value === 'string') {
if (value.match(/rand/)) {
value = h.parseRand(value);
}
}
return value;
}
/*
Method to parse postion option.
@param {String} Property name.
@param {Any} Property Value.
@returns {String} Parsed options value.
*/
_parsePositionOption (key, value) {
if (h.unitOptionMap[key]) { value = h.parseUnit(value).string; }
return value;
}
/*
Method to parse strokeDash.. option.
@param {String} Property name.
@param {Any} Property value.
@returns {String} Parsed options value.
*/
_parseStrokeDashOption (key, value) {
var result = value;
// parse numeric/percent values for strokeDash.. properties
if ( this._arrayPropertyMap[key] ) {
var result = [];
switch (typeof value) {
case 'number':
result.push(h.parseUnit(value));
break;
case 'string':
var array = value.split(' ');
for (var i = 0; i < array.length; i++ ) {
result.push(h.parseUnit(array[i]));
}
break;
}
}
return result;
}
/*
Method to check if the property is delta property.
@private
@param {Any} Parameter value to check.
@returns {Boolean}
*/
_isDelta ( optionsValue ) {
var isObject = h.isObject( optionsValue );
isObject = isObject && !optionsValue.unit;
return !(!isObject || h.isArray(optionsValue) || h.isDOM(optionsValue));
}
/*
Method to get delta from property and set
the property's start value to the props object.
@private
@param {String} Key name to get delta for.
@param {Object} Option value to get the delta for.
*/
_getDelta ( key, optionsValue ) {
var delta;
if ((key === 'left' || key === 'top') && !this._o.ctx) {
h.warn(`Consider to animate x/y properties instead of left/top,
as it would be much more performant`, optionsValue);
}
// skip delta calculation for a property if it is listed
// in skipPropsDelta object
if ( this._skipPropsDelta && this._skipPropsDelta[key] ) { return; }
// get delta
delta = h.parseDelta(key, optionsValue, this._index);
// if successfully parsed - save it
if (delta.type != null) { this._deltas[key] = delta; }
var deltaEnd = ( typeof delta.end === 'object' )
? (delta.end.value === 0) ? 0 : delta.end.string
: delta.end;
// set props to end value of the delta
// 0 should be 0 regardless units
this._props[key] = deltaEnd;
}
/*
Method to copy `_o` options to `_props` object
with fallback to `_defaults`.
@private
*/
_extendDefaults ( ) {
this._props = {};
this._deltas = {};
for (var key in this._defaults) {
// skip property if it is listed in _skipProps
// if (this._skipProps && this._skipProps[key]) { continue; }
// copy the properties to the _o object
var value = ( this._o[key] != null ) ? this._o[key] : this._defaults[key];
// parse option
this._parseOption( key, value );
}
}
/*
Method to tune new oprions to _o and _props object.
@private
@param {Object} Options object to tune to.
*/
_tuneNewOptions (o) {
// hide the module before tuning it's options
// cuz the user could see the change
this._hide();
for (var key in o) {
// skip property if it is listed in _skipProps
// if (this._skipProps && this._skipProps[key]) { continue; }
// copy the properties to the _o object
// delete the key from deltas
o && (delete this._deltas[key]);
// rewrite _o record
this._o[key] = o[key];
// save the options to _props
this._parseOption( key, o[key] );
}
}
/*
Method to parse option value.
@private
@param {String} Option name.
@param {Any} Option value.
*/
_parseOption ( name, value ) {
// if delta property
if ( this._isDelta( value ) && !this._skipPropsDelta[name] ) {
this._getDelta( name, value );
var deltaEnd = h.getDeltaEnd( value );
return this._assignProp( name, this._parseProperty( name, deltaEnd ) );
}
this._assignProp( name, this._parseProperty( name, value ) );
}
/*
Method to parse postion and string props.
@private
@param {String} Property name.
@param {Any} Property value.
@returns {Any} Parsed property value.
*/
_parsePreArrayProperty ( name, value ) {
// parse stagger and rand values
value = this._parseOptionString(value);
// parse units for position properties
return this._parsePositionOption(name, value);
}
/*
Method to parse property value.
@private
@param {String} Property name.
@param {Any} Property value.
@returns {Any} Parsed property value.
*/
_parseProperty ( name, value ) {
// parse `HTML` element in `parent` option
if ( name === 'parent' ) { return h.parseEl( value ); }
// parse `stagger`, `rand` and `position`
value = this._parsePreArrayProperty( name, value );
// parse numeric/percent values for strokeDash.. properties
return this._parseStrokeDashOption(name, value);
}
/*
Method to parse values inside ∆.
@private
@param {String} Key name.
@param {Object} Delta.
@returns {Object} Delta with parsed parameters.
*/
_parseDeltaValues (name, delta) {
// return h.parseDelta( name, delta, this._index );
var d = {};
for (var key in delta) {
var value = delta[key];
// delete delta[key];
// add parsed properties
var newEnd = this._parsePreArrayProperty(name, value);
d[this._parsePreArrayProperty(name, key)] = newEnd;
}
return d;
}
/*
Method to parse delta and nondelta properties.
@private
@param {String} Property name.
@param {Any} Property value.
@returns {Any} Parsed property value.
*/
_preparsePropValue (key, value) {
return ( this._isDelta(value) )
? this._parseDeltaValues(key, value)
: this._parsePreArrayProperty( key, value );
}
/*
Method to calculate current progress of the deltas.
@private
@param {Number} Eased progress to calculate - [0..1].
@param {Number} Progress to calculate - [0..1].
*/
_calcCurrentProps ( easedProgress, p ) {
for (var key in this._deltas) {
var value = this._deltas[key];
// get eased progress from delta easing if defined and not curve
var isCurve = !!value.curve;
var ep = ( value.easing != null && !isCurve )
? value.easing( p ) : easedProgress;
if ( value.type === 'array' ) {
var arr;
// if prop property is array - reuse it else - create an array
if ( h.isArray( this._props[key] ) ) {
arr = this._props[key];
arr.length = 0;
} else { arr = []; }
// just optimization to prevent curve
// calculations on every array item
var proc = (isCurve) ? value.curve(p) : null;
for ( var i = 0; i < value.delta.length; i++ ) {
var item = value.delta[i],
dash = (!isCurve)
? value.start[i].value + ep * item.value
: proc * (value.start[i].value + p * item.value);
arr.push({
string: `${dash}${item.unit}`,
value: dash,
unit: item.unit,
});
}
this._props[key] = arr;
} else if ( value.type === 'number' ) {
this._props[key] = (!isCurve)
? value.start + ep * value.delta
: value.curve(p) * ( value.start + p * value.delta );
} else if ( value.type === 'unit' ) {
var currentValue = ( !isCurve )
? value.start.value + ep*value.delta
: value.curve(p) * ( value.start.value + p * value.delta );
this._props[key] = `${currentValue}${value.end.unit}`;
} else if ( value.type === 'color' ) {
var r, g, b, a;
if ( !isCurve ) {
r = parseInt(value.start.r + ep * value.delta.r, 10);
g = parseInt(value.start.g + ep * value.delta.g, 10);
b = parseInt(value.start.b + ep * value.delta.b, 10);
a = parseFloat(value.start.a + ep * value.delta.a);
} else {
var cp = value.curve(p);
r = parseInt(cp * (value.start.r + p*value.delta.r), 10);
g = parseInt(cp * (value.start.g + p*value.delta.g), 10);
b = parseInt(cp * (value.start.b + p*value.delta.b), 10);
a = parseFloat(cp * (value.start.a + p*value.delta.a));
}
this._props[key] = `rgba(${r},${g},${b},${a})`;
}
}
}
/*
Method to calculate current progress and probably draw it in children.
@private
@param {Number} Eased progress to set - [0..1].
@param {Number} Progress to set - [0..1].
*/
_setProgress ( easedProgress, progress ) {
this._progress = easedProgress;
this._calcCurrentProps( easedProgress, progress );
}
}
export default Module;