Tuesday, November 18, 2014

Custom Dropdown With Angular.

The more I get into front-end development the more I end up disliking standard HTML inputs.  I dislike them for different reasons, but my biggest issue has been the inability to style them how I need them to.  This goes especially double for new HTML 5 elements that you pretty much have to jump through hoops to get any kind of custom styling.  I already took on restyling a checkbox since it doesn't really work well for my fat fingers.  Now I am going to take on the select tag.

What styling a select feels like.
I don't really dislike the select tag.  Overall it is pretty simple and you can style most of it.  However. if you end up deciding to style it with colors of any type, then you will hate it.  You will hate it because you can't change the hover color when it is expanded.  You will hate it because you can't change the arrow/box at the end that indicates it is a drop down.  You will hate it even more because not only can these styles not be modified, but they are different per browser/OS combo.

So after googling a bit, I didn't find anything I completely liked, so I decided I would handle it myself.  I decided that since I would be using it for angular development that I would make it a directive and have it function as a select element, that can be customized.

Here is the end result.






Wednesday, October 15, 2014

Creating A Resortable Drag & Drop List With Angular JS.

I recently ran into a requirement that required a user to be able to resort a given list elements via drag and drop.  As soon as I heard about the requirement I shuddered a bit.  I had the vague memory of trying drag and drop once and it not being as intuitive as it should have been.

I started with a quick Google search to see what was out there and while there were some good starting points, there wasn't anything that was exactly what I wanted.  Pretty much all the examples I found were dragging from area A to area B like this.  Nothing showed resorting in the same list like I had hoped.  What was even a bit more dismaying was when I searched specifically for angular.js implementations of drag and drop I found that it didn't even come with directives for those events.  There were some directives that others implemented for drag and drop, but I decided I should figure it out for myself instead of trying to fiddle with other people's code.

So to get started I decided to break my angular code into 3 directives.  One for making an element, draggable, one for making an element droppable, and one for making the entire list sortable.

Three sir.

Draggable directive.

To make something draggable we want to create a directive that can be applied to any element that we want to make draggable.  So I restricted the directive to "A" for attribute, and added a link function that attached event handlers to the dragstart and dragend events.  In the dragstart event I needed to do a couple of things.  First set the data that is going to be transferred (this could be anything but I set mine to an attribute on the element ), tell the browser what type of transfer is happening, and finally I want to add a class to the element I'm dragging so I can style the element more appropriately.  In the dragend event handler I just want to remove said class.

The end result ends up looking like this:

directive('draggable', function(){
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {
            var el = element[0];
            el.draggable = true;
            
            el.addEventListener('dragstart', handleDragStart, false);
            el.addEventListener('dragend', handleDragEnd, false);
            function handleDragStart(e){
              this.classList.add('dragging');
              
              e.dataTransfer.effectAllowed = 'move';
              e.dataTransfer.setData('data', attributes.dragData);                
                
            }
            
            function handleDragEnd(e) {
              // this/e.target is the source node.
              this.classList.remove('dragging');
            
                           
            }
        }
    }
})

What is important to note here is whatever effectAllowed property you set, will dictate certain styles like the mouse cursor, and it limits where it can be dragged to other elements that have that same effect allowed.

Likewise, for the setData attribute, you can name that whatever you like.  It is a message for you to handle later.  So if you actually have multiple sets of data to drag and drop, you can limit what accepts it by having different "types".

Droppable directive.

The droppable directive is going to be similar.  Once again I am going to limit it to be an attribute on the element and to mostly adding events to the element.  The first events that I am going to handle are dragenter and dragleave.  All I am going to end up doing with these events is adding and removing a class to the element we are dragging over so we can add an additional style to it.  Next I am going to handle the dragover event where I am going to set the dropEffect of the element to match what is in the effectAllowed above.  Finally I am going to handle the drop event in which I will grab the data specified in the draggable directive above, and call a function on the scope.

