Skip to content

Commit

Permalink
Fixes #15, No special template is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
asafdav committed Jun 2, 2014
1 parent 74bc075 commit 3191652
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 48 deletions.
5 changes: 1 addition & 4 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-csv",
"version": "0.2.0",
"version": "0.2.1",
"main": "build/ng-csv.min.js",
"dependencies": {
"angular": "~1",
Expand All @@ -9,8 +9,5 @@
"devDependencies": {
"angular-mocks": "~1",
"angular-scenario": "~1"
},
"resolutions": {
"angular": "~1"
}
}
48 changes: 28 additions & 20 deletions build/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ angular.module('ngCsv.services').
var def = $q.defer();

var that = this;
var csvContent = "data:text/csv;charset=utf-8,";
var csv;
var csvContent;

if(window.navigator.msSaveOrOpenBlob) {
csvContent = "";
}else{
csvContent = "data:text/csv;charset=utf-8,";
}

var dataPromise = $q.when(data).then(function (responseData)
{
Expand Down Expand Up @@ -111,7 +117,11 @@ angular.module('ngCsv.services').
csvContent += index < arrData.length ? dataString + "\n" : dataString;
});

csv = encodeURI(csvContent);
if(window.navigator.msSaveOrOpenBlob) {
csv = csvContent;
}else{
csv = encodeURI(csvContent);
}
def.resolve(csv);
});

Expand All @@ -133,8 +143,6 @@ angular.module('ngCsv.directives').
directive('ngCsv', ['$parse', '$q', 'CSV', function ($parse, $q, CSV) {
return {
restrict: 'AC',
replace: true,
transclude: true,
scope: {
data:'&ngCsv',
filename:'@filename',
Expand Down Expand Up @@ -196,31 +204,31 @@ angular.module('ngCsv.directives').
};
}
],
template: '<div class="csv-wrap">' +
'<div class="element" ng-transclude></div>' +
'<a class="hidden-link" ng-hide="true" ng-href="{{ csv }}" download="{{ getFilename() }}"></a>' +
'</div>',
link: function (scope, element, attrs) {
var subject = angular.element(element.children()[0]),
link = angular.element(element.children()[1]);

function doClick() {
link[0].href = "";
link[0].click();
link[0].href = scope.csv;
link[0].click();
if(window.navigator.msSaveOrOpenBlob) {
var blob = new Blob([scope.csv],{
type: "text/csv;charset=utf-8;"
});
navigator.msSaveBlob(blob, scope.getFilename());
} else {
var downloadLink = document.createElement("a");
downloadLink.href = scope.csv;
downloadLink.download = scope.getFilename();

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}

}

subject.bind('click', function (e)
element.bind('click', function (e)
{
scope.buildCSV().then(function(csv) {
doClick();
});
scope.$apply();

if (!!scope.ngClick) {
scope.ngClick();
}
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions build/ng-csv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions example/lazy_load.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ <h1>ngCsv <small>example with lazy load</small></h1>
};

$scope.getHeader = function () {return ["A", "B"]};

$scope.clickFn = function() {
console.log("click click click");
};
});
</script>

Expand Down
27 changes: 9 additions & 18 deletions src/ng-csv/directives/ng-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ angular.module('ngCsv.directives').
directive('ngCsv', ['$parse', '$q', 'CSV', function ($parse, $q, CSV) {
return {
restrict: 'AC',
replace: true,
transclude: true,
scope: {
data:'&ngCsv',
filename:'@filename',
Expand Down Expand Up @@ -71,38 +69,31 @@ angular.module('ngCsv.directives').
};
}
],
template: '<div class="csv-wrap">' +
'<div class="element" ng-transclude></div>' +
'<a class="hidden-link" ng-hide="true" ng-href="{{ csv }}" download="{{ getFilename() }}"></a>' +
'</div>',
link: function (scope, element, attrs) {
var subject = angular.element(element.children()[0]),
link = angular.element(element.children()[1]);

function doClick() {
link[0].href = "";
link[0].click();
if(window.navigator.msSaveOrOpenBlob) {
var blob = new Blob([scope.csv],{
type: "text/csv;charset=utf-8;"
});
navigator.msSaveBlob(blob, scope.getFilename());
} else {
link[0].href = scope.csv;
link[0].click();
var downloadLink = document.createElement("a");
downloadLink.href = scope.csv;
downloadLink.download = scope.getFilename();

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}

}

subject.bind('click', function (e)
element.bind('click', function (e)
{
scope.buildCSV().then(function(csv) {
doClick();
});
scope.$apply();

if (!!scope.ngClick) {
scope.ngClick();
}
});
}
};
Expand Down

0 comments on commit 3191652

Please sign in to comment.