<!DOCTYPE html>
<html>
<head>
<title>Field Mapper</title>
<style>
.field-container {
margin: 20px;
}
#blocklyDiv {
height: 400px;
width: 600px;
display: none;
}
</style>
</head>
<body>
<div class="field-container">
<label>Source Field Name:</label>
<input type="text" id="sourceFieldName" value="sourceProductCode" readonly>
<br>
<label>Source Field Value:</label>
<input type="text" id="sourceFieldValue" value="5839">
<br>
<label>Target Field Name:</label>
<input type="text" id="targetFieldName" value="targetProductCode">
<br>
<label>Target Field Value:</label>
<input type="text" id="targetFieldValue" readonly>
<br>
<button onclick="showBlockly()">Complex Map</button>
</div>
<div id="blocklyDiv"></div>
<button id="confirmButton" onclick="confirmAndClose()" style="display:none">Confirm & Close</button>
<xml id="toolbox" style="display: none">
<block type="source_block"></block>
<block type="transform_block"></block>
<block type="result_block"></block>
</xml>
<script>
let workspace;
Blockly.Blocks['source_block'] = {
init: function() {
this.appendDummyInput()
.appendField(document.getElementById('sourceFieldName').value)
.appendField(new Blockly.FieldTextInput(document.getElementById('sourceFieldValue').value), "SOURCE_VALUE");
this.setNextStatement(true);
this.setColour(120);
}
};
Blockly.Blocks['transform_block'] = {
init: function() {
this.appendDummyInput()
.appendField("Prepend PC-")
.appendField("Current result [")
.appendField(new Blockly.FieldTextInput(""), "TRANSFORM_RESULT")
.appendField("]");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(230);
}
};
Blockly.Blocks['result_block'] = {
init: function() {
this.appendDummyInput()
.appendField(document.getElementById('targetFieldName').value)
.appendField(new Blockly.FieldTextInput(""), "RESULT_VALUE");
this.setPreviousStatement(true); // Only connect from above
this.setColour(0);
}
};
function showBlockly() {
document.getElementById('blocklyDiv').style.display = 'block';
document.getElementById('confirmButton').style.display = 'block';
workspace = Blockly.inject('blocklyDiv', {
toolbox: document.getElementById('toolbox'),
trashcan: true
});
// Add change listener
workspace.addChangeListener((event) => {
if (event.type === Blockly.Events.BLOCK_MOVE) {
const blocks = workspace.getAllBlocks();
let currentValue = '';
blocks.forEach(block => {
if (block.type === 'source_block') {
currentValue = block.getFieldValue('SOURCE_VALUE');
}
if (block.type === 'transform_block') {
const transformedValue = 'PC-' + currentValue;
block.setFieldValue(transformedValue, 'TRANSFORM_RESULT');
currentValue = transformedValue;
}
if (block.type === 'result_block') {
block.setFieldValue(currentValue, 'RESULT_VALUE');
}
});
}
});
}
function confirmAndClose() {
const blocks = workspace.getAllBlocks();
let resultValue = '';
blocks.forEach(block => {
if (block.type === 'source_block') {
resultValue = block.getFieldValue('SOURCE_VALUE');
}
if (block.type === 'transform_block') {
resultValue = 'PC-' + resultValue;
}
if (block.type === 'result_block') {
block.setFieldValue(resultValue, 'RESULT_VALUE');
}
});
document.getElementById('targetFieldValue').value = resultValue;
document.getElementById('blocklyDiv').style.display = 'none';
document.getElementById('confirmButton').style.display = 'none';
}
</script>
</body>
</html>