-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.web.js
285 lines (269 loc) · 7.64 KB
/
index.web.js
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
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
const { StyleSheet, Platform } = require('react-native');
const deepDiffer = require('./lib/deepDiffer.web.js');
var _themes = {};
var _proxies = {};
var _components = {};
var _current = 'default';
var _rootComponent = null;
/**
* styleHasFunction - check style object has function
*
* @param {object} style object
* @returns {boolean}
*/
function styleHasFunction(style) {
for (var key in style) {
if (typeof style[key] === 'function') {
return true;
} else if (typeof style[key] === 'object') {
if (styleHasFunction(style[key])) {
return true;
}
}
}
return false;
}
/**
* platformSpecific - allow platforms specific styles
*
* @param {object} styles input styles object
* @returns {object} styles object for a specific platform
*/
function platformSpecific(styles) {
const result = {};
for (var key in styles) {
var platforms = ['ios', 'android', 'windows', 'web'];
var styles_key = styles[key];
var style = {};
for (var i in styles_key) {
if (platforms.indexOf(i) === -1) {
style[i] = styles_key[i];
}
}
result[key] = styles_key.hasOwnProperty(Platform.OS) ? _extends({}, style, styles_key[Platform.OS]) : style;
}
return result;
}
/**
* defineComponent - private define theme component
*
* @param {object} type component's type
* @returns {undefined}
*/
function defineComponent(type) {
Object.defineProperty(Theme, type, {
get: function () {
if (_components[_current] !== undefined && _components[_current][type] !== undefined) {
var component = _components[_current][type];
if (typeof component === 'function' && component.prototype.isReactComponent === undefined) {
component = component();
_components[_current][type] = component;
}
return component;
}
if (_current !== 'default' && _components.default !== undefined) {
var component = _components.default[type];
if (typeof component === 'function' && component.prototype.isReactComponent === undefined) {
component = component();
_components.default[type] = component;
}
return component;
}
return undefined;
}
});
}
// Theme class
const Theme = {
/**
* styles - get styles depend on theme
*
* @returns {object} StyleSheet object
*/
get styles() {
if (_current === 'default') {
if (_themes.default !== undefined) {
return _themes.default;
}
_proxies.default = {};
}
if (_proxies[_current] === undefined) {
if (_themes.default !== undefined) {
_proxies[_current] = _extends({}, _themes.default, _themes[_current]);
} else {
return _themes[_current];
}
}
return _proxies[_current];
},
/**
* name - get current theme name
*
* @returns {string}
*/
get name() {
return _current;
},
/**
* add - add style for a theme
*
* @param {object} styles styles to add
* @param {string} name = 'default' theme's name
* @returns {number}
*/
add(styles, name = 'default') {
// Check styles is processed
var processed = 2;
for (var key in styles) {
if (typeof styles[key] === 'number') {
processed = 1;
} else {
processed = 0;
// Platform Specific
styles = platformSpecific(styles);
}
break;
}
if (processed === 2) {
return 2;
}
// Add new theme
if (_themes[name] === undefined) {
_themes[name] = processed ? styles : StyleSheet.create(styles);
return 0;
}
// Merge theme
var theme = _themes[name];
for (var key in styles) {
var style = styles[key];
if (theme[key] !== undefined) {
var style_obj = StyleSheet.flatten([theme[key], style]);
if (styleHasFunction(StyleSheet.flatten(style))) {
theme[key] = StyleSheet.create({
style: style_obj
}).style;
} else if (deepDiffer(StyleSheet.flatten(theme[key]), style_obj)) {
if (typeof style !== 'number' || deepDiffer(style_obj, StyleSheet.flatten(style))) {
theme[key] = StyleSheet.create({
style: style_obj
}).style;
} else {
theme[key] = style;
}
}
} else {
theme[key] = typeof style === 'number' ? style : StyleSheet.create({ style }).style;
}
}
// Clear proxies cache
if (name !== 'default' && _proxies[name] !== undefined) {
delete _proxies[name];
}
return 0;
},
/**
* active - active a theme by name
*
* @param {string} name = 'default' theme's name
* @returns {undefined}
*/
active(name = 'default') {
if (name !== _current && _themes[name] !== undefined) {
_current = name;
if (_rootComponent !== null) {
_rootComponent.forceUpdate();
}
} else {
if (name !== _current) {
console.warn('You must add theme data before active it.');
}
console.warn('Activated theme: ' + _current);
}
},
/**
* setRoot - set a component to be root of theme
*
* @param {object} root component
* @returns {undefined}
*/
setRoot(root) {
if (typeof root === 'undefined') {
_rootComponent = null;
} else if (typeof root === 'object' && typeof root.forceUpdate === 'function') {
_rootComponent = root;
} else {
console.warn('setRoot: root must be a react native component or undefined');
}
},
/**
* css - transform mixed styles in to RN styles object
*
* @param {object} styles mixed styles
* @returns {object} react native styles
*/
css(styles) {
if (typeof styles === 'string' || Array.isArray(styles)) {
if (typeof styles === 'string') {
var result = styles.split(' ').map(function (name) {
if (name.length !== 0 && Theme.styles[name] !== undefined) {
return Theme.styles[name];
}
return 0;
}).filter(function (style) {
return style;
});
} else {
var result = [];
for (var i = 0, styleLength = styles.length; i < styleLength; ++i) {
var computed = Theme.css(styles[i]);
if (Array.isArray(computed)) {
Array.prototype.push.apply(result, computed);
} else if (computed !== 0) {
result.push(computed);
}
}
}
if (result.length === 0) {
return 0;
} else if (result.length === 1) {
return result[0];
}
return result;
}
return styles;
},
/**
* addComponents - add components to a theme
*
* @param {object} components components object {name: component}
* @param {string} name = 'default' theme's name
* @returns {undefined}
*/
addComponents(components, name = 'default') {
if (typeof components !== 'object') {
console.warn('Expected argument to be an object.');
return;
}
if (_components[name] === undefined) {
_components[name] = {};
}
var theme = _components[name];
var types = Object.keys(components);
for (var i = 0, typesLength = types.length; i < typesLength; i++) {
var type = types[i];
theme[type] = components[type];
Theme[type] !== undefined || defineComponent(type);
}
},
/**
* reset - reset themes data
*/
reset() {
_themes = {};
_proxies = {};
_components = {};
}
};
module.exports = Theme;