@@ -3,45 +3,90 @@ var TypeListView = require('./type-list');
33var MinichartView = require ( '../minicharts' ) ;
44var FieldCollection = require ( 'mongodb-schema' ) . FieldCollection ;
55var ViewSwitcher = require ( 'ampersand-view-switcher' ) ;
6+ var debug = require ( 'debug' ) ( 'scout-ui:field-list:index' ) ;
7+ var $ = require ( 'jquery' ) ;
68var _ = require ( 'lodash' ) ;
79
8- var BasicFieldView = View . extend ( {
10+ function handleCaret ( el , value , previousValue ) {
11+ var $el = $ ( el ) ;
12+ // only apply to own caret, not children carets
13+ if ( $el . next ( ) . text ( ) !== this . model . name ) return ;
14+ if ( this . model . fields || this . model . arrayFields ) {
15+ $el . addClass ( 'caret' ) ;
16+ } else {
17+ $el . removeClass ( 'caret' ) ;
18+ }
19+ }
20+
21+ var FieldView = View . extend ( {
922 props : {
10- minichartModel : 'state'
23+ minichartModel : 'state' ,
24+ expanded : {
25+ type : 'boolean' ,
26+ default : false
27+ }
1128 } ,
1229 bindings : {
13- 'model.name' : [
14- {
15- hook : 'name'
16- } ,
17- {
18- hook : 'name' ,
19- type : function ( el ) {
20- if ( this . model . getId ( ) === '__basic__' ) {
21- el . classList . add ( 'hidden' ) ;
22- }
23- }
24- }
25- ]
30+ 'model.name' : {
31+ hook : 'name'
32+ } ,
33+ 'model.fields' : {
34+ type : handleCaret ,
35+ hook : 'caret'
36+ } ,
37+ 'model.arrayFields' : {
38+ type : handleCaret ,
39+ hook : 'caret'
40+ } ,
41+ 'expanded' : {
42+ type : 'booleanClass' ,
43+ yes : 'expanded' ,
44+ no : 'collapsed'
45+ }
2646 } ,
27- template : require ( './basic-field.jade' ) ,
47+ events : {
48+ 'click .schema-field-name' : 'click' ,
49+ } ,
50+ template : require ( './field.jade' ) ,
2851 subviews : {
2952 types : {
30- hook : 'types-container ' ,
53+ hook : 'types-subview ' ,
3154 prepareView : function ( el ) {
3255 return new TypeListView ( {
3356 el : el ,
3457 parent : this ,
3558 collection : this . model . types
3659 } ) ;
3760 }
61+ } ,
62+ fields : {
63+ hook : 'fields-subview' ,
64+ waitFor : 'model.fields' ,
65+ prepareView : function ( el ) {
66+ return new FieldListView ( {
67+ el : el ,
68+ parent : this ,
69+ collection : this . model . fields
70+ } ) ;
71+ }
72+ } ,
73+ arrayFields : {
74+ hook : 'arrayfields-subview' ,
75+ waitFor : 'model.arrayFields' ,
76+ prepareView : function ( el ) {
77+ return new FieldListView ( {
78+ el : el ,
79+ parent : this ,
80+ collection : this . model . arrayFields
81+ } ) ;
82+ }
3883 }
3984 } ,
4085 initialize : function ( ) {
4186 var that = this ;
4287 // debounce prevents excessive rendering
43- this . model . values . on ( 'add ' , _ . debounce ( function ( evt ) {
44- // for now pick first type, @todo: make the type bars clickable and toggle chart
88+ this . model . on ( 'change:count ' , _ . debounce ( function ( ) {
89+ // pick first type initially
4590 that . switchView ( that . model . types . at ( 0 ) ) ;
4691 } , 300 ) ) ;
4792 } ,
@@ -63,28 +108,6 @@ var BasicFieldView = View.extend({
63108 model : typeModel ,
64109 } ) ;
65110 this . viewSwitcher . set ( miniview ) ;
66- }
67- } ) ;
68-
69- var ExpandableFieldMixin = {
70- bindings : {
71- 'model.name' : {
72- hook : 'name'
73- } ,
74- 'expanded' : {
75- type : 'booleanClass' ,
76- yes : 'expanded' ,
77- no : 'collapsed'
78- }
79- } ,
80- events : {
81- 'click .schema-field-name' : 'click' ,
82- } ,
83- props : {
84- expanded : {
85- type : 'boolean' ,
86- default : false
87- }
88111 } ,
89112 click : function ( evt ) {
90113 // @todo : persist state of open nodes
@@ -93,27 +116,7 @@ var ExpandableFieldMixin = {
93116 evt . preventDefault ( ) ;
94117 evt . stopPropagation ( ) ;
95118 return false ;
96- } ,
97- subviews : {
98- fields : {
99- hook : 'fields-container' ,
100- prepareView : function ( el ) {
101- return new FieldListView ( {
102- el : el ,
103- parent : this ,
104- collection : this . model . fields
105- } ) ;
106- }
107- }
108119 }
109- } ;
110-
111- var EmbeddedArrayFieldView = View . extend ( ExpandableFieldMixin , {
112- template : require ( './array-field.jade' )
113- } ) ;
114-
115- var EmbeddedDocumentFieldView = View . extend ( ExpandableFieldMixin , {
116- template : require ( './object-field.jade' )
117120} ) ;
118121
119122var FieldListView = View . extend ( {
@@ -123,16 +126,7 @@ var FieldListView = View.extend({
123126 template : require ( './index.jade' ) ,
124127 render : function ( ) {
125128 this . renderWithTemplate ( ) ;
126- this . renderCollection ( this . collection , function ( options ) {
127- var type = options . model . type ;
128- if ( type === 'Array' ) {
129- return new EmbeddedArrayFieldView ( options ) ;
130- }
131- if ( type === 'Object' ) {
132- return new EmbeddedDocumentFieldView ( options ) ;
133- }
134- return new BasicFieldView ( options ) ;
135- } , this . queryByHook ( 'fields' ) ) ;
129+ this . renderCollection ( this . collection , FieldView , this . queryByHook ( 'fields' ) ) ;
136130 }
137131} ) ;
138132
0 commit comments