Skip to content

Commit b9116e7

Browse files
committed
ncp-web: faster first load by asynchronous call to is_active()
1 parent 7eecd81 commit b9116e7

File tree

6 files changed

+69
-52
lines changed

6 files changed

+69
-52
lines changed

changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2-
[v0.53.23](https://github.com/nextcloud/nextcloudpi/commit/b7ee6cd) (2018-04-05) ncp-web: force reload CSRF tokens every time
2+
[v0.53.24](https://github.com/nextcloud/nextcloudpi/commit/181603a) (2018-04-05) ncp-web: faster first load by asynchronous call to is_active()
3+
4+
[v0.53.23](https://github.com/nextcloud/nextcloudpi/commit/6fa40c5) (2018-04-05) ncp-web: force reload CSRF tokens every time
35

46
[v0.53.22](https://github.com/nextcloud/nextcloudpi/commit/6f916a1) (2018-04-05) ncp-web: collapse sidebar menu when clicking in new sections
57

ncp-web/activate/JS.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ function errorMsg()
1717
$('#error-box').fill( "Something went wrong. Try refreshing the page" );
1818
}
1919

20+
function launch_nc_passwd()
21+
{
22+
// request
23+
$.request('post', '../ncp-launcher.php', { action: 'launch',
24+
ref : 'nc-passwd',
25+
config: '{ "PASSWORD":"' + $('#ncp-pwd').get('.value') + '",'
26+
+ '"CONFIRM" :"' + $('#ncp-pwd').get('.value') + '"}',
27+
csrf_token: $( '#csrf-token' ).get( '.value' ) }).then(
28+
29+
function success( result )
30+
{
31+
var ret = $.parseJSON( result );
32+
if ( ret.ret == '0' )
33+
{
34+
setTimeout( function(){
35+
$('#loading-gif').hide();
36+
$('#error-box').fill( "ACTIVATION SUCCESSFUL" );
37+
var url = window.location.protocol + '//' + window.location.hostname + ':4443';
38+
if ( !window.open( url, '_blank' ) ) // try to open in a new tab first
39+
window.location.replace( url );
40+
}, 2000 );
41+
} else {
42+
$('#error-box').fill( "nc-passwd error" );
43+
}
44+
} ).error( errorMsg );
45+
}
46+
2047
$(function()
2148
{
2249
// print info page
@@ -64,30 +91,7 @@ $(function()
6491
if ( ret.ret == '0' ) {
6592
if ( ret.token )
6693
$('#csrf-token').set( { value: ret.token } );
67-
68-
// request
69-
$.request('post', '../ncp-launcher.php', { action: 'launch',
70-
ref : 'nc-passwd',
71-
config: '{ "PASSWORD":"' + $('#ncp-pwd').get('.value') + '",'
72-
+ '"CONFIRM" :"' + $('#ncp-pwd').get('.value') + '"}',
73-
csrf_token: $( '#csrf-token' ).get( '.value' ) }).then(
74-
75-
function success( result )
76-
{
77-
var ret = $.parseJSON( result );
78-
if ( ret.ret == '0' )
79-
{
80-
setTimeout( function(){
81-
$('#loading-gif').hide();
82-
$('#error-box').fill( "ACTIVATION SUCCESSFUL" );
83-
var url = window.location.protocol + '//' + window.location.hostname + ':4443';
84-
if ( !window.open( url, '_blank' ) ) // try to open in a new tab first
85-
window.location.replace( url );
86-
}, 2000 );
87-
} else {
88-
$('#error-box').fill( "nc-passwd error" );
89-
}
90-
} ).error( errorMsg );
94+
launch_nc_passwd();
9195
} else {
9296
$('#error-box').fill( "nc-admin error" );
9397
}

ncp-web/index.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
<div id='overlay' class="hidden"></div>
164164
<div id="app-navigation">
165165
<ul id="ncp-options">
166-
<?php echo print_sidebar($l); ?>
166+
<?php echo print_sidebar($l, false); ?>
167167
</ul>
168168
</div>
169169

@@ -237,8 +237,8 @@
237237

238238
<?php
239239
include('csrf.php');
240-
echo '<input type="hidden" id="csrf-token" name="csrf-token" value="' . getCSRFToken() . '"/>';
241-
echo '<input type="hidden" id="csrf-token-dash" name="csrf-token-dash" value="' . getCSRFToken() . '"/>';
240+
echo '<input type="hidden" id="csrf-token" name="csrf-token" value="' . getCSRFToken() . '"/>';
241+
echo '<input type="hidden" id="csrf-token-ui" name="csrf-token-ui" value="' . getCSRFToken() . '"/>';
242242
?>
243243
<script src="minified.js"></script>
244244
<script src="ncp.js"></script>

ncp-web/ncp-launcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
require( "sidebar.php" );
200200
// return JSON
201201
echo '{ "token": "' . getCSRFToken() . '",'; // Get new token
202-
echo ' "output": ' . json_encode( print_sidebar( $l ) ) . ' , ';
202+
echo ' "output": ' . json_encode( print_sidebar( $l, true ) ) . ' , ';
203203
echo ' "ret": "0" }';
204204
}
205205
}

ncp-web/ncp.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,37 @@ function set_sidebar_click_handlers()
105105
});
106106
}
107107

108+
function print_dashboard()
109+
{
110+
$.request('post', 'ncp-launcher.php', { action: 'info',
111+
csrf_token: $( '#csrf-token-ui' ).get( '.value' ) }).then(
112+
113+
function success( result )
114+
{
115+
var ret = $.parseJSON( result );
116+
if ( ret.token )
117+
$('#csrf-token-ui').set( { value: ret.token } );
118+
$('#loading-info-gif').hide();
119+
$('#dashboard-table').ht( ret.table );
120+
$('#dashboard-suggestions').ht( ret.suggestions );
121+
reload_sidebar();
122+
} ).error( errorMsg );
123+
}
124+
108125
function reload_sidebar()
109126
{
110127
// request
111128
$.request('post', 'ncp-launcher.php', { action:'sidebar',
112-
csrf_token: $( '#csrf-token' ).get( '.value' ) }).then(
129+
csrf_token: $( '#csrf-token-ui' ).get( '.value' ) }).then(
113130
function success( result )
114131
{
115132
var ret = $.parseJSON( result );
116133
if ( ret.token )
117-
$('#csrf-token').set( { value: ret.token } );
118-
$('#ncp-options').ht( ret.output );
119-
set_sidebar_click_handlers();
134+
$('#csrf-token-ui').set( { value: ret.token } );
135+
if ( ret.ret && ret.ret == '0' ) {
136+
$('#ncp-options').ht( ret.output );
137+
set_sidebar_click_handlers();
138+
}
120139
}).error( errorMsg );
121140
}
122141

