/** * Remember to adjust the var app line in the custom.js file to include customActions: * var app = angular.module('viewCustom', ['angularLoad', 'customActions']); */ /** * Add a Custom "Report a Problem" button to the "Send To" Action with LibWizard Service * Original github code for Primo from Orbis Cascade Alliance; modifications made for Primo VE. * https://github.com/alliance-pcsg/primo-explore-custom-actions * Contributed by Evan Barber (University of Illinois Springfield) and Paxton Luangnikone */ "use strict"; 'use strict'; angular.module('customActions', []); /* eslint-disable max-len */ angular.module('customActions').component('customAction', { bindings: { name: '@', label: '@', icon: '@', iconSet: '@', link: '@', index: '<' }, require: { prmActionCtrl: '^prmActionList' }, controller: ['customActions', function (customActions) { var _this = this; this.$onInit = function () { _this.action = { name: _this.name, label: _this.label, index: _this.index, icon: { icon: _this.icon, iconSet: _this.iconSet, type: 'svg' }, onToggle: customActions.processLinkTemplate(_this.link, _this.prmActionCtrl.item) }; customActions.addAction(_this.action, _this.prmActionCtrl); }; this.$onDestroy = function () { return customActions.removeAction(_this.action, _this.prmActionCtrl); }; }] }); /* eslint-disable max-len */ angular.module('customActions').factory('customActions', function () { return { /** * Adds an action to the actions menu, including its icon. * @param {object} action action object * @param {object} ctrl instance of prmActionCtrl */ addAction: function addAction(action, ctrl) { this.addActionIcon(action, ctrl); if (!this.actionExists(action, ctrl)) { ctrl.actionListService.requiredActionsList.splice(action.index, 0, action.name); ctrl.actionListService.actionsToIndex[action.name] = action.index; ctrl.actionListService.onToggle[action.name] = action.onToggle; ctrl.actionListService.actionsToDisplay.unshift(action.name); } }, /** * Removes an action from the actions menu, including its icon. * @param {object} action action object * @param {object} ctrl instance of prmActionCtrl */ removeAction: function removeAction(action, ctrl) { if (this.actionExists(action, ctrl)) { this.removeActionIcon(action, ctrl); delete ctrl.actionListService.actionsToIndex[action.name]; delete ctrl.actionListService.onToggle[action.name]; var i = ctrl.actionListService.actionsToDisplay.indexOf(action.name); ctrl.actionListService.actionsToDisplay.splice(i, 1); i = ctrl.actionListService.requiredActionsList.indexOf(action.name); ctrl.actionListService.requiredActionsList.splice(i, 1); } }, /** * Registers an action's icon. * Called internally by addAction(). * @param {object} action action object * @param {object} ctrl instance of prmActionCtrl */ addActionIcon: function addActionIcon(action, ctrl) { ctrl.actionLabelNamesMap[action.name] = action.label; ctrl.actionIconNamesMap[action.name] = action.name; ctrl.actionIcons[action.name] = action.icon; }, /** * Deregisters an action's icon. * Called internally by removeAction(). * @param {object} action action object * @param {object} ctrl instance of prmActionCtrl */ removeActionIcon: function removeActionIcon(action, ctrl) { delete ctrl.actionLabelNamesMap[action.name]; delete ctrl.actionIconNamesMap[action.name]; delete ctrl.actionIcons[action.name]; }, /** * Check if an action exists. * Returns true if action is part of actionsToIndex. * @param {object} action action object * @param {object} ctrl instance of prmActionCtrl * @return {bool} */ actionExists: function actionExists(action, ctrl) { return ctrl.actionListService.actionsToIndex.hasOwnProperty(action.name); }, /** * Process a link into a function to call when the action is clicked. * The function will open the processed link in a new tab. * Will replace {pnx.xxx.xxx} expressions with properties from the item. * CUSTOM CODE: added a line of code that will also replace the string '{url}' contained in the link with the current url of the user's browser * @param {string} link the original link string from the html * @param {object} item the item object obtained from the controller * @return {function} function to call when the action is clicked */ processLinkTemplate: function processLinkTemplate(link, item) { var processedLink = link; var pnxProperties = link.match(/\{(pnx\..*?)\}/g) || []; pnxProperties.forEach(function (property) { var value = property.replace(/[{}]/g, '').split('.').reduce(function (o, i) { try { var h = /(.*)(\[\d\])/.exec(i); if (h instanceof Array) { return o[h[1]][h[2].replace(/[^\d]/g, '')]; } return o[i]; } catch (e) { return ''; } }, item); // EDITED CODE: called encodeURIComponent on value to ensure that pnx properties are URL-friendly processedLink = processedLink.replace(property, encodeURIComponent(value)); }); // Add url // CUSTOM CODE: Replaces the string '{url}' contained in the link with the current url of the user's browser; used to get the url of the user's web browser when an error is reported for more specific details processedLink = processedLink.replace("{url}", encodeURIComponent(document.URL)); return function () { return window.open(processedLink, '_blank'); }; } }; }); /** END of Add a Custom "Report a Problem" button to the "Send To" Action with LibWizard Service */ /** Custom record action - Report a Problem TEMPLATE * Paste after var app = ... line in custom.js. * Edit the link URL for your LibWizard Service (or other service). * Edit the label as desired. */ app.component('prmActionListAfter', { /** * template: * name: The internal name of the component; not visible to front-end users * label: The text displayed underneath the icon * index: The 1-dimensional location of the icon in the list of record actions (under [Send to] or the 3-dot options menu) * icon: The name of the icon displayed; the string is in the format 'ic__24px'; icons and their names can be found here: https://fonts.google.com/icons?selected=Material+Icons * icon-set: (**no need to change this**) The set of icons where the current icon is retrieved from * link: The destination of the custom action button when it is clicked. (handled in the function processLinkTemplate) * */ template: ` ` }) /** END of Custom Report a Problem TEMPLATE */