File indexing completed on 2025-03-09 04:05:57

0001 /****************************************************************************
0002 **
0003 ** SPDX-FileCopyrightText: 2011 Nokia Corporation and /or its subsidiary(-ies).
0004 ** All rights reserved.
0005 ** Contact: Nokia Corporation (qt-info@nokia.com)
0006 **
0007 ** This file is part of the Qt Components project.
0008 **
0009 ** $QT_BEGIN_LICENSE:BSD$
0010 ** SPDX-License-Identifier: BSD-3-Clause
0011 **
0012 ****************************************************************************/
0013 
0014 // Page stack. Items are page containers.
0015 var pageStack = [];
0016 
0017 // Page component cache map. Key is page url, value is page component.
0018 var componentCache = {};
0019 
0020 // Returns the page stack depth.
0021 function getDepth() {
0022     return pageStack.length;
0023 }
0024 
0025 // Pushes a page on the stack.
0026 function push(page, properties, replace, immediate) {
0027     // page order sanity check
0028     if ((!replace && page == currentPage)
0029         || (replace && pageStack.length > 1
0030         && page == pageStack[pageStack.length - 2].page)) {
0031         throw new Error("Cannot navigate so that the resulting page stack has two consecutive entries of the same page instance.");
0032     }
0033 
0034     // figure out if more than one page is being pushed
0035     var pages;
0036     if (page instanceof Array) {
0037         pages = page;
0038         page = pages.pop();
0039         if (page.createObject === undefined && page.parent === undefined && typeof page != "string") {
0040             properties = properties || page.properties;
0041             page = page.page;
0042         }
0043     }
0044 
0045     // get the current container
0046     var oldContainer = pageStack[pageStack.length - 1];
0047 
0048     // pop the old container off the stack if this is a replace
0049     if (oldContainer && replace) {
0050         pageStack.pop();
0051     }
0052 
0053     // push any extra defined pages onto the stack
0054     if (pages) {
0055         var i;
0056         for (i = 0; i < pages.length; i++) {
0057             var tPage = pages[i];
0058             var tProps;
0059             if (tPage.createObject === undefined && tPage.parent === undefined && typeof tPage != "string") {
0060                 tProps = tPage.properties;
0061                 tPage = tPage.page;
0062             }
0063             pageStack.push(initPage(tPage, tProps));
0064         }
0065     }
0066 
0067     // initialize the page
0068     var container = initPage(page, properties);
0069 
0070     // push the page container onto the stack
0071     pageStack.push(container);
0072 
0073     depth = pageStack.length;
0074     currentPage = container.page;
0075 
0076     // perform page transition
0077     immediate = immediate || !oldContainer;
0078     var orientationChange = false;
0079     if (oldContainer) {
0080         orientationChange = orientationChanges(oldContainer.page, container.page);
0081         oldContainer.pushExit(replace, immediate, orientationChange);
0082     }
0083 
0084     // sync tool bar
0085     var tools = container.page.tools || null;
0086     if (toolBar) {
0087         toolBar.setTools(tools, immediate ? "set" : replace ? "replace" : "push");
0088     }
0089 
0090     container.pushEnter(immediate, orientationChange);
0091     return container.page;
0092 }
0093 
0094 // Initializes a page and its container.
0095 function initPage(page, properties) {
0096     var container = containerComponent.createObject(root);
0097 
0098     var pageComp;
0099     if (page.createObject) {
0100         // page defined as component
0101         pageComp = page;
0102     } else if (typeof page == "string") {
0103         // page defined as string (a url)
0104         pageComp = componentCache[page];
0105         if (!pageComp) {
0106             pageComp = componentCache[page] = Qt.createComponent(page);
0107         }
0108     }
0109     if (pageComp) {
0110         if (pageComp.status == Component.Error) {
0111             throw new Error("Error while loading page: " + pageComp.errorString());
0112         } else {
0113             // instantiate page from component
0114             page = pageComp.createObject(container, properties || {});
0115         }
0116     } else {
0117         // copy properties to the page
0118         for (var prop in properties) {
0119             if (properties.hasOwnProperty(prop)) {
0120                 page[prop] = properties[prop];
0121             }
0122         }
0123     }
0124 
0125     container.page = page;
0126     if (page.parent == null) {
0127         container.owner = container;
0128     } else {
0129         container.owner = page.parent;
0130     }
0131 
0132     // the page has to be reparented if
0133     if (page.parent != container) {
0134         page.parent = container;
0135     }
0136 
0137     if (page.pageStack !== undefined) {
0138         page.pageStack = root;
0139     }
0140 
0141     page.anchors.fill = container
0142 
0143     return container;
0144 }
0145 
0146 // Pops a page off the stack.
0147 function pop(page, immediate) {
0148     // make sure there are enough pages in the stack to pop
0149     if (pageStack.length > 1) {
0150         //unwind to itself means no pop
0151         if (page !== undefined && page == pageStack[pageStack.length - 1].page) {
0152             return
0153         }
0154         // pop the current container off the stack and get the next container
0155         var oldContainer = pageStack.pop();
0156         var container = pageStack[pageStack.length - 1];
0157         if (page !== undefined) {
0158             // an unwind target has been specified - pop until we find it
0159             while (page != container.page && pageStack.length > 1) {
0160                 container.cleanup();
0161                 pageStack.pop();
0162                 container = pageStack[pageStack.length - 1];
0163             }
0164         }
0165 
0166         depth = pageStack.length;
0167         currentPage = container.page;
0168 
0169         // perform page transition
0170         var orientationChange = orientationChanges(oldContainer.page, container.page);
0171         oldContainer.popExit(immediate, orientationChange);
0172         container.popEnter(immediate, orientationChange);
0173 
0174         // sync tool bar
0175         var tools = container.page.tools || null;
0176         if (toolBar) {
0177             toolBar.setTools(tools, immediate ? "set" : "pop");
0178         }
0179         return oldContainer.page;
0180     } else {
0181         return null;
0182     }
0183 }
0184 
0185 // Checks if the orientation changes between oldPage and newPage
0186 function orientationChanges(oldPage, newPage) {
0187     return false;
0188 }
0189 
0190 // Clears the page stack.
0191 function clear() {
0192     var container;
0193     while (container = pageStack.pop()) {
0194         container.cleanup();
0195     }
0196     depth = 0;
0197     currentPage = null;
0198 }
0199 
0200 // Iterates through all pages in the stack (top to bottom) to find a page.
0201 function find(func) {
0202     for (var i = pageStack.length - 1; i >= 0; i--) {
0203         var page = pageStack[i].page;
0204         if (func(page)) {
0205             return page;
0206         }
0207     }
0208     return null;
0209 }
0210