');
this.appendControls();
this.element = $('').css({
width: (newX > 0) ? (newX).toString() + 'px' : '100%'
}).addClass('wysiwyg').append(panel).append($('').css({
clear: 'both'
})).append(editor);
$(element).hide().before(this.element);
this.viewHTML = false;
this.initialHeight = newY - 8;
/**
* @link http://code.google.com/p/jwysiwyg/issues/detail?id=52
*/
this.initialContent = $(element).val();
this.initFrame();
if (this.initialContent.length == 0) this.setContent('');
/**
* http://code.google.com/p/jwysiwyg/issues/detail?id=100
*/
var form = $(element).closest('form');
if (this.options.autoSave) {
form.submit(function () {
self.saveContent();
});
}
form.bind('reset', function () {
self.setContent(self.initialContent);
self.saveContent();
});
},
initFrame: function () {
var self = this;
var style = '';
/**
* @link http://code.google.com/p/jwysiwyg/issues/detail?id=14
*/
if (this.options.css && this.options.css.constructor == String) {
style = '';
}
this.editorDoc = $(this.editor).document();
this.editorDoc_designMode = false;
try {
this.editorDoc.designMode = 'on';
this.editorDoc_designMode = true;
} catch (e) {
// Will fail on Gecko if the editor is placed in an hidden container element
// The design mode will be set ones the editor is focused
$(this.editorDoc).focus(function () {
self.designMode();
});
}
this.editorDoc.open();
this.editorDoc.write(
this.options.html
/**
* @link http://code.google.com/p/jwysiwyg/issues/detail?id=144
*/
.replace(/INITIAL_CONTENT/, function () {
return self.initialContent;
}).replace(/STYLE_SHEET/, function () {
return style;
}));
this.editorDoc.close();
this.editorDoc.contentEditable = 'true';
if ($.browser.msie) {
/**
* Remove the horrible border it has on IE.
*/
setTimeout(function () {
$(self.editorDoc.body).css('border', 'none');
}, 0);
}
$(this.editorDoc).click(function (event) {
self.checkTargets(event.target ? event.target : event.srcElement);
});
/**
* @link http://code.google.com/p/jwysiwyg/issues/detail?id=20
*/
$(this.original).focus(function () {
if (!$.browser.msie) {
self.focus();
}
});
if (this.options.autoSave) {
/**
* @link http://code.google.com/p/jwysiwyg/issues/detail?id=11
*/
$(this.editorDoc).keydown(function () {
self.saveContent();
}).keyup(function () {
self.saveContent();
}).mousedown(function () {
self.saveContent();
});
}
if (this.options.css) {
setTimeout(function () {
if (self.options.css.constructor == String) {
/**
* $(self.editorDoc)
* .find('head')
* .append(
* $('')
* .attr('href', self.options.css)
* );
*/
} else $(self.editorDoc).find('body').css(self.options.css);
}, 0);
}
$(this.editorDoc).keydown(function (event) {
if ($.browser.msie && self.options.brIE && event.keyCode == 13) {
var rng = self.getRange();
rng.pasteHTML(' ');
rng.collapse(false);
rng.select();
return false;
}
return true;
});
},
designMode: function () {
if (!(this.editorDoc_designMode)) {
try {
this.editorDoc.designMode = 'on';
this.editorDoc_designMode = true;
} catch (e) {}
}
},
getSelection: function () {
return (window.getSelection) ? window.getSelection() : document.selection;
},
getRange: function () {
var selection = this.getSelection();
if (!(selection)) return null;
return (selection.rangeCount > 0) ? selection.getRangeAt(0) : selection.createRange();
},
getContent: function () {
return $($(this.editor).document()).find('body').html();
},
setContent: function (newContent) {
$($(this.editor).document()).find('body').html(newContent);
},
saveContent: function () {
if (this.original) {
var content = this.getContent();
if (this.options.rmUnwantedBr) {
content = (content.substr(-4) == ' ') ? content.substr(0, content.length - 4) : content;
}
$(this.original).val(content);
}
},
withoutCss: function () {
if ($.browser.mozilla) {
try {
this.editorDoc.execCommand('styleWithCSS', false, false);
} catch (e) {
try {
this.editorDoc.execCommand('useCSS', false, true);
} catch (e) {}
}
}
},
appendMenu: function (cmd, args, className, fn, tooltip) {
var self = this;
args = args || [];
$('
').append(
$('' + (className || cmd) + '').addClass(className || cmd).attr('title', tooltip)).click(function () {
if (fn) fn.apply(self);
else {
self.withoutCss();
self.editorDoc.execCommand(cmd, false, args);
}
if (self.options.autoSave) self.saveContent();
}).appendTo(this.panel);
},
appendMenuSeparator: function () {
$('').appendTo(this.panel);
},
appendControls: function () {
for (var name in this.options.controls) {
var control = this.options.controls[name];
if (control.separator) {
if (control.visible !== false) this.appendMenuSeparator();
} else if (control.visible) {
this.appendMenu(
control.command || name, control.arguments || [], control.className || control.command || name || 'empty', control.exec, control.tooltip || control.command || name || '');
}
}
},
checkTargets: function (element) {
for (var name in this.options.controls) {
var control = this.options.controls[name];
var className = control.className || control.command || name || 'empty';
$('.' + className, this.panel).removeClass('active');
if (control.tags) {
var elm = element;
do {
if (elm.nodeType != 1) break;
if ($.inArray(elm.tagName.toLowerCase(), control.tags) != -1) $('.' + className, this.panel).addClass('active');
} while ((elm = elm.parentNode));
}
if (control.css) {
var elm = $(element);
do {
if (elm[0].nodeType != 1) break;
for (var cssProperty in control.css)
if (elm.css(cssProperty).toString().toLowerCase() == control.css[cssProperty]) $('.' + className, this.panel).addClass('active');
} while ((elm = elm.parent()));
}
}
},
getElementByAttributeValue: function (tagName, attributeName, attributeValue) {
var elements = this.editorDoc.getElementsByTagName(tagName);
for (var i = 0; i < elements.length; i++) {
var value = elements[i].getAttribute(attributeName);
if ($.browser.msie) { /** IE add full path, so I check by the last chars. */
value = value.substr(value.length - attributeValue.length);
}
if (value == attributeValue) return elements[i];
}
return false;
}
});
})(jQuery);