github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/static/app/images/images.controller.js (about) 1 (function(){ 2 'use strict'; 3 4 angular 5 .module('shipyard.images') 6 .controller('ImagesController', ImagesController); 7 8 ImagesController.$inject = ['images', 'ImagesService', '$state', '$timeout', '$scope']; 9 function ImagesController(images, ImagesService, $state, $timeout, $scope) { 10 var vm = this; 11 vm.images = images; 12 vm.pulling = false; 13 vm.selectedImage = null; 14 vm.refresh = refresh; 15 vm.removeImage = removeImage; 16 vm.pullImage = pullImage; 17 vm.pullImageName = ""; 18 vm.tagImage = tagImage; 19 vm.tagImageName = ""; 20 vm.showTagImageDialog = showTagImageDialog; 21 vm.showRemoveImageDialog = showRemoveImageDialog; 22 vm.showPullImageDialog = showPullImageDialog; 23 vm.error = ""; 24 25 function showTagImageDialog(image) { 26 vm.selectedImage = image; 27 $('#tag-modal').modal('show'); 28 } 29 30 function showRemoveImageDialog(image) { 31 vm.selectedImage = image; 32 $('#remove-modal').modal('show'); 33 } 34 35 function showPullImageDialog(image) { 36 $('#pull-modal').modal('show'); 37 } 38 39 function refresh() { 40 ImagesService.list() 41 .then(function(data) { 42 vm.images = data; 43 }, function(data) { 44 vm.error = data; 45 }); 46 vm.error = ""; 47 } 48 49 function removeImage() { 50 ImagesService.remove(vm.selectedImage) 51 .then(function(data) { 52 vm.error = ""; 53 vm.refresh(); 54 }, function(data) { 55 vm.error = data.status + ": " + data.data; 56 }); 57 } 58 59 function tagImage() { 60 ImagesService.tag(vm.selectedImage, vm.tagImageName) 61 .then(function(data) { 62 vm.error = ""; 63 vm.refresh(); 64 }, function(data) { 65 vm.error = data.status + ": " + data.data; 66 vm.tagImageName = ""; 67 }); 68 } 69 70 function pullImage() { 71 vm.error = ""; 72 vm.pulling = true; 73 // this is to prevent errors in the console since we 74 // get a stream like response back 75 oboe({ 76 url: '/images/create?fromImage=' + vm.pullImageName, 77 method: "POST", 78 withCredentials: true, 79 headers: { 80 'X-Access-Token': localStorage.getItem("X-Access-Token") 81 } 82 }) 83 .done(function(node) { 84 // We expect two nodes, e.g. 85 // 1) Pulling busybox... 86 // 2) Pulling busybox... : downloaded 87 if(node.status && node.status.indexOf(":") > -1) { 88 if(node.status.indexOf("downloaded") == -1) { 89 $scope.$apply(); 90 } else { 91 setTimeout(vm.refresh, 1000); 92 } 93 } 94 vm.pullImageName = ""; 95 vm.pulling = false; 96 }) 97 .fail(function(err) { 98 console.log(err); 99 vm.pulling = false; 100 vm.pullImageName = ""; 101 vm.error = err; 102 }); 103 } 104 } 105 })();