From 4ed811a38128a29512e3de0f9dd84857ebb17145 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Mon, 25 Jan 2016 18:30:48 -0500 Subject: [PATCH] webstaff: add egJsonExporter directive This directive is used to allow a piece of JSON to be saved to the user's filesystem. For example: Export specifies a button that, when click, allows the user to save the contents of the scope variable foo. The dialog that appears will use "example.json" as the default filename. This also adds a new dependency, the MIT-licensed angular-file-saver service written by Philipp Alferov: https://alferov.github.io/angular-file-saver/ Signed-off-by: Galen Charlton Signed-off-by: Kathy Lussier --- Open-ILS/src/templates/staff/base_js.tt2 | 1 + Open-ILS/web/js/ui/default/staff/Gruntfile.js | 1 + Open-ILS/web/js/ui/default/staff/bower.json | 3 ++- .../web/js/ui/default/staff/services/core.js | 2 +- .../web/js/ui/default/staff/services/file.js | 18 +++++++++++++++++- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/templates/staff/base_js.tt2 b/Open-ILS/src/templates/staff/base_js.tt2 index 4601e9c39b..db4f85a550 100644 --- a/Open-ILS/src/templates/staff/base_js.tt2 +++ b/Open-ILS/src/templates/staff/base_js.tt2 @@ -8,6 +8,7 @@ + diff --git a/Open-ILS/web/js/ui/default/staff/Gruntfile.js b/Open-ILS/web/js/ui/default/staff/Gruntfile.js index 087f0ff829..2c0d289cd2 100644 --- a/Open-ILS/web/js/ui/default/staff/Gruntfile.js +++ b/Open-ILS/web/js/ui/default/staff/Gruntfile.js @@ -22,6 +22,7 @@ module.exports = function(grunt) { 'bower_components/angular-bootstrap/ui-bootstrap.min.js', 'bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js', 'bower_components/angular-hotkeys/build/hotkeys.min.js', + 'bower_components/angular-file-saver/dist/angular-file-saver.bundle.min.js', 'bower_components/angular-location-update/angular-location-update.min.js', 'bower_components/jquery/dist/jquery.min.js', ] diff --git a/Open-ILS/web/js/ui/default/staff/bower.json b/Open-ILS/web/js/ui/default/staff/bower.json index db8e90037d..fd1c566719 100644 --- a/Open-ILS/web/js/ui/default/staff/bower.json +++ b/Open-ILS/web/js/ui/default/staff/bower.json @@ -27,6 +27,7 @@ }, "dependencies": { "angular-hotkeys": "chieffancypants/angular-hotkeys#~1.3.0", - "angular-location-update": "./extern/angular-location-update/" + "angular-location-update": "./extern/angular-location-update/", + "angular-file-saver": "~1.0.2" } } diff --git a/Open-ILS/web/js/ui/default/staff/services/core.js b/Open-ILS/web/js/ui/default/staff/services/core.js index e0ef02116e..94c4b46aac 100644 --- a/Open-ILS/web/js/ui/default/staff/services/core.js +++ b/Open-ILS/web/js/ui/default/staff/services/core.js @@ -3,4 +3,4 @@ * egCoreMod houses all of the services, etc. required by all pages * for basic functionality. */ -angular.module('egCoreMod', ['cfp.hotkeys']); +angular.module('egCoreMod', ['cfp.hotkeys', 'ngFileSaver']); diff --git a/Open-ILS/web/js/ui/default/staff/services/file.js b/Open-ILS/web/js/ui/default/staff/services/file.js index 8dc0c2be77..473f4feba6 100644 --- a/Open-ILS/web/js/ui/default/staff/services/file.js +++ b/Open-ILS/web/js/ui/default/staff/services/file.js @@ -25,4 +25,20 @@ angular.module('egCoreMod') }); } } -}]); +}]) + +.directive('egJsonExporter', ['FileSaver', 'Blob', function(FileSaver, Blob) { + return { + scope: { + container: '=', + defaultFileName: '=' + }, + link: function (scope, element, attributes) { + element.bind('click', function (clickEvent) { + var data = new Blob([JSON.stringify(scope.container)], {type : 'application/json'}); + FileSaver.saveAs(data, scope.defaultFileName); + }); + } + } +}]) +; -- 2.43.2