jens-scheffler-bosch commented on code in PR #27063:
URL: https://github.com/apache/airflow/pull/27063#discussion_r1088324192
##########
airflow/www/static/js/trigger.js:
##########
@@ -19,33 +19,190 @@
/* global document, CodeMirror, window */
-const textArea = document.getElementById('json');
+let jsonForm;
+const objectFields = new Map();
const recentConfigList = document.getElementById('recent_configs');
-const minHeight = 300;
-const maxHeight = window.innerHeight - 450;
-const height = maxHeight > minHeight ? maxHeight : minHeight;
-
-CodeMirror.fromTextArea(textArea, {
- lineNumbers: true,
- mode: {
- name: 'javascript',
- json: true,
- },
- gutters: ['CodeMirror-lint-markers'],
- lint: true,
-})
- .setSize(null, height);
+
+/**
+ * Update the generated JSON DagRun.conf JSON field if any field changed
+ */
+function updateJSONconf() {
+ const jsonStart = document.getElementById('json_start').value;
+ const params = JSON.parse(jsonStart);
+ const elements = document.getElementById('trigger_form');
+ for (let i = 0; i < elements.length; i += 1) {
+ if (elements[i].name && elements[i].name.startsWith('element_')) {
+ const keyName = elements[i].name.substr(8);
+ if (elements[i].type === 'checkbox') {
+ params[keyName] = elements[i].checked;
+ } else if (elements[i].attributes.valuetype &&
elements[i].attributes.valuetype.value === 'array') {
+ const lines = elements[i].value.split('\n');
+ const values = [];
+ for (let j = 0; j < lines.length; j += 1) {
+ if (lines[j].trim().length > 0) {
+ values[values.length] = lines[j].trim();
+ }
+ }
+ params[keyName] = values;
+ } else if (elements[i].value.length === 0) {
+ params[keyName] = null;
+ } else if (elements[i].attributes.valuetype &&
elements[i].attributes.valuetype.value === 'object') {
+ try {
+ const textValue = objectFields.get(elements[i].name).getValue();
+ if (textValue.length > 0) {
+ const objValue = JSON.parse(textValue);
+ params[keyName] = objValue;
+
objectFields.get(elements[i].name).setValue(JSON.stringify(objValue, null, 4));
+ } else {
+ params[keyName] = null;
+ }
+ } catch (e) {
+ // ignore JSON parsing errors
+ }
+ } else if (Number.isNaN(elements[i].value)) {
+ params[keyName] = elements[i].value;
+ } else if (elements[i].attributes.valuetype &&
elements[i].attributes.valuetype.value === 'number') {
+ params[keyName] = Number(elements[i].value);
+ } else {
+ params[keyName] = elements[i].value;
+ }
+ }
+ }
+ jsonForm.setValue(JSON.stringify(params, null, 4));
+}
+
+/**
+ * Initialize the form during load of the web page
+ */
+function initForm() {
+ const formSectionsElement = document.getElementById('form_sections');
+ const formWithFields = (formSectionsElement != null);
+
+ // Initialize the Generated JSON form or JSON entry form
+ const minHeight = 300;
+ const maxHeight = (formWithFields ? window.innerHeight / 2 :
window.innerHeight) - 550;
+ const height = maxHeight > minHeight ? maxHeight : minHeight;
+ jsonForm = CodeMirror.fromTextArea(document.getElementById('json'), {
+ lineNumbers: true,
+ mode: { name: 'javascript', json: true },
+ gutters: ['CodeMirror-lint-markers'],
+ lint: true,
+ });
+ jsonForm.setSize(null, height);
+
+ if (formWithFields) {
+ // Apply JSON formatting and linting to all object fields in the form
+ const elements = document.getElementById('trigger_form');
+ for (let i = 0; i < elements.length; i += 1) {
+ if (elements[i].name && elements[i].name.startsWith('element_')) {
+ if (elements[i].attributes.valuetype
+ && elements[i].attributes.valuetype.value === 'object') {
+ const field = CodeMirror.fromTextArea(elements[i], {
+ lineNumbers: true,
+ mode: { name: 'javascript', json: true },
+ gutters: ['CodeMirror-lint-markers'],
+ lint: true,
+ });
+ /* eslint-disable no-unused-vars */
+ field.on('blur', (cm, change) => { updateJSONconf(); });
Review Comment:
Great, thanks. I am not a JS expert, such via this PR I learned something
new :-D
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]