browser.js 4.4 KB

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