|
1 | 1 | /* Update Helper (c) 2008-2009 Tobie Langel |
2 | | - * |
| 2 | + * |
3 | 3 | * Requires Prototype >= 1.6.0 |
4 | | - * |
| 4 | + * |
5 | 5 | * Update Helper is distributable under the same terms as Prototype |
6 | 6 | * (MIT-style license). For details, see the Prototype web site: |
7 | 7 | * http://www.prototypejs.org/ |
8 | | - * |
| 8 | + * |
9 | 9 | *--------------------------------------------------------------------------*/ |
10 | 10 |
|
11 | 11 | var UpdateHelper = Class.create({ |
12 | | - logLevel: 0, |
13 | | - MessageTemplate: new Template('Update Helper: #{message}\n#{stack}'), |
14 | | - Regexp: new RegExp("@" + window.location.protocol + ".*?\\d+\\n", "g"), |
15 | | - |
16 | | - initialize: function(deprecatedMethods) { |
17 | | - var notify = function(message, type) { |
18 | | - this.notify(message, type); |
19 | | - }.bind(this); // Late binding to simplify testing. |
20 | | - |
21 | | - deprecatedMethods.each(function(d) { |
22 | | - var condition = d.condition, |
23 | | - type = d.type || 'info', |
24 | | - message = d.message, |
25 | | - namespace = d.namespace, |
26 | | - method = d.methodName; |
27 | | - |
28 | | - namespace[method] = (namespace[method] || function() {}).wrap(function(proceed) { |
29 | | - var args = $A(arguments).splice(1); |
30 | | - if (!condition || condition.apply(this, args)) notify(message, type); |
31 | | - return proceed.apply(proceed, args); |
32 | | - }); |
33 | | - }); |
34 | | - Element.addMethods(); |
35 | | - }, |
36 | | - |
37 | | - notify: function(message, type) { |
38 | | - switch(type) { |
39 | | - case 'info': |
40 | | - if (this.logLevel > UpdateHelper.Info) return false; |
41 | | - case 'warn': |
42 | | - if (this.logLevel > UpdateHelper.Warn) return false; |
43 | | - default: |
44 | | - if (this.logLevel > UpdateHelper.Error) return false; |
45 | | - } |
46 | | - this.log(this.MessageTemplate.evaluate({ |
47 | | - message: message, |
48 | | - stack: this.getStack() |
49 | | - }), type); |
50 | | - return true; |
51 | | - }, |
52 | | - |
53 | | - getStack: function() { |
54 | | - try { |
55 | | - throw new Error("stack"); |
56 | | - } catch(e) { |
57 | | - var match = (e.stack || '').match(this.Regexp); |
58 | | - if (match) { |
59 | | - return match.reject(function(path) { |
60 | | - return (/(prototype|unittest|update_helper)\.js/).test(path); |
61 | | - }).join("\n"); |
62 | | - } else { return ''; } |
63 | | - } |
64 | | - }, |
65 | | - |
66 | | - log: function(message, type) { |
67 | | - if (type == 'error') console.error(message); |
68 | | - else if (type == 'warn') console.warn(message); |
69 | | - else console.log(message); |
70 | | - } |
| 12 | + logLevel : 0, |
| 13 | + MessageTemplate: new Template('Update Helper: #{message}\n#{stack}'), |
| 14 | + Regexp : new RegExp('@' + window.location.protocol + '.*?\\d+\\n', 'g'), |
| 15 | + |
| 16 | + initialize: function(deprecatedMethods){ |
| 17 | + var notify = function(message, type){ |
| 18 | + this.notify(message, type); |
| 19 | + }.bind(this); // Late binding to simplify testing. |
| 20 | + |
| 21 | + deprecatedMethods.each(function(d){ |
| 22 | + var condition = d.condition, |
| 23 | + type = d.type || 'info', |
| 24 | + message = d.message, |
| 25 | + namespace = d.namespace, |
| 26 | + method = d.methodName; |
| 27 | + |
| 28 | + namespace[method] = (namespace[method] || function(){ |
| 29 | + }).wrap(function(proceed){ |
| 30 | + var args = $A(arguments).splice(1); |
| 31 | + if(!condition || condition.apply(this, args)){ |
| 32 | + notify(message, type); |
| 33 | + } |
| 34 | + return proceed.apply(proceed, args); |
| 35 | + }); |
| 36 | + }); |
| 37 | + Element.addMethods(); |
| 38 | + }, |
| 39 | + |
| 40 | + notify: function(message, type){ |
| 41 | + switch(type){ |
| 42 | + case 'info': |
| 43 | + if(this.logLevel > UpdateHelper.Info){ |
| 44 | + return false; |
| 45 | + } |
| 46 | + case 'warn': |
| 47 | + if(this.logLevel > UpdateHelper.Warn){ |
| 48 | + return false; |
| 49 | + } |
| 50 | + default: |
| 51 | + if(this.logLevel > UpdateHelper.Error){ |
| 52 | + return false; |
| 53 | + } |
| 54 | + } |
| 55 | + this.log(this.MessageTemplate.evaluate({ |
| 56 | + message: message, |
| 57 | + stack : this.getStack(), |
| 58 | + }), type); |
| 59 | + return true; |
| 60 | + }, |
| 61 | + |
| 62 | + getStack: function(){ |
| 63 | + try{ |
| 64 | + throw new Error('stack'); |
| 65 | + } |
| 66 | + catch(e){ |
| 67 | + var match = (e.stack || '').match(this.Regexp); |
| 68 | + if(match){ |
| 69 | + return match.reject(function(path){ |
| 70 | + return (/(prototype|unittest|update_helper)\.js/).test(path); |
| 71 | + }).join('\n'); |
| 72 | + } |
| 73 | + else{ |
| 74 | + return ''; |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + log: function(message, type){ |
| 80 | + if(type == 'error'){ |
| 81 | + console.error(message); |
| 82 | + } |
| 83 | + else if(type == 'warn'){ |
| 84 | + console.warn(message); |
| 85 | + } |
| 86 | + else{ |
| 87 | + console.log(message); |
| 88 | + } |
| 89 | + }, |
71 | 90 | }); |
72 | 91 |
|
73 | 92 | Object.extend(UpdateHelper, { |
74 | | - Info: 0, |
75 | | - Warn: 1, |
76 | | - Error: 2 |
| 93 | + Info : 0, |
| 94 | + Warn : 1, |
| 95 | + Error: 2 |
77 | 96 | }); |
78 | 97 |
|
0 commit comments