/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2021 - 2023 Martin Grimme <martin.grimme@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
require "shellfish/fengshui" as fengshui;
/**
* Element for managing a stack of {@link ui.Page Page} elements.
*
* Pages are stacked and only the topmost page is active. New pages get added
* to the top of the stack.
*
* @memberof ui
* @name PageStack
* @class
*
* @property {string} initialPage - The URL to load the initial page from.
* @property {bool} loading - [readonly] `true` while a page is being loaded.
* @property {ui.Page[]} pages - [readonly] The stack of pages.
*/
Object {
id: pageStack
property initialPage: ""
property loading: true
property pages: []
/**
* Pushes a new page onto the page stack.
*
* @example
* pageStack.pushPage("MyPage.shui", {
* title: "My Page",
* foo: "bar"
* });
*
* @memberof ui.PageStack.prototype
* @name pushPage
* @method
*
* @param {string} target - The URL of the new page.
* @param {object} props - The properties to set on the page.
*/
function pushPage(target, props)
{
if (typeof target === "function")
{
const page = target();
loading = false;
if (pages.length > 0)
{
pages[pages.length - 1].frozen = true;
pages[pages.length - 1].currentPage = false;
}
page.pageStack = pageStack;
if (props)
{
for (let key in props)
{
page[key] = props[key];
}
}
thisDocument.add(page);
pages.push(page);
page.show(pages.length === 1);
page.currentPage = true;
pagesChanged();
return;
}
const url = target;
loading = true;
fengshui.load(url, __rslv__)
.then(page =>
{
loading = false;
if (pages.length > 0)
{
pages[pages.length - 1].frozen = true;
pages[pages.length - 1].currentPage = false;
}
page.pageStack = pageStack;
if (props)
{
for (let key in props)
{
page[key] = props[key];
}
}
thisDocument.add(page);
pages.push(page);
page.show(pages.length === 1);
page.currentPage = true;
pagesChanged();
});
}
/**
* Pops the topmost page off the page stack.
*
* @memberof ui.PageStack.prototype
* @name popPage
* @method
*/
function popPage()
{
if (pages.length > 1)
{
const p = pages.pop();
p.close(() =>
{
p.releaseLater(); // later as we're still in the callback context of p
pages[pages.length - 1].frozen = false;
pages[pages.length - 1].currentPage = true;
});
pagesChanged();
}
}
onInitialization: () =>
{
if (initialPage !== "")
{
pushPage(initialPage);
}
}
}