<?php $columnsLimit = $block->getColumnsLimit() ? : 0; ?>
<?php $_menu2 = $block->getHtml('level-top', 'submenu', $columnsLimit) ?>



<?php $_menustatus=$this->helper('Megnor\Category\Helper\Data')->allowExtension();
      $_menu = $block->getHtml('level-top', 'submenu', $columnsLimit) ?>
<?php $_home=$this->helper('Megnor\Category\Helper\Data')->ishomeEnabled(); 
      $_custom=$this->helper('Megnor\Category\Helper\Data')->iscustomEnabled(); ?>
<?php $_homelink = $this->getUrl('');
$name = $this->helper('Megnor\Category\Helper\Data')->getConfig('megnor_category_menu/menu_settings/title');
$customblock = $this->helper('Megnor\Category\Helper\Data')->getConfig('megnor_category_menu/menu_settings/customblock'); 
$home=__('Home');
?>



<?php if ($block->allowExtension()): ?>
    <script>

        require([
            'jquery',
            'megnormegamenu'
        ], function ($) {


            $('.menu > ul > li:has( > ul)').addClass('menu-dropdown-icon level0');
            //Checks if li has sub (ul) and adds class for toggle icon - just an UI

            $('.menu > ul > li > ul:not(:has(ul))').addClass('normal-sub');
            //Checks if drodown menu's li elements have anothere level (ul), if not the dropdown is shown as regular dropdown, not a mega menu (thanks Luka Kladaric)

            $(".menu > ul").before("<a href=\"#\" class=\"menu-mobile\">Navigation</a>");

            //Adds menu-mobile class (for mobile toggle menu) before the normal menu
            //Mobile menu is hidden if width is more then 959px, but normal menu is displayed
            //Normal menu is hidden if width is below 959px, and jquery adds mobile menu
            //Done this way so it can be used with wordpress without any trouble

            $(".menu > ul > li").hover(function (e) {
                if ($(window).width() > 943) {
                    $(this).children("ul").stop(true, false).fadeToggle(150);
                    e.preventDefault();
                }
            });
            //If width is more than 943px dropdowns are displayed on hover

            $(".menu > ul > li").click(function () {
                if ($(window).width() <= 943) {
                    $(this).children("ul").fadeToggle(150);
                }
            });
            //If width is less or equal to 943px dropdowns are displayed on click (thanks Aman Jain from stackoverflow)

            $(".menu-mobile").click(function (e) {
                $(".menu > ul").toggleClass('show-on-mobile');
                e.preventDefault();
            });

        });

    </script>
    <?php
    /**
     * Top menu for store
     *
     * @see \Magento\Theme\Block\Html\Topmenu
     */
    ?>
    <nav class="navigation <?php if($_menustatus){?>custommenu<?php } ?>" role="navigation" >
       <div class="menu-title">
            <div class="menu-title-inner"> <?php /* @escapeNotVerified */ echo __('Shop by Categories') ?></div>
       </div>
        <div class="menu mainmenu" id="mainmenu">
            <ul>
                <?php /* @escapeNotVerified */  if($_home && $_menustatus)
            echo '<li class="level0 nav-1 first level-top"><a href="'.$_homelink.'" class="level-top"><span>'.$home.'</span></a></li>';

                    echo $_menu2;

            if($_custom && $_menustatus){
            echo '<li class="level0 nav-1 first level-top custom-header-menu"><a href="#" class="level-top"><span>'.$name.'</span></a><div class="level0 customblock_inner newsubmenu">'.$block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($customblock)->toHtml().'</div></li>';
            } 
                ?>
            </ul>
        </div>
        <ul id="mobile" data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
            <?php /* @escapeNotVerified */ echo $_menu; ?>
        </ul>
    </nav>

<?php else : ?>

    <nav class="navigation" role="navigation">
        <ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
        <?php /* @escapeNotVerified */ echo $_menu; ?>
        <?php /* @escapeNotVerified */ echo $block->getChildHtml(); ?>
        </ul>
    </nav>
<?php endif; ?>