browser staff : print config UI cont.
authorBill Erickson <berick@esilibrary.com>
Mon, 21 Apr 2014 15:52:55 +0000 (11:52 -0400)
committerBill Erickson <berick@esilibrary.com>
Mon, 21 Apr 2014 15:52:55 +0000 (11:52 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/templates/staff/admin/workstation/t_printing.tt2
Open-ILS/web/js/ui/default/staff/admin/workstation/app.js
Open-ILS/web/js/ui/default/staff/services/printstore.js

index 86fd3fa..997d4a1 100644 (file)
@@ -1,4 +1,4 @@
-<div class="container">
+<div class="container" id="admin-workstation-printing">
 
   <style>
     /* TODO: more context and move me */
@@ -6,9 +6,10 @@
       height: 400px;
       width: 100%;
     }
-    .tab-pane {
+    .tab-pane .row {
       padding-top: 20px;
     }
+    h2 { margin-bottom: 15px }
       
   </style>
 
                     <span class="caret"></span></button>
                   <ul class="dropdown-menu">
                     <li ng-repeat="printer in printers">
-                      <a href="#">{{printer['printer-name']}}</a>
+                      <a href='' ng-click="applyConfAttr('name', printer['printer-name'])">
+                        {{printer['printer-name']}}
+                      </a>
                     </li>
                   </ul>
                 </div><!-- /btn-group -->
-                <input type="text" class="form-control" value="printe 1">
+                <input ng-if="!printers[0]" type="text" 
+                  class="form-control" disabled="disabled"
+                  value="[% l('No Printers Found') %]">
+                <input ng-if="printers[0] && !printConfig[context]" type="text" 
+                  class="form-control" disabled="disabled"
+                  value="[% l('No Printer Selected') %]">
+                <input ng-if="printConfig[context].name" type="text" 
+                  class="form-control" disabled="disabled"
+                  value="{{printConfig[context].name}}">
               </div><!-- /input-group -->
-            </div>
-          </div>
+            </div><!-- col -->
+          </div><!-- row -->
+
+          <!-- media selector -->
+          <div class="row">
+            <div class="col-md-6">
+              <div class="input-group">
+                <div class="input-group-btn">
+                  <button type="button" class="btn btn-default dropdown-toggle" 
+                    data-toggle="dropdown">[% l('Select Media') %]
+                    <span class="caret"></span></button>
+                  <ul class="dropdown-menu">
+                    <li ng-repeat="media in currentPrinter().media">
+                      <a href='' ng-click="applyConfAttr('media', media)">
+                        {{media}}
+                      </a>
+                    </li>
+                  </ul>
+                </div><!-- /btn-group -->
+                <input type="text"
+                  ng-if="!currentPrinter().media.length" 
+                  class="form-control" disabled="disabled"
+                  value="[% l('No Media Found for Selected Printer') %]">
+                <input type="text"
+                  ng-if="currentPrinter().media.length && !printConfig[context].media"
+                  class="form-control" disabled="disabled"
+                  value="[% l('No Media Selected') %]">
+                <input type="text"
+                  ng-if="printConfig[context].media" 
+                  class="form-control" disabled="disabled"
+                  value="{{printConfig[context].media}}">
+              </div><!-- input grp -->
+            </div><!-- col -->
+          </div><!-- row -->
+
+          <!-- orientation selector -->
+          <div class="row">
+            <div class="col-md-6">
+              <div class="input-group">
+                <div class="input-group-btn">
+                  <button type="button" class="btn btn-default dropdown-toggle" 
+                    data-toggle="dropdown">[% l('Select Orientation') %]
+                    <span class="caret"></span></button>
+                  <ul class="dropdown-menu">
+                    <li> 
+                      <a href='' 
+                        ng-click="applyConfAttr('orientation-requested', 'portrait')">
+                        [% l('Portrait') %]</a>
+                    </li>
+                    <li> 
+                      <a href='' 
+                        ng-click="applyConfAttr('orientation-requested', 'landscape')">
+                        [% l('Landscape') %]</a>
+                    </li>
+                  </ul>
+                </div><!-- /btn-group -->
+                <input type="text"
+                  ng-if="!printConfig[context]['orientation-requested']"
+                  class="form-control" disabled="disabled"
+                  value="[% l('No Orientation Selected') %]">
+                <input type="text"
+                  ng-if="printConfig[context]['orientation-requested']"
+                  class="form-control" disabled="disabled"
+                  value="{{printConfig[context]['orientation-requested']}}">
+              </div><!-- input grp -->
+            </div><!-- col -->
+          </div><!-- row -->
+
+
+          
+          <!-- save button -->
+          <div class="row">
+            <div class="col-md-6">
+              <button type="button" 
+                ng-click="storePrinterSettings()"
+                ng-class="{disabled : actionPending}"
+                class="btn btn-default btn-success">
+                  [% l('Save Changes') %]
+              </button>
         </div>
       </div>
     </div>
index e47fc38..dcb0d09 100644 (file)
@@ -32,16 +32,86 @@ angular.module('egWorkstationAdmin', ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'e
 function($scope , egPrintStore) {
     console.log('PrintingCtrl');
 
-    egPrintStore.printers()
-    .then(function(printers) { $scope.printers = printers });
+    $scope.actionPending = false;
 
-    $scope.setContext = function(ctx) { $scope.context = ctx }
+    $scope.setContext = function(ctx) { 
+        $scope.context = ctx; 
+    }
+    $scope.setContext('default');
+
+    $scope.getPrinterByAttr = function(name, value) {
+        var printer;
+        angular.forEach($scope.printers, function(p) {
+            if (p[name] == value) printer = p;
+        });
+        return printer;
+    }
+
+    $scope.currentPrinter = function() {
+        if ($scope.printConfig && $scope.printConfig[$scope.context]) {
+            return $scope.getPrinterByAttr(
+                'printer-name', 
+                $scope.printConfig[$scope.context].name
+            );
+        }
+    }
+
+    // fetch info on all remote printers
+    egPrintStore.getPrinters()
+    .then(function(printers) { 
+        $scope.printers = printers;
+        $scope.defaultPrinter = 
+            $scope.getPrinterByAttr('is-default', true);
+    })
+    .then(function() { return egPrintStore.getPrintConfig() })
+    .then(function(config) {
+        $scope.printConfig = config;
+
+        // apply the default printer to every context which has
+        // no printer configured.
+        angular.forEach(
+            ['default','receipt','label','mail','offline'],
+            function(ctx) {
+                if (!$scope.printConfig[ctx]) {
+                    $scope.printConfig[ctx] = {
+                        context : ctx,
+                        name : $scope.defaultPrinter['printer-name']
+                    }
+                }
+            }
+        );
+
+        console.debug('loaded print config ' + js2JSON($scope.printConfig));
+    });
+
+    // apply an attribute value to the current printer context
+    // TODO: use a form instead?
+    $scope.applyConfAttr = function(name, value) {
+        if (name == 'name') {
+            // user is changing the printer
+            $scope.currentPrinter = $scope.getPrinterByAttr('name', value);
+        }
+        $scope.printConfig[$scope.context][name] = value;
+    }
+
+    // store the currently active printer configuration data
+    $scope.storePrinterSettings = function() {
+        $scope.actionPending = true;
+        egPrintStore.setPrintConfig($scope.printConfig)
+        .then(function() { console.debug('print settings stored') })
+        .finally(function() { $scope.actionPending = false });
+    }
+
+
+    // for testing
     $scope.setContentType = function(type) { $scope.contentType = type }
+
     $scope.testPrint = function() {
         if ($scope.contentType == 'text/plain') {
             egPrintStore.print($scope.contentType, $scope.textPrintContent);
         } else {
             egPrintStore.print(
+                $scope.context,
                 $scope.contentType, 
                 $scope.htmlPrintContent, 
                 {
@@ -53,7 +123,6 @@ function($scope , egPrintStore) {
         }
     }
 
-    $scope.setContext('default');
     $scope.setContentType('text/plain');
 
 }])
index 2dbf595..b1fcabf 100644 (file)
@@ -80,13 +80,13 @@ angular.module('egCoreMod')
         delete service.messages[msg.msgid]; // un-cache
 
         // resolve / reject
-        if (msg.success) {
-            console.debug("command succeeded : " + msg.success);
-            msg.deferred.resolve(msg.success);
-        } else {
-            console.error("command failed : " + msg.error);
+        if (msg.error) {
+            console.error("egPrintStore command failed : " + msg.error);
             msg.deferred.reject(msg.error);
-        }
+        } else {
+            console.debug("egPrintStore command succeeded : " + msg.content);
+            msg.deferred.resolve(msg.content);
+        } 
     }
 
     // pass each request off to its local handler function
@@ -115,6 +115,13 @@ angular.module('egCoreMod')
                 case 'remove':
                     service.handleLocalStorageRequest(msg);
                     break;
+                case 'printers' : 
+                    // there is fall-through handler for printers
+                    msg.content = [];
+                    break;
+                default:
+                    console.debug(
+                        'no fall-thru handler for requested action: ' + msg.action);
             }
             service.resolveRequest(msg); 
         }
