| 1 |
/* |
| 2 |
* Copyright 2012 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
//.pragma library // FIXME: cannot refer to Component.Error if I use this. |
| 18 |
// FIXME: ideally we would make this a stateless library, but that breaks applications |
| 19 |
// that rely on accessing context variables in pages that were pushed on a PageStack |
| 20 |
// by url (PageStack.push("FileName.qml")) because of a Qt bug: |
| 21 |
// https://bugreports.qt-project.org/browse/QTBUG-31347 |
| 22 |
|
| 23 |
/*! |
| 24 |
\internal |
| 25 |
Initialize pageWrapper.object. |
| 26 |
*/ |
| 27 |
function initPage(pageWrapper) { |
| 28 |
var pageComponent; |
| 29 |
|
| 30 |
if (pageWrapper.reference.createObject) { |
| 31 |
// page reference is a component |
| 32 |
pageComponent = pageWrapper.reference; |
| 33 |
} else if (typeof pageWrapper.reference == "string") { |
| 34 |
// page reference is a string (url) |
| 35 |
pageComponent = Qt.createComponent(pageWrapper.reference); |
| 36 |
} |
| 37 |
|
| 38 |
var pageObject; |
| 39 |
if (pageComponent) { |
| 40 |
if (pageComponent.status === Component.Error) { |
| 41 |
throw new Error("Error while loading page: " + pageComponent.errorString()); |
| 42 |
} else { |
| 43 |
// create the object |
| 44 |
if (pageWrapper.properties) { |
| 45 |
// initialize the object with the given properties |
| 46 |
pageObject = pageComponent.createObject(pageWrapper, pageWrapper.properties); |
| 47 |
} else { |
| 48 |
pageObject = pageComponent.createObject(pageWrapper); |
| 49 |
} |
| 50 |
pageWrapper.canDestroy = true; |
| 51 |
} |
| 52 |
} else { |
| 53 |
// page reference is an object |
| 54 |
pageObject = pageWrapper.reference; |
| 55 |
pageObject.parent = pageWrapper; |
| 56 |
pageWrapper.canDestroy = false; |
| 57 |
|
| 58 |
// copy the properties to the page object |
| 59 |
for (var prop in pageWrapper.properties) { |
| 60 |
if (pageWrapper.properties.hasOwnProperty(prop)) { |
| 61 |
pageObject[prop] = pageWrapper.properties[prop]; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pageWrapper.object = pageObject; |
| 67 |
return pageObject; |
| 68 |
} |
| 69 |
|
| 70 |
/*! |
| 71 |
\internal |
| 72 |
Create the page object if needed, and make the page object visible. |
| 73 |
*/ |
| 74 |
function activate(pageWrapper) { |
| 75 |
if (!pageWrapper.object) { |
| 76 |
initPage(pageWrapper); |
| 77 |
} |
| 78 |
// Having the same page pushed multiple times on a stack changes |
| 79 |
// the parent of the page object. Change it back here. |
| 80 |
pageWrapper.object.parent = pageWrapper; |
| 81 |
|
| 82 |
// Some page objects are invisible initially. Make visible. |
| 83 |
|
| 84 |
pageWrapper.object.visible = true; |
| 85 |
pageWrapper.active = true; |
| 86 |
} |
| 87 |
|
| 88 |
/*! |
| 89 |
\internal |
| 90 |
Hide page object. |
| 91 |
*/ |
| 92 |
function deactivate(pageWrapper) { |
| 93 |
pageWrapper.active = false; |
| 94 |
} |
| 95 |
|
| 96 |
/*! |
| 97 |
\internal |
| 98 |
Destroy the page object if pageWrapper.canDestroy is true. |
| 99 |
Do nothing if pageWrapper.canDestroy is false. |
| 100 |
*/ |
| 101 |
function destroyObject(pageWrapper) { |
| 102 |
if (pageWrapper.canDestroy) { |
| 103 |
pageWrapper.object.destroy(); |
| 104 |
pageWrapper.object = null; |
| 105 |
pageWrapper.canDestroy = false; |
| 106 |
} |
| 107 |
} |