@@ -3,45 +3,90 @@ var TypeListView = require('./type-list');
3
3
var MinichartView = require ( '../minicharts' ) ;
4
4
var FieldCollection = require ( 'mongodb-schema' ) . FieldCollection ;
5
5
var ViewSwitcher = require ( 'ampersand-view-switcher' ) ;
6
+ var debug = require ( 'debug' ) ( 'scout-ui:field-list:index' ) ;
7
+ var $ = require ( 'jquery' ) ;
6
8
var _ = require ( 'lodash' ) ;
7
9
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 ( {
9
22
props : {
10
- minichartModel : 'state'
23
+ minichartModel : 'state' ,
24
+ expanded : {
25
+ type : 'boolean' ,
26
+ default : false
27
+ }
11
28
} ,
12
29
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
+ }
26
46
} ,
27
- template : require ( './basic-field.jade' ) ,
47
+ events : {
48
+ 'click .schema-field-name' : 'click' ,
49
+ } ,
50
+ template : require ( './field.jade' ) ,
28
51
subviews : {
29
52
types : {
30
- hook : 'types-container ' ,
53
+ hook : 'types-subview ' ,
31
54
prepareView : function ( el ) {
32
55
return new TypeListView ( {
33
56
el : el ,
34
57
parent : this ,
35
58
collection : this . model . types
36
59
} ) ;
37
60
}
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
+ }
38
83
}
39
84
} ,
40
85
initialize : function ( ) {
41
86
var that = this ;
42
87
// 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
45
90
that . switchView ( that . model . types . at ( 0 ) ) ;
46
91
} , 300 ) ) ;
47
92
} ,
@@ -63,28 +108,6 @@ var BasicFieldView = View.extend({
63
108
model : typeModel ,
64
109
} ) ;
65
110
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
- }
88
111
} ,
89
112
click : function ( evt ) {
90
113
// @todo : persist state of open nodes
@@ -93,27 +116,7 @@ var ExpandableFieldMixin = {
93
116
evt . preventDefault ( ) ;
94
117
evt . stopPropagation ( ) ;
95
118
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
- }
108
119
}
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' )
117
120
} ) ;
118
121
119
122
var FieldListView = View . extend ( {
@@ -123,16 +126,7 @@ var FieldListView = View.extend({
123
126
template : require ( './index.jade' ) ,
124
127
render : function ( ) {
125
128
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' ) ) ;
136
130
}
137
131
} ) ;
138
132
0 commit comments