-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.js
78 lines (68 loc) · 2.62 KB
/
Utils.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
var Utils = {
/*
* Returns a function that creates a ReactElement. Use instead of React.createClass.
* This method is almost equivalent to calling React.createFactory(React.createClass(param)), except
* that it returns a factory function that allows you to omit the "options" argument
* altogether instead of requiring you to explicitly pass null.
*
* @param {Object} obj The object that defines the class.
* @returns {function} a factory function
*/
createFactory: function(obj) {
var reactFactory = React.createFactory(React.createClass(obj));
return Utils.convertFactory(reactFactory);
},
/*
* Converts a React factory function into another factory function that allows you to omit the "options" argument
* altogether instead of requiring you to explicitly pass null.
*
* @param {function} reactFactory React factory function
* @returns {function} a factory function
*/
convertFactory: function(reactFactory) {
return function(maybeOptions) {
// An options object was explicitly passed.
if (maybeOptions !== undefined && !React.isValidElement(maybeOptions) && typeof (maybeOptions) !== "string" && !Utils.isArray(maybeOptions)) {
return reactFactory.apply(null, arguments);
} else {
return reactFactory.apply(null, [null].concat(arguments));
}
};
},
/**
* Uses canvas.measureText to compute and return the width of the given text of given font in pixels.
*
* @param {String} text The text to be rendered
* @param {String} font The css font descriptor that text is to be rendered with (e.g. "bold 14px verdana").
*
* @see http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
*/
getTextWidth: function(text, font) {
// re-use canvas object for better performance
var canvas = Utils.getTextWidth.canvas || (Utils.getTextWidth.canvas = document.createElement("canvas"));
var context = canvas.getContext("2d");
context.font = font;
var metrics = context.measureText(text);
return metrics.width;
},
/**
* Capitalizes the first letter in a string.
*
* @param {String} str
* @returns {String}
*/
capitalize: function(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
isArray: function( obj ) {
return toString.call(obj) === "[object Array]";
},
};
// Add all standard element factories in React.DOM to the window to provide a nicer syntax for using standard DOM elements.
Object.keys(React.DOM).forEach(function(key) {
var windowKey = Utils.capitalize(key);
if (window.hasOwnProperty(windowKey)) {
windowKey += 'Element';
}
window[windowKey] = Utils.convertFactory(React.DOM[key]);
});