mirror of
https://github.com/ansible/awx.git
synced 2026-05-16 05:47:38 -02:30
Add shared add/edit controller, view, and resolve logic
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
function AddCredentialsController (models, $state) {
|
function AddCredentialsController (models, $state) {
|
||||||
let vm = this || {};
|
let vm = this || {};
|
||||||
|
|
||||||
console.log($state.get());
|
|
||||||
let me = models.me;
|
let me = models.me;
|
||||||
let credential = models.credential;
|
let credential = models.credential;
|
||||||
let credentialType = models.credentialType;
|
let credentialType = models.credentialType;
|
||||||
@@ -10,7 +9,7 @@ function AddCredentialsController (models, $state) {
|
|||||||
omit: ['user', 'team', 'inputs']
|
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._placeholder = 'SELECT A TYPE';
|
||||||
vm.form.credential_type._display = 'name';
|
vm.form.credential_type._display = 'name';
|
||||||
vm.form.credential_type._key = 'id';
|
vm.form.credential_type._key = 'id';
|
||||||
@@ -24,9 +23,9 @@ function AddCredentialsController (models, $state) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
vm.form.save = data => {
|
vm.form.save = data => {
|
||||||
data.user = me.getId();
|
data.user = me.get('results[0].id');
|
||||||
|
|
||||||
return credential.post(data);
|
return credential.request('post', data);
|
||||||
};
|
};
|
||||||
|
|
||||||
vm.form.saveSuccess = res => {
|
vm.form.saveSuccess = res => {
|
||||||
|
|||||||
@@ -40,23 +40,38 @@ function config ($stateExtenderProvider, pathServiceProvider) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function CredentialsAddResolve ($q, meModel, credentialModel, credentialTypeModel) {
|
function CredentialsResolve ($q, params, Me, Credential, CredentialType) {
|
||||||
let promises = [
|
let models;
|
||||||
meModel.get(),
|
let id = params.id;
|
||||||
credentialModel.options(),
|
let promises = {
|
||||||
credentialTypeModel.get()
|
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)
|
return $q.all(promises)
|
||||||
.then(() => ({
|
.then(_models_ => {
|
||||||
me: meModel,
|
models = _models_;
|
||||||
credential: credentialModel,
|
|
||||||
credentialType: credentialTypeModel
|
return new CredentialType('get', models.credential.get('id'));
|
||||||
}));
|
})
|
||||||
|
.then(credentialType => {
|
||||||
|
models.credentialType = credentialType;
|
||||||
|
|
||||||
|
return models;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
CredentialsAddResolve.$inject = [
|
CredentialsResolve.$inject = [
|
||||||
'$q',
|
'$q',
|
||||||
|
'$stateParams',
|
||||||
'MeModel',
|
'MeModel',
|
||||||
'CredentialModel',
|
'CredentialModel',
|
||||||
'CredentialTypeModel'
|
'CredentialTypeModel'
|
||||||
@@ -76,29 +91,27 @@ function config ($stateExtenderProvider, pathServiceProvider) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
resolvedModels: CredentialsAddResolve
|
resolvedModels: CredentialsResolve
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
stateExtender.addState({
|
||||||
*stateExtender.addState({
|
name: 'credentials.edit',
|
||||||
* name: 'credentials.edit',
|
route: '/edit/:id',
|
||||||
* route: '/edit/:id',
|
ncyBreadcrumb: {
|
||||||
* ncyBreadcrumb: {
|
label: N_('EDIT')
|
||||||
* label: N_('EDIT')
|
},
|
||||||
* },
|
views: {
|
||||||
* views: {
|
'edit@credentials': {
|
||||||
* 'edit@credentials': {
|
templateUrl: pathService.getViewPath('credentials/add-credentials'),
|
||||||
* templateUrl: pathService.getViewPath('credentials/add-credentials'),
|
controller: AddController,
|
||||||
* controller: AddController,
|
controllerAs: 'vm'
|
||||||
* controllerAs: 'vm'
|
}
|
||||||
* },
|
},
|
||||||
* resolve: {
|
resolve: {
|
||||||
* resolvedModels: CredentialsAddResolve
|
resolvedModels: CredentialsResolve
|
||||||
* }
|
}
|
||||||
* }
|
});
|
||||||
*});
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config.$inject = [
|
config.$inject = [
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ export default ['$scope', 'Rest', 'CredentialList', 'Prompt', 'ClearScope',
|
|||||||
};
|
};
|
||||||
|
|
||||||
$scope.editCredential = function(id) {
|
$scope.editCredential = function(id) {
|
||||||
$state.go('credentials.edit', { credential_id: id });
|
$state.go('credentials.edit', { id: id });
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.deleteCredential = function(id, name) {
|
$scope.deleteCredential = function(id, name) {
|
||||||
|
|||||||
@@ -1,77 +1,123 @@
|
|||||||
function BaseModel ($http) {
|
let $http;
|
||||||
return function extend (path) {
|
|
||||||
this.get = () => {
|
|
||||||
let request = {
|
|
||||||
method: 'GET',
|
|
||||||
url: this.path
|
|
||||||
};
|
|
||||||
|
|
||||||
return $http(request)
|
function request (method, ...args) {
|
||||||
.then(response => {
|
method = method.toUpperCase();
|
||||||
this.model.get = response;
|
|
||||||
|
|
||||||
return response;
|
switch (method) {
|
||||||
});
|
case 'OPTIONS':
|
||||||
};
|
return this.httpOptions(...args);
|
||||||
|
case 'GET':
|
||||||
this.post = data => {
|
return this.httpGet(...args);
|
||||||
let request = {
|
case 'POST':
|
||||||
method: 'POST',
|
return this.httpPost(...args);
|
||||||
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);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
let BaseModel;
|
||||||
BaseModel.call(this, 'credentials');
|
|
||||||
|
|
||||||
this.createFormSchema = (type, config) => {
|
|
||||||
let schema = Object.assign({}, this.getOptions(type));
|
|
||||||
|
|
||||||
if (config && config.omit) {
|
function createFormSchema (type, config) {
|
||||||
config.omit.forEach(key => {
|
let schema = Object.assign({}, this.get('actions.POST'));
|
||||||
delete schema[key];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let key in schema) {
|
if (config && config.omit) {
|
||||||
schema[key].id = key;
|
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) {
|
let BaseModel;
|
||||||
BaseModel.call(this, 'credential_types');
|
|
||||||
|
|
||||||
this.categorizeByKind = () => {
|
function categorizeByKind () {
|
||||||
let group = {};
|
let group = {};
|
||||||
|
|
||||||
this.model.get.data.results.forEach(result => {
|
this.get('results').forEach(result => {
|
||||||
group[result.kind] = group[result.kind] || [];
|
group[result.kind] = group[result.kind] || [];
|
||||||
group[result.kind].push(result);
|
group[result.kind].push(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
return Object.keys(group).map(category => ({
|
return Object.keys(group).map(category => ({
|
||||||
data: group[category],
|
data: group[category],
|
||||||
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;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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');
|
BaseModel.call(this, 'me');
|
||||||
|
|
||||||
this.getId = () => {
|
return this.request(method)
|
||||||
return this.model.get.data.results[0].id;
|
.then(() => this);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MeModel.$inject = ['BaseModel'];
|
function MeModelLoader (_BaseModel_) {
|
||||||
|
BaseModel = _BaseModel_;
|
||||||
|
|
||||||
export default MeModel;
|
return MeModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
MeModelLoader.$inject = ['BaseModel'];
|
||||||
|
|
||||||
|
export default MeModelLoader;
|
||||||
|
|||||||
Reference in New Issue
Block a user