Roel van Lisdonk on September 8th, 2014

Kudos to Chris Love:

 

 

 

http://www.love2dev.com/#!article/Chrome-and-Pointer-Events-Its-Not-Over-but-We-Need-Your-Help

 

 

What Can You Do?

I got wind the Blink team, that is the team that created Google’s Chrome browser, is watching the Chromium bug tracker issue #162757. This is the issue/bug report filed to have pointer events added to Chrome. They are trying to gauge how popular the API is with developers. The metric is how many times the issue is starred.

 

Chromium Pointer Events Bug Issue 162757

 

So please take a moment and tap the star in the top-left corner of the page and register your vote in support of pointer events in Chrome. Then tell some friends to do the same. Maybe the Blink team will take notice and use the code that was checked into the WebKit source almost 2 years ago and give developers an easy to use input modality abstraction API. In the end developers will have a much easier time dealing with new input modalities and end users can have better user experiences.

Tags:

Roel van Lisdonk on September 5th, 2014

If you want to reliable bring the main window of an external process to the foreground in C#, use the “simulate alt key” technique found at: http://stackoverflow.com/questions/10740346/setforegroundwindow-only-working-while-visual-studio-is-open

Just start Notepad, then run the following test in Visual Studio.

It should bring Notepad as a maximized window in the foreground.

namespace Research.EndToEndTests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    [TestClass]
    public class ResearchTester
    {
        
        [TestMethod]
        public void Test()
        {
            Reliable_set_window_to_forground();
            Assert.IsTrue(true);
        }

        public void Reliable_set_window_to_forground()
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process proc in processes)
            {
                if (ProcessIsNotepad(proc))
                {
                    ActivateWindow(proc.MainWindowHandle);
                }
            }
        }

        public bool ProcessIsNotepad(Process proc) 
        {
            return proc.MainWindowTitle.EndsWith("Notepad", StringComparison.InvariantCultureIgnoreCase);
        }
        
        private const int ALT = 0xA4;
        private const int EXTENDEDKEY = 0x1;
        private const int KEYUP = 0x2;
        private const int SHOW_MAXIMIZED = 3;

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        
        public static void ActivateWindow(IntPtr mainWindowHandle)
        {
            // Guard: check if window already has focus.
            if (mainWindowHandle == GetForegroundWindow()) return;

            // Show window maximized.
            ShowWindow(mainWindowHandle, SHOW_MAXIMIZED);
            
            // Simulate an "ALT" key press.
            keybd_event((byte)ALT, 0x45, EXTENDEDKEY | 0, 0);
                        
            // Simulate an "ALT" key release.
            keybd_event((byte)ALT, 0x45, EXTENDEDKEY | KEYUP, 0);

            // Show window in forground.
            SetForegroundWindow(mainWindowHandle);
        }
    }
}

Tags:

The roslyn compiler found in Visual Studio 14 enables us, to adjust C# code during runtime, by compiling the new C# code in-memory. Now we can edit the UI part (HTML, CSS, JavaScript) of a single page app, including the backend C# ASP .NET Web Api service dynamically in runtime.

 

On of the things, that disrupts my workflow, when creating a single page app with AngularJS, backed by a web api C# service in Visual Studio 2013, is the fact that, when I find a bug in my C# code while testing the UI, I have to stop the debugger, adjust the web api C# code, re-compile, re-run and then recreating state in the web app before I can resume testing the UI.

This pain will go away, when using Visual Studio 14

 

Download and install Visual Studio 14 CTP 3 from http://msdn.microsoft.com/en-US/subscriptions/downloads/

Open Visual Studio, choose File > New > Project …

 

image

 

image

 

Start the web application by pressing CTRL + F5 (so don’t start the debugger).

 

image

 

Click on the Contact page in the browser:

You will see the text “Your contact page”.

image

 

Now go to the Controllers\HomeController.cs

image

 

Adjust the the C# code: ViewBag.Message = "Your contact page.";

to

ViewBag.Message = "My contact page.";

 

image

 

Save the file and in the browser press F5, you might see a short delay, because the roslyn compiler is now dynamically compiling the code.

 

The C# code is changed without disrupting my workflow.

 

image

Tags: ,

