Changeset View
Changeset View
Standalone View
Standalone View
applets/kickoff/package/contents/code/tools.js
1 | /*************************************************************************** | 1 | /*************************************************************************** | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2013 by Aurélien Gâteau <agateau@kde.org> * | 2 | * Copyright (C) 2013 by Aurélien Gâteau <agateau@kde.org> * | ||
3 | * Copyright (C) 2013-2015 by Eike Hein <hein@kde.org> * | 3 | * Copyright (C) 2013-2015 by Eike Hein <hein@kde.org> * | ||
4 | * Copyright (C) 2017 by Ivan Cukic <ivan.cukic@kde.org> * | ||||
4 | * * | 5 | * * | ||
5 | * This program is free software; you can redistribute it and/or modify * | 6 | * This program is free software; you can redistribute it and/or modify * | ||
6 | * it under the terms of the GNU General Public License as published by * | 7 | * it under the terms of the GNU General Public License as published by * | ||
7 | * the Free Software Foundation; either version 2 of the License, or * | 8 | * the Free Software Foundation; either version 2 of the License, or * | ||
8 | * (at your option) any later version. * | 9 | * (at your option) any later version. * | ||
9 | * * | 10 | * * | ||
10 | * This program is distributed in the hope that it will be useful, * | 11 | * This program is distributed in the hope that it will be useful, * | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
13 | * GNU General Public License for more details. * | 14 | * GNU General Public License for more details. * | ||
14 | * * | 15 | * * | ||
15 | * You should have received a copy of the GNU General Public License * | 16 | * You should have received a copy of the GNU General Public License * | ||
16 | * along with this program; if not, write to the * | 17 | * along with this program; if not, write to the * | ||
17 | * Free Software Foundation, Inc., * | 18 | * Free Software Foundation, Inc., * | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | ||
19 | ***************************************************************************/ | 20 | ***************************************************************************/ | ||
20 | 21 | | |||
21 | function fillActionMenu(actionMenu, actionList, favoriteModel, favoriteId) { | 22 | function fillActionMenu(actionMenu, actionList, favoriteModel, favoriteId) { | ||
22 | // Accessing actionList can be a costly operation, so we don't | 23 | // Accessing actionList can be a costly operation, so we don't | ||
23 | // access it until we need the menu. | 24 | // access it until we need the menu. | ||
24 | 25 | | |||
25 | var action = createFavoriteAction(favoriteModel, favoriteId); | 26 | var actions = createFavoriteActions(favoriteModel, favoriteId); | ||
26 | 27 | | |||
27 | if (action) { | 28 | if (actions) { | ||
28 | if (actionList && actionList.length > 0) { | 29 | if (actionList && actionList.length > 0) { | ||
29 | var separator = { "type": "separator" }; | 30 | var separator = { "type": "separator" }; | ||
30 | actionList.push(separator, action); | 31 | actionList.unshift(separator); | ||
32 | actionList.unshift.apply(actionList, actions); | ||||
31 | } else { | 33 | } else { | ||
32 | actionList = [action]; | 34 | actionList = actions; | ||
33 | } | 35 | } | ||
34 | } | 36 | } | ||
35 | 37 | | |||
36 | actionMenu.actionList = actionList; | 38 | actionMenu.actionList = actionList; | ||
37 | } | 39 | } | ||
38 | 40 | | |||
39 | function createFavoriteAction(favoriteModel, favoriteId) { | 41 | function createFavoriteActions(favoriteModel, favoriteId) { | ||
40 | // Don't allow changes to favorites when system is immutable. | 42 | // Don't allow changes to favorites when system is immutable. | ||
41 | if (plasmoid.immutability === PlasmaCore.Types.SystemImmutable) { | 43 | if (plasmoid.immutability === PlasmaCore.Types.SystemImmutable) { | ||
42 | return null; | 44 | return null; | ||
43 | } | 45 | } | ||
44 | 46 | | |||
45 | if (favoriteModel === null || !favoriteModel.enabled || favoriteId == null) { | 47 | if (favoriteModel === null || !favoriteModel.enabled || favoriteId == null) { | ||
46 | return null; | 48 | return null; | ||
47 | } | 49 | } | ||
48 | 50 | | |||
51 | var activities = favoriteModel.activities.runningActivities; | ||||
52 | | ||||
53 | if (activities.length <= 1) { | ||||
49 | var action = {}; | 54 | var action = {}; | ||
50 | 55 | | |||
51 | if (favoriteModel.isFavorite(favoriteId)) { | 56 | if (favoriteModel.isFavorite(favoriteId)) { | ||
52 | action.text = i18n("Remove from Favorites"); | 57 | action.text = i18n("Remove from Favorites"); | ||
53 | action.icon = "list-remove"; | 58 | action.icon = "list-remove"; | ||
54 | action.actionId = "_kicker_favorite_remove"; | 59 | action.actionId = "_kicker_favorite_remove"; | ||
55 | } else if (favoriteModel.maxFavorites == -1 || favoriteModel.count < favoriteModel.maxFavorites) { | 60 | } else if (favoriteModel.maxFavorites == -1 || favoriteModel.count < favoriteModel.maxFavorites) { | ||
56 | action.text = i18n("Add to Favorites"); | 61 | action.text = i18n("Add to Favorites"); | ||
57 | action.icon = "bookmark-new"; | 62 | action.icon = "bookmark-new"; | ||
58 | action.actionId = "_kicker_favorite_add"; | 63 | action.actionId = "_kicker_favorite_add"; | ||
59 | } else { | 64 | } else { | ||
60 | return null; | 65 | return null; | ||
61 | } | 66 | } | ||
62 | 67 | | |||
63 | action.actionArgument = { favoriteModel: favoriteModel, favoriteId: favoriteId }; | 68 | action.actionArgument = { favoriteModel: favoriteModel, favoriteId: favoriteId }; | ||
64 | 69 | | |||
65 | return action; | 70 | return [action]; | ||
71 | | ||||
72 | } else { | ||||
73 | var actions = []; | ||||
74 | | ||||
75 | var linkedActivities = favoriteModel.linkedActivitiesFor(favoriteId); | ||||
76 | | ||||
77 | // Adding the item to link/unlink to all activities | ||||
78 | | ||||
79 | var linkedToAllActivities = | ||||
80 | !(linkedActivities.indexOf(":global") === -1); | ||||
81 | | ||||
82 | actions.push({ | ||||
83 | text : i18n("On All Activities"), | ||||
84 | checkable : true, | ||||
85 | | ||||
86 | actionId : linkedToAllActivities ? | ||||
87 | "_kicker_favorite_remove_from_activity" : | ||||
88 | "_kicker_favorite_set_to_activity", | ||||
89 | checked : linkedToAllActivities, | ||||
90 | | ||||
91 | actionArgument : { | ||||
92 | favoriteModel: favoriteModel, | ||||
93 | favoriteId: favoriteId, | ||||
94 | favoriteActivity: "" | ||||
95 | } | ||||
96 | }); | ||||
97 | | ||||
98 | | ||||
99 | // Adding items for each activity separately | ||||
100 | | ||||
101 | var addActivityItem = function(activityId, activityName) { | ||||
102 | var linkedToThisActivity = | ||||
103 | !(linkedActivities.indexOf(activityId) === -1); | ||||
104 | | ||||
105 | actions.push({ | ||||
106 | text : activityName, | ||||
107 | checkable : true, | ||||
108 | checked : linkedToThisActivity && !linkedToAllActivities, | ||||
109 | | ||||
110 | actionId : | ||||
111 | // If we are on all activities, and the user clicks just one | ||||
112 | // specific activity, unlink from everything else | ||||
113 | linkedToAllActivities ? "_kicker_favorite_set_to_activity" : | ||||
114 | | ||||
115 | // If we are linked to the current activity, just unlink from | ||||
116 | // that single one | ||||
117 | linkedToThisActivity ? "_kicker_favorite_remove_from_activity" : | ||||
118 | | ||||
119 | // Otherwise, link to this activity, but do not unlink from | ||||
120 | // other ones | ||||
121 | "_kicker_favorite_add_to_activity", | ||||
122 | | ||||
123 | actionArgument : { | ||||
124 | favoriteModel : favoriteModel, | ||||
125 | favoriteId : favoriteId, | ||||
126 | favoriteActivity : activityId | ||||
127 | } | ||||
128 | }); | ||||
129 | }; | ||||
130 | | ||||
131 | // Adding the item to link/unlink to the current activity | ||||
132 | | ||||
133 | addActivityItem(favoriteModel.activities.currentActivity, i18n("On The Current Activity")); | ||||
134 | | ||||
135 | actions.push({ | ||||
136 | type: "separator", | ||||
137 | actionId: "_kicker_favorite_separator" | ||||
138 | }); | ||||
139 | | ||||
140 | // Adding the items for each activity | ||||
141 | | ||||
142 | activities.forEach(function(activityId) { | ||||
143 | addActivityItem(activityId, favoriteModel.activityNameForId(activityId)); | ||||
144 | }); | ||||
145 | | ||||
146 | return [{ | ||||
147 | text : i18n("Show In Favorites"), | ||||
148 | icon : "favorite", | ||||
149 | subActions : actions | ||||
150 | }]; | ||||
151 | } | ||||
66 | } | 152 | } | ||
67 | 153 | | |||
68 | function triggerAction(model, index, actionId, actionArgument) { | 154 | function triggerAction(model, index, actionId, actionArgument) { | ||
69 | function startsWith(txt, needle) { | 155 | function startsWith(txt, needle) { | ||
70 | return txt.substr(0, needle.length) === needle; | 156 | return txt.substr(0, needle.length) === needle; | ||
71 | } | 157 | } | ||
72 | 158 | | |||
73 | if (startsWith(actionId, "_kicker_favorite_")) { | 159 | if (startsWith(actionId, "_kicker_favorite_")) { | ||
Show All 10 Lines | |||||
84 | 170 | | |||
85 | function handleFavoriteAction(actionId, actionArgument) { | 171 | function handleFavoriteAction(actionId, actionArgument) { | ||
86 | var favoriteId = actionArgument.favoriteId; | 172 | var favoriteId = actionArgument.favoriteId; | ||
87 | var favoriteModel = actionArgument.favoriteModel; | 173 | var favoriteModel = actionArgument.favoriteModel; | ||
88 | 174 | | |||
89 | if (favoriteModel === null || favoriteId == null) { | 175 | if (favoriteModel === null || favoriteId == null) { | ||
90 | return null; | 176 | return null; | ||
91 | } | 177 | } | ||
178 | | ||||
92 | if (actionId == "_kicker_favorite_remove") { | 179 | if (actionId == "_kicker_favorite_remove") { | ||
93 | favoriteModel.removeFavorite(favoriteId); | 180 | console.log("Removing from all activities"); | ||
181 | favoriteModel.removeFavoriteFrom(favoriteId, ":any"); | ||||
182 | | ||||
94 | } else if (actionId == "_kicker_favorite_add") { | 183 | } else if (actionId == "_kicker_favorite_add") { | ||
95 | favoriteModel.addFavorite(favoriteId); | 184 | console.log("Adding to global activity"); | ||
185 | favoriteModel.addFavoriteTo(favoriteId, ":global"); | ||||
186 | | ||||
187 | } else if (actionId == "_kicker_favorite_remove_from_activity") { | ||||
188 | console.log("Removing from a specific activity"); | ||||
189 | favoriteModel.removeFavoriteFrom(favoriteId, actionArgument.favoriteActivity); | ||||
190 | | ||||
191 | } else if (actionId == "_kicker_favorite_add_to_activity") { | ||||
192 | console.log("Adding to another activity"); | ||||
193 | favoriteModel.addFavoriteTo(favoriteId, actionArgument.favoriteActivity); | ||||
194 | | ||||
195 | } else if (actionId == "_kicker_favorite_set_to_activity") { | ||||
196 | console.log("Removing the item from the favourites, and re-adding it just to be on a specific activity"); | ||||
197 | favoriteModel.setFavoriteOn(favoriteId, actionArgument.favoriteActivity); | ||||
198 | | ||||
96 | } | 199 | } | ||
97 | } | 200 | } |