Skip to content

Commit 3e59db5

Browse files
author
Anthony Du Pont
committed
✨ Release v1.2.1
1 parent f1acc41 commit 3e59db5

File tree

15 files changed

+81
-64
lines changed

15 files changed

+81
-64
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,33 +248,38 @@ Check out the [**examples**](https://github.com/Dogstudio/highway#examples) for
248248

249249
Last but not least, **Highway** extends [**tiny-emitter**](https://github.com/scottcorgan/tiny-emitter) to send events along the navigation process you can listen to in order to extend its capabilities. There are three events available for you:
250250

251+
- `NAVIGATE_CALL`: Trigger right after a link or an history button of the browser is clicked.
251252
- `NAVIGATE_START`: Trigger when a navigation starts.
252253
- `NAVIGATE_END`: Trigger when a navigation ends.
253254
- `NAVIGATE_IN`: Trigger when the `in` transition starts.
254255
- `NAVIGATE_OUT`: Trigger when the `out` transition starts.
255256
- `NAVIGATE_ERROR`: Trigger when an error occurs in navigation process.
256257

257-
All events except `NAVIGATE_ERROR` give you access to some parameters in this order:
258+
All events except `NAVIGATE_CALL` and `NAVIGATE_ERROR` give you access to some parameters in this order:
258259

259260
- `from`: The renderer of the page you come from.
260261
- `to`: The renderer of the page you go to.
261262
- `state`: The state of **Highway** that contains all the informations about the URL of the page you go to.
262263

263264
```javascript
264265
// [...]
266+
H.on('NAVIGATE_CALL', () => {
267+
// [...]
268+
});
269+
265270
H.on('NAVIGATE_START', (from, to, state) => {
266271
// [...]
267272
});
268273

269-
H.on('NAVIGATE_END', (from, to, state) => {
274+
H.on('NAVIGATE_OUT', (from, to, state) => {
270275
// [...]
271276
});
272277

273278
H.on('NAVIGATE_IN', (from, to, state) => {
274279
// [...]
275280
});
276281

277-
H.on('NAVIGATE_OUT', (from, to, state) => {
282+
H.on('NAVIGATE_END', (from, to, state) => {
278283
// [...]
279284
});
280285

@@ -324,6 +329,12 @@ const H = new Highway.Core({
324329
- [ ] More Examples
325330

326331
## History
332+
#### 1.2.1 (2018-03-24)
333+
334+
- Add `NAVIGATE_CALL` event
335+
- Improve renderers that now update the `html` and `body` classname properly
336+
- Improve documentation
337+
327338
#### 1.2.0 (2018-03-23)
328339

329340
- Add `NAVIGATE_IN` and `NAVIGATE_OUT` events

dist/highway.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ return /******/ (function(modules) { // webpackBootstrap
9090
* @file Highway helper methods used all acrosse the script.
9191
* @author Anthony Du Pont <[email protected]>
9292
*/
93-
var TITLE_REGEX = /<title>(.+)<\/title>/;
9493
var PARAM_REGEX = /\?([\w_\-.=&]+)/;
9594
var ANCHOR_REGEX = /(#.*)$/;
9695
var ORIGIN_REGEX = /(https?:\/\/[\w\-.]+)/;
@@ -193,16 +192,12 @@ function getInfos(url) {
193192
* @return {string} Page DOM
194193
*/
195194
function getDOM(page) {
196-
// We create a fake DOM element that will contain our page HTML and let us
197-
// select DOM nodes properly. This element is only used in Javascript.
198-
var FRAGMENT = document.createElement('div');
199-
200-
// This is the trick to transform our page HTML from string to DOM element by
201-
// using our fake container we created before and by updating its inner HTML.
202-
FRAGMENT.innerHTML = page;
195+
// We create instance of the DOM parser in order to parse our string and
196+
// return the DOM properly.
197+
var parser = new DOMParser();
203198

204199
// Now we can return the DOM.
205-
return FRAGMENT;
200+
return parser.parseFromString(page, 'text/html');
206201
}
207202

208203
/**
@@ -225,17 +220,6 @@ function getSlug(page) {
225220
return getView(page).getAttribute('router-view');
226221
}
227222

228-
/**
229-
* Get page's title from page HTML
230-
*
231-
* @arg {string} page — Page HTML
232-
* @return {string} Page title
233-
*/
234-
function getTitle(page) {
235-
var match = page.match(TITLE_REGEX);
236-
return match ? match[1] : '';
237-
}
238-
239223
/**
240224
* Get page renderer
241225
*
@@ -293,7 +277,6 @@ module.exports = {
293277
getSlug: getSlug,
294278
getView: getView,
295279
getInfos: getInfos,
296-
getTitle: getTitle,
297280
getParam: getParam,
298281
getParams: getParams,
299282
getOrigin: getOrigin,
@@ -341,12 +324,13 @@ var HighwayRenderer = function () {
341324
// the page informations. In our case the content and title of the document.
342325
this.view = view;
343326
this.page = _helpers2.default.getDOM(page);
344-
this.title = _helpers2.default.getTitle(page);
327+
this.title = this.page.title;
345328
this.transition = transition ? new transition(view) : null; // eslint-disable-line
346329

347-
if (this.title && document.title !== this.title) {
348-
document.title = this.title;
349-
}
330+
// We are getting the `html` and `body` tags class attribute value to make
331+
// sure we always have the correct classnames in our DOM.
332+
this.bodyClass = this.page.body.className;
333+
this.HTMLClass = this.page.documentElement.className;
350334

351335
// The [router-wrapper] is the main container of the router and the ancestor of our
352336
// [router-view] that let us now where to remove of append our view in the DOM.
@@ -370,6 +354,22 @@ var HighwayRenderer = function () {
370354
return new Promise(function (resolve) {
371355
_this.wrapper = document.querySelector('[router-wrapper]');
372356

357+
// Now we update all the informations in the DOM we need!
358+
// We update the class attribute on the `html` tag
359+
if (_this.HTMLClass && _this.HTMLClass !== document.documentElement.className) {
360+
document.documentElement.className = _this.HTMLClass;
361+
}
362+
363+
// We update the class attribute on the `body` tag
364+
if (_this.bodyClass && _this.bodyClass !== document.body.className) {
365+
document.body.className = _this.bodyClass;
366+
}
367+
368+
// We update the document title
369+
if (_this.title && document.title !== _this.title) {
370+
document.title = _this.title;
371+
}
372+
373373
// Before doing anything crazy you need to know your view doesn't exists
374374
// in the [router-wrapper] so it is appended to it right now!
375375
_this.wrapper.appendChild(_this.view);
@@ -795,6 +795,10 @@ var HighwayCore = function (_Emitter) {
795795
value: function beforeFetch(url, history) {
796796
var _this3 = this;
797797

798+
// We trigger an event when a link is clicked to let you know do whatever
799+
// you want at this point of the process.
800+
this.emit('NAVIGATE_CALL');
801+
798802
// Use of a boolean to avoid repetitive fetch calls by super excited users
799803
// that could lead to some serious issues.
800804
this.navigating = true;

dist/highway.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/highway.min.js.gz

46 Bytes
Binary file not shown.

examples/basic-css-transition/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dogstudio/highway",
33
"license": "MIT",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "webpack",

examples/basic-google-analytics/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dogstudio/highway",
33
"license": "MIT",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "webpack",

examples/basic-menu-active/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dogstudio/highway",
33
"license": "MIT",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "webpack",

examples/basic-setup/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dogstudio/highway",
33
"license": "MIT",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "webpack",

examples/basic-transition/dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/basic-transition/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dogstudio/highway",
33
"license": "MIT",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "webpack",

0 commit comments

Comments
 (0)