Вход Регистрация
Файл: send/jquery.wysiwyg.js
Строк: 669
<?php
(function( $ )
{
    $.
fn.document = function()
    {
        var 
element this[0];

        if ( 
element.nodeName.toLowerCase() == 'iframe' )
            return 
element.contentWindow.document;
            
/*
            return ( $.browser.msie )
                ? document.frames[element.id].document
                : element.contentWindow.document // contentDocument;
             */
        
else
            return $(
this);
    };

    $.
fn.documentSelection = function()
    {
        var 
element this[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 && arguments[0].constructor == String )
        {
            var 
action arguments[0].toString();
            var 
params = [];

            for ( var 
1arguments.lengthi++ )
                
params[1] = arguments[i];

            if ( 
action in Wysiwyg )
            {
                return 
this.each(function()
                {
                    $.
data(this'wysiwyg')
                     .
designMode();

                    
Wysiwyg[action].apply(thisparams);
                });
            }
            else return 
this;
        }

        var 
controls = {};

        if ( 
options && options.controls )
        {
            var 
controls options.controls;
            
delete options.controls;
        }

        var 
options = $.extend({
            
html '<'+'?xml version="1.0" encoding="UTF-8"?'+'><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">STYLE_SHEET</head><body>INITIAL_CONTENT</body></html>',
            
css  : {},
            
debug        false,
            
autoSave     true,
            
rmUnwantedBr true,
            
brIE         true,
            
controls : {},
            
messages : {}
        }, 
options);

        
options.messages = $.extend(trueoptions.messagesWysiwyg.MSGS_EN);
        
options.controls = $.extend(trueoptions.controlsWysiwyg.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(thisoptions);
        });
    };

    function 
Wysiwygelementoptions )
    {
        return 
this instanceof Wysiwyg
            
this.init(elementoptions)
            : new 
Wysiwyg(elementoptions);
    }

    $.
extend(Wysiwyg, {
        
insertImage : function( szURLattributes )
        {
            var 
self = $.data(this'wysiwyg');

            if ( 
self.constructor == Wysiwyg && szURL && szURL.length )
            {
                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(attributeattributes[attribute]);
                        }
                    }
                }
                else
                {
                    
self.editorDoc.execCommand('insertImage'falseszURL);
                }
            }
        },

        
createLink : function( szURL )
        {
            var 
self = $.data(this'wysiwyg');

            if ( 
self.constructor == Wysiwyg && szURL && szURL.length )
            {
                var 
selection = $(self.editor).documentSelection();

                if ( 
selection.length )
                {
                    
self.editorDoc.execCommand('unlink'false, []);
                    
self.editorDoc.execCommand('createLink'falseszURL);
                }
                else if ( 
self.options.messages.nonSelection )
                    
alert(self.options.messages.nonSelection);
            }
        },

        
setContent : function( newContent )
        {
            var 
self = $.data(this'wysiwyg');
                
self.setContentnewContent );
                
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 truetags : ['b''strong'], css : { fontWeight 'bold' } },
            
italic        : { visible truetags : ['i''em'], css : { fontStyle 'italic' } },
            
strikeThrough : { visible falsetags : ['s''strike'], css : { textDecoration 'line-through' } },
            
underline     : { visible falsetags : ['u'], css : { textDecoration 'underline' } },

            
separator00 : { visible falseseparator true },

            
justifyLeft   : { visible falsecss : { textAlign 'left' } },
            
justifyCenter : { visible falsetags : ['center'], css : { textAlign 'center' } },
            
justifyRight  : { visible falsecss : { textAlign 'right' } },
            
justifyFull   : { visible falsecss : { textAlign 'justify' } },

            
separator01 : { visible falseseparator true },

            
indent  : { visible false },
            
outdent : { visible false },

            
separator02 : { visible falseseparator true },

            
subscript   : { visible falsetags : ['sub'] },
            
superscript : { visible falsetags : ['sup'] },

            
separator03 : { visible falseseparator true },

            
undo : { visible false },
            
redo : { visible false },

            
separator04 : { visible falseseparator true },

            
insertOrderedList    : { visible falsetags : ['ol'] },
            
insertUnorderedList  : { visible falsetags : ['ul'] },
            
