forked from backbone-input/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone.input.scroll.js
More file actions
111 lines (91 loc) · 2.94 KB
/
backbone.input.scroll.js
File metadata and controls
111 lines (91 loc) · 2.94 KB
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
/* Backbone Input: Scroll
* Source: https://github.com/backbone-input/scroll
*
* Created by Makis Tracend (@tracend)
* (c) by [Makesites.org](http://makesites.org)
*
* Released under the [MIT license](http://makesites.org/licenses/MIT)
*/
(function($, _, Backbone, APP) {
"use strict";
// support for Backbone APP() view if available...
var isAPP = ( typeof APP !== "undefined" && typeof APP.View !== "undefined" );
var View = ( isAPP ) ? APP.View : Backbone.View;
// save existing options/params
var options = (View.prototype.options instanceof Object) ? View.prototype.options : {};
var params = (View.prototype.params instanceof Backbone.Model) ? View.prototype.params : new Backbone.Model();
// defaults
params.set({
scroll: {
top : 0,
height : 0,
max : 0
}
});
var Scroll = View.extend({
options : _.extend({}, options, {
monitorScroll: false
}),
params: params,
events: {
},
initialize : function(){
_.bindAll(this, "_scroll");
if( this.options.monitorScroll ){
$(window).scroll(this._scroll);
//$(window).bind("touchmove", this._scroll); // is this overkill?
}
return View.prototype.initialize.apply(this, arguments);
},
_scroll : function( e ){
// Firefox/IE scroll the html tag instead :P
// could also use: $(document).scrollTop() //returns scroll position from top of document
var scrollTop = $("body").scrollTop() ||$("html").scrollTop() || 0
, scrollHeight = $(document).height()
, maxScroll = scrollHeight - $(this.el).height()
this.params.set({
scroll: {
top : scrollTop,
height : scrollHeight,
middle : scrollTop + ($(window).height()/2),
max : maxScroll
}
});
// update views
this.trigger("scroll");
//console.log("scrollTop", scrollTop);
//console.log("scrollHeight", scrollHeight);
//console.log("middle", this.pos.middle);
//console.log("maxScroll", maxScroll);
}
});
// fallbacks
if( _.isUndefined( Backbone.Input ) ) Backbone.Input = {};
Backbone.Input.Scroll = Scroll;
// Support module loaders
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
// Expose as module.exports in loaders that implement CommonJS module pattern.
module.exports = Scroll;
} else {
// Register as a named AMD module, used in Require.js
if ( typeof define === "function" && define.amd ) {
define( "backbone.input.scroll", [], function () { return Scroll; } );
}
}
// If there is a window object, that at least has a document property
if ( typeof window === "object" && typeof window.document === "object" ) {
if( isAPP ){
// update APP namespace
APP.Input = APP.Input || {};
APP.Input.Scroll = Backbone.Input.Scroll;
// update APP view
APP.View = Scroll;
window.APP = APP;
} else {
// replacing default view
Backbone.View = Scroll;
window.Backbone = Backbone;
//return Backbone;
}
}
})(this.jQuery, this._, this.Backbone, this.APP);