Skip to content

Commit bce057a

Browse files
committed
update stix to use core composite functions.
1 parent b3ba0cf commit bce057a

File tree

1 file changed

+11
-64
lines changed
  • application/backend/src/main/resources/apps/typerefinery/components/stix/forms/fields/composite/clientlibs

1 file changed

+11
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,37 @@
11
window.Typerefinery = window.Typerefinery || {};
22
window.Typerefinery.Components = Typerefinery.Components || {};
3+
window.Typerefinery.Components.Forms = Typerefinery.Components.Forms || {};
4+
window.Typerefinery.Components.Forms.Composite = Typerefinery.Components.Forms.Composite || {};
35
window.Typerefinery.Components.Stix = Typerefinery.Components.Stix || {};
46
window.Typerefinery.Components.Stix.Forms = Typerefinery.Components.Stix.Forms || {};
57
window.Typerefinery.Components.Stix.Forms.Composite = Typerefinery.Components.Stix.Forms.Composite || {};
68

7-
8-
;(function ($, ns, componentNs, window, document) {
9+
;(function ($, ns, componentNs, formsNs, compositeNs, document, window) {
910
"use strict";
1011

11-
ns.selectorAttribute = "isCompositeParent";
12-
ns.selector = `[${ns.selectorAttribute}]`;
13-
ns.selectorValueAttribute = "isCompositeValue";
14-
ns.selectorValue = `[${ns.selectorValueAttribute}]`;
15-
//how to find all form input fields
16-
ns.selectorInputAttribute = "isInput";
17-
ns.selectorInput = `[${ns.selectorInputAttribute}]`;
18-
//how to find all input fields that are part of this composite input
19-
ns.selectorCompositeInputAttribute = "isCompositeInput";
20-
ns.selectorCompositeInput = `[${ns.selectorCompositeInputAttribute}]`;
21-
//template for form components in parent
22-
ns.selectorTemplate = "> [template]";
23-
ns.selectonNameAttribute = "name";
24-
25-
$.fn.findExclude = function(selector, mask) {
26-
return this.find(selector).not(this.find(mask).find(selector));
27-
}
28-
29-
$.fn.compositeVal = function() {
30-
//get all immediate isCompositeParent
31-
var $compositeParents = this.parent(ns.selector).findExclude(ns.selector,ns.selector);
32-
var data = {};
33-
34-
//add current field values to data
35-
Object.assign(data,JSON.parse(this.val()));
36-
37-
//for each get their values and merge their composites in
38-
$compositeParents.each(function(){
39-
//find composite value input field
40-
var $compositeValue = $(this).findExclude(ns.selectorValue,ns.selector);
41-
if ($compositeValue) {
42-
// create placeholder for composite value
43-
data[$compositeValue.attr(ns.selectonNameAttribute)] = {};
44-
// get composite value for this field, this will cascade to other composite fields
45-
Object.assign(data[$compositeValue.attr(ns.selectonNameAttribute)],$compositeValue.compositeVal());
46-
}
47-
});
48-
return data;
49-
}
50-
51-
ns.compileValue = function($compositeParent) {
52-
var $field = $compositeParent.findExclude(ns.selectorValue,ns.selector);
53-
var $templateFields = $compositeParent.findExclude(ns.selectorTemplate,ns.selector).findExclude(ns.selectorCompositeInput,ns.selector);
54-
var data = {};
55-
56-
$templateFields.each(function(){
57-
//get name and value of each input field
58-
var name = $(this).attr(ns.selectonNameAttribute);
59-
var value = $(this).val();
60-
data[name] = value;
61-
});
62-
//set value of composite input
63-
$field.val(JSON.stringify(data));
64-
}
6512

6613
ns.init = async ($component) => {
6714
const componentConfig = componentNs.getComponentConfig($component);
6815

6916
$(document).ready(function () {
7017

71-
$(ns.selector).each(function(){
18+
$(compositeNs.selector).each(function(){
7219
//find all input fields that are not inside another composite input
73-
var $field = $(this).findExclude(ns.selectorTemplate,ns.selector).findExclude(ns.selectorInput, ns.selector);
20+
var $field = $(this).findExclude(compositeNs.selectorTemplate,compositeNs.selector).findExclude(compositeNs.selectorInput, compositeNs.selector);
7421
//remove isInput attribute and add isCompositeInput attribute, this will ensure that form fields are not processed by form submit
75-
$field.removeAttr(ns.selectorInputAttribute);
76-
$field.attr(ns.selectorCompositeInputAttribute,"true");
22+
$field.removeAttr(compositeNs.selectorInputAttribute);
23+
$field.attr(compositeNs.selectorCompositeInputAttribute,"true");
7724
//compile value for composite input
78-
ns.compileValue($(this));
25+
compositeNs.compileValue($(this));
7926
});
8027
})
8128

8229
//listen for change event on all input fields and compile value for composite input
83-
$(ns.selector).on("change", function() {
84-
ns.compileValue($(this));
30+
$(compositeNs.selector).on("change", function() {
31+
compositeNs.compileValue($(this));
8532
});
8633

8734
}
8835

8936

90-
})(jQuery, Typerefinery.Components.Stix.Forms.Composite, Typerefinery.Components, window, document);
37+
})(jQuery, Typerefinery.Components.Stix.Forms.Composite, Typerefinery.Components, Typerefinery.Components.Forms, Typerefinery.Components.Forms.Composite, document, window);

0 commit comments

Comments
 (0)