redoc/lib/services/hash.service.ts

33 lines
757 B
TypeScript
Raw Normal View History

2016-05-07 10:54:44 +03:00
'use strict';
import { Injectable, EventEmitter, Output } from '@angular/core';
2016-08-28 21:46:10 +03:00
import { BrowserDomAdapter as DOM } from '../utils/browser-adapter';
2016-05-07 10:54:44 +03:00
import { global } from '@angular/core/src/facade/lang';
2016-05-09 22:55:16 +03:00
import { RedocEventsService } from './events.service';
2016-05-07 10:54:44 +03:00
@Injectable()
export class Hash {
@Output() changed = new EventEmitter();
private _cancel: any;
2016-08-28 21:46:10 +03:00
constructor(private events:RedocEventsService) {
2016-05-07 10:54:44 +03:00
this.bind();
events.bootstrapped.subscribe(() => this.changed.next(this.hash));
}
get hash() {
2016-08-28 21:46:10 +03:00
return DOM.getLocation().hash;
2016-05-07 10:54:44 +03:00
}
bind() {
2016-08-28 21:46:10 +03:00
this._cancel = DOM.onAndCancel(global, 'hashchange', (evt) => {
2016-05-07 10:54:44 +03:00
this.changed.next(this.hash);
evt.preventDefault();
});
}
unbind() {
this._cancel();
}
}