From c34ec5ddabb4602add82fdcdf51634f2794e704e Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 14 Apr 2019 01:20:54 -0400 Subject: [PATCH] Update MayanApp to support multiple menus Signed-off-by: Roberto Rosario --- .../static/appearance/js/mayan_app.js | 102 ++++-------------- .../appearance/templates/appearance/root.html | 14 ++- mayan/apps/common/apps.py | 2 +- 3 files changed, 33 insertions(+), 85 deletions(-) diff --git a/mayan/apps/appearance/static/appearance/js/mayan_app.js b/mayan/apps/appearance/static/appearance/js/mayan_app.js index 1c386d949f..ad505332b6 100644 --- a/mayan/apps/appearance/static/appearance/js/mayan_app.js +++ b/mayan/apps/appearance/static/appearance/js/mayan_app.js @@ -1,50 +1,22 @@ 'use strict'; class MayanApp { - constructor (parameters) { + constructor (options) { var self = this; - parameters = parameters || {} + options = options || { + ajaxMenusOptions: [] + } - this.ajaxSpinnerSeletor = '#ajax-spinner'; this.ajaxExecuting = false; - this.ajaxMenusOptions = [ - { - app: this, - interval: 5000, - menuSelector: '#main-menu', - url: apiTemplateMainMenuURL, - } - ]; + this.ajaxMenusOptions = options.ajaxMenusOptions; this.ajaxMenuHashes = {}; + this.ajaxSpinnerSeletor = '#ajax-spinner'; this.window = $(window); } // Class methods and variables - static mayanNotificationBadge (options, data) { - // Callback to add the notifications count inside a badge markup - var notifications = data[options.attributeName]; - - if (notifications > 0) { - // Save the original link text before adding the initial badge markup - if (!options.element.data('mn-saved-text')) { - options.element.data('mn-saved-text', options.element.html()); - } - - options.element.html( - options.element.data('mn-saved-text') + ' ' + notifications + '' - ); - } else { - if (options.element.data('mn-saved-text')) { - // If there is a saved original link text, restore it - options.element.html( - options.element.data('mn-saved-text') - ); - } - } - } - static MultiObjectFormProcess ($form, options) { /* * ajaxForm callback to add the external item checkboxes to the @@ -69,6 +41,17 @@ class MayanApp { } } + static updateNavbarState () { + var uri = new URI(window.location.hash); + var uriFragment = uri.fragment(); + $('.a-main-menu-accordion-link').each(function (index, value) { + if (value.pathname === uriFragment) { + $(this).closest('.collapse').addClass('in').parent().find('.collapsed').removeClass('collapsed').attr('aria-expanded', 'true'); + $(this).parent().addClass('active'); + } + }); + } + static tagSelectionTemplate (tag, container) { var $tag = $( ' ' + tag.text + '' @@ -87,35 +70,6 @@ class MayanApp { // Instance methods - AJAXperiodicWorker (options) { - var app = this; - - $.ajax({ - complete: function() { - if (!options.app) { - // Preserve the app reference between consecutive calls - options.app = app; - } - setTimeout(options.app.AJAXperiodicWorker, options.interval, options); - }, - success: function(data) { - if (options.callback) { - // Conver the callback string to an actual function - var callbackFunction = window; - - $.each(options.callback.split('.'), function (index, value) { - callbackFunction = callbackFunction[value] - }); - - callbackFunction(options, data); - } else { - options.element.text(data[options.attributeName]); - } - }, - url: options.APIURL - }); - } - callbackAJAXSpinnerUpdate () { if (this.ajaxExecuting) { $(this.ajaxSpinnerSeletor).fadeIn(50); @@ -132,10 +86,10 @@ class MayanApp { if ((menuHash === undefined) || (menuHash !== data.hex_hash)) { $(options.menuSelector).html(data.html); + options.app.ajaxMenuHashes[data.name] = data.hex_hash; if (options.callback !== undefined) { options.callback(); } - options.app.ajaxMenuHashes[data.name] = data.hex_hash; } }, url: options.url, @@ -214,7 +168,8 @@ class MayanApp { } initialize () { - this.setupAJAXPeriodicWorkers(); + var self = this; + this.setupAJAXSpinner(); this.setupAutoSubmit(); this.setupFullHeightResizing(); @@ -222,27 +177,12 @@ class MayanApp { this.setupNavbarCollapse(); this.setupNewWindowAnchor(); $.each(this.ajaxMenusOptions, function(index, value) { + value.app = self; app.doRefreshAJAXMenu(value); }); partialNavigation.initialize(); } - setupAJAXPeriodicWorkers () { - var app = this; - - $('a[data-apw-url]').each(function() { - var $this = $(this); - - app.AJAXperiodicWorker({ - attributeName: $this.data('apw-attribute'), - APIURL: $this.data('apw-url'), - callback: $this.data('apw-callback'), - element: $this, - interval: $this.data('apw-interval'), - }); - }); - } - setupAJAXSpinner () { var self = this; diff --git a/mayan/apps/appearance/templates/appearance/root.html b/mayan/apps/appearance/templates/appearance/root.html index b986ffc988..666ad71e07 100644 --- a/mayan/apps/appearance/templates/appearance/root.html +++ b/mayan/apps/appearance/templates/appearance/root.html @@ -33,7 +33,7 @@ {% if appearance_type == 'plain' %} {% block content_plain %}{% endblock %} {% else %} -