All files / my/form form.js

87.32% Statements 62/71
75% Branches 12/16
82.14% Functions 23/28
89.71% Lines 61/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214            2x 2x 2x   330x                                                                                     22x 286x                     22x 22x       5x       5x 5x 1x   5x                 6x 6x       9x     9x   9x 9x 9x 9x   9x       5x 1x   4x       14x       23x 23x 23x       1x 1x 1x   1x           23x 23x   23x       1x 1x 1x   1x       1x     1x 1x       1x       1x 1x 1x       1x       23x 23x     23x       1x 1x 1x 1x                         132x   1716x   42x           14x 182x   14x            
/*
 * Copyright (c) 2021, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
 */
import { LightningElement, track } from 'lwc';
import { buildContents } from 'my/buildContents';
import { sentenceCase, camelCase } from 'change-case';
 
export default class Form extends LightningElement {
  componentName = '';
 
  @track
  inputs = {
    componentName: '',
    apiVersion: '50.0', // TODO: fetch from SFDC API
    withHtml: true,
    withCss: true,
    withSvg: false,
    withTest: false,
    isExposed: true,
    primaryLabel: '',
    description: '',
    configurationEditor: '',
    svgFileName: '',
    svgFileContent: '',
    targets: {},
    propertyIds: [],
    properties: [],
    objectIds: [],
    objects: []
  };
 
  targetsArr = [
    { name: 'AppPage', value: 'lightning__AppPage' },
    { name: 'HomePage', value: 'lightning__HomePage' },
    { name: 'RecordPage', value: 'lightning__RecordPage' },
    { name: 'UtilityBar', value: 'lightning__UtilityBar' },
    { name: 'FlowScreen', value: 'lightning__FlowScreen' },
    { name: 'Tab', value: 'lightning__Tab' },
    { name: 'Inbox', value: 'lightning__Inbox' },
    { name: 'CommunityPage', value: 'lightningCommunity__Page' },
    { name: 'CommunityDefault', value: 'lightningCommunity__Default' },
    { name: 'SnapinChatMessage', value: 'lightningSnapin__ChatMessage' },
    { name: 'SnapinMinimized', value: 'lightningSnapin__Minimized' },
    { name: 'SnapinPreChat', value: 'lightningSnapin__PreChat' },
    { name: 'SnapinChatHeader', value: 'lightningSnapin__ChatHeader' }
  ];
  pDefCount = 0;
  oDefCount = 0;
 
  connectedCallback() {
    this.targetsArr.forEach((t) => {
      this.inputs.targets[t.value] = {
        name: t.name,
        value: t.value,
        enabled: false,
        small: false,
        large: false,
        properties: [],
        objects: []
      };
    });
 
    this.addObjectRow();
    this.addPropertyRow();
  }
 
  onChangeInput(e) {
    const formattedValue = this.formatInputValue(
      e.currentTarget.name,
      e.currentTarget.value
    );
    this.inputs[e.currentTarget.name] = formattedValue;
    if (e.currentTarget.name === 'componentName' && !this.inputs.primaryLabel) {
      this.inputs.primaryLabel = sentenceCase(formattedValue);
    }
    this.updateContent();
  }
 
  onChangeSelect(e) {
    this.inputs[e.currentTarget.name] = e.currentTarget.value;
    this.updateContent();
  }
 
  onChangeCheckbox(e) {
    this.inputs[e.currentTarget.name] = e.currentTarget.checked;
    this.updateContent();
  }
 
  onChangeTargetValue(e) {
    Iif (!e.detail) {
      return;
    }
    const { target, enabled, small, large } = e.detail;
 
    this.inputs.targets[target.value].enabled = enabled;
    Eif (enabled) {
      this.inputs.targets[target.value].small = small;
      this.inputs.targets[target.value].large = large;
    }
    this.updateContent();
  }
 
  formatInputValue = (key, value) => {
    if (key === 'componentName') {
      return camelCase(value);
    }
    return value;
  };
 
  get isRecordPageSelected() {
    return this.inputs.targets.lightning__RecordPage.enabled === true;
  }
 
  addPropertyRow = () => {
    const pId = `propertyId_${this.pDefCount}`;
    this.inputs.propertyIds.push(pId);
    this.pDefCount++;
  };
 
  deletePropertyRow = (e) => {
    const targetId = e.detail;
    this.inputs.propertyIds = this.inputs.propertyIds.filter(
      (pId) => pId !== targetId
    );
    this.inputs.properties = this.inputs.properties.filter(
      (p) => p.id !== targetId
    );
  };
 
  addObjectRow = () => {
    const oId = `objectId_${this.oDefCount}`;
    this.inputs.objectIds.push(oId);
    // this.inputs.objects.push({ id: oId });
    this.oDefCount++;
  };
 
  deleteObjectRow = (e) => {
    const targetId = e.detail;
    this.inputs.objectIds = this.inputs.objectIds.filter(
      (oId) => oId !== targetId
    );
    this.inputs.objects = this.inputs.objects.filter((o) => o.id !== targetId);
  };
 
  onChangePropertyRow = (e) => {
    const pIndex = this.inputs.properties.findIndex(
      (p) => p.id === e.detail.id
    );
    Eif (pIndex === -1) {
      this.inputs.properties.push(e.detail);
    } else {
      this.inputs.properties[pIndex] = e.detail;
    }
    this.updateContent();
  };
 
  onChangeObjectRow = (e) => {
    const oIndex = this.inputs.objects.findIndex((o) => o.id === e.detail.id);
    Eif (oIndex === -1) {
      this.inputs.objects.push(e.detail);
    } else {
      this.inputs.objects[oIndex] = e.detail;
    }
    this.updateContent();
  };
 
  updateContent() {
    const formatted = buildContents(this.inputs);
    const e = new CustomEvent('updatecontent', {
      detail: formatted
    });
    this.dispatchEvent(e);
  }
 
  handleSvgUpload = (e) => {
    const { fileName, fileContent } = e.detail;
    this.inputs.svgFileName = fileName;
    this.inputs.svgFileContent = fileContent;
    this.updateContent();
  };
 
  nonPropertyTargets = [
    'lightning__Tab',
    'lightningSnapin__ChatMessage',
    'lightningCommunity__Page',
    'lightningSnapin__Minimized',
    'lightningSnapin__PreChat',
    'lightningSnapin__ChatHeader'
  ];
 
  get hasPropertyTarget() {
    return (
      Object.values(this.inputs.targets)
        .filter((t) => t.enabled)
        .filter((t) => {
          return !this.nonPropertyTargets.includes(t.value);
        }).length > 0
    );
  }
 
  get isFlowOnly() {
    const enabledTargets = Object.values(this.inputs.targets).filter(
      (t) => t.enabled
    );
    return (
      enabledTargets.length === 1 &&
      enabledTargets[0].value === 'lightning__FlowScreen'
    );
  }
}