HTML5 Zone is brought to you in partnership with:

Paul is a principal consultant at ThoughtWorks. He is enthusiastic about open source in particular. He is known for Dependency Injection (one of its pioneers with PicoContainer), Selenium browser automation (co-founder), Branch by Abstraction and most recently Client-Side MVC frameworks. Paul is a DZone MVB and is not an employee of DZone and has posted 73 posts at DZone. You can read more from them at their website. View Full User Profile

A better Slideshow App, using Angular

03.08.2013
| 2282 views |
  • submit to reddit

My colleague Stephane Bisson has a better app than the one ThoughtWorkers and friends made a month ago. The source for Stephane’s project ‘prez’ is on GitHub and the running app is here though you should hit the left or right arrow to get it working. It does not yet do Mobile device swipe left/right. He says he’ll “accept pull requests” to get it that working.

Trying to do the same with Angular

What Stephane did in HTML is invent a element ‘slide’ that he uses like a div. Yes that’s a non-standard element – don’t try it on older browsers. Indeed the modern browsers process this like a div, with CSS transparently adding styling. Angular, via directive can be told to intercept the <slide> element and do the right thing with it – namely show one slide at a time.

Whereas the app we made a month ago, has some repeat-yourself attributes per slide:

<!-- these two rows repeat per slide -->
<tr id="2" ng-show="currentSlide == 2">
    <td>
        <img ngm-swiperight="nav(1)" ngm-swipeleft="nav(3)" 
              ng-style="imgStyle" src="angular_slide_2.svg"/>
    </td>
</tr>
<tr ng-show="currentSlide == 2 && !isTouchScreen" style="display:none;">
    <td>
        <div class="navitems">
            <a ng-click="nav(1)">back</a>
            <a class="right" ng-click="nav(3)">next</a>
        </div>
    </td>
</tr>

Stephane’s slideshow does not have boiler-plate slide html that the above has. See here:

<slide>
    <h1>dsl</h1>
    <p>This slide shows the dsl. The deck and slide tags.</p>
</slide>

Stephane’s handles left-arrow-key and right-arrow key without having to encode that per slide, and also the implicit show/hide logic per slide. All that is in the code behind the directive that underpins <slide> from Angular’s point of view. Much less HTML.

Under the hood.

Inside the main app.js script, there’s a directive:

angularModule.directive('slide', function (deck) {
    return {
        restrict: 'E',
        link: function ($scope, $element) {
            $element.hide();
            deck.add($element);
        }
    };
})

There’s much more to the app’s JavaScript around ‘deck’ and next/back processing that is shown in that snippet. That makes for more JavaScript than our example from a month ago. But the result is much terser and more elegant HTML.

Put another way, Stephane’s example has less view logic, more controller logic and about the same amount of model properties (referring to MVC of course). With four slides it’s a wash as to while os more code. With 50 slides Stephane’s wins by a wide margin.

I’m reminded of something.

Sami Lempinen coded a simple slideshow technology ‘XSLies’ some 14 years ago using XML and XSL. All that was done on the server side, and the resulting multi-page HTML was shipped to the client side. It was cool stuff in it’s day, and the terse markup language was much the same. Sadly Sami has not been a good historian for his own URLs, so here’s what’s left of it on a third-party site

Making the older slideshow terser too.

Andrew Dean and a few at the Dallas Geeknight meetup went on to make some changes to make that version a bit more DRY too. Less boilerplate via a single navigation row that adapts to the ‘current’ slide:

<tr ng-show="!isTouchScreen" style="display: none;">
    <td>
        <div class="navitems">
            <a ng-click="nav(currentSlide - 1)" 
               ng-show="currentSlide > 1">back</a>
            <a class="right" ng-click="nav(currentSlide + 1)" 
               ng-show="currentSlide < 4">next</a></div>
    </td>    
</tr>

Here is their fork with their improvements.

How does Google see it ?

Specifically in the search results, and the preview-panel?

In the search results:

The preview bot’s screen shot:

Not quite what we were hoping for :(



Published at DZone with permission of Paul Hammant, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)