-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevator.js
164 lines (141 loc) · 4.6 KB
/
elevator.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
(function(){
var Elevators = window.Elevators = window.Elevators || {};
var Elevator = Elevators.Elevator = function($el){
this.$el = $el;
var duration = null;
var mainAudio = true;
var endAudio;
var rideAudio;
}
Elevator.prototype = {
elevate : function (opt, key) {
var option = opt || false;
var keyboard = key || false
if (!elevator.movable(this) || !elevator.movable(keyboard)){
endAudio.play();
return;
}
var $buttons = $(".elevator-button");
var topOrBot = "0px";
if ($(this).hasClass('down') || option === true) {
topOrBot = $(document).height() - $(window).height();
}
var animationComplete = false;
var that = this;
rideAudio.play();
$buttons.attr("disabled", true);
$(that).addClass('moving');
$("html, body").animate({
scrollTop: topOrBot,
easing: "easeInOutQuad",
}, duration, function(){
if (!animationComplete){
animationComplete = true
rideAudio.pause();
endAudio.play();
$(that).removeClass('moving');
$buttons.attr("disabled", false);
}
});
},
appendButtons : function () {
var $elevatorFooter = $('<div/>').addClass("elevator-footer");
var $upButton = $('<button/>').html('▲')
.addClass("elevator-button up");
$upButton.appendTo($elevatorFooter);
var $downButton = $('<button/>').html('▼')
.addClass("elevator-button down");
$downButton.appendTo($elevatorFooter);
$elevatorFooter.appendTo(this.$el);
},
removeButtons : function () {
$(".elevator-button up").remove();
$(".elevator-button down").remove();
$(".elevator-footer").remove();
},
toggleButtons : function () {
if($(".elevator-button").length === 0){
elevator.appendButtons();
elevator.bindElevate();
} else {
elevator.removeButtons();
}
},
bindElevate : function () {
$(".elevator-button").bind('click', this.elevate);
},
setDuration : function () {
// duration = $(document).height() * 3;
duration = 5000;
},
loadEndAudio : function () {
endAudio = new Audio();
endAudio.src = chrome.extension.getURL('audio/bellRing.mp3');
endAudio.load();
endAudio.currentTime = 1;
endAudio.addEventListener("play", function(){
setTimeout(function(){
this.resetAudio();
}.bind(this), 600);
}.bind(this));
rideAudio = new Audio();
rideAudio.src = chrome.extension.getURL('audio/rideAudio.mp3');
rideAudio.load();
rideAudio.currentTime = 48.5;
},
resetAudio : function () {
rideAudio.pause();
rideAudio.currentTime = 48.5;
endAudio.pause();
endAudio.currentTime = 1;
},
movable : function (btn) {
var docHeight = $(window).scrollTop() + $(window).height();
if ((btn === "down" || $(btn).hasClass('down')) && docHeight === $(document).height())
return false;
if ((btn === "up" || $(btn).hasClass('up')) && $(window).scrollTop() === 0)
return false;
return true;
},
bindButtonToggle : function () {
var keys = {};
window.addEventListener('keydown', function(e) {
keys[e.which] = true;
$(document).keydown(function (e) {
keys[e.which] = true;
addOrRemoveButtons().bind(e);
});
$(document).keyup(function (e) {
delete keys[e.which];
});
function addOrRemoveButtons() {
if (keys.hasOwnProperty(17) && keys.hasOwnProperty(16) && keys.hasOwnProperty(66)){
elevator.toggleButtons();
}
if (keys.hasOwnProperty(17) && keys.hasOwnProperty(16) && keys.hasOwnProperty(189)){
//down
this.stop();
elevator.elevate(true, "down")
}
if (keys.hasOwnProperty(17) && keys.hasOwnProperty(16) && keys.hasOwnProperty(187)){
//up
this.stop();
elevator.elevate(false, "up");
}
if (keys.hasOwnProperty(17) && keys.hasOwnProperty(16) && keys.hasOwnProperty(221))
duration -= 1000
if (keys.hasOwnProperty(17) && keys.hasOwnProperty(16) && keys.hasOwnProperty(219))
duration += 1000
}
}, false)
},
main : function (element) {
this.loadEndAudio();
this.setDuration();
this.bindButtonToggle();
},
}
})();
var $el = $("body");
var elevator = new Elevators.Elevator($el);
elevator.main($el);