browser-ctrl.js 1.7 KB

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