Roel van Lisdonk on September 4th, 2014

All new C# 6 and beyond features and their status, can be found at:

https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status&referringTitle=Home

This sight also contains the design meetings, so you are able to predict which direction the creators of C# are taking.

 

This new Microsoft Open Source thing, rocks!

Tags:

I was testing some JavaScript ajax code found at: http://youmightnotneedjquery.com/ and I was getting the error:

 

Uncaught ReferenceError: request is not defined from ReferenceError: request is not defined
   at Object.rli.app.self.makeXmlHttpRequest (
http://localhost:50258/Client/Features/Posts/request_is_not_defined.html:27:25)
   at Object.rli.app.self.onExecuteClick (http://localhost:50258/Client/Features/Posts/request_is_not_defined.html:55:22)
   at HTMLButtonElement.onclick(http://localhost:50258/Client/Features/Posts/request_is_not_defined.html:12:145) request_is_not_defined.html:63
Uncaught ReferenceError: request is not defined

 

image

 

Cause

This was caused by the line:

request = new XMLHttpRequest();

When you use ‘use strict’, variables need to be defined before use.

Solution

So adding var before request fixed the problem:

<!DOCTYPE html> <html> <head> <title>Vanilla JavaScript.</title> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" /> </head> <body> <div class="spa-page"> <button onclick="rli.app.onExecuteClick()">Execute</button> </div> <script type="text/javascript"> // Use a namespace to prevent pollution of the global namespace. var rli = rli || {}; // Define application root object. rli.app = (function () { 'use strict'; var self = {}; self.makeXmlHttpRequest = function () {

// When "var" is removed from this line, an error is thrown:
// Uncaught ReferenceError: request is not defined.
var request = new XMLHttpRequest();

request.open('GET', 'http://www.google.com', true); request.onload = function () { if (this.status >= 200 && this.status < 400) { // Success! var data = JSON.parse(this.response); console.log(data); } else { // We reached our target server, but it returned an error. console.log("Error status not between 200 and 400."); } }; request.onerror = function (e) { // There was a connection error of some sort. console.log(e); }; request.send(); }; self.onExecuteClick = function () { self.makeXmlHttpRequest(); }; self.start = function () { // Define global exception handler. window.onerror = function (message, file, line, col, error) { console.log(message, "from", error.stack); }; }; return self; })(); // Start the application. rli.app.start(); </script> </body> </html>

Tags:

Roel van Lisdonk on August 11th, 2014

Check out this article on HTML5rocks:

http://www.html5rocks.com/en/tutorials/es7/observe/

Observe data without dirty checking on POJO’s.

Tags:

After I installed: http://msdn.microsoft.com/en-us/vstudio/dn722381.aspx.

Visual Studio 2013 would keep on crashing when opening a Multi-Device Hybrid Apps project.

I found the fix at: http://stackoverflow.com/questions/24404132/visual-studio-crashes-every-time-a-project-is-opened

 

Go to Tools > Options > Extensions and Updates > Add Web Essentials Nightly builds:

image

 

Then go to Tools > Extensions and Updates… (install the latest Web Essentials build)

image

 

I also updated to the latest version of the Windows Phone emulator, but I don’t think that is necessary.

 

 

Running a cordova AngularJS app from Visual Studio 2013 in the ripple:

 

image

 

Running app in the Android emulator from Visual Studio 2013:

 

Listing all available AVD’s

C:\Users\Roel\AppData\Local\Android\android-sdk\tools>android list targets

Creating a AVD

C:\Users\Roel\AppData\Local\Android\android-sdk\tools>android create avd -n rli_
_4__android_4.4.2 -t 4

 

image

 

Note: first time launch of the emulator might take a while in my case (2 minutes staring at a black screen with android logo on it Winking smile)

Tags:

 

In this post all will be extracting a data service from the code created in the previous posts and I will be adding some client side validation by using the breeze directive: data-z-validate.

 

Note

All code can be found at:

https://github.com/roelvanlisdonk/Research/tree/master/Research/Research.UI.Web/Client/Features/AngularJS_and_Breeze/Part4

 

image

 

Just by adding the data-z-validate attribute to the input elements in the grid, breeze will automatically show client side validation errors, by default an asterisk “*” is shown for required fields and a message is shown, when the user clears a required field.

image

 

In this case I also added a maxlength data annotation to the lastName field:

image

 

Model

namespace Research.UI.Web.Server.Model
{
    using System.ComponentModel.DataAnnotations;

    public class Employee
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [MaxLength(20)]
        public string FirstName { get; set; }
        [Required]
        [MaxLength(50)]
        public string LastName { get; set; }
        [MaxLength(20)]
        public string PhoneNumber { get; set; }
    }
}

Note

I encountered a problem with the data-z-validate directive, this was caused by added the directive dynamically in a custom directive created by me, see: http://stackoverflow.com/questions/22849402/angular-directive-with-a-template-does-not-work-with-z-validate but this was fixed by altering my custom directive.

 

The custom directive with fix

// Angular directive [spaField] responsible for generating grid cell controls.
spa.app.directive('spaField', ['$compile', function ($compile)
{
    var directive = {
        restrict: 'A', /* Restrict this directive to attributes.  */
        replace: true, /* The given element will be replaced in the link function. */
        link: function ($scope, element, attrs)
        {
            // The data-z-validate directive will append a [span class="z-decorator"] to the following [input] element, by using the jquery "append" function.
            var html = '<input ng-disabled="{{vm.isKeyField(prop)}}" type="text" data-ng-model="entity[prop.name]" data-z-validate>';

            if ($scope.prop.relatedNavigationProperty) {
                var relatedTableName = $scope.prop.relatedNavigationProperty.nameOnServer;
                html = '<select kendo-combo-box ng-model="entity[prop.name]" ng-options="record.id as record.firstName for record in vm[\'' + relatedTableName + '\']"></select>';
            } else if ($scope.prop.dataType.name === "DateTime") {
                html = '<input kendo-date-picker k-ng-model="entity[prop.name]" k-format="\'dd-MMM-yyyy\'" />';
            } else if ($scope.prop.dataType.name === "Boolean")
            {
                html = '<input kendo-mobile-switch k-on-label="\'YES\'" k-off-label="\'NO\'" />';
            }

            // Apply scope to the created html fragment.
            var compiled = $compile(html)($scope);

            // Get the [<span class="z-decorator"] appended to the input element by the z-validate directive.
            var span = compiled[0].parentNode.children[1];

            // The following 2 lines will only add the input element to the DOM and not the [span class="z-decorator"], that is added by the z-validate directive.
            element.replaceWith(compiled);
            element = compiled;

            // Add the [span class="z-decorator"] to the current parent element of the input element.
            element.parent().append(span);
            element.parent().append(span);
        }
    };
    
    return directive;
}]);

Tags: , ,

 

Using Kendo UI mobile switch in AngularJS generated grid.

image

 

Using Kendo UI date picker in AngularJS generated grid.

image

 

Using Kendo UI combobox in AngularJS generated grid.

image

 

All code can be found at:

https://github.com/roelvanlisdonk/Research/tree/master/Research/Research.UI.Web/Client/Features/AngularJS_and_Breeze/Part3

 

Some highlights:

 

AngularJS directive

This directive will render the controls in the grid

spa.app.directive('ngField',['$compile', function ($compile) {

    var directive = {
        restrict: 'A', /* restrict this directive to elements */

        link: function ($scope, element, attrs) {
            var html = '<input ng-disabled="{{vm.isKeyField(prop)}}" type="text" ng-model="entity[prop.name]">';

            if ($scope.prop.relatedNavigationProperty) {
                var relatedTableName = $scope.prop.relatedNavigationProperty.nameOnServer;
                html = '<select kendo-combo-box ng-model="entity[prop.name]" ng-options="record.id as record.firstName for record in vm[\'' + relatedTableName + '\']"></select>';
            } else if ($scope.prop.dataType.name === "DateTime") {
                html = '<input kendo-date-picker k-ng-model="entity[prop.name]" k-format="\'dd-MMM-yyyy\'" />';
            } else if ($scope.prop.dataType.name === "Boolean")
            {
                html = '<input kendo-mobile-switch k-on-label="\'YES\'" k-off-label="\'NO\'" />';
            }

            var compiled = $compile(html)($scope);
            element.replaceWith(compiled);
            element = compiled;
        }
    };
    
    return directive;
}]);

 

HTML

The HTML shows a splash page with the text “Loading data…” by using the ng-cloak AngularJS directive.

By putting all external resources, like “link” and “script” tags at the bottom of the page and inlining the minimal CSS to show the splash page, the splash page is immediately shown to the user, while the AngularJS app is booted and the data is loaded.

<!DOCTYPE html>
<html data-ng-app="app">
<head>
    <title data-ng-bind="title">Angular and Breeze</title>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />

    <!-- All external resources are moved to the bottom of the page, to allow direct rendering of the [splash screen]. -->
    <!-- Only the styling for the [loader / splash screen] is included in the head. -->
    <style type=text/css>
        html, body {
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            box-sizing: border-box; /* Border boxing is used, so the padding, margin and borders are within the width and height of the element. */
            height: 100%;           /* Full screen single page app. */
            margin: 0;              /* Prevent unnecessary white space. */
            max-height: 100%;       /* Full screen single page app. */
            outline: 0;             /* Prevent unnecessary white space. */
            padding: 0;             /* Prevent unnecessary white space. */
        }

        body {
            padding: 20px;
        }

        div.spa-splash {
            display: none;
        }

        div.spa-splash, div.spa-splash > div {
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            box-sizing: border-box; /* Border boxing is used, so the padding, margin and borders are within the width and height of the element. */
            height: 100%;           /* Full screen single page app. */
            max-height: 100%;       /* Full screen single page app. */
            padding: 20px;            
        }

        div.spa-splash > div {
            border: 1px solid rgb(212, 212, 212);
            padding: 10px;
        }

        [ng-cloak].spa-splash {
          display: block !important;
        }

        [ng-cloak] {
            display: none;
        }
    </style>
</head>
<body>
    <div class="spa-splash" ng-cloak><div>Loading data...</div></div>
    <div class="spa-page container" data-ng-controller="admin as vm" ng-cloak>
        <div class="spa-page-sidebar">
            <div>Tables</div>
            <div ng-repeat="entityType in vm.entityTypes"><a class="spa-action-link" ng-click="vm.refresh(entityType)">{{ entityType.shortName }}</a></div>
        </div>
        <div class="spa-page-content">
            <div class="spa-grid-toolbar">
                <a class="spa-action-link" ng-click="vm.save()">save</a> |
                <a class="spa-action-link" ng-click="vm.reset()">reset</a> |
                <a class="spa-action-link" ng-click="vm.create()">create</a>
                <i class="fa fa-exclamation-circle"
                   title="Some data has change. Press save to save the changes to the server!"
                   ng-show="vm.isDirty"></i>
            </div>
            <table class="spa-grid">
                <thead>
                    <tr>
                        <th> </th>
                        <th ng-repeat='(key, prop) in vm.entityDataFields'>{{ prop.name }}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="entity in vm.entities">
                        <td><a class="spa-action-link" ng-click="vm.delete(entity)">delete</a></td>
                        <td ng-repeat='(key, prop) in vm.entityDataFields'>
                            <div ng-field></div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Libraries -->
    <link rel="stylesheet" type="text/css" href="../../../Libraries/FontAwesome/css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="../../../Libraries/Toastr/toastr.min.css" />

    <!-- App -->
    <link rel="stylesheet" type="text/css" href="app.css" />

    <!-- Libraries -->
    <script type="text/javascript" src="../../../Libraries/jQuery/jquery-2.1.1.js"></script>
    <script type="text/javascript" src="../../../Libraries/Angular/angular.js"></script>

    <!-- Kendo -->
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.2.625/styles/kendo.common.min.css" />
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.2.625/styles/kendo.rtl.min.css" />
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.2.625/styles/kendo.metro.min.css" />
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.2.625/styles/kendo.metro.mobile.min.css" />
    <script type="text/javascript" src="//cdn.kendostatic.com/2014.2.625/js/kendo.all.min.js"></script>

    <!-- Breeze -->
    <script type="text/javascript" src="../../../Libraries/Breeze/breeze.debug.js"></script>
    <script type="text/javascript" src="../../../Libraries/Breeze/breeze.angular.js"></script>

    <!-- Add toastr which needs jQuery (Breeze does not need jQuery). -->
    <script type="text/javascript" src="../../../Libraries/Toastr/toastr.js"></script>
    <script type="text/javascript" src="../../../Libraries/Moment/moment.js"></script>
    <script type="text/javascript" src="../../../Libraries/Spin/spin.js"></script>

    <!-- Add breeze.savequeuing which needs Q (Breeze does not need Q). -->
    <script type="text/javascript" src="../../../Libraries/Q/q.min.js"></script>
    <script type="text/javascript" src="../../../Libraries/Breeze/breeze.savequeuing.js"></script>

    <!-- App -->
    <script type="text/javascript" src="app.js"></script>
</body>
</html>

Tags: , , ,

Roel van Lisdonk on July 14th, 2014

The native confirm dialog, shown when you use the confirm function in JavaScript can’t be positioned in JavaScript.

A Kendo UI window can be positioned.

 

 

image

 

HTML

<!DOCTYPE html>
<html>
<head>
    <title>Kendo element binding research page.</title>
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.1.528/styles/kendo.common.min.css" />
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.1.528/styles/kendo.rtl.min.css" />
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.1.528/styles/kendo.metro.min.css" />
    <link rel="stylesheet" type="text/css" href="//cdn.kendostatic.com/2014.1.528/styles/kendo.metro.mobile.min.css" />
    <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
    <style>
        /* Resets */
        div, span, i, p, input, select {
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            box-sizing: border-box; /* Border boxing is used, so the padding, margin and borders are within the width and height of de element. */
            color: rgba(112, 112, 112, 1);
            font-family: Arial, Helvetica, sans-serif; /* For know use default fonts used on google.com stackoverflow.com, telerik.com etc. */
            font-size: 13px;
            margin: 0; /* Margin zero is used to prevent unnecessary white space. */
            padding: 0; /* Padding zero is used to prevent unnecessary white space. */
        }        

        html, body {
            height: 100%;
            max-height: 100%;
        }

        body {
            padding: 20px;
        }

        .page {
            border: 1px solid rgb(212, 212, 212);
            height: 100%;
            max-height: 100%;
            padding: 10px;
            position: relative;
        }

        #confirm > p {
            padding-bottom: 20px;
        }

        .k-button {
            cursor: pointer;
        }

        .k-window-titlebar, .k-window-titlebar > span {
            cursor: move;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="page">
        <button class="k-button" data-bind="click: onShowClick">Show confirm</button>
        <div id="confirm"></div>
    </div>
    <script type="text/x-kendo-template" id="confirmTemplate">
        <p>#= ConfirmText #</p>
        <button class="k-button" id="yesButton">Yes</button>
        <button class="k-button" id="noButton"> No</button>
    </script>
    <!-- Libraries -->
    <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="//cdn.kendostatic.com/2014.1.528/js/kendo.all.min.js"></script>
    
    <!-- Custom -->
    <script type="text/javascript" src="app.js"></script>
</body>
</html>

 

Code

var kendo = kendo || {};

kendo.controller = (function () {
    var self = {};
    var _vm = null;
    var _confirm = null;
    var _confirmTemplate = null;

    self.show = function () {
        _confirm =  $("#confirm").kendoWindow({
            title: "Delete record",
            visible: false, // The window will not appear before its .open method is called.
            width: "230px",
            height: "100px",
        }).data("kendoWindow");
        _confirmTemplate = kendo.template($("#confirmTemplate").html());
        _vm = new kendo.data.ObservableObject({
            onShowClick: function () {
                // Dynamically set some window options.
                _confirm.setOptions({
                    position: {
                        top: 200,
                        left: 200
                    }
                });
                _confirm.content(_confirmTemplate({ ConfirmText: "Delete record?"}));
                _confirm.open();

                $("#yesButton").click(function () {

                    // TODO: Delete record.
                    _confirm.close();
                })
                $("#noButton").click(function () {
                    // User cancelled the window.
                    _confirm.close();
                })
            }
        });

        kendo.bind($(".page"), _vm);
    };

    return self;
})();

kendo.controller.show();

Tags: ,