-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathview-support.js
41 lines (36 loc) · 1.28 KB
/
view-support.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Mixin from '@ember/object/mixin';
import { scheduleOnce } from '@ember/runloop';
import { oneWay } from '@ember/object/computed';
import { get } from '@ember/object';
import { injectConfig } from './controller-support';
export default Mixin.create({
_anchorConfig: injectConfig(),
anchorQueryParam: oneWay('_anchorConfig.anchorQueryParam'),
init() {
this._super(...arguments);
let controllerProp = this.get('a') ? 'a' : `controller.${this.get('anchorQueryParam')}`;
this.addObserver(controllerProp, this, this._onQpChanged);
},
_onQpChanged() {
let controllerProp = get(this, 'a') ? 'a' : `controller.${this.get('anchorQueryParam')}`;
let qp = this.get('anchorQueryParam');
let elem = document.querySelector(`[data-${qp}="${this.get(controllerProp)}"]`);
if (!elem) {
return;
}
scheduleOnce('afterRender', this, '_scrollToElemPosition');
},
didInsertElement() {
this._super(...arguments);
this._scrollToElemPosition();
},
_scrollToElemPosition() {
let qp = this.get('anchorQueryParam');
let qpVal = this.get(get(this, 'a') ? 'a' : `controller.${qp}`);
let elem = document.querySelector(`[data-${qp}="${qpVal}"]`);
let offset = elem && elem.offsetTop;
if (offset) {
elem.scrollIntoView();
}
}
});