github.com/matm/etcd@v0.3.1-0.20140328024009-5b4a473f1453/mod/dashboard/app/scripts/controllers/browser.js (about)

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