Thursday, June 25, 2015

Tips for building AngularJS forms with validation and Bootstrap

Lets get started, in this article we will be using AngularJS 1.3.15, because that's what version the project I'm currently working on uses. Keep in mind, this stuff changes all the time but, for now, this seems to be the "correct" way.

You've probably been using AngularJS for a while now, you also know how forms work, and you probably have forms in you're AngularJS project that are collecting data and everything is working fine. So why do you still need help with forms? That's where I was at a few days ago. I've been building forms with a basic understanding of ng-model and FormController, and they work, but I've been writing a lot of extra JavaScript to do it.

A developer on my team, figured out a better way to do a lot of the validation using AngularJS and it's was so simple. It was right here the whole time: code.angularjs.org/1.3.16/docs/guide/forms.

So, let's put a contact us form together using AngularJS and Bootstrap.
angular.module("mainApp")
    .controller("ContactCtrl", function ($scope, $log) {
        "use strict";
});
<!-- form.tpl.html -->
<form class="form-horizontal" name="contactForm" novalidate>
</form>

Starting with a basic controller and a form template. We already have a ton of stuff setup here thanks to AngularJS. AngularJS adds ng-form code to all <form> and <ng-form> tags that sets up a property on the $scope that matches the name you gave the form. In this example, even though we didn't create it, we can now access $scope.contactForm which is an instance of FormController.

For the HTML template, I'm going to copy the code from the link (code.angularjs.org/1.3.16/docs/guide/forms) to setup a name and email field in our form. Before we use it though, let's update the code to match what Bootstrap has for the basic HTML structure for a horizontal form using their CSS classes.
<!-- form.tpl.html -->
<form class="form-horizontal" name="contactForm">

    <div class="form-group">
        <label for="inputName" class="col-sm-2 control-label">Name:</label>
        <div class="col-sm-10">
            
            <input type="text" id="inputName" name="uName"
                   class="form-control"
                   placeholder="Enter your name"
                   ng-model="user.name"
                   required />
            
            <div ng-show="contactForm.$submitted || contactForm.uName.$touched">
                <span ng-show="contactForm.uName.$error.required">
                    Tell us your name.</span>
            </div>
            
        </div>
    </div>

    <div class="form-group">
        <label for="inputEmail" class="col-sm-2 control-label">E-mail:</label>
        <div class="col-sm-10">
            
            <input type="email" id="inputEmail" name="uEmail"
                   class="form-control"
                   placeholder="E-mail"
                   ng-model="user.email"
                   required />
            
            <div ng-show="contactForm.$submitted || contactForm.uEmail.$touched">
                <span ng-show="contactForm.uEmail.$error.required">
                    Tell us your email.</span>
                <span ng-show="contactForm.uEmail.$error.email">
                    This is not a valid email.</span>
            </div>
            
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            
            <button type="button" 
                    class="btn btn-default" 
                    ng-click="reset()">Reset</button>
            
            <button type="submit" 
                    class="btn btn-primary" 
                    ng-click="save()" 
                    ng-disabled="contactForm.$invalid">Save</button>
        </div>
    </div>

</form>

In the code above, everything looks pretty standard.

The name attribute
The thing to look at is that on each input we have added a name attribute. This is what I was missing when I first tried to setup forms using validation in this way. Adding the name "uName" will in turn add that property to the parent formController instance "contactForm".

Now, that element's ng-model scope can be accessed via $scope.contactForm.uName. This is big because then we can easily add validation messages like ng-show="contactForm.uEmail.$error.required" in a list under each form input.

$touched
Another thing I overlooked was the wrapping div around the validation messages. With out it, as soon as the form loads, we see the required error message and the input it outlined in red. I thought this was an issue with AngularJS and spent a lot of time getting around it, but actually I just need to use the $touched property. This will cause the input not to be shown as invalid until the user causes a blur event on the field.

$submitted
The last thing I want to point out is the contactForm.$submitted property. Since AngularJS converts all form tags to ng-form, they no longer submit to the server by default when you mark a button as type="submit". What happens is that this property on formController is set to true, which allows us another chance to show any validation errors in the form.

