| 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 1.3 |
| 44 |
import Lomiri.Components.Popups 1.3 |
| 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 if (typeof popup === "string") { |
| 61 |
popupComponent = Qt.createComponent(popup); |
| 62 |
rootObject = (caller !== undefined) ? QuickUtils.rootItem(caller) : QuickUtils.rootItem(null); |
| 63 |
} else { |
| 64 |
print("PopupUtils.open(): "+popup+" is not a component or a link"); |
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
if (!rootObject) { |
| 69 |
print("PopupUtils.open(): Failed to get the root object."); |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
var popupObject; |
| 74 |
// If there's an active item, save it so we can restore it later |
| 75 |
var prevFocusItem = (typeof window !== "undefined") && window ? window.activeFocusItem : null; |
| 76 |
if (params !== undefined) { |
| 77 |
popupObject = popupComponent.createObject(rootObject, params); |
| 78 |
} else { |
| 79 |
popupObject = popupComponent.createObject(rootObject); |
| 80 |
} |
| 81 |
if (!popupObject) { |
| 82 |
print(popupComponent.errorString().slice(0, -1)); |
| 83 |
print("PopupUtils.open(): Failed to create the popup object."); |
| 84 |
return; |
| 85 |
} else if (popupObject.hasOwnProperty("caller") && caller) { |
| 86 |
popupObject.caller = caller; |
| 87 |
} else if (popupObject.hasOwnProperty("__setPreviousActiveFocusItem")) { |
| 88 |
popupObject.__setPreviousActiveFocusItem(prevFocusItem); |
| 89 |
} |
| 90 |
|
| 91 |
// if caller is specified, connect its cleanup to the popup's close |
| 92 |
// so popups will be removed together with the caller. |
| 93 |
if (caller) |
| 94 |
caller.Component.onDestruction.connect(popupObject.__closePopup); |
| 95 |
|
| 96 |
// Instantly (no fading) close and destroy the popup when the component |
| 97 |
// gets destroyed because that invalidates the QML context of the popup |
| 98 |
// and prevents the closing after fadeout. See bug 1568016. |
| 99 |
popupComponent.Component.destruction.connect(popupObject.__closePopup); |
| 100 |
|
| 101 |
popupObject.show(); |
| 102 |
popupObject.onVisibleChanged.connect(popupObject.__closeIfHidden); |
| 103 |
return popupObject; |
| 104 |
} |
| 105 |
|
| 106 |
/*! |
| 107 |
\qmlmethod popupUtils::close(popupObject) |
| 108 |
Closes (hides and destroys) the given popup. |
| 109 |
|
| 110 |
\qml |
| 111 |
import Lomiri.Components 1.3 |
| 112 |
import Lomiri.Components.Popups 1.3 |
| 113 |
|
| 114 |
Button { |
| 115 |
onClicked: PopupUtils.close(popup) |
| 116 |
} |
| 117 |
\endqml |
| 118 |
*/ |
| 119 |
function close(popupObject) { |
| 120 |
popupObject.hide(); |
| 121 |
} |