Files
opensourcepos/public/js/plugin_data_helper.js
T
Travis Garrison 184b483103 refactor(tests): move PluginTestCase to Tests\\Support namespace
- Relocate PluginTestCase from App\\Libraries\\Plugins to Tests\\Support
- Update all references in AGENTS.md, README.md, and test files
- Add TestModulePlugin fixture for module registration tests
- Add PluginModuleRegistrationTest covering namespace registration
- Add plugin_data_helper.js for zero-JS plugin form field collection

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>
2026-07-30 13:46:46 +04:00

36 lines
1.2 KiB
JavaScript

/**
* Collects all [data-plugin-field] inputs on the page and injects them as a
* plugin_data JSON hidden field into any form marked with data-plugin-form
* before submission. Plugin devs only need data-plugin-field="pluginid_varname"
* on their inputs — no JS required in plugin partials.
*/
$(function() {
$('form[data-plugin-form]').on('submit', function() {
var pluginData = {};
$('[data-plugin-field]').each(function() {
var key = $(this).data('plugin-field');
var $el = $(this);
var val;
if ($el.is(':checkbox')) {
// bootstrap-toggle stores state on the original hidden input via .prop('checked')
val = $el.prop('checked');
} else {
val = $el.val();
}
pluginData[key] = val;
});
var $existing = $(this).find('input[name="plugin_data"]');
if ($existing.length) {
$existing.val(JSON.stringify(pluginData));
} else {
$(this).append(
$('<input>').attr({ type: 'hidden', name: 'plugin_data' }).val(JSON.stringify(pluginData))
);
}
});
});