Let's do a little work on our controller now.
angular.module("mainApp")
    .controller("ContactCtrl", function ($scope, $log, userService) {
        "use strict";

    //we need to create a new user to hold our form data. 
    $scope.user = userService.createUser();

    //we'll assume that userService.createUser() returns an object like this:
    //{ name : "", email : "" };

    $scope.reset = function () {
        //here we can just do a simple call to create a new user again
        $scope.user = userService.createUser();
    };

    $scope.save = function () {
        //we can use our formController to do a simple check to see if the form is valid
        if( $scope.contactForm.$valid ) {
            //all fields are valid, so submit to server.
            userService.save( $scope.user );
        }
    };
});

As you can see, we can let AngularJS do most of the work with forms and validation. No more if/else statements like this to check all the custom validation on your form elements.
    $scope.save = function () {
        if($scope.user.name.length === 0) {
             $scope.userNameError = "Name is Required!";
        } else if($scope.user.email.length > 0) {
             $scope.userEmailError = "Email is Required!";
        } else if(! isEmail($scope.user.email) ) {
             $scope.userEmailError = "This is not a valid email.";
        } else {
             //form is valid
             userService.save( $scope.user );
        }
    };

But you never wrote code like that anyway, right?

Wednesday, April 29, 2015

HTML5 Canvas Drawing and Animation 101 (no frameworks!)

The other day I had some downtime and decided to mess around with drawing and animating with the canvas tag. As far as I can tell, unless your building a game or some 3D interactive thing on the web, you probably should opt for svg vector drawing and/or animation since they are way simpler.

For drawing with the canvas element, I recommend one of these great frameworks to ease development: EaselJS and Three.js.

For this experiment, I thought I'd just write vanilla JavaScript since I was just messing around and didn't want to spend a lot of time reading documentation. After a few hours, I end up with this neat little network of dots animation:


Below, I will talk through how I achieved this animation and show the code used.

First off, we need some JavaScript to add the canvas tag to the page. You can always just write the <canvas /> tag in the body of the page and give it an id, so whichever you prefer.
var canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);

Now for the heart of our application, the looper function. This is the function that will repeat over and over again and draw each frame of our animation, just like the frames of a movie. For this, it is recommended that we use the requestAnimationFrame() method in JavaScript instead of setInterval() since it is geared towards animating elements on the screen and will perform better/smoother. Plus, if the tab or window is inactive, the animation stops which is better for the computer's CPU.
var startTime = new Date().getTime(),
    lastTime = 0;

//start looping
requestAnimationFrame( looper );

function looper() {
    requestAnimationFrame( looper );

    //calculate current frame time and delta time
    //delta time - elapsed time since the last call to the looper method
    var now = new Date().getTime() - startTime,
        delta = (now - lastTime)/1000;

    //get the 2d context object
    var ctx = canvas.getContext('2d');

    //clear all graphics from the canvas
    ctx.clearRect(0 , 0 , canvas.width , canvas.height);

    //update game physics (animations)
    update( delta );

    //render everything back to the canvas
    draw( ctx );
    
    //save the current time for next loop
    lastTime = now;
}

//these 2 methods will be finished below
function update(delta) { }

function draw(ctx) { }


In the code above, I ended up not using delta time in my experiment, but if you are building a serious app or game be sure to take a look at some blog post about it: Fix Your Timestep. There is also quite a bit more code you'll need to make sure your game runs at a constant speed no matter what computer or browser it's running on.

Now to talk about what we are drawing/animating. In the picture above we have an array of orange circles (nodes) and a bunch of green lines (links) that are chasing each other around the screen. When one node catches it's target the link is broken and the node stops moving. In the code I represent each orange circle as a node agent. An agent is basically just an object that keeps track of the current values for one of the circles on the screen. This way, our draw function ends up being rather simple. Just loop over all node agents in the array and draw them on the screen.
//create network of nodes
var nodesArray = createNodes(200, canvas);

//This method will create an array of node agents to 
//be drawn on the screen
function createNodes(size, canvas) {
    var nodes = [];

    for(var i = 0; i < size; i++ ){
        //Here we create an agent at a random x, y position
        //with a random radius that is between 5px and 25px
        nodes.push(
            createNodeAgent(
                Math.random() * canvas.width,
                Math.random() * canvas.height,
                5 + (Math.random() * 20)
            )
        );
    }
    return nodes;
}
//This is a factory function that return a new node agent object
function createNodeAgent(x, y, r) {
    return {
        x : x,
        y : y,
        radius : r,
        //Here we give it the orange color and set the alpha
        //based on this agent's size (bigger means more opaque)
        color : "rgba(161,96,9," + r/30 + ")",
        link : -1
    };
}

