-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update stix to use core composite functions.
- Loading branch information
Showing
1 changed file
with
11 additions
and
64 deletions.
There are no files selected for viewing
75 changes: 11 additions & 64 deletions
75
...esources/apps/typerefinery/components/stix/forms/fields/composite/clientlibs/functions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,37 @@ | ||
window.Typerefinery = window.Typerefinery || {}; | ||
window.Typerefinery.Components = Typerefinery.Components || {}; | ||
window.Typerefinery.Components.Forms = Typerefinery.Components.Forms || {}; | ||
window.Typerefinery.Components.Forms.Composite = Typerefinery.Components.Forms.Composite || {}; | ||
window.Typerefinery.Components.Stix = Typerefinery.Components.Stix || {}; | ||
window.Typerefinery.Components.Stix.Forms = Typerefinery.Components.Stix.Forms || {}; | ||
window.Typerefinery.Components.Stix.Forms.Composite = Typerefinery.Components.Stix.Forms.Composite || {}; | ||
|
||
|
||
;(function ($, ns, componentNs, window, document) { | ||
;(function ($, ns, componentNs, formsNs, compositeNs, document, window) { | ||
"use strict"; | ||
|
||
ns.selectorAttribute = "isCompositeParent"; | ||
ns.selector = `[${ns.selectorAttribute}]`; | ||
ns.selectorValueAttribute = "isCompositeValue"; | ||
ns.selectorValue = `[${ns.selectorValueAttribute}]`; | ||
//how to find all form input fields | ||
ns.selectorInputAttribute = "isInput"; | ||
ns.selectorInput = `[${ns.selectorInputAttribute}]`; | ||
//how to find all input fields that are part of this composite input | ||
ns.selectorCompositeInputAttribute = "isCompositeInput"; | ||
ns.selectorCompositeInput = `[${ns.selectorCompositeInputAttribute}]`; | ||
//template for form components in parent | ||
ns.selectorTemplate = "> [template]"; | ||
ns.selectonNameAttribute = "name"; | ||
|
||
$.fn.findExclude = function(selector, mask) { | ||
return this.find(selector).not(this.find(mask).find(selector)); | ||
} | ||
|
||
$.fn.compositeVal = function() { | ||
//get all immediate isCompositeParent | ||
var $compositeParents = this.parent(ns.selector).findExclude(ns.selector,ns.selector); | ||
var data = {}; | ||
|
||
//add current field values to data | ||
Object.assign(data,JSON.parse(this.val())); | ||
|
||
//for each get their values and merge their composites in | ||
$compositeParents.each(function(){ | ||
//find composite value input field | ||
var $compositeValue = $(this).findExclude(ns.selectorValue,ns.selector); | ||
if ($compositeValue) { | ||
// create placeholder for composite value | ||
data[$compositeValue.attr(ns.selectonNameAttribute)] = {}; | ||
// get composite value for this field, this will cascade to other composite fields | ||
Object.assign(data[$compositeValue.attr(ns.selectonNameAttribute)],$compositeValue.compositeVal()); | ||
} | ||
}); | ||
return data; | ||
} | ||
|
||
ns.compileValue = function($compositeParent) { | ||
var $field = $compositeParent.findExclude(ns.selectorValue,ns.selector); | ||
var $templateFields = $compositeParent.findExclude(ns.selectorTemplate,ns.selector).findExclude(ns.selectorCompositeInput,ns.selector); | ||
var data = {}; | ||
|
||
$templateFields.each(function(){ | ||
//get name and value of each input field | ||
var name = $(this).attr(ns.selectonNameAttribute); | ||
var value = $(this).val(); | ||
data[name] = value; | ||
}); | ||
//set value of composite input | ||
$field.val(JSON.stringify(data)); | ||
} | ||
|
||
ns.init = async ($component) => { | ||
const componentConfig = componentNs.getComponentConfig($component); | ||
|
||
$(document).ready(function () { | ||
|
||
$(ns.selector).each(function(){ | ||
$(compositeNs.selector).each(function(){ | ||
//find all input fields that are not inside another composite input | ||
var $field = $(this).findExclude(ns.selectorTemplate,ns.selector).findExclude(ns.selectorInput, ns.selector); | ||
var $field = $(this).findExclude(compositeNs.selectorTemplate,compositeNs.selector).findExclude(compositeNs.selectorInput, compositeNs.selector); | ||
//remove isInput attribute and add isCompositeInput attribute, this will ensure that form fields are not processed by form submit | ||
$field.removeAttr(ns.selectorInputAttribute); | ||
$field.attr(ns.selectorCompositeInputAttribute,"true"); | ||
$field.removeAttr(compositeNs.selectorInputAttribute); | ||
$field.attr(compositeNs.selectorCompositeInputAttribute,"true"); | ||
//compile value for composite input | ||
ns.compileValue($(this)); | ||
compositeNs.compileValue($(this)); | ||
}); | ||
}) | ||
|
||
//listen for change event on all input fields and compile value for composite input | ||
$(ns.selector).on("change", function() { | ||
ns.compileValue($(this)); | ||
$(compositeNs.selector).on("change", function() { | ||
compositeNs.compileValue($(this)); | ||
}); | ||
|
||
} | ||
|
||
|
||
})(jQuery, Typerefinery.Components.Stix.Forms.Composite, Typerefinery.Components, window, document); | ||
})(jQuery, Typerefinery.Components.Stix.Forms.Composite, Typerefinery.Components, Typerefinery.Components.Forms, Typerefinery.Components.Forms.Composite, document, window); |