diff --git a/build/ng-csv.js b/build/ng-csv.js index 2d7c915..757440b 100644 --- a/build/ng-csv.js +++ b/build/ng-csv.js @@ -42,54 +42,59 @@ angular.module('ngCsv.directives', []). filename:'@filename', header: '&csvHeader', txtDelim: '@textDelimiter', - fieldSep: '@fieldSeparator' + fieldSep: '@fieldSeparator', + ngClick: '&' }, - controller: ['$scope', '$element', '$attrs', '$transclude', function ( - $scope, $element, $attrs, $transclude - ) { - $scope.csv = ''; + controller: [ + '$scope', + '$element', + '$attrs', + '$transclude', + function ($scope, $element, $attrs, $transclude) { + $scope.csv = ''; - $scope.$watch(function (newValue) { - $scope.buildCsv(); - }, true); + $scope.$watch(function (newValue) { + $scope.buildCsv(); + }, true); - $scope.buildCsv = function () { - var data = $scope.data(); - var csvContent = 'data:text/csv;charset=utf-8,'; + $scope.buildCsv = function () { + var data = $scope.data(); + var csvContent = 'data:text/csv;charset=utf-8,'; - // Check if there's a provided header array - var header = $scope.header(); - if (header) { - var encodingArray = []; - angular.forEach(header, function (title) { - this.push(title); - }, encodingArray); + // Check if there's a provided header array + var header = $scope.header(); + if (header) { + var encodingArray = []; + angular.forEach(header, function (title) { + this.push(title); + }, encodingArray); - var headerString = encodingArray.join($scope.fieldSep || ','); - csvContent += headerString + '\r\n'; - } + var headerString = encodingArray.join($scope.fieldSep || ','); + csvContent += headerString + '\r\n'; + } - // Process the data - angular.forEach(data, function (row, index) { - var infoArray = []; - angular.forEach(row, function (field) { - if (typeof field === 'string' && $scope.txtDelim) { - field = $scope.txtDelim + field + $scope.txtDelim; - } - this.push(field); - }, infoArray); - dataString = infoArray.join($scope.fieldSep || ','); - csvContent += index < data.length ? dataString + '\r\n' : dataString; - }); + // Process the data + angular.forEach(data, function (row, index) { + var infoArray = []; + angular.forEach(row, function (field) { + if (typeof field === 'string' && $scope.txtDelim) { + field = $scope.txtDelim + field + $scope.txtDelim; + } + this.push(field); + }, infoArray); + dataString = infoArray.join($scope.fieldSep || ','); + csvContent += index < data.length ? dataString + '\r\n' : dataString; + }); - $scope.csv = encodeURI(csvContent); - return $scope.csv; - }; + $scope.csv = encodeURI(csvContent); + return $scope.csv; + }; - $scope.getFilename = function () { - return $scope.filename || 'download.csv'; - }; - }], + $scope.getFilename = function () { + return $scope.filename || 'download.csv'; + }; + } + ], template: '
' + '
' + '
',link:function(a,b){var c=angular.element(b.children()[0]),d=angular.element(b.children()[1]);c.bind("click",function(){d[0].click()})}}}])}(window,document); \ No newline at end of file +/*! ngcsv 17-01-2014 */ +!function(){angular.module("ngCsv.config",[]).value("ngCsv.config",{debug:!0}).config(["$compileProvider",function(a){angular.isDefined(a.urlSanitizationWhitelist)?a.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/):a.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/)}]),angular.module("ngCsv.directives",[]),angular.module("ngCsv",["ngCsv.config","ngCsv.directives","ngSanitize"]),angular.module("ngCsv.directives",[]).directive("ngCsv",["$parse",function(){return{restrict:"AC",replace:!0,transclude:!0,scope:{data:"&ngCsv",filename:"@filename",header:"&csvHeader",txtDelim:"@textDelimiter",fieldSep:"@fieldSeparator",ngClick:"&"},controller:["$scope","$element","$attrs","$transclude",function(a){a.csv="",a.$watch(function(){a.buildCsv()},!0),a.buildCsv=function(){var b=a.data(),c="data:text/csv;charset=utf-8,",d=a.header();if(d){var e=[];angular.forEach(d,function(a){this.push(a)},e);var f=e.join(a.fieldSep||",");c+=f+"\r\n"}return angular.forEach(b,function(d,e){var f=[];angular.forEach(d,function(b){"string"==typeof b&&a.txtDelim&&(b=a.txtDelim+b+a.txtDelim),this.push(b)},f),dataString=f.join(a.fieldSep||","),c+=e
',link:function(a,b){var c=angular.element(b.children()[0]),d=angular.element(b.children()[1]);c.bind("click",function(){d[0].click(),a.ngClick&&a.ngClick()})}}}])}(window,document); \ No newline at end of file diff --git a/example/index.html b/example/index.html index 317a55a..3026e46 100644 --- a/example/index.html +++ b/example/index.html @@ -4,9 +4,7 @@ - - - + @@ -29,10 +27,14 @@

ngCsv example

+ >Export to CSV + >Export to CSV with header + + @@ -47,8 +49,12 @@

ngCsv example

$scope.addRandomRow = function() { $scope.getArray.push({a: Math.floor((Math.random()*10)+1), b: Math.floor((Math.random()*10)+1)}); - } + }; $scope.getHeader = function () {return ["A", "B"]}; + + $scope.clickFn = function() { + console.log("click click click"); + }; }); diff --git a/src/ng-csv/directives/ng-csv.js b/src/ng-csv/directives/ng-csv.js index 27bdce3..0c8feb1 100644 --- a/src/ng-csv/directives/ng-csv.js +++ b/src/ng-csv/directives/ng-csv.js @@ -15,54 +15,59 @@ angular.module('ngCsv.directives', []). filename:'@filename', header: '&csvHeader', txtDelim: '@textDelimiter', - fieldSep: '@fieldSeparator' + fieldSep: '@fieldSeparator', + ngClick: '&' }, - controller: ['$scope', '$element', '$attrs', '$transclude', function ( - $scope, $element, $attrs, $transclude - ) { - $scope.csv = ''; + controller: [ + '$scope', + '$element', + '$attrs', + '$transclude', + function ($scope, $element, $attrs, $transclude) { + $scope.csv = ''; - $scope.$watch(function (newValue) { - $scope.buildCsv(); - }, true); + $scope.$watch(function (newValue) { + $scope.buildCsv(); + }, true); - $scope.buildCsv = function () { - var data = $scope.data(); - var csvContent = 'data:text/csv;charset=utf-8,'; + $scope.buildCsv = function () { + var data = $scope.data(); + var csvContent = 'data:text/csv;charset=utf-8,'; - // Check if there's a provided header array - var header = $scope.header(); - if (header) { - var encodingArray = []; - angular.forEach(header, function (title) { - this.push(title); - }, encodingArray); + // Check if there's a provided header array + var header = $scope.header(); + if (header) { + var encodingArray = []; + angular.forEach(header, function (title) { + this.push(title); + }, encodingArray); - var headerString = encodingArray.join($scope.fieldSep || ','); - csvContent += headerString + '\r\n'; - } + var headerString = encodingArray.join($scope.fieldSep || ','); + csvContent += headerString + '\r\n'; + } - // Process the data - angular.forEach(data, function (row, index) { - var infoArray = []; - angular.forEach(row, function (field) { - if (typeof field === 'string' && $scope.txtDelim) { - field = $scope.txtDelim + field + $scope.txtDelim; - } - this.push(field); - }, infoArray); - dataString = infoArray.join($scope.fieldSep || ','); - csvContent += index < data.length ? dataString + '\r\n' : dataString; - }); + // Process the data + angular.forEach(data, function (row, index) { + var infoArray = []; + angular.forEach(row, function (field) { + if (typeof field === 'string' && $scope.txtDelim) { + field = $scope.txtDelim + field + $scope.txtDelim; + } + this.push(field); + }, infoArray); + dataString = infoArray.join($scope.fieldSep || ','); + csvContent += index < data.length ? dataString + '\r\n' : dataString; + }); - $scope.csv = encodeURI(csvContent); - return $scope.csv; - }; + $scope.csv = encodeURI(csvContent); + return $scope.csv; + }; - $scope.getFilename = function () { - return $scope.filename || 'download.csv'; - }; - }], + $scope.getFilename = function () { + return $scope.filename || 'download.csv'; + }; + } + ], template: '
' + '
' + '
")($rootScope); + element.triggerHandler('click'); + + // fire all the watches, so the scope expression {{1 + 1}} will be evaluated + $rootScope.$digest(); + // Check that the compiled element contains the templated content + expect(element.html()).toContain('download="download.csv"'); + + // Check if clickTest was executed + expect($rootScope.clicked).toBeTruthy(); + }); + it('Sets the provided filename', function () { // Compile a piece of HTML containing the directive var element = $compile("
")($rootScope);