browser.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. angular.module('etcdControlPanel')
  3. .controller('BrowserCtrl', function ($scope, $location, $window, EtcdV2, keyPrefix, $, _, moment) {
  4. $scope.save = 'etcd-save-hide';
  5. $scope.preview = 'etcd-preview-hide';
  6. $scope.enableBack = true;
  7. $scope.writingNew = false;
  8. $scope.key = '';
  9. $scope.list = [];
  10. // etcdPath is the path to the key that is currenly being looked at.
  11. $scope.etcdPath = keyPrefix;
  12. $scope.setActiveKey = function(key) {
  13. $scope.etcdPath = keyPrefix + _.str.trim(key, '/');
  14. };
  15. $scope.$watch('etcdPath', function() {
  16. function etcdPathKey() {
  17. return pathKey($scope.etcdPath);
  18. }
  19. function pathKey(path) {
  20. var parts = path.split(keyPrefix);
  21. if (parts.length === 1) {
  22. return '';
  23. }
  24. return parts[1];
  25. }
  26. // Notify everyone of the update
  27. localStorage.setItem('etcdPath', $scope.etcdPath);
  28. $scope.enableBack = true;
  29. //disable back button if at root (/v2/keys/)
  30. if ($scope.etcdPath === keyPrefix) {
  31. $scope.enableBack = false;
  32. }
  33. $scope.key = EtcdV2.getKey(etcdPathKey($scope.etcdPath));
  34. });
  35. $scope.$watch('key', function() {
  36. if ($scope.writingNew === true) {
  37. return;
  38. }
  39. $scope.key.get().success(function (data, status, headers, config) {
  40. //hide any errors
  41. $('#etcd-browse-error').hide();
  42. // Looking at a directory if we got an array
  43. if (data.dir === true) {
  44. $scope.list = data.node.nodes;
  45. $scope.preview = 'etcd-preview-hide';
  46. } else {
  47. $scope.singleValue = data.node.value;
  48. $scope.preview = 'etcd-preview-reveal';
  49. $scope.key.getParent().get().success(function(data) {
  50. $scope.list = data.node.nodes;
  51. });
  52. }
  53. $scope.previewMessage = 'No key selected.';
  54. }).error(function (data, status, headers, config) {
  55. $scope.previewMessage = 'Key does not exist.';
  56. $scope.showBrowseError(data.message);
  57. });
  58. });
  59. //back button click
  60. $scope.back = function() {
  61. $scope.etcdPath = $scope.key.getParent().path();
  62. //$scope.syncLocation();
  63. $scope.preview = 'etcd-preview-hide';
  64. $scope.writingNew = false;
  65. };
  66. //$scope.syncLocation = function() {
  67. //$location.path($scope.etcdPath);
  68. //};
  69. $scope.showSave = function() {
  70. $scope.save = 'etcd-save-reveal';
  71. };
  72. $scope.saveData = function() {
  73. // TODO: fixup etcd to allow for empty values
  74. $scope.key.set($scope.singleValue || ' ').then(function(response) {
  75. $scope.save = 'etcd-save-hide';
  76. $scope.preview = 'etcd-preview-hide';
  77. $scope.back();
  78. $scope.writingNew = false;
  79. }, function (response) {
  80. $scope.showSaveError(data.message);
  81. });
  82. };
  83. $scope.deleteKey = function() {
  84. $scope.key.deleteKey().then(function(response) {
  85. //TODO: remove loader
  86. $scope.save = 'etcd-save-hide';
  87. $scope.preview = 'etcd-preview-hide';
  88. $scope.back();
  89. }, function (response) {
  90. //TODO: remove loader
  91. //show errors
  92. $scope.showBrowseError('Could not delete the key');
  93. });
  94. };
  95. $scope.add = function() {
  96. $scope.save = 'etcd-save-reveal';
  97. $scope.preview = 'etcd-preview-reveal';
  98. $scope.singleValue = '';
  99. $('.etcd-browser-path').find('input').focus();
  100. $scope.writingNew = true;
  101. };
  102. $scope.showBrowseError = function(message) {
  103. $('#etcd-browse-error').find('.etcd-popover-content').text('Error: ' + message);
  104. $('#etcd-browse-error').addClass('etcd-popover-right').show();
  105. };
  106. $scope.showSaveError = function(message) {
  107. $('#etcd-save-error').find('.etcd-popover-content').text('Error: ' + message);
  108. $('#etcd-save-error').addClass('etcd-popover-left').show();
  109. };
  110. $scope.getHeight = function() {
  111. return $($window).height();
  112. };
  113. $scope.$watch($scope.getHeight, function() {
  114. $('.etcd-body').css('height', $scope.getHeight()-45);
  115. });
  116. $window.onresize = function(){
  117. $scope.$apply();
  118. };
  119. });