2018-09-25 16:28:26 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { alias, gt } from '@ember/object/computed';
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import { computed } from '@ember/object';
|
2018-08-16 17:48:24 +00:00
|
|
|
import keyUtils from 'vault/lib/key-utils';
|
|
|
|
import pathToTree from 'vault/lib/path-to-tree';
|
|
|
|
import { task, timeout } from 'ember-concurrency';
|
|
|
|
|
|
|
|
const { ancestorKeysForKey } = keyUtils;
|
|
|
|
const DOT_REPLACEMENT = '☃';
|
|
|
|
const ANIMATION_DURATION = 250;
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: '',
|
2018-09-25 16:28:26 +00:00
|
|
|
namespaceService: service('namespace'),
|
|
|
|
auth: service(),
|
2018-10-02 15:05:34 +00:00
|
|
|
store: service(),
|
2018-08-16 17:48:24 +00:00
|
|
|
namespace: null,
|
2018-10-02 15:05:34 +00:00
|
|
|
listCapability: null,
|
|
|
|
canList: alias('listCapability.canList'),
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.get('namespaceService.findNamespacesForUser').perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
let ns = this.get('namespace');
|
|
|
|
let oldNS = this.get('oldNamespace');
|
|
|
|
if (!oldNS || ns !== oldNS) {
|
|
|
|
this.get('setForAnimation').perform();
|
2018-10-02 15:05:34 +00:00
|
|
|
this.get('fetchListCapability').perform();
|
2018-08-16 17:48:24 +00:00
|
|
|
}
|
|
|
|
this.set('oldNamespace', ns);
|
|
|
|
},
|
|
|
|
|
2018-10-02 15:05:34 +00:00
|
|
|
fetchListCapability: task(function*() {
|
|
|
|
try {
|
|
|
|
if (this.listCapability) {
|
2018-10-10 04:46:29 +00:00
|
|
|
yield this.listCapability.reload();
|
|
|
|
return;
|
2018-10-02 15:05:34 +00:00
|
|
|
}
|
|
|
|
let capability = yield this.store.findRecord('capabilities', 'sys/namespaces/');
|
|
|
|
this.set('listCapability', capability);
|
|
|
|
} catch (e) {
|
|
|
|
//do nothing here
|
|
|
|
}
|
|
|
|
}),
|
2018-08-16 17:48:24 +00:00
|
|
|
setForAnimation: task(function*() {
|
|
|
|
let leaves = this.get('menuLeaves');
|
|
|
|
let lastLeaves = this.get('lastMenuLeaves');
|
|
|
|
if (!lastLeaves) {
|
|
|
|
this.set('lastMenuLeaves', leaves);
|
|
|
|
yield timeout(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let isAdding = leaves.length > lastLeaves.length;
|
|
|
|
let changedLeaf = (isAdding ? leaves : lastLeaves).get('lastObject');
|
|
|
|
this.set('isAdding', isAdding);
|
|
|
|
this.set('changedLeaf', changedLeaf);
|
|
|
|
|
|
|
|
// if we're adding we want to render immediately an animate it in
|
|
|
|
// if we're not adding, we need time to move the item out before
|
|
|
|
// a rerender removes it
|
|
|
|
if (isAdding) {
|
|
|
|
this.set('lastMenuLeaves', leaves);
|
|
|
|
yield timeout(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
yield timeout(ANIMATION_DURATION);
|
|
|
|
this.set('lastMenuLeaves', leaves);
|
|
|
|
}).drop(),
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
isAnimating: alias('setForAnimation.isRunning'),
|
2018-08-16 17:48:24 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
namespacePath: alias('namespaceService.path'),
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
// this is an array of namespace paths that the current user
|
|
|
|
// has access to
|
2018-09-25 16:28:26 +00:00
|
|
|
accessibleNamespaces: alias('namespaceService.accessibleNamespaces'),
|
|
|
|
inRootNamespace: alias('namespaceService.inRootNamespace'),
|
2018-08-16 17:48:24 +00:00
|
|
|
|
|
|
|
namespaceTree: computed('accessibleNamespaces', function() {
|
|
|
|
let nsList = this.get('accessibleNamespaces');
|
|
|
|
|
|
|
|
if (!nsList) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return pathToTree(nsList);
|
|
|
|
}),
|
|
|
|
|
|
|
|
maybeAddRoot(leaves) {
|
|
|
|
let userRoot = this.get('auth.authData.userRootNamespace');
|
|
|
|
if (userRoot === '') {
|
|
|
|
leaves.unshift('');
|
|
|
|
}
|
|
|
|
|
|
|
|
return leaves.uniq();
|
|
|
|
},
|
|
|
|
|
|
|
|
pathToLeaf(path) {
|
|
|
|
// dots are allowed in namespace paths
|
|
|
|
// so we need to preserve them, and replace slashes with dots
|
|
|
|
// in order to use Ember's get function on the namespace tree
|
|
|
|
// to pull out the correct level
|
|
|
|
return (
|
|
|
|
path
|
|
|
|
// trim trailing slash
|
|
|
|
.replace(/\/$/, '')
|
|
|
|
// replace dots with snowman
|
|
|
|
.replace(/\.+/g, DOT_REPLACEMENT)
|
|
|
|
// replace slash with dots
|
|
|
|
.replace(/\/+/g, '.')
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
// an array that keeps track of what additional panels to render
|
|
|
|
// on the menu stack
|
|
|
|
// if you're in 'foo/bar/baz',
|
|
|
|
// this array will be: ['foo', 'foo.bar', 'foo.bar.baz']
|
|
|
|
// the template then iterates over this, and does Ember.get(namespaceTree, leaf)
|
|
|
|
// to render the nodes of each leaf
|
|
|
|
|
|
|
|
// gets set as 'lastMenuLeaves' in the ember concurrency task above
|
|
|
|
menuLeaves: computed('namespacePath', 'namespaceTree', function() {
|
|
|
|
let ns = this.get('namespacePath');
|
2019-08-01 21:53:57 +00:00
|
|
|
ns = ns.replace(/^\//, '');
|
2019-08-01 20:49:37 +00:00
|
|
|
let leaves = ancestorKeysForKey(ns);
|
2018-08-16 17:48:24 +00:00
|
|
|
leaves.push(ns);
|
|
|
|
leaves = this.maybeAddRoot(leaves);
|
|
|
|
|
|
|
|
leaves = leaves.map(this.pathToLeaf);
|
|
|
|
return leaves;
|
|
|
|
}),
|
|
|
|
|
|
|
|
// the nodes at the root of the namespace tree
|
|
|
|
// these will get rendered as the bottom layer
|
|
|
|
rootLeaves: computed('namespaceTree', function() {
|
|
|
|
let tree = this.get('namespaceTree');
|
|
|
|
let leaves = Object.keys(tree);
|
|
|
|
return leaves;
|
|
|
|
}),
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
currentLeaf: alias('lastMenuLeaves.lastObject'),
|
|
|
|
canAccessMultipleNamespaces: gt('accessibleNamespaces.length', 1),
|
2018-08-16 17:48:24 +00:00
|
|
|
isUserRootNamespace: computed('auth.authData.userRootNamespace', 'namespacePath', function() {
|
|
|
|
return this.get('auth.authData.userRootNamespace') === this.get('namespacePath');
|
|
|
|
}),
|
|
|
|
|
|
|
|
namespaceDisplay: computed('namespacePath', 'accessibleNamespaces', 'accessibleNamespaces.[]', function() {
|
|
|
|
let namespace = this.get('namespacePath');
|
|
|
|
if (namespace === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
let parts = namespace.split('/');
|
|
|
|
return parts[parts.length - 1];
|
|
|
|
}),
|
|
|
|
});
|