diff options
author | Steve Beaver <sbeaver@netgate.com> | 2017-08-08 13:41:15 -0400 |
---|---|---|
committer | Steve Beaver <sbeaver@netgate.com> | 2017-08-08 13:42:16 -0400 |
commit | d7c2f4fe6cf9d38a397e17d1f6789e5f692fbac3 (patch) | |
tree | 638c9a18317748d417024c80c075a950e48237f5 /src | |
parent | 71783286d57383b07dddc5052aca3d08c9658738 (diff) | |
download | pfsense-d7c2f4fe6cf9d38a397e17d1f6789e5f692fbac3.zip pfsense-d7c2f4fe6cf9d38a397e17d1f6789e5f692fbac3.tar.gz |
Experimental centralized widget refresh system
Used by the interfaces.widget.php widget only for now
Diffstat (limited to 'src')
-rw-r--r-- | src/usr/local/www/index.php | 57 | ||||
-rw-r--r-- | src/usr/local/www/widgets/widgets/interfaces.widget.php | 44 |
2 files changed, 80 insertions, 21 deletions
diff --git a/src/usr/local/www/index.php b/src/usr/local/www/index.php index 51adbae..c8ea9ce 100644 --- a/src/usr/local/www/index.php +++ b/src/usr/local/www/index.php @@ -121,6 +121,7 @@ foreach (glob("/usr/local/www/widgets/widgets/*.widget.php") as $file) { if (!is_array($config['widgets'])) { $config['widgets'] = array(); } + if (!is_array($user_settings['widgets'])) { $user_settings['widgets'] = array(); } @@ -385,7 +386,7 @@ foreach ($available as $widgetkey => $widgetconfig): ?> <div class="col-sm-3"><a href="#" id="btnadd-<?=$widgetconfig['basename']?>"><i class="fa fa-plus"></i> <?=$widgetconfig['title']?></a></div> <?php endif; ?> -<?php +<?php endforeach; ?> </div> @@ -566,8 +567,19 @@ function set_widget_checkbox_events(checkbox_panel_ref, all_none_button_id) { }); } -events.push(function() { +// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------ +// These need to live outsie of the events.push() function to enable the widgets to see them +var ajaxspecs = new Array(); // Array to hold each widget refresh specification (object) +var ajaxidx = 0; +var ajaxmutex = false; + +// Add a widget refresh object to the array list +function register_ajax(ws) { + ajaxspecs.push(ws); +} +// --------------------------------------------------------------------------------------------------- +events.push(function() { // Make panels destroyable $('.container .panel-heading a[data-toggle="close"]').each(function (idx, el) { $(el).on('click', function(e) { @@ -622,9 +634,50 @@ events.push(function() { $('#btnstore').removeClass("invisible"); } }); + + // --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------ + function make_ajax_call(wd) { + ajaxmutex = true; + + $.ajax({ + type: 'POST', + url: wd.url, + dataType: 'html', + data: wd.parms, + + success: function(data){ + wd.callback(data); + ajaxmutex = false; + }, + + error: function(e){ + alert("Error: " + e) + ajaxmutex = false; + } + }); + } + + // Loop through each AJAX widget refresh object, make the AJAX call and pass the + //results back to the widget's callback function + function executewidget() { + if (ajaxspecs.length > 0) { + if (!ajaxmutex) { + make_ajax_call(ajaxspecs[ajaxidx]); + ajaxidx = ++ajaxidx % ajaxspecs.length; + } + + setTimeout(function() { executewidget(); }, 2000); + } + } + + // Kick it off + executewidget(); + + //---------------------------------------------------------------------------------------------------- }); //]]> </script> + <?php //build list of javascript include files foreach (glob('widgets/javascript/*.js') as $file) { diff --git a/src/usr/local/www/widgets/widgets/interfaces.widget.php b/src/usr/local/www/widgets/widgets/interfaces.widget.php index 13514e1..60d1240 100644 --- a/src/usr/local/www/widgets/widgets/interfaces.widget.php +++ b/src/usr/local/www/widgets/widgets/interfaces.widget.php @@ -208,28 +208,34 @@ if ($_REQUEST['widgetkey']) { <script type="text/javascript"> //<![CDATA[ -function getstatus_ifaces_<?=$widgetkey_nodash?>() { - $.ajax({ - type: 'get', - url: '/widgets/widgets/interfaces.widget.php', - dataType: 'html', - data: { widgetkey: "<?=$widgetkey?>" }, - dataFilter: function(raw){ - // We reload the entire widget, strip this block of javascript from it - return raw.replace(/<script>([\s\S]*)<\/script>/gi, ''); - }, - success: function(data){ - $('#ifaces_status_<?=$widgetkey?>').html(data); - }, - error: function(){ - $('#ifaces_status_<?=$widgetkey?>').html("<div class=\"alert alert-danger\"><?=gettext('Unable to retrieve status'); ?></div>"); - } - }); -} events.push(function(){ + + /// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------ + + // Callback function called by refresh system when data is retrieved + function interfaces_callback(s) { + $('#ifaces_status_<?=$widgetkey?>').html(s); + } + + // POST data to send via AJAX + var postdata = '{"widgetkey":"<?=$widgetkey?>"}'; + + // Create aN object defining widget refresh AJAX call + var interfacesObject = new Object(); + + interfacesObject.name = "Interfaces"; + interfacesObject.url = "/widgets/widgets/interfaces.widget.php"; + interfacesObject.callback = interfaces_callback; + interfacesObject.parms = postdata; + + // Register the AJAX object + register_ajax(interfacesObject); + + // --------------------------------------------------------------------------------------------------- + set_widget_checkbox_events("#<?=$widget_panel_footer_id?> [id^=show]", "<?=$widget_showallnone_id?>"); - setInterval('getstatus_ifaces_<?=$widgetkey_nodash?>()', "<?=$widgetperiod?>"); + }); //]]> </script> |