Before moving further, let us look at the previous articles of the series
JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript, and what mistakes a developer generally makes and what differences they should be aware of.
Introduction
WinJS is a Windows library for JavaScript. WinJS provides the controls. You need to make your app professional, equipped with the modern UI controls and include JS, CSS, and HTML all in one place. It is an open-source library by Microsoft and the source code is available at GitHub.
What’s the use of WinJS?
WinJS contains the standard implementation of Win 8 controls which aren’t covered by HTML5. It makes it possible to add Windows UI controls in HTML such as:
- AppBar
- DatePicker
- FlipView
- Flyout
- ListView
- Rating
- SemanticZoom
- TimePicker
- ToggleSwitch
- Tooltip
- ViewBox
and many more…
I found these controls pretty cool as it enhances your webApp UI experience. Microsoft developed this for a universal experience across Win 8 and HTML apps, where you can add WinJS.
Which frameworks are supported by WinJS?
WinJS provides support to the three popular JavaScript frameworks via adapters. These are:
What is an adapter?
An adapter is a slim layer, which facilitates the usage of WinJS controls with the above three frameworks. Hence, there are different adapters available.
Create your first WinJS application
I will leverage the AngularJS adapter to build my first WinJS Application. I use the Yeoman Angular generator to scaffold my Application. Yeoman is a fantastic tool to kickstart new projects. For more information, visit here.
Setup basic AngularJS webapp
- Go to command prompt, type YO and it will show you the list of installed generators.
You must install the Angular generator before you can use it. If you can’t find the installed Angular generator, then scroll below to see the option:
You will see the list of matching generators.
Select the top, “Angular,” to install it.
- Now, fire YO command again and create a project using “Angular” generator and it will prompt multiple options. Hence, select accordingly.
- Now, you are done with your basic scaffold Application.
- Run the basic Application, using GRUNT command. The default port on which it runs is 9000.
Output
- Go to the directory and the basic structure is ready for you.
Add WinJS in your application
There are multiple ways to include WinJS in your Application.
- Dowload WinJS from GIT.
- Refer from CDN.
- Use command prompt tools npm, bower [I prefer this way].
- Fire the commands shown below in your WebApp console location.
npm install winjs
bower install winjs
This will install WinJS files under app\bower_components folder.
- As I mentioned earlier, we’ll add the AngularJS adapter, hence, fire the command shown below:
bower install angularjs-winjs
Goto directory app\bower_components and see the folder structure, shown below:
- Modify below files as
index.html: For our WebApp, we shall include all the relevant WinJS and the adapter files:
- <script src="bower_components/angular/angular.js"></script>
- <script src="bower_components/angular-route/angular-route.js"></script>
- <script src="bower_components/winjs/js/base.js"></script>
- <script src="bower_components/winjs/js/ui.js"></script>
- <script src="bower_components/angular-winjs/js/angular-winjs.js"></script>
- <link href="bower_components/winjs/css/ui-dark.css" rel="stylesheet" />
The index.html will be transformed, as shown below:
- <!doctype html>
-
-
-
-
- <html class="no-js">
-
-
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title></title>
- <meta name="description" content="">
- <meta name="viewport" content="width=device-width">
-
-
-
- <link rel="stylesheet" href="styles/main.css">
-
- </head>
-
- <body ng-app="NewAppApp">
- <!--[if lt IE 7]>
- <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
- <![endif]-->
-
- <!--[if lt IE 9]>
- <script src="bower_components/es5-shim/es5-shim.js"></script>
- <script src="bower_components/json3/lib/json3.min.js"></script>
- <![endif]-->
-
-
- <div class="container" ng-view=""></div>
-
-
- <script>
- (function(i, s, o, g, r, a, m)
- {
- i['GoogleAnalyticsObject'] = r;
- i[r] = i[r] || function()
- {
- (i[r].q = i[r].q || []).push(arguments)
- }, i[r].l = 1 * new Date();
- a = s.createElement(o),
- m = s.getElementsByTagName(o)[0];
- a.async = 1;
- a.src = g;
- m.parentNode.insertBefore(a, m)
- })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
-
- ga('create', 'UA-XXXXX-X');
- ga('send', 'pageview');
- </script>
- <script src="bower_components/angular/angular.js"></script>
- <script src="bower_components/angular-route/angular-route.js"></script>
- <script src="bower_components/winjs/js/base.js"></script>
- <script src="bower_components/winjs/js/ui.js"></script>
- <script src="bower_components/angular-winjs/js/angular-winjs.js"></script>
- <link href="bower_components/winjs/css/ui-dark.css" rel="stylesheet" />
-
-
-
-
- <script src="scripts/app.js"></script>
- <script src="scripts/controllers/main.js"></script>
-
- </body>
-
- </html>
App.js: We know this is the starting file of AngularJS and we shall include WinJS module dependency, as shown below:
- 'use strict';
- angular.module('NewAppApp', ['winjs', 'ngRoute'])
- .config(function($routeProvider)
- {
- $routeProvider
- .when('/',
- {
- templateUrl: 'views/main.html',
- controller: 'MainCtrl'
- })
- .otherwise
- ({
- redirectTo: '/'
- });
- });
Main.html: Modify main.html file and include the directive win-rating, as shown below:
- <win-rating max-rating="maxRating" user-rating="rating"></win-rating>
- <div>
- <h2 class="win-h2">Rating: {{rating}}</h2>
- <input class="win-slider" type="range" min="1" max="{{maxRating}}" ng-model="rating" />
- </div>
- <div>
- <h2 class="win-h2">Maximum Rating: {{maxRating}}</h2>
- <input class="win-slider" type="range" min="1" max="50" ng-model="maxRating" />
- </div>
Main.js: In above main.html, we are using two variable, “rating” and “maxRating,” and we can initialize their value in MainCtrl (controller),
- 'use strict';
-
- angular.module('NewAppApp')
- .controller('MainCtrl', function($scope)
- {
- $scope.rating = 1;
- $scope.maxRating = 5;
- });
Output
See that the “win-rating” directive has added stars according to the value of the slider.
You can download the code shown above from my GitHub Repository.
Note: Once you clone, please don’t forget to run “node install” and “bower install” commands.
There are many other such Win 8 controls which you can try:
- win-date-picker: Add below code in “main.html” and date control will show up,
- <win-date-picker current="date" on-change="dateChanged()"></win-date-picker>
Output
- win-date-picker: Add the code shown below in “main.html” and click the button to see flyout effect.
- <button id="flyoutAnchor">Flyout!</button>
- <win-flyout anchor="'#flyoutAnchor'">Happy coding!</win-flyout>
Output
- win-time-picker: Add the code generated below in “main.html” and see time control.
- <win-time-picker current="time"></win-time-picker>
Output
There are many other nice controls available and you can do a lot to enhance your UI experience.
Playground
If you would like to try using WinJS without installing the above stuff, you can go to two playgrounds.
Test the various samples that are provided. For example,
Summary
Please try the GitHub code and samples given above. I appreciate your feedback.