-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreact-domify.js
147 lines (128 loc) · 4.32 KB
/
react-domify.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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.DOMify = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (global){
'use strict';
var React = (typeof window !== "undefined" ? window['React'] : typeof global !== "undefined" ? global['React'] : null);
var styles = {
string: {
color: '#0e4889',
cursor: 'default'
},
bool: {
color: '#06624b',
cursor: 'default',
fontStyle: 'italic'
},
number: {
color: '#ca000a',
cursor: 'default'
},
date: {
color: '#009f7b',
cursor: 'default'
},
empty: {
color: '#999999',
cursor: 'default'
},
array: {
color: '#666666',
cursor: 'default'
},
object: {
color: '#0b89b6',
cursor: 'default'
},
comma: {
color: '#999999',
cursor: 'default'
}
};
function transform(obj, fromRecur, comma) {
var tag = fromRecur ? 'span' : 'div';
var nextLevel = (fromRecur || 0) + 1;
var children = [];
comma = comma ? React.createElement(
'span',
{ style: styles.comma },
','
) : null;
// strings
if (typeof obj === 'string') {
return React.createElement(tag, { style: styles.string }, '"' + obj + '"', comma);
}
// booleans, null and undefined
else if (typeof obj === 'boolean' || obj === null || obj === undefined) {
return React.createElement(tag, { style: styles.bool }, String(obj), comma);
}
// numbers
else if (typeof obj === 'number') {
return React.createElement(tag, { style: styles.number }, String(obj), comma);
}
// dates
else if (Object.prototype.toString.call(obj) === '[object Date]') {
return React.createElement(tag, { style: styles.date }, String(obj), comma);
}
// arrays
else if (Array.isArray(obj)) {
if (!obj.length) {
return React.createElement(tag, { style: styles.empty }, 'Array: []');
}
children.push(React.createElement(tag, { key: '__array:open__', style: styles.array }, 'Array: ['));
for (var i = 0; i < obj.length; i++) {
children.push(React.createElement(
'div',
{ key: 'i' + i, style: { paddingLeft: '20px' } },
transform(obj[i], nextLevel, i < obj.length - 1)
));
}
children.push(React.createElement(tag, { key: '__array:close__', style: styles.array }, ']'));
}
// objects
else if (obj && typeof obj === 'object') {
var len = Object.keys(obj).length;
if (fromRecur && !len) {
return React.createElement(tag, { style: styles.empty }, 'Object: {}');
}
if (fromRecur) {
children.push(React.createElement(tag, { key: '__object:open__', style: styles.object }, 'Object: {'));
}
for (var key in obj) {
if (typeof obj[key] !== 'function') {
children.push(React.createElement(
'div',
{ key: key, style: { paddingLeft: fromRecur ? '20px' : '0px' } },
React.createElement(
'span',
{ style: { paddingRight: '5px', cursor: 'default' } },
key,
':'
),
transform(obj[key], nextLevel)
));
}
}
if (fromRecur) {
children.push(React.createElement(tag, { key: '__object:close__', style: styles.object }, '}'));
}
}
return React.createElement(
'div',
null,
children,
comma
);
}
var DOMify = React.createClass({
displayName: 'DOMify',
render: function render() {
return React.createElement(
'div',
{ className: this.props.className, style: this.props.style },
transform(this.props.value)
);
}
});
module.exports = DOMify;
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}]},{},[1])(1)
});