Skip to content

Commit 9f14289

Browse files
Create shim and move handling to instance initializer
1 parent a99cb2c commit 9f14289

File tree

5 files changed

+80
-32
lines changed

5 files changed

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

lib/hash-to-query/index.js

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

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Ember from 'ember';
2+
import { initialize } from 'ember-api-docs/initializers/hash-to-query-redirect';
3+
import { module, test } from 'qunit';
4+
import destroyApp from '../../helpers/destroy-app';
5+
6+
module('Unit | Initializer | hash to query redirect', {
7+
beforeEach() {
8+
Ember.run(() => {
9+
this.application = Ember.Application.create();
10+
this.application.deferReadiness();
11+
});
12+
},
13+
afterEach() {
14+
destroyApp(this.application);
15+
}
16+
});
17+
18+
// Replace this with your real tests.
19+
test('it works', function(assert) {
20+
initialize(this.application);
21+
22+
// you would normally confirm the results of the initializer here
23+
assert.ok(true);
24+
});

0 commit comments

Comments
 (0)