Skip to content

Commit bfcd2bf

Browse files
committed
✨ app: add basic routing
1 parent fe7841c commit bfcd2bf

File tree

1 file changed

+116
-25
lines changed

1 file changed

+116
-25
lines changed

k2e/bootplate/source/App.js

Lines changed: 116 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/* global k2e:false */
1+
/* globals k2e */
22

3-
(function () {
3+
((Router) => {
44

55
let exportPreparationSem;
66
let docsExport;
@@ -12,12 +12,14 @@ enyo.kind({
1212
name: 'k2e.App',
1313
kind: 'FittableRows',
1414
cookieModel: undefined,
15+
destroyClipListRouteHandler: undefined,
16+
destroyClipRouteHandler: undefined,
17+
settingsActive: undefined,
1518
published: {
1619
periodicalTitleSet: undefined,
1720
ignoredTitleSet: undefined,
1821
documents: undefined,
1922
currentThemeClass: 'k2e-document-view-dark',
20-
settingsActive: false,
2123
currentMainPanel: undefined,
2224
currentAppToolbar: undefined,
2325
},
@@ -29,6 +31,8 @@ enyo.kind({
2931
{ from: '.$.mainPanels.index', to: '.currentMainPanel' },
3032
{ from: '.$.appToolbar.index', to: '.currentAppToolbar' },
3133
{ from: '.$.appToolbarSlideable.value', to: '.appToolbarSlidePercentage' },
34+
{ from: '.$.settings.slidedToMin', to: '.settingsActive',
35+
transform: (slidedToMin) => !slidedToMin },
3236
],
3337
observers: [
3438
{ method: 'appToolbarSearchFilterChanged', path: ['$.appToolbar.searchFilter'] },
@@ -58,9 +62,11 @@ enyo.kind({
5862
{ name: 'appToolbar', kind: 'k2e.AppToolbar',
5963
onExportRequested: 'prepareDocumentsAndExport',
6064
onReloadClippingRequested: 'reloadClippings',
61-
onMultiSelectionToggled: 'handleMultiSelectionToggled' },
65+
onMultiSelectionToggled: 'handleMultiSelectionToggled',
66+
onToolbarStateToggled: 'handleToolbarStateToggled' },
6267
] },
63-
{ name: 'settings', kind: 'k2e.settings.SettingsSlideable' },
68+
{ name: 'settings', kind: 'k2e.settings.SettingsSlideable',
69+
onToggleAnimateFinish: 'handleToggleAnimateFinish' },
6470
{ kind: 'FittableColumns', fit: true, components: [
6571
{ name: 'mainPanels', kind: 'Panels', fit: true, arrangerKind: 'CollapsingArranger',
6672
realtimeFit: true, wrap: false, components: [
@@ -78,6 +84,7 @@ enyo.kind({
7884

7985
appToolbarSearchFilterChanged,
8086
appToolbarSlidePercentageChanged,
87+
handleToolbarStateToggled,
8188
handleMultiSelectionToggled,
8289
toggleFullscreen,
8390
exportDocuments,
@@ -97,6 +104,7 @@ enyo.kind({
97104
handleClippingsTextChanged,
98105
handleKeydown,
99106
handleSettingsToggled,
107+
handleToggleAnimateFinish,
100108
handleThemeChanged,
101109
handleFontSizeChanged,
102110
handleFontChanged,
@@ -105,13 +113,13 @@ enyo.kind({
105113
parseKindleClippings,
106114
reloadClippings,
107115
loadClippings,
108-
settingsActiveChanged,
109116
currentMainPanelChanged,
110117
currentAppToolbarChanged,
111118
scrollDocumentToTop,
112119
reflow,
113120
rendered,
114121
create,
122+
destroy,
115123
});
116124

117125
/////////////////////////////////////////////////////////////
@@ -120,6 +128,18 @@ function appToolbarSearchFilterChanged(/*previous, current, property*/) {
120128
this.$.documentSelectorList.scrollToTop();
121129
}
122130

131+
function handleToolbarStateToggled(inSender, { oldState, newState }) {
132+
if (
133+
oldState === this.$.appToolbar.SELECTED_DOCUMENT_TOOLBAR &&
134+
newState === this.$.appToolbar.MAIN_TOOLBAR
135+
) {
136+
// currentMainPanelChanged will handle this
137+
return;
138+
}
139+
140+
changeRoute(this);
141+
}
142+
123143
function appToolbarSlidePercentageChanged(oldValue, newValue) {
124144
if (!this.$.fittableColumns) {
125145
return;
@@ -155,10 +175,6 @@ function handleMultiSelectionToggled(inSender, inEvent) {
155175
this.$.documentSelectorList.set('multiSelected', active);
156176
}
157177

158-
function settingsActiveChanged(/*old, active*/) {
159-
this.$.settings.toggle();
160-
}
161-
162178
function toggleFullscreen() {
163179
const isFullscreen = this.isFullscreen();
164180

@@ -372,23 +388,33 @@ function processQueryError(/*inSender, inResponse*/) {
372388
this.handleQueryEnd();
373389
}
374390

375-
function handleDocumentSelected(inSender, inEvent) {
391+
function handleDocumentSelected(inSender, { reSelected, originator }) {
392+
// Routing: react on document selection
376393
const isScreenNarrow = enyo.Panels.isScreenNarrow();
377394
if (isScreenNarrow) {
378395
this.$.mainPanels.set('index', DOCUMENT_PANEL);
396+
this.$.fittableColumns.applyStyle('padding-bottom', '0');
379397
}
380-
if (inEvent.reSelected) {
398+
if (reSelected) {
381399
return;
382400
}
383401

384-
const docSelector = inEvent.originator;
402+
const docSelector = originator;
385403
const doc = this.documents.getDocumentByKey(docSelector.getKey());
386404
this.log(docSelector.get('key'));
387405
this.log(docSelector.get('index'));
388406
this.log(doc);
389407
this.$.documentControl.set('document', doc);
390408
this.$.appToolbar.set('selectedDocumentTitle', doc.title);
391409
this.$.appToolbarSlideable.animateToMax();
410+
411+
// on narrow screens the route change will happen in
412+
// currentMainPanelChanged since the panel also changes
413+
// along w/ the selected doc
414+
415+
if (!Router.isNavigating && !isScreenNarrow) {
416+
changeRoute(this, this.$.mainPanels.index);
417+
}
392418
}
393419

394420
function handleDocumentMultiSelected(/*inSender, inEvent*/) {
@@ -481,13 +507,9 @@ function loadClippings(clippingsText) {
481507
throw ex;
482508
}
483509

484-
const isScreenNarrow = enyo.Panels.isScreenNarrow();
485510
const settings = k2e.settings.SettingsStorage;
486511
settings.setSetting('clippingsText', clippingsText);
487512
this.$.documentSelectorList.set('documentsRef', this.documents);
488-
if (this.$.mainPanels.index === SIDEBAR_PANEL && !isScreenNarrow) {
489-
this.$.documentSelectorList.selectNextDocument();
490-
}
491513
}
492514

493515
function handleKeydown(inSender, inEvent) {
@@ -538,8 +560,12 @@ function handleKeydown(inSender, inEvent) {
538560
return true;
539561
}
540562

541-
function handleSettingsToggled(/*inSender, inEvent*/) {
542-
this.$.settings.toggle();
563+
function handleSettingsToggled(/* inSender, inEvent */) {
564+
this.$.settings.toggle(true);
565+
}
566+
567+
function handleToggleAnimateFinish() {
568+
changeRoute(this);
543569
}
544570

545571
function handleThemeChanged(inSender, inEvent) {
@@ -682,7 +708,7 @@ function reloadClippings() {
682708

683709
function handleClippingsCleared() {
684710
if (this.$.settings.isAtMax()) {
685-
this.set('settingsActive', false);
711+
// this.set('settingsActive', false);
686712
}
687713

688714
this.$.clippingPickerPopup.show({ autoDismiss: false });
@@ -698,17 +724,18 @@ function currentMainPanelChanged(old, newIndex) {
698724
) {
699725
this.$.appToolbar.popState();
700726
}
727+
728+
changeRoute(this, newIndex);
701729
}
702730

703-
function currentAppToolbarChanged(oldToolbar) {
704-
this.log(oldToolbar);
731+
function currentAppToolbarChanged(oldToolbar/* , newToolbar */) {
705732
if (oldToolbar !== k2e.AppToolbar.SELECTED_DOCUMENT_TOOLBAR) {
706733
return;
707734
}
708735

709-
const appToolbarHeight = this.$.appToolbar.hasNode().scrollHeight;
736+
// const appToolbarHeight = this.$.appToolbar.hasNode().scrollHeight;
710737
this.$.toTopButton.addRemoveClass('visible', false);
711-
this.$.fittableColumns.applyStyle('padding-bottom', `${appToolbarHeight}px`);
738+
// this.$.fittableColumns.applyStyle('padding-bottom', `${appToolbarHeight}px`);
712739
this.$.mainPanels.set('index', SIDEBAR_PANEL);
713740
this.$.appToolbarSlideable.animateToMax();
714741
}
@@ -739,17 +766,38 @@ function reflow() {
739766

740767
function rendered() {
741768
this.inherited(arguments);
769+
const isScreenNarrow = enyo.Panels.isScreenNarrow();
742770
if (!this.documents) {
743771
this.$.clippingPickerPopup.show({ autoDismiss: false });
744772
}
745773
this.reflow();
746774
this.$.sidebar.resize();
775+
Router.resolve();
776+
777+
// Select the first doc in the doc list on non-narrow displays
778+
// if none is already selected
779+
780+
if (
781+
!isScreenNarrow &&
782+
!this.$.documentSelectorList.get('selDocumentSelectorItem') &&
783+
this.$.mainPanels.index === SIDEBAR_PANEL
784+
) {
785+
this.$.documentSelectorList.selectNextDocument();
786+
}
747787
}
748788

749789
function create() {
750790
this.inherited(arguments);
751791
const settings = k2e.settings.SettingsStorage;
752792

793+
this.destroyClipListRouteHandler = Router.onClipListRouteChanged(
794+
handleRouteChange(this, SIDEBAR_PANEL, k2e.AppToolbar.MAIN_TOOLBAR)
795+
);
796+
797+
this.destroyClipRouteHandler = Router.onClipRouteChanged(
798+
handleRouteChange(this, DOCUMENT_PANEL, k2e.AppToolbar.SELECTED_DOCUMENT_TOOLBAR)
799+
);
800+
753801
const cookieModel = new k2e.CookieModel();
754802
cookieModel.fetch();
755803
this.set('cookieModel', cookieModel);
@@ -771,4 +819,47 @@ function create() {
771819
exportPreparationSem = new k2e.util.AsyncSemaphore({ func: this.exportDocuments.bind(this) });
772820
}
773821

774-
})();
822+
function handleRouteChange(app, panelId, defaultToolbarId) {
823+
return (key, { settings: settingsActive, toolbar: toolbarId }) => {
824+
settingsActive = settingsActive === 'true';
825+
app.$.mainPanels.set('index', panelId);
826+
if (key !== undefined) {
827+
app.$.documentSelectorList.selectDocument(key);
828+
}
829+
if (app.get('settingsActive') !== settingsActive) {
830+
app.$.settings.toggle();
831+
}
832+
toolbarId = toolbarId !== undefined ?
833+
Number.parseInt(toolbarId, 10) :
834+
defaultToolbarId;
835+
if (app.$.appToolbar.currentState !== toolbarId) {
836+
const pushed = app.$.appToolbar.tryPushState(toolbarId);
837+
if (!pushed) {
838+
app.$.appToolbar.popState();
839+
}
840+
}
841+
};
842+
}
843+
844+
function changeRoute(app, panelId) {
845+
panelId = panelId !== undefined ? panelId : app.$.mainPanels.index;
846+
const doc = app.$.documentSelectorList.get('selDocumentSelectorItem');
847+
if (!doc) {
848+
return;
849+
}
850+
(panelId === DOCUMENT_PANEL ?
851+
Router.tryClipChanged : Router.tryClipListChanged
852+
)(doc.get('key'), {
853+
settings: app.settingsActive,
854+
toolbar: app.$.appToolbar.currentState,
855+
});
856+
}
857+
858+
function destroy() {
859+
this.inherited(arguments);
860+
861+
this.destroyClipListRouteHandler();
862+
this.destroyClipRouteHandler();
863+
}
864+
865+
})(k2e.util.Router);

0 commit comments

Comments
 (0)