From 5fe9aadc0e84bac277db7e6ab78b8731c2ba31fc Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 31 Mar 2017 14:54:34 +0200 Subject: Set progress --- app/Resources/static/themes/_global/js/tools.js | 66 ++++++++++++++++++------- app/Resources/static/themes/baggy/index.js | 36 ++++++++++++++ app/Resources/static/themes/material/index.js | 33 +++++++++++++ 3 files changed, 116 insertions(+), 19 deletions(-) (limited to 'app') diff --git a/app/Resources/static/themes/_global/js/tools.js b/app/Resources/static/themes/_global/js/tools.js index 774f4539..6386688b 100644 --- a/app/Resources/static/themes/_global/js/tools.js +++ b/app/Resources/static/themes/_global/js/tools.js @@ -5,30 +5,58 @@ import './shortcuts/entry'; /* Allows inline call qr-code call */ import jrQrcode from 'jr-qrcode'; // eslint-disable-line -function supportsLocalStorage() { - try { - return 'localStorage' in window && window.localStorage !== null; - } catch (e) { - return false; - } -} - function savePercent(id, percent) { - if (!supportsLocalStorage()) { return false; } - localStorage[`wallabag.article.${id}.percent`] = percent; - return true; + fetch(`/progress/${id}/${percent * 100}`, { credentials: 'include' }); } -function retrievePercent(id) { - if (!supportsLocalStorage()) { return false; } - - const bheight = $(document).height(); - const percent = localStorage[`wallabag.article.${id}.percent`]; - const scroll = bheight * percent; +function retrievePercent() { + const percent = $('#article').attr('data-progress'); + console.log(percent); + const scroll = $(document).height() * percent; $('html,body').animate({ scrollTop: scroll }, 'fast'); +} + +function initFilters() { + // no display if filters not available + if ($('div').is('#filters')) { + $('#button_filters').show(); + $('.js-filters-action').sideNav({ edge: 'right' }); + $('#clear_form_filters').on('click', () => { + $('#filters input').val(''); + $('#filters :checked').removeAttr('checked'); + return false; + }); + } +} + +function initExport() { + // no display if export not available + if ($('div').is('#export')) { + $('#button_export').show(); + $('.js-export-action').sideNav({ edge: 'right' }); + } +} - return true; +function throttle(callback, delay) { + let last; + let timer; + return function () { + const context = this; + const now = new Date(); + const args = arguments; + if (last && now < last + delay) { + // le délai n'est pas écoulé on reset le timer + clearTimeout(timer); + timer = setTimeout(function () { + last = now; + callback.apply(context, args); + }, delay); + } else { + last = now; + callback.apply(context, args); + } + }; } -export { savePercent, retrievePercent }; +export { savePercent, retrievePercent, initFilters, initExport, throttle }; diff --git a/app/Resources/static/themes/baggy/index.js b/app/Resources/static/themes/baggy/index.js index 39ad49aa..27e3e374 100755 --- a/app/Resources/static/themes/baggy/index.js +++ b/app/Resources/static/themes/baggy/index.js @@ -9,6 +9,9 @@ import './js/shortcuts/entry'; /* Tools */ import toggleSaveLinkForm from './js/uiTools'; +import {savePercent, retrievePercent, throttle} from '../_global/js/tools'; + +global.jquery = $; /* Theme style */ import './css/index.scss'; @@ -50,6 +53,39 @@ $(document).ready(() => { }); } + /* ========================================================================== + Annotations & Remember position + ========================================================================== */ + + if ($('article').length) { + const app = new annotator.App(); + + app.include(annotator.ui.main, { + element: document.querySelector('article'), + }); + + const x = JSON.parse($('#annotationroutes').html()); + app.include(annotator.storage.http, x); + + app.start().then(() => { + app.annotations.load({ entry: x.entryId }); + }); + + window.addEventListener('unload', () => { + const scrollTop = $(window).scrollTop(); + const docHeight = $(document).height(); + const scrollPercent = (scrollTop) / (docHeight); + const scrollPercentRounded = Math.round(scrollPercent * 100) / 100; + savePercent(x.entryId, scrollPercentRounded); + }); + + retrievePercent(x.entryId); + + $(window).resize(() => { + retrievePercent(x.entryId); + }); + } + /** * Close window after adding entry if popup */ diff --git a/app/Resources/static/themes/material/index.js b/app/Resources/static/themes/material/index.js index d6afbb8a..408528ea 100755 --- a/app/Resources/static/themes/material/index.js +++ b/app/Resources/static/themes/material/index.js @@ -75,4 +75,37 @@ $(document).ready(() => { const scrollPercent = (s / (d - c)) * 100; $('.progress .determinate').css('width', `${scrollPercent}%`); }); + +/* ========================================================================== + Annotations & Remember position + ========================================================================== */ + + if ($('article').length) { + const app = new annotator.App(); + const x = JSON.parse($('#annotationroutes').html()); + + app.include(annotator.ui.main, { + element: document.querySelector('article'), + }); + + app.include(annotator.storage.http, x); + + app.start().then(() => { + app.annotations.load({ entry: x.entryId }); + }); + + window.addEventListener('unload', () => { + const scrollTop = $(window).scrollTop(); + const docHeight = $(document).height(); + const scrollPercent = (scrollTop) / (docHeight); + const scrollPercentRounded = Math.round(scrollPercent * 100) / 100; + savePercent(x.entryId, scrollPercentRounded); + }); + + retrievePercent(x.entryId); + + $(window).resize(() => { + retrievePercent(x.entryId); + }); + } }); -- cgit v1.2.3