forked from prototypejs/prototype
/
update_helper.js
78 lines (69 loc) · 2.22 KB
/
update_helper.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
/* Update Helper (c) 2008-2009 Tobie Langel
*
* Requires Prototype >= 1.6.0
*
* Update Helper is distributable under the same terms as Prototype
* (MIT-style license). For details, see the Prototype web site:
* http://www.prototypejs.org/
*
*--------------------------------------------------------------------------*/
var UpdateHelper = Class.create({
logLevel: 0,
MessageTemplate: new Template('Update Helper: #{message}\n#{stack}'),
Regexp: new RegExp("@" + window.location.protocol + ".*?\\d+\\n", "g"),
initialize: function(deprecatedMethods) {
var notify = function(message, type) {
this.notify(message, type);
}.bind(this); // Late binding to simplify testing.
deprecatedMethods.each(function(d) {
var condition = d.condition,
type = d.type || 'info',
message = d.message,
namespace = d.namespace,
method = d.methodName;
namespace[method] = (namespace[method] || function() {}).wrap(function(proceed) {
var args = $A(arguments).splice(1);
if (!condition || condition.apply(this, args)) notify(message, type);
return proceed.apply(proceed, args);
});
});
Element.addMethods();
},
notify: function(message, type) {
switch(type) {
case 'info':
if (this.logLevel > UpdateHelper.Info) return false;
case 'warn':
if (this.logLevel > UpdateHelper.Warn) return false;
default:
if (this.logLevel > UpdateHelper.Error) return false;
}
this.log(this.MessageTemplate.evaluate({
message: message,
stack: this.getStack()
}), type);
return true;
},
getStack: function() {
try {
throw new Error("stack");
} catch(e) {
var match = (e.stack || '').match(this.Regexp);
if (match) {
return match.reject(function(path) {
return (/(prototype|unittest|update_helper)\.js/).test(path);
}).join("\n");
} else { return ''; }
}
},
log: function(message, type) {
if (type == 'error') console.error(message);
else if (type == 'warn') console.warn(message);
else console.log(message);
}
});
Object.extend(UpdateHelper, {
Info: 0,
Warn: 1,
Error: 2
});