Monday, October 24, 2011

Need Query String Parameters?: jQuery.query

This extension creates a singleton query string object for quick and readable query string modification and creation.  This plugin provides a simple way of taking a page's query string and creating a modified version of this with little code.

Format/Structure:

Get:
          var value = $.query.get( [Query String Parameter Name] );

Set:
           var url = $.query.set( [Parameter Name] , [Parameter Value] );

Remove:
          var result = $.query.REMOVE ( [Parameter Name] );

Examples:

Getting a query string parameter:

          var id = $.query.get('id');

Setting a query string parameter:

          var queryString = $.query.set('id',1);

Removing a query string parameter:

          var results = $.query.REMOVE('id');


This plugin also contains the ability to customize a set of parameters to your liking.

Customization parameters:

  • separator
    • The default value for this setting is "&" as that is the standard for parameter separation.
  • space
    • The default value for this is true as most people prefer plus signs in query strings to be converted to spaces
  • suffix
    • The default for this is true.  If set to true, any arrays, when outputted to a query string will be suffixed with "[]" to show them as an array more clearly.
  • hash
    • The default for this is false.  If set to true, the output of a query string will be prefixed by a hash "#" rather than a question mark.
  • prefix
    • The default for this is true.  If set to false, the output of a query string is NOT prefixed by a hash or question mark.
  • numbers
    • The default for this is true.  If set to false, numbers in query strings will NOT be parsed.

View more information about this plugin here: JQuery.query

Download this plugin here: Download Page

Monday, October 17, 2011

Behold the wonder of jQTouch!

I had the pleasure of working with some jQTouch this past week while integrating a mobile site into our CMS.

jQTouch is, boiled down to basics, a simple framework for creating a mobile site that, when viewed on a phone, looks and feels like a native app. The site I was working with was more or less designed with iPhone-like interface elements, but it was all HTML and CSS, no native phone code whatsoever.

Creating a simple site is easy. First, we create the jQTouch object:

// Create jQTouch object
var jqt = new $.jQTouch(
{
 addGlossToIcon: false,
 statusBar: 'black',
 preloadImages: [
   '/images/mobile/back_button_tapped.png',
   '/images/mobile/back_button_tapped@2x.png',
   '/images/mobile/button_tapped.png',
   '/images/mobile/button_tapped@2x.png'
 ]
});

Some things you can set in this include class selectors that trigger navigation events (like "back", "cancel", "submit"), preloading assets so that they display quicker (as seen above), animation options, "app" options like home screen icons, etc. See a whole list here: https://github.com/senchalabs/jQTouch/wiki/initoptions

The jQTouch object is used throughout your code after it is made primarily for history navigation, but you can also add custom animations. Making a custom animation involves some CSS3 transitions, but we're not going to get into that here.

Let's talk about how a basic jQTouch page is set up...
    
<html>
    <head>
        <meta charset="UTF-8" />
        <title>jQTouch demo</title>
        <script type="text/javascript" src="//www.google.com/jsapi"></script>
        <script type="text/javascript"> google.load('jquery', '1.4.2'); </script>
        <script type="text/javascript" src="js/jqtouch.min.js"></script>
    </head>
    <body>
        <div id="jqt">
            <div id="home">
             <div class="toolbar">
                 <h1>Tech demo</h1>
                </div>
                <a href="#test" class="slideUp">Test</a><br /><br />
                <a href="#test2" class="flip">Test2</a><br /><br />
                <a href="#ajax" class="slideLeft">Let's try some AJAX</a>
            </div>  
            <div id="test">
         <div class="toolbar">
                    <h1>Tech demo</h1>
                    <a href="#" class="back">Go back</a>
                </div>  
                <div class="container">
                    OMG!
                </div>
            </div>
            <div id="test2">
            <div class="toolbar">
                    <h1>Tech demo</h1>
                    <a href="#" class="back">Go back</a>
                </div>             
                <div class="container">
                Hello!<br />
                </div>                
            </div>
            <div id="ajax">
             <div class="toolbar">
                    <h1>Tech demo</h1>
                    <a href="#" class="back">Go back</a>
                </div> 
                <div>Put data here: <br /></div>               
                <div class="container">
             Waiting...        
                </div>
            </div>
        </div> 
    </body>
</html>

Brutally simple, it consists of a container called "jqt", in which you put a div for every "screen" your "app" will have.

Animations are specified through classes. Some animations that exist by default are:
  1. slide (Normal links, by default, use the "slide" animation.)
  2. flip
  3. slideUp
  4. swap
  5. pop
  6. dissolve
  7. cube
  8. fade
Most of the stuff we do requires sending queries and transforming blocks of XML from data sources, but everything is on one page, so you have to resort to ajax to grab your data. Let's look at an example of how we'd do an ajax call to load some data.

$(function() {
    
 $("#ajax").bind("pageAnimationStart", function() {
  $("#ajax .container").load("test.txt");
 });
    
});

Looks easy enough, right?

We're still missing something. "pageAnimationStart" is an event that throws when you enter the page, AND when you exit the page. Luckily, you can pass some parameters into the empty function to grab the direction you are going. Then we just check for when the direction is "in" to only grab data when we are visiting the page.

$(function() {
    
 $("#ajax").bind("pageAnimationStart", function(e, info) {
                if (info.direction == 'in') {
          $("#ajax .container").load("test.txt");
                }
 });
    
});

Now we'll only get data once we visit the page, and avoid extra ajax calls, which can bog down mobile phones and use up their data. I'm using load in this example, but any sort of ajax function will work.

View this site live at http://www.nessthehero.com/junk/jqtdemo

Resources:

Monday, October 10, 2011

palateExpander.js Beta Release


This plugin gets instantiated on an un-ordered list (via an ID) with list items each containing an image and a link with a nested span. All of the class names and extra markup are dynamically injected. At the moment it only plays you tube videos, but it could easily be modified to handle the HTML5 video element. Each list item is initially detected by the script as being either a photo or video. They can be intermingled. CMS integration shouldn't be very difficult, and customization should be fairly easy.



In it's simplest implementation, you can just call it out without any params.


$('#theList').palateExpander();

The default param values are as follows:



$('#theList').palateExpander({

'size' : 'custom',

'randomStart' : 'yeah',

'carouselWidth' : 400,

'itemPadding' : 10,

'itemItemHeight' : 60,

'maxWidth' : 150,

'maxHeight' : 200,

'rowIncrement':5,

'maxPhotoHeight':400,

'videoHeight':259

});

Any combination of them can be passed. I like to think that they're fairly intuitive. I almost built an auto-rotation into it, but that seemed dumb. I haven't tested it at full screen yet...but why the hell would you want to do that anyway? Most importantly, you'll need to set the containing element to be position:relative; and give it a fixed width, because the script makes the targeted unordered list position:absolute to allow the expanded states to overlap other page elements. Obviously you will also not want to apply overflow:hidden; for exactly that same reason.