queueCtrl.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var queueCtrl = angular.module('queueCtrl', ['runsFactory']);
  2. queueCtrl.controller('QueueCtrl', ['$scope', '$routeParams', '$location', 'Runs', 'API', function($scope, $routeParams, $location, Runs, API) {
  3. $scope.runId = $routeParams.runId;
  4. var numberOfTries = 0;
  5. var favicon = document.querySelector('link[rel=icon]');
  6. var faviconUrl = 'img/favicon.png';
  7. var faviconSuccessUrl = 'img/favicon-success.png';
  8. var faviconFailUrl = 'img/favicon-fail.png';
  9. var faviconInterval = null;
  10. var faviconCounter = 0;
  11. var faviconCanvas = null;
  12. var faviconCanvasContext = null;
  13. var faviconImage = null;
  14. function getRunStatus () {
  15. Runs.get({runId: $scope.runId}, function(data) {
  16. $scope.url = data.params.url;
  17. $scope.status = data.status;
  18. $scope.progress = data.progress;
  19. $scope.notFound = false;
  20. $scope.connectionLost = false;
  21. if (data.status.statusCode === 'awaiting') {
  22. numberOfTries ++;
  23. rotateFavicon();
  24. // Retrying every 2 seconds (and increasing the delay a bit more each time)
  25. setTimeout(getRunStatus, 2000 + (numberOfTries * 100));
  26. } else if (data.status.statusCode === 'running') {
  27. numberOfTries ++;
  28. rotateFavicon();
  29. // Retrying every second or so
  30. setTimeout(getRunStatus, 1000 + (numberOfTries * 10));
  31. } else if (data.status.statusCode === 'complete') {
  32. stopFavicon(true);
  33. $location.path('/result/' + $scope.runId).replace();
  34. } else {
  35. stopFavicon(false);
  36. // The rest is handled by the view
  37. }
  38. }, function(response) {
  39. if (response.status === 404) {
  40. stopFavicon(false);
  41. $scope.notFound = true;
  42. $scope.connectionLost = false;
  43. } else if (response.status === 0) {
  44. // Connection lost, retry in 10 seconds
  45. setTimeout(getRunStatus, 10000);
  46. $scope.connectionLost = true;
  47. $scope.notFound = false;
  48. }
  49. });
  50. }
  51. function rotateFavicon() {
  52. if (!faviconInterval) {
  53. faviconImage = new Image();
  54. faviconImage.onload = function() {
  55. faviconCanvas = document.getElementById('faviconRotator');
  56. faviconCanvasContext = faviconCanvas.getContext('2d');
  57. faviconCanvasContext.fillStyle = '#212240';
  58. if (!!faviconCanvasContext) {
  59. faviconInterval = window.setInterval(faviconTick, 300);
  60. }
  61. };
  62. faviconImage.src = faviconUrl;
  63. }
  64. }
  65. function faviconTick() {
  66. faviconCounter ++;
  67. faviconCanvasContext.save();
  68. faviconCanvasContext.fillRect(0, 0, 32, 32);
  69. faviconCanvasContext.translate(16, 16);
  70. faviconCanvasContext.rotate(22.5 * faviconCounter * Math.PI / 180);
  71. faviconCanvasContext.translate(-16, -16);
  72. faviconCanvasContext.drawImage(faviconImage, 0, 0, 32, 32);
  73. faviconCanvasContext.restore();
  74. favicon.href = faviconCanvas.toDataURL('image/png');
  75. }
  76. function stopFavicon(isSuccess) {
  77. window.clearInterval(faviconInterval);
  78. faviconInterval = null;
  79. favicon.href = isSuccess ? faviconSuccessUrl : faviconFailUrl;
  80. }
  81. getRunStatus();
  82. }]);