42
42
* # STATUS
43
43
*
44
44
* Status updates are dispatched as `statusChange` jQuery events. The status
45
- * codes are:
45
+ * is passed as the first parameter and the reason for the change as the second
46
+ * parameter. Currently only the "Inactive" status supports the reason parameter.
47
+ * The status codes are:
46
48
*
47
49
* -1: Error
48
50
* 0: Inactive
49
51
* 1: Connecting to the remote debugger
50
52
* 2: Loading agents
51
53
* 3: Active
52
54
* 4: Out of sync
55
+ *
56
+ * The reason codes are:
57
+ * - null (Unknown reason)
58
+ * - "explicit_close" (LiveDevelopment.close() was called)
59
+ * - "navigated_away" (The browser changed to a location outside of the project)
60
+ * - "detached_target_closed" (The tab or window was closed)
61
+ * - "detached_replaced_with_devtools" (The developer tools were opened in the browser)
53
62
*/
54
63
define ( function LiveDevelopment ( require , exports , module ) {
55
64
"use strict" ;
@@ -133,7 +142,8 @@ define(function LiveDevelopment(require, exports, module) {
133
142
var _liveDocument ; // the document open for live editing.
134
143
var _relatedDocuments ; // CSS and JS documents that are used by the live HTML document
135
144
var _serverProvider ; // current LiveDevServerProvider
136
-
145
+ var _closeReason ; // reason why live preview was closed
146
+
137
147
function _isHtmlFileExt ( ext ) {
138
148
return ( FileUtils . isStaticHtmlFileExt ( ext ) ||
139
149
( ProjectManager . getBaseUrl ( ) && FileUtils . isServerHtmlFileExt ( ext ) ) ) ;
@@ -451,8 +461,14 @@ define(function LiveDevelopment(require, exports, module) {
451
461
* @param {integer } new status
452
462
*/
453
463
function _setStatus ( status ) {
464
+ // Don't send a notification when the status didn't actually change
465
+ if ( status === exports . status ) {
466
+ return ;
467
+ }
468
+
454
469
exports . status = status ;
455
- $ ( exports ) . triggerHandler ( "statusChange" , status ) ;
470
+ var reason = status === STATUS_INACTIVE ? _closeReason : null ;
471
+ $ ( exports ) . triggerHandler ( "statusChange" , [ status , reason ] ) ;
456
472
}
457
473
458
474
/** Triggered by Inspector.error */
@@ -504,13 +520,6 @@ define(function LiveDevelopment(require, exports, module) {
504
520
} ) ;
505
521
}
506
522
507
- /** Triggered by Inspector.detached */
508
- function _onDetached ( event , res ) {
509
- // res.reason, e.g. "replaced_with_devtools", "target_closed", "canceled_by_user"
510
- // Sample list taken from https://chromiumcodereview.appspot.com/10947037/patch/12001/13004
511
- // However, the link refers to the Chrome Extension API, it may not apply 100% to the Inspector API
512
- }
513
-
514
523
// WebInspector Event: Page.frameNavigated
515
524
function _onFrameNavigated ( event , res ) {
516
525
// res = {frame}
@@ -539,6 +548,7 @@ define(function LiveDevelopment(require, exports, module) {
539
548
if ( ! url . match ( baseUrlRegExp ) ) {
540
549
// No longer in site, so terminate live dev, but don't close browser window
541
550
Inspector . disconnect ( ) ;
551
+ _closeReason = "navigated_away" ;
542
552
_setStatus ( STATUS_INACTIVE ) ;
543
553
_serverProvider = null ;
544
554
}
@@ -554,10 +564,22 @@ define(function LiveDevelopment(require, exports, module) {
554
564
_setStatus ( STATUS_INACTIVE ) ;
555
565
}
556
566
567
+ function _onDetached ( event , res ) {
568
+ // If there already is a reason for closing the session, do not overwrite it
569
+ if ( ! _closeReason ) {
570
+ // Get the explanation from res.reason, e.g. "replaced_with_devtools", "target_closed", "canceled_by_user"
571
+ // Examples taken from https://chromiumcodereview.appspot.com/10947037/patch/12001/13004
572
+ // However, the link refers to the Chrome Extension API, it may not apply 100% to the Inspector API
573
+ // Prefix with "detached_" to create a quasi-namespace for Chrome's reasons
574
+ _closeReason = "detached_" + res . reason ;
575
+ }
576
+ }
577
+
557
578
function reconnect ( ) {
558
579
unloadAgents ( ) ;
559
- var promises = loadAgents ( ) ;
580
+
560
581
_setStatus ( STATUS_LOADING_AGENTS ) ;
582
+ var promises = loadAgents ( ) ;
561
583
$ . when . apply ( undefined , promises ) . done ( _onLoad ) . fail ( _onError ) ;
562
584
}
563
585
@@ -569,6 +591,8 @@ define(function LiveDevelopment(require, exports, module) {
569
591
var browserStarted = false ;
570
592
var retryCount = 0 ;
571
593
594
+ _closeReason = null ;
595
+
572
596
function showWrongDocError ( ) {
573
597
Dialogs . showModalDialog (
574
598
Dialogs . DIALOG_ID_ERROR ,
@@ -722,6 +746,8 @@ define(function LiveDevelopment(require, exports, module) {
722
746
* @return {jQuery.Promise } Resolves once the connection is closed
723
747
*/
724
748
function close ( ) {
749
+ _closeReason = "explicit_close" ;
750
+
725
751
var deferred = $ . Deferred ( ) ;
726
752
727
753
/*
@@ -835,7 +861,6 @@ define(function LiveDevelopment(require, exports, module) {
835
861
$ . when . apply ( undefined , promises ) . done ( _onLoad ) . fail ( _onError ) ;
836
862
}
837
863
838
- $ ( Inspector . Inspector ) . on ( "detached.livedev" , _onDetached ) ;
839
864
$ ( Inspector . Page ) . on ( "frameNavigated.livedev" , _onFrameNavigated ) ;
840
865
841
866
waitForInterstitialPageLoad ( )
@@ -960,6 +985,7 @@ define(function LiveDevelopment(require, exports, module) {
960
985
$ ( Inspector ) . on ( "connect" , _onConnect )
961
986
. on ( "disconnect" , _onDisconnect )
962
987
. on ( "error" , _onError ) ;
988
+ $ ( Inspector . Inspector ) . on ( "detached" , _onDetached ) ;
963
989
$ ( DocumentManager ) . on ( "currentDocumentChange" , _onDocumentChange )
964
990
. on ( "documentSaved" , _onDocumentSaved )
965
991
. on ( "dirtyFlagChange" , _onDirtyFlagChange ) ;
0 commit comments