Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add [data-href-to-replace] attr to use .replaceWith() #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion addon/href-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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'];
}
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
[<a href="{{href-to 'about'}}">About</a>]
[<a href="{{href-to 'pages.first'}}">First Page</a>]
[<a href="{{href-to 'pages.second'}}">Second Page</a>]
[<a href="{{href-to 'pages.second'}}" data-href-to-replace>Second Page (with replaceWith)</a>]
[<a href="{{href-to params=dynamicParams}}">Second Page (with dynamic params)</a>]
[<a href="{{href-to 'pages.second'}}"><span id="inner-span">Second Page (with inner span)</span></a>]
[<a>An anchor with no href</a>]
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/href-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<a href='' data-href-to-replace>");
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("<a href=''>");
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("<a href='' data-href-to-ignore>");
let hrefTo = createHrefToForEvent(event);
Expand Down