From 10ae77848ae3dfc571adaffca39428b8bc7be707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ragnar=20=C3=9E=C3=B3r=20Valgeirsson?= Date: Tue, 27 Nov 2018 15:15:35 +0000 Subject: [PATCH] Add [data-href-to-replace] attr to use replaceWith --- addon/href-to.js | 13 ++++++++++++- tests/dummy/app/templates/application.hbs | 1 + tests/unit/href-to-test.js | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/addon/href-to.js b/addon/href-to.js index e4e309b..8042734 100644 --- a/addon/href-to.js +++ b/addon/href-to.js @@ -28,7 +28,14 @@ export default class { handle() { let router = this._getRouter(); - router.transitionTo(this.getUrlWithoutRoot()); + let url = this.getUrlWithoutRoot(); + + if (this.isNotReplaceWithLink()) { + router.transitionTo(url); + } else { + router.replaceWith(url); + } + this.event.preventDefault(); } @@ -43,6 +50,10 @@ export default class { return !attr || attr.value !== '_blank'; } + isNotReplaceWithLink() { + return !this.target.attributes["data-href-to-replace"]; + } + isNotIgnored() { return !this.target.attributes['data-href-to-ignore']; } diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index fd0b6d8..d51b386 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -17,6 +17,7 @@ [About] [First Page] [Second Page] + [Second Page (with replaceWith)] [Second Page (with dynamic params)] [Second Page (with inner span)] [An anchor with no href] diff --git a/tests/unit/href-to-test.js b/tests/unit/href-to-test.js index 2d386fd..c72cbd9 100644 --- a/tests/unit/href-to-test.js +++ b/tests/unit/href-to-test.js @@ -41,6 +41,20 @@ test('#isUnmodifiedLeftClick should be false for right clicks', function(assert) assert.notOk(hrefTo.isUnmodifiedLeftClick()); }); +test('#isNotReplaceWithLink should be false if [data-href-to-replace] is present', function(assert) { + let event = getClickEventOnEl(""); + let hrefTo = createHrefToForEvent(event); + + assert.notOk(hrefTo.isNotReplaceWithLink()); +}); + +test('#isNotReplaceWithLink should be true if [data-href-to-replace] is not present', function(assert) { + let event = getClickEventOnEl(""); + let hrefTo = createHrefToForEvent(event); + + assert.ok(hrefTo.isNotReplaceWithLink()); +}); + test('#isNotIgnored should be false if [data-href-to-ignore] is present', function(assert) { let event = getClickEventOnEl(""); let hrefTo = createHrefToForEvent(event);