From 59385900a23a0681de9e3e643df0b4bf19264d48 Mon Sep 17 00:00:00 2001 From: Jonas Fischer Date: Fri, 11 Dec 2015 15:13:15 +0100 Subject: [PATCH] Update angular-csv-import.js Wraped strictly executed code into a imidiately executed closure as it is commonly done in todays javascript libraries. This prefents issues from happening if the framework will be used in a probjekt in combinations with a bundleing framework and other javascript libararies that aren't marked as strict. Had an issue with that resulting in 'use strict' affecting the entire bundled script file which leaded to trouble when using firefox. --- lib/angular-csv-import.js | 202 +++++++++++++++++++------------------- 1 file changed, 102 insertions(+), 100 deletions(-) diff --git a/lib/angular-csv-import.js b/lib/angular-csv-import.js index 01912eb..721e8f7 100644 --- a/lib/angular-csv-import.js +++ b/lib/angular-csv-import.js @@ -1,109 +1,111 @@ -'use strict'; - -var csvImport = angular.module('ngCsvImport', []); - -csvImport.directive('ngCsvImport', function() { - return { - restrict: 'E', - transclude: true, - replace: true, - scope:{ - content:'=?', - header: '=?', - headerVisible: '=?', - separator: '=?', - separatorVisible: '=?', - result: '=?', - encoding: '=?', - encodingVisible: '=?', - accept: '=?' - }, - template: '
'+ - '
Header
'+ - '
Encoding
{{encoding}}
'+ - '
'+ - '
Seperator
'+ - ''+ - '
'+ - '
'+ - '
', - link: function(scope, element) { - scope.separatorVisible = scope.separatorVisible || false; - scope.headerVisible = scope.headerVisible || false; - - angular.element(element[0].querySelector('.separator-input')).on('keyup', function(e) { - if ( scope.content != null ) { - var content = { - csv: scope.content, - header: scope.header, - separator: e.target.value, - encoding: scope.encoding - }; - scope.result = csvToJSON(content); - scope.$apply(); - } - }); - - element.on('change', function(onChangeEvent) { - var reader = new FileReader(); - scope.filename = onChangeEvent.target.files[0].name; - reader.onload = function(onLoadEvent) { - scope.$apply(function() { - var content = { - csv: onLoadEvent.target.result.replace(/\r\n|\r/g,'\n'), - header: scope.header, - separator: scope.separator - }; - scope.content = content.csv; - scope.result = csvToJSON(content); - scope.result.filename = scope.filename; - }); - }; - - if ( (onChangeEvent.target.type === "file") && (onChangeEvent.target.files != null || onChangeEvent.srcElement.files != null) ) { - reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0], scope.encoding); - } else { +(function(undefined) { + 'use strict'; + + var csvImport = angular.module('ngCsvImport', []); + + csvImport.directive('ngCsvImport', function() { + return { + restrict: 'E', + transclude: true, + replace: true, + scope:{ + content:'=?', + header: '=?', + headerVisible: '=?', + separator: '=?', + separatorVisible: '=?', + result: '=?', + encoding: '=?', + encodingVisible: '=?', + accept: '=?' + }, + template: '
'+ + '
Header
'+ + '
Encoding
{{encoding}}
'+ + '
'+ + '
Seperator
'+ + ''+ + '
'+ + '
'+ + '
', + link: function(scope, element) { + scope.separatorVisible = scope.separatorVisible || false; + scope.headerVisible = scope.headerVisible || false; + + angular.element(element[0].querySelector('.separator-input')).on('keyup', function(e) { if ( scope.content != null ) { var content = { csv: scope.content, - header: !scope.header, - separator: scope.separator + header: scope.header, + separator: e.target.value, + encoding: scope.encoding }; scope.result = csvToJSON(content); + scope.$apply(); } - } - }); - - var csvToJSON = function(content) { - var lines=content.csv.split('\n'); - var result = []; - var start = 0; - var columnCount = lines[0].split(content.separator).length; - - var headers = []; - if (content.header) { - headers=lines[0].split(content.separator); - start = 1; - } - - for (var i=start; i