insertHorizontalRule : { visible falsetags : ['hr'] },

            
separator05 : { separator true },

            
createLink : {
                
visible true,
                
exec    : function()
                {
                    var 
selection = $(this.editor).documentSelection();

                    if ( 
selection.length )
                    {
                        if ( $.
browser.msie )
                            
this.editorDoc.execCommand('createLink'truenull);
                        else
                        {
                            var 
szURL prompt('Адрес ссылки''http://');

                            if ( 
szURL && szURL.length )
                            {
                                
this.editorDoc.execCommand('unlink'false, []);
                                
this.editorDoc.execCommand('createLink'falseszURL);
                            }
                        }
                    }
                    else if ( 
this.options.messages.nonSelection )
                        
alert(this.options.messages.nonSelection);
                },

                
tags : ['a']
            },

            
insertImage : {
                
visible true,
                
exec    : function()
                {
                    if ( $.
browser.msie )
                        
this.editorDoc.execCommand('insertImage'truenull);
                    else
                    {
                        var 
szURL prompt('Адрес изображения''http://');

                        if ( 
szURL && szURL.length )
                            
this.editorDoc.execCommand('insertImage'falseszURL);
                    }
                },

                
tags : ['img']
            },

            
separator06 : { separator true },

            
h1mozilla : { visible true && $.browser.mozillaclassName 'h1'command 'heading'arguments : ['h1'], tags : ['h1'] },
            
h2mozilla : { visible true && $.browser.mozillaclassName 'h2'command 'heading'arguments : ['h2'], tags : ['h2'] },
            
h3mozilla : { visible true && $.browser.mozillaclassName 'h3'command 'heading'arguments : ['h3'], tags : ['h3'] },

            
h1 : { visible true && !( $.browser.mozilla ), className 'h1'command 'formatBlock'arguments : ['Heading 1'], tags : ['h1'] },
            
h2 : { visible true && !( $.browser.mozilla ), className 'h2'command 'formatBlock'arguments : ['Heading 2'], tags : ['h2'] },
            
h3 : { visible true && !( $.browser.mozilla ), className 'h3'command 'formatBlock'arguments : ['Heading 3'], tags : ['h3'] },

            
separator07 : { visible falseseparator true },

            
cut   : { visible false },
            
copy  : { visible false },
            
paste : { visible false },

            
separator08 : { separator true && !( $.browser.msie ) },

            
increaseFontSize : { visible true && !( $.browser.msie ), tags : ['big'] },
            
decreaseFontSize : { visible true && !( $.browser.msie ), tags : ['small'] },

            
separator09 : { separator true },

            
html : {
                
visible false,
                
exec    : function()
                {
                    if ( 
this.viewHTML )
                    {
                        
this.setContent( $(this.original).val() );
                        $(
this.original).hide();
                        
document.getElementById("wysiwygIFrame").style.display="inline";
                    }
                    else
                    {
                        
this.saveContent();
                        $(
this.original).show();
                        
document.getElementById("wysiwygIFrame").style.display="none";

                    }

                    
this.viewHTML = !( this.viewHTML );
                }
            },

            
removeFormat : {
                
visible false,
                
exec    : function()
                {
                    
this.editorDoc.execCommand('removeFormat'false, []);
                    
this.editorDoc.execCommand('unlink'false, []);
                }
            }
        }
    });

    $.
