Debian Codegraph @

src/imports/Components/Popups/2.0/PopupUtils.js

raw
1 /*
2 * Copyright 2016 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 \qmltype popupUtils
19 \inqmlmodule Lomiri.Components.Popups 1.0
20 \ingroup lomiri-popups
21 \brief Popover utility functions.
22 */
23
24 /*!
25 \qmlmethod popupUtils::open(popup, caller, params)
26 The function creates and shows a popup object from either a \b Component or URL of a
27 QML document. The caller drives the placement of the popup as well as the
28 pointer's. The third parameter (param) can hold a series of properties configuring
29 the popup to be opened. This can be any property defined by the popups and additional
30 custom ones defined in derived popups.
31
32 \a caller should be given when a \l ComposerSheet or \l Dialog is specified using a URL
33 and opened inside a \b Window. If not, the application's root item will be the dismiss area.
34
35 Returns a popup object, which can be closed using \l close.
36
37 Note that popups created from a file or component will be created as children
38 of the root item. If that's not a good choice, a \b MainWindow used as the
39 root item can set the \b visualRoot property an arbitrary \b Item that acts
40 as the parent of all popups.
41
42 \qml
43 import Lomiri.Components
44 import Lomiri.Components.Popups
45
46 Button {
47 onClicked: PopupUtils.open(popoverComponent, popoverButton, { 'dummy': true } )
48 }
49 \endqml
50
51 See \l Popover, \l ComposerSheet and \l Dialog for their respective documentation and more examples.
52 */
53 function open(popup, caller, params) {
54 var popupComponent = null;
55 var rootObject = null;
56 if (popup.createObject) {
57 // popup is a component and can create an object
58 popupComponent = popup;
59 rootObject = QuickUtils.rootItem(caller !== undefined ? caller : popup);
60 } else {
61 popupComponent = Qt.createComponent(popup);
62 rootObject = (caller !== undefined) ? QuickUtils.rootItem(caller) : QuickUtils.rootItem(null);
63 }
64
65 if (!rootObject) {
66 print("PopupUtils.open(): Failed to get the root object.");
67 return null;
68 }
69
70 var popupObject;
71 // If there's an active item, save it so we can restore it later
72 var prevFocusItem = (typeof window !== "undefined") && window ? window.activeFocusItem : null;
73 if (params !== undefined) {
74 popupObject = popupComponent.createObject(rootObject, params);
75 } else {
76 popupObject = popupComponent.createObject(rootObject);
77 }
78 if (!popupObject) {
79 print(popupComponent.errorString().slice(0, -1));
80 print("PopupUtils.open(): Failed to create the popup object.");
81 return;
82 } else if (popupObject.hasOwnProperty("caller") && caller) {
83 popupObject.caller = caller;
84 } else if (popupObject.hasOwnProperty("__setPreviousActiveFocusItem")) {
85 popupObject.__setPreviousActiveFocusItem(prevFocusItem);
86 }
87
88 // if caller is specified, connect its cleanup to the popup's close
89 // so popups will be removed together with the caller.
90 if (caller)
91 caller.Component.onDestruction.connect(popupObject.__closePopup);
92
93 // Instantly (no fading) close and destroy the popup when the component
94 // gets destroyed because that invalidates the QML context of the popup
95 // and prevents the closing after fadeout. See bug 1568016.
96 popupComponent.Component.destruction.connect(popupObject.__closePopup);
97
98 popupObject.show();
99 popupObject.onVisibleChanged.connect(popupObject.__closeIfHidden);
100 return popupObject;
101 }
102
103 /*!
104 \qmlmethod popupUtils::close(popupObject)
105 Closes (hides and destroys) the given popup.
106
107 \qml
108 import Lomiri.Components
109 import Lomiri.Components.Popups
110
111 Button {
112 onClicked: PopupUtils.close(popup)
113 }
114 \endqml
115 */
116 function close(popupObject) {
117 popupObject.hide();
118 }