Introduction
In this article I describe the rotation of images in Windows Store Apps using JavaScript. The FlipView control exposes methods and events to allow developers to create custom controls to give the user an idea of where the current item is and alternative mechanisms for navigating the collection.
The example here shows a set of styled Radio Buttons that are kept in sync with the Flip View. I assume you can create a simple Windows Store Apps using JavaScript; for more help visit Simple Windows Store Apps using JavaScript.
To start the creation of the Apps, add three JavaScript pages by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then give an appropriate name. In the same way, add a HTML page to your project. Now copy the images in the Images folder and right-click on the images folder and select Add > existing Item >select all the images and select Add.
Write the following code in Default.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Apps</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/script2.js"></script>
<script src="/js/default.js"></script>
</head>
<center><body role="application"style="background-color:gray">
<div id="rootGrid">
<div id="content">
<h1 id="featureLabel"></h1>
<div id="contentHost"></div>
</div>
</div>
</body></center>
</html>
Write the following code in Default.js:
(function () {
"use strict";
var sampleTitle = "";
var Pages = [
{ url: "page.html", title: "Creating a Context Control" }
];
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", {
sampleTitle: sampleTitle,
Pages: Pages
});
WinJS.Application.addEventListener("activated", activated, false);
WinJS.Application.start();
})();
Write the following code in page.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/css/contextControl.css" />
<script src="/js/script2.js"></script>
<script src="/js/script1.js"></script>
</head>
<body>
<div data-win-control="App.PageInput">
</div>
<div data-win-control="App.PageOutput">
<div class="contextControlPage">
<div id="contextControl_ItemTemplate" data-win-control="WinJS.Binding.Template" style="display: none">
<div class="overlaidItemTemplate">
<img class="image" data-win-bind="src: picture; alt: title" />
<div class="overlay">
<h2 class="ItemTitle" data-win-bind="innerText: title"></h2>
</div>
</div>
</div>
<div>
<div id="contextControl_FlipView" class="flipView"
data-win-control="WinJS.UI.FlipView"
data-win-options="{ itemDataSource: DefaultData.bindingList.dataSource, itemTemplate: contextControl_ItemTemplate }">
</div>
<div id="ContextContainer"></div>
</div>
</div>
</div>
</body>
</html>
Write the following code in script.js:
(function () {
"use strict";
var myFlipview = null;
var page = WinJS.UI.Pages.define("page.html", {
processed: function (element, options) {
myFlipview = document.getElementById("contextControl_FlipView").winControl;
myFlipview.count().done(countRetrieved);
}
});
function countRetrieved(count) {
var contextControl = document.createElement("div");
contextControl.className = "contextControl";
var isFlipping = false;
function radioButtonClicked(eventObject) {
if (eventObject.propertyName !== "checked" || !eventObject.srcElement.checked) {
return;
}
if (isFlipping) {
var currentPage = myFlipview.currentPage;
radioButtons[currentPage].checked = true;
} else {
var targetPage = eventObject.srcElement.getAttribute("value");
myFlipview.currentPage = parseInt(targetPage);
}
}
myFlipview.addEventListener("pagevisibilitychanged", function (eventObject) {
if (eventObject.detail.visible === true) {
isFlipping = true;
}
}, false);
var radioButtons = [];
for (var i = 0; i < count; ++i) {
var radioButton = document.createElement("input");
radioButton.setAttribute("type", "radio");
radioButton.setAttribute("name", "flipperContextGroup");
radioButton.setAttribute("value", i);
radioButton.setAttribute("aria-label", (i + 1) + " of " + count);
radioButton.onpropertychange = radioButtonClicked;
radioButtons.push(radioButton);
contextControl.appendChild(radioButton);
}
if (count > 0) {
radioButtons[myFlipview.currentPage].checked = true;
}
myFlipview.addEventListener("pageselected", function () {
isFlipping = false;
var currentPage = myFlipview.currentPage;
radioButtons[currentPage].checked = true;
}, false);
var contextContainer = document.getElementById("ContextContainer");
contextContainer.appendChild(contextControl);
}
})();
Write the following code in script1.js:
(function () {
"use strict";
var array = [
{ type: "item", title: "My Bike", picture: "images/1.jpg" },
{ type: "item", title: "My Bike", picture: "images/2.jpg" },
{ type: "item", title: "My Bike", picture: "images/3.jpg" },
{ type: "item", title: "My Bike", picture: "images/4.jpg" },
{ type: "item", title: "My Bike", picture: "images/5.jpg" }
];
var bindingList = new WinJS.Binding.List(array);
WinJS.Namespace.define("DefaultData", {
bindingList: bindingList,
array: array
});
var e = DefaultData.bindingList.dataSource;
})();
Write the following code in script2.js:
(function () {
var PageOutput = WinJS.Class.define(
function (element, options) {
element.winControl = this;
this.element = element;
new WinJS.Utilities.QueryCollection(element)
.setAttribute("role", "region")
.setAttribute("aria-labelledby", "outputLabel")
.setAttribute("aria-live", "assertive");
element.id = "output";
this._addOutputLabel(element);
this._addStatusOutput(element);
}, {
_addOutputLabel: function (element) {
var label = document.createElement("h2");
label.id = "outputLabel";
label.textContent = "Output";
element.parentNode.insertBefore(label, element);
},
_addStatusOutput: function (element) {
var statusDiv = document.createElement("div");
statusDiv.id = "statusMessage";
element.insertBefore(statusDiv, element.childNodes[0]);
}
}
);
var currentPageUrl = null;
WinJS.Navigation.addEventListener("navigating", function (evt) {
currentPageUrl = evt.detail.location;
});
WinJS.log = function (message, tag, type) {
var statusDiv = document.getElementById("statusMessage");
};
function activated(e) {
WinJS.Utilities.query("#featureLabel")[0].textContent = App.sampleTitle;
}
WinJS.Application.addEventListener("activated", activated, false);
WinJS.Namespace.define("App", {
PageOutput: PageOutput
});
})();
Output
Summary
In this article I described how to create an app for image navigation in Windows Store Apps using JavaScript. I hope this article has helped you in understanding this topic. Please share it. if you know more about this, your feedback and constructive contributions are welcome.