-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (78 loc) · 3.22 KB
/
index.js
File metadata and controls
105 lines (78 loc) · 3.22 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
/*global window:true */
// We rely on bind, make sure it's there
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {
},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
(function (window) {
'use strict';
// TODO: create version for require js
// The Facade encapsulates objectIn according to the description
// The exposed facade is guaranteed to have exactly the functions described in description.
var Facade = function (description, objectIn) {
var facade, mixIn, warn, self;
facade = {};
self = {};
warn = function (message) {
if (window.console && window.console.log) {
console.log(" **************** Warning ****************** ");
console.log(message);
console.log("+---------------------------------------------+");
}
};
mixIn = function (description, objectIn) {
var property, method;
for (property in objectIn) {
if (objectIn.hasOwnProperty(property)) {
// Only apply .bind() to object methods
if (objectIn[property] && objectIn[property].bind) {
self[property] = objectIn[property].bind(self);
} else {
self[property] = objectIn[property];
}
}
}
for (method in description) {
if (description.hasOwnProperty(method)) {
if (!objectIn[method]) {
warn(method + " not implemented for this facade");
}
if ('mixIn' === method) {
warn(method + " is being used as a faced method name. This means you cannot mixIn anymore " +
"facades.")
}
// Must be a function - bind is needed to enable use of methods other than those on the interface
facade[method] = objectIn[method].bind(self);
}
}
return facade;
};
facade.mixIn = mixIn;
facade.mixIn(description, objectIn);
return facade;
};
if (typeof define === "function" && define.amd) {
define( "facade", [], function () { return Facade; } );
} else if (typeof exports == "object") {
module.exports = Facade;
} else if ( typeof window === "object" && typeof window.document === "object" ) {
window.Facade = Facade;
}
}(window));