forked from 15001217168/mojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-proto.babel.js
More file actions
105 lines (93 loc) · 2.27 KB
/
Copy pathclass-proto.babel.js
File metadata and controls
105 lines (93 loc) · 2.27 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
/**
* ClassProto - base class for module.
* It is needed to:
* - declare `_defaults`
* - extend `_defaults` by `options` and save result to `_props`
* - declare `_vars` after extention
* - call `_render` eventually
*/
const ClassProto = {};
/**
* `get` - Method to get a property from `_props`.
*
* @public
* @param {String} Key.
* @returns {Any} Value from the `_props` by `key`.
*/
ClassProto.get = function (key) {
return this._props[key];
};
/**
* `set` - Method to get a property from `_props`.
*
* @public
* @param {String} Key.
* @param {Any} Value.
*/
ClassProto.set = function (key, value) {
this._props[key] = value;
};
/**
* `setIfNotSet` - function to set a property if it isn't
* present in the initialization options.
*
* @public
* @param {String} Key.
* @param {Any} Value.
* @returns {Object} This instance.
*/
ClassProto.setIfNotSet = function (key, value) {
if (this._o[key] === undefined) {
this.set(key, value);
}
return this;
};
/**
* `init` - lifecycle initialization function.
*
* @private
*/
ClassProto.init = function (o = {}) {
// save options
this._o = { ...o };
// parse index and delete it from options
this.index = this._o.index || 0;
delete this._o.index;
// parse total items and delete it from options
this._totalItemsInStagger = this._o.totalItemsInStagger || 1;
delete this._o.totalItemsInStagger;
this._declareDefaults();
this._extendDefaults();
this._vars();
};
/**
* _declareDefaults - function to declare `_defaults` object.
*
* @private
*/
ClassProto._declareDefaults = function () { this._defaults = {}; };
/**
* _extendDefaults - Method to copy `_o` options to `_props` object
* with fallback to `_defaults`.
* @private
*/
ClassProto._extendDefaults = function () {
this._props = { ...this._defaults };
const keys = Object.keys(this._o);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = this._o[key];
// only if value is defined
if (value !== undefined) {
this._props[key] = value;
}
}
};
/**
* _vars - function do declare `variables` after `_defaults` were extended
* by `options` and saved to `_props`
*
* @private
*/
ClassProto._vars = function () {};
export { ClassProto };