From 48b324e50d925af629ac3c796cfed3d83d0b973d Mon Sep 17 00:00:00 2001 From: oterral Date: Thu, 29 Jan 2015 16:30:47 +0100 Subject: [PATCH] Add button to cancel print process --- .../offline/partials/offline-selector.html | 2 +- src/components/print/PrintDirective.js | 107 +++++++++++++----- src/components/print/partials/print.html | 9 +- src/locales/de.json | 2 +- src/locales/empty.json | 2 +- src/locales/en.json | 2 +- src/locales/fr.json | 2 +- src/locales/it.json | 2 +- src/locales/rm.json | 2 +- 9 files changed, 93 insertions(+), 37 deletions(-) diff --git a/src/components/offline/partials/offline-selector.html b/src/components/offline/partials/offline-selector.html index 75ec61488c..813f38099e 100644 --- a/src/components/offline/partials/offline-selector.html +++ b/src/components/offline/partials/offline-selector.html @@ -12,6 +12,6 @@ translate>offline_save + translate>abort diff --git a/src/components/print/PrintDirective.js b/src/components/print/PrintDirective.js index 90fdd9216b..266ca28ecb 100644 --- a/src/components/print/PrintDirective.js +++ b/src/components/print/PrintDirective.js @@ -8,8 +8,8 @@ 'pascalprecht.translate']); module.controller('GaPrintDirectiveController', function($rootScope, $scope, - $http, $window, $translate, $timeout, - gaLayers, gaPermalink, gaBrowserSniffer, gaWaitCursor) { + $http, $q, $window, $translate, $timeout, gaLayers, gaPermalink, + gaBrowserSniffer, gaWaitCursor) { var pdfLegendsToDownload = []; var pdfLegendString = '_big.pdf'; @@ -23,7 +23,8 @@ var printConfigLoaded = false; var currentTime = undefined; var layersYears = []; - + var canceller; + var currentMultiPrintId; $scope.options.multiprint = false; $scope.options.movie = false; $scope.options.printing = false; @@ -32,8 +33,10 @@ // Get print config var updatePrintConfig = function() { - var http = $http.get($scope.options.printConfigUrl); - http.success(function(data) { + canceller = $q.defer(); + var http = $http.get($scope.options.printConfigUrl, { + timeout: canceller.promise + }).success(function(data) { $scope.capabilities = data; // default values: @@ -591,6 +594,27 @@ $scope.options.printing = false; }; + // Abort the print process + var pollMultiPromise; // Promise of the last $timeout called + $scope.abort = function() { + $scope.options.printing = false; + // Abort the current $http request + if (canceller) { + canceller.resolve(); + } + // Abort the current $timeout + if (pollMultiPromise) { + $timeout.cancel(pollMultiPromise); + } + // Tell the server to cancel the print process + if (currentMultiPrintId) { + $http.get($scope.options.printPath + 'cancel?id=' + + currentMultiPrintId); + currentMultiPrintId = null; + } + }; + + // Start the print process $scope.submit = function() { if (!$scope.options.active) { return; @@ -613,6 +637,7 @@ pdfLegendsToDownload = []; layersYears = []; + // Transform layers to literal angular.forEach(layers, function(layer) { if (layer.visible) { var attribution = layer.attribution; @@ -648,6 +673,8 @@ }); defaultPage['timestamp'] = years.join(','); } + + // Transform graticule to literal if ($scope.options.graticule) { var graticule = { 'baseURL': 'http://wms.geo.admin.ch/', @@ -663,6 +690,8 @@ }; encLayers.push(graticule); } + + // Transform overlays to literal // FIXME this is a temporary solution var overlays = $scope.map.getOverlays(); var resolution = $scope.map.getView().getResolution(); @@ -705,25 +734,34 @@ } }); - var scales = this.scales.map(function(scale) { - return parseInt(scale.value); - }); - var that = this; - $http.get($scope.options.shortenUrl, { + + // Get the short link + var shortLink; + canceller = $q.defer(); + var promise = $http.get($scope.options.shortenUrl, { + timeout: canceller.promise, params: { url: gaPermalink.getHref() } }).success(function(response) { + shortLink = response.shorturl.replace('/shorten', ''); + }); + + // Build the complete json then send it to the print server + promise.then(function() { + if (!$scope.options.printing) { + return; + } var movieprint = $scope.options.movie && $scope.options.multiprint; var spec = { - layout: that.layout.name, + layout: $scope.layout.name, srs: proj.getCode(), units: proj.getUnits() || 'm', rotation: -((view.getRotation() * 180.0) / Math.PI), app: 'config', lang: lang, //use a function to get correct dpi according to layout (A4/A3) - dpi: getDpi(that.layout.name, that.dpi), + dpi: getDpi($scope.layout.name, $scope.dpi), layers: encLayers, legends: encLegends, enableLegends: (encLegends && encLegends.length > 0), @@ -733,22 +771,29 @@ angular.extend({ center: getPrintRectangleCenterCoord(), bbox: getPrintRectangleCoords(), - display: [that.layout.map.width, that.layout.map.height], + display: [$scope.layout.map.width, $scope.layout.map.height], // scale has to be one of the advertise by the print server scale: $scope.scale.value, dataOwner: '© ' + attributions.join(), - shortLink: response.shorturl.replace('/shorten', ''), + shortLink: shortLink || '', rotation: -((view.getRotation() * 180.0) / Math.PI) }, defaultPage) ] }; - var startPollTime; var pollErrors; var pollMulti = function(url) { - $timeout(function() { - var http = $http.get(url); - http.success(function(data) { + pollMultiPromise = $timeout(function() { + if (!$scope.options.printing) { + return; + } + canceller = $q.defer(); + var http = $http.get(url, { + timeout: canceller.promise + }).success(function(data) { + if (!$scope.options.printing) { + return; + } if (!data.getURL) { // Write progress using the following logic // First 60% is pdf page creationg @@ -781,6 +826,10 @@ $scope.downloadUrl(data.getURL); } }).error(function() { + if ($scope.options.printing == false) { + pollErrors = 0; + return; + } pollErrors += 1; if (pollErrors > 2) { $scope.options.printing = false; @@ -791,18 +840,21 @@ }, POLL_INTERVAL, false); }; - var printUrl = that.capabilities.createURL; + var printUrl = $scope.capabilities.createURL; //When movie is on, we use printmulti if (movieprint) { printUrl = printUrl.replace('/print/', '/printmulti/'); } - var http = $http.post(printUrl + '?url=' + - encodeURIComponent(printUrl), spec); - http.success(function(data) { + canceller = $q.defer(); + var http = $http.post(printUrl + '?url=' + encodeURIComponent(printUrl), + spec, { + timeout: canceller.promise + }).success(function(data) { if (movieprint) { //start polling process var pollUrl = $scope.options.printPath + 'progress?id=' + data.idToCheck; + currentMultiPrintId = data.idToCheck; startPollTime = new Date(); pollErrors = 0; pollMulti(pollUrl); @@ -812,18 +864,16 @@ }).error(function() { $scope.options.printing = false; }); - }).error(function() { - $scope.options.printing = false; }); }; - function getDpi(layoutName, dpiConfig) { + var getDpi = function(layoutName, dpiConfig) { if (/a4/i.test(layoutName) && dpiConfig.length > 1) { return dpiConfig[1].value; } else { return dpiConfig[0].value; } - } + }; var getPrintRectangleCoords = function() { // Framebuffer size!! @@ -935,13 +985,12 @@ }); module.directive('gaPrint', - function($http, $log, $translate) { + function() { return { restrict: 'A', templateUrl: 'components/print/partials/print.html', controller: 'GaPrintDirectiveController', - link: function(scope, elt, attrs, controller) { - } + link: function(scope, elt, attrs, controller) {} }; } ); diff --git a/src/components/print/partials/print.html b/src/components/print/partials/print.html index 92f2f6d90c..88a0713355 100644 --- a/src/components/print/partials/print.html +++ b/src/components/print/partials/print.html @@ -40,7 +40,14 @@
{{options.progress}}
- + + diff --git a/src/locales/de.json b/src/locales/de.json index a6f4a9c5f4..0a4f479a67 100644 --- a/src/locales/de.json +++ b/src/locales/de.json @@ -1,4 +1,5 @@ { + "abort": "Abbrechen", "add_layer": "Layer hinzufügen", "add_wms_layer_failed": "Fehler beim Laden WMS Layer", "add_wms_layer_succeeded": "WMS Layer erfolgreich geladen", @@ -180,7 +181,6 @@ "object_information": "Objekt-Information", "obstacle_deleted_last_2_weeks": "Gelöschte Hindernisse letzte 2 Wochen", "obstacle_started_last_2_weeks": "Aktivierte Hindernisse letzte 2 Wochen", - "offline_abort": "Abbrechen", "offline_abort_warning": "Wollen Sie wirklich alle Karten löschen?", "offline_bad_layer_type": "Einer der Datensätze kann nicht für den Offline-Gebrauch gespeichert werden.", "offline_clear_db_error": "Fehler beim Löschen der gespeicherten Karten. Bitte versuchen Sie es noch einmal.", diff --git a/src/locales/empty.json b/src/locales/empty.json index a1dbd2febc..2e6e61c83a 100644 --- a/src/locales/empty.json +++ b/src/locales/empty.json @@ -245,7 +245,7 @@ "vu_service_link_label": "", "vu_service_link_href": "", "offline_save": "", - "offline_abort": "", + "abort": "", "offline_modal_title": "", "offline_delete_data": "", "offline_show_extent": "", diff --git a/src/locales/en.json b/src/locales/en.json index 22a2e60532..ba0841b837 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -1,4 +1,5 @@ { + "abort": "Abort", "add_layer": "Add Layer", "add_wms_layer_failed": "Error when loading WMS layer", "add_wms_layer_succeeded": "WMS layer successfully loaded", @@ -180,7 +181,6 @@ "object_information": "Object information", "obstacle_deleted_last_2_weeks": "Deleted obstacles of last 2 weeks", "obstacle_started_last_2_weeks": "Activated obstacles of last 2 weeks", - "offline_abort": "Abort", "offline_abort_warning": "Do you really want to delete all maps?", "offline_bad_layer_type": "A layer has an invalid type. It can't be saved for an offline use. ", "offline_clear_db_error": "Error clearing saved maps. Please try again.", diff --git a/src/locales/fr.json b/src/locales/fr.json index 246aadff09..16814ba21a 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -1,4 +1,5 @@ { + "abort": "Annuler", "add_layer": "Ajouter couche", "add_wms_layer_failed": "Erreur au chargement de la couche WMS", "add_wms_layer_succeeded": "Couche WMS chargée avec succès", @@ -180,7 +181,6 @@ "object_information": "Information objet", "obstacle_deleted_last_2_weeks": "Obstacles supprimés ces 2 dernières semaines", "obstacle_started_last_2_weeks": "Obstacles activés ces 2 dernières semaines", - "offline_abort": "Annuler", "offline_abort_warning": "Voulez-vous vraiment supprimer tous les cartes?", "offline_bad_layer_type": "Une couche a un type invalide. Elle ne peut être sauvegardée pour un usage hors-ligne.", "offline_clear_db_error": "Erreur lors de la suppression de la base de données. Réessayez.", diff --git a/src/locales/it.json b/src/locales/it.json index 8e4aef28f3..98e6cf547a 100644 --- a/src/locales/it.json +++ b/src/locales/it.json @@ -1,4 +1,5 @@ { + "abort": "Annullare", "add_layer": "Aggiungere layer", "add_wms_layer_failed": "Errore nel caricamento del layer WMS", "add_wms_layer_succeeded": "Layer WMS caricato con successo", @@ -180,7 +181,6 @@ "object_information": "Informazione oggetto", "obstacle_deleted_last_2_weeks": "Ostacoli soppressi nelle ultime 2 settimane", "obstacle_started_last_2_weeks": "Ostacoli attivati nelle ultime 2 settimane", - "offline_abort": "Annullare", "offline_abort_warning": "Volete veramente cancellare tutte le carte?", "offline_bad_layer_type": "Uno dei layer non è valido. Non potrà venir salvato per l'uso offline.", "offline_clear_db_error": "Errore nell'eliminazione delle carte. Riprovare.", diff --git a/src/locales/rm.json b/src/locales/rm.json index f514fede3c..2aa45fa60d 100644 --- a/src/locales/rm.json +++ b/src/locales/rm.json @@ -1,4 +1,5 @@ { + "abort": "Annullar", "add_layer": "Agiuntar Layer", "add_wms_layer_failed": "Sbagl tar il chargiar de la WMS layer", "add_wms_layer_succeeded": "Chargià con success il WMS Layer", @@ -180,7 +181,6 @@ "object_information": "Objekt infurmaziun", "obstacle_deleted_last_2_weeks": "Gelöschte Hindernisse letzte 2 Wochen", "obstacle_started_last_2_weeks": "Aktivierte Hindernisse letzte 2 Wochen", - "offline_abort": "Annullar", "offline_abort_warning": "Vulais Vus propi stizzar tut ils chartas?", "offline_bad_layer_type": "Einer der Datensätze kann nicht für den Offline-Gebrauch gespeichert werden.", "offline_clear_db_error": "Errur cun stizzar las chartas memorisadas. Per plaschair empruvais anc ina giada.",