/*
* Lightweight RTE - jQuery Plugin, v1.2
* Basic Toolbars
* Copyright (c) 2009 Andrey Gayvoronsky - http://www.gayvoronsky.com
*/
var rte_tag = '-rte-tmp-tag-';
var rte_toolbar_old = {
style : {exec: lwrte_style, init: lwrte_style_init},
removeFormat : {exec: lwrte_unformat},
word : {exec: lwrte_cleanup_word},
clear : {exec: lwrte_clear}
};
var rte_toolbar = {
bold : {command: 'bold', tags:['b', 'strong']},
italic : {command: 'italic', tags:['i', 'em']},
s1 : {separator : true},
justifyLeft : {command: 'justifyleft'},
justifyCenter : {command: 'justifycenter'},
justifyRight : {command: 'justifyright'},
justifyFull : {command: 'justifyfull'},
s2 : {separator : true},
unorderedList : {command: 'insertunorderedlist', tags: ['ul'] },
s3 : {separator : true},
removeFormat : {exec: lwrte_unformat},
word : {exec: lwrte_cleanup_word},
clear : {exec: lwrte_clear}
};
var html_toolbar = {
s1 : {separator: true},
word : {exec: lwrte_cleanup_word},
clear : {exec: lwrte_clear}
};
/*** tag compare callbacks ***/
function lwrte_block_compare(node, tag) {
tag = tag.replace(/<([^>]*)>/, '$1');
return (tag.toLowerCase() == node.nodeName.toLowerCase());
}
/*** init callbacks ***/
function lwrte_style_init(rte) {
var self = this;
self.select = '';
// load CSS info. javascript only issue is not working correctly, that's why ajax-php :(
if(rte.css.length) {
$.ajax({
url: "http://www.pierreyovanovitch.com/uisdk/getcss",
type: "POST",
data: { css: rte.css[rte.css.length - 1] },
async: false,
success: function(data) {
var list = data.split(',');
var select = "";
for(var name in list)
select += '';
self.select = '';
}});
}
}
/*** exec callbacks ***/
function lwrte_style(args) {
if(args) {
try {
var css = args.options[args.selectedIndex].value
var self = this;
var html = self.get_selected_text();
html = '' + html + '';
self.selection_replace_with(html);
args.selectedIndex = 0;
} catch(e) {
}
}
}
function lwrte_color(){
var self = this;
var panel = self.create_panel('Set color for text', 385);
var mouse_down = false;
var mouse_over = false;
panel.append('\
\
\
\
\
'
).show();
var preview = $('#preview', panel);
var color = $("#color", panel);
var palette = $("#palette", panel);
var colors = [
'#660000', '#990000', '#cc0000', '#ff0000', '#333333',
'#006600', '#009900', '#00cc00', '#00ff00', '#666666',
'#000066', '#000099', '#0000cc', '#0000ff', '#999999',
'#909000', '#900090', '#009090', '#ffffff', '#cccccc',
'#ffff00', '#ff00ff', '#00ffff', '#000000', '#eeeeee'
];
for(var i = 0; i < colors.length; i++)
$("").addClass("item").css('background', colors[i]).appendTo(palette);
var height = $('#rgb').height();
var part_width = $('#rgb').width() / 6;
$('#rgb,#gray,#palette', panel)
.mousedown( function(e) {mouse_down = true; return false; } )
.mouseup( function(e) {mouse_down = false; return false; } )
.mouseout( function(e) {mouse_over = false; return false; } )
.mouseover( function(e) {mouse_over = true; return false; } );
$('#rgb').mousemove( function(e) { if(mouse_down && mouse_over) compute_color(this, true, false, false, e); return false;} );
$('#gray').mousemove( function(e) { if(mouse_down && mouse_over) compute_color(this, false, true, false, e); return false;} );
$('#palette').mousemove( function(e) { if(mouse_down && mouse_over) compute_color(this, false, false, true, e); return false;} );
$('#rgb').click( function(e) { compute_color(this, true, false, false, e); return false;} );
$('#gray').click( function(e) { compute_color(this, false, true, false, e); return false;} );
$('#palette').click( function(e) { compute_color(this, false, false, true, e); return false;} );
$('#cancel', panel).click( function() { panel.remove(); return false; } );
$('#ok', panel).click(
function() {
var value = color.html();
if(value.length > 0 && value.charAt(0) =='#') {
if(self.iframe_doc.selection) //IE fix for lost focus
self.range.select();
self.editor_cmd('foreColor', value);
}
panel.remove();
return false;
}
);
function to_hex(n) {
var s = "0123456789abcdef";
return s.charAt(Math.floor(n / 16)) + s.charAt(n % 16);
}
function get_abs_pos(element) {
var r = { x: element.offsetLeft, y: element.offsetTop };
if (element.offsetParent) {
var tmp = get_abs_pos(element.offsetParent);
r.x += tmp.x;
r.y += tmp.y;
}
return r;
};
function get_xy(obj, event) {
var x, y;
event = event || window.event;
var el = event.target || event.srcElement;
// use absolute coordinates
var pos = get_abs_pos(obj);
// subtract distance to middle
x = event.pageX - pos.x;
y = event.pageY - pos.y;
return { x: x, y: y };
}
function compute_color(obj, is_rgb, is_gray, is_palette, e) {
var r, g, b, c;
var mouse = get_xy(obj, e);
var x = mouse.x;
var y = mouse.y;
if(is_rgb) {
r = (x >= 0)*(x < part_width)*255 + (x >= part_width)*(x < 2*part_width)*(2*255 - x * 255 / part_width) + (x >= 4*part_width)*(x < 5*part_width)*(-4*255 + x * 255 / part_width) + (x >= 5*part_width)*(x < 6*part_width)*255;
g = (x >= 0)*(x < part_width)*(x * 255 / part_width) + (x >= part_width)*(x < 3*part_width)*255 + (x >= 3*part_width)*(x < 4*part_width)*(4*255 - x * 255 / part_width);
b = (x >= 2*part_width)*(x < 3*part_width)*(-2*255 + x * 255 / part_width) + (x >= 3*part_width)*(x < 5*part_width)*255 + (x >= 5*part_width)*(x < 6*part_width)*(6*255 - x * 255 / part_width);
var k = (height - y) / height;
r = 128 + (r - 128) * k;
g = 128 + (g - 128) * k;
b = 128 + (b - 128) * k;
} else if (is_gray) {
r = g = b = (height - y) * 1.7;
} else if(is_palette) {
x = Math.floor(x / 10);
y = Math.floor(y / 10);
c = colors[x + y * 5];
}
if(!is_palette)
c = '#' + to_hex(r) + to_hex(g) + to_hex(b);
preview.css('background', c);
color.html(c);
}
}
function lwrte_image() {
var self = this;
var panel = self.create_panel('Insert image', 385);
panel.append('\
\
\
'
).show();
var url = $('#url', panel);
var upload = $('#file', panel).upload( {
autoSubmit: false,
action: 'uploader.php',
onSelect: function() {
var file = this.filename();
var ext = (/[.]/.exec(file)) ? /[^.]+$/.exec(file.toLowerCase()) : '';
if(!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
alert('Invalid file extension');
return;
}
this.submit();
},
onComplete: function(response) {
if(response.length <= 0)
return;
response = eval("(" + response + ")");
if(response.error && response.error.length > 0)
alert(response.error);
else
url.val((response.file && response.file.length > 0) ? response.file : '');
}
});
$('#view', panel).click( function() {
(url.val().length >0 ) ? window.open(url.val()) : alert("Enter URL of image to view");
return false;
}
);
$('#cancel', panel).click( function() { panel.remove(); return false;} );
$('#ok', panel).click(
function() {
var file = url.val();
self.editor_cmd('insertImage', file);
panel.remove();
return false;
}
)
}
function lwrte_unformat() {
this.editor_cmd('removeFormat');
this.editor_cmd('unlink');
}
function lwrte_clear() {
if(confirm('Clear Document?'))
this.set_content('');
}
function lwrte_cleanup_word() {
this.set_content(cleanup_word(this.get_content(), true, true, true));
function cleanup_word(s, bIgnoreFont, bRemoveStyles, bCleanWordKeepsStructure) {
s = s.replace(/\s*<\/o:p>/g, '') ;
s = s.replace(/[\s\S]*?<\/o:p>/g, ' ') ;
// Remove mso-xxx styles.
s = s.replace( /\s*mso-[^:]+:[^;"]+;?/gi, '' ) ;
// Remove margin styles.
s = s.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, '' ) ;
s = s.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;
s = s.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, '' ) ;
s = s.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;
s = s.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;
s = s.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;
s = s.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;
s = s.replace( /\s*tab-stops:[^;"]*;?/gi, '' ) ;
s = s.replace( /\s*tab-stops:[^"]*/gi, '' ) ;
// Remove FONT face attributes.
if (bIgnoreFont) {
s = s.replace( /\s*face="[^"]*"/gi, '' ) ;
s = s.replace( /\s*face=[^ >]*/gi, '' ) ;
s = s.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, '' ) ;
}
// Remove Class attributes
s = s.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove styles.
if (bRemoveStyles)
s = s.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;
// Remove style, meta and link tags
s = s.replace( /