1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
// This is a roxen module. Copyright © 1999 - 2009, Roxen IS. 
// 
 
constant cvs_version = "$Id$"; 
constant thread_safe = 1; 
 
#include <module.h> 
inherit "module"; 
 
constant module_type = MODULE_TAG; 
constant module_name = "Tags: Folding lists"; 
constant module_doc  = "Provides the &lt;foldlist&gt; tag, which is used to " 
"build folding lists. The folding lists work like <tt>&lt;dl&gt;</tt> lists " 
"where each item can be folded or unfolded."; 
 
TAGDOCUMENTATION; 
#ifdef manual 
constant tagdoc=([ 
 
"foldlist":({#"<desc type='cont'><p><short hide='hide'> 
 This tag is used to build folding lists, that are like &lt;dl&gt; lists, 
 but where each element can be unfolded.</short>This tag is used to 
 build folding lists, that are like <tag>dl</tag> lists, but where 
 each element can be unfolded. The tags used to build the lists 
 elements are <tag>ft</tag> and <tag>fd</tag>.</p></desc> 
 
<attr name='unfolded'><p> 
 Will make all the elements in the list unfolded by default.</p> 
</attr> 
 
<attr name='foldedsrc'><p> 
 The image to use for folded items. The default is '/internal-roxen-unfold'.</p> 
</attr> 
 
<attr name='unfoldedsrc'><p> 
 The image to use for unfolded items. The default is '/internal-roxen-fold'.</p> 
</attr>", 
 
([ "ft":({#"<desc type='cont'><p> 
This tag is used within the foldlist tag. The contents of this 
container, that is not within an fd, tag will be visible both when the 
element is folded and unfolded.</p></desc> 
 
<attr name='folded'><p> 
 Will make this element folded by default. Overrides an unfolded 
 attribute set in the foldlist tag.</p> 
</attr> 
 
<attr name='unfolded'><p> 
 Will make this element unfolded by default.</p> 
</attr> 
 
<attr name='foldedsrc'><p> 
 The image to use for folded items. Overrides the 'foldedsrc' 
 attribute in <tag>tablist</tag> for this item.</p> 
</attr> 
 
<attr name='unfoldedsrc'><p> 
 The image to use for unfolded items. Overrides the 'foldedsrc' 
 attribute in <tag>tablist</tag> for this item.</p> 
</attr>", 
 
([ "fd":#"<desc type='cont'><p> 
 The contents of this container will only be visible when the element 
 it is written in is unfolded.</p> 
</desc> 
 
<ex> 
 <foldlist> 
   <ft> 
     Heading1 
     <fd>Contents 1</fd> 
   </ft> 
   <ft> 
     Heading2 
     <fd>Contents 2</fd> 
   </ft> 
 </foldlist> 
</ex>" ]) 
  })]) 
})]); 
#endif 
 
 
string encode_url(array states, RequestID id){ 
  object state=id->misc->foldlist->state; 
 
  int value, q; 
  foreach(states, int tmp) { 
    value+=tmp*(2->pow(q)); 
    q+=2; 
  } 
  value=(value<<1)+1; 
 
  return state->encode_revisit_url (id, value); 
} 
 
class TagFoldlist { 
  inherit RXML.Tag; 
  constant name = "foldlist"; 
 
  class TagFD { 
    inherit RXML.Tag; 
    constant name = "fd"; 
 
    class Frame { 
      inherit RXML.Frame; 
      int show; 
 
      array do_enter(RequestID id) { 
        show=id->misc->foldlist_show; 
        if(show) 
          do_iterate=0; 
        else 
          do_iterate=-1; 
        return 0; 
      } 
 
      int do_iterate; 
 
      array do_return(RequestID id) { 
        if(show) 
          result="<dt><dd>"+content+"</dd></dt>"; 
        else 
          result=""; 
        return 0; 
      } 
    } 
  } 
 
  class TagFT { 
    inherit RXML.Tag; 
    constant name = "ft"; 
 
    class Frame { 
      inherit RXML.Frame; 
      int index, show; 
 
      int set_def(RequestID id) { 
        if (args->unfold) 
          return 1; 
        if (args->fold) 
          return 0; 
        return id->misc->foldlist->def; 
      } 
 
      array do_enter(RequestID id) { 
        index=id->misc->foldlist->counter++; 
        if(sizeof(id->misc->foldlist->states)>index) 
          switch(id->misc->foldlist->states[index]) { 
          case 0: 
            show = set_def(id); 
            break; 
          case 1: 
            show = 0; 
            break; 
          case 2: 
            show = 1; 
            break; 
          } 
        else { 
          id->misc->foldlist->states+=({ 0 }); 
          show=set_def(id); 
        } 
        id->misc->foldlist_show=show; 
        return 0; 
      } 
 
      array do_return(RequestID id) { 
        array states=copy_value(id->misc->foldlist->states); 
        states[index]=!show+1; 
        result="<dt><a target=\"_self\" href=\""+ 
          encode_url(states, id)+ 
          "\"><img src=\""+ 
          (args[(show?"un":"")+"foldedsrc"]||id->misc->foldlist[(show?"u":"")+"fsrc"])+ 
          "\" border=\"0\" alt=\""+({ "+", "-" })[show]+"\" /></a>"+ 
          content+"</dt>"; 
        return 0; 
      } 
 
    } 
  } 
 
  // This tag set can probably be shared, but I don't know for sure. /mast 
  RXML.TagSet internal = RXML.TagSet(this_module(), "foldlist", ({ TagFT(), TagFD() }) ); 
 
  class Frame { 
    inherit RXML.Frame; 
    RXML.TagSet additional_tags = internal; 
 
    mapping foldlist; 
    string state_id; 
 
    array do_enter(RequestID id) { 
 
      // The initial state 
      int def=!!args->unfolded; 
 
      // Find out environment 
      foldlist=id->misc->foldlist; 
      string hist=""; 
      if(foldlist && foldlist->hist) { 
        hist=foldlist->hist; 
        if(foldlist->cnt > 15) hist+=":"; 
        hist+=sprintf("%x",foldlist->cnt); 
      } 
      else 
        id->misc->foldlist_depth++; 
 
      // Register ourselfs as state consumers and incorporate our initial state. 
      state_id = (args->name || "fl")+":"+id->misc->foldlist_depth+":"+hist; 
      StateHandler.Page_state state=StateHandler.Page_state(id); 
      state_id = state->register_consumer(state_id); 
      if(id->real_variables->__state && !state->uri_decode(id->real_variables->__state[0])) 
        RXML.run_error("Error in state."); 
 
      //Get our real state 
      // 00 unknown 
      // 01 folded 
      // 10 unfolded 
      array(int) states=({}); 
      int istates=state->get(); 
      //      werror("istates: %d\n",istates); 
      if(istates) 
        istates=istates>>1; 
      while(istates) { 
        states+=({ istates & 3 }); 
        istates=istates >> 2; 
      } 
      while(sizeof(states) && states[-1]==0) 
        states=states[..sizeof(states)-2]; 
 
      // Export our findings 
      id->misc->foldlist=(["states":states, 
                           "state":state, 
                           "def":def, 
                           "cnt":0, 
                           "hist":hist, 
                           "fsrc":args->foldedsrc||"/internal-roxen-unfold", 
                           "ufsrc":args->unfoldedsrc||"/internal-roxen-fold" 
      ]); 
      return 0; 
    } 
 
    array do_return(RequestID id) { 
      id->misc->foldlist=foldlist; 
      result = (id->misc->debug?"<!-- "+state_id+" -->\n":"")+ 
        "<dl>"+content+"</dl>\n"; 
      m_delete(id->misc, "foldlist_show"); 
      return 0; 
    } 
 
  } 
}