2 * Copyright (C) 2014 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
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 General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import Ubuntu.Components 1.1
22 objectName: "dashNavigation"
25 property var scope: null
26 property var scopeStyle: null
27 property color foregroundColor: Theme.palette.normal.baseText
28 property bool isAltNavigation: false
29 property bool showDivider: false
32 readonly property var currentNavigation: scope && scope[hasNavigation] ? getNavigation(scope[currentNavigationId]) : null
33 readonly property alias listView: navigationListView
34 property bool showList: false
37 // Are we drilling down the tree or up?
38 property bool isGoingBack: false
39 readonly property string hasNavigation: isAltNavigation ? "hasAltNavigation" : "hasNavigation"
40 readonly property string currentNavigationId: isAltNavigation ? "currentAltNavigationId" : "currentNavigationId"
41 function getNavigation(navId) {
42 if (isAltNavigation) {
43 return scope.getAltNavigation(navId);
45 return scope.getNavigation(navId);
49 visible: root.currentNavigation != null
52 navigationListView.updateMaxHeight();
53 root.showList = !root.showList;
58 anchors.margins: units.gu(2)
59 anchors.rightMargin: units.gu(5)
60 verticalAlignment: Text.AlignVCenter
61 text: root.currentNavigation ? root.currentNavigation.label : ""
62 color: root.foregroundColor
63 elide: Text.ElideRight
68 anchors.verticalCenter: parent.verticalCenter
69 anchors.right: parent.right
70 anchors.rightMargin: units.gu(2)
71 name: showList ? "up" : "down"
74 color: root.foregroundColor
77 // navigationListView is outside root
79 id: navigationListView
80 objectName: "navigationListView"
82 orientation: ListView.Horizontal
84 clip: root.width != windowWidth
88 // navigationId: the navigation id of the navigation the list represents
89 // nullifyNavigation: overrides navigationId to be null
90 // This is used to "clear" the delegate when going "right" on the tree
92 anchors.top: root.bottom
93 property int maxHeight: -1
94 Component.onCompleted: updateMaxHeight();
95 function updateMaxHeight()
97 maxHeight = (windowHeight - mapToItem(null, 0, 0).y) - units.gu(8);
99 property int prevHeight: maxHeight
100 height: currentItem ? currentItem.height : maxHeight
103 prevHeight = currentItem.desiredHeight;
106 highlightMoveDuration: UbuntuAnimation.FastDuration
107 delegate: DashNavigationList {
108 objectName: "navigation" + index
110 width: navigationListView.width
111 scopeStyle: root.scopeStyle
112 foregroundColor: root.foregroundColor
113 property real desiredHeight: {
115 if (navigation && navigation.loaded && x == navigationListView.contentX)
117 navigationListView.updateMaxHeight();
118 return Math.min(implicitHeight, navigationListView.maxHeight);
120 return navigationListView.prevHeight;
126 height: desiredHeight
127 navigation: (nullifyNavigation || !scope) ? null : getNavigation(navigationId)
128 currentNavigation: root.currentNavigation
130 scope.setNavigationState(newNavigationId, isAltNavigation);
131 // We only need to add a new item to the model
132 // if we have children, otherwise just load it
135 navigationModel.append({"navigationId": newNavigationId, "nullifyNavigation": false});
136 navigationListView.currentIndex++;
141 onGoBackToParentClicked: {
142 scope.setNavigationState(navigation.parentNavigationId, isAltNavigation);
144 navigationModel.setProperty(navigationListView.currentIndex - 1, "nullifyNavigation", false);
145 navigationListView.currentIndex--;
147 onAllNavigationClicked: {
149 if (root.currentNavigation.parentNavigationId == navigation.navigationId) {
150 // For leaves we have to go to the parent too
151 scope.setNavigationState(root.currentNavigation.parentNavigationId, isAltNavigation);
156 if (contentX == width * navigationListView.currentIndex) {
158 navigationModel.remove(navigationListView.currentIndex + 1);
160 navigationModel.setProperty(navigationListView.currentIndex - 1, "nullifyNavigation", true);
168 top: navigationListView.bottom
169 left: navigationListView.left
170 right: navigationListView.right
172 fillMode: Image.Stretch
173 source: "graphics/navigation_shadow.png"
174 visible: root.showList
178 navigationModel.clear();
179 if (scope && scope[hasNavigation]) {
180 navigationModel.append({"navigationId": scope[currentNavigationId], "nullifyNavigation": false});
186 // This is duplicated since we can't have something based on the dynamic hasNavigation string property
187 onHasNavigationChanged: {
188 if (!root.isAltNavigation) {
189 if (scope.hasNavigation) {
190 navigationModel.append({"navigationId": scope.currentNavigationId, "nullifyNavigation": false});
192 navigationModel.clear();
196 onHasAltNavigationChanged: {
197 if (root.isAltNavigation) {
198 if (scope.hasAltNavigation) {
199 navigationModel.append({"navigationId": scope.currentAltNavigationId, "nullifyNavigation": false});
201 navigationModel.clear();
208 anchors.fill: navigationListView
209 enabled: root.showList
210 onPressed: root.showList = false
214 visible: root.showDivider
217 topMargin: units.dp(1)
218 bottom: parent.bottom
220 leftMargin: -units.dp(0.5)
223 color: root.foregroundColor