From 954800612d880cdb85c0ccf1b2fb655bbd9cb485 Mon Sep 17 00:00:00 2001 From: Paul Rumkin Date: Thu, 23 Jul 2020 02:18:13 +0300 Subject: [PATCH] [doc] Fix error in site.js and make it satisfy lint rules --- example/site.js | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/example/site.js b/example/site.js index d9f9270..47a1e64 100644 --- a/example/site.js +++ b/example/site.js @@ -11,48 +11,49 @@ pill('#page', { indicator.style.display = 'block' }, - onUnmounting(page, url, element){ - PreserveFormPlugin(element); + onUnmounting(page, url, element) { + PreserveFormPlugin(element) }, onReady(page, element) { + // Delay to simulate long content loading timeout = setTimeout(() => { indicator.style.display = 'none' }, 1000) - PopulateFormPlugin(element); + PopulateFormPlugin(element) }, - onMounting(){ + onMounting() { console.log('updating content') } }) const PopulateFormPlugin = element =>{ const key = location.pathname; - const fields = Array.from(element.querySelectorAll('input, textarea, select')); - if(fields.length > 0){ - const obj = JSON.parse(localStorage.getItem(key)); - obj.forEach(field=>{ - const input = document.querySelector('[name='+field.fieldName+']'); - if(input.type == 'checkbox' || input.type=='radio'){ + const fields = Array.from(element.querySelectorAll('input, textarea, select')) + if (fields.length > 0) { + const obj = JSON.parse(localStorage.getItem(key) || '[]') + obj.forEach((field) => { + const input = document.querySelector('[name=' + field.fieldName + ']') + if (input.type === 'checkbox' || input.type === 'radio') { input.checked = field.value - } else if (input.nodeName == 'TEXTAREA'){ + } else if (input.nodeName === 'TEXTAREA') { input.textContent = field.value } else { input.value = field.value } - }); + }) } } const PreserveFormPlugin = (element) =>{ - const key = location.pathname; - const fields = Array.from(element.querySelectorAll('input, textarea, select')); - if(fields.length > 0){ - const values = fields.map(val=>{ + const key = location.pathname + const fields = Array.from(element.querySelectorAll('input, textarea, select')) + if (fields.length > 0) { + const values = fields.map((val) => { return { fieldName: val.name, - value: val.type == 'checkbox' || val.type == 'radio'? val.checked : val.value + value: val.type == 'checkbox' || val.type == 'radio' ? val.checked : val.value } - }); - localStorage.setItem(key, JSON.stringify(values)); + }) + localStorage.setItem(key, JSON.stringify(values)) } -} \ No newline at end of file +}