@@ -3,14 +3,22 @@ var format = require('util').format;
3
3
var numeral = require ( 'numeral' ) ;
4
4
var tooltipMixin = require ( '../tooltip-mixin' ) ;
5
5
var _ = require ( 'lodash' ) ;
6
- // var debug = require('debug')('scout:field-list:type-list');
6
+ var debug = require ( 'debug' ) ( 'scout:field-list:type-list' ) ;
7
7
8
8
var TypeListView ;
9
9
10
10
var TypeListItem = View . extend ( tooltipMixin , {
11
11
template : require ( './type-list-item.jade' ) ,
12
- namespace : 'TypeListItem' ,
12
+ modelType : 'TypeListItem' ,
13
13
bindings : {
14
+ active : {
15
+ type : 'booleanClass' ,
16
+ name : 'active'
17
+ } ,
18
+ selected : {
19
+ type : 'booleanClass' ,
20
+ name : 'selected'
21
+ } ,
14
22
'model.name' : [
15
23
{
16
24
hook : 'name'
@@ -34,13 +42,22 @@ var TypeListItem = View.extend(tooltipMixin, {
34
42
// @see https://github.com/twbs/bootstrap/issues/14769
35
43
this . tooltip ( {
36
44
title : this . tooltip_message ,
37
- placement : this . hasSubtype ? 'bottom' : 'top'
45
+ placement : this . hasSubtype ? 'bottom' : 'top' ,
46
+ container : 'body'
38
47
} ) . attr ( 'data-original-title' , this . tooltip_message ) ;
39
48
}
40
49
}
41
50
} ,
42
- props : {
43
- parent : 'state'
51
+ session : {
52
+ parent : 'state' ,
53
+ active : {
54
+ type : 'boolean' ,
55
+ default : false
56
+ } ,
57
+ selected : {
58
+ type : 'boolean' ,
59
+ default : false
60
+ }
44
61
} ,
45
62
derived : {
46
63
probability_percentage : {
@@ -55,10 +72,10 @@ var TypeListItem = View.extend(tooltipMixin, {
55
72
return format ( '%s (%s)' , this . model . getId ( ) , numeral ( this . model . probability ) . format ( '%' ) ) ;
56
73
}
57
74
} ,
58
- hasSubtype : {
75
+ isSubtype : {
59
76
deps : [ 'parent' ] ,
60
77
fn : function ( ) {
61
- return this . parent . hasSubtype ;
78
+ return this . parent . hasSubtypes ;
62
79
}
63
80
}
64
81
} ,
@@ -73,48 +90,68 @@ var TypeListItem = View.extend(tooltipMixin, {
73
90
return new TypeListView ( {
74
91
el : el ,
75
92
parent : this ,
76
- hasSubtype : true ,
93
+ hasSubtypes : true ,
77
94
collection : this . model . types
78
95
} ) ;
79
96
}
80
97
}
81
98
} ,
99
+ initialize : function ( ) {
100
+ this . on ( 'change:active' , this . activeChanged ) ;
101
+ } ,
82
102
typeClicked : function ( evt ) {
83
103
evt . stopPropagation ( ) ;
84
104
85
- // no clicks on Undefined allowed
86
- if ( this . model . getId ( ) === 'Undefined' ) return ;
87
-
88
- // find the field view, at most 2 levels up
89
- var fieldView = this . parent . parent ;
90
- if ( fieldView . getType ( ) !== 'FieldView' ) {
91
- fieldView = fieldView . parent . parent ;
92
- }
105
+ if ( this . active ) {
106
+ // already active, query building mode
107
+ this . toggle ( 'selected' ) ;
108
+ } else {
109
+ // no clicks on Undefined allowed
110
+ if ( this . model . getId ( ) === 'Undefined' ) return ;
93
111
94
- // if type model has changed, render its minichart
95
- if ( fieldView . type_model !== this . model ) {
96
- fieldView . type_model = this . model ;
97
- fieldView . renderMinicharts ( ) ;
112
+ // find the field view, at most 2 levels up
113
+ var fieldView = this . parent . parent ;
114
+ if ( fieldView . getType ( ) !== 'FieldView' ) {
115
+ fieldView = fieldView . parent . parent ;
116
+ }
117
+ // if type model has changed, render its minichart
118
+ if ( fieldView . type_model !== this . model ) {
119
+ this . active = true ;
120
+ fieldView . type_model = this . model ;
121
+ fieldView . renderMinicharts ( ) ;
122
+ }
98
123
}
99
124
} ,
100
- render : function ( ) {
101
- this . renderWithTemplate ( this ) ;
125
+ activeChanged : function ( view , value ) {
126
+ debug ( 'active changed to %s for %s -> %s' , value , view . model . parent . name , view . model . name ) ;
102
127
}
103
128
} ) ;
104
129
105
130
106
131
TypeListView = module . exports = View . extend ( {
107
- props : {
108
- hasSubtype : {
132
+ modelType : 'TypeListView' ,
133
+ session : {
134
+ collectionView : 'object' ,
135
+ hasSubtypes : {
109
136
type : 'boolean' ,
110
137
default : false
111
- }
138
+ } ,
139
+ parent : 'state'
112
140
} ,
113
141
template : require ( './type-list.jade' ) ,
142
+ deactivateOthers : function ( view ) {
143
+ if ( ! this . collectionView ) return ;
144
+ _ . each ( this . collectionView . views , function ( typeView ) {
145
+ if ( view !== typeView ) {
146
+ typeView . active = false ;
147
+ typeView . selected = false ;
148
+ }
149
+ } ) ;
150
+ } ,
114
151
render : function ( ) {
115
- if ( ! _ . get ( this , 'parent.hasSubtype' ) ) {
116
- this . renderWithTemplate ( this ) ;
117
- this . renderCollection ( this . collection , TypeListItem , this . queryByHook ( 'types' ) ) ;
118
- }
152
+ this . renderWithTemplate ( this ) ;
153
+ this . collectionView = this . renderCollection ( this . collection , TypeListItem ,
154
+ this . queryByHook ( 'types' ) ) ;
155
+ this . collectionView . views [ 0 ] . active = true ;
119
156
}
120
157
} ) ;
0 commit comments