mirror of
https://github.com/ansible/awx.git
synced 2026-01-19 21:51:26 -03:30
Add shared add/edit controller, view, and resolve logic
This commit is contained in:
parent
a3099ac285
commit
475718a4de
@ -1,7 +1,6 @@
|
||||
function AddCredentialsController (models, $state) {
|
||||
let vm = this || {};
|
||||
|
||||
console.log($state.get());
|
||||
let me = models.me;
|
||||
let credential = models.credential;
|
||||
let credentialType = models.credentialType;
|
||||
@ -10,7 +9,7 @@ function AddCredentialsController (models, $state) {
|
||||
omit: ['user', 'team', 'inputs']
|
||||
});
|
||||
|
||||
vm.form.credential_type._data = credentialType.getResults();
|
||||
vm.form.credential_type._data = credentialType.get('results');
|
||||
vm.form.credential_type._placeholder = 'SELECT A TYPE';
|
||||
vm.form.credential_type._display = 'name';
|
||||
vm.form.credential_type._key = 'id';
|
||||
@ -24,9 +23,9 @@ function AddCredentialsController (models, $state) {
|
||||
};
|
||||
|
||||
vm.form.save = data => {
|
||||
data.user = me.getId();
|
||||
|
||||
return credential.post(data);
|
||||
data.user = me.get('results[0].id');
|
||||
|
||||
return credential.request('post', data);
|
||||
};
|
||||
|
||||
vm.form.saveSuccess = res => {
|
||||
|
||||
@ -40,23 +40,38 @@ function config ($stateExtenderProvider, pathServiceProvider) {
|
||||
}
|
||||
});
|
||||
|
||||
function CredentialsAddResolve ($q, meModel, credentialModel, credentialTypeModel) {
|
||||
let promises = [
|
||||
meModel.get(),
|
||||
credentialModel.options(),
|
||||
credentialTypeModel.get()
|
||||
];
|
||||
function CredentialsResolve ($q, params, Me, Credential, CredentialType) {
|
||||
let models;
|
||||
let id = params.id;
|
||||
let promises = {
|
||||
me: new Me('get')
|
||||
};
|
||||
|
||||
if (!id) {
|
||||
promises.credential = new Credential('options');
|
||||
promises.credentialType = new CredentialType('get');
|
||||
|
||||
return $q.all(promises).then(models => models);
|
||||
}
|
||||
|
||||
promises.credential = new Credential('get', id);
|
||||
|
||||
return $q.all(promises)
|
||||
.then(() => ({
|
||||
me: meModel,
|
||||
credential: credentialModel,
|
||||
credentialType: credentialTypeModel
|
||||
}));
|
||||
.then(_models_ => {
|
||||
models = _models_;
|
||||
|
||||
return new CredentialType('get', models.credential.get('id'));
|
||||
})
|
||||
.then(credentialType => {
|
||||
models.credentialType = credentialType;
|
||||
|
||||
return models;
|
||||
});
|
||||
}
|
||||
|
||||
CredentialsAddResolve.$inject = [
|
||||
CredentialsResolve.$inject = [
|
||||
'$q',
|
||||
'$stateParams',
|
||||
'MeModel',
|
||||
'CredentialModel',
|
||||
'CredentialTypeModel'
|
||||
@ -76,29 +91,27 @@ function config ($stateExtenderProvider, pathServiceProvider) {
|
||||
}
|
||||
},
|
||||
resolve: {
|
||||
resolvedModels: CredentialsAddResolve
|
||||
resolvedModels: CredentialsResolve
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
*stateExtender.addState({
|
||||
* name: 'credentials.edit',
|
||||
* route: '/edit/:id',
|
||||
* ncyBreadcrumb: {
|
||||
* label: N_('EDIT')
|
||||
* },
|
||||
* views: {
|
||||
* 'edit@credentials': {
|
||||
* templateUrl: pathService.getViewPath('credentials/add-credentials'),
|
||||
* controller: AddController,
|
||||
* controllerAs: 'vm'
|
||||
* },
|
||||
* resolve: {
|
||||
* resolvedModels: CredentialsAddResolve
|
||||
* }
|
||||
* }
|
||||
*});
|
||||
*/
|
||||
stateExtender.addState({
|
||||
name: 'credentials.edit',
|
||||
route: '/edit/:id',
|
||||
ncyBreadcrumb: {
|
||||
label: N_('EDIT')
|
||||
},
|
||||
views: {
|
||||
'edit@credentials': {
|
||||
templateUrl: pathService.getViewPath('credentials/add-credentials'),
|
||||
controller: AddController,
|
||||
controllerAs: 'vm'
|
||||
}
|
||||
},
|
||||
resolve: {
|
||||
resolvedModels: CredentialsResolve
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
config.$inject = [
|
||||
|
||||
@ -65,7 +65,7 @@ export default ['$scope', 'Rest', 'CredentialList', 'Prompt', 'ClearScope',
|
||||
};
|
||||
|
||||
$scope.editCredential = function(id) {
|
||||
$state.go('credentials.edit', { credential_id: id });
|
||||
$state.go('credentials.edit', { id: id });
|
||||
};
|
||||
|
||||
$scope.deleteCredential = function(id, name) {
|
||||
|
||||
@ -1,77 +1,123 @@
|
||||
function BaseModel ($http) {
|
||||
return function extend (path) {
|
||||
this.get = () => {
|
||||
let request = {
|
||||
method: 'GET',
|
||||
url: this.path
|
||||
};
|
||||
let $http;
|
||||
|
||||
return $http(request)
|
||||
.then(response => {
|
||||
this.model.get = response;
|
||||
function request (method, ...args) {
|
||||
method = method.toUpperCase();
|
||||
|
||||
return response;
|
||||
});
|
||||
};
|
||||
|
||||
this.post = data => {
|
||||
let request = {
|
||||
method: 'POST',
|
||||
url: this.path,
|
||||
data,
|
||||
};
|
||||
|
||||
return $http(request)
|
||||
.then(response => {
|
||||
this.model.post = response;
|
||||
|
||||
return response;
|
||||
});
|
||||
};
|
||||
|
||||
this.options = () => {
|
||||
let request = {
|
||||
method: 'OPTIONS',
|
||||
url: this.path
|
||||
};
|
||||
|
||||
return $http(request)
|
||||
.then(response => {
|
||||
this.model.options = response;
|
||||
|
||||
return response;
|
||||
});
|
||||
};
|
||||
|
||||
this.getOptions = (method, key) => {
|
||||
if (!method) {
|
||||
return this.model.options.data;
|
||||
}
|
||||
|
||||
method = method.toUpperCase();
|
||||
|
||||
if (method && !key) {
|
||||
return this.model.options.data.actions[method];
|
||||
}
|
||||
|
||||
if (method && key) {
|
||||
return this.model.options.data.actions[method][key];
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
this.normalizePath = resource => {
|
||||
let version = '/api/v2/';
|
||||
|
||||
return `${version}${resource}/`;
|
||||
};
|
||||
|
||||
this.model = {};
|
||||
this.path = this.normalizePath(path);
|
||||
};
|
||||
switch (method) {
|
||||
case 'OPTIONS':
|
||||
return this.httpOptions(...args);
|
||||
case 'GET':
|
||||
return this.httpGet(...args);
|
||||
case 'POST':
|
||||
return this.httpPost(...args);
|
||||
}
|
||||
}
|
||||
|
||||
BaseModel.$inject = ['$http'];
|
||||
function httpGet (id) {
|
||||
let req = {
|
||||
method: 'GET',
|
||||
url: this.path
|
||||
};
|
||||
|
||||
export default BaseModel;
|
||||
if (id) {
|
||||
req.url = `${this.path}/${id}`;
|
||||
}
|
||||
|
||||
return $http(req)
|
||||
.then(res => {
|
||||
this.res = res;
|
||||
this.model = res.data;
|
||||
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
function httpPost (data) {
|
||||
let req = {
|
||||
method: 'POST',
|
||||
url: this.path,
|
||||
data
|
||||
};
|
||||
|
||||
return $http(req)
|
||||
.then(res => {
|
||||
this.res = res;
|
||||
this.model = res.data;
|
||||
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
function httpOptions () {
|
||||
let req = {
|
||||
method: 'OPTIONS',
|
||||
url: this.path
|
||||
};
|
||||
|
||||
return $http(req)
|
||||
.then(res => {
|
||||
this.res = res;
|
||||
this.model = res.data;
|
||||
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
function get (_keys_) {
|
||||
let keys = _keys_.split('.');
|
||||
let value = this.model;
|
||||
|
||||
try {
|
||||
keys.forEach(key => {
|
||||
let bracketIndex = key.indexOf('[');
|
||||
let hasArray = bracketIndex !== -1;
|
||||
|
||||
if (!hasArray) {
|
||||
value = value[key];
|
||||
return;
|
||||
}
|
||||
|
||||
if (bracketIndex === 0) {
|
||||
value = value[Number(key.substring(1, key.length - 1))];
|
||||
return;
|
||||
}
|
||||
|
||||
let prop = key.substring(0, bracketIndex);
|
||||
let index = Number(key.substring(bracketIndex + 1, key.length - 1));
|
||||
|
||||
value = value[prop][index];
|
||||
});
|
||||
} catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function normalizePath (resource) {
|
||||
let version = '/api/v2/';
|
||||
|
||||
return `${version}${resource}/`;
|
||||
}
|
||||
|
||||
function BaseModel (path) {
|
||||
this.get = get;
|
||||
this.httpGet = httpGet;
|
||||
this.httpOptions = httpOptions;
|
||||
this.httpPost = httpPost;
|
||||
this.normalizePath = normalizePath;
|
||||
this.request = request;
|
||||
|
||||
this.model = {};
|
||||
this.path = this.normalizePath(path);
|
||||
};
|
||||
|
||||
function BaseModelLoader (_$http_) {
|
||||
$http = _$http_;
|
||||
|
||||
return BaseModel;
|
||||
}
|
||||
|
||||
BaseModelLoader.$inject = ['$http'];
|
||||
|
||||
export default BaseModelLoader;
|
||||
|
||||
@ -1,23 +1,36 @@
|
||||
function CredentialModel (BaseModel, CredentialTypeModel) {
|
||||
BaseModel.call(this, 'credentials');
|
||||
|
||||
this.createFormSchema = (type, config) => {
|
||||
let schema = Object.assign({}, this.getOptions(type));
|
||||
let BaseModel;
|
||||
|
||||
if (config && config.omit) {
|
||||
config.omit.forEach(key => {
|
||||
delete schema[key];
|
||||
});
|
||||
}
|
||||
function createFormSchema (type, config) {
|
||||
let schema = Object.assign({}, this.get('actions.POST'));
|
||||
|
||||
for (let key in schema) {
|
||||
schema[key].id = key;
|
||||
}
|
||||
if (config && config.omit) {
|
||||
config.omit.forEach(key => {
|
||||
delete schema[key];
|
||||
});
|
||||
}
|
||||
|
||||
return schema;
|
||||
};
|
||||
for (let key in schema) {
|
||||
schema[key].id = key;
|
||||
}
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
CredentialModel.$inject = ['BaseModel', 'CredentialTypeModel'];
|
||||
function CredentialModel (method, id) {
|
||||
BaseModel.call(this, 'credentials');
|
||||
|
||||
this.createFormSchema = createFormSchema;
|
||||
|
||||
export default CredentialModel;
|
||||
return this.request(method, id)
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
function CredentialModelLoader (_BaseModel_ ) {
|
||||
BaseModel = _BaseModel_;
|
||||
|
||||
return CredentialModel;
|
||||
}
|
||||
|
||||
CredentialModelLoader.$inject = ['BaseModel'];
|
||||
|
||||
export default CredentialModelLoader;
|
||||
|
||||
@ -1,37 +1,47 @@
|
||||
function CredentialTypeModel (BaseModel) {
|
||||
BaseModel.call(this, 'credential_types');
|
||||
let BaseModel;
|
||||
|
||||
this.categorizeByKind = () => {
|
||||
let group = {};
|
||||
function categorizeByKind () {
|
||||
let group = {};
|
||||
|
||||
this.model.get.data.results.forEach(result => {
|
||||
group[result.kind] = group[result.kind] || [];
|
||||
group[result.kind].push(result);
|
||||
});
|
||||
this.get('results').forEach(result => {
|
||||
group[result.kind] = group[result.kind] || [];
|
||||
group[result.kind].push(result);
|
||||
});
|
||||
|
||||
return Object.keys(group).map(category => ({
|
||||
data: group[category],
|
||||
category
|
||||
}));
|
||||
};
|
||||
|
||||
this.mergeInputProperties = type => {
|
||||
return type.inputs.fields.map(field => {
|
||||
if (!type.inputs.required || type.inputs.required.indexOf(field.id) === -1) {
|
||||
field.required = false;
|
||||
} else {
|
||||
field.required = true;
|
||||
}
|
||||
|
||||
return field;
|
||||
});
|
||||
};
|
||||
|
||||
this.getResults = () => {
|
||||
return this.model.get.data.results;
|
||||
};
|
||||
return Object.keys(group).map(category => ({
|
||||
data: group[category],
|
||||
category
|
||||
}));
|
||||
}
|
||||
|
||||
CredentialTypeModel.$inject = ['BaseModel'];
|
||||
function mergeInputProperties (type) {
|
||||
return type.inputs.fields.map(field => {
|
||||
if (!type.inputs.required || type.inputs.required.indexOf(field.id) === -1) {
|
||||
field.required = false;
|
||||
} else {
|
||||
field.required = true;
|
||||
}
|
||||
|
||||
export default CredentialTypeModel;
|
||||
return field;
|
||||
});
|
||||
}
|
||||
|
||||
function CredentialTypeModel (method, id) {
|
||||
BaseModel.call(this, 'credential_types');
|
||||
|
||||
this.categorizeByKind = categorizeByKind;
|
||||
this.mergeInputProperties = mergeInputProperties;
|
||||
|
||||
return this.request(method, id)
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
function CredentialTypeModelLoader (_BaseModel_) {
|
||||
BaseModel = _BaseModel_;
|
||||
|
||||
return CredentialTypeModel;
|
||||
}
|
||||
|
||||
CredentialTypeModelLoader.$inject = ['BaseModel'];
|
||||
|
||||
export default CredentialTypeModelLoader;
|
||||
|
||||
@ -1,11 +1,18 @@
|
||||
function MeModel (BaseModel) {
|
||||
let BaseModel;
|
||||
|
||||
function MeModel (method) {
|
||||
BaseModel.call(this, 'me');
|
||||
|
||||
this.getId = () => {
|
||||
return this.model.get.data.results[0].id;
|
||||
};
|
||||
return this.request(method)
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
MeModel.$inject = ['BaseModel'];
|
||||
function MeModelLoader (_BaseModel_) {
|
||||
BaseModel = _BaseModel_;
|
||||
|
||||
export default MeModel;
|
||||
return MeModel;
|
||||
}
|
||||
|
||||
MeModelLoader.$inject = ['BaseModel'];
|
||||
|
||||
export default MeModelLoader;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user