-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
81 lines (59 loc) · 1.5 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
function mediator() {
if(!( this instanceof mediator )) {
var rVal = Object.create(mediator.prototype);
mediator.apply(rVal, arguments);
return rVal;
}
this.items = Array.prototype.slice.call(arguments);
}
mediator.prototype = {
init: function(data, done) {
this.callAll('init', data, done);
},
resize: function(w, h) {
for(var i = 0, len = this.items.length; i < len; i++) {
if(typeof this.items[ i ].resize === 'function') {
this.items[ i ].resize(w, h);
}
}
},
animateIn: function(data, done) {
this.callAll('animateIn', data, done);
},
animateOut: function(data, done) {
this.callAll('animateOut', data, done);
},
destroy: function(data, done) {
this.callAll('destroy', data, done);
},
clear: function(done) {
this.callAll('clear', data, done);
},
callAll: function(func, data, done) {
var numCalled = 0;
var numToCall = 0;
var i;
var len;
this.items.forEach(function(section) {
if(typeof section[ func ] === 'function') {
numToCall++;
}
});
// if there are no functions to call simply just return
if(numToCall === 0) {
done();
} else {
this.items.forEach(function(section) {
if(typeof section[ func ] === 'function') {
section[ func ].call(section, data, onSectionDone);
}
});
}
function onSectionDone() {
if(++numCalled === numToCall) {
done();
}
}
}
};
module.exports = mediator;