Skip to content
Snippets Groups Projects
Commit e06e8cd2 authored by Takumaru Sekine's avatar Takumaru Sekine Committed by Yas Naoi
Browse files

Issue #3253229 by sekinet, yas: Refactor the K8s launch template edit form (Tabs for Git and YAML)

parent 9c32c636
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@
labels: {
info: '',
},
footer: dataTable.classList.contains('show-footer') ? true: false,
footer: dataTable.classList.contains('show-footer') ? true : false,
paging: false,
perPage: 9999,
perPageSelect: false,
......
......@@ -15,7 +15,7 @@ entity_type: cloud_launch_template
bundle: k8s
label: 'Source type'
description: 'Select the source type of Git repository.'
required: false
required: true
translatable: false
default_value:
-
......
(function ($) {
'use strict';
const active_tab_observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
// changed vertival tab.
updateSourcetypeOptions(mutation.target.value);
});
});
const tab_configuration_observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
let active_tab = mutation.target.className.includes('active') ? 'git' : 'yml';
// changed vertival tab.
updateSourcetypeOptions(active_tab);
});
});
active_tab_observer.observe(
document.querySelector('.vertical-tabs__active-tab'),
{
attributes: true,
attributeFilter: ['value']
}
);
tab_configuration_observer.observe(
document.querySelector('#edit-configuration-git'),
{
attributes: true,
attributeFilter: ['class']
}
);
let updateSourcetypeOptions = function (active_tab) {
if (!active_tab) {
return;
}
$('#edit-field-source-type option').each(function () {
let source_type = $(this).val();
if (source_type == '_none') {
$(this).hide();
return true;
}
if(active_tab.includes('git') && source_type === 'yml') {
$(this).hide();
return true;
}
if (active_tab.includes('git') && source_type === 'git') {
$(this).prop('selected', 'selected');
} else {
if ($(this).prop('selected')) {
$(this).prop('selected', '');
}
}
});
};
})(jQuery);
(function ($, Drupal, drupalSettings) {
'use strict';
let changeTabBySourcetype = function (context) {
if ($("select[name='field_source_type']").length > 0) {
const $source_type = $("select[name='field_source_type']")[0].value.includes('git') ? 'git' : 'yml';
$(context).find('[data-vertical-tabs-panes]').each(function () {
const $tab_tag = $(this).find('> details').length > 0 ? 'details' : 'div';
const $tabs = $(this).addClass('vertical-tabs__panes').find($tab_tag + '[id*="edit-configuration"]')
const $select_tab_id = 'edit-configuration-' + $source_type;
let $tab_focus = '';
$tabs.each(function () {
if (this.id === $select_tab_id) {
$tab_focus = $(this);
}
});
if ($tab_focus.length > 0) {
$tab_focus.data('verticalTab').focus();
}
});
}
}
Drupal.behaviors.k8sLaunchTemplateVerticalTabs = {
// Drupal 9.3.x or earlier
attach: function (context) {
changeTabBySourcetype(context);
},
// Drupal 9.4.x or later
attach: function attach(context) {
changeTabBySourcetype(context);
},
};
})(jQuery, Drupal, drupalSettings);
......@@ -3428,3 +3428,14 @@ function k8s_update_8336(): void {
];
\Drupal::service('cloud')->updateYmlDefinitions($files, 'k8s');
}
/**
* Update source_type.
*/
function k8s_update_8337(): void {
$files = [
'field.field.cloud_launch_template.k8s.field_source_type.yml',
];
\Drupal::service('cloud')->updateYmlDefinitions($files, 'k8s');
}
......@@ -97,3 +97,17 @@ k8s_node_allocated_resources_horizon_chart:
dependencies:
- core/jquery
- cloud/d3
k8s_launch_template_vertical_tab:
version: 4.x
js:
js/k8s_launch_template_vertical_tab.js: {}
dependencies:
- core/jquery
k8s_launch_template_source_type:
version: 4.x
js:
js/k8s_launch_template_source_type.js: {}
dependencies:
- core/jquery
......@@ -799,17 +799,12 @@ function k8s_entity_view_alter(array &$build, EntityInterface $entity, EntityVie
// Configure Source fields.
$configuration_fields = [
[
'name' => 'source_type',
'title' => t('Source type'),
'open' => TRUE,
'fields' => [],
],
[
'name' => 'source',
'title' => t('Source'),
'open' => TRUE,
'fields' => [
'field_source_type',
'field_git_repository',
],
],
......@@ -867,7 +862,7 @@ function k8s_entity_view_alter(array &$build, EntityInterface $entity, EntityVie
$build['k8s']['cloud_context'][0]['#context']['value'] = $values[$build['k8s']['cloud_context'][0]['#context']['value']];
$build['k8s']['field_object']['#title'] = t('Kind');
if ((!empty($build['source_type']['field_source_type']) && !str_contains($build['source_type']['field_source_type'][0]['#markup'], 'Git'))
if ((!empty($build['configuration_git']['source']['field_source_type']) && !str_contains($build['configuration_git']['source']['field_source_type'][0]['#markup'], 'Git'))
|| (empty($build['configuration_git']['credentials']['field_git_access_token'][0]['#context']['value'])
&& empty($build['configuration_git']['credentials']['field_git_username'][0]['#context']['value']))) {
unset($build['configuration_git']['credentials']);
......@@ -1273,11 +1268,26 @@ function k8s_form_cloud_launch_template_k8s_form_common_alter(array &$form, Form
// to not lose the ajax action attached to it.
$form['field_git_access_token']['widget'][0]['value']['#attributes']['id'] = 'edit-field-git-access-token-0-value';
$server_template = $form_state
->getFormObject()
->getEntity();
$is_metrics_server = empty($server_template)
|| empty($server_template->hasField('field_is_metrics_server'))
|| empty($server_template->get('field_is_metrics_server'))
? FALSE
: $server_template->field_is_metrics_server->value;
$source_type = $server_template->get('field_source_type')->getValue();
// Build Git source fieldsets.
$form['tabs_configuration'] = [
'#type' => 'vertical_tabs',
'#tree' => TRUE,
'#default_tab' => sprintf(
'edit-configuration-%s',
str_contains($source_type[0]['value'], 'git') ? 'git' : 'yml',
),
];
$form['tabs_configuration']['#attached']['library'][] = 'k8s/k8s_launch_template_vertical_tab';
$configuration_fields = [
[
......@@ -1293,6 +1303,7 @@ function k8s_form_cloud_launch_template_k8s_form_common_alter(array &$form, Form
'title' => t('Source'),
'open' => TRUE,
'fields' => [
'field_source_type',
'field_git_repository',
],
],
......@@ -1340,23 +1351,14 @@ function k8s_form_cloud_launch_template_k8s_form_common_alter(array &$form, Form
$k8s_service = \Drupal::service('k8s');
$git_available = $k8s_service->isGitCommandAvailable();
$server_template = $form_state
->getFormObject()
->getEntity();
$is_metrics_server = empty($server_template)
|| empty($server_template->hasField('field_is_metrics_server'))
|| empty($server_template->get('field_is_metrics_server'))
? FALSE
: $server_template->field_is_metrics_server->value;
$source_type = $server_template->get('field_source_type')->getValue();
\Drupal::service('cloud')->reorderForm(
$form,
k8s_server_template_field_orders(TRUE, $is_metrics_server, TRUE, TRUE)
);
if (!$git_available && $server_template->isNew()) {
$form['source_type']['field_source_type']['widget']['#type'] = 'hidden';
$form['source_type']['field_source_type']['widget']['#value'] = 'git';
$form['configuration_git']['source']['field_source_type']['widget']['#type'] = 'hidden';
$form['configuration_git']['source']['field_source_type']['widget']['#value'] = 'git';
$form['configuration_git']['branch']['field_git_branch']['#access'] = FALSE;
$form['configuration_git']['branch']['field_git_path']['#access'] = FALSE;
$form['configuration_git']['credentials']['#access'] = FALSE;
......@@ -1368,10 +1370,6 @@ function k8s_form_cloud_launch_template_k8s_form_common_alter(array &$form, Form
if (!$git_available && !$server_template->isNew() && str_contains($source_type[0]['value'], 'git')) {
\Drupal::messenger()->addError(t('A git command could not be found on the server. Install the git command and try again.'));
}
$form['tabs_configuration']['#default_tab'] = sprintf(
'edit-configuration-%s',
str_contains($source_type[0]['value'], 'git') ? 'git' : 'yml',
);
// Define ajax callbacks.
$ajax = [
......@@ -1387,7 +1385,8 @@ function k8s_form_cloud_launch_template_k8s_form_common_alter(array &$form, Form
// Attach ajax callbacks.
$form['configuration_git']['source']['field_git_repository']['widget'][0]['uri']['#ajax'] = $ajax;
$form['configuration_git']['credentials']['field_git_access_token']['widget'][0]['value']['#ajax'] = $ajax;
$form['source_type']['field_source_type']['widget']['#ajax'] = $git_access_token_ajax;
$form['configuration_git']['source']['field_source_type']['widget']['#ajax'] = $git_access_token_ajax;
$form['configuration_git']['source']['field_source_type']['#attached']['library'][] = 'k8s/k8s_launch_template_source_type';
$form['configuration_git']['branch']['field_git_branch']['widget'][0]['value']['#type'] = 'select';
$form['configuration_git']['branch']['field_git_branch']['widget'][0]['value']['#size'] = 1;
......@@ -1420,16 +1419,6 @@ function k8s_form_cloud_launch_template_k8s_form_common_alter(array &$form, Form
[
'textarea[name="field_detail[0][value]"]' => $states_conditions,
],
'or',
[
'select[name="field_source_type"]' => [
['value' => 'git'],
'or',
['value' => 'github'],
'or',
['value' => 'gitlab'],
],
],
],
];
......@@ -1444,9 +1433,6 @@ function k8s_form_cloud_launch_template_k8s_form_common_alter(array &$form, Form
[
'select[name="field_source_type"]' => ['value' => 'yml'],
],
[
'select[name="field_source_type"]' => ['value' => '_none'],
],
],
];
}
......@@ -1567,13 +1553,6 @@ function k8s_server_template_field_orders(
'field_namespace',
'field_object',
],
], [
'name' => 'source_type',
'title' => t('Source type'),
'open' => TRUE,
'fields' => [
'field_source_type',
],
], [
'name' => 'configuration',
'title' => t('Configuration'),
......@@ -3279,7 +3258,7 @@ function k8s_ajax_callback_get_access_token_url(array $form, FormStateInterface
$source_type = !empty($form_state->getValue('field_source_type'))
? $form_state->getValue('field_source_type')[0]['value']
: '_none';
: 'git';
if (!empty($source_type) && str_contains($source_type, 'git')) {
......@@ -3314,7 +3293,7 @@ function k8s_get_access_token_url(array &$form, FormStateInterface $form_state):
// use the default value.
$git_source = !empty($form_state->getValue('field_source_type'))
? $form_state->getValue('field_source_type')[0]['value']
: $form['source_type']['field_source_type']['widget']['#default_value'][0];
: $form['configuration_git']['source']['field_source_type']['widget']['#default_value'][0];
// If there is still no source type, return.
if (empty($git_source)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment