<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Dev411 Blog: Category dhtml</title>
  <subtitle type="html">John Wang on Technology</subtitle>
  <id>tag:www.dev411.com,2005:Typo</id>
  <generator uri="http://www.typosphere.org" version="4.0">Typo</generator>
  <link href="http://www.dev411.com/blog/xml/atom/category/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.dev411.com/blog/tag/dhtml" rel="alternate" type="text/html"/>
  <updated>2007-06-16T12:30:25-05:00</updated>
  <entry>
    <author>
      <name>John Wang</name>
    </author>
    <id>urn:uuid:f3906b2c-8ae7-4b4c-a5b6-bc1bd25f1746</id>
    <published>2007-02-05T18:28:00-06:00</published>
    <updated>2007-06-16T12:30:25-05:00</updated>
    <title type="html">Displaying Dates and Times Using JavaScript</title>
    <link href="http://www.dev411.com/blog/2007/02/05/displaying-dates-and-times-using-javascript" rel="alternate" type="text/html"/>
    <category term="scalability" scheme="http://www.dev411.com/blog/tag/scalability" label="scalability"/>
    <category term="typo" scheme="http://www.dev411.com/blog/tag/typo" label="typo"/>
    <category term="javascript" scheme="http://www.dev411.com/blog/tag/javascript" label="javascript"/>
    <category term="dhtml" scheme="http://www.dev411.com/blog/tag/dhtml" label="dhtml"/>
    <category term="datetime" scheme="http://www.dev411.com/blog/tag/datetime" label="datetime"/>
    <summary type="html">&lt;p&gt;Some considerations when displaying dates and times on a website include showing delta times, customized timezones and caching. Often it's nice to show a delta time like "10 minutes ago" or "5 days ago" to give readers a frame of reference instead of an absolute date. When the date is far enough in the past and an absolute date becomes desired, customizing the date to the user's timezone is useful. And if your site grows large enough that caching becomes useful, finding a way to display customized deltas and timezone information in a cacheable static page becomes an ideal solution.&lt;/p&gt;</summary>
    <content type="html">&lt;p&gt;Some considerations when displaying dates and times on a website include showing delta times, customized timezones and caching. Often it's nice to show a delta time like "10 minutes ago" or "5 days ago" to give readers a frame of reference instead of an absolute date. When the date is far enough in the past and an absolute date becomes desired, customizing the date to the user's timezone is useful. And if your site grows large enough that caching becomes useful, finding a way to display customized deltas and timezone information in a cacheable static page becomes an ideal solution.&lt;/p&gt;

&lt;p&gt;JavaScript is an ideal solution for all three issues. With JavaScript you can place an absolute date in the web page and have the JS dynamically update it when the page is loaded. This can be used to calculate delta times and accommodate timezones as well. The result is that the page can embed the same date every time and thus becomes more cache-friendly.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://typosphere.org"&gt;Typo&lt;/a&gt; blog engine (which runs this blog) comes with a useful MIT-licensed JavaScript in it's &lt;a href="http://trac.typosphere.org/browser/trunk/public/javascripts/typo.js"&gt;typo.js&lt;/a&gt; script. Just copy three of the JS date/time functions, wrap your dates with spans (using the appropriate class name and absolute date in the span title) and then call &lt;span class="fix"&gt;show_dates_as_local_time()&lt;/span&gt; when your page is finished loading. The two other functions you'll need are &lt;span class="fix"&gt;get_local_time_for_date(time)&lt;/span&gt; and &lt;span class="fix"&gt;distance_of_time_in_words(minutes)&lt;/span&gt;. This is what I did for &lt;a href="http://planet.catalystframework.org/"&gt;Planet Catalyst&lt;/a&gt;'s &lt;a href="http://plagger.org"&gt;Plagger&lt;/a&gt; theme a while back.&lt;/p&gt;

&lt;p&gt;Although it's pretty easy to accommodate timezones, the Typo script doesn't do that. I've done this for some projects and might post some code in the future but it's not hard.&lt;/p&gt;

