-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (100 loc) · 2.74 KB
/
index.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
var Ractive = require('ractive');
var IScroll = require('iscroll-browserify');
var extend = require('xtend');
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver || null;
/**
* @description
*
* Scrollable (based on iscroll)
*
* Usage:
* ======
*
* 1) Include CSS for .scroll-wrapper and .scroller (see iscroll.css for example)
*
* 2) In your template (all attributes are optional):
*
* <Scroll class="myscroll" id="..." opts='{scrollbars: false}'>
* ..template..
* </Scroll>
*
* 3) Define height and/or width for .myscroll or .scroll-wrapper
* (a class assigned to each <Scroll>)
*
* Changing `opts' attribute causes re-creation of iscroll instance.
*/
var defaultOpts = {
scrollY: true,
mouseWheel: true,
scrollbars: true,
debounceTimeout: 10,
obs: {
childList: true,
subtree: true,
characterData: true,
}
};
var template =
"<div class='scroll-wrapper {{class}}' id='{{id}}' intro='updateScroll'>" +
" <div class='scroller'>" +
" {{yield}}" +
" </div>" +
"</div>";
// Component definition
var Scroll = Ractive.extend({
template: template,
transitions: {
// Use intro transition to determine when
// wrapped template has finished animations
// and to get actual DOM node.
updateScroll: function (t) {
var self = this;
this.node = t.node;
var userOpts = this.get('opts');
this.opts = extend(defaultOpts, userOpts);
setTimeout(function () {
self.s = new IScroll(t.node, self.opts);
t.complete();
}, 0);
}
},
oncomplete: function () {
var self = this;
var update = debounce(function () {
self.s.refresh();
self.opts.onRefresh && self.opts.onRefresh(self.s);
}, self.opts.debounceTimeout);
if(MutationObserver) {
this.obs = new MutationObserver(update);
this.obs.observe(this.node, this.opts.obs);
} else {
console.warn('Scroll: Your browser does not support MutationObserver.')
}
this.on('teardown', function () {
this.s.destroy();
this.obs && this.obs.disconnect();
});
this.observe('opts', function (n) {
var opts = extend(scrollOpts, n);
// Ignore "accidental" updates
if (JSON.stringify(opts) === JSON.stringify(this.opts))
return;
this.opts = opts;
setTimeout(function () {
self.s.destroy();
self.s = new IScroll(self.node, self.opts);
}, 0);
});
}
});
function debounce (fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
Ractive.components.Scroll = Scroll;