Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class AttributeCopier {
  2.   copy(sourceElement, destinationElement) {
  3.     try {
  4.       return this.copyAttributes(sourceElement, destinationElement);
  5.     } catch (e) {
  6.       throw Error('Parameters sourceElement and destinationElement must be of type Element.');
  7.     }
  8.   }
  9.  
  10.   copyAttributes(sourceElement, destinationElement) {
  11.     const destinationElementCopy = destinationElement.cloneNode(true);
  12.  
  13.     Array
  14.       .from(sourceElement.attributes)
  15.       .forEach(attribute => destinationElementCopy.setAttribute(attribute.name, attribute.value));
  16.  
  17.     return destinationElementCopy;
  18.   }
  19. }
  20.  
  21. class AttributeParser {
  22.   constructor(attributes, destinationElement) {
  23.     this.attributes = attributes;
  24.     this.destinationElement = destinationElement;
  25.   }
  26.  
  27.   addAttribute(sourceElement, name, value) {
  28.     sourceElement.setAttribute(name.toString(), value.toString());
  29.   }
  30.  
  31.   parse() {
  32.     try {
  33.       return this.parseAttributes();
  34.     } catch (e) {
  35.       throw Error('Parameter destinationElement must be of type Element.');
  36.     }
  37.   }
  38.  
  39.   parseAttributes() {
  40.     const destinationElementCopy = this.destinationElement.cloneNode(true);
  41.  
  42.     for (const attribute in this.attributes) {
  43.       if (this.attributes.hasOwnProperty(attribute)) {
  44.         this.addAttribute(destinationElementCopy, attribute, this.attributes[attribute]);
  45.       }
  46.     }
  47.  
  48.     return destinationElementCopy;
  49.   }
  50. }
  51.  
  52. class ConfigurationParser {
  53.   constructor(configuration, destinationElement) {
  54.     this.configuration = configuration;
  55.     this.destinationElement = destinationElement;
  56.  
  57.     this.dynamicFormComponentFactory = new DynamicFormComponentFactory();
  58.   }
  59.  
  60.   getButtonConfigurationItems() {
  61.     return this.configuration
  62.       .filter(configuration => configuration.render === 'df-button-wrapper');
  63.   }
  64.  
  65.   getFormControlConfigurationItems() {
  66.     return this.configuration
  67.       .filter(configuration => configuration.render === 'df-input-wrapper');
  68.   }
  69.  
  70.   getSortedConfigurationByRenderPriority() {
  71.     //
  72.     const buttonConfigurationItems = this.getButtonConfigurationItems();
  73.     const formControlConfigurationItems = this.getFormControlConfigurationItems();
  74.  
  75.     //
  76.     const buttonRenderPriorityProvider = new RenderPriorityProvider(buttonConfigurationItems);
  77.     const prioritizedButtonConfigurationItems = buttonRenderPriorityProvider.checkConfigurationItems();
  78.  
  79.     //
  80.     const formControlRenderPriorityProvider = new RenderPriorityProvider(formControlConfigurationItems);
  81.     const prioritizedFormControlConfigurationItems = formControlRenderPriorityProvider.checkConfigurationItems();
  82.  
  83.     //
  84.     const buttonRenderPrioritySorter = new RenderPrioritySorter(prioritizedButtonConfigurationItems);
  85.     const sortedButtonConfigurationItems = buttonRenderPrioritySorter.sort();
  86.  
  87.     //
  88.     const formControlRenderPrioritySorter = new RenderPrioritySorter(prioritizedFormControlConfigurationItems);
  89.     const sortedFormControlConfigurationItems = formControlRenderPrioritySorter.sort();
  90.  
  91.     return sortedFormControlConfigurationItems.concat(sortedButtonConfigurationItems);
  92.   }
  93.  
  94.   parse() {
  95.     try {
  96.       return this.parseConfiguration();
  97.     } catch (e) {
  98.       throw Error('Configuration object must be of type Array.');
  99.     }
  100.   }
  101.  
  102.   parseConfiguration() {
  103.     const sortedConfigurationByRenderPriority = this.getSortedConfigurationByRenderPriority();
  104.     const destinationElementCopy = this.destinationElement.cloneNode(true);
  105.  
  106.     sortedConfigurationByRenderPriority.forEach(configurationItem => {
  107.       const dynamicFormComponent = this.dynamicFormComponentFactory.create(configurationItem.render);
  108.       const attributes = configurationItem.attributes;
  109.  
  110.       const attributeParser = new AttributeParser(attributes, dynamicFormComponent);
  111.       const dynamicFormComponentWithAttributes = attributeParser.parse();
  112.  
  113.       destinationElementCopy.appendChild(dynamicFormComponentWithAttributes);
  114.     });
  115.  
  116.     return destinationElementCopy;
  117.   }
  118. }
  119.  
  120. class DynamicFormComponentFactory {
  121.   create(componentName) {
  122.     switch (componentName) {
  123.       case 'df-button-wrapper':
  124.         return this.createButton();
  125.       case 'df-input-wrapper':
  126.         return this.createInput();
  127.       default:
  128.         throw Error('Unexpected dynamic form component type.');
  129.     }
  130.   }
  131.  
  132.   createButton() {
  133.     return document.createElement('df-button-wrapper');
  134.   }
  135.  
  136.   createInput() {
  137.     return document.createElement('df-input-wrapper');
  138.   }
  139. }
  140.  
  141. class RenderPriorityProvider {
  142.   constructor(configuration) {
  143.     this.configuration = configuration;
  144.     this.settingsInspector = new SettingsInspector();
  145.   }
  146.  
  147.   checkConfigurationItems() {
  148.     const highestRenderPriority = this.findHighestRenderPriority();
  149.  
  150.     const configurationCopy = [
  151.       ...this.configuration
  152.     ];
  153.  
  154.     return configurationCopy.map((configurationItem, index) => {
  155.       const configurationItemPosition = ++index;
  156.       return this.mapper(configurationItem, configurationItemPosition, highestRenderPriority);
  157.     });
  158.   }
  159.  
  160.   findHighestRenderPriority() {
  161.     let highestRenderPriority = 0;
  162.  
  163.     this.configuration.forEach(configurationItem => {
  164.       const hasRenderPriorityProperty = this.settingsInspector.hasRenderPriorityProperty(configurationItem);
  165.  
  166.       if (hasRenderPriorityProperty) {
  167.         const renderPriority = configurationItem.settings.renderPriority;
  168.  
  169.         if (renderPriority > highestRenderPriority) {
  170.           highestRenderPriority = renderPriority;
  171.         }
  172.       }
  173.     });
  174.  
  175.     return highestRenderPriority;
  176.   }
  177.  
  178.   mapper(configurationItem, configurationItemPosition, highestRenderPriority) {
  179.     if (!this.settingsInspector.hasRenderPriorityProperty(configurationItem)) {
  180.       this.provideRenderPriority(configurationItem, configurationItemPosition, highestRenderPriority);
  181.     }
  182.  
  183.     return configurationItem;
  184.   }
  185.  
  186.   provideRenderPriority(configurationItem, configurationItemPosition, highestRenderPriority) {
  187.     if (!this.settingsInspector.hasSettingsProperty(configurationItem)) {
  188.       configurationItem.settings = {};
  189.     }
  190.  
  191.     if (!this.settingsInspector.hasRenderPriorityProperty(configurationItem)) {
  192.       configurationItem.settings.renderPriority = highestRenderPriority + configurationItemPosition;
  193.     }
  194.   }
  195. }
  196.  
  197. class RenderPrioritySorter {
  198.   constructor(configuration) {
  199.     this.configuration = configuration;
  200.   }
  201.  
  202.   sort() {
  203.     try {
  204.       return this.configuration.sort(this.compareAscending);
  205.     } catch (e) {
  206.       throw Error('Property configuration must be of type Array.');
  207.     }
  208.   }
  209.  
  210.   compareAscending(firstItem, secondItem) {
  211.     return firstItem.settings.renderPriority - secondItem.settings.renderPriority;
  212.   }
  213. }
  214.  
  215. class SettingsInspector {
  216.   hasRenderPriorityProperty(configurationItem) {
  217.     if (configurationItem && this.hasSettingsProperty(configurationItem)) {
  218.       return Boolean(configurationItem.settings.renderPriority);
  219.     }
  220.   }
  221.  
  222.   hasSettingsProperty(configurationItem) {
  223.     if (configurationItem) {
  224.       return Boolean(configurationItem.settings);
  225.     }
  226.  
  227.     return false;
  228.   }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement