webstaff: add egJsonExporter directive
authorGalen Charlton <gmc@esilibrary.com>
Mon, 25 Jan 2016 23:30:48 +0000 (18:30 -0500)
committerKathy Lussier <klussier@masslnc.org>
Tue, 2 Feb 2016 19:58:53 +0000 (14:58 -0500)
This directive is used to allow a piece of JSON
to be saved to the user's filesystem.  For example:

<span eg-json-exporter container="foo" default-file-name="example.json">Export</span>

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 <gmc@esilibrary.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/templates/staff/base_js.tt2
Open-ILS/web/js/ui/default/staff/Gruntfile.js
Open-ILS/web/js/ui/default/staff/bower.json
Open-ILS/web/js/ui/default/staff/services/core.js
Open-ILS/web/js/ui/default/staff/services/file.js

index 4601e9c..db4f85a 100644 (file)
@@ -8,6 +8,7 @@
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/build/js/angular-route.min.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/build/js/ui-bootstrap-tpls.min.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/build/js/hotkeys.min.js"></script>
+<script src="[% ctx.media_prefix %]/js/ui/default/staff/build/js/angular-file-saver.bundle.min.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/build/js/angular-location-update.min.js"></script>
 
 <!-- IDL / opensrf (network) -->
index 087f0ff..2c0d289 100644 (file)
@@ -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',
           ]
index db8e900..fd1c566 100644 (file)
@@ -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"
   }
 }
index e0ef021..94c4b46 100644 (file)
@@ -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']);
index 8dc0c2b..473f4fe 100644 (file)
@@ -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);
+            });
+        }
+    }
+}])
+;