-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More simple lightbox for SDK and compatible with custom History
Changes to go along with matrix-org/matrix-viewer#12 Split out from #653
- Loading branch information
1 parent
8b22998
commit ad56f4d
Showing
7 changed files
with
129 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2022 Bruno Windels <[email protected]> | ||
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"; | ||
|
||
// 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'); | ||
|
||
/** | ||
* 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]) { | ||
vm[lightboxViewModelSymbol] = vm.disposeTracked(vm[lightboxViewModelSymbol]); | ||
// Let the `LightboxView` know that the `LightboxViewModel` has changed | ||
vm.emitChange(fieldName); | ||
} | ||
// Create the new `LightboxViewModel` if the `eventEntry` exists directly or | ||
// `eventId` which we can load from the store | ||
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); | ||
} | ||
} | ||
|
||
/** | ||
* 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, { | ||
get: function() { | ||
return vm[lightboxViewModelSymbol]; | ||
} | ||
}); | ||
|
||
// Whenever the page navigates somewhere, keep the `lightboxViewModel` up to date | ||
const lightbox = vm.navigation.observe("lightbox"); | ||
vm.track(lightbox.subscribe(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(); | ||
updateLightboxViewModel(vm, fieldName, lightboxChildOptionsFunction(initialLightBoxEventId)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright 2020 Bruno Windels <[email protected]> | ||
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 {TemplateView} from "./general/TemplateView"; | ||
|
||
export class LinkView extends TemplateView { | ||
render(t, vm) { | ||
return t.a({ | ||
...vm, | ||
onClick: (e) => { | ||
// Allow the `urlRouter` to cancel the URL navigation and any upstream | ||
// consumer that added their own `onClick` handler. | ||
return vm.urlCreator.linkClick(this, e) || vm.onClick?.(e); | ||
} | ||
}); | ||
} | ||
} |