Skip to content

Commit d3a39cd

Browse files
imlucaskangas
authored andcommitted
INT-404: Make everything fast and synchronized
1 parent 0cd38d1 commit d3a39cd

File tree

5 files changed

+75
-69
lines changed

5 files changed

+75
-69
lines changed

src/app.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,6 @@ var Application = View.extend({
6262
events: {
6363
'click a': 'onLinkClick'
6464
},
65-
derived: {
66-
/**
67-
* Based on the active connection, this is how models will talk to `scout-server`.
68-
* @see scout-client
69-
*/
70-
client: {
71-
deps: ['connection.uri'],
72-
fn: function() {
73-
var c = createClient({
74-
seed: this.connection.uri
75-
});
76-
debug('created scout client', c);
77-
return c;
78-
}
79-
}
80-
},
8165
/**
8266
* We have what we need, we can now start our router and show the appropriate page!
8367
*/

src/field-list/index.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var View = require('ampersand-view');
22
var TypeListView = require('./type-list');
33
var MinichartView = require('../minicharts');
4-
var FieldCollection = require('mongodb-schema').FieldCollection;
54
var ViewSwitcher = require('ampersand-view-switcher');
65
var $ = require('jquery');
6+
var debug = require('debug')('scout:field-list');
77
var _ = require('lodash');
8+
var SampledSchema = require('../models/sampled-schema');
89

910
function handleCaret(el) {
1011
var $el = $(el);
@@ -20,11 +21,15 @@ function handleCaret(el) {
2021
var FieldListView;
2122

2223
var FieldView = View.extend({
23-
props: {
24-
minichartModel: 'state',
24+
session: {
2525
expanded: {
2626
type: 'boolean',
2727
default: false
28+
},
29+
type_model: 'state',
30+
visible: {
31+
type: 'boolean',
32+
default: false
2833
}
2934
},
3035
bindings: {
@@ -43,6 +48,10 @@ var FieldView = View.extend({
4348
type: 'booleanClass',
4449
yes: 'expanded',
4550
no: 'collapsed'
51+
},
52+
visible: {
53+
type: 'booleanClass',
54+
no: 'hidden'
4655
}
4756
},
4857
events: {
@@ -52,11 +61,12 @@ var FieldView = View.extend({
5261
subviews: {
5362
types: {
5463
hook: 'types-subview',
64+
waitFor: 'visible',
5565
prepareView: function(el) {
5666
return new TypeListView({
5767
el: el,
5868
parent: this,
59-
collection: this.model.types
69+
collection: this.model.types.sort()
6070
});
6171
}
6272
},
@@ -84,50 +94,55 @@ var FieldView = View.extend({
8494
}
8595
},
8696
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);
9398
},
9499
render: function() {
95100
this.renderWithTemplate(this);
96101
this.viewSwitcher = new ViewSwitcher(this.queryByHook('minichart-container'));
97-
if (this.model.types.length > 0) {
98-
this.switchView(this.model.types.at(0));
99-
}
100102
},
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);
106105

107-
this.minichartModel = typeModel;
106+
debug('setting miniview for type_model_id `%s`', this.type_model.getId());
108107
var miniview = new MinichartView({
109-
model: typeModel
108+
model: this.type_model
110109
});
111110
this.viewSwitcher.set(miniview);
111+
112+
// _.each(this._subviews, function(subview) {
113+
// subview.visible = true;
114+
// debugger;
115+
// });
112116
},
113117
click: function(evt) {
114-
// @todo: persist state of open nodes
115118
this.toggle('expanded');
116-
117119
evt.preventDefault();
118120
evt.stopPropagation();
119-
return false;
120121
}
121122
});
122123

123124
FieldListView = View.extend({
124-
collections: {
125-
collection: FieldCollection
125+
session: {
126+
field_collection_view: 'view'
126127
},
127128
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+
},
128142
render: function() {
129143
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'));
131146
}
132147
});
133148

src/field-list/type-list-item.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
var View = require('ampersand-view');
22
var format = require('util').format;
3-
var _ = require('lodash');
43
var numeral = require('numeral');
54
var tooltipMixin = require('../tooltip-mixin');
6-
var $ = require('jquery');
75

86
module.exports = View.extend(tooltipMixin, {
97
template: require('./type-list-item.jade'),
@@ -19,35 +17,46 @@ module.exports = View.extend(tooltipMixin, {
1917
el.classList.add('schema-field-type-' + this.model.getId().toLowerCase());
2018
}
2119
}
22-
]
20+
],
21+
probability_percentage: {
22+
hook: 'bar',
23+
type: function(el) {
24+
el.style.width = this.probability_percentage;
25+
}
26+
},
27+
tooltip_message: {
28+
type: function() {
29+
// need to set `title` and `data-original-title` due to bug in bootstrap's tooltip
30+
// @see https://github.com/twbs/bootstrap/issues/14769
31+
this.tooltip({
32+
title: this.tooltip_message
33+
}).attr('data-original-title', this.tooltip_message);
34+
}
35+
}
36+
},
37+
derived: {
38+
probability_percentage: {
39+
deps: ['model.probability'],
40+
fn: function() {
41+
return numeral(this.model.probability).format('%');
42+
}
43+
},
44+
tooltip_message: {
45+
deps: ['probability_percentage'],
46+
fn: function() {
47+
return format('%s (%s)', this.model.getId(), this.probability_percentage);
48+
}
49+
}
2350
},
2451
events: {
2552
'click .schema-field-wrapper': 'typeClicked'
2653
},
27-
initialize: function() {
28-
this.listenTo(this.model, 'change:probability', _.debounce(this.update.bind(this), 300));
29-
},
30-
update: function() {
31-
var tooltext = format('%s (%s)', this.model.getId(),
32-
numeral(this.model.probability).format('%'));
33-
// need to set `title` and `data-original-title` due to bug in bootstrap's tooltip
34-
// @see https://github.com/twbs/bootstrap/issues/14769
35-
this.tooltip({
36-
title: tooltext
37-
}).attr('data-original-title', tooltext);
38-
$(this.queryByHook('bar')).css({
39-
width: Math.floor(this.model.probability * 100) + '%'
40-
});
41-
},
4254
typeClicked: function() {
43-
var fieldList = this.parent.parent;
44-
if (!fieldList.minichartModel || fieldList.minichartModel.modelType !== this.model.modelType) {
45-
fieldList.switchView(this.model);
46-
}
55+
var fieldView = this.parent.parent;
56+
fieldView.type_model = this.model;
57+
fieldView.renderMinicharts();
4758
},
4859
render: function() {
4960
this.renderWithTemplate(this);
50-
this.update();
51-
return this;
5261
}
5362
});

src/field-list/type-list.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ var TypeListItem = require('./type-list-item');
44
module.exports = View.extend({
55
template: require('./type-list.jade'),
66
render: function() {
7-
this.renderWithTemplate({});
8-
this.collection.sort();
7+
this.renderWithTemplate();
98
this.renderCollection(this.collection, TypeListItem, this.queryByHook('types'));
109
}
1110
});

src/home/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ var HomeView = View.extend({
1818
}
1919
},
2020
initialize: function() {
21-
app.statusbar.watch(this, app.instance);
2221
this.listenTo(app.instance, 'sync', this.onInstanceFetched);
2322
this.listenTo(app.connection, 'change:name', this.updateTitle);
2423
this.once('change:rendered', this.onRendered);

0 commit comments

Comments
 (0)