@@ -336,20 +355,6 @@ $(function()
336355
// click to nextcloud button
337356
$('#nextcloud-btn').set( '@href', window.location.protocol + '//' + window.location.hostname );
338357

339-
// load dashboard info
340-
$.request('post', 'ncp-launcher.php', { action: 'info',
341-
csrf_token: $( '#csrf-token-dash' ).get( '.value' ) }).then(
342-
343-
function success( result )
344-
{
345-
var ret = $.parseJSON( result );
346-
if ( ret.token )
347-
$('#csrf-token-dash').set( { value: ret.token } );
348-
$('#loading-info-gif').hide();
349-
$('#dashboard-table').ht( ret.table );
350-
$('#dashboard-suggestions').ht( ret.suggestions );
351-
} ).error( errorMsg );
352-
353358
// dashboard button
354359
$( '#dashboard-btn' ).on('click', function(e)
355360
{
@@ -365,6 +370,9 @@ $(function()
365370
close_menu();
366371
switch_to_section( 'nc-config' );
367372
} );
373+
374+
// load dashboard info
375+
print_dashboard();
368376
} );
369377

370378
// License

ncp-web/sidebar.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// fill options with contents from directory
1212

13-
function print_sidebar( $l /* translations l10n object */ )
13+
function print_sidebar( $l /* translations l10n object */, $ticks /* wether to calculate ticks(slow) */ )
1414
{
1515
$modules_path = '/usr/local/etc/nextcloudpi-config.d/';
1616
$files = array_diff(scandir($modules_path), array('.', '..', 'nc-wifi.sh', 'nc-info.sh', 'l10n'));
@@ -21,10 +21,13 @@ function print_sidebar( $l /* translations l10n object */ )
2121
$txt = file_get_contents($modules_path . $file);
2222

2323
$active = "";
24-
$etc = '/usr/local/etc';
25-
exec("bash -c \"source $etc/library.sh && is_active_script $etc/nextcloudpi-config.d/$script\".sh", $output, $retval);
26-
if ($retval == 0)
27-
$active = "";
24+
if ( $ticks ) {
25+
$etc = '/usr/local/etc';
26+
exec("bash -c \"source $etc/library.sh && is_active_script $etc/nextcloudpi-config.d/$script\".sh", $output, $retval);
27+
if ($retval == 0)
28+
$active = "";
29+
} else if (preg_match('/^ACTIVE_=yes$/m', $txt, $matches))
30+
$active = "";
2831

2932
$ret .= "<li id=\"$script\" class=\"nav-recent\">";
3033
$ret .= "<a href=\"#\"> {$l->__($script, $script)}$active </a>";

0 commit comments

Comments
 (0)