Changeset View
Changeset View
Standalone View
Standalone View
applets/kicker/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 * | ||
Context not available. | |||||
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.unshift(action, separator); | 31 | actionList.unshift(separator); | ||
32 | // actionList = actions.concat(actionList); // this crashes Qt O.o | ||||
33 | actionList.unshift.apply(actionList, actions); | ||||
31 | } else { | 34 | } else { | ||
32 | actionList = [action]; | 35 | actionList = actions; | ||
33 | } | 36 | } | ||
34 | } | 37 | } | ||
35 | 38 | | |||
36 | actionMenu.actionList = actionList; | 39 | actionMenu.actionList = actionList; | ||
37 | } | 40 | } | ||
38 | 41 | | |||
39 | function createFavoriteAction(favoriteModel, favoriteId) { | 42 | function createFavoriteActions(favoriteModel, favoriteId) { | ||
40 | if (favoriteModel === null || !favoriteModel.enabled || favoriteId == null) { | 43 | if (favoriteModel === null || !favoriteModel.enabled || favoriteId == null) { | ||
41 | return null; | 44 | return null; | ||
42 | } | 45 | } | ||
43 | 46 | | |||
44 | var action = {}; | 47 | var activities = favoriteModel.activities.runningActivities; | ||
48 | | ||||
49 | if (activities.length <= 1) { | ||||
50 | var action = {}; | ||||
51 | | ||||
52 | if (favoriteModel.isFavorite(favoriteId)) { | ||||
53 | action.text = i18n("Remove from Favorites"); | ||||
54 | action.icon = "list-remove"; | ||||
55 | action.actionId = "_kicker_favorite_remove"; | ||||
56 | } else if (favoriteModel.maxFavorites == -1 || favoriteModel.count < favoriteModel.maxFavorites) { | ||||
57 | action.text = i18n("Add to Favorites"); | ||||
58 | action.icon = "bookmark-new"; | ||||
59 | action.actionId = "_kicker_favorite_add"; | ||||
60 | } else { | ||||
61 | return null; | ||||
62 | } | ||||
63 | | ||||
64 | action.actionArgument = { favoriteModel: favoriteModel, favoriteId: favoriteId }; | ||||
65 | | ||||
66 | return [action]; | ||||
45 | 67 | | |||
46 | if (favoriteModel.isFavorite(favoriteId)) { | | |||
47 | action.text = i18n("Remove from Favorites"); | | |||
48 | action.icon = "list-remove"; | | |||
49 | action.actionId = "_kicker_favorite_remove"; | | |||
50 | } else if (favoriteModel.maxFavorites == -1 || favoriteModel.count < favoriteModel.maxFavorites) { | | |||
51 | action.text = i18n("Add to Favorites"); | | |||
52 | action.icon = "bookmark-new"; | | |||
53 | action.actionId = "_kicker_favorite_add"; | | |||
54 | } else { | 68 | } else { | ||
55 | return null; | 69 | var actions = []; | ||
56 | } | 70 | | ||
71 | var linkedActivities = favoriteModel.linkedActivitiesFor(favoriteId); | ||||
72 | | ||||
73 | // Adding the item to link/unlink to all activities | ||||
74 | | ||||
75 | var linkedToAllActivities = | ||||
76 | !(linkedActivities.indexOf(":global") === -1); | ||||
77 | | ||||
78 | actions.push({ | ||||
79 | text : i18n("On All Activities"), | ||||
80 | checkable : true, | ||||
81 | | ||||
82 | actionId : linkedToAllActivities ? | ||||
83 | "_kicker_favorite_remove_from_activity" : | ||||
84 | "_kicker_favorite_set_to_activity", | ||||
85 | checked : linkedToAllActivities, | ||||
86 | | ||||
87 | actionArgument : { | ||||
88 | favoriteModel: favoriteModel, | ||||
89 | favoriteId: favoriteId, | ||||
90 | favoriteActivity: "" | ||||
91 | } | ||||
92 | }); | ||||
93 | | ||||
94 | | ||||
95 | // Adding items for each activity separately | ||||
96 | | ||||
97 | var addActivityItem = function(activityId, activityName) { | ||||
98 | var linkedToThisActivity = | ||||
99 | !(linkedActivities.indexOf(activityId) === -1); | ||||
57 | 100 | | |||
58 | action.actionArgument = { favoriteModel: favoriteModel, favoriteId: favoriteId }; | 101 | actions.push({ | ||
102 | text : activityName, | ||||
103 | checkable : true, | ||||
104 | checked : linkedToThisActivity && !linkedToAllActivities, | ||||
59 | 105 | | |||
60 | return action; | 106 | actionId : | ||
107 | // If we are on all activities, and the user clicks just one | ||||
108 | // specific activity, unlink from everything else | ||||
109 | linkedToAllActivities ? "_kicker_favorite_set_to_activity" : | ||||
110 | | ||||
111 | // If we are linked to the current activity, just unlink from | ||||
112 | // that single one | ||||
113 | linkedToThisActivity ? "_kicker_favorite_remove_from_activity" : | ||||
114 | | ||||
115 | // Otherwise, link to this activity, but do not unlink from | ||||
116 | // other ones | ||||
117 | "_kicker_favorite_add_to_activity", | ||||
118 | | ||||
119 | actionArgument : { | ||||
120 | favoriteModel : favoriteModel, | ||||
121 | favoriteId : favoriteId, | ||||
122 | favoriteActivity : activityId | ||||
123 | } | ||||
124 | }); | ||||
125 | }; | ||||
126 | | ||||
127 | // Adding the item to link/unlink to the current activity | ||||
128 | | ||||
129 | addActivityItem(favoriteModel.activities.currentActivity, i18n("On The Current Activity")); | ||||
130 | | ||||
131 | actions.push({ | ||||
132 | type: "separator", | ||||
133 | actionId: "_kicker_favorite_separator" | ||||
134 | }); | ||||
135 | | ||||
136 | // Adding the items for each activity | ||||
137 | | ||||
138 | activities.forEach(function(activityId) { | ||||
139 | addActivityItem(activityId, favoriteModel.activityNameForId(activityId)); | ||||
140 | }); | ||||
141 | | ||||
142 | return [{ | ||||
143 | text : i18n("Show In Favorites"), | ||||
144 | icon : "favorite", | ||||
145 | subActions : actions | ||||
146 | }]; | ||||
147 | } | ||||
61 | } | 148 | } | ||
62 | 149 | | |||
63 | function triggerAction(model, index, actionId, actionArgument) { | 150 | function triggerAction(model, index, actionId, actionArgument) { | ||
Context not available. | |||||
85 | var favoriteId = actionArgument.favoriteId; | 172 | var favoriteId = actionArgument.favoriteId; | ||
86 | var favoriteModel = actionArgument.favoriteModel; | 173 | var favoriteModel = actionArgument.favoriteModel; | ||
87 | 174 | | |||
175 | console.log(actionId); | ||||
176 | | ||||
88 | if (favoriteModel === null || favoriteId == null) { | 177 | if (favoriteModel === null || favoriteId == null) { | ||
89 | return null; | 178 | return null; | ||
90 | } | 179 | } | ||
180 | | ||||
91 | if (actionId == "_kicker_favorite_remove") { | 181 | if (actionId == "_kicker_favorite_remove") { | ||
92 | favoriteModel.removeFavorite(favoriteId); | 182 | console.log("Removing from all activities"); | ||
183 | favoriteModel.removeFavoriteFrom(favoriteId, ":any"); | ||||
184 | | ||||
93 | } else if (actionId == "_kicker_favorite_add") { | 185 | } else if (actionId == "_kicker_favorite_add") { | ||
94 | favoriteModel.addFavorite(favoriteId); | 186 | console.log("Adding to global activity"); | ||
187 | favoriteModel.addFavoriteTo(favoriteId, ":global"); | ||||
188 | | ||||
189 | } else if (actionId == "_kicker_favorite_remove_from_activity") { | ||||
190 | console.log("Removing from a specific activity"); | ||||
191 | favoriteModel.removeFavoriteFrom(favoriteId, actionArgument.favoriteActivity); | ||||
192 | | ||||
193 | } else if (actionId == "_kicker_favorite_add_to_activity") { | ||||
194 | console.log("Adding to another activity"); | ||||
195 | favoriteModel.addFavoriteTo(favoriteId, actionArgument.favoriteActivity); | ||||
196 | | ||||
197 | } else if (actionId == "_kicker_favorite_set_to_activity") { | ||||
198 | console.log("Removing the item from the favourites, and re-adding it just to be on a specific activity"); | ||||
199 | favoriteModel.setFavoriteOn(favoriteId, actionArgument.favoriteActivity); | ||||
200 | | ||||
95 | } | 201 | } | ||
96 | } | 202 | } | ||
Context not available. |