mirror of
https://github.com/portainer/portainer.git
synced 2025-07-20 13:59:40 +02:00
* refactor(app): replace notification with es6 service (#6015) [EE-1897] chore(app): format * refactor(containers): remove the dependency on angular modal service (#6017) [EE-1898] * refactor(app): remove angular from http-request [EE-1899] (#6016) * feat(app): add axios [EE-2035](#6077) * refactor(feature): remove angular dependency from feature service [EE-2034] (#6078) * refactor(app): replace box-selector with react component (#6046) fix: rename angular2react refactor(app): make box-selector type generic feat(app): add story for box-selector feat(app): test box-selector feat(app): add stories for box selector item fix(app): remove unneccesary element refactor(app): remove assign * feat(feature): add be-indicator in react [EE-2005] (#6106) * refactor(app): add react components for headers [EE-1949] (#6023) * feat(auth): provide user context * feat(app): added base header component [EE-1949] style(app): reformat refactor(app/header): use same api as angular * feat(app): add breadcrumbs component [EE-2024] * feat(app): remove u element from user links * fix(users): handle axios errors Co-authored-by: Chaim Lev-Ari <chiptus@gmail.com> * refactor(app): convert switch component to react [EE-2005] (#6025) Co-authored-by: Marcelo Rydel <marcelorydel26@gmail.com>
169 lines
5 KiB
JavaScript
169 lines
5 KiB
JavaScript
import { buildOption } from '@/portainer/components/BoxSelector';
|
|
import { AccessControlFormData } from '@/portainer/components/accessControlForm/porAccessControlFormModel';
|
|
|
|
class KubeCreateCustomTemplateViewController {
|
|
/* @ngInject */
|
|
constructor($async, $state, Authentication, CustomTemplateService, FormValidator, ModalService, Notifications, ResourceControlService) {
|
|
Object.assign(this, { $async, $state, Authentication, CustomTemplateService, FormValidator, ModalService, Notifications, ResourceControlService });
|
|
|
|
this.methodOptions = [
|
|
buildOption('method_editor', 'fa fa-edit', 'Web editor', 'Use our Web editor', 'editor'),
|
|
buildOption('method_upload', 'fa fa-upload', 'Upload', 'Upload from your computer', 'upload'),
|
|
];
|
|
|
|
this.templates = null;
|
|
|
|
this.state = {
|
|
method: 'editor',
|
|
actionInProgress: false,
|
|
formValidationError: '',
|
|
isEditorDirty: false,
|
|
};
|
|
|
|
this.formValues = {
|
|
FileContent: '',
|
|
File: null,
|
|
Title: '',
|
|
Description: '',
|
|
Note: '',
|
|
Logo: '',
|
|
AccessControlData: new AccessControlFormData(),
|
|
};
|
|
|
|
this.onChangeFile = this.onChangeFile.bind(this);
|
|
this.onChangeFileContent = this.onChangeFileContent.bind(this);
|
|
this.onChangeMethod = this.onChangeMethod.bind(this);
|
|
this.onBeforeOnload = this.onBeforeOnload.bind(this);
|
|
}
|
|
|
|
onChangeMethod(method) {
|
|
this.state.method = method;
|
|
}
|
|
|
|
onChangeFileContent(content) {
|
|
this.formValues.FileContent = content;
|
|
this.state.isEditorDirty = true;
|
|
}
|
|
|
|
onChangeFile(file) {
|
|
this.formValues.File = file;
|
|
}
|
|
|
|
async createCustomTemplate() {
|
|
return this.$async(async () => {
|
|
const { method } = this.state;
|
|
|
|
if (!this.validateForm(method)) {
|
|
return;
|
|
}
|
|
|
|
this.state.actionInProgress = true;
|
|
try {
|
|
const customTemplate = await this.createCustomTemplateByMethod(method, this.formValues);
|
|
|
|
const accessControlData = this.formValues.AccessControlData;
|
|
const userDetails = this.Authentication.getUserDetails();
|
|
const userId = userDetails.ID;
|
|
await this.ResourceControlService.applyResourceControl(userId, accessControlData, customTemplate.ResourceControl);
|
|
|
|
this.Notifications.success('Custom template successfully created');
|
|
this.state.isEditorDirty = false;
|
|
this.$state.go('kubernetes.templates.custom');
|
|
} catch (err) {
|
|
this.Notifications.error('Failure', err, 'Failed creating custom template');
|
|
} finally {
|
|
this.state.actionInProgress = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
createCustomTemplateByMethod(method, template) {
|
|
template.Type = 3;
|
|
|
|
switch (method) {
|
|
case 'editor':
|
|
return this.createCustomTemplateFromFileContent(template);
|
|
case 'upload':
|
|
return this.createCustomTemplateFromFileUpload(template);
|
|
}
|
|
}
|
|
|
|
createCustomTemplateFromFileContent(template) {
|
|
return this.CustomTemplateService.createCustomTemplateFromFileContent(template);
|
|
}
|
|
|
|
createCustomTemplateFromFileUpload(template) {
|
|
return this.CustomTemplateService.createCustomTemplateFromFileUpload(template);
|
|
}
|
|
|
|
validateForm(method) {
|
|
this.state.formValidationError = '';
|
|
|
|
if (method === 'editor' && this.formValues.FileContent === '') {
|
|
this.state.formValidationError = 'Template file content must not be empty';
|
|
return false;
|
|
}
|
|
|
|
const title = this.formValues.Title;
|
|
const isNotUnique = this.templates.some((template) => template.Title === title);
|
|
if (isNotUnique) {
|
|
this.state.formValidationError = 'A template with the same name already exists';
|
|
return false;
|
|
}
|
|
|
|
const isAdmin = this.Authentication.isAdmin();
|
|
const accessControlData = this.formValues.AccessControlData;
|
|
const error = this.FormValidator.validateAccessControl(accessControlData, isAdmin);
|
|
|
|
if (error) {
|
|
this.state.formValidationError = error;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
async $onInit() {
|
|
return this.$async(async () => {
|
|
const { fileContent, type } = this.$state.params;
|
|
|
|
this.formValues.FileContent = fileContent;
|
|
if (type) {
|
|
this.formValues.Type = +type;
|
|
}
|
|
|
|
try {
|
|
this.templates = await this.CustomTemplateService.customTemplates(3);
|
|
} catch (err) {
|
|
this.Notifications.error('Failure loading', err, 'Failed loading custom templates');
|
|
}
|
|
|
|
this.state.loading = false;
|
|
|
|
window.addEventListener('beforeunload', this.onBeforeOnload);
|
|
});
|
|
}
|
|
|
|
$onDestroy() {
|
|
window.removeEventListener('beforeunload', this.onBeforeOnload);
|
|
}
|
|
|
|
isEditorDirty() {
|
|
return this.state.method === 'editor' && this.formValues.FileContent && this.state.isEditorDirty;
|
|
}
|
|
|
|
onBeforeOnload(event) {
|
|
if (this.isEditorDirty()) {
|
|
event.preventDefault();
|
|
event.returnValue = '';
|
|
}
|
|
}
|
|
|
|
uiCanExit() {
|
|
if (this.isEditorDirty()) {
|
|
return this.ModalService.confirmWebEditorDiscard();
|
|
}
|
|
}
|
|
}
|
|
|
|
export default KubeCreateCustomTemplateViewController;
|