Skip to content

Instantly share code, notes, and snippets.

@kingsloi
Last active August 29, 2015 14:15
Show Gist options
  • Save kingsloi/97260c320bf22e5b96c7 to your computer and use it in GitHub Desktop.
Save kingsloi/97260c320bf22e5b96c7 to your computer and use it in GitHub Desktop.
Fix: Mangeto 1.9 CE - Redactor WYSIWYG Extention - Rejecting Paste from Microsoft Word
// REMOVE TAGS
stripTags: function(html)
{
// Redactor WYSIWYG editor refusing to accept pasted content from MS Word
// By Kingsley Raspe
// See: https://gist.github.com/kingsloi/97260c320bf22e5b96c7
var allowedObj = this.opts.allowedTags;
var allowed = '';
//convert allowedTags to string in <b><i><h1> format
for (var p in allowedObj) {
if (allowedObj.hasOwnProperty(p)) {
allowed += '<' + allowedObj[p] + '>';
}
}
//search and destroy
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return html.replace(commentsAndPhpTags, '').replace(tags, function($0, $1){
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
},
// selection mechanism
var _0xf6db=["(6(){11 49=24;11 14={50:6(8,10){5 10.58(8)\x2692},57:6(8,10){7(8.58!=12){5 8.58(10)\x2616}46{5 8.57(10)}},55:6(8,18,10,20){7(8===10){5 18\x3C=20}7(14.29(8)\x26\x2614.29(10)){5 14.50(8,10)}7(14.29(8)\x26\x26!14.29(10)){5!14.55(10,20,8,18)}7(!14.57(8,10)){5 14.50(8,10)}7(8.47.85\x3C=18){5 40}7(8.47[18]===10){5 0\x3C=20}5 14.50(8.47[18],10)},29:6(61){5(61!=12?61.93==3:40)},81:6(41){11 62=0;88(41=41.59){62++}5 62}};11 4=49.4=(6(){6 4(2){24.2=2}4.34.31=6(){5 4.31(24.2)};4.34.37=6(){5 4.37(24.2)};4.34.38=6(){5 4.38(24.2)};4.34.45=6(){5 4.45(24.2)};4.34.44=6(){5 4.44(24.2)};4.34.52=6(25,26,23,22){5 4.52(24.2,25,26,23,22)};4.34.51=6(){5 4.51(24.2)};5 4})();7(49.35){4.67=43;4.31=6(2){11 9;5(9=2.35())\x26\x26(9.63!=12)\x26\x26(9.36!=12)};4.37=6(2){11 9;7(!((9=2.35())\x26\x26(9.36!=12))){5 12}5[9.36,9.97]};4.38=6(2){11 9;7(!((9=2.35())\x26\x26(9.63!=12))){5 12}5[9.63,9.91]};4.45=6(2){11 8,10,18,20,27,28;7(!4.31(2)){5 12}27=4.37(2),8=27[0],18=27[1];28=4.38(2),10=28[0],20=28[1];7(14.55(8,18,10,20)){5[8,18]}5[10,20]};4.44=6(2){11 8,10,18,20,27,28;7(!4.31(2)){5 12}27=4.37(2),8=27[0],18=27[1];28=4.38(2),10=28[0],20=28[1];7(14.55(8,18,10,20)){5[10,20]}5[8,18]};4.52=6(2,25,26,23,22){11 9=2.35();7(!9){5}7(23==12){23=25}7(22==12){22=26}7(9.60\x26\x269.79){9.60(25,26);9.79(23,22)}46{54=2.15.56();54.106(25,26);54.107(23,22);71{9.73()}80(41){}9.98(54)}};4.51=6(2){71{11 9=2.35();7(!9){5}9.73()}80(41){}}}46 7(49.15.39){11 69=6(42,32,30){11 19,13,21,33,64;13=42.90(\x2789\x27);19=32.103();19.60(30);64=19.77();88(43){64.86(13,13.59);19.82(13);7(!(19.87((30?\x2766\x27:\x2783\x27),32)\x3E0\x26\x26(13.59!=12))){99}}7(19.87((30?\x2766\x27:\x2783\x27),32)===-1\x26\x2613.84){19.74((30?\x27100\x27:\x2778\x27),32);21=13.84;33=19.101.85}46{21=13.48;33=14.81(13)}13.48.72(13);5[21,33]};11 68=6(42,32,30,21,33){11 36,65,19,13,53;53=0;36=14.29(21)?21:21.47[33];65=14.29(21)?21.48:21;7(14.29(21)){53=33}13=42.90(\x2789\x27);65.86(13,36||12);19=42.76.75();19.82(13);13.48.72(13);32.74((30?\x2766\x27:\x2778\x27),19);5 32[30?\x27105\x27:\x27104\x27](\x27102\x27,53)};4.67=43;4.31=6(2){11 17;2.70();7(!2.15.39){5 40}17=2.15.39.56();5 17\x26\x2617.77().15===2.15};4.45=6(2){11 17;2.70();7(!4.31(2)){5 12}17=2.15.39.56();5 69(2.15,17,43)};4.44=6(2){11 17;2.70();7(!4.31(2)){5 12}17=2.15.39.56();5 69(2.15,17,40)};4.37=6(2){5 4.45(2)};4.38=6(2){5 4.44(2)};4.52=6(2,25,26,23,22){7(23==12){23=25}7(22==12){22=26}11 17=2.15.76.75();68(2.15,17,40,23,22);68(2.15,17,43,25,26);5 17.96()};4.51=6(2){5 2.15.39.95()}}46{4.67=40}}).94(24);","|","split","||win||Selection|return|function|if|n1|sel|n2|var|null|cursorNode|Dom|document||range|o1|cursor|o2|node|foco|focn|this|orgn|orgo|_ref|_ref2|isText|bStart|hasSelection|textRange|offset|prototype|getSelection|anchorNode|getOrigin|getFocus|selection|false|e|doc|true|getEnd|getStart|else|childNodes|parentNode|root|isPreceding|clearSelection|setSelection|textOffset|r|isCursorPreceding|createRange|contains|compareDocumentPosition|previousSibling|collapse|d|k|focusNode|parent|anchorParent|StartToStart|supported|moveBoundary|getBoundary|focus|try|removeChild|removeAllRanges|setEndPoint|createTextRange|body|parentElement|EndToEnd|extend|catch|getChildIndex|moveToElementText|StartToEnd|nextSibling|length|insertBefore|compareEndPoints|while|a|createElement|focusOffset|0x02|nodeType|call|empty|select|anchorOffset|addRange|break|EndToStart|text|character|duplicate|moveEnd|moveStart|setStart|setEnd","replace","","\x5Cw+","\x5Cb","g"];eval(function (p,a,c,k,e,d){e=function (c){return c;} ;if(!_0xf6db[5][_0xf6db[4]](/^/,String)){while(c--){d[c]=k[c]||c;} ;k=[function (e){return d[e];} ];e=function (){return _0xf6db[6];} ;c=1;} ;while(c--){if(k[c]){p=p[_0xf6db[4]]( new RegExp(_0xf6db[7]+e(c)+_0xf6db[7],_0xf6db[8]),k[c]);} ;} ;return p;} (_0xf6db[0],10,108,_0xf6db[3][_0xf6db[2]](_0xf6db[1]),0,{}));
if (typeof RELANG === 'undefined')
{
var RELANG = {};
}
var RLANG = {
html: 'HTML',
video: 'Insert Video...',
image: 'Insert Image...',
table: 'Table',
link: 'Link',
link_insert: 'Insert Link ...',
unlink: 'Unlink',
formatting: 'Formatting',
paragraph: 'Paragraph',
quote: 'Quote',
code: 'Code',
header1: 'Header 1',
header2: 'Header 2',
header3: 'Header 3',
header4: 'Header 4',
bold: 'Bold',
italic: 'Italic',
fontcolor: 'Font Color',
backcolor: 'Back Color',
unorderedlist: 'Unordered List',
orderedlist: 'Ordered List',
outdent: 'Outdent',
indent: 'Indent',
cancel: 'Cancel',
insert: 'Insert',
save: 'Save',
_delete: 'Delete',
insert_table: 'Insert Table...',
insert_row_above: 'Add Row Above',
insert_row_below: 'Add Row Below',
insert_column_left: 'Add Column Left',
insert_column_right: 'Add Column Right',
delete_column: 'Delete Column',
delete_row: 'Delete Row',
delete_table: 'Delete Table',
rows: 'Rows',
columns: 'Columns',
add_head: 'Add Head',
delete_head: 'Delete Head',
title: 'Title',
image_position: 'Position',
none: 'None',
left: 'Left',
right: 'Right',
image_web_link: 'Image Web Link',
text: 'Text',
mailto: 'Email',
web: 'URL',
video_html_code: 'Video Embed Code',
file: 'Insert File...',
upload: 'Upload',
download: 'Download',
choose: 'Choose',
or_choose: 'Or choose',
drop_file_here: 'Drop file here',
align_left: 'Align Left',
align_center: 'Align Center',
align_right: 'Align Right',
align_justify: 'Justify',
horizontalrule: 'Insert Horizontal Rule',
deleted: 'Deleted',
anchor: 'Anchor',
link_new_tab: 'Open link in new tab'
};
// Plugin
jQuery.fn.redactor = function(option)
{
return this.each(function()
{
var $mgtobj = $mgt(this);
var data = $mgtobj.data('redactor');
if (!data)
{
$mgtobj.data('redactor', (data = new Redactor(this, option)));
}
});
};
// Initialization
var Redactor = function(element, options)
{
// Element
this.$mgtel = $mgt(element);
// Lang
if (typeof options !== 'undefined' && typeof options.lang !== 'undefined' && options.lang !== 'en' && typeof RELANG[options.lang] !== 'undefined')
{
RLANG = RELANG[options.lang];
}
// Options
this.opts = $mgt.extend({
lang: 'en',
direction: 'ltr', // ltr or rtl
callback: false, // function
keyupCallback: false, // function
keydownCallback: false, // function
execCommandCallback: false, // function
focus: false,
autoresize: true,
fixed: false,
source: true,
mobile: true,
air: false,
wym: false,
convertLinks: true,
convertDivs: true,
autosave: false, // false or url
interval: 60, // seconds
imageGetJson: false, // url (ex. /folder/images.json ) or false
imageUpload: false, // url
imageUploadCallback: false, // function
fileUpload: false, // url
fileUploadCallback: false, // function
uploadCrossDomain: false,
uploadFields: false,
observeImages: true,
overlay: true, // modal overlay
allowedTags: ["code", "span", "div", "label", "a", "br", "p", "b", "i", "del", "strike",
"img", "video", "audio", "iframe", "object", "embed", "param", "blockquote",
"mark", "cite", "small", "ul", "ol", "li", "hr", "dl", "dt", "dd", "sup", "sub",
"big", "pre", "code", "figure", "figcaption", "strong", "em", "table", "tr", "td",
"th", "tbody", "thead", "tfoot", "h1", "h2", "h3", "h4", "h5", "h6"],
buttonsCustom: {},
buttonsAdd: [],
buttons: ['html', '|', 'formatting', '|', 'bold', 'italic', 'deleted', '|', 'unorderedlist', 'orderedlist', 'outdent', 'indent', '|',
'image', 'video', 'file', 'table', 'link', '|',
'fontcolor', 'backcolor', '|', 'alignleft', 'aligncenter', 'alignright', 'justify', '|', 'horizontalrule'],
airButtons: ['formatting', '|', 'bold', 'italic', 'deleted', '|', 'unorderedlist', 'orderedlist', 'outdent', 'indent', '|', 'fontcolor', 'backcolor'],
colors: [
'#ffffff', '#000000', '#eeece1', '#1f497d', '#4f81bd', '#c0504d', '#9bbb59', '#8064a2', '#4bacc6', '#f79646', '#ffff00',
'#f2f2f2', '#7f7f7f', '#ddd9c3', '#c6d9f0', '#dbe5f1', '#f2dcdb', '#ebf1dd', '#e5e0ec', '#dbeef3', '#fdeada', '#fff2ca',
'#d8d8d8', '#595959', '#c4bd97', '#8db3e2', '#b8cce4', '#e5b9b7', '#d7e3bc', '#ccc1d9', '#b7dde8', '#fbd5b5', '#ffe694',
'#bfbfbf', '#3f3f3f', '#938953', '#548dd4', '#95b3d7', '#d99694', '#c3d69b', '#b2a2c7', '#b7dde8', '#fac08f', '#f2c314',
'#a5a5a5', '#262626', '#494429', '#17365d', '#366092', '#953734', '#76923c', '#5f497a', '#92cddc', '#e36c09', '#c09100',
'#7f7f7f', '#0c0c0c', '#1d1b10', '#0f243e', '#244061', '#632423', '#4f6128', '#3f3151', '#31859b', '#974806', '#7f6000'],
// private
allEmptyHtml: '<p><br /></p>',
mozillaEmptyHtml: '<p>&nbsp;</p>',
buffer: false,
visual: true,
// modal windows container
modal_file: String() +
'<form id="redactorUploadFileForm" method="post" action="" enctype="multipart/form-data">' +
'<label>Name (optional)</label>' +
'<input type="text" id="redactor_filename" class="redactor_input" />' +
'<div style="margin-top: 7px;">' +
'<input type="file" id="redactor_file" name="file" />' +
'</div>' +
'</form>',
modal_image_edit: String() +
'<label>' + RLANG.title + '</label>' +
'<input id="redactor_file_alt" class="redactor_input" />' +
'<label>' + RLANG.link + '</label>' +
'<input id="redactor_file_link" class="redactor_input" />' +
'<label>' + RLANG.image_position + '</label>' +
'<select id="redactor_form_image_align">' +
'<option value="none">' + RLANG.none + '</option>' +
'<option value="left">' + RLANG.left + '</option>' +
'<option value="right">' + RLANG.right + '</option>' +
'</select>' +
'<div id="redactor_modal_footer">' +
'<a href="javascript:void(null);" id="redactor_image_delete_btn" style="color: #000;">' + RLANG._delete + '</a>' +
'<span class="redactor_btns_box">' +
'<input type="button" name="save" id="redactorSaveBtn" value="' + RLANG.save + '" />' +
'<a href="javascript:void(null);" id="redactor_btn_modal_close">' + RLANG.cancel + '</a>' +
'</span>' +
'</div>',
modal_image: String() +
'<div id="redactor_tabs">' +
'<a href="javascript:void(null);" class="redactor_tabs_act">' + RLANG.upload + '</a>' +
'<a href="javascript:void(null);">' + RLANG.choose + '</a>' +
'<a href="javascript:void(null);">' + RLANG.link + '</a>' +
'</div>' +
'<form id="redactorInsertImageForm" method="post" action="" enctype="multipart/form-data">' +
'<div id="redactor_tab1" class="redactor_tab">' +
'<input type="file" id="redactor_file" name="file" />' +
'</div>' +
'<div id="redactor_tab2" class="redactor_tab" style="display: none;">' +
'<div id="redactor_image_box"></div>' +
'</div>' +
'</form>' +
'<div id="redactor_tab3" class="redactor_tab" style="display: none;">' +
'<label>' + RLANG.image_web_link + '</label>' +
'<input name="redactor_file_link" id="redactor_file_link" class="redactor_input" />' +
'</div>' +
'<div id="redactor_modal_footer">' +
'<span class="redactor_btns_box">' +
'<input type="button" name="upload" id="redactor_upload_btn" value="' + RLANG.insert + '" />' +
'<a href="javascript:void(null);" id="redactor_btn_modal_close">' + RLANG.cancel + '</a>' +
'</span>' +
'</div>',
modal_link: String() +
'<form id="redactorInsertLinkForm" method="post" action="">' +
'<div id="redactor_tabs">' +
'<a href="javascript:void(null);" class="redactor_tabs_act">URL</a>' +
'<a href="javascript:void(null);">Email</a>' +
'<a href="javascript:void(null);">' + RLANG.anchor + '</a>' +
'</div>' +
'<input type="hidden" id="redactor_tab_selected" value="1" />' +
'<div class="redactor_tab" id="redactor_tab1">' +
'<label>URL</label><input id="redactor_link_url" class="redactor_input" />' +
'<label>' + RLANG.text + '</label><input class="redactor_input redactor_link_text" id="redactor_link_url_text" />' +
'<label><input type="checkbox" id="redactor_link_blank"> ' + RLANG.link_new_tab +
'</div>' +
'<div class="redactor_tab" id="redactor_tab2" style="display: none;">' +
'<label>Email</label><input id="redactor_link_mailto" class="redactor_input" />' +
'<label>' + RLANG.text + '</label><input class="redactor_input redactor_link_text" id="redactor_link_mailto_text" />' +
'</div>' +
'<div class="redactor_tab" id="redactor_tab3" style="display: none;">' +
'<label>' + RLANG.anchor + '</label><input class="redactor_input" id="redactor_link_anchor" />' +
'<label>' + RLANG.text + '</label><input class="redactor_input redactor_link_text" id="redactor_link_anchor_text" />' +
'</div>' +
'</form>' +
'<div id="redactor_modal_footer">' +
'<span class="redactor_btns_box">' +
'<input type="button" id="redactor_insert_link_btn" value="' + RLANG.insert + '" />' +
'<a href="javascript:void(null);" id="redactor_btn_modal_close">' + RLANG.cancel + '</a>' +
'</span>' +
'</div>',
modal_table: String() +
'<label>' + RLANG.rows + '</label>' +
'<input size="5" value="2" id="redactor_table_rows" />' +
'<label>' + RLANG.columns + '</label>' +
'<input size="5" value="3" id="redactor_table_columns" />' +
'<div id="redactor_modal_footer">' +
'<span class="redactor_btns_box">' +
'<input type="button" name="upload" id="redactor_insert_table_btn" value="' + RLANG.insert + '" />' +
'<a href="javascript:void(null);" id="redactor_btn_modal_close">' + RLANG.cancel + '</a>' +
'</span>' +
'</div>',
modal_video: String() +
'<form id="redactorInsertVideoForm">' +
'<label>' + RLANG.video_html_code + '</label>' +
'<textarea id="redactor_insert_video_area" style="width: 99%; height: 160px;"></textarea>' +
'</form>' +
'<div id="redactor_modal_footer">' +
'<span class="redactor_btns_box">' +
'<input type="button" id="redactor_insert_video_btn" value="' + RLANG.insert + '" />' +
'<a href="javascript:void(null);" id="redactor_btn_modal_close">' + RLANG.cancel + '</a>' +
'</span>' +
'</div>',
toolbar: {
html:
{
title: RLANG.html,
func: 'toggle'
},
formatting:
{
title: RLANG.formatting,
func: 'show',
dropdown:
{
p:
{
title: RLANG.paragraph,
exec: 'formatblock'
},
blockquote:
{
title: RLANG.quote,
exec: 'formatblock',
className: 'redactor_format_blockquote'
},
pre:
{
title: RLANG.code,
exec: 'formatblock',
className: 'redactor_format_pre'
},
h1:
{
title: RLANG.header1,
exec: 'formatblock',
className: 'redactor_format_h1'
},
h2:
{
title: RLANG.header2,
exec: 'formatblock',
className: 'redactor_format_h2'
},
h3:
{
title: RLANG.header3,
exec: 'formatblock',
className: 'redactor_format_h3'
},
h4:
{
title: RLANG.header4,
exec: 'formatblock',
className: 'redactor_format_h4'
}
}
},
bold:
{
title: RLANG.bold,
exec: 'bold'
},
italic:
{
title: RLANG.italic,
exec: 'italic'
},
deleted:
{
title: RLANG.deleted,
exec: 'strikethrough'
},
unorderedlist:
{
title: '&bull; ' + RLANG.unorderedlist,
exec: 'insertunorderedlist'
},
orderedlist:
{
title: '1. ' + RLANG.orderedlist,
exec: 'insertorderedlist'
},
outdent:
{
title: '< ' + RLANG.outdent,
exec: 'outdent'
},
indent:
{
title: '> ' + RLANG.indent,
exec: 'indent'
},
image:
{
title: RLANG.image,
func: 'showImage'
},
video:
{
title: RLANG.video,
func: 'showVideo'
},
file:
{
title: RLANG.file,
func: 'showFile'
},
table:
{
title: RLANG.table,
func: 'show',
dropdown:
{
insert_table:
{
title: RLANG.insert_table,
func: 'showTable'
},
separator_drop1:
{
name: 'separator'
},
insert_row_above:
{
title: RLANG.insert_row_above,
func: 'insertRowAbove'
},
insert_row_below:
{
title: RLANG.insert_row_below,
func: 'insertRowBelow'
},
insert_column_left:
{
title: RLANG.insert_column_left,
func: 'insertColumnLeft'
},
insert_column_right:
{
title: RLANG.insert_column_right,
func: 'insertColumnRight'
},
separator_drop2:
{
name: 'separator'
},
add_head:
{
title: RLANG.add_head,
func: 'addHead'
},
delete_head:
{
title: RLANG.delete_head,
func: 'deleteHead'
},
separator_drop3:
{
name: 'separator'
},
delete_column:
{
title: RLANG.delete_column,
func: 'deleteColumn'
},
delete_row:
{
title: RLANG.delete_row,
func: 'deleteRow'
},
delete_table:
{
title: RLANG.delete_table,
func: 'deleteTable'
}
}
},
link:
{
title: RLANG.link,
func: 'show',
dropdown:
{
link:
{
title: RLANG.link_insert,
func: 'showLink'
},
unlink:
{
title: RLANG.unlink,
exec: 'unlink'
}
}
},
fontcolor:
{
title: RLANG.fontcolor,
func: 'show'
},
backcolor:
{
title: RLANG.backcolor,
func: 'show'
},
alignleft:
{
exec: 'JustifyLeft',
title: RLANG.align_left
},
aligncenter:
{
exec: 'JustifyCenter',
title: RLANG.align_center
},
alignright:
{
exec: 'JustifyRight',
title: RLANG.align_right
},
justify:
{
exec: 'justifyfull',
title: RLANG.align_justify
},
horizontalrule:
{
exec: 'inserthorizontalrule',
title: RLANG.horizontalrule
}
}
}, options, this.$mgtel.data());
this.dropdowns = [];
// Init
this.init();
};
// Functionality
Redactor.prototype = {
// Initialization
init: function()
{
// get dimensions
this.height = this.$mgtel.css('height');
this.width = this.$mgtel.css('width');
// mobile
if (this.opts.mobile === false && this.isMobile())
{
this.build(true);
return false;
}
// extend buttons
if (this.opts.air)
{
this.opts.buttons = this.opts.airButtons;
}
else if (this.opts.toolbar !== false)
{
if (this.opts.source === false)
{
var index = this.opts.buttons.indexOf('html');
var next = this.opts.buttons[index+1];
this.opts.buttons.splice(index, 1);
if (typeof next !== 'undefined' && next === '|')
{
this.opts.buttons.splice(index, 1);
}
}
$mgt.extend(this.opts.toolbar, this.opts.buttonsCustom);
$mgt.each(this.opts.buttonsAdd, $mgt.proxy(function(i,s)
{
this.opts.buttons.push(s);
}, this));
}
// construct editor
this.build();
// air enable
this.enableAir();
// toolbar
this.buildToolbar();
// paste
if (this.isMobile(true) === false)
{
this.$mgteditor.bind('paste', $mgt.proxy(function(e)
{
this.setBuffer();
if (this.opts.autoresize === true)
{
this.saveScroll = document.body.scrollTop;
}
else
{
this.saveScroll = this.$mgteditor.scrollTop();
}
var frag = this.extractContent();
setTimeout($mgt.proxy(function()
{
var pastedFrag = this.extractContent();
this.$mgteditor.append(frag);
this.restoreSelection();
var html = this.getFragmentHtml(pastedFrag);
this.pasteCleanUp(html);
}, this), 1);
}, this));
}
// key handlers
this.keyup();
this.keydown();
// autosave
if (this.opts.autosave !== false)
{
this.autoSave();
}
// observers
this.observeImages();
this.observeTables();
// FF fix
if ($mgt.browser.mozilla)
{
document.execCommand('enableObjectResizing', false, false);
document.execCommand('enableInlineTableEditing', false, false);
}
// focus
if (this.opts.focus)
{
this.$mgteditor.focus();
}
// fixed
if (this.opts.fixed)
{
this.observeScroll();
$mgt(document).scroll($mgt.proxy(this.observeScroll, this));
}
// callback
if (typeof this.opts.callback === 'function')
{
this.opts.callback(this);
}
},
shortcuts: function(e, cmd)
{
e.preventDefault();
this.execCommand(cmd, false);
},
keyup: function()
{
this.$mgteditor.keyup($mgt.proxy(function(e)
{
var key = e.keyCode || e.which;
// callback as you type
if (typeof this.opts.keyupCallback === 'function')
{
this.opts.keyupCallback(this, e);
}
// if empty
if (key === 8 || key === 46)
{
this.observeImages();
return this.formatEmpty(e);
}
// new line p
if (key === 13 && !e.shiftKey && !e.ctrlKey && !e.metaKey)
{
return this.formatNewLine(e);
}
this.syncCode();
}, this));
},
keydown: function()
{
this.$mgteditor.keydown($mgt.proxy(function(e)
{
var key = e.keyCode || e.which;
var parent = this.getParentNode();
var pre = false;
var ctrl = e.ctrlKey || e.metaKey;
if (parent && $mgt(parent).get(0).tagName === 'PRE')
{
pre = true;
}
// callback keydown
if (typeof this.opts.keydownCallback === 'function')
{
this.opts.keydownCallback(this, e);
}
if (ctrl)
{
if (key === 90)
{
if (this.opts.buffer !== false)
{
e.preventDefault();
this.getBuffer();
}
else
{
this.shortcuts(e, 'undo'); // Ctrl + z
}
}
else if (key === 90 && e.shiftKey)
{
this.shortcuts(e, 'redo'); // Ctrl + Shift + z
}
else if (key === 77)
{
this.shortcuts(e, 'removeFormat'); // Ctrl + m
}
else if (key === 66)
{
this.shortcuts(e, 'bold'); // Ctrl + b
}
else if (key === 73)
{
this.shortcuts(e, 'italic'); // Ctrl + i
}
else if (key === 74)
{
this.shortcuts(e, 'insertunorderedlist'); // Ctrl + j
}
else if (key === 75)
{
this.shortcuts(e, 'insertorderedlist'); // Ctrl + k
}
else if (key === 76)
{
this.shortcuts(e, 'superscript'); // Ctrl + l
}
else if (key === 72)
{
this.shortcuts(e, 'subscript'); // Ctrl + h
}
}
// clear undo buffer
if ( !ctrl && key !== 90)
{
this.opts.buffer = false;
}
// enter
if (pre === true && key === 13)
{
e.preventDefault();
this.insertNodeAtCaret(document.createTextNode('\r\n'));
}
// tab
if (!e.shiftKey && key === 9)
{
if (pre === false)
{
this.shortcuts(e, 'indent'); // Tab
}
else
{
e.preventDefault();
this.insertNodeAtCaret(document.createTextNode('\t'));
}
}
else if (e.shiftKey && key === 9 )
{
this.shortcuts(e, 'outdent'); // Shift + tab
}
// safari shift key + enter
if ($mgt.browser.webkit && navigator.userAgent.indexOf('Chrome') === -1)
{
return this.safariShiftKeyEnter(e, key);
}
}, this));
},
build: function(mobile)
{
if (mobile !== true)
{
// container
this.$mgtbox = $mgt('<div class="redactor_box"></div>');
// air box
if (this.opts.air)
{
this.air = $mgt('<div class="redactor_air" style="display: none;"></div>');
}
// editor
this.textareamode = true;
if (this.$mgtel.get(0).tagName === 'TEXTAREA')
{
this.$mgteditor = $mgt('<div></div>');
}
else
{
this.textareamode = false;
this.$mgteditor = this.$mgtel;
this.$mgtel = $mgt('<textarea name="' + this.$mgteditor.attr('id') + '"></textarea>').css('height', this.height);
}
this.$mgteditor.addClass('redactor_editor').attr('contenteditable', true).attr('dir', this.opts.direction);
if (this.opts.wym === true)
{
this.$mgteditor.addClass('redactor_editor_wym');
}
if (this.opts.autoresize === false)
{
this.$mgteditor.css('height', this.height);
}
// hide textarea
this.$mgtel.hide();
// append box and frame
var html = '';
if (this.textareamode)
{
// get html
html = this.$mgtel.val();
this.$mgtbox.insertAfter(this.$mgtel).append(this.$mgteditor).append(this.$mgtel);
}
else
{
// get html
html = this.$mgteditor.html();
this.$mgtbox.insertAfter(this.$mgteditor).append(this.$mgtel).append(this.$mgteditor);
}
// conver newlines to p
html = this.paragraphy(html);
// enable
this.$mgteditor.html(html);
if (this.textareamode === false)
{
this.syncCode();
}
}
else
{
if (this.$mgtel.get(0).tagName !== 'TEXTAREA')
{
var html = this.$mgtel.val();
var textarea = $mgt('<textarea name="' + this.$mgteditor.attr('id') + '"></textarea>').css('height', this.height).val(html);
this.$mgtel.hide();
this.$mgtel.after(textarea);
}
}
},
enableAir: function()
{
if (this.opts.air === false)
{
return false;
}
this.air.hide();
this.$mgteditor.bind('textselect', $mgt.proxy(function(e)
{
this.showAir(e);
}, this));
this.$mgteditor.bind('textunselect', $mgt.proxy(function()
{
this.air.hide();
}, this));
},
showAir: function(e)
{
$mgt('.redactor_air').hide();
var width = this.air.innerWidth();
var left = e.clientX;
if ($mgt(document).width() < (left + width))
{
left = left - width;
}
this.air.css({ left: left + 'px', top: (e.clientY + $mgt(document).scrollTop() + 14) + 'px' }).show();
},
syncCode: function()
{
this.$mgtel.val(this.$mgteditor.html());
},
// API functions
setCode: function(html)
{
this.$mgteditor.html(html).focus();
this.syncCode();
},
getCode: function()
{
if (this.opts.visual)
{
return this.$mgteditor.html()
}
else
{
return this.$mgtel.val();
}
},
insertHtml: function(html)
{
this.execCommand('inserthtml', html);
this.observeImages();
},
destroy: function()
{
var html = this.getCode();
if (this.textareamode)
{
this.$mgtbox.after(this.$mgtel);
this.$mgtbox.remove();
this.$mgtel.height(this.height).val(html).show();
}
else
{
this.$mgtbox.after(this.$mgteditor);
this.$mgtbox.remove();
this.$mgteditor.removeClass('redactor_editor').removeClass('redactor_editor_wym').attr('contenteditable', false).html(html).show();
}
$mgt('.redactor_air').remove();
for (var i = 0; i < this.dropdowns.length; i++)
{
this.dropdowns[i].remove();
delete(this.dropdowns[i]);
}
},
// end API functions
// OBSERVERS
observeImages: function()
{
if (this.opts.observeImages === false)
{
return false;
}
this.$mgteditor.find('img').each($mgt.proxy(function(i,s)
{
if ($mgt.browser.msie)
{
$mgt(s).attr('unselectable', 'on');
}
this.resizeImage(s);
}, this));
},
observeTables: function()
{
this.$mgteditor.find('table').click($mgt.proxy(this.tableObserver, this));
},
observeScroll: function()
{
var scrolltop = $mgt(document).scrollTop();
var boxtop = this.$mgtbox.offset().top;
if (scrolltop > boxtop)
{
this.fixed = true;
this.$mgttoolbar.css({ position: 'fixed', width: '100%', zIndex: 100 });
}
else
{
this.fixed = false;
this.$mgttoolbar.css({ position: 'relative', width: 'auto', zIndex: 1 });
}
},
// BUFFER
setBuffer: function()
{
this.saveSelection();
this.opts.buffer = this.$mgteditor.html();
},
getBuffer: function()
{
if (this.opts.buffer === false)
{
return false;
}
this.$mgteditor.html(this.opts.buffer);
if (!$mgt.browser.msie)
{
this.restoreSelection();
}
this.opts.buffer = false;
},
// EXECCOMMAND
execCommand: function(cmd, param)
{
try
{
var parent;
if (cmd === 'inserthtml' && $mgt.browser.msie)
{
document.selection.createRange().pasteHTML(param);
}
else if (cmd === 'formatblock' && $mgt.browser.msie)
{
document.execCommand(cmd, false, '<' + param + '>');
}
else if (cmd === 'unlink')
{
parent = this.getParentNode();
if ($mgt(parent).get(0).tagName === 'A')
{
$mgt(parent).replaceWith($mgt(parent).text());
}
else
{
document.execCommand(cmd, false, param);
}
}
else if (cmd === 'formatblock' && param === 'blockquote')
{
parent = this.getParentNode();
if ($mgt(parent).get(0).tagName === 'BLOCKQUOTE')
{
document.execCommand(cmd, false, 'p');
}
else if ($mgt(parent).get(0).tagName === 'P')
{
var parent2 = $mgt(parent).parent();
if ($mgt(parent2).get(0).tagName === 'BLOCKQUOTE')
{
var node = $mgt('<p>' + $mgt(parent).html() + '</p>');
$mgt(parent2).replaceWith(node);
this.setFocusNode(node.get(0));
}
else
{
document.execCommand(cmd, false, param);
}
}
else
{
document.execCommand(cmd, false, param);
}
}
else if (cmd === 'formatblock' && param === 'pre')
{
parent = this.getParentNode();
if ($mgt(parent).get(0).tagName === 'PRE')
{
$mgt(parent).replaceWith('<p>' + $mgt(parent).text() + '</p>');
}
else
{
document.execCommand(cmd, false, param);
}
}
else
{
document.execCommand(cmd, false, param);
}
if (cmd === 'inserthorizontalrule')
{
this.$mgteditor.find('hr').removeAttr('id');
}
this.syncCode();
if (this.oldIE())
{
this.$mgteditor.focus();
}
if (typeof this.opts.execCommandCallback === 'function')
{
this.opts.execCommandCallback(this, cmd);
}
if (this.opts.air)
{
this.air.hide();
}
}
catch (e) { }
},
// FORMAT NEW LINE
formatNewLine: function(e)
{
if ($mgt.browser.webkit)
{
var parent = this.getParentNode();
if (parent.nodeName === 'DIV' && parent.className === 'redactor_editor')
{
e.preventDefault();
var element = $mgt(this.getCurrentNode());
if (element.get(0).tagName === 'DIV' && (element.html() === '' || element.html() === '<br>'))
{
var newElement = $mgt('<p>').append(element.clone().get(0).childNodes);
element.replaceWith(newElement);
newElement.html('<br />');
this.setFocusNode(newElement.get(0));
this.syncCode();
return false;
}
else
{
this.syncCode();
}
// convert links
if (this.opts.convertLinks)
{
this.$mgteditor.linkify();
}
}
else
{
this.syncCode();
return true;
}
}
else
{
this.syncCode();
return true;
}
},
// SAFARI SHIFT KEY + ENTER
safariShiftKeyEnter: function(e, key)
{
if (e.shiftKey && key === 13)
{
e.preventDefault();
var node1 = $mgt('<span><br /></span>');
this.insertNodeAtCaret(node1.get(0));
this.syncCode();
return false;
}
else
{
return true;
}
},
// FORMAT EMPTY
formatEmpty: function(e)
{
var html = $mgt.trim(this.$mgteditor.html());
if ($mgt.browser.mozilla)
{
html = html.replace(/<br>/i, '');
}
if (html === '')
{
e.preventDefault();
var nodehtml = this.opts.allEmptyHtml;
if ($mgt.browser.mozilla)
{
nodehtml = this.opts.mozillaEmptyHtml;
}
var node = $mgt(nodehtml).get(0);
this.$mgteditor.html(node);
this.setFocusNode(node);
this.syncCode();
return false;
}
else
{
this.syncCode();
}
},
// PARAGRAPHY
paragraphy: function (str)
{
str = $mgt.trim(str);
if (str === '')
{
if (!$mgt.browser.mozilla)
{
return this.opts.allEmptyHtml;
}
else
{
return this.opts.mozillaEmptyHtml;
}
}
// convert div to p
if (this.opts.convertDivs)
{
str = str.replace(/<div(.*?)>([\w\W]*?)<\/div>/gi, '<p>$2</p>');
}
// inner functions
var X = function(x, a, b) { return x.replace(new RegExp(a, 'g'), b); };
var R = function(a, b) { return X(str, a, b); };
// block elements
var blocks = '(table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|style|script|object|input|param|p|h[1-6])';
//str = '<p>' + str;
str += '\n';
R('<br />\\s*<br />', '\n\n');
R('(<' + blocks + '[^>]*>)', '\n$1');
R('(</' + blocks + '>)', '$1\n\n');
R('\r\n|\r', '\n'); // newlines
R('\n\n+', '\n\n'); // remove duplicates
R('\n?((.|\n)+?)$', '<p>$1</p>\n'); // including one at the end
R('<p>\\s*?</p>', ''); // remove empty p
R('<p>(<div[^>]*>\\s*)', '$1<p>');
R('<p>([^<]+)\\s*?(</(div|address|form)[^>]*>)', '<p>$1</p>$2');
R('<p>\\s*(</?' + blocks + '[^>]*>)\\s*</p>', '$1');
R('<p>(<li.+?)</p>', '$1');
R('<p>\\s*(</?' + blocks + '[^>]*>)', '$1');
R('(</?' + blocks + '[^>]*>)\\s*</p>', '$1');
R('(</?' + blocks + '[^>]*>)\\s*<br />', '$1');
R('<br />(\\s*</?(p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)', '$1');
// pre
if (str.indexOf('<pre') != -1)
{
R('(<pre(.|\n)*?>)((.|\n)*?)</pre>', function(m0, m1, m2, m3)
{
return X(m1, '\\\\([\'\"\\\\])', '$1') + X(X(X(m3, '<p>', '\n'), '</p>|<br />', ''), '\\\\([\'\"\\\\])', '$1') + '</pre>';
});
}
return R('\n</p>$', '</p>');
},
// REMOVE TAGS
stripTags: function(html)
{
// Redactor WYSIWYG editor refusing to accept pasted content from MS Word
// By Kingsley Raspe
// See: https://gist.github.com/kingsloi/97260c320bf22e5b96c7
var allowedObj = this.opts.allowedTags;
var allowed = '';
//convert allowedTags to string in <b><i><h1> format
for (var p in allowedObj) {
if (allowedObj.hasOwnProperty(p)) {
allowed += '<' + allowedObj[p] + '>';
}
}
//search and destroy
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return html.replace(commentsAndPhpTags, '').replace(tags, function($0, $1){
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
},
// PASTE CLEANUP
pasteCleanUp: function(html)
{
// remove comments and php tags
html = html.replace(/<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi, '');
// remove nbsp
html = html.replace(/(&nbsp;){1,}/gi, '&nbsp;');
// remove fancy quotes
html = html.replace(/[\u2018\u2019]/g, "'");
html = html.replace(/[\u201C\u201D]/g, '"');
// remove google docs marker
html = html.replace(/<b\sid="internal-source-marker(.*?)">([\w\W]*?)<\/b>/gi, "$2");
// strip tags
html = this.stripTags(html);
// prevert
html = html.replace(/<td><br><\/td>/gi, '[td]');
html = html.replace(/<a(.*?)>([\w\W]*?)<\/a>/gi, '[a$1]$2[/a]');
html = html.replace(/<iframe(.*?)>([\w\W]*?)<\/iframe>/gi, '[iframe$1]$2[/iframe]');
html = html.replace(/<video(.*?)>([\w\W]*?)<\/video>/gi, '[video$1]$2[/video]');
html = html.replace(/<audio(.*?)>([\w\W]*?)<\/audio>/gi, '[audio$1]$2[/audio]');
html = html.replace(/<object(.*?)>([\w\W]*?)<\/object>/gi, '[object$1]$2[/object]');
html = html.replace(/<img(.*?)>/gi, '[img$1]');
// remove attributes
html = html.replace(/<(\w+)([\w\W]*?)>/gi, '<$1>');
// remove empty
html = html.replace(/<[^\/>][^>]*>(\s*|\t*|\n*|&nbsp;|<br>)<\/[^>]+>/gi, '');
html = html.replace(/<[^\/>][^>]*>(\s*|\t*|\n*|&nbsp;|<br>)<\/[^>]+>/gi, '');
// revert
html = html.replace(/\[td\]/gi, '<td><br></td>');
html = html.replace(/\[a(.*?)\]([\w\W]*?)\[\/a\]/gi, '<a$1>$2</a>');
html = html.replace(/\[iframe(.*?)\]([\w\W]*?)\[\/iframe\]/gi, '<iframe$1>$2</iframe>');
html = html.replace(/\[video(.*?)\]([\w\W]*?)\[\/video\]/gi, '<video$1>$2[/video>');
html = html.replace(/\[audio(.*?)\]([\w\W]*?)\[\/audio\]/gi, '<audio$1>$2[/audio>');
html = html.replace(/\[object(.*?)\]([\w\W]*?)\[\/object\]/gi, '<object$1>$2</object>');
html = html.replace(/\[img(.*?)\]/gi, '<img$1>');
// convert div to p
if (this.opts.convertDivs)
{
html = html.replace(/<div(.*?)>([\w\W]*?)<\/div>/gi, '<p>$2</p>');
}
// remove span
html = html.replace(/<span>([\w\W]*?)<\/span>/gi, '$1');
html = html.replace(/\n{3,}/gi, '\n');
// remove dirty p
html = html.replace(/<p><p>/g, '<p>');
html = html.replace(/<\/p><\/p>/g, '</p>');
this.execCommand('inserthtml', html);
},
// TEXTAREA CODE FORMATTING
formattingRemove: function(html)
{
// save pre
var prebuffer = [];
var pre = html.match(/<pre(.*?)>([\w\W]*?)<\/pre>/gi);
if (pre !== null)
{
$.each(pre, function(i,s)
{
html = html.replace(s, 'prebuffer_' + i);
prebuffer.push(s);
});
}
html = html.replace(/\s{2,}/g, ' ');
html = html.replace(/\n/g, ' ');
html = html.replace(/[\t]*/g, '');
html = html.replace(/\n\s*\n/g, "\n");
html = html.replace(/^[\s\n]*/g, '');
html = html.replace(/[\s\n]*$/g, '');
html = html.replace(/>\s+</g, '><');
if (prebuffer)
{
$mgt.each(prebuffer, function(i,s)
{
html = html.replace('prebuffer_' + i, s);
});
prebuffer = [];
}
return html;
},
formattingIndenting: function(html)
{
html = html.replace(/<li/g, "\t<li");
html = html.replace(/<tr/g, "\t<tr");
html = html.replace(/<td/g, "\t\t<td");
html = html.replace(/<\/tr>/g, "\t</tr>");
return html;
},
formattingEmptyTags: function(html)
{
var etags = ["<pre></pre>","<blockquote>\\s*</blockquote>","<em>\\s*</em>","<ul></ul>","<ol></ol>","<li></li>","<table></table>","<tr></tr>","<span>\\s*<span>", "<span>&nbsp;<span>", "<b>\\s*</b>", "<b>&nbsp;</b>", "<p>\\s*</p>", "<p>&nbsp;</p>", "<p>\\s*<br>\\s*</p>", "<div>\\s*</div>", "<div>\\s*<br>\\s*</div>"];
for (var i = 0; i < etags.length; ++i)
{
var bbb = etags[i];
html = html.replace(new RegExp(bbb,'gi'), "");
}
return html;
},
formattingAddBefore: function(html)
{
var lb = '\r\n';
var btags = ["<p", "<form","</ul>", '</ol>', "<fieldset","<legend","<object","<embed","<select","<option","<input","<textarea","<pre","<blockquote","<ul","<ol","<li","<dl","<dt","<dd","<table", "<thead","<tbody","<caption","</caption>","<th","<tr","<td","<figure"];
for (var i = 0; i < btags.length; ++i)
{
var eee = btags[i];
html = html.replace(new RegExp(eee,'gi'),lb+eee);
}
return html;
},
formattingAddAfter: function(html)
{
var lb = '\r\n';
var atags = ['</p>', '</div>', '</h1>', '</h2>', '</h3>', '</h4>', '</h5>', '</h6>', '<br>', '<br />', '</dl>', '</dt>', '</dd>', '</form>', '</blockquote>', '</pre>', '</legend>', '</fieldset>', '</object>', '</embed>', '</textarea>', '</select>', '</option>', '</table>', '</thead>', '</tbody>', '</tr>', '</td>', '</th>', '</figure>'];
for (var i = 0; i < atags.length; ++i)
{
var aaa = atags[i];
html = html.replace(new RegExp(aaa,'gi'),aaa+lb);
}
return html;
},
formatting: function(html)
{
html = this.formattingRemove(html);
// empty tags
html = this.formattingEmptyTags(html);
// add formatting before
html = this.formattingAddBefore(html);
// add formatting after
html = this.formattingAddAfter(html);
// indenting
html = this.formattingIndenting(html);
return html;
},
toggle: function()
{
var html;
if (this.opts.visual)
{
this.$mgteditor.hide();
html = this.$mgteditor.html();
html = $mgt.trim(this.formatting(html));
this.$mgtel.height(this.$mgteditor.innerHeight()).val(html).show().focus();
this.setBtnActive('html');
this.opts.visual = false;
}
else
{
this.$mgtel.hide();
this.$mgteditor.html(this.$mgtel.val());
this.$mgteditor.show();
if (this.$mgteditor.html() === '')
{
if (!$mgt.browser.mozilla)
{
html = this.opts.allEmptyHtml;
}
else
{
html = this.opts.mozillaEmptyHtml;
}
this.setCode(html);
}
this.$mgteditor.focus();
this.setBtnInactive('html');
this.opts.visual = true;
this.observeImages();
this.observeTables();
}
},
// AUTOSAVE
autoSave: function()
{
setInterval($mgt.proxy(function()
{
$mgt.post(this.opts.autosave, { data: this.getCode() });
}, this), this.opts.interval*1000);
},
// TOOLBAR
buildToolbar: function()
{
if (this.opts.toolbar === false)
{
return false;
}
this.$mgttoolbar = $mgt('<ul>').addClass('redactor_toolbar');
if (this.opts.air)
{
$mgt(this.air).append(this.$mgttoolbar);
$mgt('body').prepend(this.air);
}
else
{
this.$mgtbox.prepend(this.$mgttoolbar);
}
$mgt.each(this.opts.buttons, $mgt.proxy(function(i,key)
{
if (key !== '|' && typeof this.opts.toolbar[key] !== 'undefined')
{
var s = this.opts.toolbar[key];
if (this.opts.fileUpload === false && key === 'file')
{
return true;
}
var li = $mgt('<li>');
var a = this.buildButton(key, s);
// dropdown
if (key === 'backcolor' || key === 'fontcolor' || typeof(s.dropdown) !== 'undefined')
{
var dropdown = $mgt('<div class="redactor_dropdown" style="display: none;">');
if (key === 'backcolor' || key === 'fontcolor')
{
dropdown = this.buildColorPicker(dropdown, key);
}
else
{
dropdown = this.buildDropdown(dropdown, s.dropdown);
}
this.dropdowns.push(dropdown.appendTo($mgt(document.body)));
// observing dropdown
this.hdlHideDropDown = $mgt.proxy(function(e) { this.hideDropDown(e, dropdown, key); }, this);
this.hdlShowDropDown = $mgt.proxy(function(e) { this.showDropDown(e, dropdown, key); }, this);
a.click(this.hdlShowDropDown);
}
this.$mgttoolbar.append($mgt(li).append(a));
}
if (key === '|')
{
this.$mgttoolbar.append($mgt('<li class="redactor_separator"></li>'));
}
}, this));
$mgt(document).click(this.hdlHideDropDown);
this.$mgteditor.click(this.hdlHideDropDown);
},
buildButton: function(key, s)
{
var button = $mgt('<a href="javascript:void(null);" title="' + s.title + '" class="redactor_btn_' + key + '"></a>');
if (typeof s.func === 'undefined')
{
button.click($mgt.proxy(function() { this.execCommand(s.exec, key); }, this));
}
else if (s.func !== 'show')
{
button.click($mgt.proxy(function(e) {
this[s.func](e);
}, this));
}
if (typeof s.callback !== 'undefined')
{
button.click($mgt.proxy(function(e) { s.callback(this, e, key); }, this));
}
return button;
},
buildDropdown: function(dropdown, obj)
{
$mgt.each(obj, $mgt.proxy(
function (x, d)
{
if (typeof(d.className) === 'undefined')
{
d.className = '';
}
var drop_a;
if (typeof d.name !== 'undefined' && d.name === 'separator')
{
drop_a = $mgt('<a class="redactor_separator_drop">');
}
else
{
drop_a = $mgt('<a href="javascript:void(null);" class="' + d.className + '">' + d.title + '</a>');
if (typeof(d.func) === 'undefined')
{
$mgt(drop_a).click($mgt.proxy(function() { this.execCommand(d.exec, x); }, this));
}
else
{
$mgt(drop_a).click($mgt.proxy(function(e) { this[d.func](e); }, this));
}
}
$mgt(dropdown).append(drop_a);
}, this)
);
return dropdown;
},
buildColorPicker: function(dropdown, key)
{
var mode;
if (key === 'backcolor')
{
if ($mgt.browser.msie)
{
mode = 'BackColor';
}
else
{
mode = 'hilitecolor';
}
}
else
{
mode = 'forecolor';
}
$mgt(dropdown).width(210);
var len = this.opts.colors.length;
for (var i = 0; i < len; ++i)
{
var color = this.opts.colors[i];
var swatch = $mgt('<a rel="' + color + '" href="javascript:void(null);" class="redactor_color_link"></a>').css({ 'backgroundColor': color });
$mgt(dropdown).append(swatch);
var _self = this;
$mgt(swatch).click(function()
{
_self.execCommand(mode, $mgt(this).attr('rel'));
if (mode === 'forecolor')
{
_self.$mgteditor.find('font').replaceWith(function() {
return $mgt('<span style="color: ' + $mgt(this).attr('color') + ';">' + $mgt(this).html() + '</span>');
});
}
if ($mgt.browser.msie && mode === 'BackColor')
{
_self.$mgteditor.find('font').replaceWith(function() {
return $mgt('<span style="' + $mgt(this).attr('style') + '">' + $mgt(this).html() + '</span>');
});
}
});
}
var elnone = $mgt('<a href="javascript:void(null);" class="redactor_color_none"></a>').html(RLANG.none);
if (key === 'backcolor')
{
elnone.click($mgt.proxy(this.setBackgroundNone, this));
}
else
{
elnone.click($mgt.proxy(this.setColorNone, this));
}
$mgt(dropdown).append(elnone);
return dropdown;
},
setBackgroundNone: function()
{
$mgt(this.getParentNode()).css('background-color', 'transparent');
this.syncCode();
},
setColorNone: function()
{
$mgt(this.getParentNode()).attr('color', '').css('color', '');
this.syncCode();
},
// DROPDOWNS
showDropDown: function(e, dropdown, key)
{
if (this.getBtn(key).hasClass('dropact'))
{
this.hideAllDropDown();
}
else
{
this.hideAllDropDown();
this.setBtnActive(key);
this.getBtn(key).addClass('dropact');
var left = this.getBtn(key).offset().left;
if (this.opts.air)
{
var air_top = this.air.offset().top;
$mgt(dropdown).css({ position: 'absolute', left: left + 'px', top: air_top+30 + 'px' }).show();
}
else if (this.opts.fixed && this.fixed)
{
$mgt(dropdown).css({ position: 'fixed', left: left + 'px', top: '30px' }).show();
}
else
{
var top = this.$mgttoolbar.offset().top + 30;
$mgt(dropdown).css({ position: 'absolute', left: left + 'px', top: top + 'px' }).show();
}
}
},
hideAllDropDown: function()
{
this.$mgttoolbar.find('a.dropact').removeClass('act').removeClass('dropact');
$mgt('.redactor_dropdown').hide();
},
hideDropDown: function(e, dropdown, key)
{
if (!$mgt(e.target).hasClass('dropact'))
{
$mgt(dropdown).removeClass('act');
this.showedDropDown = false;
this.hideAllDropDown();
}
},
// SELECTION AND NODE MANIPULATION
getSelection: function ()
{
if (typeof window.getSelection !== 'undefined')
{
return document.getSelection();
}
else if (typeof document.selection !== 'undefined')
{
return document.selection.createRange();
}
},
getFragmentHtml: function (fragment)
{
var cloned = fragment.cloneNode(true);
var div = document.createElement('div');
div.appendChild(cloned);
return div.innerHTML;
},
extractContent: function()
{
var node = this.$mgteditor.get(0);
var frag = document.createDocumentFragment(), child;
while ((child = node.firstChild))
{
frag.appendChild(child);
}
return frag;
},
saveSelection: function()
{
this.savedSel = null;
this.savedSelObj = null;
if ($mgt.browser.msie && parseInt($mgt.browser.version, 10) < 9)
{
var node = this.$mgteditor.get(0);
this.savedSel = window.Selection.getOrigin(node);
this.savedSelObj = window.Selection.getFocus(node);
}
else
{
this.savedSel = window.Selection.getOrigin(window);
this.savedSelObj = window.Selection.getFocus(window);
}
},
restoreSelection: function()
{
if (this.savedSel !== null && this.savedSelObj !== null && this.savedSel[0].tagName !== 'BODY')
{
window.Selection.setSelection(window, this.savedSel[0], this.savedSel[1], this.savedSelObj[0], this.savedSelObj[1]);
}
else
{
this.$mgteditor.focus();
}
},
getParentNode: function()
{
if (typeof window.getSelection !== 'undefined')
{
var s = window.getSelection();
if (s.rangeCount > 0)
{
return this.getSelection().getRangeAt(0).startContainer.parentNode;
}
else return false;
}
else if (typeof document.selection !== 'undefined')
{
return this.getSelection().parentElement();
}
},
getCurrentNode: function()
{
if (typeof window.getSelection !== 'undefined')
{
return this.getSelection().getRangeAt(0).startContainer;
}
else if (typeof document.selection !== 'undefined')
{
return this.getSelection();
}
},
setFocusNode: function(node)
{
if (typeof node === 'undefined')
{
return false;
}
try {
var range = document.createRange();
var selection = this.getSelection();
if (selection !== null)
{
range.selectNodeContents(node);
selection.addRange(range);
selection.collapse(node, 0);
}
this.$mgteditor.focus();
} catch (e) { }
},
insertNodeAtCaret: function (node)
{
if (window.getSelection)
{
var sel = this.getSelection();
if (sel.rangeCount)
{
var range = sel.getRangeAt(0);
range.collapse(false);
range.insertNode(node);
range = range.cloneRange();
range.selectNodeContents(node);
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
}
}
else if (document.selection)
{
var html = (node.nodeType === 1) ? node.outerHTML : node.data;
var id = "marker_" + ("" + Math.random()).slice(2);
html += '<span id="' + id + '"></span>';
var textRange = this.getSelection();
textRange.collapse(false);
textRange.pasteHTML(html);
var markerSpan = document.getElementById(id);
textRange.moveToElementText(markerSpan);
textRange.select();
markerSpan.parentNode.removeChild(markerSpan);
}
},
getSelectedHtml: function()
{
var html = '';
if (window.getSelection)
{
var sel = window.getSelection();
if (sel.rangeCount)
{
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i)
{
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
}
else if (document.selection)
{
if (document.selection.type === "Text")
{
html = document.selection.createRange().htmlText;
}
}
return html;
},
// BUTTONS MANIPULATIONS
getBtn: function(key)
{
return $mgt(this.$mgttoolbar.find('a.redactor_btn_' + key));
},
setBtnActive: function(key)
{
this.getBtn(key).addClass('act');
},
setBtnInactive: function(key)
{
this.getBtn(key).removeClass('act');
},
// RESIZE IMAGES
resizeImage: function(resize)
{
var clicked = false;
var clicker = false;
var start_x;
var start_y;
var ratio = $mgt(resize).width()/$mgt(resize).height();
var min_w = 10;
var min_h = 10;
$mgt(resize).hover(function() { $mgt(resize).css('cursor', 'nw-resize'); }, function() { $mgt(resize).css('cursor','default'); clicked = false; });
$mgt(resize).mousedown(function(e)
{
e.preventDefault();
clicked = true;
clicker = true;
start_x = Math.round(e.pageX - $mgt(resize).eq(0).offset().left);
start_y = Math.round(e.pageY - $mgt(resize).eq(0).offset().top);
});
$mgt(resize).mouseup($mgt.proxy(function(e)
{
clicked = false;
this.syncCode();
}, this));
$mgt(resize).click($mgt.proxy(function(e)
{
if (clicker)
{
this.imageEdit(e);
}
}, this));
$mgt(resize).mousemove(function(e)
{
if (clicked)
{
clicker = false;
var mouse_x = Math.round(e.pageX - $mgt(this).eq(0).offset().left) - start_x;
var mouse_y = Math.round(e.pageY - $mgt(this).eq(0).offset().top) - start_y;
var div_h = $mgt(resize).height();
var new_h = parseInt(div_h, 10) + mouse_y;
var new_w = new_h*ratio;
if (new_w > min_w)
{
$mgt(resize).width(new_w);
}
if (new_h > min_h)
{
$mgt(resize).height(new_h);
}
start_x = Math.round(e.pageX - $mgt(this).eq(0).offset().left);
start_y = Math.round(e.pageY - $mgt(this).eq(0).offset().top);
}
});
},
// TABLE
showTable: function()
{
this.saveSelection();
this.modalInit(RLANG.table, 'table', 300, $mgt.proxy(function()
{
$mgt('#redactor_insert_table_btn').click($mgt.proxy(this.insertTable, this));
}, this),
function()
{
$mgt('#redactor_table_rows').focus();
}
);
},
insertTable: function()
{
var rows = $mgt('#redactor_table_rows').val();
var columns = $mgt('#redactor_table_columns').val();
var table_box = $mgt('<div></div>');
var tableid = Math.floor(Math.random() * 99999);
var table = $mgt('<table id="table' + tableid + '"><tbody></tbody></table>');
for (var i = 0; i < rows; i++)
{
var row = $mgt('<tr></tr>');
for (var z = 0; z < columns; z++)
{
var column = $mgt('<td><br></td>');
$mgt(row).append(column);
}
$mgt(table).append(row);
}
$mgt(table_box).append(table);
var html = $mgt(table_box).html() + '<p></p>';
this.restoreSelection();
this.execCommand('inserthtml', html);
this.modalClose();
this.observeTables();
},
tableObserver: function(e)
{
this.$mgttable = $mgt(e.target).parents('table');
this.$mgttable_tr = this.$mgttable.find('tr');
this.$mgttable_td = this.$mgttable.find('td');
this.$mgttable_td.removeClass('current');
this.$mgttbody = $mgt(e.target).parents('tbody');
this.$mgtthead = $mgt(this.$mgttable).find('thead');
this.$mgtcurrent_td = $mgt(e.target);
this.$mgtcurrent_td.addClass('current');
this.$mgtcurrent_tr = $mgt(e.target).parents('tr');
},
deleteTable: function()
{
$mgt(this.$mgttable).remove();
this.$mgttable = false;
this.syncCode();
},
deleteRow: function()
{
$mgt(this.$mgtcurrent_tr).remove();
this.syncCode();
},
deleteColumn: function()
{
var index = $mgt(this.$mgtcurrent_td).get(0).cellIndex;
$mgt(this.$mgttable).find('tr').each(function()
{
$mgt(this).find('td').eq(index).remove();
});
this.syncCode();
},
addHead: function()
{
if ($mgt(this.$mgttable).find('thead').size() !== 0)
{
this.deleteHead();
}
else
{
var tr = $mgt(this.$mgttable).find('tr').first().clone();
tr.find('td').html('&nbsp;');
this.$mgtthead = $mgt('<thead></thead>');
this.$mgtthead.append(tr);
$mgt(this.$mgttable).prepend(this.$mgtthead);
this.syncCode();
}
},
deleteHead: function()
{
$mgt(this.$mgtthead).remove();
this.$mgtthead = false;
this.syncCode();
},
insertRowAbove: function()
{
this.insertRow('before');
},
insertRowBelow: function()
{
this.insertRow('after');
},
insertColumnLeft: function()
{
this.insertColumn('before');
},
insertColumnRight: function()
{
this.insertColumn('after');
},
insertRow: function(type)
{
var new_tr = $mgt(this.$mgtcurrent_tr).clone();
new_tr.find('td').html('&nbsp;');
if (type === 'after')
{
$mgt(this.$mgtcurrent_tr).after(new_tr);
}
else
{
$mgt(this.$mgtcurrent_tr).before(new_tr);
}
this.syncCode();
},
insertColumn: function(type)
{
var index = 0;
this.$mgtcurrent_td.addClass('current');
this.$mgtcurrent_tr.find('td').each(function(i,s)
{
if ($mgt(s).hasClass('current'))
{
index = i;
}
});
this.$mgttable_tr.each(function(i,s)
{
var current = $mgt(s).find('td').eq(index);
var td = current.clone();
td.html('&nbsp;');
if (type === 'after')
{
$mgt(current).after(td);
}
else
{
$mgt(current).before(td);
}
});
this.syncCode();
},
// INSERT VIDEO
showVideo: function()
{
this.saveSelection();
this.modalInit(RLANG.video, 'video', 600, $mgt.proxy(function()
{
$mgt('#redactor_insert_video_btn').click($mgt.proxy(this.insertVideo, this));
}, this),
function()
{
$mgt('#redactor_insert_video_area').focus();
}
);
},
insertVideo: function()
{
var data = $mgt('#redactor_insert_video_area').val();
data = this.stripTags(data);
this.restoreSelection();
this.execCommand('inserthtml', data);
this.modalClose();
},
// INSERT IMAGE
imageEdit: function(e)
{
var $mgtel = $mgt(e.target);
var parent = $mgtel.parent();
var handler = $mgt.proxy(function()
{
$mgt('#redactor_file_alt').val($mgtel.attr('alt'));
$mgt('#redactor_image_edit_src').attr('href', $mgtel.attr('src'));
$mgt('#redactor_form_image_align').val($mgtel.css('float'));
if ($mgt(parent).get(0).tagName === 'A')
{
$mgt('#redactor_file_link').val($mgt(parent).attr('href'));
}
$mgt('#redactor_image_delete_btn').click($mgt.proxy(function() { this.imageDelete($mgtel); }, this));
$mgt('#redactorSaveBtn').click($mgt.proxy(function() { this.imageSave($mgtel); }, this));
}, this);
this.modalInit(RLANG.image, 'image_edit', 380, handler);
},
imageDelete: function(el)
{
$mgt(el).remove();
this.modalClose();
this.syncCode();
},
imageSave: function(el)
{
var parent = $mgt(el).parent();
$mgt(el).attr('alt', $mgt('#redactor_file_alt').val());
var floating = $mgt('#redactor_form_image_align').val();
if (floating === 'left')
{
$mgt(el).css({ 'float': 'left', margin: '0 10px 10px 0' });
}
else if (floating === 'right')
{
$mgt(el).css({ 'float': 'right', margin: '0 0 10px 10px' });
}
else
{
$mgt(el).css({ 'float': 'none', margin: '0' });
}
// as link
var link = $mgt.trim($mgt('#redactor_file_link').val());
if (link !== '')
{
if ($mgt(parent).get(0).tagName !== 'A')
{
$mgt(el).replaceWith('<a href="' + link + '">' + this.outerHTML(el) + '</a>');
}
else
{
$mgt(parent).attr('href', link);
}
}
else
{
if ($mgt(parent).get(0).tagName === 'A')
{
$mgt(parent).replaceWith(this.outerHTML(el));
}
}
this.modalClose();
this.observeImages();
this.syncCode();
},
showImage: function()
{
this.saveSelection();
var handler = $mgt.proxy(function()
{
// json
if (this.opts.imageGetJson !== false)
{
$mgt.getJSON(this.opts.imageGetJson, $mgt.proxy(function(data) {
var folders = {};
var z = 0;
// folders
$mgt.each(data, $mgt.proxy(function(key, val)
{
if (typeof val.folder !== 'undefined')
{
z++;
folders[val.folder] = z;
}
}, this));
var folderclass = false;
$mgt.each(data, $mgt.proxy(function(key, val)
{
// title
var thumbtitle = '';
if (typeof val.title !== 'undefined')
{
thumbtitle = val.title;
}
var folderkey = 0;
if (!$mgt.isEmptyObject(folders) && typeof val.folder !== 'undefined')
{
folderkey = folders[val.folder];
if (folderclass === false)
{
folderclass = '.redactorfolder' + folderkey;
}
}
var img = $mgt('<img src="' + val.thumb + '" class="redactorfolder redactorfolder' + folderkey + '" rel="' + val.image + '" title="' + thumbtitle + '" />');
$mgt('#redactor_image_box').append(img);
$mgt(img).click($mgt.proxy(this.imageSetThumb, this));
}, this));
// folders
if (!$mgt.isEmptyObject(folders))
{
$mgt('.redactorfolder').hide();
$mgt(folderclass).show();
var onchangeFunc = function(e)
{
$mgt('.redactorfolder').hide();
$mgt('.redactorfolder' + $mgt(e.target).val()).show();
}
var select = $mgt('<select id="redactor_image_box_select">');
$mgt.each(folders, function(k,v)
{
select.append($mgt('<option value="' + v + '">' + k + '</option>'));
});
$mgt('#redactor_image_box').before(select);
select.change(onchangeFunc);
}
}, this));
}
else
{
$mgt('#redactor_tabs a').eq(1).remove();
}
if (this.opts.imageUpload !== false)
{
// dragupload
if (this.opts.uploadCrossDomain === false && this.isMobile() === false)
{
if ($mgt('#redactor_file').size() !== 0)
{
$mgt('#redactor_file').dragupload(
{
url: this.opts.imageUpload,
uploadFields: this.opts.uploadFields,
success: $mgt.proxy(this.imageUploadCallback, this)
});
}
}
// ajax upload
this.uploadInit('redactor_file', { auto: true, url: this.opts.imageUpload, success: $mgt.proxy(this.imageUploadCallback, this) });
}
else
{
$mgt('.redactor_tab').hide();
if (this.opts.imageGetJson === false)
{
$mgt('#redactor_tabs').remove();
$mgt('#redactor_tab3').show();
}
else
{
var tabs = $mgt('#redactor_tabs a');
tabs.eq(0).remove();
tabs.eq(1).addClass('redactor_tabs_act');
$mgt('#redactor_tab2').show();
}
}
$mgt('#redactor_upload_btn').click($mgt.proxy(this.imageUploadCallbackLink, this));
}, this);
var endCallback = $mgt.proxy(function()
{
if (this.opts.imageUpload === false && this.opts.imageGetJson === false)
{
$mgt('#redactor_file_link').focus();
}
}, this);
this.modalInit(RLANG.image, 'image', 570, handler, endCallback, true);
},
imageSetThumb: function(e)
{
this._imageSet('<img src="' + $mgt(e.target).attr('rel') + '" alt="' + $mgt(e.target).attr('title') + '" />', true);
},
imageUploadCallbackLink: function()
{
if ($mgt('#redactor_file_link').val() !== '')
{
var data = '<img src="' + $mgt('#redactor_file_link').val() + '" />';
this._imageSet(data, true);
}
else
{
this.modalClose();
}
},
imageUploadCallback: function(data)
{
this._imageSet(data);
},
_imageSet: function(json, link)
{
this.restoreSelection();
if (json !== false)
{
var html = '', data = '';
if (link !== true)
{
data = $mgt.parseJSON(json);
html = '<p><img src="' + data.filelink + '" /></p>';
}
else
{
html = json;
}
this.execCommand('inserthtml', html);
// upload image callback
if (link !== true && typeof this.opts.imageUploadCallback === 'function')
{
this.opts.imageUploadCallback(this, data);
}
}
this.modalClose();
this.observeImages();
},
// INSERT LINK
showLink: function()
{
this.saveSelection();
var handler = $mgt.proxy(function()
{
this.insert_link_node = false;
var sel = this.getSelection();
var url = '', text = '', target = '';
if ($mgt.browser.msie)
{
var parent = this.getParentNode();
if (parent.nodeName === 'A')
{
this.insert_link_node = $mgt(parent);
text = this.insert_link_node.text();
url = this.insert_link_node.attr('href');
target = this.insert_link_node.attr('target');
}
else
{
if (this.oldIE())
{
text = sel.text;
}
else
{
text = sel.toString();
}
}
}
else
{
if (sel && sel.anchorNode && sel.anchorNode.parentNode.tagName === 'A')
{
url = sel.anchorNode.parentNode.href;
text = sel.anchorNode.parentNode.text;
target = sel.anchorNode.parentNode.target;
if (sel.toString() === '')
{
this.insert_link_node = sel.anchorNode.parentNode;
}
}
else
{
text = sel.toString();
}
}
$mgt('.redactor_link_text').val(text);
var turl = url.replace(self.location.href, '');
if (url.search('mailto:') === 0)
{
this.setModalTab(2);
$mgt('#redactor_tab_selected').val(2);
$mgt('#redactor_link_mailto').val(url.replace('mailto:', ''));
}
else if (turl.search(/^#/gi) === 0)
{
this.setModalTab(3);
$mgt('#redactor_tab_selected').val(3);
$mgt('#redactor_link_anchor').val(turl.replace(/^#/gi, ''));
}
else
{
$mgt('#redactor_link_url').val(url);
}
if (target === '_blank')
{
$mgt('#redactor_link_blank').attr('checked', true);
}
$mgt('#redactor_insert_link_btn').click($mgt.proxy(this.insertLink, this));
}, this);
var endCallback = function(url)
{
$mgt('#redactor_link_url').focus();
};
this.modalInit(RLANG.link, 'link', 460, handler, endCallback);
},
insertLink: function()
{
var tab_selected = $mgt('#redactor_tab_selected').val();
var link = '', text = '', target = '';
if (tab_selected === '1') // url
{
link = $mgt('#redactor_link_url').val();
text = $mgt('#redactor_link_url_text').val();
if ($mgt('#redactor_link_blank').attr('checked'))
{
target = '_blank';
}
}
else if (tab_selected === '2') // mailto
{
link = 'mailto:' + $mgt('#redactor_link_mailto').val();
text = $mgt('#redactor_link_mailto_text').val();
}
else if (tab_selected === '3') // anchor
{
link = '#' + $mgt('#redactor_link_anchor').val();
text = $mgt('#redactor_link_anchor_text').val();
}
this._insertLink('<a href="' + link + '" target="' + target + '">' + text + '</a>&nbsp;', $mgt.trim(text), link, target);
},
_insertLink: function(a, text, link, target)
{
this.$mgteditor.focus();
this.restoreSelection();
if (text !== '')
{
if (this.insert_link_node)
{
$mgt(this.insert_link_node).text(text);
$mgt(this.insert_link_node).attr('href', link);
if (target !== '')
{
$mgt(this.insert_link_node).attr('target', target);
}
this.syncCode();
}
else
{
this.execCommand('inserthtml', a);
}
}
this.modalClose();
},
// INSERT FILE
showFile: function()
{
this.saveSelection();
var handler = $mgt.proxy(function()
{
var sel = this.getSelection();
var text = '';
if (this.oldIE())
{
text = sel.text;
}
else
{
text = sel.toString();
}
$mgt('#redactor_filename').val(text);
// dragupload
if (this.opts.uploadCrossDomain === false && this.isMobile() === false)
{
$mgt('#redactor_file').dragupload(
{
url: this.opts.fileUpload,
uploadFields: this.opts.uploadFields,
success: $mgt.proxy(function(data)
{
this.fileUploadCallback(data);
}, this)
});
}
this.uploadInit('redactor_file', { auto: true, url: this.opts.fileUpload, success: $mgt.proxy(function(data) {
this.fileUploadCallback(data);
}, this)});
}, this);
this.modalInit(RLANG.file, 'file', 500, handler);
},
fileUploadCallback: function(json)
{
this.restoreSelection();
if (json !== false)
{
var data = $mgt.parseJSON(json);
var text = $mgt('#redactor_filename').val();
if (text === '')
{
text = data.filename;
}
var link = '<a href="' + data.filelink + '">' + text + '</a>';
// chrome fix
if ($mgt.browser.webkit && !!window.chrome)
{
link = link + '&nbsp;';
}
this.execCommand('inserthtml', link);
// file upload callback
if (typeof this.opts.fileUploadCallback === 'function')
{
this.opts.fileUploadCallback(this, data);
}
}
this.modalClose();
},
// MODAL
modalInit: function(title, url, width, handler, endCallback)
{
// modal overlay
if ($mgt('#redactor_modal_overlay').size() === 0)
{
this.overlay = $mgt('<div id="redactor_modal_overlay" style="display: none;"></div>');
$mgt('body').prepend(this.overlay);
}
if (this.opts.overlay)
{
$mgt('#redactor_modal_overlay').show();
$mgt('#redactor_modal_overlay').click($mgt.proxy(this.modalClose, this));
}
if ($mgt('#redactor_modal').size() === 0)
{
this.modal = $mgt('<div id="redactor_modal" style="display: none;"><div id="redactor_modal_close">&times;</div><div id="redactor_modal_header"></div><div id="redactor_modal_inner"></div></div>');
$mgt('body').append(this.modal);
}
$mgt('#redactor_modal_close').click($mgt.proxy(this.modalClose, this));
this.hdlModalClose = $mgt.proxy(function(e) { if ( e.keyCode === 27) { this.modalClose(); } }, this);
$mgt(document).keyup(this.hdlModalClose);
this.$mgteditor.keyup(this.hdlModalClose);
$mgt('#redactor_modal_inner').html(this.opts['modal_' + url]);
$mgt('#redactor_modal_header').html(title);
// tabs
if ($mgt('#redactor_tabs').size() !== 0)
{
var that = this;
$mgt('#redactor_tabs a').each(function(i,s)
{
i++;
$mgt(s).click(function()
{
$mgt('#redactor_tabs a').removeClass('redactor_tabs_act');
$mgt(this).addClass('redactor_tabs_act');
$mgt('.redactor_tab').hide();
$mgt('#redactor_tab' + i).show();
$mgt('#redactor_tab_selected').val(i);
if (that.isMobile() === false)
{
var height = $mgt('#redactor_modal').outerHeight();
$mgt('#redactor_modal').css('margin-top', '-' + (height+10)/2 + 'px');
}
});
});
}
$mgt('#redactor_btn_modal_close').click($mgt.proxy(this.modalClose, this));
// callback
if (typeof(handler) === 'function')
{
handler();
}
// setup
var height = $mgt('#redactor_modal').outerHeight();
if (this.isMobile() === false)
{
$mgt('#redactor_modal').css({ position: 'fixed', top: '50%', left: '50%', width: width + 'px', height: 'auto', minHeight: 'auto', marginTop: '-' + (height+10)/2 + 'px', marginLeft: '-' + (width+60)/2 + 'px' }).fadeIn('fast');
this.modalSaveBodyOveflow = $mgt(document.body).css('overflow');
$mgt(document.body).css('overflow', 'hidden');
}
else
{
$mgt('#redactor_modal').css({ position: 'fixed', width: '100%', height: '100%', top: '0', left: '0', margin: '0', minHeight: '300px' }).show();
}
// end callback
if (typeof(endCallback) === 'function')
{
endCallback();
}
},
modalClose: function()
{
$mgt('#redactor_modal_close').unbind('click', this.modalClose);
$mgt('#redactor_modal').fadeOut('fast', $mgt.proxy(function()
{
$mgt('#redactor_modal_inner').html('');
if (this.opts.overlay)
{
$mgt('#redactor_modal_overlay').hide();
$mgt('#redactor_modal_overlay').unbind('click', this.modalClose);
}
$mgt(document).unbind('keyup', this.hdlModalClose);
this.$mgteditor.unbind('keyup', this.hdlModalClose);
}, this));
if (this.isMobile() === false)
{
$mgt(document.body).css('overflow', this.modalSaveBodyOveflow);
}
},
setModalTab: function(num)
{
$mgt('.redactor_tab').hide();
var tabs = $mgt('#redactor_tabs a');
tabs.removeClass('redactor_tabs_act');
tabs.eq(num-1).addClass('redactor_tabs_act');
$mgt('#redactor_tab' + num).show();
},
// UPLOAD
uploadInit: function(element, options)
{
// Upload Options
this.uploadOptions = {
url: false,
success: false,
start: false,
trigger: false,
auto: false,
input: false
};
$mgt.extend(this.uploadOptions, options);
// Test input or form
if ($mgt('#' + element).size() !== 0 && $mgt('#' + element).get(0).tagName === 'INPUT')
{
this.uploadOptions.input = $mgt('#' + element);
this.element = $mgt($mgt('#' + element).get(0).form);
}
else
{
this.element = $mgt('#' + element);
}
this.element_action = this.element.attr('action');
// Auto or trigger
if (this.uploadOptions.auto)
{
$mgt(this.uploadOptions.input).change($mgt.proxy(function()
{
this.element.submit(function(e) { return false; });
this.uploadSubmit();
}, this));
}
else if (this.uploadOptions.trigger)
{
$mgt('#' + this.uploadOptions.trigger).click($mgt.proxy(this.uploadSubmit, this));
}
},
uploadSubmit : function()
{
this.uploadForm(this.element, this.uploadFrame());
},
uploadFrame : function()
{
this.id = 'f' + Math.floor(Math.random() * 99999);
var d = document.createElement('div');
var iframe = '<iframe style="display:none" id="'+this.id+'" name="'+this.id+'"></iframe>';
d.innerHTML = iframe;
document.body.appendChild(d);
// Start
if (this.uploadOptions.start)
{
this.uploadOptions.start();
}
$mgt('#' + this.id).load($mgt.proxy(this.uploadLoaded, this));
return this.id;
},
uploadForm : function(f, name)
{
if (this.uploadOptions.input)
{
var formId = 'redactorUploadForm' + this.id;
var fileId = 'redactorUploadFile' + this.id;
this.form = $mgt('<form action="' + this.uploadOptions.url + '" method="POST" target="' + name + '" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
// append hidden fields
if (this.opts.uploadFields !== false && typeof this.opts.uploadFields === 'object')
{
$mgt.each(this.opts.uploadFields, $mgt.proxy(function(k,v)
{
if (v.indexOf('#') === 0)
{
v = $mgt(v).val();
}
var hidden = $mgt('<input type="hidden" name="' + k + '" value="' + v + '">');
$mgt(this.form).append(hidden);
}, this));
}
var oldElement = this.uploadOptions.input;
var newElement = $mgt(oldElement).clone();
$mgt(oldElement).attr('id', fileId);
$mgt(oldElement).before(newElement);
$mgt(oldElement).appendTo(this.form);
$mgt(this.form).css('position', 'absolute');
$mgt(this.form).css('top', '-2000px');
$mgt(this.form).css('left', '-2000px');
$mgt(this.form).appendTo('body');
this.form.submit();
}
else
{
f.attr('target', name);
f.attr('method', 'POST');
f.attr('enctype', 'multipart/form-data');
f.attr('action', this.uploadOptions.url);
this.element.submit();
}
},
uploadLoaded : function()
{
var i = $mgt('#' + this.id);
var d;
if (i.contentDocument)
{
d = i.contentDocument;
}
else if (i.contentWindow)
{
d = i.contentWindow.document;
}
else
{
d = window.frames[this.id].document;
}
// Success
if (this.uploadOptions.success)
{
if (typeof d !== 'undefined')
{
// Remove bizarre <pre> tag wrappers around our json data:
var rawString = d.body.innerHTML;
var jsonString = rawString.match(/\{.*\}/)[0];
this.uploadOptions.success(jsonString);
}
else
{
alert('Upload failed!');
this.uploadOptions.success(false);
}
}
this.element.attr('action', this.element_action);
this.element.attr('target', '');
},
// UTILITY
oldIE: function()
{
if ($mgt.browser.msie && parseInt($mgt.browser.version, 10) < 9)
{
return true;
}
return false;
},
outerHTML: function(s)
{
return $mgt("<p>").append($mgt(s).eq(0).clone()).html();
},
normalize: function(str)
{
return parseInt(str.replace('px',''), 10);
},
isMobile: function(ipad)
{
if (ipad === true && /(iPhone|iPod|iPad|BlackBerry|Android)/.test(navigator.userAgent))
{
return true;
}
else if (/(iPhone|iPod|BlackBerry|Android)/.test(navigator.userAgent))
{
return true;
}
else
{
return false;
}
}
};
if(typeof $mgt != 'undefined') {
// API
$mgt.fn.getEditor = function()
{
return this.data('redactor').$mgteditor;
};
$mgt.fn.getCode = function()
{
return this.data('redactor').getCode();
};
$mgt.fn.getText = function()
{
return this.data('redactor').$mgteditor.text();
};
$mgt.fn.getSelected = function()
{
return this.data('redactor').getSelectedHtml();
};
$mgt.fn.setCode = function(html)
{
this.data('redactor').setCode(html);
};
$mgt.fn.insertHtml = function(html)
{
this.data('redactor').insertHtml(html);
};
$mgt.fn.destroyEditor = function()
{
this.data('redactor').destroy();
this.removeData('redactor');
};
$mgt.fn.setFocus = function()
{
this.data('redactor').focus();
};
$mgt.fn.execCommand = function(cmd, param)
{
this.data('redactor').execCommand(cmd, param);
};
}
/*
Plugin Drag and drop Upload v1.0.2
http://imperavi.com/
Copyright 2012, Imperavi Inc.
*/
(function($mgt){
"use strict";
// Initialization
$mgt.fn.dragupload = function(options)
{
return this.each(function() {
var obj = new Construct(this, options);
obj.init();
});
};
// Options and variables
function Construct(el, options) {
this.opts = $mgt.extend({
url: false,
success: false,
preview: false,
uploadFields: false,
text: RLANG.drop_file_here,
atext: RLANG.or_choose
}, options);
this.$mgtel = $mgt(el);
}
// Functionality
Construct.prototype = {
init: function()
{
if (!$mgt.browser.msie)
{
this.droparea = $mgt('<div class="redactor_droparea"></div>');
this.dropareabox = $mgt('<div class="redactor_dropareabox">' + this.opts.text + '</div>');
this.dropalternative = $mgt('<div class="redactor_dropalternative">' + this.opts.atext + '</div>');
this.droparea.append(this.dropareabox);
this.$mgtel.before(this.droparea);
this.$mgtel.before(this.dropalternative);
// drag over
this.dropareabox.bind('dragover', $mgt.proxy(function() { return this.ondrag(); }, this));
// drag leave
this.dropareabox.bind('dragleave', $mgt.proxy(function() { return this.ondragleave(); }, this));
var uploadProgress = $mgt.proxy(function(e)
{
var percent = parseInt(e.loaded / e.total * 100, 10);
this.dropareabox.text('Loading ' + percent + '%');
}, this);
var xhr = jQuery.ajaxSettings.xhr();
if (xhr.upload)
{
xhr.upload.addEventListener('progress', uploadProgress, false);
}
var provider = function () { return xhr; };
// drop
this.dropareabox.get(0).ondrop = $mgt.proxy(function(event)
{
event.preventDefault();
this.dropareabox.removeClass('hover').addClass('drop');
var file = event.dataTransfer.files[0];
var fd = new FormData();
// append hidden fields
if (this.opts.uploadFields !== false && typeof this.opts.uploadFields === 'object')
{
$mgt.each(this.opts.uploadFields, $mgt.proxy(function(k,v)
{
if (v.indexOf('#') === 0)
{
v = $mgt(v).val();
}
fd.append(k, v);
}, this));
}
// append file data
fd.append('file', file);
$mgt.ajax({
dataType: 'html',
url: this.opts.url,
data: fd,
xhr: provider,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: $mgt.proxy(function(data)
{
if (this.opts.success !== false)
{
this.opts.success(data);
}
if (this.opts.preview === true)
{
this.dropareabox.html(data);
}
}, this)
});
}, this);
}
},
ondrag: function()
{
this.dropareabox.addClass('hover');
return false;
},
ondragleave: function()
{
this.dropareabox.removeClass('hover');
return false;
}
};
})(jQuery);
//Define: Linkify plugin from stackoverflow
(function($){
"use strict";
var url1 = /(^|&lt;|\s)(www\..+?\..+?)(\s|&gt;|$)/g,
url2 = /(^|&lt;|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|&gt;|$)/g,
linkifyThis = function ()
{
var childNodes = this.childNodes,
i = childNodes.length;
while(i--)
{
var n = childNodes[i];
if (n.nodeType === 3)
{
var html = n.nodeValue;
if (html)
{
html = html.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(url1, '$1<a href="http://$2">$2</a>$3')
.replace(url2, '$1<a href="$2">$2</a>$5');
$(n).after(html).remove();
}
}
else if (n.nodeType === 1 && !/^(a|button|textarea)$/i.test(n.tagName))
{
linkifyThis.call(n);
}
}
};
$.fn.linkify = function ()
{
this.each(linkifyThis);
};
})(jQuery);
/* jQuery plugin textselect
* version: 0.9
* author: Josef Moravec, [email protected]
* updated: Imperavi Inc.
*
*/
(function($mgt){$mgt.event.special.textselect={setup:function(data,namespaces){$mgt(this).data("textselected",false);$mgt(this).data("ttt",data);$mgt(this).bind('mouseup',$mgt.event.special.textselect.handler)},teardown:function(data){$mgt(this).unbind('mouseup',$mgt.event.special.textselect.handler)},handler:function(event){var data=$mgt(this).data("ttt");var text=$mgt.event.special.textselect.getSelectedText(data).toString();if(text!=''){$mgt(this).data("textselected",true);event.type="textselect";event.text=text;$mgt.event.handle.apply(this,arguments)}},getSelectedText:function(data){var text='';if(window.getSelection)text=window.getSelection();else if(document.getSelection) text=document.getSelection();else if(document.selection)text=document.selection.createRange().text;return text}};$mgt.event.special.textunselect={setup:function(data,namespaces){$mgt(this).data("rttt",data);$mgt(this).data("textselected",false);$mgt(this).bind('mouseup',$mgt.event.special.textunselect.handler);$mgt(this).bind('keyup',$mgt.event.special.textunselect.handlerKey)},teardown:function(data){$mgt(this).unbind('mouseup',$mgt.event.special.textunselect.handler)},handler:function(event){if($mgt(this).data("textselected")){var data=$mgt(this).data("rttt");var text=$mgt.event.special.textselect.getSelectedText(data).toString();if(text==''){$mgt(this).data("textselected",false);event.type="textunselect";$mgt.event.handle.apply(this,arguments)}}},handlerKey:function(event){if($mgt(this).data("textselected")){var data=$mgt(this).data("rttt");var text=$mgt.event.special.textselect.getSelectedText(data).toString();if((event.keyCode=27)&&(text=='')){$mgt(this).data("textselected",false);event.type="textunselect";$mgt.event.handle.apply(this,arguments)}}}}})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment