~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Configure/JS.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Foswiki::Configure::JS;
 
2
 
 
3
use strict;
 
4
 
 
5
use vars qw( $js1 $js2 );
 
6
 
 
7
sub js1 {
 
8
    local $/ = undef;
 
9
    return <DATA>;
 
10
}
 
11
 
 
12
sub js2 {
 
13
    return <<'HERE';
 
14
//<!--
 
15
document.write("<style type='text/css'>");
 
16
document.write(".foldableBlockClosed {display:none;}");
 
17
document.write("<\/style>");
 
18
//-->
 
19
HERE
 
20
}
 
21
 
 
22
1;
 
23
__DATA__
 
24
//<!--
 
25
 
 
26
var lastOpenBlock = null;
 
27
var lastOpenBlockLink = null;
 
28
var allBlocks = null; // array of all foldable blocks
 
29
var allBlockLinks = null; // array of all foldable block links (headers)
 
30
 
 
31
function foldBlock(id) {
 
32
    var shouldClose = false;
 
33
    var block = null;
 
34
    if (lastOpenBlock == null) {
 
35
        block = document.getElementById(id);
 
36
        if (block.open) {
 
37
            shouldClose = true;
 
38
        }
 
39
    }
 
40
    if (shouldClose) {
 
41
        closeBlock(id);
 
42
    } else {
 
43
        var o = openBlock(id);
 
44
        if (lastOpenBlock != null) {
 
45
            closeBlockElement(lastOpenBlock, lastOpenBlockLink);
 
46
        }
 
47
    }
 
48
    window.location.hash = id + "link";
 
49
    
 
50
    if (o && o.block) {
 
51
        lastOpenBlock = (lastOpenBlock == o.block) ? null : o.block;
 
52
    }
 
53
    if (o && o.blockLink) {
 
54
        lastOpenBlockLink = (lastOpenBlockLink == o.blockLink) ? null : o.blockLink;
 
55
    }
 
56
}
 
57
 
 
58
function openBlock(id) {
 
59
    var block = document.getElementById(id);
 
60
    var blockLink = document.getElementById('blockLink' + id);
 
61
    openBlockElement(block, blockLink);
 
62
    return {block:block, blockLink:blockLink};
 
63
}
 
64
 
 
65
function openBlockElement(block, blockLink) {
 
66
        var indicator = getElementsByClassName(blockLink, 'blockLinkIndicator')[0];
 
67
        indicator.innerHTML = '&#9660;';
 
68
    block.className = 'foldableBlock foldableBlockOpen';
 
69
    block.open = true;
 
70
    blockLink.className = 'blockLink blockLinkOn';
 
71
}
 
72
 
 
73
function closeBlock(id) {
 
74
    var block = document.getElementById(id);
 
75
    var blockLink = document.getElementById('blockLink' + id);
 
76
    closeBlockElement(block, blockLink);
 
77
    return {block:block, blockLink:blockLink};
 
78
}
 
79
 
 
80
function closeBlockElement(block, blockLink) {
 
81
        var indicator = getElementsByClassName(blockLink, 'blockLinkIndicator')[0];
 
82
        indicator.innerHTML = '&#9658;';
 
83
    block.className = 'foldableBlock foldableBlockClosed';
 
84
    block.open = false;
 
85
    blockLink.className = 'blockLink blockLinkOff';
 
86
}
 
87
 
 
88
function toggleAllOptions(open) {
 
89
    if (allBlocks == null) {
 
90
        allBlocks = getElementsByClassName(document, 'foldableBlock');
 
91
    }
 
92
    if (allBlockLinks == null) {
 
93
        allBlockLinks = getElementsByClassName(document, 'blockLink');
 
94
    }
 
95
    var i, ilen=allBlocks.length;
 
96
    if (open) {
 
97
        for (i=0; i<ilen; ++i) {
 
98
            openBlockElement(allBlocks[i], allBlockLinks[i]);
 
99
        }
 
100
    } else {
 
101
        for (i=0; i<ilen; ++i) {
 
102
            closeBlockElement(allBlocks[i], allBlockLinks[i]);
 
103
        }
 
104
    }
 
105
    lastOpenBlock = null;
 
106
    lastOpenBlockLink = null;
 
107
}
 
108
 
 
109
function getElementsByClassName(inRootElem, inClassName, inTag) {
 
110
        var rootElem = inRootElem || document;
 
111
        var tag = inTag || '*';
 
112
        var elms = rootElem.getElementsByTagName(tag);
 
113
        var className = inClassName.replace(/\-/g, "\\-");
 
114
        var re = new RegExp("\\b" + className + "\\b");
 
115
        var el;
 
116
        var hits = new Array();
 
117
        for (var i = 0; i < elms.length; i++) {
 
118
                el = elms[i];
 
119
                if (re.test(el.className)) {
 
120
                        hits.push(el);
 
121
                }
 
122
        }
 
123
        return hits;
 
124
}
 
125
 
 
126
function addLoadEvent (inFunction, inDoPrepend) {
 
127
        if (typeof(inFunction) != "function") {
 
128
                return;
 
129
        }
 
130
        var oldonload = window.onload;
 
131
        if (typeof window.onload != 'function') {
 
132
                window.onload = function() {
 
133
                        inFunction();
 
134
                };
 
135
        } else {
 
136
                var prependFunc = function() {
 
137
                        inFunction(); oldonload();
 
138
                };
 
139
                var appendFunc = function() {
 
140
                        oldonload(); inFunction();
 
141
                };
 
142
                window.onload = inDoPrepend ? prependFunc : appendFunc;
 
143
        }
 
144
}
 
145
 
 
146
function initDeltaIndicators() {
 
147
        var elems = getElementsByClassName(document.forms.update, 'delta' , 'SPAN');    
 
148
        var i, ilen = elems.length;
 
149
        for (i=0; i<ilen; ++i) {
 
150
                initDelta(elems[i]);
 
151
        }
 
152
}
 
153
 
 
154
function initDelta(inElem) {
 
155
        var value = replaceStubChars(inElem.title);
 
156
        var type = inElem.className.split(" ")[0];
 
157
        var title = formatLinkValueInTitle(type, "default=", value);
 
158
        inElem.title = title;
 
159
}
 
160
 
 
161
function initDefaultLinks() {
 
162
        var elems = getElementsByClassName(document.forms.update, 'defaultValueLink' , 'A');    
 
163
        var i, ilen = elems.length;
 
164
        for (i=0; i<ilen; ++i) {
 
165
                initDefaultLink(elems[i]);
 
166
        }
 
167
}
 
168
 
 
169
/**
 
170
Initializes the 2 states of "reset to default" links.
 
171
State 1: restore to default
 
172
State 2: undo restore
 
173
*/
 
174
function initDefaultLink(inLink) {
 
175
 
 
176
        // extract type
 
177
        var type = inLink.className.split(" ")[0];
 
178
        inLink.type = type;
 
179
        
 
180
        // retrieve value from title tag
 
181
        inLink.defaultValue = replaceStubChars(inLink.title);
 
182
        // set title states
 
183
        inLink.setDefaultTitle = 'Set to default value:';
 
184
        inLink.undoDefaultTitle = 'Undo default and use previous value:';
 
185
        // set link label states
 
186
        inLink.setDefaultLinkText = 'use&nbsp;default';
 
187
        inLink.undoDefaultLinkText = 'undo';
 
188
        
 
189
        // set defaults
 
190
        inLink.title = formatLinkValueInTitle(inLink.type, inLink.setDefaultTitle, inLink.defaultValue);
 
191
        inLink.innerHTML = inLink.setDefaultLinkText;
 
192
}
 
193
 
 
194
/**
 
195
Prepend a string to a human readable value string.
 
196
*/
 
197
function formatLinkValueInTitle (inType, inString, inValue) {
 
198
        return (inString + createHumanReadableValueString(inType, inValue));
 
199
}
 
200
 
 
201
/**
 
202
Called from "reset to default" link.
 
203
Values are set in Value.pm
 
204
*/
 
