github.com/sym3tri/etcd@v0.2.1-0.20140422215517-a563d82f95d6/mod/dashboard/app/page/browser/browser-ctrl.js (about)

     1  'use strict';
     2  
     3  angular.module('etcd.page')
     4  .controller('BrowserCtrl', function($scope, $modal, etcdApiSvc, pathSvc,
     5        ETCD_EVENT, d3, pollerSvc) {
     6  
     7    $scope.currPath = '/';
     8    $scope.currNode = null;
     9  
    10    $scope.rowClick = function(node) {
    11      if (node.dir) {
    12        $scope.currPath = node.key;
    13      }
    14    };
    15  
    16    $scope.truncateKey = function(key) {
    17      return pathSvc.tail(key);
    18    };
    19  
    20    $scope.openCreateModal = function(key) {
    21      $modal.open({
    22        templateUrl: '/page/browser/create-node.html',
    23        controller: 'CreateNodeCtrl',
    24        resolve: {
    25          key: d3.functor(key || $scope.currPath),
    26        }
    27      });
    28    };
    29  
    30    /**
    31     * Refresh the list whenever a node is changed.
    32     */
    33    $scope.$on(ETCD_EVENT.NODE_CHANGED, function(e, node) {
    34      var parentKey = pathSvc.getParent(node.key);
    35      $scope.refreshNode(parentKey);
    36    });
    37  
    38    /**
    39     * Refresh the list whenever a node is deleted.
    40     */
    41    $scope.$on(ETCD_EVENT.NODE_DELETED, function(e, node) {
    42      var parentKey = pathSvc.getParent(node.key);
    43      $scope.refreshNode(parentKey);
    44    });
    45  
    46    $scope.breadcrumbCallback = function(result) {
    47      $scope.currPath = result.path;
    48    };
    49  
    50    /**
    51     * Update currPath and always refetch that node.
    52     */
    53    $scope.refreshNode = function(path) {
    54      if ($scope.currPath === path) {
    55        // Force refresh.
    56        $scope.fetchNode();
    57      } else {
    58        $scope.currPath = path;
    59      }
    60    };
    61  
    62    $scope.fetchNode = function() {
    63      return etcdApiSvc.fetch($scope.currPath)
    64      .then(function(node) {
    65        $scope.currNode = node;
    66      });
    67    };
    68  
    69    $scope.$watch('currPath', function(currPath) {
    70      if (!currPath || currPath === '') {
    71        $scope.currPath = '/';
    72        return;
    73      }
    74      $scope.fetchNode();
    75    });
    76  
    77    pollerSvc.register('nodePoller', {
    78      fn: $scope.fetchNode,
    79      scope: $scope
    80    });
    81  
    82  });
    83