Debian Codegraph @

src/imports/Components/Popups/1.2/popupUtils.js

raw
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 /*!
18 The function opens a popup object which can be either a Component or in a separate
19 QML document. The caller drives the Popup's and Dialog's placement as well as the
20 pointer's. The third parameter (param) can hold a series of properties configuring
21 the popup to be opened. This can be any property defined by the popups and additional
22 custom ones defined in derived popups.
23
24 caller parameter must be given when the Sheet or Dialog is specified using url
25 and opened inside a Window component. If not, the Sheet or Dialog will use the
26 application's root item as dismiss area.
27 */
28 function open(popup, caller, params) {
29 var popupComponent = null;
30 var rootObject = null;
31 if (popup.createObject) {
32 // popup is a component and can create an object
33 popupComponent = popup;
34 rootObject = QuickUtils.rootItem(popup);
35 } else if (typeof popup === "string") {
36 popupComponent = Qt.createComponent(popup);
37 rootObject = (caller !== undefined) ? QuickUtils.rootItem(caller) : QuickUtils.rootItem(null);
38 } else {
39 print("PopupUtils.open(): "+popup+" is not a component or a link");
40 return null;
41 }
42
43 var popupObject;
44 if (params !== undefined) {
45 popupObject = popupComponent.createObject(rootObject, params);
46 } else {
47 popupObject = popupComponent.createObject(rootObject);
48 }
49 if (!popupObject) {
50 print(popupComponent.errorString().slice(0, -1));
51 print("PopupUtils.open(): Failed to create the popup object.");
52 return;
53 } else if (popupObject.hasOwnProperty("caller") && caller)
54 popupObject.caller = caller;
55
56 // if caller is specified, connect its cleanup to the popup's close
57 // so popups will be removed together with the caller.
58 if (caller)
59 caller.Component.onDestruction.connect(popupObject.__closePopup);
60
61 popupObject.show();
62 popupObject.onVisibleChanged.connect(popupObject.__closeIfHidden);
63 return popupObject;
64 }
65
66 /*!
67 Closes (hides and destroys) the given popup.
68 */
69 function close(popupObject) {
70 popupObject.hide();
71 }