b94455079a295967f55dc930267907409297fafb.svn-base
8.12 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
'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; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
* @format
*/
var CONSTANTS = require('./YGEnums');
var Layout = function () {
function Layout(left, right, top, bottom, width, height) {
_classCallCheck(this, Layout);
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
this.width = width;
this.height = height;
}
_createClass(Layout, [{
key: 'fromJS',
value: function fromJS(expose) {
expose(this.left, this.right, this.top, this.bottom, this.width, this.height);
}
}, {
key: 'toString',
value: function toString() {
return '<Layout#' + this.left + ':' + this.right + ';' + this.top + ':' + this.bottom + ';' + this.width + ':' + this.height + '>';
}
}]);
return Layout;
}();
var Size = function () {
_createClass(Size, null, [{
key: 'fromJS',
value: function fromJS(_ref) {
var width = _ref.width,
height = _ref.height;
return new Size(width, height);
}
}]);
function Size(width, height) {
_classCallCheck(this, Size);
this.width = width;
this.height = height;
}
_createClass(Size, [{
key: 'fromJS',
value: function fromJS(expose) {
expose(this.width, this.height);
}
}, {
key: 'toString',
value: function toString() {
return '<Size#' + this.width + 'x' + this.height + '>';
}
}]);
return Size;
}();
var Value = function () {
function Value(unit, value) {
_classCallCheck(this, Value);
this.unit = unit;
this.value = value;
}
_createClass(Value, [{
key: 'fromJS',
value: function fromJS(expose) {
expose(this.unit, this.value);
}
}, {
key: 'toString',
value: function toString() {
switch (this.unit) {
case CONSTANTS.UNIT_POINT:
return String(this.value);
case CONSTANTS.UNIT_PERCENT:
return this.value + '%';
case CONSTANTS.UNIT_AUTO:
return 'auto';
default:
{
return this.value + '?';
}
}
}
}, {
key: 'valueOf',
value: function valueOf() {
return this.value;
}
}]);
return Value;
}();
module.exports = function (bind, lib) {
function patch(prototype, name, fn) {
var original = prototype[name];
prototype[name] = function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return fn.call.apply(fn, [this, original].concat(args));
};
}
var _arr = ['setPosition', 'setMargin', 'setFlexBasis', 'setWidth', 'setHeight', 'setMinWidth', 'setMinHeight', 'setMaxWidth', 'setMaxHeight', 'setPadding'];
var _loop = function _loop() {
var _methods;
var fnName = _arr[_i];
var methods = (_methods = {}, _defineProperty(_methods, CONSTANTS.UNIT_POINT, lib.Node.prototype[fnName]), _defineProperty(_methods, CONSTANTS.UNIT_PERCENT, lib.Node.prototype[fnName + 'Percent']), _defineProperty(_methods, CONSTANTS.UNIT_AUTO, lib.Node.prototype[fnName + 'Auto']), _methods);
patch(lib.Node.prototype, fnName, function (original) {
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
// We patch all these functions to add support for the following calls:
// .setWidth(100) / .setWidth("100%") / .setWidth(.getWidth()) / .setWidth("auto")
var value = args.pop();
var unit = void 0,
asNumber = void 0;
if (value === 'auto') {
unit = CONSTANTS.UNIT_AUTO;
asNumber = undefined;
} else if (value instanceof Value) {
unit = value.unit;
asNumber = value.valueOf();
} else {
unit = typeof value === 'string' && value.endsWith('%') ? CONSTANTS.UNIT_PERCENT : CONSTANTS.UNIT_POINT;
asNumber = parseFloat(value);
if (!Number.isNaN(value) && Number.isNaN(asNumber)) {
throw new Error('Invalid value ' + value + ' for ' + fnName);
}
}
if (!methods[unit]) throw new Error('Failed to execute "' + fnName + '": Unsupported unit \'' + value + '\'');
if (asNumber !== undefined) {
var _methods$unit;
return (_methods$unit = methods[unit]).call.apply(_methods$unit, [this].concat(args, [asNumber]));
} else {
var _methods$unit2;
return (_methods$unit2 = methods[unit]).call.apply(_methods$unit2, [this].concat(args));
}
});
};
for (var _i = 0; _i < _arr.length; _i++) {
_loop();
}
patch(lib.Config.prototype, 'free', function () {
// Since we handle the memory allocation ourselves (via lib.Config.create),
// we also need to handle the deallocation
lib.Config.destroy(this);
});
patch(lib.Node, 'create', function (_, config) {
// We decide the constructor we want to call depending on the parameters
return config ? lib.Node.createWithConfig(config) : lib.Node.createDefault();
});
patch(lib.Node.prototype, 'free', function () {
// Since we handle the memory allocation ourselves (via lib.Node.create),
// we also need to handle the deallocation
lib.Node.destroy(this);
});
patch(lib.Node.prototype, 'freeRecursive', function () {
for (var t = 0, T = this.getChildCount(); t < T; ++t) {
this.getChild(0).freeRecursive();
}
this.free();
});
patch(lib.Node.prototype, 'setMeasureFunc', function (original, measureFunc) {
// This patch is just a convenience patch, since it helps write more
// idiomatic source code (such as .setMeasureFunc(null))
// We also automatically convert the return value of the measureFunc
// to a Size object, so that we can return anything that has .width and
// .height properties
if (measureFunc) {
return original.call(this, function () {
return Size.fromJS(measureFunc.apply(undefined, arguments));
});
} else {
return this.unsetMeasureFunc();
}
});
patch(lib.Node.prototype, 'calculateLayout', function (original) {
var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : NaN;
var height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NaN;
var direction = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : CONSTANTS.DIRECTION_LTR;
// Just a small patch to add support for the function default parameters
return original.call(this, width, height, direction);
});
return _extends({
Config: lib.Config,
Node: lib.Node,
Layout: bind('Layout', Layout),
Size: bind('Size', Size),
Value: bind('Value', Value),
getInstanceCount: function getInstanceCount() {
return lib.getInstanceCount.apply(lib, arguments);
}
}, CONSTANTS);
};