&lt;p&gt;Customization and cacheability, two great advantages for using JavaScript to handle dates and times.&lt;/p&gt;</content>
  </entry>
  <entry>
    <author>
      <name>John Wang</name>
    </author>
    <id>urn:uuid:4062f3dbcec251fe52a4d8393d7f1511</id>
    <published>2006-07-12T01:48:00-05:00</published>
    <updated>2007-06-16T12:30:22-05:00</updated>
    <title type="html">Scriptaculous article slider for Typo themes</title>
    <link href="http://www.dev411.com/blog/2006/07/12/scriptaculous-article-slider-for-typo-themes" rel="alternate" type="text/html"/>
    <category term="typo" scheme="http://www.dev411.com/blog/tag/typo" label="typo"/>
    <category term="hacks" scheme="http://www.dev411.com/blog/tag/hacks" label="hacks"/>
    <category term="dhtml" scheme="http://www.dev411.com/blog/tag/dhtml" label="dhtml"/>
    <category term="scriptaculous" scheme="http://www.dev411.com/blog/tag/scriptaculous" label="scriptaculous"/>
    <summary type="html">&lt;p&gt;Several Typo themes have nice DHTML effects and I thought it would be useful to extract some of these for use in other themes. In particular I am interested in adding &lt;a href="http://www.typogarden.org/articles/2005/11/16/modernist-theme"&gt;Modernist&lt;/a&gt;'s DHTML &lt;a href="http://script.aculo.us"&gt;Scriptaculous&lt;/a&gt; ScrollTo article slider to my modded Azure theme. You can see this in action at &lt;a href="http://poocs.net"&gt;poocs.net&lt;/a&gt; which is currently running Modernist. This article covers the steps necessary to repurpose the DHTML article slider and involves templates, CSS and images. I've shown how Modernist does it and, where applicable, how I prefer to do it. It turns out to be pretty easy.&lt;/p&gt;

&lt;p&gt;I've added this feature to my test system, however it's not here yet because I've decided to create a custom Dev411_Azure theme to handle the views (and controller/helper patches) for the growing number of mods I'm using rather than to continue manually hacking files under &lt;span class="fix"&gt;themes/azure&lt;/span&gt; and &lt;span class="fix"&gt;app&lt;/span&gt;. This should be here before long.&lt;/p&gt;</summary>
    <content type="html">&lt;p&gt;Several Typo themes have nice DHTML effects and I thought it would be useful to extract some of these for use in other themes. In particular I am interested in adding &lt;a href="http://www.typogarden.org/articles/2005/11/16/modernist-theme"&gt;Modernist&lt;/a&gt;'s DHTML &lt;a href="http://script.aculo.us"&gt;Scriptaculous&lt;/a&gt; ScrollTo article slider to my modded Azure theme. You can see this in action at &lt;a href="http://poocs.net"&gt;poocs.net&lt;/a&gt; which is currently running Modernist. This article covers the steps necessary to repurpose the DHTML article slider and involves templates, CSS and images. I've shown how Modernist does it and, where applicable, how I prefer to do it. It turns out to be pretty easy.&lt;/p&gt;

&lt;p&gt;I've added this feature to my test system, however it's not here yet because I've decided to create a custom Dev411_Azure theme to handle the views (and controller/helper patches) for the growing number of mods I'm using rather than to continue manually hacking files under &lt;span class="fix"&gt;themes/azure&lt;/span&gt; and &lt;span class="fix"&gt;app&lt;/span&gt;. This should be here before long.&lt;/p&gt;

&lt;p&gt;The things discussed here are the changes necessary to add the DHTML slider to a theme of your choice. For the files below the path part &lt;span class="fix"&gt;...&lt;/span&gt; should be along the lines of &lt;span class="fix"&gt;"themes/#{mytheme}"&lt;/span&gt; but can be simply &lt;span class="fix"&gt;app&lt;/span&gt; if you don't mind losing your changes when the admin console is used to change themes.&lt;/p&gt;

&lt;p&gt;I'll show where my preferences deviate from Modernist's implementation. My pet peeve is the use of the extra &lt;span class="fix"&gt;@ta&lt;/span&gt; instance varaible since it's unnecessary and the name isn't self-explanatory. I'll use &lt;span class="fix"&gt;each_with_index&lt;/span&gt; with &lt;span class="fix"&gt;article_index&lt;/span&gt; as the index name.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;.../views/articles/index.rhtml&lt;/strong&gt;: in order to know the which article is next, Modernist introduces a counter called &lt;span class="fix"&gt;@ta&lt;/span&gt; in the &lt;span class="fix"&gt;index.rhtml&lt;/span&gt; file. I'll probably name this something more intuitive like &lt;span class="fix"&gt;@article_index&lt;/span&gt; but here's the code from:
&lt;span class="fix"&gt;themes/modernist/views/articles/index.rhtml&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modernist:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;span class="highlight"&gt;&amp;lt;% @ta=0 %&amp;gt;&lt;/span&gt;
&amp;lt;% for article in @articles -%&amp;gt;
  &amp;lt;%= render_partial "article", article %&amp;gt;
  &lt;span class="highlight"&gt;&amp;lt;% @ta+=1 %&amp;gt;&lt;/span&gt;
&amp;lt;% end -%&amp;gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;My preference:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;span class="highlight"&gt;&amp;lt;% @articles.each_with_index do |article,article_index| -%&amp;gt;&lt;/span&gt;
  &amp;lt;%= render :partial =&amp;gt; "article",
             :object  =&amp;gt; article,
             :locals  =&amp;gt; { &lt;span class="highlight"&gt;:article_index =&amp;gt; article_index&lt;/span&gt; }
  %&amp;gt;
&amp;lt;% end -%&amp;gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;use &lt;span class="fix"&gt;each_with_index&lt;/span&gt; instead of creating an extra counter since the index is already available&lt;/il&gt;
&lt;li&gt;use &lt;span class="fix"&gt;render&lt;/span&gt; instead of &lt;span class="fix"&gt;render_partial&lt;/span&gt; just because the former was indicated as preferred on the &lt;span class="fix"&gt;#typo&lt;/span&gt; channel&lt;/li&gt;
&lt;li&gt;use self-explanatory variable names: &lt;span class="fix"&gt;article_index&lt;/span&gt; instead of &lt;span class="fix"&gt;ta&lt;/span&gt;
&lt;li&gt;&lt;p&gt;I would actually prefer to send the article object to the render method as a local variable since that's what it seems to be and I don't see a reason to treat it differently.&lt;/p&gt;

&lt;pre&gt;:locals =&gt; {
    :article =&gt; article
    :article_index =&gt; article_index
}&lt;/pre&gt;

&lt;p&gt;instead of as &lt;span class="fix"&gt;:object&lt;/span&gt; but I can't be sure that will always work since &lt;span class="fix"&gt;article&lt;/span&gt; is also the name of the template and there's some magic there.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;.../views/articles/_article.rhtml&lt;/strong&gt;: there are two pertinent changes in this file: (a) the Scriptaculous &lt;span class="fix"&gt;ScrollTo &lt;/span&gt; controls and (b) adding a DOM &lt;span class="fix"&gt;id&lt;/span&gt; to the article header that can be navigated to.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ScrollTo controls&lt;/strong&gt;: In the Modernist theme, the Scriptaculous controls are embedded directly in &lt;span class="fix"&gt;_article.rhtml&lt;/span&gt; as follows. Another option is to put it in a separate &lt;span class="fix"&gt;_article_flip.rhtml&lt;/span&gt; template include but I'm not sure if this will be automatically copied in by Typo's theme admin manager. In any event, the following should be added to the top of &lt;span class="fix"&gt;_article.rhtml&lt;/span&gt; either directly or as an include. Notice the innerHTML of the links below is a space. The links will be rendered as graphics once the CSS is enabled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modernist:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&amp;lt;div class="flip"&amp;gt;
&amp;lt;a href="#header" onclick="new Effect.ScrollTo('header'); 
  return false" class="flip-top"
  title="Scroll to the top"&amp;gt; &amp;lt;/a&amp;gt;