205
function resetToDefaultValue (inLink, inFormType, inName, inValue) {
 
206
 
 
207
        var name = replaceStubChars(inName);
 
208
        var elem = document.forms.update[name];
 
209
        if (!elem) return;
 
210
        
 
211
        var value = replaceStubChars(inValue);
 
212
        if (inLink.oldValue != null) value = inLink.oldValue;
 
213
 
 
214
        var oldValue;
 
215
        var type = elem.type;
 
216
 
 
217
        if (type == 'checkbox') {
 
218
                oldValue = elem.checked;
 
219
                elem.checked = value;
 
220
        } else if (type == 'select-one') {
 
221
                // find selected element
 
222
                var index;
 
223
                for (var i=0; i<elem.options.length; ++i) {
 
224
                        if (elem.options[i].value == value) {
 
225
                                index = i;
 
226
                                break;
 
227
                        }
 
228
                }
 
229
                oldValue = elem.options[elem.selectedIndex].value;
 
230
                elem.selectedIndex = index;
 
231
        } else if (type == 'radio') {
 
232
                oldValue = elem.checked;
 
233
                elem.checked = value;
 
234
        } else {
 
235
                // including type='text' 
 
236
                oldValue = elem.value;
 
237
                elem.value = value;
 
238
        }
 
239
        
 
240
        if (inLink.oldValue == null) {
 
241
                // we have just set the default value
 
242
                // prepare undo link
 
243
                inLink.innerHTML = inLink.undoDefaultLinkText;
 
244
                inLink.oldValue = oldValue;
 
245
                inLink.title = formatLinkValueInTitle(inLink.type, inLink.undoDefaultTitle, oldValue);
 
246
        } else {
 
247
                // we have just set the old value
 
248
                inLink.innerHTML = inLink.setDefaultLinkText;
 
249
                inLink.oldValue = null;
 
250
                inLink.title = formatLinkValueInTitle(inLink.type, inLink.setDefaultTitle, value);
 
251
        }
 
252
        
 
253
}
 
254
 
 
255
/**
 
256
Translates a value to a readable string that makes sense in a form.
 
257
For instance, 'false' gets translated to 'off' with checkboxes.
 
258
 
 
259
Possible types:
 
260
URL
 
261
PATH
 
262
URLPATH
 
263
STRING
 
264
BOOLEAN
 
265
NUMBER
 
266
SELECTCLASS
 
267
SELECT
 
268
REGEX
 
269
OCTAL
 
270
COMMAND
 
271
PASSWORD
 
272
PERL (?)
 
273
*/
 
274
function createHumanReadableValueString (inType, inValue) {
 
275
        if (inType == 'NUMBER') {
 
276
                // do not convert numbers
 
277
                return inValue;
 
278
        }
 
279
        if (inType == 'BOOLEAN') {
 
280
                if (isTrue(inValue)) {
 
281
                        return 'on';
 
282
                } else {
 
283
                        return 'off';
 
284
                }
 
285
        }
 
286
        if (inValue.length == 0) {
 
287
                return '""';
 
288
        }
 
289
        // all other cases
 
290
        return inValue;
 
291
}
 
292
 
 
293
/**
 
294
Checks if a value can be considered true.
 
295
*/
 
296
function isTrue (v) {
 
297
        if (v == 1 || v == '1' || v == 'on' || v == 'true') return 1;
 
298
        return 0;
 
299
}
 
300
 
 
301
/**
 
302
Replaces stubs for single and double quotes and newlines with the real characters.
 
303
*/
 
304
function replaceStubChars(v) {
 
305
        // replace &#26;
 
306
        var re
 
307
        re = new RegExp(/#26;/g);
 
308
        v = v.replace(re, "'");
 
309
        // replace &#22;
 
310
        re = new RegExp(/#22;/g);
 
311
        v = v.replace(re, '"');
 
312
        re = new RegExp(/#13;/g);
 
313
        v = v.replace(re, "\r");
 
314
        return v;
 
315
}
 
316
 
 
317
addLoadEvent(toggleAllOptions, 0);
 
318
addLoadEvent(initDeltaIndicators);
 
319
addLoadEvent(initDefaultLinks);
 
320
 
 
321
//-->
 
322