Skip to content
This repository has been archived by the owner on Nov 12, 2019. It is now read-only.

Commit

Permalink
Normalise indentation and fix new item remove icon
Browse files Browse the repository at this point in the history
  • Loading branch information
knagurski committed Jul 28, 2018
1 parent ca1aaee commit 844696f
Showing 1 changed file with 110 additions and 110 deletions.
220 changes: 110 additions & 110 deletions editable-list/main.js
@@ -1,132 +1,132 @@
'use strict';

(function() {
class EditableList extends HTMLElement {
constructor() {
// establish prototype chain
super();

// attaches shadow tree and returns shadow root reference
// https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow
const shadow = this.attachShadow({ mode: 'open' });

// creating a container for the editable-list component
const editableListContainer = document.createElement('div');

// get attribute values from getters
const title = this.title;
const addItemText = this.addItemText;
const listItems = this.items;

// adding a class to our container for the sake of clarity
editableListContainer.classList.add('editable-list');

// creating the inner HTML of the editable list element
editableListContainer.innerHTML = `
<style>
li, div > div {
display: flex;
align-items: center;
justify-content: space-between;
}
.icon {
background-color: #fff;
border: none;
cursor: pointer;
float: right;
font-size: 1.8rem;
}
</style>
<h3>${title}</h3>
<ul class="item-list">
${listItems.map(item => `
<li>${item}
<button class="editable-list-remove-item icon">&ominus;</button>
</li>
`).join('')}
</ul>
<div>
<label>${addItemText}</label>
<input class="add-new-list-item-input" type="text"></input>
<button class="editable-list-add-item icon">&oplus;</button>
</div>
`;

// binding methods
this.addListItem = this.addListItem.bind(this);
this.handleRemoveItemListeners = this.handleRemoveItemListeners.bind(this);
this.removeListItem = this.removeListItem.bind(this);

// appending the container to the shadow DOM
shadow.appendChild(editableListContainer);
}

// add items to the list
addListItem(e) {
const textInput = this.shadowRoot.querySelector('.add-new-list-item-input');
class EditableList extends HTMLElement {
constructor() {
// establish prototype chain
super();

// attaches shadow tree and returns shadow root reference
// https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow
const shadow = this.attachShadow({ mode: 'open' });

// creating a container for the editable-list component
const editableListContainer = document.createElement('div');

// get attribute values from getters
const title = this.title;
const addItemText = this.addItemText;
const listItems = this.items;

// adding a class to our container for the sake of clarity
editableListContainer.classList.add('editable-list');

// creating the inner HTML of the editable list element
editableListContainer.innerHTML = `
<style>
li, div > div {
display: flex;
align-items: center;
justify-content: space-between;
}
.icon {
background-color: #fff;
border: none;
cursor: pointer;
float: right;
font-size: 1.8rem;
}
</style>
<h3>${title}</h3>
<ul class="item-list">
${listItems.map(item => `
<li>${item}
<button class="editable-list-remove-item icon">&ominus;</button>
</li>
`).join('')}
</ul>
<div>
<label>${addItemText}</label>
<input class="add-new-list-item-input" type="text"></input>
<button class="editable-list-add-item icon">&oplus;</button>
</div>
`;

// binding methods
this.addListItem = this.addListItem.bind(this);
this.handleRemoveItemListeners = this.handleRemoveItemListeners.bind(this);
this.removeListItem = this.removeListItem.bind(this);

// appending the container to the shadow DOM
shadow.appendChild(editableListContainer);
}

if (textInput.value) {
const li = document.createElement('li');
const button = document.createElement('button');
const childrenLength = this.itemList.children.length;
// add items to the list
addListItem(e) {
const textInput = this.shadowRoot.querySelector('.add-new-list-item-input');

li.textContent = textInput.value;
button.classList.add('editable-list-remove-item', 'icon');
button.textContent = '\u2796';
if (textInput.value) {
const li = document.createElement('li');
const button = document.createElement('button');
const childrenLength = this.itemList.children.length;

this.itemList.appendChild(li);
this.itemList.children[childrenLength].appendChild(button);
li.textContent = textInput.value;
button.classList.add('editable-list-remove-item', 'icon');
button.innerHTML = '&ominus;';

this.handleRemoveItemListeners([...this.itemList.children]);
this.itemList.appendChild(li);
this.itemList.children[childrenLength].appendChild(button);

textInput.value = '';
}
}
this.handleRemoveItemListeners([...this.itemList.children]);

// fires after the element has been attached to the DOM
connectedCallback() {
const removeElementButtons = [...this.shadowRoot.querySelectorAll('.editable-list-remove-item')];
const addElementButton = this.shadowRoot.querySelector('.editable-list-add-item');
textInput.value = '';
}
}

this.itemList = this.shadowRoot.querySelector('.item-list');
// fires after the element has been attached to the DOM
connectedCallback() {
const removeElementButtons = [...this.shadowRoot.querySelectorAll('.editable-list-remove-item')];
const addElementButton = this.shadowRoot.querySelector('.editable-list-add-item');

this.handleRemoveItemListeners(removeElementButtons);
addElementButton.addEventListener('click', this.addListItem, false);
}
this.itemList = this.shadowRoot.querySelector('.item-list');

// gathering data from element attributes
get title() {
return this.getAttribute('title') || '';
}
this.handleRemoveItemListeners(removeElementButtons);
addElementButton.addEventListener('click', this.addListItem, false);
}

get items() {
const items = [];
// gathering data from element attributes
get title() {
return this.getAttribute('title') || '';
}

[...this.attributes].forEach(attr => {
if (attr.name.includes('list-item')) {
items.push(attr.value);
}
});
get items() {
const items = [];

return items;
[...this.attributes].forEach(attr => {
if (attr.name.includes('list-item')) {
items.push(attr.value);
}
});

get addItemText() {
return this.getAttribute('add-item-text') || '';
}
return items;
}

handleRemoveItemListeners(arrayOfElements) {
arrayOfElements.forEach(element => {
element.addEventListener('click', this.removeListItem, false)
});
}
get addItemText() {
return this.getAttribute('add-item-text') || '';
}

removeListItem(e) {
e.target.parentNode.remove();
}
handleRemoveItemListeners(arrayOfElements) {
arrayOfElements.forEach(element => {
element.addEventListener('click', this.removeListItem, false);
});
}

removeListItem(e) {
e.target.parentNode.remove();
}
}

// let the browser know about the custom element
customElements.define('editable-list', EditableList);
// let the browser know about the custom element
customElements.define('editable-list', EditableList);
})();

0 comments on commit 844696f

Please sign in to comment.