Skip to content

Commit

Permalink
update build
Browse files Browse the repository at this point in the history
  • Loading branch information
iyel committed Feb 2, 2015
1 parent 7eac40d commit ba282f9
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 15 deletions.
12 changes: 6 additions & 6 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ path = require 'path'
module.exports = (grunt) ->
grunt.initConfig
cmpnt: grunt.file.readJSON('bower.json'),
banner: '/*! ngTableExport v<%= cmpnt.version %> by Vitalii Savchuk(esvit666@gmail.com) - ' +
'https://github.com/esvit/ng-table-export - New BSD License */\n',
banner: '/*! ngTableExport v<%= cmpnt.version %> by iyel (https://github.com/iyel/) - ' +
'https://github.com/iyel/ng-table-export - New BSD License */\n',

# Deletes built file and temp directories.
clean:
Expand All @@ -19,19 +19,19 @@ module.exports = (grunt) ->
uglify:
# concat js files before minification
js:
src: ['ng-table-export.src.js']
dest: 'ng-table-export.js'
src: ['./dist/ng-table-export.js']
dest: './dist/ng-table-export.min.js'
options:
banner: '<%= banner %>'
sourceMap: (fileName) ->
fileName.replace /\.js$/, '.map'
fileName +='.map'
concat:
# concat js files before minification
js:
src: [
'src/scripts/*.js'
]
dest: 'ng-table-export.src.js'
dest: './dist/ng-table-export.js'

grunt.loadNpmTasks 'grunt-contrib-clean'
grunt.loadNpmTasks 'grunt-contrib-copy'
Expand Down
29 changes: 20 additions & 9 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
{
"name": "ng-table-export",
"version": "0.1.0",
"main": [
"ng-table-export.js"
"version": "0.0.4",
"homepage": "https://github.com/iyel/ng-table-export",
"authors": [
"iyel <ilyatrueone@gmail.com>"
],
"ignore": [
"src"
"description": "Export to CSV format for ng-table",
"main": "./dist/ng-table-export.min.js",
"keywords": [
"ng-table",
"ng-table",
"export"
],
"dependencies": {
"angular": ">=1.2.0-rc.2"
}
}
"license": "New BSD License",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
128 changes: 128 additions & 0 deletions dist/ng-table-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
angular.module('ngTableExport', [])
.config(['$compileProvider', function($compileProvider) {
// allow data links
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|data):/);
}])
.directive('exportCsv', ['$parse', '$timeout', function ($parse, $timeout) {

var delimiter = '\t';
var header = 'data:text/csv;charset=UTF-8,';

return {
restrict: 'A',
scope: false,

/**
* scope is table scope, element is <table>
*/
link: function(scope, element, attrs) {

var data = '';

// allow pass in of delimiter via directive attrs
if (attrs.delimiter) { delimiter = attrs.delimiter; }

function stringify(str) {
return '"' +
str.replace(/^\s\s*/, '').replace(/\s*\s$/, '') // trim spaces
.replace(/"/g,'""') + // replace quotes with double quotes
'"';
}

/**
* Parse the table and build up data uri
*/
function parseTable() {
data = '';
var rows = element.find('tr');
angular.forEach(rows, function(row, i) {
var tr = angular.element(row),
tds = tr.find('th'),
rowData = '';
if (tr.hasClass('ng-table-filters')) {
return;
}
if (tds.length === 0) {
tds = tr.find('td');
}
if (i !== 1) {
angular.forEach(tds, function(td) {
// respect colspan in row data
rowData += stringify(angular.element(td).text()) + Array.apply(null, Array(td.colSpan)).map(function () { return delimiter; }).join('');
});
rowData = rowData.slice(0, rowData.length - 1); //remove last semicolon
}
data += rowData + '\n';
});
// add delimiter hint for excel so it opens without having to import
data = 'sep=' + delimiter + '\n' + data;
}

/**
* Dynamically generate a link and click it; works in chrome + firefox; unfortunately, safari
* does not support the `download` attribute, so it ends up opening the file in a new tab https://bugs.webkit.org/show_bug.cgi?id=102914
*/
function download(dataUri, filename) {
// tested in chrome / firefox / safari
var link = document.createElement('a');
// chrome + firefox
link.style.display = 'none';
link.href = dataUri;
link.download = filename;
link.target = '_blank';
// needs to get wrapped to play nicely with angular $digest
// else may cause '$digest already in progress' errors with other angular controls (e.g. angular-ui dropdown)
$timeout(function () {
// must append to body for firefox; chrome & safari don't mind
document.body.appendChild(link);
link.click();
// destroy
document.body.removeChild(link);
}, 0, false);
}

var csv = {
/**
* Generate data URI from table data
*/
generate: function(event, filename) {

var table = scope.params,
settings = table.settings(),
cnt = table.count(),
total = settings.total;

// is pager on? if so, we have to disable it temporarily
if (cnt < total) {
var $off = settings.$scope.$on('ngTableAfterReloadData', function () {
// de-register callback so it won't continue firing
$off();
// give browser some time to re-render; FIXME - no good way to know when rendering is done?
$timeout(function () {
// generate data from table
parseTable();
// finally, restore original table cnt
table.count(cnt);
table.reload();
// dynamically trigger download
download(header + encodeURIComponent(data), filename);
}, 1000, false);
});

// disable the pager and reload the table so we get all the data
table.count(Infinity);
table.reload();

} else {
// pager isn't on, just parse the table
parseTable();
download(header + encodeURIComponent(data), filename);
}
}
};

// attach csv to table scope
$parse(attrs.exportCsv).assign(scope.$parent, csv);
}
};
}]);
4 changes: 4 additions & 0 deletions dist/ng-table-export.min.js

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

1 change: 1 addition & 0 deletions dist/ng-table-export.min.js.map

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

0 comments on commit ba282f9

Please sign in to comment.