0

I am trying to implement Looper theme to my blazor server-side app, and I have the javascript libraries referenced at the end of the in _Host.cshtml.However some scripts in theme.min.js is not running. Why?

    <script src="/Library/vendor/jquery/jquery.min.js"></script>
    <script src="/Library/vendor/popper.js/umd/popper.min.js"></script>
    <script src="/Library/vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="/Library/vendor/pace-progress/pace.min.js"></script>
    <script src="/Library/vendor/stacked-menu/js/stacked-menu.min.js"></script>
    <script src="/Library/vendor/perfect-scrollbar/perfect-scrollbar.min.js"></script>
    <script src="/Library/javascript/theme.min.js"></script>
    <script src="_framework/blazor.server.js"></script>

My problem is that while

    <div data-toggle="drowndown"></div> works, but hamburger menu toggle 
<button class="hamburger hamburger-squeeze mr-2" type="button" data-toggle="aside-menu" aria-label="toggle aside menu"><span class="hamburger-box"><span class="hamburger-inner"></span></span></button>

does not work. What am I missing here? What am I doing wrong? My theme change script also isn't running. If I step through theme.js and I can see that this script runs when blazor is not active (commenting out blazor.js) but with blazor active, this script does not run.

(line 610) in /library/javascript/theme.js
}, {
    key: "toggleAside",
    value: function toggleAside() {
      var _this4 = this;

      var $trigger = $('[data-toggle="aside"]');
      $trigger.on('click', function () {
        var isShown = $('.app-aside').hasClass('show');
        $trigger.toggleClass('active', !isShown);
        if (isShown) _this4.hideAside();else _this4.showAside();
      });
    }

My educated guess is that theme.js is using something that blazor does not allow? Is anybody experienced enough with Looper theme (or with javascript in general) to know why it wouldn't work? Particularly the hamburger toggle and the theme switching code

    (line 1992 in /library/javascript/theme.js)
var Looper = function () {
  var Looper = new Theme(); // toggle skin thought button

  $('[data-toggle="skin"]').on('click', function (e) {
    e.preventDefault();
    var skin = Looper.skin === 'dark' ? 'default' : 'dark';
    Looper.setSkin(skin); // we need to refresh our page after change the skin

    location.reload();
  }).each(function () {
    var isDarkSkin = Looper.skin === 'dark';
    var $icon = $(this).find('.fa-moon');

    if (isDarkSkin) {
      $icon.addClass('far');
      $icon.removeClass('fas');
    }
  }); // make it global

  return Looper;
}();

This is the website https://worshipground.azurewebsites.net/ You can use the inspector tool to see that blazor has been correctly loaded and all the javascript files are loaded in /library/javascript and /library/vendor/...

    <!DOCTYPE html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- End Required meta tags -->


    <title> Starter Template | Looper - Bootstrap 4 Admin Theme </title>
    <base href="~/" />
    <meta name="theme-color" content="#3063A0">
    <link rel="apple-touch-icon" sizes="144x144" href="Library/apple-touch-icon.png">
    <link rel="shortcut icon" href="Library/favicon.ico">


    <link href="https://fonts.googleapis.com/css?family=Fira+Sans:400,500,600" rel="stylesheet">
    <link rel="stylesheet" href="/Library/vendor/open-iconic/font/css/open-iconic-bootstrap.min.css">
    <link rel="stylesheet" href="/Library/vendor/fontawesome/fontawesome-free/css/all.min.css">
    <link rel="stylesheet" href="/Library/stylesheets/theme.min.css" data-skin="default">
    <link rel="stylesheet" href="/Library/stylesheets/theme-dark.min.css" data-skin="dark">
    <link rel="stylesheet" href="/Library/stylesheets/custom-app.css">
    <link rel="stylesheet" href="/Library/stylesheets/custom.css" data-skin="default">
    <link rel="stylesheet" href="/Library/stylesheets/custom-dark.css" data-skin="dark">
    <script>
        var skin = localStorage.getItem('skin') || 'default';
        var isCompact = JSON.parse(localStorage.getItem('hasCompactMenu'));
        var disabledSkinStylesheet = document.querySelector('link[data-skin]:not([data-skin="' + skin + '"])');
        // Disable unused skin immediately
        disabledSkinStylesheet.setAttribute('rel', '');
        disabledSkinStylesheet.setAttribute('disabled', true);
        // add flag class to html immediately
        if (isCompact == true) document.querySelector('html').classList.add('preparing-compact-menu');
    </script>
   
</head>
<body>
    <component type="typeof(App)" render-mode="ServerPrerendered" />

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss"></a>
    </div>





    <script src="/Library/vendor/jquery/jquery.min.js"></script>
    <script src="/Library/vendor/popper.js/umd/popper.min.js"></script>
    <script src="/Library/vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="/Library/vendor/pace-progress/pace.min.js"></script>
    <script src="/Library/vendor/stacked-menu/js/stacked-menu.min.js"></script>
    <script src="/Library/vendor/perfect-scrollbar/perfect-scrollbar.min.js"></script>
    <script src="/Library/javascript/theme.min.js"></script>
    <script>
        Object.defineProperty(WebSocket, 'OPEN', { value: 1, });
    </script>
    <script src="_framework/blazor.server.js"></script>
</body>
</html>

2 Answers2

0

You may find you have your work cut out in terms of making a JavaScript heavy template work nicely in Blazor.

You will probably need to utilise IJSRuntime

https://learn.microsoft.com/en-us/aspnet/core/blazor/call-javascript-from-dotnet?view=aspnetcore-5.0

DotNetDublin
  • 770
  • 1
  • 9
  • 23
  • What I don't understand is that code in bootstrap.js works fine but why wouldn't the hamburger menu toggle not work? While I understand why you would say "... probably need to utilize IJSRuntime", I don't think it is a good answer because the very scope of the question is figuring out why it sometimes works and sometimes it isn't. – Jimmy Sungmin Park Mar 19 '21 at 16:51
  • Problems occur because of conflicts between what the JS libraries get up to and what `blazor.server.js` does. Your building a half way house. If you have heavy JS DOM manipulation activity within a Blazor rendered component/page, good luck, you'll need it! – MrC aka Shaun Curtis Mar 19 '21 at 17:56
0

I think I know where this problem comes from! If you comment out the following code on the "Host.cshtml" file, this problem will most likely go away!

<script>
    Object.defineProperty(WebSocket, 'OPEN', { value: 1, });
</script>

But!!!! You will get some javascript errors related to "WebSocket" instead! something like this: "WebSocket is not in the OPEN state" Or "Uncaught (in promise) Error: Cannot send data if the connection is not in the 'Connected' State." The cause of these errors is a conflict between "pace.min.js" file and "blazor.server.js" file! You can check this link for more information and get more help