Behaviour Javascript Framework Contrib
Behaviour Javascript event library to create Javascript based interactions that degrade well when Javascript is not available
Introduction
This contrib packages the third-party Behaviour Javascript event library, available from http://bennolan.com/behaviour/.
Behaviour uses CSS selectors to subscribe to Javascript event handlers. This allows to create clean code, separated from HTML (and well suited to create Javascript based interaction that degrades nicely when Javascript is not available).
From the website:
After all the work of WASP and others to promote clean markup, valid pages and graceful degradation via css - it sucks that we're going back to tag soup days by throwing javascript tags into our html.
The better way to do javascript is to do it unobtrusively. PPK and Simon Willison have been recommending this approach for ages. And it's definitely the way to go. The only problem is that it's a bit of a pain in the ass.
That's why I came up with Behaviour - my solution to unobtrusive javascript behaviours.
How does it work?
Behaviour lets you use CSS selectors to specify elements to add javascript events to. This means that instead of writing:
<li>
<a onclick="this.parentNode.removeChild(this)" href="#">
Click me to delete me
</a>
</li>
You can use:
<ul id="example">
<li>
<a href="/someurl">Click me to delete me</a>
</li>
</ul>
And then use css selectors to select that element and add javascript functions to it.
var myrules = {
'#example li' : function(el){
el.onclick = function(){
this.parentNode.removeChild(this);
}
}
};
Behaviour.register(myrules);
Usage
Include the Javascript file:
<script type="text/javascript" src="%PUBURL%/%SYSTEMWEB%/BehaviourContrib/behaviour.js"></script>
In your code you create a "rules" object, with sub-objects for each html element class name or id:
var myrules = {
'.classname' : function(element) {
// element event
element.onclick = function() {
// code here
}
},
'#id' : function(element) {
// element event
element.onclick = function() {
// code here
}
}
};
Or use nested identifiers:
var myrules = {
'.menu li a' : function(element) {
element.onclick = function() {
// code here
}
}
};
Apply the rules with:
Behaviour.register(myrules);
Example
If we have a 'normal' link to TWiki Web hometopic: TWiki Web Home, we can use javascript to make it open a popup window. When javascript is not available the link behaviour defaults to opening the page in the current window.
<div id="demoblock" style="padding:1em; width:100px; text-align:center;">
MOUSE OVER ME
</div>
<script type="text/javascript">
// <![CDATA[
var myrules = {
'#demoblock' : function(el) {
var defaultColor = '#A3D6F8';
var highlightColor = '#4A7FB5';
el.style.backgroundColor = defaultColor;
el.onmouseover = function() {
this.style.backgroundColor = highlightColor;
return false;
}
el.onmouseout = function() {
this.style.backgroundColor = defaultColor;
return false;
}
},
'#demoblock span' : function(el) {
var text = el.innerHTML;
var fisherYates = function (inArray) {
var i = inArray.length;
if ( i == 0 ) return false;
while ( --i ) {
var j = Math.floor( Math.random() * ( i + 1 ) );
var tempi = inArray[i];
var tempj = inArray[j];
inArray[i] = tempj;
inArray[j] = tempi;
}
}
var randomize = function(inText) {
var letters = inText.split('');
fisherYates(letters);
return letters.join('');
}
el.onmouseover = function() {
this.innerHTML = randomize(text);
return false;
}
el.onmouseout = function() {
this.innerHTML = text;
return false;
}
}
};
Behaviour.register(myrules);
// ]]>
</script>
Creates:
MOUSE OVER ME
Leaking Danger
Behaviour code leaks memory on Windows Explorer prior to version 7. To prevent leaking, set the element variable to null :
var myrules = {
'table.test td' : function(element) {
element.onmouseover = function() {
this.style.backgroundColor = highlightColor;
return false;
}
element = null; // by setting this IE will not leak
}
};
Behaviour.register(myrules);
Development
License
Behaviour is freely distributable under the terms of an BSD license. For details see the Behaviour website.
Links
Installation Instructions
- For an automated installation, run the configure script and follow "Find More Extensions" in the in the Extensions section.
- Or, follow these manual installation steps:
- Download the ZIP file from the extension home on twiki.org (see below).
- Unzip
BehaviourContrib.zip in your twiki installation directory.
- Set the ownership of the extracted directories and files to the webserver user.
- Install the dependencies (if any).
Contrib Settings
- Set SHORTDESCRIPTION =
Behaviour Javascript event library to create Javascript based interactions that degrade well when Javascript is not available
You can also set the global TWiki variable BEHAVIOURCONTRIB_DEBUG to 1 to make the contrib use uncompressed javascript sources, in the event of problems.
Contrib Info
Related Topics: TWikiPreferences |