data/TWiki/BehaviourContrib.txt,v
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/data/TWiki/BehaviourContrib.txt,v	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,417 @@
     1.4 +head	1.2;
     1.5 +access;
     1.6 +symbols;
     1.7 +locks; strict;
     1.8 +comment	@# @;
     1.9 +
    1.10 +
    1.11 +1.2
    1.12 +date	2008.01.22.03.21.31;	author TWikiContributor;	state Exp;
    1.13 +branches;
    1.14 +next	1.1;
    1.15 +
    1.16 +1.1
    1.17 +date	2007.01.16.04.11.59;	author TWikiContributor;	state Exp;
    1.18 +branches;
    1.19 +next	;
    1.20 +
    1.21 +
    1.22 +desc
    1.23 +@buildrelease
    1.24 +@
    1.25 +
    1.26 +
    1.27 +1.2
    1.28 +log
    1.29 +@buildrelease
    1.30 +@
    1.31 +text
    1.32 +@%META:TOPICINFO{author="TWikiContributor" date="1183913691" format="1.1" reprev="1." version="2"}%
    1.33 +---+!! !Behaviour Javascript framework Contrib
    1.34 +
    1.35 +This contrib packages the third-party =Behaviour= Javascript event library, available from http://bennolan.com/behaviour/.
    1.36 +
    1.37 +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).
    1.38 +
    1.39 +%TOC{title="On this page:"}%
    1.40 +
    1.41 +
    1.42 +---++ Introduction
    1.43 +From the website:
    1.44 +<blockquote>
    1.45 +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.
    1.46 +
    1.47 +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.
    1.48 +
    1.49 +That's why I came up with Behaviour - my solution to unobtrusive javascript behaviours.
    1.50 +
    1.51 +*How does it work?*
    1.52 +
    1.53 +Behaviour lets you use CSS selectors to specify elements to add javascript events to. This means that instead of writing:
    1.54 +
    1.55 +<verbatim>
    1.56 +<li>
    1.57 +	<a onclick="this.parentNode.removeChild(this)" href="#">
    1.58 +		Click me to delete me
    1.59 +	</a>
    1.60 +</li>
    1.61 +</verbatim>
    1.62 +
    1.63 +You can use:
    1.64 +
    1.65 +<verbatim>
    1.66 +<ul id="example">
    1.67 +	<li>
    1.68 +		<a href="/someurl">Click me to delete me</a>
    1.69 +	</li>
    1.70 +</ul>
    1.71 +</verbatim>
    1.72 +
    1.73 +And then use css selectors to select that element and add javascript functions to it.
    1.74 +
    1.75 +<verbatim>
    1.76 +var myrules = {
    1.77 +	'#example li' : function(el){
    1.78 +		el.onclick = function(){
    1.79 +			this.parentNode.removeChild(this);
    1.80 +
    1.81 +		}
    1.82 +	}
    1.83 +};
    1.84 +
    1.85 +Behaviour.register(myrules);
    1.86 +</verbatim>
    1.87 +</blockquote>
    1.88 +
    1.89 +
    1.90 +
    1.91 +---++ Usage
    1.92 +Include the javascript file:
    1.93 +
    1.94 +<blockquote>
    1.95 +<verbatim>
    1.96 +<script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/BehaviourContrib/behaviour.js"></script>
    1.97 +</verbatim>
    1.98 +</blockquote>
    1.99 +
   1.100 +In your code you create a "rules" object, with sub-objects for each html element class name or id:
   1.101 +<blockquote>
   1.102 +<verbatim>
   1.103 +var myrules = {
   1.104 +	'.classname' : function(element) {
   1.105 +		// element event
   1.106 +		element.onclick = function() {
   1.107 +			// code here
   1.108 +		}
   1.109 +	},
   1.110 +	
   1.111 +	'#id' : function(element) {
   1.112 +		// element event
   1.113 +		element.onclick = function() {
   1.114 +			// code here
   1.115 +		}
   1.116 +	}
   1.117 +};
   1.118 +</verbatim>
   1.119 +
   1.120 +Or use nested identifiers:
   1.121 +
   1.122 +<verbatim>
   1.123 +var myrules = {
   1.124 +	'.menu li a' : function(element) {
   1.125 +		element.onclick = function() {
   1.126 +			// code here
   1.127 +		}
   1.128 +	}
   1.129 +};
   1.130 +</verbatim>
   1.131 +</blockquote>
   1.132 +
   1.133 +Apply the rules with:
   1.134 +
   1.135 +<blockquote>
   1.136 +<verbatim>
   1.137 +Behaviour.register(myrules);
   1.138 +</verbatim>
   1.139 +</blockquote>
   1.140 +
   1.141 +
   1.142 +
   1.143 +---+++ Example
   1.144 +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.
   1.145 +
   1.146 +<blockquote>
   1.147 +<verbatim>
   1.148 +<div id="demoblock" style="padding:1em; width:100px; text-align:center;">
   1.149 +MOUSE OVER ME
   1.150 +</div>
   1.151 +
   1.152 +<script type="text/javascript">
   1.153 +// <![CDATA[
   1.154 +var myrules = {
   1.155 +	'#demoblock' : function(el) {
   1.156 +		var defaultColor = '#A3D6F8';
   1.157 +		var highlightColor = '#4A7FB5';
   1.158 +		
   1.159 +		el.style.backgroundColor = defaultColor;
   1.160 +		
   1.161 +		el.onmouseover = function() {
   1.162 +			this.style.backgroundColor = highlightColor;
   1.163 +			return false;
   1.164 +		}
   1.165 +		el.onmouseout = function() {
   1.166 +			this.style.backgroundColor = defaultColor;
   1.167 +			return false;
   1.168 +		}
   1.169 +	},
   1.170 +	'#demoblock span' : function(el) {
   1.171 +		
   1.172 +		var text = el.innerHTML;
   1.173 +		
   1.174 +		var fisherYates = function (inArray) {
   1.175 +		  var i = inArray.length;
   1.176 +		  if ( i == 0 ) return false;
   1.177 +		  while ( --i ) {
   1.178 +			 var j = Math.floor( Math.random() * ( i + 1 ) );
   1.179 +			 var tempi = inArray[i];
   1.180 +			 var tempj = inArray[j];
   1.181 +			 inArray[i] = tempj;
   1.182 +			 inArray[j] = tempi;
   1.183 +		   }
   1.184 +		}
   1.185 +		
   1.186 +		var randomize = function(inText) {
   1.187 +			var letters = inText.split('');
   1.188 +			fisherYates(letters);
   1.189 +			return letters.join('');
   1.190 +		}
   1.191 +		el.onmouseover = function() {
   1.192 +			this.innerHTML = randomize(text);
   1.193 +			return false;
   1.194 +		}
   1.195 +		el.onmouseout = function() {
   1.196 +			this.innerHTML = text;
   1.197 +			return false;
   1.198 +		}
   1.199 +	}
   1.200 +};
   1.201 +Behaviour.register(myrules);
   1.202 +// ]]>
   1.203 +</script>
   1.204 +</verbatim>
   1.205 +
   1.206 +Creates:
   1.207 +
   1.208 +<div id="demoblock" style="padding:1em; width:150px; text-align:center;">
   1.209 +<span>MOUSE OVER ME</span>
   1.210 +</div>
   1.211 +
   1.212 +<script type="text/javascript">
   1.213 +// <![CDATA[
   1.214 +var myrules = {
   1.215 +	'#demoblock' : function(el) {
   1.216 +		var defaultColor = '#A3D6F8';
   1.217 +		var highlightColor = '#4A7FB5';
   1.218 +		
   1.219 +		el.style.backgroundColor = defaultColor;
   1.220 +		
   1.221 +		el.onmouseover = function() {
   1.222 +			this.style.backgroundColor = highlightColor;
   1.223 +			return false;
   1.224 +		}
   1.225 +		el.onmouseout = function() {
   1.226 +			this.style.backgroundColor = defaultColor;
   1.227 +			return false;
   1.228 +		}
   1.229 +	},
   1.230 +	'#demoblock span' : function(el) {
   1.231 +		
   1.232 +		var text = el.innerHTML;
   1.233 +		
   1.234 +		var fisherYates = function (inArray) {
   1.235 +		  var i = inArray.length;
   1.236 +		  if ( i == 0 ) return false;
   1.237 +		  while ( --i ) {
   1.238 +			 var j = Math.floor( Math.random() * ( i + 1 ) );
   1.239 +			 var tempi = inArray[i];
   1.240 +			 var tempj = inArray[j];
   1.241 +			 inArray[i] = tempj;
   1.242 +			 inArray[j] = tempi;
   1.243 +		   }
   1.244 +		}
   1.245 +		
   1.246 +		var randomize = function(inText) {
   1.247 +			var letters = inText.split('');
   1.248 +			fisherYates(letters);
   1.249 +			return letters.join('');
   1.250 +		}
   1.251 +		el.onmouseover = function() {
   1.252 +			this.innerHTML = randomize(text);
   1.253 +			return false;
   1.254 +		}
   1.255 +		el.onmouseout = function() {
   1.256 +			this.innerHTML = text;
   1.257 +			return false;
   1.258 +		}
   1.259 +	}
   1.260 +};
   1.261 +Behaviour.register(myrules);
   1.262 +// ]]>
   1.263 +</script>
   1.264 +
   1.265 +
   1.266 +---+++ Leaking danger
   1.267 +Behaviour code leaks memory on Windows Explorer prior to version 7. To prevent leaking, set the element variable to =null=:
   1.268 +<blockquote>
   1.269 +<verbatim>
   1.270 +var myrules = {
   1.271 +	'table.test td' : function(element) {
   1.272 +		element.onmouseover = function() {
   1.273 +			this.style.backgroundColor = highlightColor;
   1.274 +			return false;
   1.275 +		}
   1.276 +		element = null; // by setting this IE will not leak  
   1.277 +	}
   1.278 +};	
   1.279 +Behaviour.register(myrules);
   1.280 +</verbatim>
   1.281 +</blockquote>
   1.282 +
   1.283 +
   1.284 +---++ Development
   1.285 +   * [[http://groups.google.com/group/behaviour][Google Groups: Behaviour Javascript Library]]
   1.286 +   * [[http://www.nabble.com/Behaviour-Javascript-Library-f16264.html][Nabble - Behaviour Javascript Library forum & mailing list archive]]
   1.287 +   * [[http://groups.google.com/group/behaviour/browse_thread/thread/e9828f9fdb482ac1/8ca704730053e23f?#8ca704730053e23f][Behaviour2]] - update in the making, since 2006
   1.288 +
   1.289 +
   1.290 +---++ License
   1.291 +Behaviour is freely distributable under the terms of an BSD license.
   1.292 +For details see the Behaviour website.
   1.293 +
   1.294 +
   1.295 +
   1.296 +---++ Links
   1.297 +   * [[http://bennolan.com/behaviour/][Behaviour website]]
   1.298 +   * [[http://groups.google.com/group/behaviour][Behaviour Google Group]]
   1.299 +
   1.300 +---++ Installation Instructions
   1.301 +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.
   1.302 +
   1.303 +Like many other TWiki extensions, this module is shipped with a fully
   1.304 +automatic installer script written using the Build<nop>Contrib.
   1.305 +   * If you have TWiki 4.2 or later, you can install from the =configure= interface (Go to Plugins->Find More Extensions)
   1.306 +      * See the [[http://twiki.org/cgi-bin/view/Plugins/BuildContribInstallationSupplement][installation supplement]] on TWiki.org for more information.
   1.307 +   * If you have any problems, then you can still install manually from the command-line:
   1.308 +      1 Download one of the =.zip= or =.tgz= archives
   1.309 +      1 Unpack the archive in the root directory of your TWiki installation.
   1.310 +      1 Run the installer script ( =perl &lt;module&gt;_installer= )
   1.311 +      1 Run =configure= and enable the module, if it is a plugin.
   1.312 +      1 Repeat for any missing dependencies.
   1.313 +   * If you are *still* having problems, then instead of running the installer script:
   1.314 +      1 Make sure that the file permissions allow the webserver user to access all files.
   1.315 +      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)
   1.316 +      1 Manually edit !LocalSite.cfg to set any configuration variables.
   1.317 +
   1.318 +%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>"}%
   1.319 +
   1.320 +
   1.321 +---++ Contrib Settings
   1.322 +   * Set SHORTDESCRIPTION = =Behaviour= Javascript event library to create javascript based interactions that degrade well when javascript is not available
   1.323 +
   1.324 +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.
   1.325 +
   1.326 +---++ Contrib Info
   1.327 +|  Author: | TWiki:Main.ArthurClemens |
   1.328 +|  Copyright: | Code: =behaviour.js= version 1.1 - Copyright (c) Ben Nolan and Simon Willison. TWiki distribution and updates/additions: TWiki:Main.ArthurClemens. |
   1.329 +|  License: | BSD |
   1.330 +|  Version: | 15675 (22 Jan 2008) |
   1.331 +|  Dependencies: | None |
   1.332 +|  Contrib&nbsp;Version: | 1.3.1 |
   1.333 +|  Change&nbsp;History: | <!-- specify latest version first -->&nbsp; |
   1.334 +|  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]]. |
   1.335 +|  02 Jul 2007 | 1.2 Integrated other faster code by Dean Edwards: [[http://dean.edwards.name/weblog/2006/06/again/][faster onload (again)]]. |
   1.336 +|  08 Mar 2007 | 1.1 Integrated code by Dean Edwards (see [[#CodeUpdate][Code update version 1.1 with faster DOM queries]]). |
   1.337 +|  04 Jun 2006 | 1.0  First Version. Included Behaviour version: 1.1. |
   1.338 +|  Home: | http://TWiki.org/cgi-bin/view/Plugins/%TOPIC% |
   1.339 +|  Feedback: | http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev |
   1.340 +|  Appraisal: | http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Appraisal |
   1.341 +
   1.342 +__Related Topics:__ %TWIKIWEB%.TWikiPreferences
   1.343 +
   1.344 +%META:FILEATTACHMENT{name="behaviour.js" attr="" autoattached="1" comment="" date="1162075796" path="behaviour.compressed.js" size="2902" user="UnknownUser" version=""}%
   1.345 +
   1.346 +@
   1.347 +
   1.348 +
   1.349 +1.1
   1.350 +log
   1.351 +@buildrelease
   1.352 +@
   1.353 +text
   1.354 +@d1 1
   1.355 +a1 1
   1.356 +%META:TOPICINFO{author="TWikiContributor" date="1162394433" format="1.1" version="1"}%
   1.357 +d6 1
   1.358 +a6 1
   1.359 +Behaviour is suited to create javascript based interaction that degrades well when javascript is not available.
   1.360 +d8 1
   1.361 +a8 1
   1.362 +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]].
   1.363 +a9 1
   1.364 +%TOC{title="On this page:"}%
   1.365 +d58 2
   1.366 +d65 1
   1.367 +a65 1
   1.368 +<script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/BehaviourContrib/behaviour.compressed.js"></script>
   1.369 +d111 2
   1.370 +a112 1
   1.371 +---++ Example
   1.372 +d117 3
   1.373 +a119 1
   1.374 +<span class="link%TWIKIWEB%%HOMETOPIC%">[[%TWIKIWEB%.%HOMETOPIC%][TWiki Web Home]]</span>
   1.375 +d124 42
   1.376 +a165 4
   1.377 +	'.link%TWIKIWEB%%HOMETOPIC% a' : function(el){
   1.378 +		el.onclick = function() {
   1.379 +			// open in a popup with no other attributes than template 'viewplain'
   1.380 +			launchTheWindow(this.href,null,null,null,"viewplain");
   1.381 +a169 1
   1.382 +
   1.383 +a174 3
   1.384 +The class name =link%<nop>TWIKIWEB%%<nop>HOMETOPIC%= will get expanded to =link%TWIKIWEB%%HOMETOPIC%=
   1.385 +</blockquote>
   1.386 +
   1.387 +d177 3
   1.388 +a179 1
   1.389 +<span class="link%TWIKIWEB%%HOMETOPIC%">[[%TWIKIWEB%.%HOMETOPIC%][TWiki Web Home]]</span>
   1.390 +d184 42
   1.391 +a225 4
   1.392 +	'.link%TWIKIWEB%%HOMETOPIC% a' : function(el){
   1.393 +		el.onclick = function() {
   1.394 +			// open in a popup with no other attributes than template 'viewplain'
   1.395 +			launchTheWindow(this.href,null,null,null,"viewplain");
   1.396 +a229 1
   1.397 +
   1.398 +d234 19
   1.399 +d255 3
   1.400 +a257 1
   1.401 +   * [[http://dean.edwards.name/weblog/2006/03/faster/][Dean Edwards: Faster DOM Queries]] - with a speed-up hack to Behaviour
   1.402 +d261 3
   1.403 +a263 1
   1.404 +For details, see the Behaviour website.
   1.405 +d268 27
   1.406 +a294 1
   1.407 +   
   1.408 +d296 11
   1.409 +a306 6
   1.410 +
   1.411 +| Author: | TWiki:Main.ArthurClemens |
   1.412 +| Copyright: | version 1.1 - Copyright (c) Ben Nolan and Simon Willison |
   1.413 +| License: | BSD |
   1.414 +| Dependencies: | None |
   1.415 +| 4 June 2006| 1.000 First Version. Included Behaviour version: 1.1 |
   1.416 +d313 2
   1.417 +a314 2
   1.418 +%META:FILEATTACHMENT{name="behaviour.compressed.js" attr="" autoattached="1" comment="" date="1162075796" path="behaviour.compressed.js" size="2902" user="UnknownUser" version=""}%
   1.419 +%META:FILEATTACHMENT{name="behaviour.js" attr="" autoattached="1" comment="" date="1161199153" path="behaviour.js" size="8149" user="UnknownUser" version=""}%
   1.420 +@