extend(Wysiwyg.prototype,
    {
        
original null,
        
options  : {},

        
element  null,
        
editor   null,

        
init : function( elementoptions )
        {
            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 == && element.cols )
                    
newX = ( element.cols ) + 21;

                if ( 
newY == && element.rows )
                    
newY = ( element.rows 16 ) + 16;

                var 
editor this.editor = $('<iframe></iframe>').css({
                    
minHeight : ( newY ).toString() + 'px',
                    
width     : ( newX ).toString() + 'px'
                
}).attr('id', $(element).attr('id') + 'IFrame');

                if ( $.
browser.msie )
                {
                    
this.editor
                        
.css('height', ( newY ).toString() + 'px');

                    
/**
                    var editor = $('<span></span>').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 = $('<ul></ul>').addClass('panel');
            
this.appendControls();
            
this.element = $('<div id="wysiwygpanel"></div>').css({
                
width : ( newX ) ? ( newX ).toString() + 'px' '100%'
            
}).addClass('wysiwyg')
              .
append(panel)
              .
append( $('<div><!-- --></div>').css({ clear 'both' }) )
              .
append(editor);

            $(
element)
            
// .css('display', 'none')
            
.hide()
            .
before(this.element);

            
this.viewHTML false;

            
this.initialHeight newY 8;

            
this.initialContent = $(element).val();

            
this.initFrame();

            if ( 
this.initialContent.length == )
                
this.setContent('');

            if ( 
this.options.autoSave )
                $(
'form').submit(function() { self.saveContent(); });

            $(
'form').bind('reset', function()
            {
                
self.setContentself.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 '<link rel="stylesheet" type="text/css" media="screen" href="' this.options.css '" />';

            
this.editorDoc = $(this.editor).document();
            
this.editorDoc_designMode false;

            try {
                
this.editorDoc.designMode 'on';
                
this.editorDoc_designMode true;
            } catch ( 
) {
                
// 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
                    
.replace(/INITIAL_CONTENT/, this.initialContent)
                    .
replace(/STYLE_SHEET/, 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.checkTargetsevent.target event.target event.srcElement);
            });

            $(
this.original).focus(function()
            {
                $(
self.editorDoc.body).focus();
            });

            if ( 
this.options.autoSave )
            {

                $(
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 )
                    {
                    }
                    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('<br />');
                        
rng.collapse(false);
                        
rng.select();

                    return 
false;
                }
            });
        },

        
designMode : function()
        {
            if ( !( 
this.editorDoc_designMode ) )
            {
                try {
                    
this.editorDoc.designMode 'on';
                    
this.editorDoc_designMode true;
                } catch ( 
) {}
            }
        },

        
getSelection : function()
        {
            return ( 
window.getSelection ) ? window.getSelection() : document.selection;
        },

        
getRange : function()
        {
            var 
selection this.getSelection();

            if ( !( 
selection ) )
                return 
null;

            return ( 
selection.rangeCount ) ? 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) == '<br>' ) ? content.substr(0content.length 4) : content;

                $(
this.original).val(content);
            }
        },

        
appendMenu : function( cmdargsclassNamefn )
        {
            var 
self this;
            var 
args args || [];

            $(
'<li></li>').append(
                $(
'<a><!-- --></a>').addClass(className || cmd)
            ).
mousedown(function() {
                if ( 
fn fn.apply(self); else self.editorDoc.execCommand(cmdfalseargs);
                if ( 
self.options.autoSave self.saveContent();
            }).
appendTothis.panel );
        },

        
appendMenuSeparator : function()
        {
            $(
'<li class="separator"></li>').appendTothis.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 || namecontrol.arguments || [],
                        
control.className || control.command || name || 'empty'control.exec
                    
);
                }
            }
        },

        
checkTargets : function( element )
        {
            for ( var 
name in this.options.controls )
            {
                var 
control this.options.controls[name];
                var 
className control.className || control.command || name || 'empty';

                $(
'.' classNamethis.panel).removeClass('active');

                if ( 
control.tags )
                {
                    var 
elm element;

                    do {
                        if ( 
elm.nodeType != )
                            break;

                        if ( $.
inArray(elm.tagName.toLowerCase(), control.tags) != -)
                            $(
'.' classNamethis.panel).addClass('active');
                    } while ( 
elm elm.parentNode );
                }

                if ( 
control.css )
                {
                    var 
elm = $(element);

                    do {
                        if ( 
elm[0].nodeType != )
                            break;

                        for ( var 
cssProperty in control.css )
                            if ( 
elm.css(cssProperty).toString().toLowerCase() == control.css[cssProperty] )
                                $(
'.' classNamethis.panel).addClass('active');
                    } while ( 
elm elm.parent() );
                }
            }
        },

        
getElementByAttributeValue : function( tagNameattributeNameattributeValue )
        {
            var 
elements this.editorDoc.getElementsByTagName(tagName);

            for ( var 
0elements.lengthi++ )
            {
                var 
value elements[i].getAttribute(attributeName);

                if ( $.
browser.msie )
                {

                    
value value.substr(value.length attributeValue.length);
                }

                if ( 
value == attributeValue )
                    return 
elements[i];
            }

            return 
false;
        }
    });
})(
jQuery);
?>
Онлайн: 0
Реклама