public static final String FIND_BY_SITE_CODE = "findBySiteCode";
public static final String P_SITE_CODE = "siteCode";
public static final String BY_SITE_CODE_QUERY = "select d from DropOffPoint d"
+ " where d.enabled = true and d.siteCode = :"
+ P_SITE_CODE;
private static final long serialVersionUID = -4241558462200818070L;
private static final Map<String, Integer> handlingTypeOrder = new HashMap<>();
private static final long serialVersionUID = -5499697325844422439L;
@OneToMany(mappedBy = "dropOffPointHeader", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH},
orphanRemoval = true)
private Set dropOffPoints = new HashSet<>();
private String name;
private String siteCode;
}
Considering the entity above, i want to create a fe page (similar to the one i have shown you, using YUI.
which has a button to insert a new dropoffpoint and a button to insert a new parameter in the dropoffpoint.
For esample to insert a new parameter for the dropoffpoint you would have a picker where you select "volume" then it would appear in the same row a form where you insert the min value , a form where you insert the max value and a form where you insert the operator
take inspiration by this page
/* global SCE */
SCE.add('sce-task-standard-execution', Y => {
'use strict'
you should also use stuff like sce.add and everything, i'm using the same framework
Certainly! Below is an example of how you might create a front-end page using YUI and your existing framework to allow inserting new DropOffPoint entities and adding parameters to them. This code takes inspiration from your provided sce-task-standard-execution module and uses similar patterns, including SCE.add, event handling, and template rendering.
JavaScript Module (sce-drop-off-point-editor.js):
/* global SCE */
SCE.add('sce-drop-off-point-editor', Y => {
'use strict';
const CLASS_NAME = 'sce-drop-off-point-editor';
// Compile templates
const _MAIN_TEMPLATE = Y.Handlebars.compile(Y.SCE.Commons.getTemplate(CLASS_NAME, 'drop-off-point-editor.html'));
const _PARAMETER_ROW_TEMPLATE = Y.Handlebars.compile(Y.SCE.Commons.getTemplate(CLASS_NAME, 'parameter-row.html'));
const SELECTORS = {
ADD_DROPOFF_POINT_BUTTON: '[data-id="btn.add_dropoff_point"]',
ADD_PARAMETER_BUTTON: '[data-id="btn.add_parameter"]',
PARAMETER_CONTAINER: '.parameter-container',
REMOVE_PARAMETER_BUTTON: '[data-id="btn.remove_parameter"]',
PARAMETER_ROW_INPUTS: '.parameter-row select, .parameter-row input',
SUBMIT_BUTTON: '[data-id="btn.submit"]',
};
Y.namespace('SCE').DropOffPointEditor = Y.Base.create(CLASS_NAME, Y.View, [], {
template: _MAIN_TEMPLATE,
parameterRowTemplate: _PARAMETER_ROW_TEMPLATE,
events: {
[SELECTORS.ADD_DROPOFF_POINT_BUTTON]: {
click: '_onAddDropOffPointClick'
},
[SELECTORS.ADD_PARAMETER_BUTTON]: {
click: '_onAddParameterClick'
},
[SELECTORS.SUBMIT_BUTTON]: {
click: '_onSubmitClick'
},
// Delegate events for dynamic elements
[`${SELECTORS.PARAMETER_CONTAINER}`]: {
click: '_onParameterContainerClick',
change: '_onParameterContainerChange',
},
},
initializer(params) {
// Initialize variables, event handles, etc.
this._eventHandles = [];
this._parameters = [];
this._resourceConfig = params.resource;
this._dataModel = params.dataModel;
this._parent = params.parent;
// Any additional initialization
},
render() {
this.container = this.get('container');
this.container.setHTML(this.template({
// Pass any needed context to the template
}));
Y.SCE.Intl.intlDomNodes(this.container);
// Render initial parameters if any
this._renderParameters();
return this;
},
_onAddDropOffPointClick(ev) {
ev.preventDefault();
ev.stopPropagation();
// Logic to clear the form for a new DropOffPoint
// (e.g., reset fields, clear parameters)
this._clearForm();
},
_onAddParameterClick(ev) {
ev.preventDefault();
ev.stopPropagation();
// Add a new parameter object to the parameters array
this._parameters.push({
parameter: '',
minValue: '',
maxValue: '',
operator: ''
});
this._renderParameters();
},
_onParameterContainerClick(ev) {
if (ev.target.matches(SELECTORS.REMOVE_PARAMETER_BUTTON)) {
this._onRemoveParameterClick(ev);
}
},
_onParameterContainerChange(ev) {
if (ev.target.matches(SELECTORS.PARAMETER_ROW_INPUTS)) {
this._onParameterChange(ev);
}
},
_onRemoveParameterClick(ev) {
ev.preventDefault();
ev.stopPropagation();
const index = parseInt(ev.currentTarget.getData('index'), 10);
this._parameters.splice(index, 1);
this._renderParameters();
},
_onParameterChange(ev) {
const node = ev.currentTarget;
const rowNode = node.ancestor('.parameter-row');
const index = parseInt(rowNode.getAttribute('data-index'), 10);
const param = this._parameters[index];
const dataId = node.getData('id');
if (dataId.startsWith('parameter-picker-')) {
param.parameter = node.get('value');
} else if (dataId.startsWith('min-value-')) {
param.minValue = node.get('value');
} else if (dataId.startsWith('max-value-')) {
param.maxValue = node.get('value');
} else if (dataId.startsWith('operator-')) {
param.operator = node.get('value');
}
},
_onSubmitClick(ev) {
ev.preventDefault();
ev.stopPropagation();
// Gather data from the form and parameters
const dropOffPointData = this._collectFormData();
// Send data to the backend (e.g., via AJAX)
this._submitDropOffPoint(dropOffPointData);
},
_renderParameters() {
const parameterContainer = this.container.one(SELECTORS.PARAMETER_CONTAINER);
parameterContainer.setHTML('');
this._parameters.forEach((param, index) => {
param.index = index;
const paramHTML = this.parameterRowTemplate(param);
parameterContainer.append(paramHTML);
});
Y.SCE.Intl.intlDomNodes(parameterContainer);
},
_clearForm() {
// Logic to reset the form fields
this.container.all('input, select').each(node => {
node.set('value', '');
});
this._parameters = [];
this._renderParameters();
},
_collectFormData() {
// Collect data from the main form
const name = this.container.one('[name="name"]').get('value');
const siteCode = this.container.one('[name="siteCode"]').get('value');
// Collect parameter data from this._parameters
const parameters = this._parameters.map(param => {
return {
parameter: param.parameter,
minValue: param.minValue,
maxValue: param.maxValue,
operator: param.operator,
};
});
return {
name,
siteCode,
parameters,
};
},
_submitDropOffPoint(data) {
// Logic to send data to the backend
Y.io('/api/dropoffpoints', {
method: 'POST',
data: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
on: {
success: (id, response) => {
// Handle success (e.g., show a message, clear form)
Y.SCE.Dialog.alert({
message: Y.SCE.Intl.getComponentLabel(CLASS_NAME, 'message.dropOffPointCreated'),
type: Y.SCE.Dialog.SUCCESS,
});
this._clearForm();
},
failure: (id, response) => {
// Handle failure (e.g., show error)
Y.SCE.Error.notify(response.responseText);
}
}
});
},
destructor() {
// Clean up any event handles, subviews, etc.
Y.each(this._eventHandles, function (handle) {
handle.detach();
});
}
}, {
ATTRS: {}
});
}, '0.0.1', {
requires: [
'view',
'handlebars',
'sce-foundation',
'sce-commons',
'sce-intl',
'sce-error',
'io-base',
]
});
The module sce-drop-off-point-editor follows the same pattern as your existing sce-task-standard-execution module.
The templates use Y.Handlebars to render the HTML. You should place drop-off-point-editor.html and parameter-row.html in the appropriate templates folder and ensure they are loaded correctly.
The events property handles user interactions, including adding and removing parameters, changing parameter values, and submitting the form.
The _parameters array keeps track of the parameters added by the user. Each parameter includes the selected parameter (e.g., 'volume'), min value, max value, and operator.
The _renderParameters method updates the parameter list in the DOM whenever parameters are added or removed.
The _submitDropOffPoint method collects all the data from the form and parameters and sends it to the backend via an AJAX request (Y.io).
You should adjust the backend endpoint (/api/dropoffpoints) and the request payload to match your actual API.
The code includes internationalization support using Y.SCE.Intl.
Usage:
Include the JavaScript module in your page.
In your main application or parent view, instantiate the Y.SCE.DropOffPointEditor view, passing any necessary parameters (e.g., resource configurations, data models, parent views).
Ensure that all required dependencies are included and that the templates are correctly set up.
Example of Instantiation in Parent View:
// In your parent view or application code
const dropOffPointEditor = new Y.SCE.DropOffPointEditor({
container: Y.one('#drop-off-point-editor-container'),
resource: this._resourceConfig,
dataModel: this._dataModel,
parent: this,
});
dropOffPointEditor.render();
Additional Considerations:
You'll need to define appropriate labels and messages in your internationalization files (e.g., for keys like 'title.dropOffPointEditor', 'button.addParameter', etc.).
If you have specific validation or business logic (e.g., parameter value constraints), you should implement that in the relevant methods.
For the parameter options and operators, ensure that they match the fields and enums defined in your DropOffPoint entity.
By following this approach, you're using your existing framework and patterns to create a user interface that allows users to insert new DropOffPoint entities and add parameters to them, providing a consistent experience within your application.