mirror of https://github.com/rancher/ui.git
131 lines
3.4 KiB
JavaScript
131 lines
3.4 KiB
JavaScript
import Ember from 'ember';
|
|
import C from 'ui/utils/constants';
|
|
|
|
export default Ember.Component.extend({
|
|
catalog: Ember.inject.service(),
|
|
project: null,
|
|
catalogs: null,
|
|
ary: null,
|
|
global: null,
|
|
|
|
kindChoices: [
|
|
{translationKey: 'catalogSettings.more.kind.native', value: 'native'},
|
|
{translationKey: 'catalogSettings.more.kind.helm', value: 'helm'},
|
|
],
|
|
|
|
actions: {
|
|
add() {
|
|
this.get('ary').pushObject(Ember.Object.create({
|
|
name: '',
|
|
branch: C.CATALOG.DEFAULT_BRANCH,
|
|
url: '',
|
|
kind: 'native',
|
|
toAdd: true
|
|
}));
|
|
|
|
Ember.run.next(() => {
|
|
if ( this.isDestroyed || this.isDestroying ) {
|
|
return;
|
|
}
|
|
|
|
this.$('INPUT.name').last()[0].focus();
|
|
});
|
|
},
|
|
remove(obj) {
|
|
Ember.set(obj, 'toRemove', true);
|
|
},
|
|
save(cb) {
|
|
if (this.validate()) {
|
|
this.set('errors', []);
|
|
var newCatalogs = this.get('ary').filterBy('toAdd', true);
|
|
var catalogsToRemove = this.get('ary').filterBy('toRemove', true);
|
|
var all = [];
|
|
|
|
newCatalogs.forEach((cat) => {
|
|
all.push(this.addCatalogs(cat));
|
|
});
|
|
|
|
catalogsToRemove.forEach((cat) => {
|
|
all.push(this.removeCatalogs(cat));
|
|
});
|
|
|
|
Ember.RSVP.all(all).then(() => {
|
|
this.set('catalog.componentRequestingRefresh', true);
|
|
this.set('saving', false);
|
|
cb(true);
|
|
Ember.run.later(() => {
|
|
this.sendAction('cancel');
|
|
}, 500);
|
|
}).catch((err) => {
|
|
this.set('errors',err);
|
|
cb(false);
|
|
this.set('saving', false);
|
|
});
|
|
} else {
|
|
cb(false);
|
|
this.set('saving', false);
|
|
}
|
|
}
|
|
},
|
|
|
|
validate() {
|
|
var errors = [];
|
|
var global = this.get('global');
|
|
var ary = this.get('ary');
|
|
|
|
ary.forEach((cat) => {
|
|
if ( (cat.name||'').trim().length === 0 ) {
|
|
errors.push('Name is required on each catalog');
|
|
}
|
|
|
|
if ( (cat.url||'').trim().length === 0 ) {
|
|
errors.push('URL is required on each catalog');
|
|
}
|
|
|
|
if ( global.filter((x) => (x.name||'').trim().toLowerCase() === cat.name.toLowerCase()).length > 1 ||
|
|
ary.filter((x) => (x.name||'').trim().toLowerCase() === cat.name.toLowerCase()).length > 1) {
|
|
errors.push('Each catalog must have a unique name');
|
|
}
|
|
});
|
|
|
|
if ( errors.length ) {
|
|
this.set('errors',errors.uniq());
|
|
return false;
|
|
} else {
|
|
this.set('errors', null);
|
|
}
|
|
|
|
return true;
|
|
},
|
|
|
|
addCatalogs(catalogs) {
|
|
return this.get('store').request({
|
|
url: `${this.get('app.catalogEndpoint')}/catalogs`,
|
|
method: 'POST',
|
|
headers: {
|
|
[C.HEADER.PROJECT_ID]: this.get('project.id')
|
|
},
|
|
body: JSON.stringify(catalogs)
|
|
});
|
|
},
|
|
|
|
removeCatalogs(catalogs) {
|
|
return this.get('store').request({
|
|
url: `${this.get('app.catalogEndpoint')}/catalogs/${catalogs.name}`,
|
|
method: 'DELETE',
|
|
headers: {
|
|
[C.HEADER.PROJECT_ID]: this.get('project.id')
|
|
},
|
|
body: JSON.stringify(catalogs)
|
|
});
|
|
},
|
|
|
|
init() {
|
|
this._super(...arguments);
|
|
this.setProperties({
|
|
ary: this.get('catalogs').filterBy('environmentId', this.get('project.id')),
|
|
global: this.get('catalogs').filterBy('environmentId', 'global') // this should change to falsey check when josh updates the catalog to remove 'global' from the id
|
|
});
|
|
}
|
|
});
|