Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AttributeCopier {
- copy(sourceElement, destinationElement) {
- try {
- return this.copyAttributes(sourceElement, destinationElement);
- } catch (e) {
- throw Error('Parameters sourceElement and destinationElement must be of type Element.');
- }
- }
- copyAttributes(sourceElement, destinationElement) {
- const destinationElementCopy = destinationElement.cloneNode(true);
- Array
- .from(sourceElement.attributes)
- .forEach(attribute => destinationElementCopy.setAttribute(attribute.name, attribute.value));
- return destinationElementCopy;
- }
- }
- class AttributeParser {
- constructor(attributes, destinationElement) {
- this.attributes = attributes;
- this.destinationElement = destinationElement;
- }
- addAttribute(sourceElement, name, value) {
- sourceElement.setAttribute(name.toString(), value.toString());
- }
- parse() {
- try {
- return this.parseAttributes();
- } catch (e) {
- throw Error('Parameter destinationElement must be of type Element.');
- }
- }
- parseAttributes() {
- const destinationElementCopy = this.destinationElement.cloneNode(true);
- for (const attribute in this.attributes) {
- if (this.attributes.hasOwnProperty(attribute)) {
- this.addAttribute(destinationElementCopy, attribute, this.attributes[attribute]);
- }
- }
- return destinationElementCopy;
- }
- }
- class ConfigurationParser {
- constructor(configuration, destinationElement) {
- this.configuration = configuration;
- this.destinationElement = destinationElement;
- this.dynamicFormComponentFactory = new DynamicFormComponentFactory();
- }
- getButtonConfigurationItems() {
- return this.configuration
- .filter(configuration => configuration.render === 'df-button-wrapper');
- }
- getFormControlConfigurationItems() {
- return this.configuration
- .filter(configuration => configuration.render === 'df-input-wrapper');
- }
- getSortedConfigurationByRenderPriority() {
- //
- const buttonConfigurationItems = this.getButtonConfigurationItems();
- const formControlConfigurationItems = this.getFormControlConfigurationItems();
- //
- const buttonRenderPriorityProvider = new RenderPriorityProvider(buttonConfigurationItems);
- const prioritizedButtonConfigurationItems = buttonRenderPriorityProvider.checkConfigurationItems();
- //
- const formControlRenderPriorityProvider = new RenderPriorityProvider(formControlConfigurationItems);
- const prioritizedFormControlConfigurationItems = formControlRenderPriorityProvider.checkConfigurationItems();
- //
- const buttonRenderPrioritySorter = new RenderPrioritySorter(prioritizedButtonConfigurationItems);
- const sortedButtonConfigurationItems = buttonRenderPrioritySorter.sort();
- //
- const formControlRenderPrioritySorter = new RenderPrioritySorter(prioritizedFormControlConfigurationItems);
- const sortedFormControlConfigurationItems = formControlRenderPrioritySorter.sort();
- return sortedFormControlConfigurationItems.concat(sortedButtonConfigurationItems);
- }
- parse() {
- try {
- return this.parseConfiguration();
- } catch (e) {
- throw Error('Configuration object must be of type Array.');
- }
- }
- parseConfiguration() {
- const sortedConfigurationByRenderPriority = this.getSortedConfigurationByRenderPriority();
- const destinationElementCopy = this.destinationElement.cloneNode(true);
- sortedConfigurationByRenderPriority.forEach(configurationItem => {
- const dynamicFormComponent = this.dynamicFormComponentFactory.create(configurationItem.render);
- const attributes = configurationItem.attributes;
- const attributeParser = new AttributeParser(attributes, dynamicFormComponent);
- const dynamicFormComponentWithAttributes = attributeParser.parse();
- destinationElementCopy.appendChild(dynamicFormComponentWithAttributes);
- });
- return destinationElementCopy;
- }
- }
- class DynamicFormComponentFactory {
- create(componentName) {
- switch (componentName) {
- case 'df-button-wrapper':
- return this.createButton();
- case 'df-input-wrapper':
- return this.createInput();
- default:
- throw Error('Unexpected dynamic form component type.');
- }
- }
- createButton() {
- return document.createElement('df-button-wrapper');
- }
- createInput() {
- return document.createElement('df-input-wrapper');
- }
- }
- class RenderPriorityProvider {
- constructor(configuration) {
- this.configuration = configuration;
- this.settingsInspector = new SettingsInspector();
- }
- checkConfigurationItems() {
- const highestRenderPriority = this.findHighestRenderPriority();
- const configurationCopy = [
- ...this.configuration
- ];
- return configurationCopy.map((configurationItem, index) => {
- const configurationItemPosition = ++index;
- return this.mapper(configurationItem, configurationItemPosition, highestRenderPriority);
- });
- }
- findHighestRenderPriority() {
- let highestRenderPriority = 0;
- this.configuration.forEach(configurationItem => {
- const hasRenderPriorityProperty = this.settingsInspector.hasRenderPriorityProperty(configurationItem);
- if (hasRenderPriorityProperty) {
- const renderPriority = configurationItem.settings.renderPriority;
- if (renderPriority > highestRenderPriority) {
- highestRenderPriority = renderPriority;
- }
- }
- });
- return highestRenderPriority;
- }
- mapper(configurationItem, configurationItemPosition, highestRenderPriority) {
- if (!this.settingsInspector.hasRenderPriorityProperty(configurationItem)) {
- this.provideRenderPriority(configurationItem, configurationItemPosition, highestRenderPriority);
- }
- return configurationItem;
- }
- provideRenderPriority(configurationItem, configurationItemPosition, highestRenderPriority) {
- if (!this.settingsInspector.hasSettingsProperty(configurationItem)) {
- configurationItem.settings = {};
- }
- if (!this.settingsInspector.hasRenderPriorityProperty(configurationItem)) {
- configurationItem.settings.renderPriority = highestRenderPriority + configurationItemPosition;
- }
- }
- }
- class RenderPrioritySorter {
- constructor(configuration) {
- this.configuration = configuration;
- }
- sort() {
- try {
- return this.configuration.sort(this.compareAscending);
- } catch (e) {
- throw Error('Property configuration must be of type Array.');
- }
- }
- compareAscending(firstItem, secondItem) {
- return firstItem.settings.renderPriority - secondItem.settings.renderPriority;
- }
- }
- class SettingsInspector {
- hasRenderPriorityProperty(configurationItem) {
- if (configurationItem && this.hasSettingsProperty(configurationItem)) {
- return Boolean(configurationItem.settings.renderPriority);
- }
- }
- hasSettingsProperty(configurationItem) {
- if (configurationItem) {
- return Boolean(configurationItem.settings);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement