/** * wysiwyg - jquery plugin 0.6 * * 澳门凯发 copyright (c) 2008-2009 juan m martinez * http://plugins.jquery.com/project/jwysiwyg * * dual licensed under the mit and gpl licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * $id: $ */ (function( $ ) { $.fn.document = function() { var element = this.get(0); if ( element.nodename.tolowercase() == 'iframe' ) { return element.contentwindow.document; /* return ( $.browser.msie ) ? document.frames[element.id].document : element.contentwindow.document // contentdocument; */ } return this; }; $.fn.documentselection = function() { var element = this.get(0); if ( element.contentwindow.document.selection ) return element.contentwindow.document.selection.createrange().text; else return element.contentwindow.getselection().tostring(); }; $.fn.wysiwyg = function( options ) { if ( arguments.length > 0 && arguments[0].constructor == string ) { var action = arguments[0].tostring(); var params = []; for ( var i = 1; i < arguments.length; i ) params[i - 1] = arguments[i]; if ( action in wysiwyg ) { return this.each(function() { $.data(this, 'wysiwyg') .designmode(); wysiwyg[action].apply(this, params); }); } else return this; } var controls = {}; /** * if the user set custom controls, we catch it, and merge with the * defaults controls later. */ if ( options && options.controls ) { var controls = options.controls; delete options.controls; } options = $.extend({ html : '<' '?xml version="1.0" encoding="utf-8"?' '>style_sheetinitial_content', css : {}, debug : false, autosave : true, // http://code.google.com/p/jwysiwyg/issues/detail?id=11 rmunwantedbr : true, // http://code.google.com/p/jwysiwyg/issues/detail?id=15 brie : true, controls : {}, messages : {} }, options); options.messages = $.extend(true, options.messages, wysiwyg.msgs_en); options.controls = $.extend(true, options.controls, wysiwyg.toolbar); for ( var control in controls ) { if ( control in options.controls ) $.extend(options.controls[control], controls[control]); else options.controls[control] = controls[control]; } // not break the chain return this.each(function() { wysiwyg(this, options); }); }; function wysiwyg( element, options ) { return this instanceof wysiwyg ? this.init(element, options) : new wysiwyg(element, options); } $.extend(wysiwyg, { insertimage : function( szurl, attributes ) { var self = $.data(this, 'wysiwyg'); if ( self.constructor == wysiwyg && szurl && szurl.length > 0 ) { if ($.browser.msie) self.focus(); if ( attributes ) { self.editordoc.execcommand('insertimage', false, '#jwysiwyg#'); var img = self.getelementbyattributevalue('img', 'src', '#jwysiwyg#'); if ( img ) { img.src = szurl; for ( var attribute in attributes ) { img.setattribute(attribute, attributes[attribute]); } } } else { self.editordoc.execcommand('insertimage', false, szurl); } } }, createlink : function( szurl ) { var self = $.data(this, 'wysiwyg'); if ( self.constructor == wysiwyg && szurl && szurl.length > 0 ) { var selection = $(self.editor).documentselection(); if ( selection.length > 0 ) { if ($.browser.msie) self.focus(); self.editordoc.execcommand('unlink', false, []); self.editordoc.execcommand('createlink', false, szurl); } else if ( self.options.messages.nonselection ) alert(self.options.messages.nonselection); } }, inserthtml : function( szhtml ) { var self = $.data(this, 'wysiwyg'); if ( self.constructor == wysiwyg && szhtml && szhtml.length > 0 ) { if ($.browser.msie) { self.focus(); self.editordoc.execcommand('insertimage', false, '#jwysiwyg#'); var img = self.getelementbyattributevalue('img', 'src', '#jwysiwyg#'); if (img) { $(img).replacewith(szhtml); } } else { self.editordoc.execcommand('inserthtml', false, szhtml); } } }, setcontent : function( newcontent ) { var self = $.data(this, 'wysiwyg'); self.setcontent( newcontent ); self.savecontent(); }, clear : function() { var self = $.data(this, 'wysiwyg'); self.setcontent(''); self.savecontent(); }, msgs_en : { nonselection : 'select the text you wish to link' }, toolbar : { bold : { visible : true, tags : ['b', 'strong'], css : { fontweight : 'bold' }, tooltip : "bold" }, italic : { visible : true, tags : ['i', 'em'], css : { fontstyle : 'italic' }, tooltip : "italic" }, strikethrough : { visible : true, tags : ['s', 'strike'], css : { textdecoration : 'line-through' }, tooltip : "strike-through" }, underline : { visible : true, tags : ['u'], css : { textdecoration : 'underline' }, tooltip : "underline" }, separator00 : { visible : true, separator : true }, justifyleft : { visible : true, css : { textalign : 'left' }, tooltip : "justify left" }, justifycenter : { visible : true, tags : ['center'], css : { textalign : 'center' }, tooltip : "justify center" }, justifyright : { visible : true, css : { textalign : 'right' }, tooltip : "justify right" }, justifyfull : { visible : true, css : { textalign : 'justify' }, tooltip : "justify full" }, separator01 : { visible : true, separator : true }, indent : { visible : true, tooltip : "indent" }, outdent : { visible : true, tooltip : "outdent" }, separator02 : { visible : false, separator : true }, subscript : { visible : true, tags : ['sub'], tooltip : "subscript" }, superscript : { visible : true, tags : ['sup'], tooltip : "superscript" }, separator03 : { visible : true, separator : true }, undo : { visible : true, tooltip : "undo" }, redo : { visible : true, tooltip : "redo" }, separator04 : { visible : true, separator : true }, insertorderedlist : { visible : true, tags : ['ol'], tooltip : "insert ordered list" }, insertunorderedlist : { visible : true, tags : ['ul'], tooltip : "insert unordered list" }, inserthorizontalrule : { visible : true, tags : ['hr'], tooltip : "insert horizontal rule" }, separator05 : { separator : true }, createlink : { visible : true, exec : function() { var selection = $(this.editor).documentselection(); if ( selection.length > 0 ) { if ( $.browser.msie ) { this.focus(); this.editordoc.execcommand('createlink', true, null); } else { var szurl = prompt('url', 'http://'); if ( szurl && szurl.length > 0 ) { this.editordoc.execcommand('unlink', false, []); this.editordoc.execcommand('createlink', false, szurl); } } } else if ( this.options.messages.nonselection ) alert(this.options.messages.nonselection); }, tags : ['a'], tooltip : "create link" }, insertimage : { visible : true, exec : function() { if ( $.browser.msie ) { this.focus(); this.editordoc.execcommand('insertimage', true, null); } else { var szurl = prompt('url', 'http://'); if ( szurl && szurl.length > 0 ) this.editordoc.execcommand('insertimage', false, szurl); } }, tags : ['img'], tooltip : "insert image" }, separator06 : { separator : true }, h1mozilla : { visible : true && $.browser.mozilla, classname : 'h1', command : 'heading', arguments : ['h1'], tags : ['h1'], tooltip : "header 1" }, h2mozilla : { visible : true && $.browser.mozilla, classname : 'h2', command : 'heading', arguments : ['h2'], tags : ['h2'], tooltip : "header 2" }, h3mozilla : { visible : true && $.browser.mozilla, classname : 'h3', command : 'heading', arguments : ['h3'], tags : ['h3'], tooltip : "header 3" }, h1 : { visible : true && !( $.browser.mozilla ), classname : 'h1', command : 'formatblock', arguments : ['

-澳门凯发

'], tags : ['h2'], tooltip : "header 2" }, h3 : { visible : true && !( $.browser.mozilla ), classname : 'h3', command : 'formatblock', arguments : ['

'], tags : ['h3'], tooltip : "header 3" }, separator07 : { visible : false, separator : true }, cut : { visible : false, tooltip : "cut" }, copy : { visible : false, tooltip : "copy" }, paste : { visible : false, tooltip : "paste" }, separator08 : { separator : false && !( $.browser.msie ) }, increasefontsize : { visible : false && !( $.browser.msie ), tags : ['big'], tooltip : "increase font size" }, decreasefontsize : { visible : false && !( $.browser.msie ), tags : ['small'], tooltip : "decrease font size" }, separator09 : { separator : true }, html : { visible : false, exec : function() { if ( this.viewhtml ) { this.setcontent( $(this.original).val() ); $(this.original).hide(); } else { this.savecontent(); $(this.original).show(); } this.viewhtml = !( this.viewhtml ); }, tooltip : "view source code" }, removeformat : { visible : true, exec : function() { if ($.browser.msie) this.focus(); this.editordoc.execcommand('removeformat', false, []); this.editordoc.execcommand('unlink', false, []); }, tooltip : "remove formatting" } } }); $.extend(wysiwyg.prototype, { original : null, options : {}, element : null, editor : null, focus : function() { $(this.editordoc.body).focus(); }, init : function( element, options ) { var self = this; this.editor = element; this.options = options || {}; $.data(element, 'wysiwyg', this); var newx = element.width || element.clientwidth; var newy = element.height || element.clientheight; if ( element.nodename.tolowercase() == 'textarea' ) { this.original = element; if ( newx == 0 && element.cols ) newx = ( element.cols * 8 ) 21; if ( newy == 0 && element.rows ) newy = ( element.rows * 16 ) 16; var editor = this.editor = $('').css({ minheight : ( newy - 6 ).tostring() 'px', width : ( newx - 8 ).tostring() 'px' }).attr('id', $(element).attr('id') 'iframe') .attr('frameborder', '0'); /** * http://code.google.com/p/jwysiwyg/issues/detail?id=96 */ this.editor.attr('tabindex', $(element).attr('tabindex')); if ( $.browser.msie ) { this.editor .css('height', ( newy ).tostring() 'px'); /** var editor = $('').css({ width : ( newx - 6 ).tostring() 'px', height : ( newy - 8 ).tostring() 'px' }).attr('id', $(element).attr('id') 'iframe'); editor.outerhtml = this.editor.outerhtml; */ } } var panel = this.panel = $(''); 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( $('') .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);

网站地图