~ubuntu-branches/ubuntu/quantal/kde-runtime/quantal

« back to all changes in this revision

Viewing changes to plasma/declarativeimports/plasmacomponents/qml/private/PageStack.js

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-06-03 21:50:00 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20120603215000-vn7oarsq0ynrydj5
Tags: upstream-4.8.80
Import upstream version 4.8.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 
4
** All rights reserved.
 
5
** Contact: Nokia Corporation (qt-info@nokia.com)
 
6
**
 
7
** This file is part of the Qt Components project.
 
8
**
 
9
** $QT_BEGIN_LICENSE:BSD$
 
10
** You may use this file under the terms of the BSD license as follows:
 
11
**
 
12
** "Redistribution and use in source and binary forms, with or without
 
13
** modification, are permitted provided that the following conditions are
 
14
** met:
 
15
**   * Redistributions of source code must retain the above copyright
 
16
**     notice, this list of conditions and the following disclaimer.
 
17
**   * Redistributions in binary form must reproduce the above copyright
 
18
**     notice, this list of conditions and the following disclaimer in
 
19
**     the documentation and/or other materials provided with the
 
20
**     distribution.
 
21
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 
22
**     the names of its contributors may be used to endorse or promote
 
23
**     products derived from this software without specific prior written
 
24
**     permission.
 
25
**
 
26
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
27
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
28
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
29
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
30
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
31
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
32
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
33
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
34
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
35
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
36
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
// Page stack. Items are page containers.
 
42
var pageStack = [];
 
43
 
 
44
// Page component cache map. Key is page url, value is page component.
 
45
var componentCache = {};
 
46
 
 
47
// Returns the page stack depth.
 
48
function getDepth() {
 
49
    return pageStack.length;
 
50
}
 
51
 
 
52
// Pushes a page on the stack.
 
53
function push(page, properties, replace, immediate) {
 
54
    // page order sanity check
 
55
    if ((!replace && page == currentPage)
 
56
        || (replace && pageStack.length > 1
 
57
        && page == pageStack[pageStack.length - 2].page)) {
 
58
        throw new Error("Cannot navigate so that the resulting page stack has two consecutive entries of the same page instance.");
 
59
    }
 
60
 
 
61
    // figure out if more than one page is being pushed
 
62
    var pages;
 
63
    if (page instanceof Array) {
 
64
        pages = page;
 
65
        page = pages.pop();
 
66
        if (page.createObject === undefined && page.parent === undefined && typeof page != "string") {
 
67
            properties = properties || page.properties;
 
68
            page = page.page;
 
69
        }
 
70
    }
 
71
 
 
72
    // get the current container
 
73
    var oldContainer = pageStack[pageStack.length - 1];
 
74
 
 
75
    // pop the old container off the stack if this is a replace
 
76
    if (oldContainer && replace) {
 
77
        pageStack.pop();
 
78
    }
 
79
 
 
80
    // push any extra defined pages onto the stack
 
81
    if (pages) {
 
82
        var i;
 
83
        for (i = 0; i < pages.length; i++) {
 
84
            var tPage = pages[i];
 
85
            var tProps;
 
86
            if (tPage.createObject === undefined && tPage.parent === undefined && typeof tPage != "string") {
 
87
                tProps = tPage.properties;
 
88
                tPage = tPage.page;
 
89
            }
 
90
            pageStack.push(initPage(tPage, tProps));
 
91
        }
 
92
    }
 
93
 
 
94
    // initialize the page
 
95
    var container = initPage(page, properties);
 
96
 
 
97
    // push the page container onto the stack
 
98
    pageStack.push(container);
 
99
 
 
100
    depth = pageStack.length;
 
101
    currentPage = container.page;
 
102
 
 
103
    // perform page transition
 
104
    immediate = immediate || !oldContainer;
 
105
    var orientationChange = false;
 
106
    if (oldContainer) {
 
107
        orientationChange = orientationChanges(oldContainer.page, container.page);
 
108
        oldContainer.pushExit(replace, immediate, orientationChange);
 
109
    }
 
110
 
 
111
    // sync tool bar
 
112
    var tools = container.page.tools || null;
 
113
    if (toolBar) {
 
114
        toolBar.setTools(tools, immediate ? "set" : replace ? "replace" : "push");
 
115
    }
 
116
 
 
117
    container.pushEnter(immediate, orientationChange);
 
118
    return container.page;
 
119
}
 
