source: extensions/bbcode_bar/bbcode.js @ 7201

Last change on this file since 7201 was 3305, checked in by patdenice, 15 years ago

New extension added:
BBCode Bar (2.0.a)

File size: 5.4 KB
Line 
1var bbcode = new Array();
2var theSelection = false;
3
4function BBChr(form, field) { BBCwrite(form, field, '', "[hr]", true); }
5function BBCdir(form, field, dirc) { document.forms[form].elements[field].dir=(dirc); }
6function BBCft(form, field, box) { BBCfont(form, field, "font", box); }
7function BBCfs(form, field, box) { 
8        //BBCfont(form, field, "size", box);
9        var val = 10 + 2 * (box.selectedIndex - 2 );
10        if ( val != 0 ) {
11                BBCwrite(form, field, "[size="+ val+"]", "[/size]", true); 
12        }
13        box.selectedIndex = 2;
14}
15function BBCfc(form, field, box) { 
16        BBCfont(form, field, "color", box); 
17        box.selectedIndex = 0;
18}
19function BBCfont(form, field, code, box) { 
20        BBCwrite(form, field, "["+code+"="+box.value+"]", "[/"+code+"]", true); 
21}
22
23function BBCwmi(form, field, type) {
24        if (type == 'img') { var URL = prompt("Please enter image URL","http://"); }
25        else { var URL = prompt("Enter the Email Address",""); }
26        if (URL == null) { return; }
27        if (!URL) { return alert("Error : You didn't write the Address"); }
28        BBCwrite(form, field, '', "["+type+"]"+URL+"[/"+type+"]", true);
29}
30
31function BBCode(form, field, code, img) {
32        var type = img.name;
33        if (BBCwrite(form, field, "["+code+"="+type+"]", "[/"+code+"]")) { return; }
34        if (bbcode[code+type+form+field] == null) {
35                ToAdd = "["+code+"="+type+"]";
36                re = new RegExp(type+".(\\w+)$");
37                img.src = img.src.replace(re, type+"1.$1");
38                bbcode[code+type+form+field] = 1;
39        } else {
40                ToAdd = "[/"+code+"]";
41                re = new RegExp(type+"1.(\\w+)$");
42                img.src = img.src.replace(re, type+".$1");
43                bbcode[code+type+form+field] = null;
44        }
45        BBCwrite(form, field, '', ToAdd, true);
46}
47
48function BBCcode(form, field, img) {
49        var code = img.name;
50        if (BBCwrite(form, field, "["+code+"]", "[/"+code+"]")) { return; }
51        if (bbcode[form+field+code] == null) {
52                ToAdd = "["+code+"]";
53                re = new RegExp(code+".(\\w+)$");
54                img.src = img.src.replace(re, code+"1.$1");
55                bbcode[form+field+code] = 1;
56        } else {
57                ToAdd = "[/"+code+"]";
58                re = new RegExp(code+"1.(\\w+)$");
59                img.src = img.src.replace(re, code+".$1");
60                bbcode[form+field+code] = null;
61        }
62        BBCwrite(form, field, '', ToAdd, true);
63}
64
65function BBClist(form, field, img) {
66        var code = img.name;
67        if (BBCwrite(form, field, "["+code+"]\n[li]", "\n[/"+code+"]")) { return; }
68        if (bbcode[form+field+code] == null) {
69                ToAdd = "["+code+"]\n[li]";
70                re = new RegExp(code+".(\\w+)$");
71                img.src = img.src.replace(re, code+"1.$1");
72                bbcode[form+field+code] = 1;
73        } else {
74                ToAdd = "\n[/"+code+"]\n";
75                re = new RegExp(code+"1.(\\w+)$");
76                img.src = img.src.replace(re, code+".$1");
77                bbcode[form+field+code] = null;
78        }
79        BBCwrite(form, field, '', ToAdd, true);
80}
81
82function BBCmm(form, field, type) {
83        var URL = prompt("Enter the "+type+" file URL", "http://");
84        if (URL == null) { return; }
85        if (!URL) { return alert("Error: You didn't write the "+type+" file URL"); }
86        var WS = prompt("Enter the "+type+" width", "250");
87        if (WS == null) { return; }
88        if (!WS) { WS = 250; }
89        var HS = prompt("Enter the "+type+" height", "200");
90        if (HS == null) { return; }
91        if (!HS) { HS = 200; }
92        BBCwrite(form, field, '', "["+type+" width="+WS+" height="+HS+"]"+URL+"[/"+type+"]", true);
93}
94
95function BBCurl(form, field) {
96        var URL = prompt("Enter the URL", "http://");
97        if (URL == null) { return; }
98        if (!URL) { return alert("Error: You didn't write the URL "); }
99        if (BBCwrite(form, field, "[url="+URL+"]", "[/url]")) { return; }
100        var TITLE = prompt("Enter the page name", "Web Page Name");
101        if (TITLE == null) { return; }
102        var Add = "]"+URL;
103        if (TITLE) { Add = "="+URL+"]"+TITLE; }
104        BBCwrite(form, field, '', "[url"+Add+"[/url]", true);
105}
106
107function BBCwrite(form, field, start, end, force) {
108        var textarea = document.forms[form].elements[field];
109       
110        storeCaret(textarea);
111       
112        if (textarea.caretPos) {
113          textarea.focus();
114                // Attempt to create a text range (IE).
115                theSelection = document.selection.createRange().text;
116                if (force || theSelection != '') {
117                        document.selection.createRange().text = start + theSelection + end;
118                        textarea.focus();
119                        return true;
120                }
121        } else if (typeof(textarea.selectionStart) != "undefined") {
122                // Mozilla text range replace.
123                var text = new Array();
124                text[0] = textarea.value.substr(0, textarea.selectionStart);
125                text[1] = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd-textarea.selectionStart);
126                text[2] = textarea.value.substr(textarea.selectionEnd);
127                caretPos = textarea.selectionEnd+start.length+end.length;
128                if (force || text[1] != '') {
129                        textarea.value = text[0]+start+text[1]+end+text[2];
130                        if (textarea.setSelectionRange) {
131                                textarea.focus();
132                                textarea.setSelectionRange(caretPos, caretPos);
133                        }
134                        return true;
135                }
136        } else if (force) {             
137                // Just put it on the end.
138                textarea.value += start+end;
139                textarea.focus(textarea.value.length-1);
140                return true;
141        }
142        return false;
143}
144
145function storeCaret(text) {
146        if (text.createTextRange) text.caretPos = document.selection.createRange().duplicate();
147}
148
149function bbstyle(bbnumber) {
150        return true;
151}
152
153function preview(form, field) { 
154        if ( document.forms[form].elements[field].value != "" )
155        {       
156                var loc = document.location.toString();
157                var idx = loc.indexOf( '&' )
158               
159                if ( idx > 0 ) 
160                        loc = loc.substring ( 0 , idx );
161               
162                document.location = loc + "&action=preview&content=" + document.forms[form].elements[field].value + "&key=" + document.forms[form].elements["key"].value;
163        }
164}
165
166function helpline ( form, field, text ) {
167  if ( typeof(document.forms[form].elements[field]) != "undefined") {
168          document.forms[form].elements[field].value = text;
169  }
170}
Note: See TracBrowser for help on using the repository browser.