1+ /*
2+ react-json-table v0.0.1
3+ https://github.com/arqex/react-json-table
4+ MIT: https://github.com/arqex/react-json-table/raw/master/LICENSE
5+ */
6+ ( function webpackUniversalModuleDefinition ( root , factory ) {
7+ if ( typeof exports === 'object' && typeof module === 'object' )
8+ module . exports = factory ( require ( undefined ) ) ;
9+ else if ( typeof define === 'function' && define . amd )
10+ define ( [ ] , factory ) ;
11+ else if ( typeof exports === 'object' )
12+ exports [ "JsonTable" ] = factory ( require ( undefined ) ) ;
13+ else
14+ root [ "JsonTable" ] = factory ( root [ "React" ] ) ;
15+ } ) ( this , function ( __WEBPACK_EXTERNAL_MODULE_1__ ) {
16+ return /******/ ( function ( modules ) { // webpackBootstrap
17+ /******/ // The module cache
18+ /******/ var installedModules = { } ;
19+
20+ /******/ // The require function
21+ /******/ function __webpack_require__ ( moduleId ) {
22+
23+ /******/ // Check if module is in cache
24+ /******/ if ( installedModules [ moduleId ] )
25+ /******/ return installedModules [ moduleId ] . exports ;
26+
27+ /******/ // Create a new module (and put it into the cache)
28+ /******/ var module = installedModules [ moduleId ] = {
29+ /******/ exports : { } ,
30+ /******/ id : moduleId ,
31+ /******/ loaded : false
32+ /******/ } ;
33+
34+ /******/ // Execute the module function
35+ /******/ modules [ moduleId ] . call ( module . exports , module , module . exports , __webpack_require__ ) ;
36+
37+ /******/ // Flag the module as loaded
38+ /******/ module . loaded = true ;
39+
40+ /******/ // Return the exports of the module
41+ /******/ return module . exports ;
42+ /******/ }
43+
44+
45+ /******/ // expose the modules object (__webpack_modules__)
46+ /******/ __webpack_require__ . m = modules ;
47+
48+ /******/ // expose the module cache
49+ /******/ __webpack_require__ . c = installedModules ;
50+
51+ /******/ // __webpack_public_path__
52+ /******/ __webpack_require__ . p = "" ;
53+
54+ /******/ // Load entry module and return exports
55+ /******/ return __webpack_require__ ( 0 ) ;
56+ /******/ } )
57+ /************************************************************************/
58+ /******/ ( [
59+ /* 0 */
60+ /***/ function ( module , exports , __webpack_require__ ) {
61+
62+ var React = __webpack_require__ ( 1 ) ;
63+
64+ var $ = React . DOM ;
65+ var JsonTable = React . createClass ( {
66+ defaultSettings : {
67+ header : true ,
68+ noRowsMessage : 'No items'
69+ } ,
70+
71+ getSetting : function ( name ) {
72+ var settings = this . props . settings ;
73+
74+ if ( ! settings || typeof settings [ name ] == 'undefined' )
75+ return this . defaultSettings [ name ] ;
76+
77+ return settings [ name ] ;
78+ } ,
79+
80+ render : function ( ) {
81+ var cols = this . normalizeColumns ( ) ,
82+ contents = [ this . renderRows ( cols ) ]
83+ ;
84+
85+ if ( this . getSetting ( 'header' ) )
86+ contents . unshift ( this . renderHeader ( cols ) ) ;
87+
88+ return $ . table ( { className : "jsonTable" } , contents ) ;
89+ } ,
90+
91+ renderHeader : function ( cols ) {
92+ var me = this ,
93+ cells = cols . map ( function ( col ) {
94+ return $ . th (
95+ { className : 'jsonColumn' , key : col . key , onClick : me . onClickHeader , "data-key" : col . key } ,
96+ col . label
97+ ) ;
98+ } )
99+ ;
100+
101+ return $ . thead ( { key : 'th' } ,
102+ $ . tr ( { className : 'jsonHeader' , key : 'h' , "data-key" : 'header' } , cells )
103+ ) ;
104+ } ,
105+
106+ renderRows : function ( cols ) {
107+ var me = this ,
108+ items = this . props . items ,
109+ i = 1
110+ ;
111+
112+ if ( ! items || ! items . length )
113+ return $ . tbody ( { } , [ $ . tr ( { } , $ . td ( { } , this . getSetting ( 'noRowsMessage' ) ) ) ] ) ;
114+
115+ var rows = items . map ( function ( item ) {
116+ return React . createElement ( Row , {
117+ key : me . getKey ( item ) ,
118+ item : item ,
119+ columns : cols ,
120+ i : i ++ ,
121+ onClickRow : me . onClickItem ,
122+ onClickCell : me . onClickCell
123+ } ) ;
124+ } ) ;
125+
126+ return $ . tbody ( { } , rows ) ;
127+ } ,
128+
129+ getItemField : function ( item , field ) {
130+ return item [ field ] ;
131+ } ,
132+
133+ normalizeColumns : function ( ) {
134+ var getItemField = this . getItemField ,
135+ cols = this . props . columns ,
136+ items = this . props . items
137+ ;
138+
139+ if ( ! cols ) {
140+ if ( ! items || ! items . length )
141+ return [ ] ;
142+
143+ return Object . keys ( items [ 0 ] ) . map ( function ( key ) {
144+ return { key : key , label : key , cell : getItemField } ;
145+ } ) ;
146+ }
147+
148+ return cols . map ( function ( col ) {
149+ var key ;
150+ if ( typeof col == 'string' ) {
151+ return {
152+ key : col ,
153+ label : col ,
154+ cell : getItemField
155+ } ;
156+ }
157+
158+ if ( typeof col == 'object' ) {
159+ key = col . key || col . label ;
160+
161+ // This is about get default column definition
162+ // we use label as key if not defined
163+ // we use key as label if not defined
164+ // we use getItemField as cell function if not defined
165+ return {
166+ key : key ,
167+ label : col . label || key ,
168+ cell : col . cell || getItemField
169+ } ;
170+ }
171+
172+ return {
173+ key : 'unknown' ,
174+ name :'unknown' ,
175+ cell : 'Unknown'
176+ } ;
177+ } ) ;
178+ } ,
179+
180+ getKey : function ( item ) {
181+ var field = this . props . settings && this . props . settings . keyField ;
182+ if ( field && item [ field ] )
183+ return item [ field ] ;
184+
185+ if ( item . id )
186+ return item . id ;
187+
188+ if ( item . _id )
189+ return item . _id ;
190+ } ,
191+
192+ shouldComponentUpdate : function ( ) {
193+ return true ;
194+ } ,
195+
196+ onClickItem : function ( e , item ) {
197+ if ( this . props . onClickItem ) {
198+ this . props . onClickItem ( e , item ) ;
199+ }
200+ } ,
201+
202+ onClickHeader : function ( e ) {
203+ if ( this . props . onClickHeader ) {
204+ this . props . onClickHeader ( e , e . target . dataset . key ) ;
205+ }
206+ } ,
207+
208+ onClickCell : function ( e , key , item ) {
209+ if ( this . props . onClickCell ) {
210+ this . props . onClickCell ( e , key , item ) ;
211+ }
212+ }
213+ } ) ;
214+
215+ var Row = React . createClass ( {
216+ render : function ( ) {
217+ var me = this ,
218+ cells = this . props . columns . map ( function ( col ) {
219+ var content = col . cell ,
220+ key = col . key
221+ ;
222+
223+ if ( typeof content == 'function' )
224+ content = content ( me . props . item , key ) ;
225+
226+ return $ . td ( {
227+ className : 'jsonCell' ,
228+ key : key ,
229+ "data-key" : key ,
230+ onClick : me . onClickCell
231+ } , content ) ;
232+ } )
233+ ;
234+
235+ var className = 'jsonRow json' ;
236+ if ( this . props . i % 2 )
237+ className += 'Odd' ;
238+ else
239+ className += 'Even' ;
240+
241+ return $ . tr ( {
242+ className : className ,
243+ onClick : me . onClickRow
244+ } , cells ) ;
245+ } ,
246+
247+ onClickCell : function ( e ) {
248+ this . props . onClickCell ( e , e . target . dataset . key , this . props . item ) ;
249+ } ,
250+
251+ onClickRow : function ( e ) {
252+ this . props . onClickRow ( e , this . props . item ) ;
253+ }
254+ } ) ;
255+
256+ module . exports = JsonTable ;
257+
258+
259+ /***/ } ,
260+ /* 1 */
261+ /***/ function ( module , exports , __webpack_require__ ) {
262+
263+ module . exports = __WEBPACK_EXTERNAL_MODULE_1__ ;
264+
265+ /***/ }
266+ /******/ ] )
267+ } ) ;
268+ ;
0 commit comments