-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_expand_bbcodes.js
More file actions
206 lines (195 loc) · 6.24 KB
/
select_expand_bbcodes.js
File metadata and controls
206 lines (195 loc) · 6.24 KB
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
/*************************************************************
* SXBB - Select Expand BBcodes MOD v1.0.2
*
* Copyright (C) 2004, Markus (http://www.phpmix.com)
* This script is released under GPL License.
* Feel free to use this script (or part of it) wherever you need
* it ...but please, give credit to original author. Thank you. :-)
* We will also appreciate any links you could give us.
*
* Enjoy! ;-)
*
*************************************************************/
//
// ADVICE: Editor Settings -> TabSize 4 ;-)
//
function SXBB_IsIEMac()
{
// Any better way to detect IEMac?
var ua = String(navigator.userAgent).toLowerCase();
if( document.all && ua.indexOf("mac") >= 0 )
return true;
return false;
}
function SXBB_IsOverflowAble()
{
// Reliable overflow usage seems to require correct DOM support with some exceptions:
// - Opera5/6 renders 'auto' as if it was 'visible'. Bang!
// - IEMac seems to be buggy in some circumstances:
// http://sonofhans.net/mac_ie5_bug/
//
if( document.getElementById && document.childNodes && !SXBB_IsIEMac() )
return true;
return false;
}
function _SXBB(id)
{
this.id = id;
this.size = this.min = 40; // --- Adjust MINIMUM_BLOCK_HEIGHT here :-)
this.extra = 5; // --- This is added to height when expanded
this.margin = 20; // --- Hide [expand] command if just a few pixels height when expanded.
this.T = [];
}
_SXBB.prototype.genCmd = function(cmd, txt)
{
return ' « <a class="genmed" style="text-decoration:none;" href="javascript:void(0)" onclick="SXBB[\''+this.id+'\'].'+cmd+'(\''+txt+'\');" onfocus="this.blur();"><b>'+txt+'</b></a> »';
}
_SXBB.prototype.writeCmd = function()
{
var s='';
if( (document.selection && !SXBB_IsIEMac()) || (document.createRange && (document.getSelection || window.getSelection)) )
{
s += this.genCmd('select', this.T['select']);
}
if( SXBB_IsOverflowAble() )
{
// Actually, the [expand] link will be placed by the onload event (if necessary) :-)
s += '<span id="'+this.id+'x"></span>';
}
document.write(s); // ‹Select› ‹Expand›
}
_SXBB.prototype.writeDiv = function()
{
// Use 'overflow:auto;height:#px' only if 'overflow:auto' is supported...
// Works on IE4+, Mozilla, Opera7, Safari
// Fails on IEMac, NS4, Opera6-
//
var s = ( SXBB_IsOverflowAble() ? 'style="overflow:auto;height:'+this.min+'px;"' : '' );
document.write('<div id="'+this.id+'" '+s+'>');
}
_SXBB.prototype.getObj = function(id)
{
return ( document.getElementById ? document.getElementById(id) : null );
}
_SXBB.prototype.select = function()
{
var o = this.getObj(this.id);
if( !o ) return;
var r, s;
if( document.selection && !SXBB_IsIEMac() )
{
// Works on: IE5+
// To be confirmed: IE4? / IEMac fails?
r = document.body.createTextRange();
r.moveToElementText(o);
r.select();
}
else if( document.createRange && (document.getSelection || window.getSelection) )
{
// Works on: Netscape/Mozilla/Konqueror/Safari
// To be confirmed: Konqueror/Safari use window.getSelection ?
r = document.createRange();
r.selectNodeContents(o);
s = window.getSelection ? window.getSelection() : document.getSelection();
s.removeAllRanges();
s.addRange(r);
}
}
_SXBB.prototype.resize = function(cmd)
{
var o = this.getObj(this.id);
if( !o ) return;
var x = this.getObj(this.id+'x');
if( !x ) return;
// First, deal with requested command...
if( cmd == 'onload' || cmd == 'onresize' )
{
if( o.scrollHeight <= this.min ) { x.innerHTML = ''; o.style.height = 'auto'; return; }
if( x.innerHTML != '' ) return;
if( cmd == 'onload' )
{
x.innerHTML = this.genCmd('resize', this.T['expand']);
return;
}
cmd = this.T['contract'];
}
if( cmd == this.T['expand'] )
{
this.size = o.scrollHeight + this.extra;
o.style.height = 'auto';
o.style.overflow = 'visible';
// Check to conditionally hide the [expand] link, if once expanded, adding [contract] is not really useful.
x.innerHTML = ( (o.scrollHeight-this.margin) > this.min ? this.genCmd('resize', this.T['contract']) : '' );
}
else
{
this.size = this.min;
o.style.height = this.size + 'px';
o.style.overflow = 'auto';
x.innerHTML = this.genCmd('resize', this.T['expand']);
}
if( cmd != 'onresize' )
{
// If necessary, adjust height of outer blocks...
if( o.parentNode ) for( o = o.parentNode; o.parentNode; o = o.parentNode )
{
if( o.tagName && o.tagName == 'DIV' && o.id && o.id.indexOf('SXBB') == 0 )
{
if( !document.all ) SXBB[o.id].resize(this.T['contract']);
SXBB[o.id].resize(this.T['expand']);
}
}
}
return false;
}
// --------------------------------------------------------------------------------
// Actually, this script is being included for every instance of a boxed BBCode,
// so... we MUST do something to ensure we only do the job just once!
//
if( typeof(SXBB) == 'undefined' ) {
// --------------------------------------------------------------------------------
var SXBB = [];
// --------------------------------------------------------------------------------
if( SXBB_IsOverflowAble() ) { // The following is Not necessary on some browsers!
// --------------------------------------------------------------------------------
var SXBB_oldOnLoad = null;
var SXBB_oldOnResize = null;
function SXBB_onLoad()
{
if( SXBB_oldOnLoad ) { SXBB_oldOnLoad(); SXBB_oldOnLoad = null; }
SXBB_evalSize('onload');
}
function SXBB_onResize()
{
if( SXBB_oldOnResize ) { SXBB_oldOnResize(); SXBB_oldOnResize = null; }
SXBB_evalSize('onresize');
}
function SXBB_evalSize(cmd)
{
for( var id in SXBB ) SXBB[id].resize(cmd);
}
if( window.addEventListener )
{
// The DOM method
window.addEventListener('load', SXBB_onLoad, false);
window.addEventListener('resize', SXBB_onResize, false);
}
else if( window.attachEvent )
{
// The IE Method
window.attachEvent('onload', SXBB_onLoad);
window.attachEvent('onresize', SXBB_onResize);
}
else
{
// The 'legacy' method
SXBB_oldOnLoad = window.onload;
SXBB_oldOnResize = window.onresize;
window.onload = SXBB_onLoad;
window.onresize = SXBB_onResize;
}
// --------------------------------------------------------------------------------
} // if( SXBB_IsOverflowAble() )
// --------------------------------------------------------------------------------
} // if( typeof(SXBB) == 'undefined' )
// --------------------------------------------------------------------------------