-
Notifications
You must be signed in to change notification settings - Fork 46
/
nf-scroll-area.js
180 lines (158 loc) · 4.81 KB
/
nf-scroll-area.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import Ember from 'ember';
import ScrollAreaActionContext from 'ember-nf-graph/utils/nf/scroll-area-action-context';
/**
A component that emits actions and events when scrolled or resized.
@namespace components
@class {nf-scroll-area}
*/
export default Ember.Component.extend({
/**
The tag name of the component
@property tagName
@type {String}
@default 'div'
*/
tagName: 'div',
classNames: ['nf-scroll-area'],
/**
The name of the action to fire when scrolled
@property scrollAction
@type {String}
@default null
*/
scrollAction: null,
/**
The name of the action to fire when resized
@property resizeAction
@type {String}
@default null
*/
resizeAction: null,
/**
The name of the action to fire when scrolled *OR* resized
@property changeAction
@type {String}
@default null
*/
changeAction: null,
/**
The name of the action to fire when the list of child DOM nodes changes
@property childrenChangedAction
@type {String}
@default null
*/
childrenChangedAction: null,
/**
Gets or sets the scrollTop by percentage (decimal) of
scrollHeight
@property scrollTopPercentage
@type {Number}
*/
scrollTopPercentage: Ember.computed(function(key, value) {
if(arguments.length > 1) {
this._scrollTopPercentage = value;
}
return this._scrollTopPercentage;
}).volatile(),
_childMutationObserver: null,
_setupChildMutationObserver: Ember.on('didInsertElement', function() {
var handler = Ember.run.bind(this, function(e) {
var context = this.createActionContext(e);
this.sendAction('childrenChangedAction', context);
this.sendAction('changeAction', context);
});
this._childMutationObserver = new MutationObserver(handler);
this._childMutationObserver.observe(this.get('element'), { childList: true, subtree: true });
// trigger initial event
handler();
}),
_teardownChildMutationObserver: Ember.on('willDestroyElement', function(){
if(this._childMutationObserver) {
this._childMutationObserver.disconnect();
}
}),
_updateScrollTop: Ember.on('didInsertElement', Ember.observer('scrollTopPercentage', function(){
var element = this.get('element');
if(element) {
var scrollTop = this.get('scrollTopPercentage') * (element.scrollHeight - this.$().outerHeight());
this.set('scrollTop', scrollTop);
}
})),
/**
Gets or sets the scrollTop of the area
@property scrollTop
@type {Number}
@default 0
*/
scrollTop: Ember.computed(function(key, value){
if(arguments.length > 1) {
this._scrollTop = value;
}
return this._scrollTop;
}).volatile(),
_updateScroll: Ember.on('didInsertElement', Ember.observer('scrollTop', function(){
if(this.get('element')) {
this.$().scrollTop(this.get('scrollTop'));
}
})),
/**
The optional action data to send with the action contextl
@property actionData
@type Any
@default null
*/
actionData: null,
_onScroll: function(e){
var context = this.createActionContext(e);
this._scrollTop = e.scrollTop;
this._scrollTopPercentage = e.scrollTop / e.scrollHeight;
this.trigger('didScroll', context);
this.sendAction('scrollAction', context);
this.sendAction('changeAction', context);
},
_onResize: function(e) {
var context = this.createActionContext(e);
this._scrollTop = e.scrollTop;
this._scrollTopPercentage = e.scrollTop / e.scrollHeight;
this.trigger('didResize', context);
this.sendAction('resizeAction', context);
this.sendAction('changeAction', context);
},
/**
Creates an action context to send with an action
@method createActionContext
@param e {Event} the original event object if there is one
@return {utils.nf.scroll-area-action-context}
*/
createActionContext: function(e){
var elem = this.$();
var context = {
width: elem.width(),
height: elem.height(),
scrollLeft: elem.scrollLeft(),
scrollTop: elem.scrollTop(),
scrollWidth: elem[0].scrollWidth,
scrollHeight: elem[0].scrollHeight,
outerWidth: elem.outerWidth(),
outerHeight: elem.outerHeight()
};
context.data = this.get('actionData');
context.source = this;
context.originalEvent = e;
return ScrollAreaActionContext.create(context);
},
_setupElement: Ember.on('didInsertElement', function() {
var elem = this.get('element');
if(elem) {
elem.addEventListener('scroll', this._onScroll.bind(this));
elem.addEventListener('resize', this._onResize.bind(this));
}
}),
_unsubscribeEvents: Ember.on('willDestroyElement', function(){
var elem = this.get('element');
if(elem) {
elem.removeEventListener('scroll', this._onScroll);
elem.removeEventListener('resize', this._onResize);
}
}),
});