-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo-setup.js
68 lines (57 loc) · 2.06 KB
/
demo-setup.js
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
(function() {
var domIDs = {
mainInput: 'mainInput',
parseButton: 'parseButton',
mainOutput: 'mainOutput',
bbcodeBold: 'bbcodeBold',
bbcodeItalic: 'bbcodeItalic',
bbcodeURL: 'bbcodeURL',
allowedBBCodes: 'allowedBBCodes',
bbcodeIMG: 'bbcodeIMG',
bbcodeCode: 'bbcodeCode',
theButtons: 'theButtons'
},
domElements = {};
function addEvent(elem, type, eventHandle) {
if (!elem)
return;
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
} else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, eventHandle );
} else {
elem["on"+type]=eventHandle;
}
}
function insertAtCursor(myField, myValueBefore, myValueAfter) {
var startPos, endPos;
myField.focus();
if (document.selection) {
document.selection.createRange().text = myValueBefore + document.selection.createRange().text + myValueAfter;
} else if (myField.selectionStart || myField.selectionStart == '0') {
startPos = myField.selectionStart;
endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) + myValueBefore + myField.value.substring(startPos, endPos)+ myValueAfter + myField.value.substring(endPos, myField.value.length);
myField.setSelectionRange(startPos,endPos + myValueBefore.length + myValueAfter.length);
} else {
myField.value += myValueBefore + myValueAfter;
}
}
function createButtons() {
var theElement, i;
for (i = BBCodeParser.allowedTags.length - 1; i >= 0 ; i--) {
theElement = document.createElement('button');
theElement.innerHTML = BBCodeParser.allowedTags[i];
domElements['theButtons'].appendChild(theElement);
addEvent(theElement, 'click', function() { insertAtCursor(domElements['mainInput'],'[' + this.innerHTML + ']','[/' + this.innerHTML + ']'); });
}
}
function doParse() {
domElements['mainOutput'].innerHTML = BBCodeParser.process(domElements['mainInput'].value);
}
for(var key in domIDs) {
domElements[key] = document.getElementById(domIDs[key]);
}
createButtons();
addEvent(domElements['parseButton'], 'click', doParse);
})();