@@ -9,6 +9,7 @@ var ArrayRootMinichartView = require('./array-root');
9
9
var vizFns = require ( './d3fns' ) ;
10
10
var QueryBuilderMixin = require ( './querybuilder' ) ;
11
11
var Collection = require ( 'ampersand-collection' ) ;
12
+ var navigator = window . navigator ;
12
13
13
14
// var debug = require('debug')('scout:minicharts:index');
14
15
@@ -59,79 +60,61 @@ module.exports = AmpersandView.extend(QueryBuilderMixin, {
59
60
return false ;
60
61
} ,
61
62
/* eslint complexity: 0 */
62
- render : function ( ) {
63
- var isGeo = false ;
63
+ _geoCoordinateCheck : function ( ) {
64
64
var coords ;
65
+ if ( ! app . isFeatureEnabled ( 'Geo Minicharts' ) ) return false ;
66
+ if ( ! navigator . onLine ) return false ;
65
67
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 ( ) {
66
99
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
69
106
&& this . model . unique === this . model . count ) {
70
107
// unique values get a div-based UniqueMinichart
71
108
this . viewOptions . renderMode = 'html' ;
72
109
this . viewOptions . vizFn = null ;
73
110
this . viewOptions . className = 'minichart unique' ;
74
111
this . subview = new UniqueMinichartView ( this . viewOptions ) ;
75
112
} 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 ) ;
109
115
} 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 ) ;
135
118
} else {
136
119
// otherwise, create a svg-based VizView for d3
137
120
this . subview = new VizView ( this . viewOptions ) ;
0 commit comments