github.com/kula/etcd@v0.2.1-0.20131226070625-e96234382ac0/mod/dashboard/app/scripts/controllers/browser.js (about) 1 'use strict'; 2 3 angular.module('etcdBrowser', ['ngRoute', 'etcd', 'timeRelative']) 4 5 .constant('keyPrefix', '/v2/keys/') 6 7 .config(['$routeProvider', 'keyPrefix', function ($routeProvider, keyPrefix) { 8 //read localstorage 9 var previousPath = localStorage.getItem('etcd_path'); 10 11 $routeProvider 12 .when('/', { 13 redirectTo: keyPrefix 14 }) 15 .otherwise({ 16 templateUrl: 'views/browser.html', 17 controller: 'MainCtrl' 18 }); 19 }]) 20 21 .controller('MainCtrl', ['$scope', '$location', 'EtcdV2', 'keyPrefix', function ($scope, $location, EtcdV2, keyPrefix) { 22 $scope.save = 'etcd-save-hide'; 23 $scope.preview = 'etcd-preview-hide'; 24 $scope.enableBack = true; 25 $scope.writingNew = false; 26 27 // etcdPath is the path to the key that is currenly being looked at. 28 $scope.etcdPath = $location.path(); 29 30 $scope.$watch('etcdPath', function() { 31 function etcdPathKey() { 32 return pathKey($scope.etcdPath); 33 } 34 35 function pathKey(path) { 36 var parts = path.split(keyPrefix); 37 if (parts.length === 1) { 38 return ''; 39 } 40 return parts[1]; 41 } 42 43 // Notify everyone of the update 44 localStorage.setItem('etcdPath', $scope.etcdPath); 45 $scope.enableBack = true; 46 //disable back button if at root (/v2/keys/) 47 if($scope.etcdPath === keyPrefix) { 48 $scope.enableBack = false; 49 } 50 51 $scope.key = EtcdV2.getKey(etcdPathKey($scope.etcdPath)); 52 }); 53 54 $scope.$watch('key', function() { 55 if ($scope.writingNew === true) { 56 return; 57 } 58 $scope.key.get().success(function (data, status, headers, config) { 59 //hide any errors 60 $('#etcd-browse-error').hide(); 61 // Looking at a directory if we got an array 62 if (data.dir === true) { 63 $scope.list = data.node.nodes; 64 $scope.preview = 'etcd-preview-hide'; 65 } else { 66 $scope.singleValue = data.value; 67 $scope.preview = 'etcd-preview-reveal'; 68 $scope.key.getParent().get().success(function(data) { 69 $scope.list = data.node.nodes; 70 }); 71 } 72 $scope.previewMessage = 'No key selected.'; 73 }).error(function (data, status, headers, config) { 74 $scope.previewMessage = 'Key does not exist.'; 75 $scope.showBrowseError(data.message); 76 }); 77 }); 78 79 //back button click 80 $scope.back = function() { 81 $scope.etcdPath = $scope.key.getParent().path(); 82 $scope.syncLocation(); 83 $scope.preview = 'etcd-preview-hide'; 84 $scope.writingNew = false; 85 }; 86 87 $scope.syncLocation = function() { 88 $location.path($scope.etcdPath); 89 }; 90 91 $scope.showSave = function() { 92 $scope.save = 'etcd-save-reveal'; 93 }; 94 95 $scope.saveData = function() { 96 // TODO: fixup etcd to allow for empty values 97 $scope.key.set($scope.singleValue || ' ').then(function(response) { 98 $scope.save = 'etcd-save-hide'; 99 $scope.preview = 'etcd-preview-hide'; 100 $scope.back(); 101 $scope.writingNew = false; 102 }, function (response) { 103 $scope.showSaveError(data.message); 104 }); 105 }; 106 107 $scope.deleteKey = function() { 108 $scope.key.deleteKey().then(function(response) { 109 //TODO: remove loader 110 $scope.save = 'etcd-save-hide'; 111 $scope.preview = 'etcd-preview-hide'; 112 $scope.back(); 113 }, function (response) { 114 //TODO: remove loader 115 //show errors 116 $scope.showBrowseError('Could not delete the key'); 117 }); 118 }; 119 120 $scope.add = function() { 121 $scope.save = 'etcd-save-reveal'; 122 $scope.preview = 'etcd-preview-reveal'; 123 $scope.singleValue = ''; 124 $('.etcd-browser-path').find('input').focus(); 125 $scope.writingNew = true; 126 }; 127 128 $scope.showBrowseError = function(message) { 129 $('#etcd-browse-error').find('.etcd-popover-content').text('Error: ' + message); 130 $('#etcd-browse-error').addClass('etcd-popover-right').show(); 131 }; 132 133 $scope.showSaveError = function(message) { 134 $('#etcd-save-error').find('.etcd-popover-content').text('Error: ' + message); 135 $('#etcd-save-error').addClass('etcd-popover-left').show(); 136 }; 137 138 $scope.getHeight = function() { 139 return $(window).height(); 140 }; 141 $scope.$watch($scope.getHeight, function() { 142 $('.etcd-body').css('height', $scope.getHeight()-45); 143 }); 144 window.onresize = function(){ 145 $scope.$apply(); 146 }; 147 148 }]) 149 150 .directive('ngEnter', function() { 151 return function(scope, element, attrs) { 152 element.bind('keydown keypress', function(event) { 153 if(event.which === 13) { 154 scope.$apply(function(){ 155 scope.$eval(attrs.ngEnter); 156 }); 157 158 event.preventDefault(); 159 } 160 }); 161 }; 162 }) 163 164 .directive('highlight', function() { 165 return { 166 restrict: 'A', 167 link: function(scope, element, attrs) { 168 if('#' + scope.etcdPath === attrs.href) { 169 element.parent().parent().addClass('etcd-selected'); 170 } 171 } 172 }; 173 }); 174 175 moment.lang('en', { 176 relativeTime : { 177 future: 'Expires in %s', 178 past: 'Expired %s ago', 179 s: 'seconds', 180 m: 'a minute', 181 mm: '%d minutes', 182 h: 'an hour', 183 hh: '%d hours', 184 d: 'a day', 185 dd: '%d days', 186 M: 'a month', 187 MM: '%d months', 188 y: 'a year', 189 yy: '%d years' 190 } 191 });