initial checkin
[org.ibex.widgets.git] / src / ibex / lib / redirect.t
1 <!-- Copyleft 2004 - see COPYING for details [LGPL] -->
2
3 <ibex>
4     <meta:doc>
5         See xwt.theme.redirect for details
6     </meta:doc>
7
8     <ui:box>
9
10         var locks = {};
11         var redirects = {};
12
13         thisbox.redirectTo = function(v) {
14             if (v == null) {
15                 ibex.log.warn("Attempted to redirect properties to a null value");
16                 return false;
17             }
18
19             for (var i=1; arguments.length > i; i++) {
20                 // get property from arguments
21                 var p = arguments[i];
22
23                 // remove the trap if it already exists
24                 if (redirects[p]) dropRedirect(v, p);
25
26                 // create and store redirecting functions
27                 redirects[p] = [v,
28                                 function(w) {
29                                     if (!locks[p]) { locks[p] = true; cascade = w; thisbox[p] = w; locks[p] = null; }
30                                 },
31                                 function(w) {
32                                     if (!locks[p]) { locks[p] = true; v[p] = w; locks[p] = null; }
33                                     return true;
34                                 },
35                                 function() { return v[p]; }];
36
37                 // assign redirecting traps
38                 try { // try/catch for properties that can't have write traps
39                     thisbox[p] ++= redirects[p][2];
40                     v[p] ++= redirects[p][1];
41                 } catch(e) {
42                     redirects[p][1] = null;
43                     redirects[p][2] = null;
44                 }
45                 // try/catch for properties that can't have read traps
46                 try { thisbox[p] ++= redirects[p][3]; }
47                 catch(e) { redirects[p][3] = null; }
48             }
49
50             // report success
51             return true;
52         }
53
54         thisbox.dropRedirect = function(v) {
55             for (var i=1; arguments.length > i; i++) {
56                 // get property from arguments
57                 var p = arguments[i];
58
59                 // remove redirect traps and reference
60                 if (redirects[p]) {
61                     if (redirects[p][1]) redirects[p][0] --= redirects[p][1];
62                     if (redirects[p][2]) thisbox[p] --= redirects[p][2];
63                     if (redirects[p][3]) thisbox[p] --= redirects[p][3];
64                     redirects[p] = null;
65                 }
66             }
67
68             return true;
69         }
70
71     </ui:box>
72 </ibex>