Blog

  • Building jQuery UI

    First off, jQuery UI is great. It makes creating a responsive UI simple, and the tabs capability is great. However, I recently came across a use case where I wanted to dynamically alter the url of a tab (specifically, the query string) dynamically, something it seemed the library didn’t allow. The load-time href of the tab was wrapped and inaccessible at run-time. The fix would be simple, but the source file I was working with was the minified output of their customizable web-based build system, making modification impossible. Downloading fresh, unminified source was equally impractical. So I looked into building it myself. It turned out surprisingly easy.

    Obtaining the source

    First, I downloaded the latest stable source (1.8.16 at this time), and took a look through it. All the modules had their individual files. In the build directory, I noticed “build.xml”. This is an Ant build configuration file, so I then proceeded to setup Ant.

    Setting up Ant

    It took reading some documentation, but this was equally simple. I already had Java, so I jumped right to getting application:

    1. Download the Ant binaries from here
    2. After unzipping it, I configured the following environment variables:
    • %ANT_HOME% = Installation path for Ant
    • %JAVA_HOME% = Path to current Java install on my system (not technically necessary, since this was already in my %PATH%)
    • Add %ANT_HOME% to %PATH% so I needed fully-qualify the exe when I execute it

    Building the Source

    Again, this was simple.

    1. Open up a command prompt
    2. cd to the build folder inside the jquery.ui source directory (where build.xml is)
    3. “ant build deploy-release” to build it to build/dist

    There were a few other build options I could’ve chosen. “Minify” will just output the minified source, without license info, documentation, or zipping it.

     

    I also experimented with building jQuery UI 1.9 beta, but ran into some issues. Getting the source from github was simple, but they’ve replaced their minification engine from the Google Closure compiler to uglify.js, since it “saves 4 minutes per build, and actually produces slightly smaller files“. This posed an issue, since the current version of the code executes uglify.js from a shell script. I converted the Linux shell script to a Windows batch file, with a little help to get the directory name. This all went fine, until it came to actually executing the js from the command line. There is no console js engine by default in Windows, and using the js.exe output when I built Firefox seemed to error when it ran the script (issue with referencing “global”). Since using 1.8.16 was alright for my immediate purposes I didn’t look into further solutions.

    I could’ve tried installing Rhino to get around this, but it’s something to try in the future. Another solution is obviously to try running it on Linux. I spent a bit of time updating my Ubuntu VM install after getting everything working. Until I try and venture into building 1.9, I’m just fine developing off of 1.8.16. After all, it works great.

  • Software teaching life lessons

    More often than not, solving problems through software is simple. The issue/requirements are there in front of you, clear and needing to be solved. Every once in a while though, something stands up and teaches me a lesson.

    Every system relies on external components. Whether it’s a third-party library to help with a specific purpose (jQuery, ComponentOne), a runtime to compile applications against (.NET, Java), or a compiler library for simple operations (iostream, math.h), all systems have dependencies. They’re everywhere.

    99 times out of 100, when something goes wrong with the code, it’s my fault. These are the cases I mentioned earlier: clear issues, simple solutions. It’s the tricky ones that exist outside my program’s black box, where creative coding and problem solving comes into the picture. These are the ones I like. They teach me to think outside the box, to not take anything for granted, and that above all, we’re all human.

    It’s what makes software fun to write.

  • Schrödinger’s bug

    Today I learned a valuable lesson in debugging. Today I learned what it can be like to chase your own tail. I had accomplished the rare feat of introducing a bug through the very act of debugging.

    A common feature for software is to behave differently depending on the rights of the user. Super users should see an entire list, while normal users should have less power, only seeing list items pertaining to them. Simple to code, tricky to test. All it takes in an “if” statement, like this:

    if(superuser) {
       // Run special code
    } else {
       // Run not-so special code
    }

    And then you write the code. Of course, with my default rights, I can only check one half of this conditional (I am a superuser). Rather than modifying the code and recompiling, or modifying my rights, I thought it would be great to use a debugging tool to bypass this check entirely, and hop right into the non-privileged. Big mistake. First time I tried this, I got a null reference error while calling a function. Not out of the ordinary, the function called a web service, maybe I wasn’t disposing of something properly. 20 fruitless minutes later, I’d rewritten that function several ways trying every way of calling, disposing, and scoping I knew. When that didn’t work, I looked higher up.

    The erroring function was called in one other place, in a non-static context. No luck when I tried changing that. Using an intermediary variable for the result? Still it crashed. After a lot of simplification, I managed to make the null error happen one line further up in the code:

    MyClass c = new MyClass();

    Now this didn’t make sense. MyClass had a default, empty constructor, and no class-level fields. Yet running this line gave me a Null Reference Exception. Getting tired of stepping the debugger forward each time, I decided to just comment out the check, recompile, and continue debugging. Then, the error stopped. After a lot of hunting through Disassemblies, I found the rub: using the debugger to bypass a line of code changed (or rather, didn’t change) some values in memory that the instructions below expected. Eventually, when it tried to issue a “call” to a memory address, there would be random garbage data there. A seg fault may’ve been happening under the scenes, which .NET happened to present as a… Null Reference Exception.

    My attempt to observe program state altered the very state I was trying to observe. Schrödinger’s bug.

  • Converting a string to XML in JavaScript

    XmlHttpRequest (XHR) often does a lot of the heavy lifting with converting text into an XML document when performing a resource request, but sometimes requires a little bit of a boost. It will only convert the document to an XML node structure if the MIME type from the server indicate it’s an xml-structured document. This can be anything from “text/xml” to more custom formats like “application/ttml+xml”. This all requires the MIME type to be registered on the server for the file extension, like so. If it isn’t, too bad, the file is coming back as “text/plain”, and the browser doesn’t convert it to XML. I hope the calling application wasn’t requiring the user to do this, because if so, the app just broke.

    What does this mean for software developers? Is it fair to expect every user to have to update mime.types or .htaccess so they can use the application? Even if it weren’t for the technological burden it places on the user, it’s just plain unfair to ask 100 people to do what you could just do yourself. So XHR allows you to overrideMimeType for the expected result, allowing for browsers to treat xml documents as xml, even if they come back as text/plain. Which would be great in a world of universal browser support.

    For example, IE 6 and below didn’t use XHR, and relied on a different mechanism (ActiveX) to perform requests from JavaScript. With the introduction of it in IE7, there still wasn’t support for mime type overriding. Which meant a convoluted process of indicating the browser should convert it when XHR.overrideMimeType was supported, and converting it manually when it wasn’t. This is great, but still brings us back to placing the burden of the user to configure what the developer was too lazy to do explicitly.

    After some digging into why some code was crashing recently, I realized this, and decided a good solution would be to manually convert the XHR result to an xml node structure if it wasn’t already. In typical circumstances, this would be the case only because the mime type was wrong. In atypical cases, the document itself is invalid XML, at which point the fix is out of the developer’s hands and responsibility shifts to the creator of the document. 95% of the time failures in situation occur though, it’ could’ve been avoided by defensive coding and re-parsing the xml content if it wasn’t automatically by the browser.

    So I made a utility function to do this. Most browsers already have the tools, all it took was creating a cross-browser wrapper for them. Grab it from GitHub, or see it below:

    // Convert a string to XML Node Structure
    // Returns null on failure
    function textToXML ( text ) {
          try {
            var xml = null;
    
            if ( window.DOMParser ) {
    
              var parser = new DOMParser();
              xml = parser.parseFromString( text, "text/xml" );
    
              var found = xml.getElementsByTagName( "parsererror" );
    
              if ( !found || !found.length || !found[ 0 ].childNodes.length ) {
                return xml;
              }
    
              return null;
            } else {
    
              xml = new ActiveXObject( "Microsoft.XMLDOM" );
    
              xml.async = false;
              xml.loadXML( text );
    
              return xml;
            }
          } catch ( e ) {
            // suppress
          }
        }
  • How I learned to stop worrying and love clean code

    In past bloggings, I’ve mentioned how my coding style has changed over time to reflect a deeper technical knowledge. Recently I’ve also been trying to learn more about software design and engineering from a maintainability and clean practices standpoint. While they are an important tool for accomplishing this, I’m not just talking about design patterns but a knowledge suite of best practices that make for intuitive-to-read and even easier to use code.

    Concepts like descriptive function and variable names, small functions, and intuitive design  are stressed heavily, as is design by functional decomposition. All of this I thought I knew, until I got torn apart at my first code review.

    Now I’m taking this, applying it to my code, and finding much cleaner and better results. JavaScript I’d written just 8 months ago looks horrendous by comparison to how I write now. Giant C-style functions with a whack of declarations at the top, 100+ lines of functionally independent code, and a general mass of structured but difficult to maintain code. In “modernizing” it and splitting it up into components, I find it reads easier, writes quicker, and even executes faster. I can only chalk this last part up to smaller scope for function JITs and tracing JITs to worry about, but it just goes to show:

    Work with the compiler, not against it.

  • Working with cookies in JS

    Well, it has certainly been a while since I’ve blogged.

    I’ve been working with data-driven tables a little bit recently, and the need came up to allow users to click on the table cells, and to persist a record of these clicks. Rather than store this in the database, it made more sense to use cookies. Not having worked with cookies for a while, I started playing around. I ran into a few gotchas.

    FIRST GOTCHA: Cookies can only be managed in the context of a web server.

    Everything started great. I found some sample code online to base the solution off of, and I began to I hooked all the events in, but data wasn’t persisting. No errors were being thrown. After some debugging work, it looked like it was never being set. Incidentally, this was because I was running the file off the file system (file://clickClick.html). No errors are thrown, no warnings are raised, it just won’t set unless you run it from a web server. After starting up Apache and running it from localhost, everything worked great. For a while.

    SECOND GOTCHA: With jQuery, the unload event is on ‘window’, not ‘document’

    My plan was to save the click results to a cookie when the page leaves, either via refresh or leaving the page altogether. Not bothering to read the documentation thoroughly, I tried this and expected it to work:

    $(document).unload(function() {   ...   });

    Turns out it didn’t. After some trawling of jquery’s documentation, I discovered it was the window object I wanted to hook into, not document. As with the cookies, this didn’t fire off an error, it just hooks into a user-created event that, unsurprisingly, is never fired by the browser. Switching ‘document’ to ‘window’ fixed this.

    FINAL GOTCHA: Internet Explorer does not seem to support character indexing via [] operator

    My method of saving data to a cookie is serializing a table into a string of ‘1’s and ‘0’s and storing it in a cookie, then reading that string back from the cookie and acting on it. Firefox and Chrome could handle reading individual characters fine by indexing the string like an array:

    if(oldCookieVal[i] === '1') {
                  ...
    }

    When testing on IE, I got an “unsupported property” error. Turns out IE treats strings like any other object, and will use indexing to perform a property lookup on the object. So if i = 3, instead of retrieving the fourth character in a string, IE was trying to do this:

    if(oldCookieVal.3 === '1') {
                  ...
    }

    Which broke the code. Switching to using the ‘charAt’ js function fixed this. Another solution would’ve been to split the string into an array before processing it.

    With these lessons learned, I successfully created a working prototype of a solution. You can see the final result here. It has been tested on Firefox 3.5.19, 6.0.2, Chrome 14, IE 9, Safari 5, and Opera 11.01/11.51. Presently there’s an issue with Opera not hooking into the unload event through jQuery, but moving the cookie setting code into the ‘mousedown’ event handler solves this.

  • Popcorn and Soundcloud

    Recently, Popcorn.js began to support custom players. When Henrik Moltke started a conversation going to get Soundcloud working with Popcorn, beautiful things started to happen. Soundcloud and Popcorn developers collaborated to create a Soundcloud player as well as some great plugins. Watch out for upcoming work from Henrik, Mark Boas and more as Mozilla’s Hyper Audio project continues: they’ve been cooking up some great works and demos. The union of Popcorn.js, Soundcloud, Radiolab, combined with Henrik, Mark and many others has produced a great Radiolab Player demo. For my own part in the process, working with everyone has been terrific.

    Popcorn 0.5 has also come out today, and includes some of the results of this effort. A custom player for use with Soundcloud has landed, which can be used with Popcorn simply, like so:

    var popcorn = Popcorn( Popcorn.soundcloud( "contentId", "http://soundcloud.com/fitn/cotton-in-the-ears" } ));

    Using the Soundcloud player with Popcorn like this will place Soundcloud’s player inside the HTML Element on the web page with the corresponding id “contentId”. The Soundcloud player will then play the track found at “http://soundcloud.com/fitn/cotton-in-the-ears”. This can then all be controlled through Popcorn, complete with timed track events. Be sure to check it and more out in the new release.

  • A retrospect: 3 years of coding changes

    I’m readying for graduation, and I’ve taken a looks back at how my coding style and structure has evolved (and am I ever glad it has). At first, there was a mess of convoluted logic, not-so-poly morphism, and generally bad code. The best example for this has been an ongoing project of mine for the last 3-4 years. I’ve been working on a chess engine, which has undergone massive rewrites as I’ve learned more. Without any knowledge of design patterns and limited knowledge of exactly what a compiler does, it was an unreadable mess of code. The only thing saving it from being spaghetti was structure.

    private bool canPiecesMove()
    {
      char colToMove = whiteTurn ? 'w' : 'b';
      short[][] moves;
      short[] discoveryCheckOrig = discoverCheckPiece;
    
        for (short x = 0; x < 8; x++)
        {
          for (short y = 0; y < 8; y++)
          {
            if (board[y, x] != null && board[y, x].Colour == colToMove && board[y, x].Value != (ushort)Game.PieceValues.king) // Come to own non-king piece
            {
              moves = board[y,x].getMoveableSquares(x,y);
              if (board[y, x].Value == (ushort)Game.PieceValues.pawn)
              {
                if (board[moves[0][1], moves[0][0]] == null && !isPinned(x, y, moves[0][0], moves[0][1])) // Pawn can move forward                             {                                 discoverCheckPiece = discoveryCheckOrig;                                 return true;                             }                             if (moves[1][1] >= 0 && moves[1][1] < 8 && moves[1][0] >= 0 && moves[1][0] < 8)
                {
                  if (board[moves[1][1], moves[1][0]] != null && board[moves[1][1], moves[1][0]].Colour != colToMove && !isPinned(x, y, moves[1][0], moves[1][1]))
                  {
                     .....
                  }
                }
              }
            }
          }
        }
      }
    }

    While it worked, it was a mess. 3 rewrites later it now takes advantage of 3 further years of knowledge and education. I’m currently working on move generation in preparation for eventual AI, but not much real feature progress has been made on the engine since. Refactoring has been the main draw of effort.

    Have I been bikeshedding in my development? Many friends say yes, but I disagree. Sure, additional features are few and far between, but isn’t one of the rules to develop one to throw away? Doesn’t the release early, release often mantra allow for ingenuity and inspiration to be unhindered by what “must be perfect?” I’ve learned a great deal since my first pass at this chess engine, and about the only unchanged code is UI-based. Without the willingness to go back and fix what wasn’t broken, I feel something would’ve been lost.

    So I say this: Never be afraid to reinvent your own wheel.

  • Custom HTML5 Video Objects: Timing

    In my last blog posting, I touched on defining your own implementation of an HTML5 Video. Basically all it takes is to implement the HTMLMediaElement interface on an object by defining the methods and attributes listed. It’s easy. In fact, here’s a starting skeleton I’ve whipped up:

    var undef;
    
    var MediaElement = {
      load: function() {},
      play: function() {},
      pause: function() {},
    
      readyState: 0,
      currentTime: 0,
      duration: 0,
      paused: 1,
      ended: 0,
      volume: 1,
      muted: 0,
      playbackRate: 1
      // These are considered to be "on" by being defined. Initialized to undefined
      autoplay: undef,
      loop: undef,
    };

    Those are a subset of methods and attributes and a give a good start, though there is a whole lot more functionality that may be implemented if desired. Even though we have the attributes, one important mechanism is missing. Videos update at regular intervals, which is something we’ll need to account for in our video object. This is also incredibly easy to do. According to the spec, time updates should occur every “15 to 250ms”:

    var msDelay = 250;
    
    function timerTick() {
      // Run code here
    
      setTimeout( timerTick, msDelay );
    }

    The above code will execute the “timerTick” function every 250 ms. I’ve created a timed loop using setTimeout instead of setInterval because setInterval has the unfortunate side-effects of not firing at reliable times as well as eating errors. In other words, setTimeout is used so that the program will let me know if it’s not working properly, as well as it will not be delayed by other js code that is executing on the page.

    With this in place, these two code snippets can be tied together (with less than 10 extra lines of code) to create a basic video wrapper:

    var undef;
    var msDelay = 250;
    
    var MediaElement = {
      load: function() {},
      play: function() {
        this.paused = 0;
        // So we can keep track of the instance
        timerTick.call( this );
      },
      pause: function() {
        this.paused = 1;
      },
    
      readyState: 0,
      currentTime: 0,
      duration: 0,
      paused: 1,
      ended: 0,
      volume: 1,
      muted: 0,
      playbackRate: 1,
    
      // These are considered to be "on" by being defined. Initialize to undefined
      autoplay: undef,
      loop: undef
    };
    
    function timerTick() {
      // The player was paused since the last time update
      if ( this.paused ) {
        return;
      }
    
      // So we can refer to the instance when setTimeout is run
      var self = this;
    
      // Run code here
    
      setTimeout( function() {
        timerTick.call( self );
      }, msDelay );
    }

    And just like that, you have a custom HTML5 “Video”. Check out a simple demo or view the source here.

  • Defining your own HTML5 Video Object

    With the introduction of HTML5, the <video> element offers a great deal of flexibility and scriptability to web-based video technologies. Popcorn.js takes this and runs with it, allowing everything on the page to work with a video to create great interactive webpages. Just check out what secretrobotron has made to see what I’m talking about. This can be taken further if you wrap non-HTML5 video sources so that they appear to look and act like HTML5 video.

    I’ve been doing a lot of twiddling with this lately, and see lots of applications for it. A simple example is a slideshow, made up of an array of images timed to change every, oh, five seconds or so. This is no different from an ordinary video where thousands of images change at a rate of 24 or more times per second. If this ordinary array of images were wrapped to look like a video, it could then be tied into Popcorn and let loose. Not only could a slideshow be started, stopped and seeked through but other neat options begin to open up.

    What if we want to dynamically caption the slide show images? What if we want to cue an <audio> element to play a sound for different sections of the slide show? What if we want to show additional information about the images or author like Twitter, Google Maps or more? This functionality is readily-available through Popcorn’s plugins. But if we really want to get creative…

    We could make one of the images in the slide show actually be a <canvas> element hosting a Processing.js sketch which plays for a few seconds in the middle of a slideshow. In that Processing.js sketch we could then have JavaScript that runs completely independent of Popcorn but may interact and control it to create a relationship where the player and video feed off of each other. We could also simplify things and just go back to our array of ordinary images but increase the cycling time from once every 5 seconds to once every 1/24 of a second to create fluid motion. Doing this would emulate a video in every sense, but 100% dynamic images means 100% dynamic video content.

    Eventually, I see the only limit being imagination.

    HTML5 Video (like HTML5 Audio) implements a standard interface called “HTMLMediaElement”. You can check out the spec here. To get a video-like object going, all one has to do is have a JavaScript object define the methods and attributes of this interface. Such is the nature of open web, where innovation only requires knowledge and expectation of an interface. While some work in this area has been done for the 0.4 version of Popcorn (out now), the real playground will open for the 0.5 release where it will be as easy to create custom “player plugins” as it is right now to make ordinary plugins.

    Time to start getting inspired.