browser staff : more print stuff
authorBill Erickson <berick@esilibrary.com>
Tue, 8 Apr 2014 21:33:35 +0000 (17:33 -0400)
committerBill Erickson <berick@esilibrary.com>
Tue, 8 Apr 2014 21:33:35 +0000 (17:33 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/staff/services/printstore.js

index a2166a7..c924f43 100644 (file)
@@ -6,7 +6,9 @@
 angular.module('egCoreMod')
 
 
-.factory('egPrintStore', ['$q', '$window', function($q, $window) {
+.factory('egPrintStore', 
+           ['$q','$window','$timeout', 
+    function($q , $window , $timeout) {
 
     var service = {};
     service.msgId = 0;
@@ -33,8 +35,27 @@ angular.module('egCoreMod')
         return msg.deferred.promise;
     }
 
+    // if we are unable to connect to Hatch, handle messages locally
+    service.redirectPendingMessages = function() {
+        service.socket = null;
+        service.hatchAvailable = false;
+        angular.forEach(service.pending, function(msg) {
+            switch(msg.action) {
+                case 'print':
+                    return service.browserPrint(msg.mime, msg.value);
+            }
+            delete service.pending[msg.msgid];
+        });
+    }
+
     service.hatchConnect = function() {
-        service.socket = new WebSocket(service.hatchURL);
+
+        try {
+            service.socket = new WebSocket(service.hatchURL);
+        } catch(e) {
+            service.redirectPendingMessages();
+            return;
+        }
 
         service.socket.onopen = function() {
             console.debug('connected to Hatch');
@@ -47,13 +68,13 @@ angular.module('egCoreMod')
         service.socket.onclose = function() {
             console.debug("disconnected from Hatch");
             service.socket = null;
+            service.hatchAvailable = null; // reset
         }
 
         service.socket.onerror = function() {
-            console.log(
+            console.debug(
                 "unable to connect to Hatch server at " + service.hatchURL);
-            service.hatchAvailable = false;
-            service.socket = null;
+            service.redirectPendingMessages();
         }
 
         service.socket.onmessage = function(evt) {
@@ -79,7 +100,29 @@ angular.module('egCoreMod')
         }
     }
 
-    service.browserPrinter = function(mime, data) {
+    service.browserPrint = function(mime, data) {
+        
+        // TODO: cleanup / angularize
+
+        console.log(data);
+        var w = window.open(
+            'data:text/plain;charset=utf-8,' + encodeURIComponent(data));
+
+        w.addEventListener('DOMContentLoaded', function(e) {
+            w.print();
+            $timeout(
+                function(){
+                    console.log(w.matchMedia('print'));
+                    w.close()
+                },
+                // TODO:
+                // http://stackoverflow.com/questions/18325025/how-to-detect-window-print-finish
+                3000
+            );
+        }, false);
+
+        return;
+
         switch (mime) {
             case 'text/csv':
             case 'text/plain':