(no commit message)
[openafs-wiki.git] / TWiki / BehaviourContrib.mdwn
1 # <a name="Behaviour Javascript framework C"></a><a name=" Behaviour Javascript framework "></a> Behaviour Javascript framework Contrib
2
3 This contrib packages the third-party `Behaviour` Javascript event library, available from <http://bennolan.com/behaviour/>.
4
5 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).
6
7 <div><span>On this page:</span><ul>
8     <li><a href="#Introduction"> Introduction</a></li>
9     <li><a href="#Usage"> Usage</a><ul>
10         <li><a href="#Example"> Example</a></li>
11         <li><a href="#Leaking danger"> Leaking danger</a></li>
12       </ul>
13     </li>
14     <li><a href="#Development"> Development</a></li>
15     <li><a href="#License"> License</a></li>
16     <li><a href="#Links"> Links</a></li>
17     <li><a href="#Installation Instructions"> Installation Instructions</a></li>
18     <li><a href="#Contrib Settings"> Contrib Settings</a></li>
19     <li><a href="#Contrib Info"> Contrib Info</a></li>
20   </ul>
21 </div>
22
23 ## <a name="Introduction"></a> Introduction
24
25 From the website:
26
27 > 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.
28 >
29 > 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.
30 >
31 > That's why I came up with Behaviour - my solution to unobtrusive javascript behaviours.
32 >
33 > **How does it work?**
34 >
35 > Behaviour lets you use CSS selectors to specify elements to add javascript events to. This means that instead of writing:
36 >
37 >     <li>
38 >        <a onclick="this.parentNode.removeChild(this)" href="#">
39 >           Click me to delete me
40 >        </a>
41 >     </li>
42 >
43 > You can use:
44 >
45 >     <ul id="example">
46 >        <li>
47 >           <a href="/someurl">Click me to delete me</a>
48 >        </li>
49 >     </ul>
50 >
51 > And then use css selectors to select that element and add javascript functions to it.
52 >
53 >     var myrules = {
54 >        '#example li' : function(el){
55 >           el.onclick = function(){
56 >              this.parentNode.removeChild(this);
57 >
58 >           }
59 >        }
60 >     };
61 >
62 >     Behaviour.register(myrules);
63
64 ## <a name="Usage"></a> Usage
65
66 Include the javascript file:
67
68 > <script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/BehaviourContrib/behaviour.js"></script>
69
70 In your code you create a "rules" object, with sub-objects for each html element class name or id:
71
72 > var myrules = {
73 >        '.classname' : function(element) {
74 >           // element event
75 >           element.onclick = function() {
76 >              // code here
77 >           }
78 >        },
79 >
80 >        '#id' : function(element) {
81 >           // element event
82 >           element.onclick = function() {
83 >              // code here
84 >           }
85 >        }
86 >     };
87 >
88 > Or use nested identifiers:
89 >
90 >     var myrules = {
91 >        '.menu li a' : function(element) {
92 >           element.onclick = function() {
93 >              // code here
94 >           }
95 >        }
96 >     };
97
98 Apply the rules with:
99
100 > Behaviour.register(myrules);
101
102 ### <a name="Example"></a> Example
103
104 If we have a 'normal' link to TWiki Web hometopic: [[TWiki Web Home|TWiki/WebHome]], 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.
105
106 > <div id="demoblock" style="padding:1em; width:100px; text-align:center;">
107 >     MOUSE OVER ME
108 >     </div>
109 >
110 >     <script type="text/javascript">
111 >     // <![CDATA[
112 >     var myrules = {
113 >        '#demoblock' : function(el) {
114 >           var defaultColor = '#A3D6F8';
115 >           var highlightColor = '#4A7FB5';
116 >
117 >           el.style.backgroundColor = defaultColor;
118 >
119 >           el.onmouseover = function() {
120 >              this.style.backgroundColor = highlightColor;
121 >              return false;
122 >           }
123 >           el.onmouseout = function() {
124 >              this.style.backgroundColor = defaultColor;
125 >              return false;
126 >           }
127 >        },
128 >        '#demoblock span' : function(el) {
129 >
130 >           var text = el.innerHTML;
131 >
132 >           var fisherYates = function (inArray) {
133 >             var i = inArray.length;
134 >             if ( i == 0 ) return false;
135 >             while ( --i ) {
136 >               var j = Math.floor( Math.random() * ( i + 1 ) );
137 >               var tempi = inArray[i];
138 >               var tempj = inArray[j];
139 >               inArray[i] = tempj;
140 >               inArray[j] = tempi;
141 >              }
142 >           }
143 >
144 >           var randomize = function(inText) {
145 >              var letters = inText.split('');
146 >              fisherYates(letters);
147 >              return letters.join('');
148 >           }
149 >           el.onmouseover = function() {
150 >              this.innerHTML = randomize(text);
151 >              return false;
152 >           }
153 >           el.onmouseout = function() {
154 >              this.innerHTML = text;
155 >              return false;
156 >           }
157 >        }
158 >     };
159 >     Behaviour.register(myrules);
160 >     // ]]>
161 >     </script>
162 >
163 > Creates:
164 >
165 > <div id="demoblock" style="padding: 1em; width: 150px; text-align: center"><span>MOUSE OVER ME</span></div>
166 >
167 > ### <a name="Leaking danger"></a> Leaking danger
168 >
169 > Behaviour code leaks memory on Windows Explorer prior to version 7. To prevent leaking, set the element variable to
170 >
171 > `null`
172 >
173 > :
174 >
175 > > var myrules = {
176 > >        'table.test td' : function(element) {
177 > >           element.onmouseover = function() {
178 > >              this.style.backgroundColor = highlightColor;
179 > >              return false;
180 > >           }
181 > >           element = null; // by setting this IE will not leak
182 > >        }
183 > >     };
184 > >     Behaviour.register(myrules);
185 >
186 > ## <a name="Development"></a> Development
187 >
188 > - [Google Groups: Behaviour Javascript Library](http://groups.google.com/group/behaviour)
189 > - [Nabble - Behaviour Javascript Library forum &amp; mailing list archive](http://www.nabble.com/Behaviour-Javascript-Library-f16264.html)
190 > - [Behaviour2](http://groups.google.com/group/behaviour/browse_thread/thread/e9828f9fdb482ac1/8ca704730053e23f?#8ca704730053e23f) - update in the making, since 2006
191 >
192 > ## <a name="License"></a> License
193 >
194 > Behaviour is freely distributable under the terms of an BSD license. For details see the Behaviour website.
195 >
196 > ## <a name="Links"></a> Links
197 >
198 > - [Behaviour website](http://bennolan.com/behaviour/)
199 > - [Behaviour Google Group](http://groups.google.com/group/behaviour)
200 >
201 > ## <a name="Installation Instructions"></a> Installation Instructions
202 >
203 > 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.
204 >
205 > Like many other TWiki extensions, this module is shipped with a fully automatic installer script written using the BuildContrib.
206 >
207 > - If you have TWiki 4.2 or later, you can install from the `configure` interface (Go to Plugins-&gt;Find More Extensions)
208 >   - See the [installation supplement](http://twiki.org/cgi-bin/view/Plugins/BuildContribInstallationSupplement) on TWiki.org for more information.
209 > - If you have any problems, then you can still install manually from the command-line:
210 >   1. Download one of the `.zip` or `.tgz` archives
211 >   2. Unpack the archive in the root directory of your TWiki installation.
212 >   3. Run the installer script ( `perl <module>_installer` )
213 >   4. Run `configure` and enable the module, if it is a plugin.
214 >   5. Repeat for any missing dependencies.
215 > - If you are **still** having problems, then instead of running the installer script:
216 >   1. Make sure that the file permissions allow the webserver user to access all files.
217 >   2. 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)
218 >   3. Manually edit LocalSite.cfg to set any configuration variables.
219 >
220 > <div class="twikiAlert">%X% WARNING: SYSTEMWEB is not defined in this TWiki. Please add these definitions to your [[Main/TWikiPreferences]], if they are not already there:<br /><pre>   * Set SYSTEMWEB = %TWIKIWEB%<br />   * Set USERSWEB = %MAINWEB%</pre></div>
221 >
222 > ## <a name="Contrib Settings"></a> Contrib Settings
223 >
224 > - Set SHORTDESCRIPTION = `Behaviour` Javascript event library to create javascript based interactions that degrade well when javascript is not available
225 >
226 > 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.
227 >
228 > ## <a name="Contrib Info"></a> Contrib Info
229 >
230 > <table border="1" cellpadding="0" cellspacing="0">
231 >   <tr>
232 >     <td align="right"> Author: </td>
233 >     <td> TWiki:Main.ArthurClemens </td>
234 >   </tr>
235 >   <tr>
236 >     <td align="right"> Copyright: </td>
237 >     <td> Code: <code>behaviour.js</code> version 1.1 - Copyright (c) Ben Nolan and Simon Willison. TWiki distribution and updates/additions: TWiki:Main.ArthurClemens. </td>
238 >   </tr>
239 >   <tr>
240 >     <td align="right"> License: </td>
241 >     <td> BSD </td>
242 >   </tr>
243 >   <tr>
244 >     <td align="right"> Version: </td>
245 >     <td> 15675 (30 Mar 2009) </td>
246 >   </tr>
247 >   <tr>
248 >     <td align="right"> Dependencies: </td>
249 >     <td> None </td>
250 >   </tr>
251 >   <tr>
252 >     <td align="right"> Contrib Version: </td>
253 >     <td> 1.3.1 </td>
254 >   </tr>
255 >   <tr>
256 >     <td align="right"> Change History: </td>
257 >     <td>  </td>
258 >   </tr>
259 >   <tr>
260 >     <td align="right"> 17 Oct 2007 </td>
261 >     <td> 1.3 Replaced "faster code" by other code from Dean Edwards, [[ packed by <a href="http://groups.google.com/group/behaviour/browse_thread/thread/85137977bedf5ed/3cf3ba8065d41a8c#3cf3ba8065d41a8c][Raymond" target="_top">http://groups.google.com/group/behaviour/browse_thread/thread/85137977bedf5ed/3cf3ba8065d41a8c#3cf3ba8065d41a8c][Raymond</a> Irving]]. </td>
262 >   </tr>
263 >   <tr>
264 >     <td align="right"> 02 Jul 2007 </td>
265 >     <td> 1.2 Integrated other faster code by Dean Edwards: <a href="http://dean.edwards.name/weblog/2006/06/again/" target="_top">faster onload (again)</a>. </td>
266 >   </tr>
267 >   <tr>
268 >     <td align="right"> 08 Mar 2007 </td>
269 >     <td> 1.1 Integrated code by Dean Edwards (see [[Main/WebHome#CodeUpdate]]). </td>
270 >   </tr>
271 >   <tr>
272 >     <td align="right"> 04 Jun 2006 </td>
273 >     <td> 1.0 First Version. Included Behaviour version: 1.1. </td>
274 >   </tr>
275 >   <tr>
276 >     <td align="right"> Home: </td>
277 >     <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%</a></td>
278 >   </tr>
279 >   <tr>
280 >     <td align="right"> Feedback: </td>
281 >     <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev</a></td>
282 >   </tr>
283 >   <tr>
284 >     <td align="right"> Appraisal: </td>
285 >     <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Appraisal" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Appraisal</a></td>
286 >   </tr>
287 > </table>
288 >
289 > **_Related Topics:_** [[TWikiPreferences]]