-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlayout.js
More file actions
99 lines (86 loc) · 2.54 KB
/
layout.js
File metadata and controls
99 lines (86 loc) · 2.54 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
'use strict';
const React = require('react');
const _layouts = {};
/**
* addModify - allow plugins to modify components
*
* @param {object} layout modifies layout object
* @param {string} keyPrefix = '' prefix key for layout_key to apply
* @returns {undefined}
*/
function addModify(layout, keyPrefix = '') {
if (typeof layout !== 'object') {
console.warn('Expected argument to be an object.');
return;
}
var layoutKeys = Object.keys(layout);
for (var i = 0, layoutKeysLength = layoutKeys.length; i < layoutKeysLength; i++) {
var layoutKey = layoutKeys[i];
var modifies = layout[layoutKey];
if (typeof modifies === 'function') {
var key = keyPrefix + layoutKey;
if (_layouts[key] === undefined) {
_layouts[key] = [modifies];
} else {
_layouts[key].push(modifies);
}
} else if (typeof modifies === 'object') {
addModify(modifies, keyPrefix + layoutKey + '_');
}
}
}
/**
* reset - reset layout modifies object
*/
addModify.reset = function() {
_layouts = {};
}
if (typeof global._createRNElement === 'undefined') {
global._createRNElement = function(type, config, child) {
// Process children
var childrenLength = arguments.length - 2;
var children = Array(childrenLength > 0 ? childrenLength : 0);
if (childrenLength === 1) {
children[0] = child;
} else if (childrenLength > 1) {
for (var i = 0; i < childrenLength; i++) {
children[i] = arguments[i + 2];
}
}
// Apply modifies
var result = {
type,
config,
children,
stopRender: false
}; // {stopRender: boolean, element: Component}
var layoutContext = config && config.layoutContext;
var params = [result];
var applyModify = function (key) {
var modifies = _layouts[key];
if (modifies !== undefined) {
for (var i = 0, modifiesLength = modifies.length; i < modifiesLength; i++) {
modifies[i].apply(layoutContext, params);
}
}
};
// Global modifies
applyModify('global');
// Key dependent modifies
if (config && config.layoutKey !== undefined) {
applyModify(config.layoutKey);
// Delete config
delete config.layoutKey;
delete config.layoutContext;
}
if (result.stopRender) {
return null;
} else if (typeof result.element === 'object') {
return result.element;
}
// Recorrect config
config = result.config;
return React.createElement.apply(React, [type, config].concat(children));
}
}
module.exports = addModify;