From 7b89dedfc16668fc319113b9e13e10bee35048c9 Mon Sep 17 00:00:00 2001 From: Jeroen Dalsem Date: Mon, 3 Jun 2024 15:25:21 +0200 Subject: [PATCH] changed: updated for Elgg 6 --- .github/workflows/phpunit.yml | 4 +- README.md | 2 +- classes/ColdTrick/TargetBlank/Bootstrap.php | 2 +- classes/ColdTrick/TargetBlank/Javascript.php | 2 +- composer.json | 2 +- composer.lock | 2 +- elgg-plugin.php | 2 +- views/default/js/target_blank/target_blank.js | 39 ------------------- views/default/target_blank/target_blank.mjs | 39 +++++++++++++++++++ 9 files changed, 48 insertions(+), 46 deletions(-) delete mode 100644 views/default/js/target_blank/target_blank.js create mode 100644 views/default/target_blank/target_blank.mjs diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 67fe092..8d99216 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -3,6 +3,8 @@ name: PHPUnit Plugin Tests on: [push, pull_request] jobs: - lint: + phpunit: name: Run PHPUnit test suites uses: ColdTrick/.github/.github/workflows/phpunit.yml@master + with: + elgg_major_version: 6 \ No newline at end of file diff --git a/README.md b/README.md index 133783b..abc3cce 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Target Blank ============ -![Elgg 5.0](https://img.shields.io/badge/Elgg-5.0-green.svg) +![Elgg 6.0](https://img.shields.io/badge/Elgg-6.0-green.svg) ![Lint Checks](https://github.com/ColdTrick/target_blank/actions/workflows/lint.yml/badge.svg?event=push) [![Latest Stable Version](https://poser.pugx.org/coldtrick/target_blank/v/stable.svg)](https://packagist.org/packages/coldtrick/target_blank) [![License](https://poser.pugx.org/coldtrick/target_blank/license.svg)](https://packagist.org/packages/coldtrick/target_blank) diff --git a/classes/ColdTrick/TargetBlank/Bootstrap.php b/classes/ColdTrick/TargetBlank/Bootstrap.php index 5ca8f97..d48f6c9 100644 --- a/classes/ColdTrick/TargetBlank/Bootstrap.php +++ b/classes/ColdTrick/TargetBlank/Bootstrap.php @@ -13,6 +13,6 @@ class Bootstrap extends DefaultPluginBootstrap { * {@inheritDoc} */ public function init() { - elgg_require_js('target_blank/target_blank'); + elgg_import_esm('target_blank/target_blank'); } } diff --git a/classes/ColdTrick/TargetBlank/Javascript.php b/classes/ColdTrick/TargetBlank/Javascript.php index 10b5751..f5e2d73 100644 --- a/classes/ColdTrick/TargetBlank/Javascript.php +++ b/classes/ColdTrick/TargetBlank/Javascript.php @@ -10,7 +10,7 @@ class Javascript { /** * Add the link suffix to the global JS data * - * @param \Elgg\Event $event 'elgg.data', 'site' + * @param \Elgg\Event $event 'elgg.data', 'page' * * @return array */ diff --git a/composer.json b/composer.json index 6d67c55..ae60977 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,6 @@ "issues": "https://github.com/ColdTrick/target_blank/issues" }, "conflict": { - "elgg/elgg": "<5.0" + "elgg/elgg": "<6.0" } } diff --git a/composer.lock b/composer.lock index 8301946..7c55064 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "176fd97ae68cb1d872a088bd0c15997a", + "content-hash": "33fb10f39fba7be84bc6149ac8e668ba", "packages": [], "packages-dev": [], "aliases": [], diff --git a/elgg-plugin.php b/elgg-plugin.php index 9dda1a6..0e43153 100644 --- a/elgg-plugin.php +++ b/elgg-plugin.php @@ -15,7 +15,7 @@ ], ], 'elgg.data' => [ - 'site' => [ + 'page' => [ '\ColdTrick\TargetBlank\Javascript::setLinkSuffix' => [], ], ], diff --git a/views/default/js/target_blank/target_blank.js b/views/default/js/target_blank/target_blank.js deleted file mode 100644 index bb239dd..0000000 --- a/views/default/js/target_blank/target_blank.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * JS to open links in a new window. - */ -define(['jquery', 'elgg'], function($, elgg) { - - var site_url = elgg.get_site_url().substring(4); - if (site_url.substring(0, 1) === 's') { - site_url = site_url.substring(1); - } - - var selector = 'a[href^="http://"]:not([target], [href^="http' + site_url + '"], [href^="https' + site_url + '"]),'; - selector += 'a[href^="https://"]:not([target], [href^="http' + site_url + '"], [href^="https' + site_url + '"])'; - - $(document).on('click', selector, function () { - $(this).attr('target', '_blank'); - }).addClass('target-blank'); - - - var suffix = null; - if (elgg.data.target_blank !== undefined && elgg.data.target_blank.link_suffix !== undefined) { - suffix = elgg.data.target_blank.link_suffix; - } - - if (suffix) { - $(selector).each(function() { - if ($(this).find(' > img, > .elgg-anchor-label > img').length) { - // there is an image in the link - return; - } - - if (!$(this).html().length) { - // there is no content in the anchor - return; - } - - $(this).append('' + suffix + ''); - }); - } -}); diff --git a/views/default/target_blank/target_blank.mjs b/views/default/target_blank/target_blank.mjs new file mode 100644 index 0000000..76b68f9 --- /dev/null +++ b/views/default/target_blank/target_blank.mjs @@ -0,0 +1,39 @@ +/** + * JS to open links in a new window. + */ +import 'jquery'; +import 'elgg'; + +var site_url = elgg.get_site_url().substring(4); +if (site_url.substring(0, 1) === 's') { + site_url = site_url.substring(1); +} + +var selector = 'a[href^="http://"]:not([target], [href^="http' + site_url + '"], [href^="https' + site_url + '"]),'; +selector += 'a[href^="https://"]:not([target], [href^="http' + site_url + '"], [href^="https' + site_url + '"])'; + +$(document).on('click', selector, function () { + $(this).attr('target', '_blank'); +}).addClass('target-blank'); + + +var suffix = null; +if (elgg.data.target_blank !== undefined && elgg.data.target_blank.link_suffix !== undefined) { + suffix = elgg.data.target_blank.link_suffix; +} + +if (suffix) { + $(selector).each(function() { + if ($(this).find(' > img, > .elgg-anchor-label > img').length) { + // there is an image in the link + return; + } + + if (!$(this).html().length) { + // there is no content in the anchor + return; + } + + $(this).append('' + suffix + ''); + }); +}