With our array of nodes we can finish writing the draw method which will fill our canvas with a background color and draw each node agent on the screen.
function draw(ctx) {
    //fill screen with black background
    ctx.fillStyle = 'black';
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);

    //draw all nodes
    nodesArray.forEach(function (agent) {
        drawNode(ctx, agent);
    });
}
function drawNode(ctx, agent) {
    ctx.beginPath();
    ctx.arc(agent.x, agent.y, agent.radius, 0, 2 * Math.PI, false);
    ctx.fillStyle = agent.color;
    ctx.fill();
}

At this point, you should be able to run the app and see all the circles on the screen. Every time you refresh the screen they are placed at random spots on the screen which is pretty cool, but having motion will be way cooler so lets finish the update method.

One thing we need to do first though is figure out how to get the nodes to chase a target node. For this we will use one of the properties we added above to each node called "link". Link is going to be the index of another node in our master array. We could just pick one at random from the master array, but after a few test, it looked much nicer when nodes that are closest to each other are linked together. You end up with more of a networking effect this way.
//connect all nodes to the target node that it is closest to.
linkNodes(nodesArray);

function linkNodes(nodes) {
    nodes.forEach(function (agent, idx) {
        //check how far all other agents are from this agent
        var dists = nodes.map(function (agentToCheck, i) {
            return Math.abs(agent.x - agentToCheck.x) + Math.abs(agent.y - agentToCheck.y);
        });

        //find the next closest agent thats not already linked
        var closest = 0,
            dist = 99999;

        dists.forEach(function (distance, i) {
            if(distance < dist && nodes[i] !== agent && nodes[i].link === -1) {
                dist = distance;
                closest = i;
            }
        });
        agent.link = closest;
    });
}

I'll admit, this method isn't pretty and there is probably a way better way to do this but, whatever, it works so I'm rolling with it.

With this method, we have iterated through each node and gave it a target node to chase. Now we can finish the update method. The update method will slowly step each node towards it's target until it gets within a certain distance. When is distance is reached, the node will clear the link and stop moving.
function update(delta) {

    //nodes move towards the agent its connected to
    nodesArray.forEach(function (agent) {
        //if this agent still has a target, update position
        if(agent.link !== -1) {
            //find target node in master array
            var targetAgent = nodesArray[agent.link];
            
            //step this agent towards the target agent using
            //a simple easing function
            agent.x = chase(agent.x, targetAgent.x, 100);
            agent.y = chase(agent.y, targetAgent.y, 100);
            
            //check to see if we are next the our target node
            var distX = Math.abs(agent.x-targetAgent.x),
                distY = Math.abs(agent.y-targetAgent.y),
                min = (agent.radius + targetAgent.radius);

            //if we are then clear link and stop moving
            if(distX < min && distY < min){
                agent.link = -1;
            }
        }
    });
}
function chase(current, target, constant) {
    var change = (target - current) / constant;
    return current + change;
)

This is getting cooler! So when you run your app, you should see all the dot slowly chasing there targets around the screen. You can add all kinds of cool math here and see what happens, also if you haven't noticed, what we have here is the start of a basic Particle System. From here we can start looking into velocity and force and all kinds of neat things. But, in the spirit of not re-inventing the wheel, we should probably defer to a fully tested framework for more advanced topics.

Before I finish, you might be wondering where all the lines in the image at the top are. Lets revise our update method and add a function to draw the lines between each linked agent.
function draw(ctx) {
    //fill screen with black background
    ctx.fillStyle = 'black';
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);

    //first pass, draw all links
    nodesArray.forEach(function (agent) {
        if(agent.link !== -1) {
            var linkedAgent = nodes[agent.link];
            drawLink(ctx, agent, linkedAgent);
        }
    });

    //second pass, draw all nodes
    nodesArray.forEach(function (agent) {
        drawNode(ctx, agent);
    });
}

function drawLink(ctx, agent, linkedAgent) {
   ctx.beginPath();
   ctx.moveTo(agent.x, agent.y);
   ctx.lineTo(linkedAgent.x, linkedAgent.y);

   //line style
   ctx.lineWidth = 5;
   ctx.lineCap = 'round';
   ctx.strokeStyle = 'rgba(161,174,20,0.5)';

   //draw
   ctx.stroke();

   //cleanup
   ctx.lineWidth = 0;
}

