File indexing completed on 2025-01-26 05:06:13
0001 /* 0002 SPDX-FileCopyrightText: 2013 Aurélien Gâteau <agateau@kde.org> 0003 SPDX-FileCopyrightText: 2013-2015 Eike Hein <hein@kde.org> 0004 SPDX-FileCopyrightText: 2017 Ivan Cukic <ivan.cukic@kde.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 .pragma library 0010 0011 function fillActionMenu(i18n, actionMenu, actionList, favoriteModel, favoriteId) { 0012 // Accessing actionList can be a costly operation, so we don't 0013 // access it until we need the menu. 0014 0015 var actions = createFavoriteActions(i18n, favoriteModel, favoriteId); 0016 0017 if (actions) { 0018 if (actionList && actionList.length > 0) { 0019 var separator = { "type": "separator" }; 0020 actionList.push(separator); 0021 // actionList = actions.concat(actionList); // this crashes Qt O.o 0022 actionList.push.apply(actionList, actions); 0023 } else { 0024 actionList = actions; 0025 } 0026 } 0027 0028 actionMenu.actionList = actionList; 0029 } 0030 0031 function createFavoriteActions(i18n, favoriteModel, favoriteId) { 0032 if (!favoriteModel || !favoriteModel.enabled || !favoriteId) { 0033 return null; 0034 } 0035 0036 0037 if (favoriteModel.activities === undefined || 0038 favoriteModel.activities.runningActivities.length <= 1) { 0039 var action = {}; 0040 0041 if (favoriteModel.isFavorite(favoriteId)) { 0042 action.text = i18n("Remove from Favorites"); 0043 action.icon = "bookmark-remove"; 0044 action.actionId = "_kicker_favorite_remove"; 0045 } else if (favoriteModel.maxFavorites === -1 || favoriteModel.count < favoriteModel.maxFavorites) { 0046 action.text = i18n("Add to Favorites"); 0047 action.icon = "bookmark-new"; 0048 action.actionId = "_kicker_favorite_add"; 0049 } else { 0050 return null; 0051 } 0052 0053 action.actionArgument = { favoriteModel: favoriteModel, favoriteId: favoriteId }; 0054 0055 return [action]; 0056 0057 } else { 0058 var actions = []; 0059 0060 var linkedActivities = favoriteModel.linkedActivitiesFor(favoriteId); 0061 0062 var activities = favoriteModel.activities.runningActivities; 0063 0064 // Adding the item to link/unlink to all activities 0065 0066 var linkedToAllActivities = 0067 !(linkedActivities.indexOf(":global") === -1); 0068 0069 actions.push({ 0070 text : i18n("On All Activities"), 0071 checkable : true, 0072 0073 actionId : linkedToAllActivities ? 0074 "_kicker_favorite_remove_from_activity" : 0075 "_kicker_favorite_set_to_activity", 0076 checked : linkedToAllActivities, 0077 0078 actionArgument : { 0079 favoriteModel: favoriteModel, 0080 favoriteId: favoriteId, 0081 favoriteActivity: "" 0082 } 0083 }); 0084 0085 0086 // Adding items for each activity separately 0087 0088 var addActivityItem = function(activityId, activityName) { 0089 var linkedToThisActivity = 0090 !(linkedActivities.indexOf(activityId) === -1); 0091 0092 actions.push({ 0093 text : activityName, 0094 checkable : true, 0095 checked : linkedToThisActivity && !linkedToAllActivities, 0096 0097 actionId : 0098 // If we are on all activities, and the user clicks just one 0099 // specific activity, unlink from everything else 0100 linkedToAllActivities ? "_kicker_favorite_set_to_activity" : 0101 0102 // If we are linked to the current activity, just unlink from 0103 // that single one 0104 linkedToThisActivity ? "_kicker_favorite_remove_from_activity" : 0105 0106 // Otherwise, link to this activity, but do not unlink from 0107 // other ones 0108 "_kicker_favorite_add_to_activity", 0109 0110 actionArgument : { 0111 favoriteModel : favoriteModel, 0112 favoriteId : favoriteId, 0113 favoriteActivity : activityId 0114 } 0115 }); 0116 }; 0117 0118 // Adding the item to link/unlink to the current activity 0119 0120 addActivityItem(favoriteModel.activities.currentActivity, i18n("On the Current Activity")); 0121 0122 actions.push({ 0123 type: "separator", 0124 actionId: "_kicker_favorite_separator" 0125 }); 0126 0127 // Adding the items for each activity 0128 0129 activities.forEach(function(activityId) { 0130 addActivityItem(activityId, favoriteModel.activityNameForId(activityId)); 0131 }); 0132 0133 return [{ 0134 text : i18n("Show in Favorites"), 0135 icon : "favorite", 0136 subActions : actions 0137 }]; 0138 } 0139 } 0140 0141 function triggerAction(model, index, actionId, actionArgument) { 0142 function startsWith(txt, needle) { 0143 return txt.substr(0, needle.length) === needle; 0144 } 0145 0146 if (startsWith(actionId, "_kicker_favorite_")) { 0147 handleFavoriteAction(actionId, actionArgument); 0148 return; 0149 } 0150 0151 var closeRequested = model.trigger(index, actionId, actionArgument); 0152 0153 if (closeRequested) { 0154 return true; 0155 } 0156 0157 return false; 0158 } 0159 0160 function handleFavoriteAction(actionId, actionArgument) { 0161 var favoriteId = actionArgument.favoriteId; 0162 var favoriteModel = actionArgument.favoriteModel; 0163 0164 if (favoriteModel === null || favoriteId === null) { 0165 return null; 0166 } 0167 0168 if (actionId === "_kicker_favorite_remove") { 0169 favoriteModel.removeFavorite(favoriteId); 0170 } else if (actionId === "_kicker_favorite_add") { 0171 favoriteModel.addFavorite(favoriteId); 0172 } else if (actionId === "_kicker_favorite_remove_from_activity") { 0173 favoriteModel.removeFavoriteFrom(favoriteId, actionArgument.favoriteActivity); 0174 } else if (actionId === "_kicker_favorite_add_to_activity") { 0175 favoriteModel.addFavoriteTo(favoriteId, actionArgument.favoriteActivity); 0176 } else if (actionId === "_kicker_favorite_set_to_activity") { 0177 favoriteModel.setFavoriteOn(favoriteId, actionArgument.favoriteActivity); 0178 } 0179 }