&amp;lt;% if &lt;span class="highlight"&gt;@ta&lt;/span&gt; &amp;lt; (@articles.size-1) %&amp;gt;
&amp;lt;a href="#post-&amp;lt;%= &lt;span class="highlight"&gt;@ta&lt;/span&gt;+1 %&amp;gt;"
  onclick="new Effect.ScrollTo('post-&amp;lt;%= &lt;span class="highlight"&gt;@ta&lt;/span&gt;+1 %&amp;gt;');
  return false" class="flip-next"
  title="Scroll to the next post"&amp;gt; &amp;lt;/a&amp;gt;
&amp;lt;% end %&amp;gt;
&amp;lt;a href="#menu" onclick="new Effect.ScrollTo('menu');
  return false" class="flip-menu"
  title="Scroll to the menu"&amp;gt; &amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;

&lt;p&gt;The Modernist theme places the menu at the bottom of the page so having a control go to &lt;span class="fix"&gt;menu&lt;/span&gt; makes sense. For a more traditional theme like Azure, I added a &lt;span class="fix"&gt;&amp;lt;div id="bottom"&amp;gt;&lt;/span&gt; around &lt;span class="fix"&gt;&amp;lt;p id="pagination"&lt;/span&gt; to indicate the bottom and changed the last link above accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My preference:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Change the &lt;span class="fix"&gt;@ta&lt;/span&gt; references to &lt;span class="fix"&gt;article_index&lt;/span&gt; as above&lt;/li&gt;&lt;/ul&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;article anchor&lt;/strong&gt;: The next change is to add a DOM &lt;span class="fix"&gt;id&lt;/span&gt; with the article index to each article's title:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modernist:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&amp;lt;h3&lt;span class="highlight"&gt; id="post-&amp;lt;%= @ta %&amp;gt;"&lt;/span&gt;&amp;gt;
  &amp;lt;%= article_link article.title, article %&amp;gt;
  &amp;lt;%= author_link(article) %&amp;gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;My preference:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&amp;lt;h3&lt;span class="highlight"&gt; id="post-&amp;lt;%= article_index %&amp;gt;"&lt;/span&gt;&amp;gt;&lt;/pre&gt;


&lt;/ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt;: Copy the CSS definitions that begin with &lt;span class="fix"&gt;.flip&lt;/span&gt; to one of your CSS files:&lt;/p&gt;

&lt;pre&gt;.flip {
        float: left;
        position: absolute;
        opacity: 0.9;
        margin: 0 0 0 -65px;
}

.flip a {
        display: block;
        width: 25px;
        height: 16px;
        padding: 4px;
        background-position: center center;
        background-repeat: no-repeat;
}

.flip-menu { background-image:
  url('/images/theme/flip-bottom-0.gif'); }
.flip-next { background-image:
  url('/images/theme/flip-down-0.gif'); }
.flip-top { background-image:
  url('/images/theme/flip-top-0.gif'); }
.flip-comments { background-image:
  url('/images/theme/flip-comments-0.gif'); }
.flip-post { background-image:
  url('/images/theme/flip-post-0.gif'); }
.flip-menu:hover { background-image:
  url('/images/theme/flip-bottom-1.gif'); }
.flip-next:hover { background-image:
  url('/images/theme/flip-down-1.gif'); }
.flip-top:hover { background-image:
  url('/images/theme/flip-top-1.gif'); }
.flip-comments:hover { background-image:
  url('/images/theme/flip-comments-1.gif'); }
.flip-post:hover { background-image:
  url('/images/theme/flip-post-1.gif'); }&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;control images&lt;/strong&gt;: the last step is to copy the control images to the right location. These are the &lt;span class="fix"&gt;flip-post-[01].gif&lt;/span&gt; images referenced in the CSS. Copy these to the correct directory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it, you should be able to get up and running.&lt;/p&gt;
</content>
  </entry>
</feed>