Here is what it looks like:

.directive('droppable', function(){
    return {
        restrict:"A",
        link: function(scope, element, attributes) {
            var el = element[0];
            el.addEventListener('dragenter', handleDragEnter, false);
            el.addEventListener('dragover', handleDragOver, false);
            el.addEventListener('dragleave', handleDragLeave, false);
            el.addEventListener('drop', handleDrop, false);
            
            function handleDragOver(e) {
              if (e.preventDefault) {
                e.preventDefault(); // Necessary. Allows us to drop.
              }
              e.dataTransfer.dropEffect = 'move';  
            
              return false;
            }
            
            function handleDragEnter(e) {
                // this / e.target is the current hover target.
                this.classList.add('dragover');
            }
            function handleDrop(e){
                // Stops some browsers from redirecting.
                if (e.stopPropagation) e.stopPropagation();
                var data =e.dataTransfer.getData('data');
                
                if ('function' == typeof scope.ondrop) {      
                    scope.ondrop(data, attributes.dropData , e);
                }
                this.classList.remove('dragover');  
            }
            function handleDragLeave(e) {
                if (e.preventDefault) e.preventDefault();
                this.classList.remove('dragover');  
               
            }
        }
    };
})


Reorder Directive

The reorder directive is tied to an Array object and attaches the draggable and droppable directives onto each element in the array.  I decided to make the reorder directive based on an element ("E") and enabled transclude on it allow you to style each list element yourself.  The rest of the directive creates 1 <li> element for each object in the array and implements the drop handler needed in the draggable directive.  In the drop handler I had to add an $apply call to notify the UI that changes had been made to the array and that it should be updated.

