browser.js 5.1 KB

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