forked from meltingice/Osimo-BBCode-Editor
/
osimo_editor.js
executable file
·428 lines (379 loc) · 12.8 KB
/
osimo_editor.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Osimo - next-generation forum system
* Licensed under GPLv3 (GPL-LICENSE.txt)
*
* os-includes/js/osimo_editor.js - Javascript for Osimo's BBCode editor
* The class is included in the standard Osimo header, but
* is *not* automatically instantiated; the theme developer must do it manually.
* This is to allow for more flexibilty and more control over it.
* --------------------------------------------------------------
* The Osimo BBCode Editor requires jQuery >= 1.3.x. This is already included
* with Osimo automatically, but if you are using this outside of
* Osimo you will need to provide jQuery on your own.
*/
(function($){
$.fn.osimoeditor = function(customOptions,data){
$.fn.osimoeditor.$this = $(this); //sets access to jQuery object returned from selector
if(typeof customOptions == 'string'){
if(customOptions == 'get'){
return $.fn.osimoeditor.get($(this).attr('id'));
}
if(customOptions == 'set'){
$.fn.osimoeditor.set($(this).attr('id'),data);
}
if(customOptions == 'append'){
$.fn.osimoeditor.append($(this).attr('id'),data);
}
if(customOptions == 'prepend'){
$.fn.osimoeditor.prepend($(this).attr('id'),data);
}
}
var options = $.extend({},$.fn.osimoeditor.defaultOptions, customOptions);
var num = 0;
this.each(function(){
if(!$(this).is('textarea')) return;
var input = $(this);
new OsimoEditor(input,options,num);
num++;
});
}
$.fn.osimoeditor.defaultOptions = {
/*
* This should be the only thing you will have to edit
* if not using this editor as a part of Osimo.
* Enter in the path to the folder that contains the
* osimo_editor files. This path *must* have a trailing
* slash and is relative to the document root.
*/
editor_path : "js/",
/*
* You can change this value to any theme that you have
* in the themes folder for the Osimo Editor. This is
* simply the folder name that the theme files are in.
* You can override this value with the options parameter.
*/
theme : 'default',
width : '500px',
height : false,
editorHeight: '100px',
styles : {},
editorStyles : {}
}
/* Do not edit the rest of the code below unless you know what you are doing! */
$.fn.osimoeditor.majorVersion = "1";
$.fn.osimoeditor.minorVersion = "1";
$.fn.osimoeditor.statusVersion = "0";
$.fn.osimoeditor.releaseDate = "February 19, 2010 @ 11:57am EDT";
$.fn.osimoeditor.get = function(editorID){
return $('#'+editorID+'_editbox').attr('value');
}
$.fn.osimoeditor.set = function(editorID,data){
$('#'+editorID+'_editbox').attr('value',data);
}
$.fn.osimoeditor.append = function(editorID,data){
$('#'+editorID+'_editbox').attr('value',this.get(editorID) + data);
}
$.fn.osimoeditor.prepend = function(editorID,data){
$('#'+editorID+'_editbox').attr('value',data + this.get(editorID));
}
function OsimoEditor(input,options,num){
this.input = input;
this.options = options;
this.num = num;
this.init();
}
/*
* this.init(elements,options)
* Runs some initialization checks to make sure
* everything will run smoothly. This is automatically
* called by the constructor.
* This function also automatically starts the
* editor building process.
* ---------------------------------------------------
* elements: array of elements (Strings) to turn into editors
* options: object with various customizations for the editor
*/
OsimoEditor.prototype.init = function(){
if(this.options.editor_path.substring(this.options.editor_path.length-1,this.options.editor_path.length) != "/"){
this.options.editor_path += "/";
}
this.options.theme_path = this.options.editor_path + "themes/" + this.options.theme + "/";
this.template = '';
this.template_size = 0;
this.controls = new OsimoEditorControls();
this.injectCSS();
this.loadTheme();
}
OsimoEditor.prototype.injectCSS = function(){
$('head').append('<link rel="stylesheet" href="'+this.options.theme_path+'osimo_editor.css" type="text/css" />');
}
OsimoEditor.prototype.loadTheme = function(){
if(this.template == '' && 'sessionStorage' in window){
var temp = window.sessionStorage.osimo_bbeditor_theme;
var temp_size = window.sessionStorage.osimo_bbeditor_theme_size;
if(temp != null && temp_size != null && temp_size > 0 && temp.length == temp_size){
this.template = temp;
this.template_size = temp_size;
}
}
if(this.template == ''){
var obj = this;
var postData = {"theme":this.options.theme};
$.ajax({
type:'POST',
url:obj.options.editor_path+"theme_loader.php",
data:postData,
dataType:'json',
success:function(data){
obj.template = data.html;
obj.template_size = data.size;
if('sessionStorage' in window){
window.sessionStorage.osimo_bbeditor_theme = data.html;
window.sessionStorage.osimo_bbeditor_theme_size = data.size;
}
obj.buildEditor();
}
});
}
else{
this.buildEditor();
}
}
OsimoEditor.prototype.buildEditor = function(){
/* Copy any preset content that might be in the textarea */
var contents = this.input.attr('value');
var inputID;
this.input.attr('id') != null ?
inputID = this.input.attr('id') :
inputID = 'osimo_editor' + num;
var that = this;
var eleAttr = {
id : inputID+'_editbox',
eClass : that.input.attr('class'),
style : that.input.attr('style'),
name : that.input.attr('name')
};
/* Begin DOM Injection */
var inject = '<textarea ';
$.each(eleAttr,function(i,val){
if(i=='eClass'){ inject += 'class="'+val+'" '; }
else{
if(val){
inject += i + '="'+val+'" ';
}
}
});
inject += '>'+contents+'</textarea>';
var template = $(this.template).attr('id',this.input.attr('id'));
var template_html = template.clone().appendTo('<div>').parent().html().replace(/\{\*osimo_editor\*\}/,inject);
this.input.replaceWith(template_html);
/* End DOM injection */
if(!$('#'+eleAttr.id).hasClass('osimo-editor-editbox')){
$('#'+eleAttr.id).addClass('osimo-editor-editbox');
}
if(this.options.width){
$('#'+inputID).css({'width':that.options.width});
$('#'+eleAttr.id).css({'width':that.options.width});
}
if(this.options.height){
$('#'+inputID).css({'height':that.options.height});
}
if(this.options.editorHeight){
$('#'+eleAttr.id).css({'height':that.options.editorHeight});
}
if(this.options.styles){
$('#'+inputID).css(that.options.styles);
}
if(this.options.editorStyles){
$('#'+eleAttr.id).css(that.options.editorStyles);
}
this.controls.activate(inputID);
}
function OsimoEditorControls(){
}
OsimoEditorControls.prototype.activate = function(inputID){
/* First add click events to all buttons */
this.input = inputID;
var that = this;
$.each($('#'+this.input+' .osimo-editor-menu, #'+this.input+' .osimo-editor-button'),function(){
if($(this).hasClass('osimo-editor-menu')){ var action = 'change'; }
else{ var action = 'click'; }
$(this).bind(action,function(){
var func_name = $(this).attr('class').split(" ")[1].replace(/\-/gi,'_')+'()';
func_name = func_name.split("osimo_editor_")[1];
eval('that.'+func_name+';');
});
});
}
/*
* this.getTextSelection()
* Utility function that returns the text that is selected
* and is cross-browser compatible.
*/
OsimoEditorControls.prototype.getTextSelection = function(){
var textarea = $('#'+this.input+'_editbox').get(0);
if (window.getSelection) {
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
var result = {"textarea":textarea,"start":start,"end":end,"len":len,"sel":sel};
return result;
}
else if (document.selection) {
/* Damn you IE, why must you torture me so? */
var range = document.selection.createRange();
var stored_range = range.duplicate();
stored_range.moveToElementText( textarea );
stored_range.setEndPoint( 'EndToEnd', range );
textarea.selectionStart = stored_range.text.length - range.text.length;
textarea.selectionEnd = textarea.selectionStart + range.text.length;
var result = {"textarea":textarea,"start":textarea.selectionStart,"end":textarea.selectionEnd,"len":textarea.value.length,"sel":range.text};
return result;
}
}
OsimoEditorControls.prototype.replaceText = function(text){
var inputID = '#'+this.input+'_editbox';
var replace = $(inputID).attr('value').substring(0,text.start) + text.replace + text.textarea.value.substring(text.end,text.len);
$(inputID).attr('value',replace).focus();
}
OsimoEditorControls.prototype.simpleReplace = function(wrapper){
var text = this.getTextSelection();
text.replace = '['+wrapper+']' + text.sel + '[/'+wrapper+']';
this.replaceText(text);
}
OsimoEditorControls.prototype.customReplace = function(before,after){
var text = this.getTextSelection();
text.replace = before + text.sel + after;
this.replaceText(text);
}
OsimoEditorControls.prototype.conditionReplace = function(textSelected,textNotSelected){
var text = this.getTextSelection();
if(text.sel){ textSelected(text); }
else{ textNotSelected(text); }
}
OsimoEditorControls.prototype.text_bold = function(){
this.simpleReplace('b');
}
OsimoEditorControls.prototype.text_italic = function(){
this.simpleReplace('i');
}
OsimoEditorControls.prototype.text_underline = function(){
this.simpleReplace('u');
}
OsimoEditorControls.prototype.align_left = function(){
this.simpleReplace('left');
}
OsimoEditorControls.prototype.align_center = function(){
this.simpleReplace('center');
}
OsimoEditorControls.prototype.align_right = function(){
this.simpleReplace('right');
}
OsimoEditorControls.prototype.no_code = function(){
this.simpleReplace('nocode');
}
OsimoEditorControls.prototype.bullet_list = function(){
this.customReplace('[list]\n[*]','\n[/list]');
}
OsimoEditorControls.prototype.font_family = function(){
var font = $('#'+this.input+" .osimo-editor-font-family").attr('value');
if(font=="") return;
this.customReplace("[font=" + font + "]","[/font]");
}
OsimoEditorControls.prototype.font_size = function(){
var size = $('#'+this.input+" .osimo-editor-font-size").attr('value');
if(size=="") return;
this.customReplace("[size=" + size + "]","[/size]");
}
OsimoEditorControls.prototype.font_color = function(){
var color = $('#'+this.input+" .osimo-editor-font-color").attr('value');
if(color=="" || !color) return;
this.customReplace("[color=" + color + "]","[/color]");
}
OsimoEditorControls.prototype.quote_user = function(){
var user = prompt("Enter the username of the person you are quoting (optional).","");
if(user==null) return;
if(user==''){
this.simpleReplace('quote');
}
else{
this.customReplace("[quote=" + user + "]","[/quote]");
}
}
OsimoEditorControls.prototype.image_add = function(){
var that = this;
this.conditionReplace(
function(text){
text.replace = "[img]" + text.sel + "[/img]";
that.replaceText(text);
},
function(text){
var url = prompt("Enter the URL to the image.","http://");
if(url==null || url=="") return;
text.replace = "[img]" + url + "[/img]";
that.replaceText(text);
}
);
}
OsimoEditorControls.prototype.link_add = function(){
var that = this;
this.conditionReplace(
function(text){
var url = prompt("Enter the URL you wish to link to.","http://");
if(url==null || url=="") return;
text.replace = "[url=" + url + "]" + text.sel + "[/url]";
that.replaceText(text);
},
function(text){
var url = prompt("Enter the URL you wish to link to.","http://");
if(url==null || url=="") return;
var content = prompt("Enter the text you want to turn into a link (optional).","");
if(content==null) return;
if(content==""){
text.replace = "[url]" + url + "[/url]";
}
else{
text.replace = "[url=" + url + "]" + content + "[/url]";
}
that.replaceText(text);
}
);
}
OsimoEditorControls.prototype.email_add = function(){
var that = this;
this.conditionReplace(
function(text){
var email = prompt("Enter the email address you wish to link to.","");
if(email==null || email=="") return;
text.replace = "[email=" + email + "]" + text.sel + "[/email]";
that.replaceText(text);
},
function(text){
var email = prompt("Enter the email address you wish to link to.","");
if(email==null || email=="") return;
var content = prompt("Enter the text you want to turn into a link (optional).","");
if(content==null) return;
if(content==""){
text.replace = "[email]" + email + "[/email]";
}
else{
text.replace = "[email=" + email + "]" + content + "[/email]";
}
that.replaceText(text);
}
);
}
OsimoEditorControls.prototype.zoom_in = function(){
var inputID = '#'+this.input+'_editbox';
var curFont = Number($(inputID).css('font-size').split('px')[0]);
var newSize = curFont + 2;
$(inputID).css({'font-size':newSize+'px'});
}
OsimoEditorControls.prototype.zoom_out = function(){
var inputID = '#'+this.input+'_editbox';
var curFont = Number($(inputID).css('font-size').split('px')[0]);
var newSize = curFont - 2;
$(inputID).css({'font-size':newSize+'px'});
}
})(jQuery);