Here is the directive:
directive('reorder', function () {
    return {
        restrict: 'E',
        transclude: true, 
        scope: {
            list: '='
        },
        template: '
  • ', link: function (scope, element) { scope.ondrop = function resort(dragIndex, dropIndex, event){ if (dragIndex == dropIndex){ return; } scope.$apply(function(){ dragIndex = Number(dragIndex); // grab element to move var elementToMove = scope.list[dragIndex]; // remove the element scope.list.splice(dragIndex, 1); // insert the element scope.list.splice(Number(dropIndex), 0, elementToMove); }); }; } }; });


    Here is the end result:
    Cheers!




    Wednesday, September 24, 2014

    Restyling the checkbox.

    When you start to work on webpages that need to function well in other devices, you start to learn that certain native HTML elements aren't really made for touch.  When using a mouse, you can be pretty precise with your mouse movements and your clicking.  However, when you attempt to touch something, smaller elements will cause the user to miss often.  There are some good articles on the interwebs on the recommended size of elements that will require touch, including some guidelines from Microsoft.  One of the elements that I have been getting frustrated with recently is the check box.  My fingers had a rough time clicking them on a tablet device.

    This episode is almost 20 years old.

    So I decided to come up with something that would be easier for a user with a touch interface to use, but still act as a check box.  Like most of my element redesign, I wanted to avoid using any JavaScript and rely on solely on CSS.  Taking a look around the web and other places I decided to settle on an on/off switch looking element.

    The first thing we want to define is the button HTML.  First we define a container element for the class.  We are going to use this element as the container and anchor for everything inside it.  Next, we add a checkbox (because we still want to use the functionality of the checkbox for binding, form submitting, etc.  I then added a "background" element that would span the entire width and change based on the input being checked or unchecked (I'll get to this more later).  Lastly, we add a few elements to give it the "switch" feel.  That is a label for "On", "Off", and label that is going to act as the "slider" for the switch.  Our HTML is going to look like this:

    <span class='toggleContainer'>
            
        <input class='toggleInput' type='checkbox' id='chk'></input>
        <span class='background'></span>
        <label class='slider' for='chk'></label>
        <label class='on' for='chk'>On</label>
        <label class='off' for='chk'>Off</label>
    
    </span>
    

    So you might think it is odd that I am using labels for these elements, especially for the "slider".  The reason that I do this, has to do with the "for" property of the label.  By clicking on the label, it will click the element for you.*

    So now that we have our structure designed we need to style everything to turn it into a toggle button.  First we will style the container.  We are going to use this to determine the size of everything.  So we need to set the position it to be relative to give us an anchor for all the child elements, set overflow to hidden so nothing flows out of  the container, and finally just set a nice border.

    .toggleContainer{
        border: 1px solid white;
        border-radius:6px;
        display:inline-block;
        width:50px;
        height:20px;
        position: relative; 
        overflow:hidden;
    }
    


    Next, lets style the check box.  We really just want to make it invisible, and not effect the rest of the HTML.
    .toggleInput{
        position: absolute;
        opacity: 0;
    }
    

    Now we want to style the on, off, slider, and background.  I include these all together because they all need two styles each.  One for checked and one for unchecked.  So let's start with the first state which is unchecked.  For the background we are going to set it as the entire width and height of the container (including the border radius), and set the background-color for when the we are unchecked which I will set to red.  I am going to also set the transition of the background color which will give a smoother transition feel.

    .toggleContainer .background {
        position: absolute;
        width: 100%;
        height: 100%;
        border-radius: 6px;
        background-color: red;
        opacity: 0.2;
        -moz-transition: background-color .3s;
        -webkit-transition: background-color .3s;
        transition: background-color .3s;
    }
    

    The slider will be defaulted to the left side of the toggle, with the on element hidden off the left, and the off displayed to the right of the slider.  Instead of just hiding the elements, I want to make it look smoother by moving objects to the left and right.  So I added a transition to the elements below.

    .toggleContainer .toggleInput ~ .slider {
        position: absolute;
        width: 45%;
        height: 100%;
        content: "";
        background-color: white;
        border-radius: 6px;
        display: inline-block;
        z-index: 1;
        left: 0;
        -moz-transition: left 0.3s;
        -webkit-transition: left 0.3s;
        transition: left 0.3s;
    }
    
    .toggleContainer .toggleInput ~ .off, .toggleContainer .toggleInput ~ .on {
        width: 50%;
        position: relative;
        z-index: 10;
        display: inline-block;
        overflow: hidden;
        height: 100%;
        top: 50%;
        margin-top: -8px;
    }
    
    .toggleContainer .toggleInput ~ .on {
        left: -50%;
        float: left;
        -moz-transition: left 0.3s;
        -webkit-transition: left 0.3s;
        transition: left 0.3s;
        color: green;
    }
    
    .toggleContainer .toggleInput ~ .off {
        right: 0;
        -moz-transition: right 0.3s;
        -webkit-transition: right 0.3s;
        transition: right 0.3s;
        float: right;
        color: red;
    }
    
    
    Now that we have a default style, we need to add styles for when the element is checked.  To do this we can use the :checked css selector and apply the styles that we want to change.  Mostly we want to move everything to the right which gives the appearance of a switch being moved.  The only exception is the background which we will change from red to green.

    .toggleContainer .toggleInput:checked ~ .background {
        background-color: green;
        -moz-transition: background-color 0.3s;
        -webkit-transition: background-color 0.3s;
        transition: background-color 0.3s;
    }
    
    .toggleContainer .toggleInput:checked ~ .slider {
        left: 55%;
        -moz-transition: left 0.3s;
        -webkit-transition: left 0.3s;
        transition: left 0.3s;
    }
    
    .toggleContainer .toggleInput:checked ~ .on {
        left: 5px;
        -moz-transition: left 0.3s;
        -webkit-transition: left 0.3s;
        transition: left 0.3s;
    }
    .toggleContainer .toggleInput:checked ~ .off {
        right: -50%;
        -moz-transition: right 0.3s;
        -webkit-transition: right 0.3s;
        transition: right 0.3s;
    }
    
    


    Here is the end result:
    I think the best way to use this would be to create a widget, directive, or whatever your JS framework supports. With a little bit of extra JS you can add some additional classes such as a class to the entire module and style the entire element differently.

    *If you are putting this on the web, you should really think about accessibility.  Having those extra labels for one input might confuse some screen readers, and by extension your users.  A couple of extra click handlers for the labels won't kill anyone. 

    Tuesday, September 16, 2014

    Using Iconic Fonts



    One of the new things I've started incorporating in my development is the use of iconic fonts.

    No No No!

    I'm talking about fonts that have icons in their character set like font awesome, batch icons, ico moon, and many many more.

    So why would you use a font with icons in them instead of just images?  Well there are a lot of really good reasons, that other blogs talk about, really well so I will summarize my thoughts and what I have learned.


    1. Really easy to use.
    2. You can style them like you would a normal font (color size etc.)
    3. You load them 1x.
    4. Pretty much all browsers support them.

    But wait, what about accessibility and users that might need to use screen readers?  Won't using these trip them up?
    Dr. Says No.
    Well, not if they do it right.  If an icon font is working correctly it will use the Private Unicode character range.  So it won't trip up the reader.

    So now that we have established the reasons of why to use them, we need to see how to use them.

    First thing you need to do is load the font via CSS as follows:
    @font-face{
        font-family:Batch;
        src:url('https://googledrive.com/host/0B4QQSpfhQZN5bFZ1ckV4QTRPb2s/batch-icons-webfont.eot');
        src:url('https://googledrive.com/host/0B4QQSpfhQZN5bFZ1ckV4QTRPb2s/batch-icons-webfont.eot?#iefix') format('embedded-opentype'),
            url('https://googledrive.com/host/0B4QQSpfhQZN5bFZ1ckV4QTRPb2s/batch-icons-webfont.woff') format('woff'),
            url('https://googledrive.com/host/0B4QQSpfhQZN5bFZ1ckV4QTRPb2s/batch-icons-webfont.ttf') format('truetype'),
            url('https://googledrive.com/host/0B4QQSpfhQZN5bFZ1ckV4QTRPb2s/batch-icons-webfont.svg#batchregular') format('svg');
        font-weight:normal;
        font-style:normal;
    }
    



    Next you use that character in your html.  
    .batch {
        font-family:"Batch"; /*or whatever you've decalred your font name as*/
        font-size:16px;
        line-height:1;
        
    }
    
    
    Super easy!


    That is nice, but I want to be awesome.  I want to have a button that does X (settings, cancel, ok, etc) and I don't want to have to have that code everywhere I have an ok button.  I just want to to have my class know about the font and character.

    You can be that awesome.  We can use the ::before and ::after pseudo-elements to do that.
    .before::before{
        font-family:"Batch";
        content: "\f000";
    }
    


    That is awesome! But you know, after thinking about it, I don't want my content in my css it doesn't feel right. I still think pseudo elements are sweet though. Good News! You can still use them and even use the 'data-' attributes as well. Just put the character code in a data attribute of your choice, and add the following class.
    .data::before{
        font-family:"Batch";
        content: attr(data-icon);
    }
    

    Here is the final output of everything!
    view the tif file.


    Wednesday, August 20, 2014

    Making Some Progress

    I just started on a new project recently and one of the things on the page I was working on was displaying progress bars in different colors.  The progress bars need to display an indeterminate state and after talking with a co-worker we thought making it look like KITT from Night Rider was the best (and most awesome) way to display that.


    Is that you Mr. Feeny?
    Because of a lot of my work being done on the canvas, and to have communication being done with web sockets, I am only supporting modern browsers for this app.  I knew there are a ton of new elements in HTML 5 and one of my personal goals is to become better at writing semantically correct html.  A quick look through the elements, and I found the <progress> tag.  Even better, it comes with an indeterminate state and is supported in the browsers I care about.  My next requirement was just to style the progress elements different colors based on their states.  I thought this is going to be easy.  All I need to do is add some classes to set the color and I'm done!


    So after a quick search I find out you pretty much can't do anything to style a progress element.  Which made me think...  how do you expect people to use this element if they can't make it look how they need it to?

    I was determined to use CSS only to get this to work and make my page semantically awesome.  I found some sites like this that show you how to clear out the native browser appearance and add your own.  So I started with that and then added my own classes to style them how I needed.  A lot of CSS later, I came up with the following:

    .progressContainer{
        position:relative;
        width: 100px;
    }
    
    progress{
      height:5px;
      border:0;
      appearance: none;
      -moz-appearance: none;
      -webkit-appearance: none;
      width:100% !important;
     
    }
    
    progress.connected::-webkit-progress-value{
      background: green;
    } 
    progress.connected::-moz-progress-value{
      background: green;
    }
    progress.connected::progress-value{
      background: green;
    }
    progress.connected::-webkit-progress-bar{
      background: green;
    } 
    progress.connected::-moz-progress-bar{
      background: green;
    }
    progress.connected::progress-bar{
      background: green;
    }
    progress.connected:not([value])::-webkit-progress-bar{
      background:green;
    }
    progress.connected:not([value])::-moz-progress-bar{
      background:green;
    }
    progress.connected:not([value])::progress-bar{
      background:green;
    }
    

    With the lower half repeated for each class that I wanted, I was almost done. While verbose, I was able to style the progress as necessary. The only thing left was to get KITT to work. After some quick thinking I figured I would use a ::before pseudo element and animate it.
    @-webkit-keyframes knightRider {
       0% {
     left: 0%;
          }
     
      50% {
     left:100%;
            margin-left:-10px;
          }
      100%{
     left:0%;
          }
    }
    @keyframes knightRider {
       0% {
     left: 0%;
          }
     
      50% {
       left:100%;
            margin-left:-10px;
          }
      100%{
     left:0%;
          }
    }
    
    progress.searching::before{
      content:"";
      background-color:blue;
      width:10px;
      height:5px;
      position: absolute;
      animation: knightRider 2s infinite linear;
      -webkit-animation: knightRider 2s infinite linear;
    }
    
    A quick refresh of my fiddle and BOOM it was working.  Now I just need to make sure it is working in Firefox which should be just a formality, right?  Wrong!  Only webkit browsers support ::before and ::after on progress for some bizzaro reason.  So I ended up adding a hidden div next to the progress bar that would use the animation from above and only show up when a specific class is added to the progress bar.

    See the Pen Progress by Andy (@andyjmeyers) on CodePen.




    Finally some progress...

    Friday, August 8, 2014

    Beyond The Wall (Proxy)

    North of the Wall, things are different. That's where the children went, and the giants, and the other old races.
    Nowadays a lot of development resources are managed via a package manager. While this makes life a lot easier almost all of the time, it can be a little bit of a headache if you aren't set up like everyone else is. When I first started out at Motorola, I had a rough time getting some of these to work. I finally realized that it had to do with me being behind the company proxy.  So here is what you need to do to get a couple things to work if you are behind a company proxy so hopefully someone else doesn't have to go through what I did.

    1. Bower
    Open up your bower config file (.bowerrc) and add the following lines:

      "proxy":"http(s)://server:port/",
      "https-proxy":"http(s)://server:port/"

    2. Git 

    Open up your .gitconfig file and add the following lines:

    [http]
        proxy = http(s)://server:port/
    [https]
        proxy = http(s)://server:port/

    3. NPM

    I believe these get stored in the .npmrc file, but npm recommends you set them via command line with these commands.

    npm config set proxy http(s)://server:port npm config set https-proxy http(s)://server:port

    Your .npmrc file then looks like this:

    proxy =http(s)://server:port
    https_proxy = http(s)://server:port


    *The image above is copyrighted by HBO

    Saturday, August 2, 2014

    Testing Angular Directives That Repeat With Transclude

    The title is a bit of a mouthful, but recently I was writing a unit test for an angular directive I was working on.  The directive's purpose is to flatten a binary tree and display all of the leafs in a list.  A demo of the working code is here: JS Fiddle.

    Here is the actual directive:
    .directive('flattenTree', function () {
    
        return {
            restrict: 'E',
            template: "<li ng-repeat='node in tree' ng-transclude></li>",
            transclude: true,
            scope: {
                root: '='
            },
            link: function (scope) {
                scope.tree = [];
                var index = 0;
                var queue = [];
                queue.push(scope.root);
                while (queue.length != 0) {
                    var node = queue.shift();
                    if (node['left']) {
                        queue.unshift( node.right, node.left);
                    } else {
                        scope.tree.push(node);
                    }
                }
      
            }
        };
    
    });

    When writing the tests I wanted to validate a few things.

    1. If the root node is null then it doesn't display anything
    2. That the root node is traversed correctly and all leaf nodes are displayed in order
    3. That each node has a corresponding <li> tag and the html in the transclude is displayed for each one.

    Here is what I came up for my test that is written with Jasmine.

    My setup code in which I grab the angular objects that I need to run the tests.
    var $compile,
        $rootScope;
    
    // Store references to $rootScope and $compile
    // so they are available to all tests in this describe block
    beforeEach(inject(function(_$compile_, _$rootScope_){
    
      // The injector unwraps the underscores (_) from around the parameter names when matching
      $compile = _$compile_;
      $rootScope = _$rootScope_;
    
    }));
    
    

    And my 3 tests.  Each test consist of 3 parts.

    1. Setting up the data and the template I want to use (always using <flatten-tree> since that is my directive).
    2. Compiling the template and calling $digest to make the angular magic work.
    3. Validating the result & DOM with the jqlite that comes with angular.

    it('replaces the element with null when empty', function() {
        // Compile a piece of HTML containing the directive
        var template = "<flatten-tree><span>{{node}}</span></flatten-tree>";
        var element = $compile(template)($rootScope);
    
        // fire all the watches, so the scope expression will be evaluated
        $rootScope.$digest();
    
        // Check that the compiled element contains the templated content
        expect(element.children().length).toBe(0);
    });
    
    it('replaces the element with a single li when one node', function() {
    
         $rootScope.node = "foobar";
        var template = "<ul><flatten-tree root='node'><span>{{node}}</span></flatten-tree></ul>";
        // Compile a piece of HTML containing the directive
        var element = $compile(template)($rootScope);
        // fire all the watches, so the scope expression will be evaluated
        $rootScope.$digest();
    
        // Check that the compiled element contains the templated content
        expect(element.find("li").length).toBe(1);
        expect(element.find("span").length).toBe(1);
        expect(element.find("span").text()).toBe("foobar");
    });
    
    it('traverses the tree and replaces the element with a list', function() {
    
        $rootScope.node = {
            right:{
                right:1,
                left:2
            },
            left:{
                right:{
                    right:3,
                    left:4
                },
                left:{
                    right:5,
                    left:6
                }
            }
        };
    
        var template = "<ul><flatten-tree root='node'><span>{{node}}</span></flatten-tree></ul>";
    
        // Compile a piece of HTML containing the directive
        var element = $compile(template)($rootScope);
    
        // fire all the watches, so the scope expression will be evaluated
        $rootScope.$digest();
    
        // Check that the compiled element contains the templated content
        expect(element.find("li").length).toBe(6);
        expect(element.find("span").length).toBe(6);
    
        var spanElements = element.find("span");
        for (var i = 0; i < spanElements.length; i++) {
               expect(spanElements[i].innerText).toBe((i+1).toString());
        }
    });
    
    
    
    Easy as pie!