Arthur Bogaart pushed to branch feature/cmng-psp1 at cms / hippo-addon-channel-manager
Commits: 5fa54534 by Arthur Bogaart at 2016-02-09T15:38:19+01:00 CHANNELMGR-437 Open external links in a new tab I ported the existing linkProcessor and hooked it up with the HippoIframeController. Confirmation messages for English and Dutch have been copied as well. - - - - - 5 changed files: - frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.controller.js - frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.js - + frontend-ng/src/angularjs/channel/hippoIframe/link.processor.service.js - frontend-ng/src/i18n/hippo-cm.en.json - frontend-ng/src/i18n/hippo-cm.nl.json Changes: ===================================== frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.controller.js ===================================== --- a/frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.controller.js +++ b/frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.controller.js @@ -14,17 +14,32 @@ * limitations under the License. */ +function constructUrl(location, ...fragments) { + let url = `${location.protocol}//${location.host}`; + fragments.forEach((fragment) => { + if (angular.isString(fragment) && fragment.length) { + const urlAndSlash = url.charAt(url.length - 1) === '/' ? url : `${url}/`; + const noSlashAndFragment = fragment.charAt(0) === '/' ? fragment.substring(1) : fragment; + url = urlAndSlash + noSlashAndFragment; + } + }); + return url; +} + export class HippoIframeCtrl { - constructor(hstCommentsProcessorService, HST_CONSTANT, ChannelService) { + constructor(linkProcessorService, hstCommentsProcessorService, HST_CONSTANT, ChannelService) { 'ngInject'; + this.linkProcessorService = linkProcessorService; this.hstCommentsProcessorService = hstCommentsProcessorService; this.HST = HST_CONSTANT; + this.ChannelService = ChannelService; this.channelService = ChannelService; } onLoad() { this.parseHstComments(); + this.parseLinks(); } parseHstComments() { @@ -45,4 +60,12 @@ export class HippoIframeCtrl { this.hstCommentsProcessorService.run(iframeDom, processHstComment); } + parseLinks() { + const iframeDom = this.iframe.contents()[0]; + const channel = this.ChannelService.channel; + const internalLinkPrefix = constructUrl(iframeDom.location, channel.contextPath, channel.cmsPreviewPrefix); + + this.linkProcessorService.run(iframeDom, internalLinkPrefix); + } + } ===================================== frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.js ===================================== --- a/frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.js +++ b/frontend-ng/src/angularjs/channel/hippoIframe/hippoIframe.js @@ -17,9 +17,11 @@ import { hippoIframe } from './hippoIframe.directive'; import { HippoIframeCtrl } from './hippoIframe.controller'; import { HstCommentsProcessorService } from './hstCommentsProcessor.service'; +import { LinkProcessorService } from './link.processor.service'; export const channelHippoIframeModule = angular .module('hippo-cm.channel.hippoIframe', []) .directive('hippoIframe', hippoIframe) .controller('hippoIframeCtrl', HippoIframeCtrl) - .service('hstCommentsProcessorService', HstCommentsProcessorService); + .service('hstCommentsProcessorService', HstCommentsProcessorService) + .service('linkProcessorService', LinkProcessorService); ===================================== frontend-ng/src/angularjs/channel/hippoIframe/link.processor.service.js ===================================== --- /dev/null +++ b/frontend-ng/src/angularjs/channel/hippoIframe/link.processor.service.js @@ -0,0 +1,53 @@ +/* + * Copyright 2016 Hippo B.V. (http://www.onehippo.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function startsWith(str, prefix) { + if (str === undefined || prefix === undefined) { + return false; + } + return prefix === str.slice(0, prefix.length); +} + +export class LinkProcessorService { + + constructor($translate) { + 'ngInject'; + + this.$translate = $translate; + } + + run(document, internalLinkPrefix) { + angular.element(document).find('a').each((index, el) => { + const link = angular.element(el); + let url = link.prop('href'); + + // handle links within SVG elements + if (url instanceof SVGAnimatedString) { + url = url.baseVal; + } + + // intercept all clicks on external links: open them in a new tab if confirmed by the user + if (!startsWith(url, internalLinkPrefix)) { + link.attr('target', '_blank'); + link.click((event) => { + if (!confirm(this.$translate.instant('CONFIRM_OPEN_EXTERNAL_LINK'))) { + event.preventDefault(); + } + }); + } + }); + } +} ===================================== frontend-ng/src/i18n/hippo-cm.en.json ===================================== --- a/frontend-ng/src/i18n/hippo-cm.en.json +++ b/frontend-ng/src/i18n/hippo-cm.en.json @@ -2,5 +2,6 @@ "TOOLBAR_BUTTON_PAGES": "Pages", "TOOLBAR_SWITCH_VIEWER_MODE": "Viewer mode", "TOOLBAR_SWITCH_VIEWER_MODE_EDIT": "EDIT", - "TOOLBAR_SWITCH_VIEWER_MODE_PREVIEW": "PREVIEW" -} \ No newline at end of file + "TOOLBAR_SWITCH_VIEWER_MODE_PREVIEW": "PREVIEW", + "CONFIRM_OPEN_EXTERNAL_LINK": "This external link will be opened in a new tab.\nDo you want to continue?" +} ===================================== frontend-ng/src/i18n/hippo-cm.nl.json ===================================== --- a/frontend-ng/src/i18n/hippo-cm.nl.json +++ b/frontend-ng/src/i18n/hippo-cm.nl.json @@ -2,5 +2,6 @@ "TOOLBAR_BUTTON_PAGES": "Pagina's", "TOOLBAR_SWITCH_VIEWER_MODE": "Viewer modus", "TOOLBAR_SWITCH_VIEWER_MODE_EDIT": "WIJZIGEN", - "TOOLBAR_SWITCH_VIEWER_MODE_PREVIEW": "KIJKEN" -} \ No newline at end of file + "TOOLBAR_SWITCH_VIEWER_MODE_PREVIEW": "KIJKEN", + "CONFIRM_OPEN_EXTERNAL_LINK": "Deze externe link zal in een nieuwe tab worden geopened.\nWilt u doorgaan?" +} View it on GitLab: https://code.onehippo.org/cms/hippo-addon-channel-manager/commit/5fa545341aac86d33816e1d3e4e3fd89fb0de290
_______________________________________________ Hippocms-svn mailing list Hippocms-svn@lists.onehippo.org https://lists.onehippo.org/mailman/listinfo/hippocms-svn