Skip to content

Commit 8a31915

Browse files
[FIX] Handle hash change in url
1 parent 42c02b5 commit 8a31915

File tree

5 files changed

+90
-28
lines changed

5 files changed

+90
-28
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import hashToQuery from 'hash-to-query';
2+
3+
export function initialize(application) {
4+
if (typeof FastBoot === 'undefined') {
5+
window.onhashchange = function() {
6+
let router = application.lookup('service:router');
7+
let redirectToUrl = hashToQuery(window.location.hash, window.location.pathname);
8+
if (redirectToUrl) {
9+
router.transitionTo(redirectToUrl);
10+
}
11+
}
12+
}
13+
}
14+
15+
export default {
16+
name: 'hash-to-query-redirect',
17+
initialize
18+
};

lib/hash-to-query/index.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,8 @@ module.exports = {
88
return true;
99
},
1010

11-
contentFor (type, config) {
12-
if (type === 'body') {
13-
return `
14-
<script type='text/javascript'>
15-
var hash = window.location.hash;
16-
if (hash && hash.length > 1) {
17-
var pathName = window.location.pathname;
18-
if (pathName.indexOf('/methods/') === -1 &&
19-
pathName.indexOf('/properties/') === -1 &&
20-
pathName.indexOf('/events/') === -1) {
21-
var type = hash.slice(1, hash.indexOf('_'));
22-
var name = hash.slice(hash.indexOf('_') + 1, hash.length);
23-
var anchor = '?anchor=' + name + '&show=inherited,protected,private,deprecated';
24-
var newPath = pathName;
25-
if (type === 'method') {
26-
newPath = pathName + '/methods/' + name;
27-
} else if (type === 'property') {
28-
newPath = pathName + '/properties/' + name;
29-
} else if (type === 'event') {
30-
newPath = pathName + '/events/' + name;
31-
}
32-
window.location.href = window.location.origin + newPath + anchor;
33-
}
34-
}
35-
</script>
36-
`
37-
}
38-
return '';
11+
included(app) {
12+
this._super.included.apply(this, arguments);
13+
this.import('vendor/hash-to-query.js');
3914
}
4015
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(() => {
2+
/* globals define */
3+
function hashToQuery(hash, pathName) {
4+
if (hash && hash.length > 1) {
5+
if (pathName.indexOf('/methods/') === -1 &&
6+
pathName.indexOf('/properties/') === -1 &&
7+
pathName.indexOf('/events/') === -1) {
8+
let type = hash.slice(1, hash.indexOf('_'));
9+
let name = hash.slice(hash.indexOf('_') + 1, hash.length);
10+
let anchor = '?anchor=' + name + '&show=inherited,protected,private,deprecated';
11+
let newPath = pathName;
12+
if (type === 'method') {
13+
newPath = pathName + '/methods/' + name;
14+
} else if (type === 'property') {
15+
newPath = pathName + '/properties/' + name;
16+
} else if (type === 'event') {
17+
newPath = pathName + '/events/' + name;
18+
}
19+
return newPath + anchor;
20+
}
21+
}
22+
}
23+
if (typeof FastBoot === 'undefined') {
24+
let redirectToUrl = hashToQuery(window.location.hash, window.location.pathname);
25+
if (redirectToUrl) {
26+
window.location.href = window.location.origin + redirectToUrl;
27+
}
28+
}
29+
define('hash-to-query', function () {
30+
'use strict';
31+
return {
32+
default: hashToQuery
33+
}
34+
});
35+
})()

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"ember-power-select": "^1.9.2",
7373
"ember-resolver": "^4.3.0",
7474
"ember-route-action-helper": "^2.0.5",
75+
"ember-router-service-polyfill": "1.0.1",
7576
"ember-service-worker": "0.6.7",
7677
"ember-service-worker-asset-cache": "0.6.1",
7778
"ember-service-worker-cache-fallback": "0.6.1",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Ember from 'ember';
2+
import { initialize } from 'ember-api-docs/instance-initializers/hash-to-query-redirect';
3+
import { module, test } from 'qunit';
4+
import destroyApp from '../../helpers/destroy-app';
5+
import hashToQuery from 'hash-to-query';
6+
7+
module('Unit | Instance Initializer | hash to query redirect', {
8+
beforeEach() {
9+
Ember.run(() => {
10+
this.application = Ember.Application.create();
11+
this.appInstance = this.application.buildInstance();
12+
});
13+
},
14+
afterEach() {
15+
Ember.run(this.appInstance, 'destroy');
16+
destroyApp(this.application);
17+
}
18+
});
19+
20+
// Replace this with your real tests.
21+
test('it works', function(assert) {
22+
initialize(this.appInstance);
23+
24+
// you would normally confirm the results of the initializer here
25+
assert.ok(true);
26+
});
27+
28+
test('hashToQuery method works', function(assert) {
29+
initialize(this.appInstance);
30+
31+
let redirectToUrl = hashToQuery('method', '/class');
32+
assert.equal(redirectToUrl, '/class?anchor=method&show=inherited,protected,private,deprecated', 'hashToQuery function works');
33+
});

0 commit comments

Comments
 (0)