1
+ /*globals CSVToGeocoder Papa*/
2
+ 'use strict' ;
1
3
var CSVToGeocoder = function ( options ) {
2
4
options = options || { } ;
3
5
options . i18n = options . i18n || { } ;
4
6
5
7
var _ = function ( k ) {
6
- return options . i18n [ k ] || k ;
8
+ return options . i18n [ k ] || k ;
7
9
} ;
8
10
9
11
var createNode = function ( what , attrs , parent , content ) {
10
12
var el = document . createElement ( what ) ;
11
- for ( var attr in attrs || { } ) el [ attr ] = attrs [ attr ] ;
13
+ for ( var attr in attrs || { } ) el [ attr ] = attrs [ attr ] ;
12
14
if ( typeof parent !== 'undefined' ) parent . appendChild ( el ) ;
13
15
if ( content ) {
14
16
if ( content . nodeType && content . nodeType === 1 ) el . appendChild ( content ) ;
15
17
else el . innerHTML = content ;
16
18
}
17
19
return el ;
18
20
} ;
21
+ CSVToGeocoder . createNode = createNode ;
22
+
19
23
var hasClass = function ( el , name ) {
20
24
return el . className . length && new RegExp ( '(^|\\s)' + name + '(\\s|$)' ) . test ( el . className ) ;
21
25
} ;
@@ -42,21 +46,22 @@ var CSVToGeocoder = function (options) {
42
46
createNode ( 'h2' , { } , container , '1. ' + _ ( 'Choose a file' ) ) ;
43
47
var fileInput = createNode ( 'input' , { type : 'file' , id : 'fileInput' } , container ) ;
44
48
var holder = createNode ( 'div' , { id : 'holder' } , container , _ ( 'Drag your file here' ) + ', ' + _ ( 'or' ) + ' <a id="browseLink" href="#">' + _ ( 'browse' ) + '</a>' ) ;
45
- createNode ( 'h2' , { className : 'step' , id : 'next' } , container , '2. ' + _ ( 'Preview the file and check encoding' ) ) ;
46
- var step2 = createNode ( 'div' , { className : 'step2 ' } , container ) ;
49
+ createNode ( 'h2' , { className : 'step-title ' , id : 'next' } , container , '2. ' + _ ( 'Preview the file and check encoding' ) ) ;
50
+ var step2 = createNode ( 'div' , { className : 'step ' } , container ) ;
47
51
var previewContainer = createNode ( 'table' , { className : 'preview' } , step2 ) ;
48
52
var helpEncoding = createNode ( 'p' , { id : 'helpEncoding' } , step2 , _ ( 'If you see weird characters in the preview, you can try with another encoding' ) + ': ' ) ;
49
53
var selectEncoding = createNode ( 'select' , { } , helpEncoding ) ;
50
54
for ( var i = 0 ; i < options . encodings . length ; i ++ ) {
51
55
createNode ( 'option' , { value : options . encodings [ i ] } , selectEncoding , options . encodings [ i ] ) ;
52
56
}
53
- createNode ( 'h2' , { className : 'step' } , container , '3. ' + _ ( 'Choose the columns you want to use to compute the addresses' ) ) ;
54
- var step3 = createNode ( 'div' , { className : 'step3' } , container ) ;
55
- var helpColumns = createNode ( 'p' , { id : 'helpColumns' } , step3 , _ ( 'Drag or click on a column to select it. You can then drag the selected columns to reorder them.' ) ) ;
57
+ createNode ( 'h2' , { className : 'step-title' } , container , '3. ' + _ ( 'Choose the columns you want to use to compute the addresses' ) ) ;
58
+ var step3 = createNode ( 'div' , { className : 'step' } , container ) ;
56
59
var availableColumns = createNode ( 'ul' , { id : 'availableColumns' } , step3 ) ;
57
60
var chosenColumns = createNode ( 'ul' , { id : 'chosenColumns' } , step3 ) ;
58
- var errorContainer = createNode ( 'div' , { className : 'error' } , container ) ;
59
- var submitButton = createNode ( 'input' , { type : 'button' , value : _ ( 'Geocode' ) , disabled : 'disabled' } , step3 ) ;
61
+ if ( options . onBuild ) options . onBuild ( { container : container } ) ;
62
+ var lastStep = createNode ( 'div' , { className : 'step' } , container ) ;
63
+ var submitButton = createNode ( 'input' , { type : 'button' , value : _ ( 'Geocode' ) , disabled : 'disabled' } , lastStep ) ;
64
+ var errorContainer = createNode ( 'div' , { className : 'error' } , lastStep ) ;
60
65
61
66
var error = function ( message ) {
62
67
if ( options . onError ) {
@@ -75,7 +80,7 @@ var CSVToGeocoder = function (options) {
75
80
var progressBar = createNode ( 'progress' , { } , container ) ;
76
81
progressBar . max = 100 ;
77
82
var xhr = new XMLHttpRequest ( ) ;
78
- xhr . open ( 'POST' , options . postURL || '.' ) ;
83
+ xhr . open ( 'POST' , options . postURL || '.' ) ;
79
84
xhr . overrideMimeType ( 'text/csv' ) ;
80
85
var columns = document . querySelectorAll ( '#chosenColumns li' ) ;
81
86
var formData = new FormData ( ) ;
@@ -85,6 +90,7 @@ var CSVToGeocoder = function (options) {
85
90
formData . append ( 'data' , blob , file . name ) ;
86
91
formData . append ( 'encoding' , getEncoding ( ) ) ;
87
92
formData . append ( 'delimiter' , parsed . meta . delimiter ) ;
93
+ if ( options . onSubmit ) options . onSubmit ( { form : formData } ) ;
88
94
xhr . onreadystatechange = function ( ) {
89
95
if ( xhr . readyState === 4 ) {
90
96
progressBar . parentNode . removeChild ( progressBar ) ;
@@ -106,7 +112,7 @@ var CSVToGeocoder = function (options) {
106
112
}
107
113
} , false ) ;
108
114
xhr . upload . addEventListener ( 'load' , function ( ) {
109
- progressBar . removeAttribute ( 'value' ) ; // Switch to undeterminate state.
115
+ progressBar . removeAttribute ( 'value' ) ; // Switch to undeterminate state.
110
116
} , false ) ;
111
117
xhr . send ( formData ) ;
112
118
return false ;
@@ -159,6 +165,7 @@ var CSVToGeocoder = function (options) {
159
165
blob = new Blob ( [ reader . result ] , { type : 'text/csv; charset=' + getEncoding ( ) } ) ;
160
166
if ( ! hasClass ( container , 'active' ) ) addClass ( container , 'active' ) ;
161
167
window . location . hash = '#next' ;
168
+ if ( options . onFileLoad ) options . onFileLoad ( { file : blob , headers : headers } ) ;
162
169
} ;
163
170
var populatePreview = function ( ) {
164
171
previewContainer . innerHTML = '' ;
@@ -205,7 +212,7 @@ var CSVToGeocoder = function (options) {
205
212
var el = document . getElementById ( e . dataTransfer . getData ( 'text/plain' ) ) ;
206
213
this . parentNode . insertBefore ( el , this ) ;
207
214
} ;
208
- var onColumnClick = function ( e ) {
215
+ var onColumnClick = function ( ) {
209
216
this . className = '' ;
210
217
var from , to ;
211
218
if ( this . parentNode === chosenColumns ) {
@@ -218,18 +225,18 @@ var CSVToGeocoder = function (options) {
218
225
from . removeChild ( this ) ;
219
226
to . appendChild ( this ) ;
220
227
} ;
221
- var onColumnDragOver = function ( e ) {
228
+ var onColumnDragOver = function ( ) {
222
229
this . className = 'hover' ;
223
230
} ;
224
- var onColumnDragLeave = function ( e ) {
231
+ var onColumnDragLeave = function ( ) {
225
232
this . className = '' ;
226
233
} ;
227
234
var onFileInputChange = function ( e ) {
228
235
stop ( e ) ;
229
236
file = this . files [ 0 ] ;
230
237
processFile ( ) ;
231
238
} ;
232
- var onSelectEncodingChange = function ( e ) {
239
+ var onSelectEncodingChange = function ( ) {
233
240
processFile ( ) ;
234
241
} ;
235
242
var listenBrowseLink = function ( ) {
@@ -243,7 +250,7 @@ var CSVToGeocoder = function (options) {
243
250
var downloadFileName = function ( ) {
244
251
// As we are in CORS ajax, we can't access the Content-Disposition header,
245
252
// so built the file name from here.
246
- var name = file . name || 'file' ;
253
+ var name = file . name || 'file' ;
247
254
if ( name . indexOf ( '.csv' , name . length - 4 ) !== - 1 ) name = name . slice ( 0 , name . length - 4 ) ;
248
255
return name + '.geocoded.csv' ;
249
256
} ;
0 commit comments