Skip to content

Commit

Permalink
Merge pull request #513 from visionmedia/click
Browse files Browse the repository at this point in the history
Export and document clickHandler
  • Loading branch information
matthewp authored Oct 24, 2018
2 parents 549018b + 72dee89 commit df926c2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ var otherPage = page.create({ window: iframe.contentWindow });
otherPage('/', main);
```
### page.clickHandler
This is the click handler used by page to handle routing when a user clicks an anchor like `<a href="/user/profile">`. This is exported for those who want to disable the click handling behavior with `page.start({ click: false })`, but still might want to dispatch based on the click handler's logic in some scenarios.
### Context
Routes are passed `Context` objects, these may
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
this._hashbang = false;

// bound functions
this._onclick = this._onclick.bind(this);
this.clickHandler = this.clickHandler.bind(this);
this._onpopstate = this._onpopstate.bind(this);

this.configure(options);
Expand Down Expand Up @@ -80,9 +80,9 @@
}

if (this._click) {
_window.document.addEventListener(clickEvent, this._onclick, false);
_window.document.addEventListener(clickEvent, this.clickHandler, false);
} else if(hasDocument) {
_window.document.removeEventListener(clickEvent, this._onclick, false);
_window.document.removeEventListener(clickEvent, this.clickHandler, false);
}

if(this._hashbang && hasWindow && !hasHistory) {
Expand Down Expand Up @@ -185,7 +185,7 @@
this._running = false;

var window = this._window;
hasDocument && window.document.removeEventListener(clickEvent, this._onclick, false);
hasDocument && window.document.removeEventListener(clickEvent, this.clickHandler, false);
hasWindow && window.removeEventListener('popstate', this._onpopstate, false);
hasWindow && window.removeEventListener('hashchange', this._onpopstate, false);
};
Expand Down Expand Up @@ -346,7 +346,7 @@
*/

/* jshint +W054 */
Page.prototype._onclick = function(e) {
Page.prototype.clickHandler = function(e) {
if (1 !== this._which(e)) return;

if (e.metaKey || e.ctrlKey || e.shiftKey) return;
Expand Down Expand Up @@ -551,6 +551,7 @@
pageFn.exit = pageInstance.exit.bind(pageInstance);
pageFn.configure = pageInstance.configure.bind(pageInstance);
pageFn.sameOrigin = pageInstance.sameOrigin.bind(pageInstance);
pageFn.clickHandler = pageInstance.clickHandler.bind(pageInstance);

pageFn.create = createPage;

Expand Down
11 changes: 6 additions & 5 deletions page.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
this._hashbang = false;

// bound functions
this._onclick = this._onclick.bind(this);
this.clickHandler = this.clickHandler.bind(this);
this._onpopstate = this._onpopstate.bind(this);

this.configure(options);
Expand Down Expand Up @@ -480,9 +480,9 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
}

if (this._click) {
_window.document.addEventListener(clickEvent, this._onclick, false);
_window.document.addEventListener(clickEvent, this.clickHandler, false);
} else if(hasDocument) {
_window.document.removeEventListener(clickEvent, this._onclick, false);
_window.document.removeEventListener(clickEvent, this.clickHandler, false);
}

if(this._hashbang && hasWindow && !hasHistory) {
Expand Down Expand Up @@ -585,7 +585,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
this._running = false;

var window = this._window;
hasDocument && window.document.removeEventListener(clickEvent, this._onclick, false);
hasDocument && window.document.removeEventListener(clickEvent, this.clickHandler, false);
hasWindow && window.removeEventListener('popstate', this._onpopstate, false);
hasWindow && window.removeEventListener('hashchange', this._onpopstate, false);
};
Expand Down Expand Up @@ -746,7 +746,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
*/

/* jshint +W054 */
Page.prototype._onclick = function(e) {
Page.prototype.clickHandler = function(e) {
if (1 !== this._which(e)) return;

if (e.metaKey || e.ctrlKey || e.shiftKey) return;
Expand Down Expand Up @@ -951,6 +951,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
pageFn.exit = pageInstance.exit.bind(pageInstance);
pageFn.configure = pageInstance.configure.bind(pageInstance);
pageFn.sameOrigin = pageInstance.sameOrigin.bind(pageInstance);
pageFn.clickHandler = pageInstance.clickHandler.bind(pageInstance);

pageFn.create = createPage;

Expand Down
11 changes: 6 additions & 5 deletions page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
this._hashbang = false;

// bound functions
this._onclick = this._onclick.bind(this);
this.clickHandler = this.clickHandler.bind(this);
this._onpopstate = this._onpopstate.bind(this);

this.configure(options);
Expand Down Expand Up @@ -474,9 +474,9 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
}

if (this._click) {
_window.document.addEventListener(clickEvent, this._onclick, false);
_window.document.addEventListener(clickEvent, this.clickHandler, false);
} else if(hasDocument) {
_window.document.removeEventListener(clickEvent, this._onclick, false);
_window.document.removeEventListener(clickEvent, this.clickHandler, false);
}

if(this._hashbang && hasWindow && !hasHistory) {
Expand Down Expand Up @@ -579,7 +579,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
this._running = false;

var window = this._window;
hasDocument && window.document.removeEventListener(clickEvent, this._onclick, false);
hasDocument && window.document.removeEventListener(clickEvent, this.clickHandler, false);
hasWindow && window.removeEventListener('popstate', this._onpopstate, false);
hasWindow && window.removeEventListener('hashchange', this._onpopstate, false);
};
Expand Down Expand Up @@ -740,7 +740,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
*/

/* jshint +W054 */
Page.prototype._onclick = function(e) {
Page.prototype.clickHandler = function(e) {
if (1 !== this._which(e)) return;

if (e.metaKey || e.ctrlKey || e.shiftKey) return;
Expand Down Expand Up @@ -945,6 +945,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
pageFn.exit = pageInstance.exit.bind(pageInstance);
pageFn.configure = pageInstance.configure.bind(pageInstance);
pageFn.sameOrigin = pageInstance.sameOrigin.bind(pageInstance);
pageFn.clickHandler = pageInstance.clickHandler.bind(pageInstance);

pageFn.create = createPage;

Expand Down
22 changes: 22 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,28 @@
});
});

describe('.clickHandler', function() {
it('is exported by the global page', function() {
expect(typeof page.clickHandler).to.equal('function');
});
});

describe('Environments without the URL constructor', function() {
var URLC;
before(function(done) {
URLC = global.URL;
global.URL = undefined;
beforeTests(null, done);
});

tests();

after(function() {
global.URL = URLC;
afterTests();
});
});

var describei = jsdomSupport ? describe : describe.skip;

describei('File protocol', function() {
Expand Down

0 comments on commit df926c2

Please sign in to comment.