Generalize the Javascript menu rendering into an API for templates that only refresh the menu when there are changes. Closes GitLab issue #511. Thanks to Daniel Carrico @daniel1113 for the report.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-09-16 21:48:37 -04:00
parent 654f2a1ad2
commit fafdb538b3
10 changed files with 98 additions and 29 deletions
@@ -8,6 +8,15 @@ class MayanApp {
this.ajaxSpinnerSeletor = '#ajax-spinner';
this.ajaxExecuting = false;
this.ajaxMenusOptions = [
{
app: this,
interval: 5000,
menuSelector: '#main-menu',
url: apiTemplateMainMenuURL,
}
];
this.ajaxMenuHashes = {};
this.window = $(window);
}
@@ -113,25 +122,22 @@ class MayanApp {
}
}
doRefreshMainMenu (options) {
var self = this;
var $mainMenu = $('#main-menu');
var $mainMenuBuffer = $('#main-menu-buffer');
doRefreshAJAXMenu (options) {
$.ajax({
complete: function() {
setTimeout(app.doRefreshMainMenu, options.interval, options);
setTimeout(app.doRefreshAJAXMenu, options.interval, options);
},
success: function(data) {
var $elements = $('.dropdown.open');
if ($elements.length === 0) {
// Don't refresh the HTML if there are open dropdowns
$mainMenuBuffer.html(data);
$mainMenuBuffer.show();
$mainMenu.hide();
$mainMenu.html($mainMenuBuffer.html());
$mainMenu.show();
$mainMenuBuffer.hide();
console.log(data);
var menuHash = options.app.ajaxMenuHashes[data.name];
console.log('menuHash' + menuHash);
if ((menuHash === undefined) || (menuHash !== data.hex_hash)) {
$(options.menuSelector).html(data.html);
if (options.callback !== undefined) {
options.callback();
}
options.app.ajaxMenuHashes[data.name] = data.hex_hash;
}
},
url: options.url,
@@ -217,9 +223,8 @@ class MayanApp {
this.setupItemsSelector();
this.setupNavbarCollapse();
this.setupNewWindowAnchor();
this.doRefreshMainMenu({
interval: 5000,
url: '/main_menu'
$.each(this.ajaxMenusOptions, function(index, value) {
app.doRefreshAJAXMenu(value);
});
partialNavigation.initialize();
}
@@ -36,7 +36,6 @@
<div id="main-menu">
{% include 'appearance/main_menu.html' %}
</div>
<div id="main-menu-buffer" style="display: none;"></div>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
@@ -114,6 +113,7 @@
{# Transfer variable from Django to javascript #}
var initialURL = '{% url home_view %}';
var djangoDEBUG = {% if debug %}true{% else %}false{% endif %};
var apiTemplateMainMenuURL = '{% url "rest_api:template-detail" "main_menu" %}';
</script>
<script src="{% static 'appearance/js/base.js' %}" type="text/javascript"></script>
<script>