File indexing completed on 2024-11-24 05:00:21

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     SPDX-FileCopyrightText: 2022 ivan tkachenko <me@ratijas.tk>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 .pragma library
0011 .import org.kde.plasma.core as PlasmaCore
0012 
0013 const defaultIconName = "start-here-kde-symbolic";
0014 
0015 function iconOrDefault(formFactor, preferredIconName) {
0016     // Vertical panels must have an icon, at least a default one.
0017     return (formFactor === PlasmaCore.Types.Vertical && preferredIconName === "")
0018         ? defaultIconName : preferredIconName;
0019 }
0020 
0021 function createFavoriteActions(i18n, favoriteModel, favoriteId) {
0022     if (!favoriteModel || !favoriteId || !favoriteModel.enabled) {
0023         return null;
0024     }
0025 
0026 
0027     if (favoriteModel.activities === undefined ||
0028         favoriteModel.activities.runningActivities.length <= 1) {
0029         var action = {};
0030 
0031         if (favoriteModel.isFavorite(favoriteId)) {
0032             action.text = i18n("Remove from Favorites");
0033             action.icon = "bookmark-remove";
0034             action.actionId = "_kicker_favorite_remove";
0035         } else if (favoriteModel.maxFavorites === -1 || favoriteModel.count < favoriteModel.maxFavorites) {
0036             action.text = i18n("Add to Favorites");
0037             action.icon = "bookmark-new";
0038             action.actionId = "_kicker_favorite_add";
0039         } else {
0040             return null;
0041         }
0042 
0043         action.actionArgument = { favoriteModel: favoriteModel, favoriteId: favoriteId };
0044 
0045         return [action];
0046 
0047     } else {
0048         var actions = [];
0049 
0050         var linkedActivities = favoriteModel.linkedActivitiesFor(favoriteId);
0051 
0052         var activities = favoriteModel.activities.runningActivities;
0053 
0054         // Adding the item to link/unlink to all activities
0055 
0056         var linkedToAllActivities =
0057             !(linkedActivities.indexOf(":global") === -1);
0058 
0059         actions.push({
0060             text      : i18n("On All Activities"),
0061             checkable : true,
0062 
0063             actionId  : linkedToAllActivities ?
0064                             "_kicker_favorite_remove_from_activity" :
0065                             "_kicker_favorite_set_to_activity",
0066             checked   : linkedToAllActivities,
0067 
0068             actionArgument : {
0069                 favoriteModel: favoriteModel,
0070                 favoriteId: favoriteId,
0071                 favoriteActivity: ""
0072             }
0073         });
0074 
0075 
0076         // Adding items for each activity separately
0077 
0078         var addActivityItem = function(activityId, activityName) {
0079             var linkedToThisActivity =
0080                 !(linkedActivities.indexOf(activityId) === -1);
0081 
0082             actions.push({
0083                 text      : activityName,
0084                 checkable : true,
0085                 checked   : linkedToThisActivity && !linkedToAllActivities,
0086 
0087                 actionId :
0088                     // If we are on all activities, and the user clicks just one
0089                     // specific activity, unlink from everything else
0090                     linkedToAllActivities ? "_kicker_favorite_set_to_activity" :
0091 
0092                     // If we are linked to the current activity, just unlink from
0093                     // that single one
0094                     linkedToThisActivity ? "_kicker_favorite_remove_from_activity" :
0095 
0096                     // Otherwise, link to this activity, but do not unlink from
0097                     // other ones
0098                     "_kicker_favorite_add_to_activity",
0099 
0100                 actionArgument : {
0101                     favoriteModel    : favoriteModel,
0102                     favoriteId       : favoriteId,
0103                     favoriteActivity : activityId
0104                 }
0105             });
0106         };
0107 
0108         // Adding the item to link/unlink to the current activity
0109 
0110         addActivityItem(favoriteModel.activities.currentActivity, i18n("On the Current Activity"));
0111 
0112         actions.push({
0113             type: "separator",
0114             actionId: "_kicker_favorite_separator"
0115         });
0116 
0117         // Adding the items for each activity
0118 
0119         activities.forEach(function(activityId) {
0120             addActivityItem(activityId, favoriteModel.activityNameForId(activityId));
0121         });
0122 
0123         return [{
0124             text       : i18n("Show in Favorites"),
0125             icon       : "favorite",
0126             subActions : actions
0127         }];
0128     }
0129 }
0130 
0131 function triggerAction(model, index, actionId, actionArgument) {
0132     function startsWith(txt, needle) {
0133         return txt.substr(0, needle.length) === needle;
0134     }
0135 
0136     if (startsWith(actionId, "_kicker_favorite_")) {
0137         handleFavoriteAction(actionId, actionArgument);
0138         return;
0139     }
0140 
0141     var closeRequested = model.trigger(index, actionId, actionArgument);
0142 
0143     if (closeRequested) {
0144         return true;
0145     }
0146 
0147     return false;
0148 }
0149 
0150 function handleFavoriteAction(actionId, actionArgument) {
0151     var favoriteId = actionArgument.favoriteId;
0152     var favoriteModel = actionArgument.favoriteModel;
0153 
0154     if (favoriteModel === null || favoriteId === null) {
0155         return null;
0156     }
0157 
0158     if (actionId === "_kicker_favorite_remove") {
0159         favoriteModel.removeFavorite(favoriteId);
0160     } else if (actionId === "_kicker_favorite_add") {
0161         favoriteModel.addFavorite(favoriteId);
0162     } else if (actionId === "_kicker_favorite_remove_from_activity") {
0163         favoriteModel.removeFavoriteFrom(favoriteId, actionArgument.favoriteActivity);
0164     } else if (actionId === "_kicker_favorite_add_to_activity") {
0165         favoriteModel.addFavoriteTo(favoriteId, actionArgument.favoriteActivity);
0166     } else if (actionId === "_kicker_favorite_set_to_activity") {
0167         favoriteModel.setFavoriteOn(favoriteId, actionArgument.favoriteActivity);
0168     }
0169 }