egCore.hatch.getPrinters()
.then(function(printers) {
$scope.printers = printers;
- $scope.defaultPrinter =
- $scope.getPrinterByAttr('is-default', true);
- })
- .then(function() { return egCore.hatch.getPrintConfig() })
- .then(function(config) {
- $scope.printConfig = config;
- var pname = '';
- if ($scope.defaultPrinter) {
- pname = $scope.defaultPrinter.name;
+ var def = $scope.getPrinterByAttr('is-default', true);
+ if (!def && printers.length) def = printers[0];
- } else if ($scope.printers.length == 1) {
- // if the OS does not report a default printer, but only
- // one printer is available, treat it as the default.
- pname = $scope.printers[0].name;
+ if (def) {
+ $scope.defaultPrinter = def;
+ loadPrinterOptions(def.name);
}
+ })
+ .then(function() {
+ $scope.printConfig = {};
// 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,
- printer : pname,
- autoMargins : true,
- allPages : true,
- pageRanges : []
+ egCore.hatch.getPrintConfig(ctx).then(function(conf) {
+ if (!conf) {
+ conf = {
+ context : ctx,
+ printer : $scope.defaultPrinter.name,
+ autoMargins : true,
+ allPages : true,
+ pageRanges : []
+ };
}
- }
+ $scope.printConfig[ctx] = conf;
+ });
}
);
-
- // load printer options for the first printer shown.
- // TODO: rethink this in combo w/ print config stuff above..
- $scope.setPrinter($scope.printConfig['default'].printer);
});
.finally(function() {$scope.actionPending = false});
}
- $scope.setPrinter = function(name) {
- $scope.printConfig[$scope.context].printer = name;
+ function loadPrinterOptions(name) {
egCore.hatch.getPrinterOptions(name).then(
function(options) {$scope.printerOptions = options});
}
+ $scope.setPrinter = function(name) {
+ $scope.printConfig[$scope.context].printer = name;
+ loadPrinterOptions(name);
+ }
+
// for testing
$scope.setContentType = function(type) { $scope.contentType = type }
service.messages = {};
service.pending = [];
service.hatchAvailable = null;
+ service.cachedPrintConfig = {};
service.state = 'IDLE'; // IDLE, INIT, CONNECTED, NO_CONNECTION
// write a message to the Hatch port
};
}
- service.getPrintConfig = function() {
- if (service.printConfig)
- return $q.when(service.printConfig);
-
- return service.getRemoteItem('eg.print.config')
- .then(function(conf) {
- return (service.printConfig = conf || {})
- });
- }
-
- service.setPrintConfig = function(conf) {
- service.printConfig = conf;
- return service.setRemoteItem('eg.print.config', conf);
- }
-
-
service.remotePrint = function(
context, contentType, content, withDialog) {
);
}
+ // 'force' avoids using the config cache
+ service.getPrintConfig = function(context, force) {
+ if (service.cachedPrintConfig[context] && !force) {
+ return $q.when(service.cachedPrintConfig[context])
+ }
+ return service.getRemoteItem('eg.print.config.' + context)
+ .then(function(config) {
+ return service.cachedPrintConfig[context] = config;
+ });
+ }
+
+ service.setPrintConfig = function(context, config) {
+ service.cachedPrintConfig[context] = config;
+ return service.setRemoteItem('eg.print.config.' + context, config);
+ }
+
service.getPrinterOptions = function(name) {
return service.attemptHatchDelivery({
action : 'printer-options',
});
}
- // launch the print dialog then attach the resulting configuration
- // to the requested context, then store the final values.
- service.configurePrinter = function(context, printer) {
-
- // load current settings
- return service.getPrintConfig()
-
- // dispatch the print configuration request
- .then(function(config) {
-
- // loaded remote config
- if (!config[context]) config[context] = {};
- config[context].printer = printer;
- return service.attemptHatchDelivery({
- key : 'no-op',
- action : 'print-config',
- config : config[context]
- })
- })
-
- // set the returned settings to the requested context
- .then(function(newconf) {
- if (angular.isObject(newconf)) {
- newconf.printer = printer;
- return service.printConfig[context] = newconf;
- } else {
- console.warn("configurePrinter() returned " + newconf);
- }
- })
-
- // store the newly linked settings
- .then(function() {
- service.setItem('eg.print.config', service.printConfig);
- })
-
- // return the final settings to the caller
- .then(function() {return service.printConfig});
- }
-
service.getPrinters = function() {
if (service.printers) // cached printers
return $q.when(service.printers);