]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/file.js
webstaff: add egJsonExporter directive
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / file.js
1 /**
2  * File upload reader.
3  * http://stackoverflow.com/questions/17063000/ng-model-for-input-type-file
4  *
5  * After reading, the contents will be available in the scope variable
6  * referred to by container="..."
7  */
8
9 angular.module('egCoreMod')
10 .directive("egFileReader", [function () {
11     return {
12         scope: {
13             container: "="
14         },
15         link: function (scope, element, attributes) {
16             // TODO: support DataURL, etc. via attrs
17             element.bind("change", function (changeEvent) {
18                 var reader = new FileReader();
19                 reader.onload = function (loadEvent) {
20                     scope.$apply(function () {
21                         scope.container = loadEvent.target.result;
22                     });
23                 }
24                 reader.readAsText(changeEvent.target.files[0]);
25             });
26         }
27     }
28 }])
29
30 .directive('egJsonExporter', ['FileSaver', 'Blob', function(FileSaver, Blob) {
31     return {
32         scope: {
33             container: '=',
34             defaultFileName: '='
35         },
36         link: function (scope, element, attributes) {
37             element.bind('click', function (clickEvent) {
38                 var data = new Blob([JSON.stringify(scope.container)], {type : 'application/json'});
39                 FileSaver.saveAs(data, scope.defaultFileName);
40             });
41         }
42     }
43 }])
44 ;