Skip to content

Commit bba40ee

Browse files
committed
DRY'ed minicharts/index.js. Offline detection.
1 parent 58e9c09 commit bba40ee

File tree

2 files changed

+46
-65
lines changed

2 files changed

+46
-65
lines changed

src/minicharts/d3fns/geo.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ var minicharts_d3fns_geo = function() {
207207
],
208208
// calback
209209
function(err, results) {
210-
debug('err/res', err, results);
211210
if (err) {
212211
debug('Error: Google map could not be loaded, disabling feature', el);
213212
// google map load timed out, disable geo feature for runtime remainder and reload
@@ -324,12 +323,11 @@ var minicharts_d3fns_geo = function() {
324323
// need to fit the map bounds after view became visible
325324
var fieldView = options.view.parent.parent;
326325
if (fieldView.parent.parent.modelType === 'FieldView') {
327-
debug('we are in a nested field, wait until it expands...');
328326
// we're inside a nested list, wait until expanded
329327
var parentFieldView = fieldView.parent.parent;
330328
parentFieldView.on('change:expanded', function(view, value) {
331329
if (value) {
332-
debug('field %s expanded. fitting map.', view.model.name);
330+
// debug('field %s expanded. fitting map.', view.model.name);
333331
_.defer(function() {
334332
google.maps.event.trigger(googleMap, 'resize');
335333
googleMap.fitBounds(bounds);

src/minicharts/index.js

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var ArrayRootMinichartView = require('./array-root');
99
var vizFns = require('./d3fns');
1010
var QueryBuilderMixin = require('./querybuilder');
1111
var Collection = require('ampersand-collection');
12+
var navigator = window.navigator;
1213

1314
// var debug = require('debug')('scout:minicharts:index');
1415

@@ -59,79 +60,61 @@ module.exports = AmpersandView.extend(QueryBuilderMixin, {
5960
return false;
6061
},
6162
/* eslint complexity: 0 */
62-
render: function() {
63-
var isGeo = false;
63+
_geoCoordinateCheck: function() {
6464
var coords;
65+
if (!app.isFeatureEnabled('Geo Minicharts')) return false;
66+
if (!navigator.onLine) return false;
6567

68+
if (this.model.name === 'Document') {
69+
if (this.model.fields.length !== 2
70+
|| !this.model.fields.get('type')
71+
|| this.model.fields.get('type').type !== 'String'
72+
|| this.model.fields.get('type').types.get('String').unique !== 1
73+
|| this.model.fields.get('type').types.get('String').values.at(0).value !== 'Point'
74+
|| this.model.fields.get('coordinates').types.get('Array').count
75+
!== this.model.fields.get('coordinates').count
76+
|| this.model.fields.get('coordinates').types.get('Array').average_length !== 2
77+
) return false;
78+
coords = this._mangleGeoCoordinates(
79+
this.model.fields.get('coordinates').types.get('Array')
80+
.types.get('Number').values.serialize());
81+
if (!coords) return false;
82+
// we have a GeoJSON document: {type: "Point", coordinates: [lng, lat]}
83+
this.model.values = coords;
84+
this.model.fields.reset();
85+
return true;
86+
} else if (this.model.name === 'Array') {
87+
var lengths = this.model.lengths;
88+
if (_.min(lengths) !== 2 || _.max(lengths) !== 2) return false;
89+
coords = this._mangleGeoCoordinates(
90+
this.model.types.get('Number').values.serialize());
91+
if (!coords) return false;
92+
// we have a legacy coordinate pair: [lng, lat]
93+
this.model.values = coords;
94+
return true;
95+
}
96+
return false;
97+
},
98+
render: function() {
6699
this.renderWithTemplate(this);
67-
68-
if (['String', 'Number'].indexOf(this.model.name) !== -1
100+
if (this._geoCoordinateCheck()) {
101+
this.viewOptions.renderMode = 'html';
102+
this.viewOptions.height = 250;
103+
this.viewOptions.vizFn = vizFns.geo;
104+
this.subview = new VizView(this.viewOptions);
105+
} else if (['String', 'Number'].indexOf(this.model.name) !== -1
69106
&& this.model.unique === this.model.count) {
70107
// unique values get a div-based UniqueMinichart
71108
this.viewOptions.renderMode = 'html';
72109
this.viewOptions.vizFn = null;
73110
this.viewOptions.className = 'minichart unique';
74111
this.subview = new UniqueMinichartView(this.viewOptions);
75112
} else if (this.model.name === 'Document') {
76-
// are these coordinates? Do a basic check for now, until we support semantic schema types
77-
// here we check for GeoJSON form: { loc: {type: "Point", "coordinates": [47.80, 9.63] } }
78-
if (app.isFeatureEnabled('Geo Minicharts')) {
79-
if (this.model.fields.length === 2
80-
&& this.model.fields.get('type')
81-
&& this.model.fields.get('type').type === 'String'
82-
&& this.model.fields.get('type').types.get('String').unique === 1
83-
&& this.model.fields.get('type').types.get('String').values.at(0).value === 'Point'
84-
&& this.model.fields.get('coordinates').types.get('Array').count
85-
=== this.model.fields.get('coordinates').count
86-
&& this.model.fields.get('coordinates').types.get('Array').average_length === 2
87-
) {
88-
coords = this._mangleGeoCoordinates(
89-
this.model.fields.get('coordinates').types.get('Array')
90-
.types.get('Number').values.serialize());
91-
if (coords) {
92-
this.model.values = coords;
93-
this.model.fields.reset();
94-
isGeo = true;
95-
}
96-
}
97-
}
98-
if (isGeo) {
99-
// coordinates get an HTML-based d3 VizView with `coordinates` vizFn
100-
this.viewOptions.renderMode = 'html';
101-
this.viewOptions.height = 250;
102-
this.viewOptions.vizFn = vizFns.geo;
103-
this.subview = new VizView(this.viewOptions);
104-
} else {
105-
// nested objects get a div-based DocumentRootMinichart
106-
this.viewOptions.height = 55;
107-
this.subview = new DocumentRootMinichartView(this.viewOptions);
108-
}
113+
this.viewOptions.height = 55;
114+
this.subview = new DocumentRootMinichartView(this.viewOptions);
109115
} else if (this.model.name === 'Array') {
110-
isGeo = false;
111-
if (app.isFeatureEnabled('Geo Minicharts')) {
112-
// are these coordinates? Do a basic check for now, until we support semantic schema types
113-
// here we check for legacy coordinates in array form: { loc: [47.80, 9.63] }
114-
var lengths = this.model.lengths;
115-
if (_.min(lengths) === 2 && _.max(lengths) === 2) {
116-
coords = this._mangleGeoCoordinates(
117-
this.model.types.get('Number').values.serialize());
118-
if (coords) {
119-
this.model.values = coords;
120-
isGeo = true;
121-
}
122-
}
123-
}
124-
if (isGeo) {
125-
// coordinates get an HTML-based d3 VizView with `coordinates` vizFn
126-
this.viewOptions.renderMode = 'html';
127-
this.viewOptions.height = 250;
128-
this.viewOptions.vizFn = vizFns.geo;
129-
this.subview = new VizView(this.viewOptions);
130-
} else {
131-
// plain arrays get a div-based ArrayRootMinichart
132-
this.viewOptions.height = 55;
133-
this.subview = new ArrayRootMinichartView(this.viewOptions);
134-
}
116+
this.viewOptions.height = 55;
117+
this.subview = new ArrayRootMinichartView(this.viewOptions);
135118
} else {
136119
// otherwise, create a svg-based VizView for d3
137120
this.subview = new VizView(this.viewOptions);

0 commit comments

Comments
 (0)