9 date 2008.08.10.15.02.40; author TWikiContributor; state Exp;
14 date 2008.08.03.22.17.48; author TWikiContributor; state Exp;
19 date 2008.01.22.03.21.31; author TWikiContributor; state Exp;
24 date 2007.01.16.04.11.59; author TWikiContributor; state Exp;
39 @%META:TOPICINFO{author="TWikiContributor" date="1183913691" format="1.1" reprev="1." version="4"}%
40 ---+!! !Behaviour Javascript framework Contrib
42 This contrib packages the third-party =Behaviour= Javascript event library, available from http://bennolan.com/behaviour/.
44 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).
46 %TOC{title="On this page:"}%
52 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.
54 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.
56 That's why I came up with Behaviour - my solution to unobtrusive javascript behaviours.
60 Behaviour lets you use CSS selectors to specify elements to add javascript events to. This means that instead of writing:
64 <a onclick="this.parentNode.removeChild(this)" href="#">
75 <a href="/someurl">Click me to delete me</a>
80 And then use css selectors to select that element and add javascript functions to it.
84 '#example li' : function(el){
85 el.onclick = function(){
86 this.parentNode.removeChild(this);
92 Behaviour.register(myrules);
99 Include the javascript file:
103 <script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/BehaviourContrib/behaviour.js"></script>
107 In your code you create a "rules" object, with sub-objects for each html element class name or id:
111 '.classname' : function(element) {
113 element.onclick = function() {
118 '#id' : function(element) {
120 element.onclick = function() {
127 Or use nested identifiers:
131 '.menu li a' : function(element) {
132 element.onclick = function() {
140 Apply the rules with:
144 Behaviour.register(myrules);
151 If we have a 'normal' link to TWiki Web hometopic: [[%TWIKIWEB%.%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.
155 <div id="demoblock" style="padding:1em; width:100px; text-align:center;">
159 <script type="text/javascript">
162 '#demoblock' : function(el) {
163 var defaultColor = '#A3D6F8';
164 var highlightColor = '#4A7FB5';
166 el.style.backgroundColor = defaultColor;
168 el.onmouseover = function() {
169 this.style.backgroundColor = highlightColor;
172 el.onmouseout = function() {
173 this.style.backgroundColor = defaultColor;
177 '#demoblock span' : function(el) {
179 var text = el.innerHTML;
181 var fisherYates = function (inArray) {
182 var i = inArray.length;
183 if ( i == 0 ) return false;
185 var j = Math.floor( Math.random() * ( i + 1 ) );
186 var tempi = inArray[i];
187 var tempj = inArray[j];
193 var randomize = function(inText) {
194 var letters = inText.split('');
195 fisherYates(letters);
196 return letters.join('');
198 el.onmouseover = function() {
199 this.innerHTML = randomize(text);
202 el.onmouseout = function() {
203 this.innerHTML = text;
208 Behaviour.register(myrules);
215 <div id="demoblock" style="padding:1em; width:150px; text-align:center;">
216 <span>MOUSE OVER ME</span>
219 <script type="text/javascript">
222 '#demoblock' : function(el) {
223 var defaultColor = '#A3D6F8';
224 var highlightColor = '#4A7FB5';
226 el.style.backgroundColor = defaultColor;
228 el.onmouseover = function() {
229 this.style.backgroundColor = highlightColor;
232 el.onmouseout = function() {
233 this.style.backgroundColor = defaultColor;
237 '#demoblock span' : function(el) {
239 var text = el.innerHTML;
241 var fisherYates = function (inArray) {
242 var i = inArray.length;
243 if ( i == 0 ) return false;
245 var j = Math.floor( Math.random() * ( i + 1 ) );
246 var tempi = inArray[i];
247 var tempj = inArray[j];
253 var randomize = function(inText) {
254 var letters = inText.split('');
255 fisherYates(letters);
256 return letters.join('');
258 el.onmouseover = function() {
259 this.innerHTML = randomize(text);
262 el.onmouseout = function() {
263 this.innerHTML = text;
268 Behaviour.register(myrules);
273 ---+++ Leaking danger
274 Behaviour code leaks memory on Windows Explorer prior to version 7. To prevent leaking, set the element variable to =null=:
278 'table.test td' : function(element) {
279 element.onmouseover = function() {
280 this.style.backgroundColor = highlightColor;
283 element = null; // by setting this IE will not leak
286 Behaviour.register(myrules);
292 * [[http://groups.google.com/group/behaviour][Google Groups: Behaviour Javascript Library]]
293 * [[http://www.nabble.com/Behaviour-Javascript-Library-f16264.html][Nabble - Behaviour Javascript Library forum & mailing list archive]]
294 * [[http://groups.google.com/group/behaviour/browse_thread/thread/e9828f9fdb482ac1/8ca704730053e23f?#8ca704730053e23f][Behaviour2]] - update in the making, since 2006
298 Behaviour is freely distributable under the terms of an BSD license.
299 For details see the Behaviour website.
304 * [[http://bennolan.com/behaviour/][Behaviour website]]
305 * [[http://groups.google.com/group/behaviour][Behaviour Google Group]]
307 ---++ Installation Instructions
308 You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server where TWiki is running.
310 Like many other TWiki extensions, this module is shipped with a fully
311 automatic installer script written using the Build<nop>Contrib.
312 * If you have TWiki 4.2 or later, you can install from the =configure= interface (Go to Plugins->Find More Extensions)
313 * See the [[http://twiki.org/cgi-bin/view/Plugins/BuildContribInstallationSupplement][installation supplement]] on TWiki.org for more information.
314 * If you have any problems, then you can still install manually from the command-line:
315 1 Download one of the =.zip= or =.tgz= archives
316 1 Unpack the archive in the root directory of your TWiki installation.
317 1 Run the installer script ( =perl <module>_installer= )
318 1 Run =configure= and enable the module, if it is a plugin.
319 1 Repeat for any missing dependencies.
320 * If you are *still* having problems, then instead of running the installer script:
321 1 Make sure that the file permissions allow the webserver user to access all files.
322 1 Check in any installed files that have existing =,v= files in your existing install (take care *not* to lock the files when you check in)
323 1 Manually edit !LocalSite.cfg to set any configuration variables.
325 %IF{"defined 'SYSTEMWEB'" else="<div class='twikiAlert'>%X% WARNING: SYSTEMWEB is not defined in this TWiki. Please add these definitions to your %MAINWEB%.TWikiPreferences, if they are not already there:<br><pre> * <nop>Set SYSTEMWEB = %<nop>TWIKIWEB%<br> * <nop>Set USERSWEB = %<nop>MAINWEB%</pre></div>"}%
328 ---++ Contrib Settings
329 * Set SHORTDESCRIPTION = =Behaviour= Javascript event library to create javascript based interactions that degrade well when javascript is not available
331 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.
334 | Author: | TWiki:Main.ArthurClemens |
335 | Copyright: | Code: =behaviour.js= version 1.1 - Copyright (c) Ben Nolan and Simon Willison. TWiki distribution and updates/additions: TWiki:Main.ArthurClemens. |
337 | Version: | 15675 (11 Aug 2008) |
338 | Dependencies: | None |
339 | Contrib Version: | 1.3.1 |
340 | Change History: | <!-- specify latest version first --> |
341 | 17 Oct 2007 | 1.3 Replaced "faster code" by other code from Dean Edwards, [[ packed by http://groups.google.com/group/behaviour/browse_thread/thread/85137977bedf5ed/3cf3ba8065d41a8c#3cf3ba8065d41a8c][Raymond Irving]]. |
342 | 02 Jul 2007 | 1.2 Integrated other faster code by Dean Edwards: [[http://dean.edwards.name/weblog/2006/06/again/][faster onload (again)]]. |
343 | 08 Mar 2007 | 1.1 Integrated code by Dean Edwards (see [[#CodeUpdate][Code update version 1.1 with faster DOM queries]]). |
344 | 04 Jun 2006 | 1.0 First Version. Included Behaviour version: 1.1. |
345 | Home: | http://TWiki.org/cgi-bin/view/Plugins/%TOPIC% |
346 | Feedback: | http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev |
347 | Appraisal: | http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Appraisal |
349 __Related Topics:__ %TWIKIWEB%.TWikiPreferences
351 %META:FILEATTACHMENT{name="behaviour.js" attr="" autoattached="1" comment="" date="1162075796" path="behaviour.compressed.js" size="2902" user="UnknownUser" version=""}%
363 %META:TOPICINFO{author="TWikiContributor" date="1183913691" format="1.1" reprev="1." version="3"}%
366 | Version: | 15675 (04 Aug 2008) |
377 %META:TOPICINFO{author="TWikiContributor" date="1183913691" format="1.1" reprev="1." version="2"}%
380 | Version: | 15675 (22 Jan 2008) |
391 %META:TOPICINFO{author="TWikiContributor" date="1162394433" format="1.1" version="1"}%
394 Behaviour is suited to create javascript based interaction that degrades well when javascript is not available.
397 Javascript file: [[%PUBURL%/%TWIKIWEB%/%TOPIC%/behaviour.js][behaviour.js]] (8.1K). The [[%PUBURL%/%TWIKIWEB%/%TOPIC%/behaviour.compressed.js][compressed javascript file]] (2.9K) has been processed by [[http://alex.dojotoolkit.org/shrinksafe/][ShrinkSafe]].
399 %TOC{title="On this page:"}%
403 <script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/BehaviourContrib/behaviour.compressed.js"></script>
409 <span class="link%TWIKIWEB%%HOMETOPIC%">[[%TWIKIWEB%.%HOMETOPIC%][TWiki Web Home]]</span>
412 '.link%TWIKIWEB%%HOMETOPIC% a' : function(el){
413 el.onclick = function() {
414 // open in a popup with no other attributes than template 'viewplain'
415 launchTheWindow(this.href,null,null,null,"viewplain");
419 The class name =link%<nop>TWIKIWEB%%<nop>HOMETOPIC%= will get expanded to =link%TWIKIWEB%%HOMETOPIC%=
424 <span class="link%TWIKIWEB%%HOMETOPIC%">[[%TWIKIWEB%.%HOMETOPIC%][TWiki Web Home]]</span>
427 '.link%TWIKIWEB%%HOMETOPIC% a' : function(el){
428 el.onclick = function() {
429 // open in a popup with no other attributes than template 'viewplain'
430 launchTheWindow(this.href,null,null,null,"viewplain");
436 * [[http://dean.edwards.name/weblog/2006/03/faster/][Dean Edwards: Faster DOM Queries]] - with a speed-up hack to Behaviour
439 For details, see the Behaviour website.
446 | Author: | TWiki:Main.ArthurClemens |
447 | Copyright: | version 1.1 - Copyright (c) Ben Nolan and Simon Willison |
449 | Dependencies: | None |
450 | 4 June 2006| 1.000 First Version. Included Behaviour version: 1.1 |
453 %META:FILEATTACHMENT{name="behaviour.compressed.js" attr="" autoattached="1" comment="" date="1162075796" path="behaviour.compressed.js" size="2902" user="UnknownUser" version=""}%
454 %META:FILEATTACHMENT{name="behaviour.js" attr="" autoattached="1" comment="" date="1161199153" path="behaviour.js" size="8149" user="UnknownUser" version=""}%