Another attempt to fix the http blog redirects.
[WebKit-https.git] / Websites / webkit.org / blog / wp-includes / js / wp-auth-check.js
1 /* global adminpage */
2 // Interim login dialog
3 (function($){
4         var wrap, next;
5
6         function show() {
7                 var parent = $('#wp-auth-check'),
8                         form = $('#wp-auth-check-form'),
9                         noframe = wrap.find('.wp-auth-fallback-expired'),
10                         frame, loaded = false;
11
12                 if ( form.length ) {
13                         // Add unload confirmation to counter (frame-busting) JS redirects
14                         $(window).on( 'beforeunload.wp-auth-check', function(e) {
15                                 e.originalEvent.returnValue = window.authcheckL10n.beforeunload;
16                         });
17
18                         frame = $('<iframe id="wp-auth-check-frame" frameborder="0">').attr( 'title', noframe.text() );
19                         frame.load( function() {
20                                 var height, body;
21
22                                 loaded = true;
23
24                                 try {
25                                         body = $(this).contents().find('body');
26                                         height = body.height();
27                                 } catch(e) {
28                                         wrap.addClass('fallback');
29                                         parent.css( 'max-height', '' );
30                                         form.remove();
31                                         noframe.focus();
32                                         return;
33                                 }
34
35                                 if ( height ) {
36                                         if ( body && body.hasClass('interim-login-success') )
37                                                 hide();
38                                         else
39                                                 parent.css( 'max-height', height + 40 + 'px' );
40                                 } else if ( ! body || ! body.length ) {
41                                         // Catch "silent" iframe origin exceptions in WebKit after another page is loaded in the iframe
42                                         wrap.addClass('fallback');
43                                         parent.css( 'max-height', '' );
44                                         form.remove();
45                                         noframe.focus();
46                                 }
47                         }).attr( 'src', form.data('src') );
48
49                         $('#wp-auth-check-form').append( frame );
50                 }
51
52                 $( 'body' ).addClass( 'modal-open' );
53                 wrap.removeClass('hidden');
54
55                 if ( frame ) {
56                         frame.focus();
57                         // WebKit doesn't throw an error if the iframe fails to load because of "X-Frame-Options: DENY" header.
58                         // Wait for 10 sec. and switch to the fallback text.
59                         setTimeout( function() {
60                                 if ( ! loaded ) {
61                                         wrap.addClass('fallback');
62                                         form.remove();
63                                         noframe.focus();
64                                 }
65                         }, 10000 );
66                 } else {
67                         noframe.focus();
68                 }
69         }
70
71         function hide() {
72                 $(window).off( 'beforeunload.wp-auth-check' );
73
74                 // When on the Edit Post screen, speed up heartbeat after the user logs in to quickly refresh nonces
75                 if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) &&
76                         typeof wp !== 'undefined' && wp.heartbeat ) {
77
78                         wp.heartbeat.connectNow();
79                 }
80
81                 wrap.fadeOut( 200, function() {
82                         wrap.addClass('hidden').css('display', '');
83                         $('#wp-auth-check-frame').remove();
84                         $( 'body' ).removeClass( 'modal-open' );
85                 });
86         }
87
88         function schedule() {
89                 var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180; // in seconds, default 3 min.
90                 next = ( new Date() ).getTime() + ( interval * 1000 );
91         }
92
93         $( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) {
94                 if ( 'wp-auth-check' in data ) {
95                         schedule();
96                         if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) {
97                                 show();
98                         } else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) {
99                                 hide();
100                         }
101                 }
102         }).on( 'heartbeat-send.wp-auth-check', function( e, data ) {
103                 if ( ( new Date() ).getTime() > next ) {
104                         data['wp-auth-check'] = true;
105                 }
106         }).ready( function() {
107                 schedule();
108                 wrap = $('#wp-auth-check-wrap');
109                 wrap.find('.wp-auth-check-close').on( 'click', function() {
110                         hide();
111                 });
112         });
113
114 }(jQuery));