github.com/emate/nomad@v0.8.2-wo-binpacking/ui/app/services/system.js (about) 1 import Service, { inject as service } from '@ember/service'; 2 import { computed } from '@ember/object'; 3 import PromiseObject from '../utils/classes/promise-object'; 4 import { namespace } from '../adapters/application'; 5 6 export default Service.extend({ 7 token: service(), 8 store: service(), 9 10 leader: computed(function() { 11 const token = this.get('token'); 12 13 return PromiseObject.create({ 14 promise: token 15 .authorizedRequest(`/${namespace}/status/leader`) 16 .then(res => res.json()) 17 .then(rpcAddr => ({ rpcAddr })) 18 .then(leader => { 19 // Dirty self so leader can be used as a dependent key 20 this.notifyPropertyChange('leader.rpcAddr'); 21 return leader; 22 }), 23 }); 24 }), 25 26 namespaces: computed(function() { 27 return this.get('store').findAll('namespace'); 28 }), 29 30 shouldShowNamespaces: computed('namespaces.[]', function() { 31 const namespaces = this.get('namespaces').toArray(); 32 return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default'); 33 }), 34 35 activeNamespace: computed('namespaces.[]', { 36 get() { 37 const namespaceId = window.localStorage.nomadActiveNamespace || 'default'; 38 const namespace = this.get('namespaces').findBy('id', namespaceId); 39 40 if (namespace) { 41 return namespace; 42 } 43 44 // If the namespace is localStorage is no longer in the cluster, it needs to 45 // be cleared from localStorage 46 this.set('activeNamespace', null); 47 return this.get('namespaces').findBy('id', 'default'); 48 }, 49 set(key, value) { 50 if (value == null) { 51 window.localStorage.removeItem('nomadActiveNamespace'); 52 } else if (typeof value === 'string') { 53 window.localStorage.nomadActiveNamespace = value; 54 return this.get('namespaces').findBy('id', value); 55 } else { 56 window.localStorage.nomadActiveNamespace = value.get('name'); 57 return value; 58 } 59 }, 60 }), 61 });