|
1 | 1 | window.Typerefinery = window.Typerefinery || {};
|
2 | 2 | window.Typerefinery.Components = Typerefinery.Components || {};
|
| 3 | +window.Typerefinery.Components.Forms = Typerefinery.Components.Forms || {}; |
| 4 | +window.Typerefinery.Components.Forms.Composite = Typerefinery.Components.Forms.Composite || {}; |
3 | 5 | window.Typerefinery.Components.Stix = Typerefinery.Components.Stix || {};
|
4 | 6 | window.Typerefinery.Components.Stix.Forms = Typerefinery.Components.Stix.Forms || {};
|
5 | 7 | window.Typerefinery.Components.Stix.Forms.Composite = Typerefinery.Components.Stix.Forms.Composite || {};
|
6 | 8 |
|
7 |
| - |
8 |
| -;(function ($, ns, componentNs, window, document) { |
| 9 | +;(function ($, ns, componentNs, formsNs, compositeNs, document, window) { |
9 | 10 | "use strict";
|
10 | 11 |
|
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 |
| - } |
65 | 12 |
|
66 | 13 | ns.init = async ($component) => {
|
67 | 14 | const componentConfig = componentNs.getComponentConfig($component);
|
68 | 15 |
|
69 | 16 | $(document).ready(function () {
|
70 | 17 |
|
71 |
| - $(ns.selector).each(function(){ |
| 18 | + $(compositeNs.selector).each(function(){ |
72 | 19 | //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); |
74 | 21 | //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"); |
77 | 24 | //compile value for composite input
|
78 |
| - ns.compileValue($(this)); |
| 25 | + compositeNs.compileValue($(this)); |
79 | 26 | });
|
80 | 27 | })
|
81 | 28 |
|
82 | 29 | //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)); |
85 | 32 | });
|
86 | 33 |
|
87 | 34 | }
|
88 | 35 |
|
89 | 36 |
|
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