120
 
 
121
// Initializes a page and its container.
 
122
function initPage(page, properties) {
 
123
    var container = containerComponent.createObject(root);
 
124
 
 
125
    var pageComp;
 
126
    if (page.createObject) {
 
127
        // page defined as component
 
128
        pageComp = page;
 
129
    } else if (typeof page == "string") {
 
130
        // page defined as string (a url)
 
131
        pageComp = componentCache[page];
 
132
        if (!pageComp) {
 
133
            pageComp = componentCache[page] = Qt.createComponent(page);
 
134
        }
 
135
    }
 
136
    if (pageComp) {
 
137
        if (pageComp.status == Component.Error) {
 
138
            throw new Error("Error while loading page: " + pageComp.errorString());
 
139
        } else {
 
140
            // instantiate page from component
 
141
            page = pageComp.createObject(container, properties || {});
 
142
        }
 
143
    } else {
 
144
        // copy properties to the page
 
145
        for (var prop in properties) {
 
146
            if (properties.hasOwnProperty(prop)) {
 
147
                page[prop] = properties[prop];
 
148
            }
 
149
        }
 
150
    }
 
151
 
 
152
    container.page = page;
 
153
    if (page.parent == null) {
 
154
        container.owner = container;
 
155
    } else {
 
156
        container.owner = page.parent;
 
157
    }
 
158
 
 
159
    // the page has to be reparented if
 
160
    if (page.parent != container) {
 
161
        page.parent = container;
 
162
    }
 
163
 
 
164
    if (page.pageStack !== undefined) {
 
165
        page.pageStack = root;
 
166
    }
 
167
 
 
168
    page.anchors.fill = container
 
169
 
 
170
    return container;
 
171
}
 
172
 
 
173
// Pops a page off the stack.
 
174
function pop(page, immediate) {
 
175
    // make sure there are enough pages in the stack to pop
 
176
    if (pageStack.length > 1) {
 
177
        //unwind to itself means no pop
 
178
        if (page !== undefined && page == pageStack[pageStack.length - 1].page) {
 
179
            return
 
180
        }
 
181
        // pop the current container off the stack and get the next container
 
182
        var oldContainer = pageStack.pop();
 
183
        var container = pageStack[pageStack.length - 1];
 
184
        if (page !== undefined) {
 
185
            // an unwind target has been specified - pop until we find it
 
186
            while (page != container.page && pageStack.length > 1) {
 
187
                container.cleanup();
 
188
                pageStack.pop();
 
189
                container = pageStack[pageStack.length - 1];
 
190
            }
 
191
        }
 
192
 
 
193
        depth = pageStack.length;
 
194
        currentPage = container.page;
 
195
 
 
196
        // perform page transition
 
197
        var orientationChange = orientationChanges(oldContainer.page, container.page);
 
198
        oldContainer.popExit(immediate, orientationChange);
 
199
        container.popEnter(immediate, orientationChange);
 
200
 
 
201
        // sync tool bar
 
202
        var tools = container.page.tools || null;
 
203
        if (toolBar) {
 
204
            toolBar.setTools(tools, immediate ? "set" : "pop");
 
205
        }
 
206
        return oldContainer.page;
 
207
    } else {
 
208
        return null;
 
209
    }
 
210
}
 
211
 
 
212
// Checks if the orientation changes between oldPage and newPage
 
213
function orientationChanges(oldPage, newPage) {
 
214
    return newPage.orientationLock != PageOrientation.Automatic
 
215
           && newPage.orientationLock != PageOrientation.LockPrevious
 
216
           && newPage.orientationLock != oldPage.orientationLock
 
217
}
 
218
 
 
219
// Clears the page stack.
 
220
function clear() {
 
221
    var container;
 
222
    while (container = pageStack.pop()) {
 
223
        container.cleanup();
 
224
    }
 
225
    depth = 0;
 
226
    currentPage = null;
 
227
}
 
228
 
 
229
// Iterates through all pages in the stack (top to bottom) to find a page.
 
230
function find(func) {
 
231
    for (var i = pageStack.length - 1; i >= 0; i--) {
 
232
        var page = pageStack[i].page;
 
233
        if (func(page)) {
 
234
            return page;
 
235
        }
 
236
    }
 
237
    return null;
 
238
}
 
239