-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (154 loc) · 6.2 KB
/
index.js
File metadata and controls
179 lines (154 loc) · 6.2 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
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _forEachRight = require('lodash/forEachRight');
var _forEachRight2 = _interopRequireDefault(_forEachRight);
var _throttle = require('lodash/throttle');
var _throttle2 = _interopRequireDefault(_throttle);
var _debounce = require('lodash/debounce');
var _debounce2 = _interopRequireDefault(_debounce);
var _remove = require('lodash/remove');
var _remove2 = _interopRequireDefault(_remove);
var _merge = require('lodash/merge');
var _merge2 = _interopRequireDefault(_merge);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// Generated by CoffeeScript 2.4.1
/*
Mediator pattern for window event listeners
Example Usage:
mediator = require 'window-event-mediator'
mediator.on 'resize', myCallback
mediator.off 'scroll', mycallback
*/
var WindowEventMediator;
WindowEventMediator = function () {
// Class definition
var WindowEventMediator = function () {
function WindowEventMediator() {
_classCallCheck(this, WindowEventMediator);
/*
Register an event handler with the mediator
event - string - event name ('resize', 'scroll', etc) callback - function
options
throttle [100] - int - milliseconds to throttle
debounce [100] - int - milliseconds to debounce
*/
this.on = this.on.bind(this);
/*
Removes any reference to the event type and callback, regardless of Throttle
or Debounce options.
event - string - event name ('resize', 'scroll', etc)
callback - function
options - target specific throttled/debounced refrences to remove.
*/
this.off = this.off.bind(this);
/*
Create an event container for the window event type and
add the actual listener to the window object
event - string - event name ('resize', 'scroll', etc)
*/
this.set = this.set.bind(this);
/*
Fires all events for a given window event type, padding the native event
object through to the mediated callback. It must be looped through backwards
so that callbacks which are removed during the loop don't break the
iteration.
e - Event - native event object
*/
this.fire = this.fire.bind(this);
}
_createClass(WindowEventMediator, [{
key: 'on',
value: function on(event, callback, options) {
options = (0, _merge2.default)({}, this.defaults, options);
return this.set(event, callback, options);
}
}, {
key: 'off',
value: function off(event, callback, options) {
var key, results;
if (this.handlers[event] == null) {
return;
}
if (options != null) {
options = (0, _merge2.default)({}, this.defaults, options);
key = options.throttle + '-' + options.debounce;
if (this.handlers[event][key] != null) {
(0, _remove2.default)(this.handlers[event][key], function (cbs) {
return cbs.original === callback;
});
if (this.handlers[event][key].length === 0) {
return delete this.handlers[event][key];
}
}
} else {
results = [];
for (key in this.handlers[event]) {
(0, _remove2.default)(this.handlers[event][key], function (cbs) {
return cbs.original === callback;
});
if (this.handlers[event][key].length === 0) {
results.push(delete this.handlers[event][key]);
} else {
results.push(void 0);
}
}
return results;
}
}
}, {
key: 'set',
value: function set(event, callback, options) {
var dispatcher, key, ref;
key = options.throttle + '-' + options.debounce;
if (this.handlers[event] == null) {
// If the event type hasn't been added, create an object to store the
// callbacks and a record of which throttle.debounce listeners have been
// added
this.handlers[event] = {};
}
// Determine which dispatcher to listen to
dispatcher = (ref = options.dispatcher[event]) != null ? ref : options.dispatcher.default;
if (!this.handlers[event].hasOwnProperty(key)) {
this.handlers[event][key] = [];
dispatcher.addEventListener(event, this.fire);
}
// Save the callback references, including the original event for removing later
return this.handlers[event][key].push({
modified: (0, _debounce2.default)((0, _throttle2.default)(callback, options.throttle), options.debounce),
original: callback
});
}
}, {
key: 'fire',
value: function fire(e) {
return (0, _forEachRight2.default)(this.handlers[e.type], function (bag) {
return (0, _forEachRight2.default)(bag, function (cbs) {
cbs.modified(e);
return true;
});
});
}
}]);
return WindowEventMediator;
}();
;
// Stores a record on all event listener types, subscribed callbacks, and
// Throttle/Debounce option keys
WindowEventMediator.prototype.handlers = {};
// Default options
WindowEventMediator.prototype.defaults = {
throttle: 100,
debounce: 0,
dispatcher: {
scroll: window, // Included as an example of how this can be used
default: window
}
};
return WindowEventMediator;
}.call(undefined);
// This operates as a singleton
exports.default = new WindowEventMediator();