Archive for the ‘jQuery’ Category

This page is obsolete (it uses jQuery UI 1.5). Please see the updated page.

Avoiding Bloat in Widgets

A while back, Justin Palmer wrote an excellent article on "Avoiding Bloat in Widgets." The basic premise (no suprise to anyone who's ever dealt with object-oriented programming) is that your widgets should not do everything possible; they should do one thing well but be flexible enough to allow others to modify them.

He describes two ways of extending objects: subclassing and aspect-oriented programming (AOP). Subclassing creates new classes, while AOP modfies the methods of a single object. Both can be useful.

Continue reading ‘Extending jQuery UI Widgets’ »

This page is obsolete (it uses jQuery UI 1.5). Please see the updated page.

This was written largely to help me make sense of using UI to create my own widgets, but I hope it may help others. "Widget" to me means a user-interface element, like a button or something more complicated like a popup date picker, but in jQuery UI terms it means a class, members of which are associated with HTML elements; things like Draggable and Sortable. In fact, not everything that I would have called a widget uses $.widget; the UI datepicker does not.

Continue reading ‘jQuery UI Widgets’ »

Unfortunately, only the administrator seems to be able to use unfiltered HTML.

I also needed to use the Text Control plugin and set formatting to "No Formatting" to prevent escaping the single quotes. But it seems to work.

The Using Javascript page in the Codex says to include common javascript files in your header.php file, but that means re-writing it whenever you change themes. I created a simple plugin that includes jQuery and ui.jquery on each page; it's

<?php
/*
Plugin Name: jQuery
Plugin URI: http://bililite.nfshost.com/blog/
Description: Includes the jQuery javascript framework (and the ui.jquery plugins) in your page, with some styling for <pre> elements
Author: Daniel Wachsstock
Version: 1.0
Author URI: http://bililite.nfshost.com/blog/
*/ 

function jQuery(){
echo '
   <script type="text/javascript" src="http://www.google.com/jsapi?key=apikey"></script>
   <script type="text/javascript">google.load("jquery","1");</script>
   <script type="text/javascript" src="http://ui.jquery.com/js/ui.js" ></script>
   <style>
      pre {
        background: #F4F4F4;
        border: 1px solid #B2B2B2;
        overflow: scroll;
      }
    </style>';
}

add_action('wp_head', 'jQuery');

?>

Replace apikey with your own Google API key.

My brother sent me a "salary survey" with one question, "Are you happy with your current salary," where the "No" button ran away from the mouse. I thought it might be useful for gag sites, so I created a jquery plugin that mimics that effect, at http://bililite.nfshost.com/blog/blogfiles/keepaway.html. It's easy enough to use, just $(selector).keepaway(options) ,with options of
  • jump: the distance to jump away from the mouse, in pixels; default 500
  • speed: the speed to move (passed intact to the animate plugin); default 'fast'
  • home: time to return to the starting position if nothing happens, in ms; default 1000
Have fun!

Please note: the CSS parser is complete and documented in this post
I had an idea to write a CSS parser to allow custom jQuery attributes, since a lot of the jQuery I use is more presentation than behavior, and the redesign of the YI website allows for switching stylesheets, which may require different plugins to be applied. My idea would be to allow something like:

div {
-jquery-round: 5;
-jquery-gradient: {from: '#008800', to: '#000088'}
}

But I never did anything about until Andy Kent developed JSS which does something similar for CSS selectors that a browser can’t handle but that jQuery can. His code also includes a cache for selectors that implements partial selector caching (thus div span img would also search the cache for div and div span as well). I emailed him back and forth a few times, and made some suggestions; my best css parser and selector cache are available here (look at the code and use Firebug to look at the console). I still haven’t got the custom jQuery stuff together yet, but the pieces may be useful right now.
Please note: the CSS parser is complete and documented in this post

I just uploaded the pictures for the annual dinner. Rabbi Shulman is a brilliant and inspiring speaker (confirming my first impressions when he came here). He discussed how a navi (prophet) always spoke in his unique voice yet never added to the unchanging truth of the Torah, and that is the same way we all must look at Judaism, and he tied it in with the dinner theme of Honoring our Past, Building our Future. I'm looking forward to hearing him weekly. The photo page on the website is one that I'm particularly proud of. The cross-fading code is based on Image Cross Fade Redux and jQuery slideshow (whose website seems to have disappeared) with some enhancements. The whole thing is 39 lines long (jQuery rocks!). The markup is simple: a div with contained images, which I get from Flickr and are wrapped in an <A> element to the original image:
<div class="make_fader">
  <a href="image link 1"><img src="image file 1/></a>
  <a href="image link 2"><img src="image file 2/></a>
  <a href="image link 3"><img src="image file 3/></a>
  <a href="image link 4"><img src="image file 4/></a>
</div>
That's as unobtrusive as it can get! The CSS uses absolute positioning to stack all the images on top of one another:
div.make_fader {
  position:relative;
  height: 5em;
}
div.make_fader img {
  position:absolute;
  height: 5em;
}
So without javascript it only shows the last picture; the rest are hidden behind. If you want to show them all with scripting off, don't make the images position: absolute in the CSS; do it in the script as: $('make_fader img').css('position', 'absolute'); The container needs the height set; if all its contents are position: absolute it would otherwise shrink to nothing. The only actual code you need after including the cross fader code is $('.make_fader').xfade(); and it starts fading! Options are, as usual for jQuery plugins, in the form of an object as the first argument: $('.make_fader').xfade({pause: 100, random: 4000, fadeIn: true}); Options include:
  • pause: {Number} minimum number of ms to show the picture. Default: 1000.
  • random: {Number} 0 to this number will randomly be added to pause. Default 0. This keeps all the faders on a page from fading at the same time.
  • fadeTime: {Number} ms for the fade effect. Default: 2000.
  • fadeIn: {Boolean} true to have the images start invisible and fade in. Default: false;
The random is very useful because with multiple faders on one page, having them all fade in and out at the same time looks bad. fadeIn makes the images fade into view rather than starting out visible. The plugin creates an object that is attached to the div that can be used to control the fading.
var xfader = $('.make_fader')[0].xfade;
xfader.stop(); // stop fading. The current image will fade in completely before the fading stops
xfade.start(); // restart fading
xfade.isStopped(); // returns true if the fader is stopped
Enjoy!

Updated: I forgot the '.' before the class name in the code to start fading (was $('make-fader') , should have been $('.make-fader') ). Apologies to all who tried to make it work!