pm_editor.js
7.05 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
207
208
209
210
211
212
213
214
215
216
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
function isUndefined(variable) {
return typeof variable == 'undefined' ? true : false;
}
function $(id) {
return document.getElementById(id);
}
function fetchOffset(obj) {
var left_offset = obj.offsetLeft;
var top_offset = obj.offsetTop;
while((obj = obj.offsetParent) != null) {
left_offset += obj.offsetLeft;
top_offset += obj.offsetTop;
}
return { 'left' : left_offset, 'top' : top_offset };
}
function _attachEvent(obj, evt, func) {
if(obj.addEventListener) {
obj.addEventListener(evt, func, false);
} else if(obj.attachEvent) {
obj.attachEvent("on" + evt, func);
}
}
function strlen(str) {
return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}
var menus = new menu_handler();
function menu_handler() {
this.menu = Array();
}
function menuitems() {
this.ctrlobj = null,
this.menuobj = null;
this.parentids = Array();
this.allowhide = 1;
this.hidelock = 0;
this.clickstatus = 0;
}
function menuobjpos(id, offset) {
if(!menus.menu[id]) {
return;
}
if(!offset) {
offset = 0;
}
var showobj = menus.menu[id].ctrlobj;
var menuobj = menus.menu[id].menuobj;
showobj.pos = fetchOffset(showobj);
showobj.X = showobj.pos['left'];
showobj.Y = showobj.pos['top'];
showobj.w = showobj.offsetWidth;
showobj.h = showobj.offsetHeight;
menuobj.w = menuobj.offsetWidth;
menuobj.h = menuobj.offsetHeight;
if(offset < 3) {
menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
} else if(offset == 3) {
menuobj.style.left = (document.body.clientWidth - menuobj.clientWidth) / 2 + document.body.scrollLeft + 'px';
menuobj.style.top = (document.body.clientHeight - menuobj.clientHeight) / 2 + document.body.scrollTop + 'px';
} else if(offset == 4) {
menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + showobj.w + 'px';
menuobj.style.top = showobj.Y + 'px';
}
if(menuobj.style.clip && !is_opera) {
menuobj.style.clip = 'rect(auto, auto, auto, auto)';
}
}
function showmenu(event, id, click, position) {
if(isUndefined(click)) click = false;
if(!menus.menu[id]) {
menus.menu[id] = new menuitems();
menus.menu[id].ctrlobj = $(id);
if(!menus.menu[id].ctrlobj.getAttribute('parentmenu')) {
menus.menu[id].parentids = Array();
} else {
menus.menu[id].parentids = menus.menu[id].ctrlobj.getAttribute('parentmenu').split(',');
}
menus.menu[id].menuobj = $(id + '_menu');
menus.menu[id].menuobj.style.position = 'absolute';
if(event.type == 'mouseover') {
_attachEvent(menus.menu[id].ctrlobj, 'mouseout', function() { setTimeout(function() {hidemenu(id)}, 100); });
_attachEvent(menus.menu[id].menuobj, 'mouseover', function() { lockmenu(id, 0); });
_attachEvent(menus.menu[id].menuobj, 'mouseout', function() { lockmenu(id, 1);setTimeout(function() {hidemenu(id)}, 100); });
} else if(click || event.type == 'click') {
menus.menu[id].clickstatus = 1;
lockmenu(id, 0);
}
} else if(menus.menu[id].clickstatus == 1) {
lockmenu(id, 1);
hidemenu(id);
menus.menu[id].clickstatus = 0;
return;
}
menuobjpos(id, position);
menus.menu[id].menuobj.style.display = '';
}
function hidemenu(id) {
if(!menus.menu[id] || !menus.menu[id].allowhide || menus.menu[id].hidelock) {
return;
}
menus.menu[id].menuobj.style.display = 'none';
}
function lockmenu(id, value) {
if(!menus.menu[id]) {
return;
}
for(i = 0;i < menus.menu[id].parentids.length;i++) {
menus.menu[menus.menu[id].parentids[i]].hidelock = value == 0 ? 1 : 0;
}
menus.menu[id].allowhide = value;
}
var lang = new Array();
function insertunit(text, textend, moveend) {
$('pm_textarea').focus();
textend = isUndefined(textend) ? '' : textend;
moveend = isUndefined(textend) ? 0 : moveend;
startlen = strlen(text);
endlen = strlen(textend);
if(!isUndefined($('pm_textarea').selectionStart)) {
var opn = $('pm_textarea').selectionStart + 0;
if(textend != '') {
text = text + $('pm_textarea').value.substring($('pm_textarea').selectionStart, $('pm_textarea').selectionEnd) + textend;
}
$('pm_textarea').value = $('pm_textarea').value.substr(0, $('pm_textarea').selectionStart) + text + $('pm_textarea').value.substr($('pm_textarea').selectionEnd);
if(!moveend) {
$('pm_textarea').selectionStart = opn + strlen(text) - endlen;
$('pm_textarea').selectionEnd = opn + strlen(text) - endlen;
}
} else if(document.selection && document.selection.createRange) {
var sel = document.selection.createRange();
if(textend != '') {
text = text + sel.text + textend;
}
sel.text = text.replace(/\r?\n/g, '\r\n');
if(!moveend) {
sel.moveStart('character', -endlen);
sel.moveEnd('character', -endlen);
}
sel.select();
} else {
$('pm_textarea').value += text;
}
}
function getSel() {
if(!isUndefined($('pm_textarea').selectionStart)) {
return $('pm_textarea').value.substr($('pm_textarea').selectionStart, $('pm_textarea').selectionEnd - $('pm_textarea').selectionStart);
} else if(document.selection && document.selection.createRange) {
return document.selection.createRange().text;
} else if(window.getSelection) {
return window.getSelection() + '';
} else {
return false;
}
}
function insertlist(type) {
txt = getSel();
type = isUndefined(type) ? '' : '=' + type;
if(txt) {
var regex = new RegExp('([\r\n]+|^[\r\n]*)(?!\\[\\*\\]|\\[\\/?list)(?=[^\r\n])', 'gi');
txt = '[list' + type + ']\n' + txt.replace(regex, '$1[*]') + '\n' + '[/list]';
insertunit(txt);
} else {
insertunit('[list' + type + ']\n', '[/list]');
while(listvalue = prompt(lang['pm_prompt_list'], '')) {
if(is_opera > 8) {
listvalue = '\n' + '[*]' + listvalue;
insertunit(listvalue);
} else {
listvalue = '[*]' + listvalue + '\n';
insertunit(listvalue);
}
}
}
}
function inserttag(tag, type) {
txt = getSel();
type = isUndefined(type) ? 0 : type;
if(!type) {
if(!txt) {
txt = prompt(lang['pm_prompt_' + tag], '')
}
if(txt) {
insertunit('[' + tag + ']' + txt + '[/' + tag + ']');
}
} else {
txt1 = prompt(lang['pm_prompt_' + tag], '');
if(!txt) {
txt = txt1;
}
if(txt1) {
insertunit('[' + tag + '=' + txt1 + ']' + txt + '[/' + tag + ']');
}
}
}