Header Menus in Windows Store Apps

Introduction

In this article I describe how to create Windows Store Apps for Header Menu using JavaScript. The header menu is part of the hierarchical navigation pattern. It offers the user a shortcut to navigate to various sections of your app without having to first go back home via the back button, and then drill into another section. The items in the header menu should match the various sections of your hub that the user can drill into, plus a home button at the bottom.

I assume you can create a simple Windows Store App using JavaScript; for more help visit Simple Windows Store Apps using JavaScript.

To start the creation of the apps, add one JavaScript page by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then provide an appropriate name. In the same way, add a HTML page to your project.

object-in-windows-store-apps.jpg

Write the following code in the default.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title></title>

    <link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />

    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>

    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <link rel="stylesheet" href="/css/default.css" />

    <script src="/js/default.js"></script>

</head>

<body role="application" style="background-color: lightgoldenrodyellow">

    <center><div id="rootGrid">

        <div id="content">

            <h1 id="featureLabel"></h1>

            <div id="contentHost"></div>

        </div>

    </div></center>

</body>

</html>

Write the following code in the default.js:
 

(function () {

    "use strict";

    var appTitle = "Flyout control app";

    var pages = [

        { url: "page.html" }

    ];

    function activated(eventObject) {

        if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {

            eventObject.setPromise(WinJS.UI.processAll().then(function () {

                var url = WinJS.Application.sessionState.lastUrl || pages[0].url;

                return WinJS.Navigation.navigate(url);

            }));

        }

    }

    WinJS.Navigation.addEventListener("navigated", function (eventObject) {

        var url = eventObject.detail.location;

        var host = document.getElementById("contentHost");

        host.winControl && host.winControl.unload && host.winControl.unload();

        WinJS.Utilities.empty(host);

        eventObject.detail.setPromise(WinJS.UI.Pages.render(url, host, eventObject.detail.state).then(function () {

            WinJS.Application.sessionState.lastUrl = url;

        }));

    });

    WinJS.Namespace.define("App", {

        appTitle: appTitle,

        pages: pages

    });

    WinJS.Application.addEventListener("activated", activated, false);

    WinJS.Application.start();

})();

Write the following code in the page.html:
 

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <link rel="stylesheet" href="/css/header-menu.css" />

    <script src="/js/script.js"></script>

</head>

<body>

    <div data-win-control="App.pageInput">

    </div>

    <div data-win-control="App.pageOutput">

        <header aria-label="Header content" role="banner">

            <button class="win-backbutton" aria-label="Back">

            </button>

            <div class="titlearea win-type-ellipsis">

                <button class="titlecontainer">

                    <h1>

                        <span class="pagetitle">Music</span>

                        <span class="chevron win-type-x-large">&#xe099</span>

                    </h1>

                </button>

            </div>

        </header>

    </div>

    <div id="headerMenu" data-win-control="WinJS.UI.Menu">

        <button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'collectionMenuItem',label:'Collection'}">

        </button>

        <button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'PunjabiMenuItem',label:'Punjabi Songs'}">

        </button>

        <button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'BhangraMenuItem',label:'Bhagra songs'}">

        </button>

        <hr data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'separator',type:'separator'}" />

        <button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'homeMenuItem',label:'Home'}">

        </button>

    </div>

</body>

</html>

Write the following code in the script.js:
 

(function () {
   
"use strict";
   
var page = WinJS.UI.Pages.define("page.html", {
        ready:
function (element, options) {
            document.querySelector(
".titlearea").addEventListener("click", showHeaderMenu, false);
            document.getElementById(
"collectionMenuItem").addEventListener("click", function () { goToSection("Collection"); }, false);
            document.getElementById(
"PunjabiMenuItem").addEventListener("click", function () { goToSection("PunjabiSongs"); }, false);
            document.getElementById(
"BhangraMenuItem").addEventListener("click", function () { goToSection("BhangraSongs"); }, false);
            document.getElementById(
"homeMenuItem").addEventListener("click", function () { goHome(); }, false);
            WinJS.log && WinJS.log(
"Click or tap the title to show the header menu.", "app", "status");
        }
    });
   
function showHeaderMenu() {
       
var title = document.querySelector("header .titlearea");
       
var menu = document.getElementById("headerMenu").winControl;
        menu.anchor = title;
        menu.placement =
"bottom";
        menu.alignment =
"center";
        menu.show();
    }
   
function goToSection(section) {
        WinJS.log && WinJS.log(
"You are viewing the " + section + " section.", "app", "status");
    }
   
function goHome() {
        WinJS.log && WinJS.log(
"You are home.", "app", "status");
    }
})();

Output:

header-in-windows-store-apps.jpg

Summary

In this article I described how to create a Windows Store App for Header Menu about an Object using JavaScript. I hope this article has helped you to understand this topic. Please share it. If you know more about this, your feedback and constructive contributions are welcome.


Similar Articles