From 941f275e498cfd155914d35017fdca08ac1d2e94 Mon Sep 17 00:00:00 2001 From: "alexander.nikiforov" Date: Wed, 11 Apr 2018 10:33:26 +0300 Subject: [PATCH 1/3] WBS-2116 scroll for elementId implemented --- src/main.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main.js b/src/main.js index 60dffd5..dbc3d9e 100644 --- a/src/main.js +++ b/src/main.js @@ -1,12 +1,21 @@ const storage = []; -module.exports = ({ history, reset = true, limit = 20 }) => ({ +module.exports = ({ history, elementId, reset = true, limit = 20 }) => ({ start: () => { if(history.action === 'PUSH') { - storage.push([ - window.scrollX, - window.scrollY - ]); + if(elementId) { + const element = document.getElementById(elementId); + element && storage.push([ + element.scrollLeft, + element.scrollTop + ]); + } + else { + storage.push([ + window.scrollX, + window.scrollY + ]); + } if(storage.length > limit) { delete storage.shift(); @@ -24,11 +33,23 @@ module.exports = ({ history, reset = true, limit = 20 }) => ({ if(storage.length && !(resetScroll && resetOnAction === 'POP')) { position = storage.pop(); } - window.scrollTo(...position); + if(elementId) { + const element = document.getElementById(elementId); + element && element.scrollTo(...position); + } + else { + window.scrollTo(...position); + } break; case 'PUSH': if(reset || (resetScroll && resetOnAction === 'PUSH')) { - window.scrollTo(...position); + if(elementId) { + const element = document.getElementById(elementId); + element && element.scrollTo(...position); + } + else { + window.scrollTo(...position); + } } break; } From ffff4901eb5188dab5bad3bc57e063debbb04d90 Mon Sep 17 00:00:00 2001 From: lamo2k123 Date: Wed, 11 Apr 2018 10:45:56 +0300 Subject: [PATCH 2/3] optimize code --- src/main.js | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/main.js b/src/main.js index dbc3d9e..d9c1f3c 100644 --- a/src/main.js +++ b/src/main.js @@ -1,20 +1,22 @@ const storage = []; module.exports = ({ history, elementId, reset = true, limit = 20 }) => ({ + const $el = elementId ? document.getElementById(elementId) : window; + start: () => { if(history.action === 'PUSH') { - if(elementId) { - const element = document.getElementById(elementId); - element && storage.push([ - element.scrollLeft, - element.scrollTop - ]); - } - else { - storage.push([ - window.scrollX, - window.scrollY - ]); + if($el) { + if(elementId) { + storage.push([ + $el.scrollLeft, + $el.scrollTop + ]); + } else { + storage.push([ + $el.scrollX, + $el.scrollY + ]); + } } if(storage.length > limit) { @@ -33,24 +35,15 @@ module.exports = ({ history, elementId, reset = true, limit = 20 }) => ({ if(storage.length && !(resetScroll && resetOnAction === 'POP')) { position = storage.pop(); } - if(elementId) { - const element = document.getElementById(elementId); - element && element.scrollTo(...position); - } - else { - window.scrollTo(...position); - } + + $el && $el.scrollTo(...position); + break; case 'PUSH': if(reset || (resetScroll && resetOnAction === 'PUSH')) { - if(elementId) { - const element = document.getElementById(elementId); - element && element.scrollTo(...position); - } - else { - window.scrollTo(...position); - } + $el && $el.scrollTo(...position); } + break; } } From 7fbae36046f94bc7905313880ce0373848fc1969 Mon Sep 17 00:00:00 2001 From: lamo2k123 Date: Wed, 11 Apr 2018 10:49:01 +0300 Subject: [PATCH 3/3] ooops fix --- src/main.js | 82 +++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/src/main.js b/src/main.js index d9c1f3c..4d36550 100644 --- a/src/main.js +++ b/src/main.js @@ -1,50 +1,52 @@ const storage = []; -module.exports = ({ history, elementId, reset = true, limit = 20 }) => ({ +module.exports = ({ history, elementId, reset = true, limit = 20 }) => { const $el = elementId ? document.getElementById(elementId) : window; - start: () => { - if(history.action === 'PUSH') { - if($el) { - if(elementId) { - storage.push([ - $el.scrollLeft, - $el.scrollTop - ]); - } else { - storage.push([ - $el.scrollX, - $el.scrollY - ]); + return { + start: () => { + if(history.action === 'PUSH') { + if($el) { + if(elementId) { + storage.push([ + $el.scrollLeft, + $el.scrollTop + ]); + } else { + storage.push([ + $el.scrollX, + $el.scrollY + ]); + } } - } - if(storage.length > limit) { - delete storage.shift(); + if(storage.length > limit) { + delete storage.shift(); + } } - } - }, - render: () => { - const { location } = history; - const state = location.state || {}; - const { resetScroll, resetOnAction = 'PUSH'} = state; - let position = [0, 0]; + }, + render: () => { + const { location } = history; + const state = location.state || {}; + const { resetScroll, resetOnAction = 'PUSH'} = state; + let position = [0, 0]; + + switch (history.action) { + case 'POP': + if(storage.length && !(resetScroll && resetOnAction === 'POP')) { + position = storage.pop(); + } - switch (history.action) { - case 'POP': - if(storage.length && !(resetScroll && resetOnAction === 'POP')) { - position = storage.pop(); - } - - $el && $el.scrollTo(...position); - - break; - case 'PUSH': - if(reset || (resetScroll && resetOnAction === 'PUSH')) { $el && $el.scrollTo(...position); - } - - break; + + break; + case 'PUSH': + if(reset || (resetScroll && resetOnAction === 'PUSH')) { + $el && $el.scrollTo(...position); + } + + break; + } } - } -}); + }; +};