1
1
var View = require ( 'ampersand-view' ) ;
2
2
var TypeListView = require ( './type-list' ) ;
3
3
var MinichartView = require ( '../minicharts' ) ;
4
- var FieldCollection = require ( 'mongodb-schema' ) . FieldCollection ;
5
4
var ViewSwitcher = require ( 'ampersand-view-switcher' ) ;
6
5
var $ = require ( 'jquery' ) ;
6
+ var debug = require ( 'debug' ) ( 'scout:field-list' ) ;
7
7
var _ = require ( 'lodash' ) ;
8
+ var SampledSchema = require ( '../models/sampled-schema' ) ;
8
9
9
10
function handleCaret ( el ) {
10
11
var $el = $ ( el ) ;
@@ -20,11 +21,15 @@ function handleCaret(el) {
20
21
var FieldListView ;
21
22
22
23
var FieldView = View . extend ( {
23
- props : {
24
- minichartModel : 'state' ,
24
+ session : {
25
25
expanded : {
26
26
type : 'boolean' ,
27
27
default : false
28
+ } ,
29
+ type_model : 'state' ,
30
+ visible : {
31
+ type : 'boolean' ,
32
+ default : false
28
33
}
29
34
} ,
30
35
bindings : {
@@ -43,6 +48,10 @@ var FieldView = View.extend({
43
48
type : 'booleanClass' ,
44
49
yes : 'expanded' ,
45
50
no : 'collapsed'
51
+ } ,
52
+ visible : {
53
+ type : 'booleanClass' ,
54
+ no : 'hidden'
46
55
}
47
56
} ,
48
57
events : {
@@ -52,11 +61,12 @@ var FieldView = View.extend({
52
61
subviews : {
53
62
types : {
54
63
hook : 'types-subview' ,
64
+ waitFor : 'visible' ,
55
65
prepareView : function ( el ) {
56
66
return new TypeListView ( {
57
67
el : el ,
58
68
parent : this ,
59
- collection : this . model . types
69
+ collection : this . model . types . sort ( )
60
70
} ) ;
61
71
}
62
72
} ,
@@ -84,50 +94,55 @@ var FieldView = View.extend({
84
94
}
85
95
} ,
86
96
initialize : function ( ) {
87
- var that = this ;
88
- // debounce prevents excessive rendering
89
- this . model . on ( 'change:count' , _ . debounce ( function ( ) {
90
- // pick first type initially
91
- that . switchView ( that . model . types . at ( 0 ) ) ;
92
- } , 300 ) ) ;
97
+ this . listenTo ( this , 'change:visible' , this . renderMinicharts ) ;
93
98
} ,
94
99
render : function ( ) {
95
100
this . renderWithTemplate ( this ) ;
96
101
this . viewSwitcher = new ViewSwitcher ( this . queryByHook ( 'minichart-container' ) ) ;
97
- if ( this . model . types . length > 0 ) {
98
- this . switchView ( this . model . types . at ( 0 ) ) ;
99
- }
100
102
} ,
101
- switchView : function ( typeModel ) {
102
- var type = typeModel . getId ( ) . toLowerCase ( ) ;
103
-
104
- // @todo currently only support boolean, number, date, category
105
- if ( [ 'objectid' , 'boolean' , 'number' , 'date' , 'string' ] . indexOf ( type ) === - 1 ) return ;
103
+ renderMinicharts : function ( ) {
104
+ this . type_model = this . type_model || this . model . types . at ( 0 ) ;
106
105
107
- this . minichartModel = typeModel ;
106
+ debug ( 'setting miniview for type_model_id `%s`' , this . type_model . getId ( ) ) ;
108
107
var miniview = new MinichartView ( {
109
- model : typeModel
108
+ model : this . type_model
110
109
} ) ;
111
110
this . viewSwitcher . set ( miniview ) ;
111
+
112
+ // _.each(this._subviews, function(subview) {
113
+ // subview.visible = true;
114
+ // debugger;
115
+ // });
112
116
} ,
113
117
click : function ( evt ) {
114
- // @todo : persist state of open nodes
115
118
this . toggle ( 'expanded' ) ;
116
-
117
119
evt . preventDefault ( ) ;
118
120
evt . stopPropagation ( ) ;
119
- return false ;
120
121
}
121
122
} ) ;
122
123
123
124
FieldListView = View . extend ( {
124
- collections : {
125
- collection : FieldCollection
125
+ session : {
126
+ field_collection_view : 'view'
126
127
} ,
127
128
template : require ( './index.jade' ) ,
129
+ initialize : function ( ) {
130
+ if ( this . collection . parent instanceof SampledSchema ) {
131
+ this . listenTo ( this . collection . parent , 'sync' , this . makeFieldVisible ) ;
132
+ } else {
133
+ this . listenTo ( this . parent , 'change:visible' , this . makeFieldVisible ) ;
134
+ }
135
+ } ,
136
+ makeFieldVisible : function ( ) {
137
+ var views = this . field_collection_view . views ;
138
+ _ . each ( views , function ( field_view ) {
139
+ field_view . visible = true ;
140
+ } ) ;
141
+ } ,
128
142
render : function ( ) {
129
143
this . renderWithTemplate ( ) ;
130
- this . renderCollection ( this . collection , FieldView , this . queryByHook ( 'fields' ) ) ;
144
+ this . field_collection_view = this . renderCollection ( this . collection ,
145
+ FieldView , this . queryByHook ( 'fields' ) ) ;
131
146
}
132
147
} ) ;
133
148
0 commit comments