@@ -183,7 +190,6 @@ angular.module('egCoreMod')
     // print locally via the browser
     service.browserPrint = function(msg) {
         service.onBrowserPrint(msg.contentType, msg.content);
-        msg.success = true; // assume browser print succeeded
     }
 
     /**
@@ -205,24 +211,58 @@ angular.module('egCoreMod')
 
     // print the provided content
     // supported values for contentType are 'text/html' and 'text/plain'
-    service.print = function(contentType, content, printScope) {
+    service.print = function(context, contentType, content, printScope) {
 
         if (contentType == 'text/html') {
             content = service.interpolateHtmlTemplate(content, printScope);
             console.debug('generated HTML ' + content);
         }
         
-        return service.dispatchRequest({
-            key : 'no-op', 
-            action : 'print',
-            content : content, 
-            contentType : contentType
-            //printer : printer // TODO: prefs, etc.
+        return service.getPrintConfig()
+        .then(function(conf) {
+            return service.dispatchRequest({
+                key : 'no-op', 
+                action : 'print',
+                context : context,
+                content : content, 
+                contentType : contentType,
+                attributes : {
+                    'printer-name' : conf.name,
+                    'media' : conf.media,
+                    'requested-orientation' : conf['requested-orientation']
+                }
+            });
+        });
+    }
+
+    service.getPrintConfig = function() {
+        if (service.printConfig) 
+            return $q.when(service.printConfig);
+
+        return service.getItem('eg.printing.config')
+        .then(function(conf) { 
+            return (service.printConfig = conf || {}) 
         });
     }
 
-    service.printers = function() {
-        return service.dispatchRequest({key : 'no-op', action : 'printers'});
+    service.setPrintConfig = function(conf) {
+        service.printConfig = conf;
+        return service.setItem('eg.printing.config', conf);
+    }
+
+    service.getPrinters = function() {
+        if (service.printers) 
+            return $q.when(service.printers);
+
+        return service.dispatchRequest(
+            {key : 'no-op', action : 'printers'})
+        .then(function(printers) {
+            service.printers = printers
+            angular.forEach(printers, function(printer) {
+                printer.media = printer.media.sort();
+            });
+            return service.printers;
+        });
     }
 
     // get the value for a stored item
@@ -232,6 +272,7 @@ angular.module('egCoreMod')
 
     // set the value for a stored or new item
     service.setItem = function(key, value) {
+        value = js2JSON(value); // all values stored as JSON text
         return service.dispatchRequest(
             {key : key, value : value, action : 'set'});
     }
@@ -267,28 +308,26 @@ angular.module('egCoreMod')
                     if (key && k.substr(0, key.length) != key) continue; 
                     keys.push(k);
                 }
-                msg.success = keys;
+                msg.content = keys;
                 break;
 
             case 'set':
                 $window.localStorage.setItem(key, value);
-                msg.success = true;
                 break;
 
             case 'get':
-                msg.success = $window.localStorage.getItem(msg.key);
+                msg.content = $window.localStorage.getItem(msg.key);
                 break;
 
             case 'remove':
                 $window.localStorage.removeItem(msg.key);
-                msg.success = true;
                 break;
 
             case 'append':
                 var item = $window.localStorage.getItem(key);
                 if (item) value = item + value;
                 $window.localStorage.setItem(key, value);
-                msg.success = value;
+                msg.content = value;
                 break;
         }
     }