1
- // Ionic Dev Server: Client Side Logger
2
-
3
1
window . IonicDevServer = {
4
-
5
2
start : function ( ) {
6
- IonicDevServer . msgQueue = [ ] ;
3
+ this . msgQueue = [ ] ;
4
+
5
+ this . consoleLog = console . log ;
6
+ this . consoleError = console . error ;
7
+ this . consoleWarn = console . warn ;
8
+
9
+ if ( IonicDevServerConfig && IonicDevServerConfig . sendConsoleLogs ) {
10
+ this . patchConsole ( ) ;
11
+ }
12
+
13
+ console . log ( 'dev server enabled' ) ;
7
14
8
- IonicDevServer . consoleLog = console . log ;
9
- IonicDevServer . consoleError = console . error ;
15
+ this . openConnection ( ) ;
10
16
11
- if ( IonicDevServerConfig . sendConsoleLogs ) {
12
- IonicDevServer . patchConsole ( ) ;
17
+ this . bindKeyboardEvents ( ) ;
18
+ } ,
19
+
20
+ handleError : function ( err ) {
21
+ var self = this ;
22
+
23
+ console . error ( 'Handling error' , err ) ;
24
+
25
+ var existing = document . querySelector ( '._ionic-error-view' ) ;
26
+ if ( existing ) {
27
+ document . body . removeChild ( existing ) ;
13
28
}
14
29
15
- IonicDevServer . openConnection ( ) ;
30
+ this . _errorWindow = this . _makeErrorWindow ( err ) ;
31
+ document . body . appendChild ( this . _errorWindow ) ;
32
+
33
+ setTimeout ( function ( ) {
34
+ window . requestAnimationFrame ( function ( ) {
35
+ self . _errorWindow . classList . add ( 'show' ) ;
36
+ } ) ;
37
+ } , 500 ) ;
38
+
39
+ } ,
40
+
41
+ _closeErrorWindow : function ( ) {
42
+ var self = this ;
43
+ window . requestAnimationFrame ( function ( ) {
44
+ self . _errorWindow . classList . remove ( 'show' ) ;
45
+ setTimeout ( function ( ) {
46
+ document . body . removeChild ( self . _errorWindow ) ;
47
+ self . _errorWindow = null ;
48
+ } , 500 ) ;
49
+ } ) ;
50
+ } ,
51
+
52
+ _makeErrorWindow : function ( err ) {
53
+ var self = this ;
54
+
55
+ var isInCordova = ! ! window . cordova ;
56
+
57
+ var d = document . createElement ( 'div' ) ;
58
+ d . className = '_ionic-error-view' ;
59
+
60
+ if ( isInCordova ) {
61
+ d . classList . add ( '_ionic-error-in-cordova' ) ;
62
+ }
63
+
64
+ d . innerHTML = '<div style="position: relative"><div class="_ionic-error-navbar"><h1 class="_title">App Runtime Error</h1><div class="_close">Close</div></div><div class="_ionic-error-content"><div class="message">' + err . message + '</div><h4>Stacktrace</h4><textarea class="stack">' + err . stack + '</textarea><!--<div class="_button" action="copy">Copy</div>-->' + this . _makeErrorButtonsHtml ( ) + '</div></div>' ;
65
+
66
+ d . querySelector ( '._close' ) . addEventListener ( 'click' , function ( e ) {
67
+ closeWindow ( d ) ;
68
+ } ) ;
69
+ /*
70
+ d.querySelector('[action="copy"]').addEventListener('click', function(e) {
71
+ if(window.IonicDevtools) {
72
+ window.IonicDevtools.copyErrorToClipboard(err);
73
+ }
74
+ });
75
+ */
16
76
77
+ return d ;
78
+ } ,
79
+ _makeErrorButtonsHtml : function ( ) {
80
+ var d = document . createElement ( 'div' ) ;
81
+ d . className = '_ion-error-buttons' ;
82
+
83
+ var b1 = document . createElement ( 'button' ) ;
84
+ b1 . innerHTML = 'Close (ESC)' ;
85
+ b1 . className = '_button' ;
86
+
87
+ var b2 = document . createElement ( 'button' ) ;
88
+ b2 . innerHTML = 'Reload (⌘)' ;
89
+ b2 . className = '_button' ;
90
+
91
+ //d.appendChild(b1);
92
+ //d.appendChild(b2);
93
+
94
+ return d . innerHTML ;
95
+ } ,
96
+ reloadApp : function ( ) {
97
+ if ( window . cordova ) {
98
+ window . location . reload ( true ) ;
99
+ }
100
+ } ,
101
+ showDebugMenu : function ( ) {
102
+ if ( window . IonicDevtools ) {
103
+ window . IonicDevtools . showDebugMenu ( ) ;
104
+ }
105
+ } ,
106
+ bindKeyboardEvents : function ( ) {
107
+ var self = this ;
108
+
109
+ document . addEventListener ( 'keyup' , function ( event ) {
110
+ var key = event . keyCode || event . charCode || 0 ;
111
+
112
+ if ( key == 27 && self . _errorWindow ) {
113
+ self . _closeErrorWindow ( ) ;
114
+ }
115
+ } ) ;
116
+ document . addEventListener ( 'keydown' , function ( event ) {
117
+ var key = event . keyCode || event . charCode || 0 ;
118
+
119
+ // Check for reload command (cmd/ctrl+R)
120
+ if ( key == 82 && ( event . metaKey || event . ctrlKey ) ) {
121
+ self . reloadApp ( ) ;
122
+ }
123
+
124
+ // Check for debugger command (cmd/ctrl+D)
125
+ /*
126
+ if(key == 68 && (event.metaKey || event.ctrlKey)) {
127
+ self.showDebugMenu();
128
+ }
129
+ */
130
+ } ) ;
17
131
} ,
18
132
19
133
openConnection : function ( ) {
20
- IonicDevServer . socket = new WebSocket ( 'ws://' + window . location . hostname + ':' + IonicDevServerConfig . wsPort ) ;
134
+ var self = this ;
135
+ this . socket = new WebSocket ( 'ws://' + window . location . hostname + ':' + IonicDevServerConfig . wsPort ) ;
21
136
22
- IonicDevServer . socket . onopen = function ( ev ) {
23
- IonicDevServer . socketReady = true ;
137
+ this . socket . onopen = function ( ev ) {
138
+ self . socketReady = true ;
24
139
25
- IonicDevServer . socket . onmessage = function ( ev ) {
140
+ self . socket . onmessage = function ( ev ) {
26
141
try {
27
142
var msg = JSON . parse ( ev . data ) ;
28
143
switch ( msg . category ) {
29
144
case 'taskEvent' :
30
- IonicDevServer . receiveTaskEvent ( msg ) ;
145
+ self . receiveTaskEvent ( msg ) ;
31
146
break ;
32
147
}
33
148
} catch ( e ) {
34
- IonicDevServer . consoleError ( 'error receiving ws message' , e ) ;
149
+ self . consoleError ( 'error receiving ws message' , e ) ;
35
150
}
36
151
} ;
37
152
38
- IonicDevServer . socket . onclose = ( ) => {
39
- IonicDevServer . consoleLog ( 'Dev server logger closed' ) ;
153
+ self . socket . onclose = ( ) => {
154
+ self . consoleLog ( 'Dev server logger closed' ) ;
40
155
} ;
41
156
42
- IonicDevServer . drainMessageQueue ( ) ;
157
+ self . drainMessageQueue ( ) ;
43
158
} ;
44
159
} ,
45
160
46
161
queueMessageSend : function ( msg ) {
47
- IonicDevServer . msgQueue . push ( msg ) ;
48
- IonicDevServer . drainMessageQueue ( ) ;
162
+ this . msgQueue . push ( msg ) ;
163
+ this . drainMessageQueue ( ) ;
49
164
} ,
50
165
51
166
drainMessageQueue : function ( ) {
52
- if ( IonicDevServer . socketReady ) {
167
+ if ( this . socketReady ) {
53
168
var msg ;
54
- while ( msg = IonicDevServer . msgQueue . shift ( ) ) {
169
+ while ( msg = this . msgQueue . shift ( ) ) {
55
170
try {
56
- IonicDevServer . socket . send ( JSON . stringify ( msg ) ) ;
57
- } catch ( e ) {
58
- IonicDevServer . consoleError ( 'ws error: ' + e ) ;
171
+ this . socket . send ( JSON . stringify ( msg ) ) ;
172
+ } catch ( e ) {
173
+ if ( e instanceof TypeError ) {
174
+
175
+ } else {
176
+ this . consoleError ( 'ws error: ' + e ) ;
177
+ }
59
178
}
60
179
}
61
180
}
62
181
} ,
63
182
64
183
patchConsole : function ( ) {
184
+ var self = this ;
65
185
function patchConsole ( consoleType ) {
66
186
console [ consoleType ] = ( function ( ) {
67
187
var orgConsole = console [ consoleType ] ;
@@ -76,7 +196,7 @@ window.IonicDevServer = {
76
196
msg . data . push ( arguments [ i ] ) ;
77
197
}
78
198
if ( msg . data . length ) {
79
- IonicDevServer . queueMessageSend ( msg ) ;
199
+ self . queueMessageSend ( msg ) ;
80
200
}
81
201
} ;
82
202
} ) ( ) ;
@@ -88,13 +208,11 @@ window.IonicDevServer = {
88
208
}
89
209
}
90
210
} ,
91
-
92
211
receiveTaskEvent : function ( taskEvent ) {
93
212
if ( taskEvent . data && [ 'bundle' , 'sass' , 'transpile' , 'template' ] . indexOf ( taskEvent . data . scope ) > - 1 ) {
94
- IonicDevServer . consoleLog ( taskEvent . data . msg ) ;
213
+ this . consoleLog ( taskEvent . data . msg ) ;
95
214
}
96
215
}
97
-
98
216
} ;
99
217
100
218
IonicDevServer . start ( ) ;
0 commit comments