Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Sunday, 21 July 2013

jQuery UI Div Content Rotation

Changing Div HTML at Intervals

Recently we released a new look home page for the website and one feature we decided upon was a content rotation div so we could cycle through some blocks of content and display different things at a set interval.

If you have read some of our other articles you will know that we like to use jQuery as a JavaScript framework to provide solutions for some basic UI, Form and other functions.


jQuery UI


Loading jQuery

We like to use the Google CDN for hosting our jQuery libraries but you could use the jQuery CDN or even host your own but we would advise using a CDN as they will normally be more reliable and also better distributed.

As you should know it is better to add any external JavaScript file includes to the <head> section of your HTML document.

On a side note the script tags now have a new attribute async which informs the browser to load it asynchronously this will allow the page to continue loading. See here for more information.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

Adding the above to our HTML file gives us something like below.
<html>
  <head>
    <title>Example Template</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
  </head>
  <body>
  </body>
</html>

This is a good start and all we need to do now is add the div to the page and write the JavaScript responsible for rotating the div.


Adding the Div

Now we can add the div to the template this will be the container which holds the HTML to be rotated.
<html>
  <head>
    <title>Example Template</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
  </head>
  <body>

    <div id="rotate_wrapper">
      <h2>This is the original</h2>
      <p>Some example text</p>
    </div>
  </body>
</html>

Now we have a base to work from we can now add the JavaScript which is responseable for the actual content rotation.


JavaScript

Adding the JavaScript is simple and a lot of leg work has been done by the jQuery developers, Add the code block bellow inside the <head> tags. This defines our rotation array the idea is each element in the array has your HTML that you want the div to switch to at predefined intervals.
<script type="text/javascript">
  var rotateArray = new Array(
    '<h2>Example Heading One</h2><p>Short description</p>,
    '<h2>Example Heading Two</h2><p>small block of example</p>',
    '<h2>Example Heading Three</h2><p>another item</p>',
    '<h2>Example Heading Four</h2><p>Some example text</p>',
    '<h2>This is the original</h2><p>Some example text</p>'
  );
</script>

Now we have the content for our rotation div we need to add the code for switching it this code block should be just inside the closing </body> tag.
<script type="text/javascript">
var numItems = rotateArray.length;
var index = 0;
setInterval(function() {
    $("#rotate_wrapper").fadeOut("slow", function() { $(this).html(rotateArray[index]).fadeIn("slow"); });
    index = (index + 1) % numItems;
}, 4000);
-->
</script>

The code above defines the size of our content array and sets an index to zero next we use the setInterval() function to do the actual rotation the second parameter to this function is the millisecond between rotations for our needs 4000 was a good value.

Inside the setInterval() function we use jQuery to fade the div out then it uses the .html method to set the content of the div to the array element defined by index and then it will fade this new content in.

Once the actual transition has been completed we increment the index and divide by the size of the content array and then take the remainder from that as our next array element index.

The Finished Markup

Below is the code block containing the complete markup for our div rotation and it should be simple to modify and customise this to your needs.

We found jQuery to be good for these kind of use cases because as you can see just by writing a few lines of code we take advantage of the power of a framework.

This leads us to using a rapid release schedule and using rolling release to keep up with the pace of change. Our new home page was implemented in a short amount of time and we are now ready to make the next release for it which is just a small clean up and polish.
<html>
  <head>
    <title>Example Template</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script type="text/javascript">
    <!--
    var rotateArray = new Array(
      '<h2>Example Heading One</h2><p>Short description</p>',
      '<h2>Example Heading Two</h2><p>small block of example</p>',
      '<h2>Example Heading Three</h2><p>another item</p>',
      '<h2>Example Heading Four</h2><p>Some example text</p>',
      '<h2>This is the original</h2><p>Some example text</p>'
    );
    -->
    </script>
  </head>
  <body>
    <div id="rotate_wrapper">
      <h2>This is the original</h2>

      <p>Some example text</p>
    </div>
    <script type="text/javascript">
    <!--
    var numItems = rotateArray.length;
    var index = 0;
    setInterval(function() {
       $("#rotate_wrapper").fadeOut("slow", function() { $(this).html(rotateArray[index]).fadeIn("slow"); });
        index = (index + 1) % numItems;
    }, 4000);
    -->
    </script>
  </body>
</html>

You can see the complete working example script working by following this link I decided to host in on my personal website although I could have hosted it on our Google sites account or jsfiddle but this was easier.


