forked from mystiq/hydrogen-web
Working lightbox pops up and closes
This commit is contained in:
parent
2d3b78b725
commit
1a0b1403ef
4 changed files with 56 additions and 13 deletions
|
@ -81,7 +81,12 @@ export class SessionViewModel extends ViewModel {
|
||||||
}));
|
}));
|
||||||
this._updateCreateRoom(createRoom.get());
|
this._updateCreateRoom(createRoom.get());
|
||||||
|
|
||||||
setupLightboxNavigation(this, 'lightboxViewModel');
|
setupLightboxNavigation(this, 'lightboxViewModel', (eventId) => {
|
||||||
|
return {
|
||||||
|
room,
|
||||||
|
eventId,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const rightpanel = this.navigation.observe("right-panel");
|
const rightpanel = this.navigation.observe("right-panel");
|
||||||
this.track(rightpanel.subscribe(() => this._updateRightPanel()));
|
this.track(rightpanel.subscribe(() => this._updateRightPanel()));
|
||||||
|
|
|
@ -19,11 +19,11 @@ import {ViewModel} from "../../ViewModel";
|
||||||
export class LightboxViewModel extends ViewModel {
|
export class LightboxViewModel extends ViewModel {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
super(options);
|
super(options);
|
||||||
this._eventId = options.eventId;
|
this._eventEntry = options.eventEntry;
|
||||||
|
this._eventId = options.eventId || options.eventEntry.id;
|
||||||
this._unencryptedImageUrl = null;
|
this._unencryptedImageUrl = null;
|
||||||
this._decryptedImage = null;
|
this._decryptedImage = null;
|
||||||
this._closeUrl = this.urlCreator.urlUntilSegment("room");
|
this._closeUrl = this.urlCreator.urlUntilSegment("room");
|
||||||
this._eventEntry = options.eventEntry;
|
|
||||||
this._date = null;
|
this._date = null;
|
||||||
this._subscribeToEvent(options.room, options.eventId);
|
this._subscribeToEvent(options.room, options.eventId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,69 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022 Bruno Windels <bruno@windels.cloud>
|
||||||
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
import {LightboxViewModel} from "./LightboxViewModel.js";
|
import {LightboxViewModel} from "./LightboxViewModel.js";
|
||||||
|
|
||||||
|
// Store the `LightboxViewModel` under a symbol so no one else can tamper with
|
||||||
|
// it. This acts like a private field on the class since no one else has the
|
||||||
|
// symbol to look it up.
|
||||||
let lightboxViewModelSymbol = Symbol('lightboxViewModel');
|
let lightboxViewModelSymbol = Symbol('lightboxViewModel');
|
||||||
|
|
||||||
export function updateLightboxViewModel(vm, fieldName, eventId) {
|
/**
|
||||||
|
* Destroys and creates a new the `LightboxViewModel` depending if
|
||||||
|
* `lightboxChildOptions.eventEntry` or `lightboxChildOptions.eventId` are
|
||||||
|
* provided.
|
||||||
|
*/
|
||||||
|
function updateLightboxViewModel(vm, fieldName, lightboxChildOptions) {
|
||||||
|
// Remove any existing `LightboxViewModel` before we assemble the new one below
|
||||||
if (vm[lightboxViewModelSymbol]) {
|
if (vm[lightboxViewModelSymbol]) {
|
||||||
vm[lightboxViewModelSymbol] = vm.disposeTracked(vm[lightboxViewModelSymbol]);
|
vm[lightboxViewModelSymbol] = vm.disposeTracked(vm[lightboxViewModelSymbol]);
|
||||||
|
// Let the `LightboxView` know that the `LightboxViewModel` has changed
|
||||||
vm.emitChange(fieldName);
|
vm.emitChange(fieldName);
|
||||||
}
|
}
|
||||||
if (eventId) {
|
// Create the new `LightboxViewModel` if the `eventEntry` exists directly or
|
||||||
const room = vm._roomFromNavigation();
|
// `eventId` which we can load from the store
|
||||||
vm[lightboxViewModelSymbol] = vm.track(new LightboxViewModel(vm.childOptions({eventId, room})));
|
if (lightboxChildOptions.eventId || lightboxChildOptions.eventEntry) {
|
||||||
|
vm[lightboxViewModelSymbol] = vm.track(new LightboxViewModel(vm.childOptions(lightboxChildOptions)));
|
||||||
|
// Let the `LightboxView` know that the `LightboxViewModel` has changed
|
||||||
vm.emitChange(fieldName);
|
vm.emitChange(fieldName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Whenever the page navigates somewhere, keep the lightboxViewModel up to date
|
/**
|
||||||
export function setupLightboxNavigation(vm, fieldName = 'lightboxViewModel') {
|
* Handles updating the `LightboxViewModel` whenever the page URL changes and
|
||||||
|
* emits changes which the `LightboxView` will use to re-render. This is a
|
||||||
|
* composable piece of logic to call in an existing `ViewModel`'s constructor.
|
||||||
|
*/
|
||||||
|
export function setupLightboxNavigation(vm, fieldName = 'lightboxViewModel', lightboxChildOptionsFunction) {
|
||||||
|
// On the given `vm`, create a getter at `fieldName` that the
|
||||||
|
// `LightboxViewModel` is exposed at for usage in the view.
|
||||||
Object.defineProperty(vm, fieldName, {
|
Object.defineProperty(vm, fieldName, {
|
||||||
get: function() {
|
get: function() {
|
||||||
vm[lightboxViewModelSymbol];
|
return vm[lightboxViewModelSymbol];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Whenever the page navigates somewhere, keep the `lightboxViewModel` up to date
|
||||||
const lightbox = vm.navigation.observe("lightbox");
|
const lightbox = vm.navigation.observe("lightbox");
|
||||||
vm.track(lightbox.subscribe(eventId => {
|
vm.track(lightbox.subscribe(eventId => {
|
||||||
updateLightboxViewModel(vm, fieldName, eventId);
|
updateLightboxViewModel(vm, fieldName, lightboxChildOptionsFunction(eventId));
|
||||||
}));
|
}));
|
||||||
|
// Also handle the case where the URL already includes `/lightbox/$eventId` (like
|
||||||
|
// from page-load)
|
||||||
const initialLightBoxEventId = lightbox.get();
|
const initialLightBoxEventId = lightbox.get();
|
||||||
updateLightboxViewModel(vm, fieldName, initialLightBoxEventId);
|
updateLightboxViewModel(vm, fieldName, lightboxChildOptionsFunction(initialLightBoxEventId));
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ export {SessionView} from "./platform/web/ui/session/SessionView.js";
|
||||||
export {RoomViewModel} from "./domain/session/room/RoomViewModel.js";
|
export {RoomViewModel} from "./domain/session/room/RoomViewModel.js";
|
||||||
export {RoomView} from "./platform/web/ui/session/room/RoomView.js";
|
export {RoomView} from "./platform/web/ui/session/room/RoomView.js";
|
||||||
export {LightboxView} from "./platform/web/ui/session/room/LightboxView.js";
|
export {LightboxView} from "./platform/web/ui/session/room/LightboxView.js";
|
||||||
export {setupLightboxNavigation, updateLightboxViewModel} from "./domain/session/room/lightbox-navigation.js";
|
export {setupLightboxNavigation} from "./domain/session/room/lightbox-navigation.js";
|
||||||
export {RightPanelView} from "./platform/web/ui/session/rightpanel/RightPanelView.js";
|
export {RightPanelView} from "./platform/web/ui/session/rightpanel/RightPanelView.js";
|
||||||
export {MediaRepository} from "./matrix/net/MediaRepository";
|
export {MediaRepository} from "./matrix/net/MediaRepository";
|
||||||
export {TilesCollection} from "./domain/session/room/timeline/TilesCollection.js";
|
export {TilesCollection} from "./domain/session/room/timeline/TilesCollection.js";
|
||||||
|
|
Loading…
Reference in a new issue