Happy Coding!

Thursday, March 5, 2015

Instead of ng-include, try an AngularJS directive

Recently, I came across a lot of code in an AngularJS project I'm working on that uses ng-include. I haven't really used ng-include in Angular very much. Instead, whenever I need to reuse a chunk of HTML, I just use create a custom AngularJS directive. Looking back at how I’ve been using directives, a ng-include might have been a better choice in some places.

With that being said, I still think that in most situations, you are better off splitting up functionality into directives. Many of the places in the current Angular project I’m working on where ng-include is being used, a directive would have been a much better choice. So how to know when to use one or the other? One big red flag is if you are thinking of using the ng-init, ng-controller attributes. These have their uses, but, I believe should be avoided when building your application.

Here is and example of what I mean:
<!-- app/views/properties.html -->
<div class="properties" ng-controller="propertiesCtrl">
    <form class="form-horizontal">
        <div class="form-group" 
             ng-repeat="key in currentElement.props" 
             ng-include="'app/views/properties/main.html'"></div>
    </form>
</div>
<!-- app/views/properties/main.html -->
<label class="col-md-3 control-label">
    {{key | capitalize}} :
</label>
<div class="col-md-7"
     ng-include="'composer/flowDesigner/views/properties/controls/DEFAULT.html'">
</div>

The code above is basically the reason why directives were created in Angular. A directive allows you to create a chuck on code that can be expressed as a tag, a pseudo web component if you will. Directives also allows us to isolate functionality and reduce the amount of code that is in a controller for a view or page.

Here is the example above but written as a directive with some comments about how the code is structured:
angular("mainApp")
    // We define the name of the directive with a prefix. The prefix helps to 
    // distinguish our custom html directive tags from actual html tags
    // example : <com-header> vs <header>
    //
    // NOTE: Angular converts camel case to dash case when rendering directives. 
    //       So in our html page, we will use <com-properties> instead of <comProperies>
    .directive("comProperies", function () {
        "use strict";

        return {
            // restrict how this directive can be used, E means as an html tag only
            restrict: "E",
 
            // replace the tag on the page with the template of this directive
            replace: true, 

            // path to our html template
            templateUrl: "app/views/comProperies.tpl.html",

            // html to JS linking function
            link: properiesLink,

            // this directive's controller function
            // NOTE: Start with a capital letter since angular uses controller like 
            //       pseudo classes
            controller: ProperiesCtrl
        };
  
        function properiesLink($scope, $element, $attrs) {
            // The linking function allows us to add any html manipulation we might need.
            // Basically, if you are using jQuery "$" selector function for anything, 
            // add it here.
            //
            // NOTE: Try to avoid using a linking function. Angular has a lot of directives 
            //       you can add to your template file so you don't need one. 
            //       If you are doing anything with the $attrs object, use $attrs.$observe. 
            //       This will trigger a function if the value of an attribute ever changes.
            //       $element is a jQuery reference to your template's root html element.
        }
  
        function ProperiesCtrl($scope) {
            // A basic controller function for this directive. One thing to remember is that 
            // all instances of this directive use the same controller. So, for example, 
            // "this.currentIndex = 0;" would be a global variable among all 
            // <com-properties> elements.
        }
    });

Not too much has changed in this example except for code structure. The real advantage can be seen as you start to expand functionality of this directive. For example, this directive looks like it creates a form and lists out the properties in currentElement.props so that they can be edited. Now we want to list out the properties of currentPlan.details. By re-using the “ng-model” attribute that is provided by angular. We can update the custom directive html element so it can be written as:
<com-properties ng-model=”currentPlan.details”></com-properties>

To make this update, first inside our template, find and change ng-repeat="key in mainTabKeys" to ng-repeat="key in ngModel". Next, add scope : {ngModel : "=" } to the return object in the directive so now our directive looks like this:
angular("mainApp")
    .directive("comProperies", function () {
        "use strict";

        return {
            restrict: "E",
            replace: true, 
            templateUrl: "app/views/comProperies.tpl.html",
            link: properiesLink,
            controller: ProperiesCtrl,
            scope : { 
                ngModel : "=" 
            }
        };
        
        // ...
    });

With this change, this directive will now list out all the properties of whatever object the attribute ng-model is set to. I encourage you to read more about directive by following the links below, since this article is a very limited overview of how directives are used in AngularJS.