Conclusion

As you can see from this article jQuery and jQuery UI can both make development of user interface a lot simpler and it makes sense to use a framework to do this heavy lifting because we all know its silly to reinvent the wheel.

The newly released home page on our website has been doing well and we are now in the process of adding support for oAuth2 login to our enhanced listings page to allow a more seamless integration of Google wallet payment support and making logins simpler for our users.

After this we will be adding DFP to our website to allow us to sell granular advertising with multiple ad networks. We will also be adding multiple payment methods to make it even easier for our customers to purchase advertising.

Sunday, 7 July 2013

News Publishing System

Care and Nursing Related News

We have been developing a news system that allows us to post care and nursing related articles direct from a browser using an editor similar to Libra Office. This gives our news posters complete control over their articles.

It also provides image support in terms of cover images for articles and images inside articles, we use AJAX (Asynchronous JavaScript and XML) to allow us to upload without refreshing the page and other various interactive functions.

We have also integrated into social media by using the Facebook opengraph and Twitter API allowing us to publish any articles directly to our social media pages allowing us to try and maximise our exposure.

In this article I will try and cover some of the development needed to implement the system from the ground up and any tools used along the way.


Screen shot of our news page with various article
News Page


Making Life Easy

Our news system takes advantage of jQuery to provide a JavaScript framework to help speed up development and provide solutions to some common tasks while developing a web site.

We have reviewed a few frameworks for client side JavaScript such as Dojo and we have also used a few plugins for jQuery to provide form handling functions for AJAX file upload, document publishing, saving, loading and reviewing.

One handy plugin we have used is jQuery Equalise which allows you to equalise the height of your divs in relation to the highest div this gives us a nice even layout. We did try to use just a left to right layout using divs but it looked to messy.

Another plugin which we use is jQuery UI which provides JavaScript effects, widgets and user interface specific functions. This allows us to provide tooltips for our forms as well as highlighting form fields to the user and shaking to get attention.




Logo saying jQuery write less, do more jquery framework
jQuery Framework

News Objects

The system we have developed was designed to make good use of OOP allowing us to reuse our code and reduce the amount we need to write.

Once the system has been release and we have fixed any release bugs found we will review all our classes and see if we can refactor any of them.

This is something we try to do regularly because our system changes over time we can usually move some classes up the hierarchy.

As well as these we have implemented a few common patterns such as the builder pattern and singleton pattern. We use the builder to create news article objects and the singleton allows us to only have a single instance of a class (internal).

We have also implemented the Iterator interface so we can use a builder to create objects and then iterate through them using a foreach loop.

For more reading on design patterns you could try and get a copy of Design Patterns element of reusable object-orientated software (ISBN-13: 978-0-201-63361-0) or find some online tutorials.


Picture of the cover of Design Patterns book
Design Patterns


Social Media

Integration into the popular social networks was accomplished by using the opengraph to interface with our Facebook timeline allowing us to post the article onto our timeline directly from the news article.

Twitter has also been integrated so we can tweet our articles directly when we publish them on The Care Homes Directory web site.

Unfortunately we could not implement Google+ integration because they only allow read access to their API so we cannot post to our stream.


A screen shot of my public profile on Google plus
Coffee


Testing Testing

We also developed some unit tests to allow us to automate some common tests for our objects and the system as a whole. We use PHPUnit for our tests as well as PHPDocumentor 2 to generate our documents using inline comments and header comments.

Each class has a corresponding test case which tries to test every possible path the code can follow and also checking for the correct output. We also try to fuzz our script because we find it helps with handling bad user input.

One thing we have learned over the years is that users are unpredictable and will use your program in ways you would never imagine so by throwing some bad inputs to it we can try and uncover these cases before our users do.

Along with these we will be developing some selenium UI tests to make sure we can test our interface as if we are a normal browser. We will be using it with PHPUnit for providing us with some comprehensive tests for our code.

We have written about how we approach testing before in an old article which you can read here if you are interested in our testing and QA process.


An image with lots of words written at random spots, words used are testing with phpunit
Testing



Conclusion

We hope our news system provides a useful service to our visitors and also any care providers who like to keep up to date with the news.

Once the system is stable and our first release is tagged we will be reviewing integrating news into our enhanced listing allowing care providers and home owners to publish any news and/or events directly to their listing.

Soon we will be publishing an article on the new feature release for our news system, RSS and Atom feeds, which were simple to implement and most of the work for this feature has already been done