Вход Регистрация
Файл: static/plugins/base/js/suitup.jquery.js
Строк: 467
<?php
"use strict";
( function( 
windoc, $ ) 

    
//-- protected functions -- //

    /**
     * Create a tag
     * 
     * @return object
     */
    
var create = function(tagNameprops
    {
        return $($.
extenddoc.createElementtagName ), props ));
    };

    
/**
     * Save html
     * 
     * @param object suitUpBlock
     * @param string type
     * @return void
     */
    
var saveHtml = function(textareasuitUpBlock)
    {
        
textarea.value suitUpBlock.html();
    };

    
/**
     * Wyswyg
     */
    
$.suitUp = {
        
/**
         * Default controls
         * 
         * @var array
         */
        
controls: [ 'italic''bold''undeline''link''image''video' ],

        
/**
         * Image upload url
         * 
         * @var string
         */
        
imageUploadUrl'',

        
/**
         * Commands
         * 
         * @var object
         */
        
commands: {
        },

        
/**
         * Custom buttons
         * 
         * @var object
         */
        
custom: {
            
/**
             * Image uploader
             * 
             * @param object textarea
             * @param object suitUpBlock
             * @return void
             */
            
image: function(textareasuitUpBlock
            {
                var 
oldSelection '';
 
                
// create the upload image input
                
var $uploadImage = $('<input type="file" class="suitup_upload_image" style="display:none" accept="image/*" />' ).html5_upload({
                    
url: $.suitUp.getImageUploadUrl(),
                    
sendBoundarywindow.FormData || $.browser.mozilla,
                    
fieldName 'file',
                    
extraFields : {
                        
'command' 'image-upload'
                    
},
                    
onStartOne: function(eventnamenumbertotal
                    {
                        
suitUpBlock.focus();
                        
oldSelection = $.suitUp.getSelection();
                        
suitUpBlock.addClass("peepm_preloader");
                        return 
true;
                    },
                    
onFinishOne: function(eventresponsenamenumbertotal
                    {
                        
suitUpBlock.removeClass("peepm_preloader");
                        var 
result jQuery.parseJSON(response);
 
                        
// show an error message
                        
if ( typeof result.error_message != "undefined" && result.error_message )
                        {
                           
PEEPM.message(result.error_message'error');
                           return;
                        }
 
                        
suitUpBlock.focus();
                        $.
suitUp.restoreSelection(oldSelection);
 
                        
// show the image
                        
var content '<br /><a href="' result.file_url 
                                
'" target="_blank"><img src="' result.file_url '"></a><br /><br />';
 
                        
document.execCommand('insertHTML'falsecontent);
                        
saveHtml(textareasuitUpBlock);
                    },
                    
onError: function(eventnameerror) {
                        
suitUpBlock.removeClass("peepm_preloader");
                    }
                });

                $(
$uploadImage).insertAfter(suitUpBlock);

                
// create a new button
                
return create("a", {
                    
className"peepm_suitup-control",
                    
href"javascript://"
                
}).attr({
                    
"data-command""insertImage"
                
}).on("click", function() {
                    
// show the file choose window
                    
$uploadImage.trigger("click");
                });
            },
            
link: function(textareasuitUpBlock
            {
                return 
create("a", {
                    
className"peepm_suitup-control",
                    
href"javascript://"
                
}).attr({
                    
"data-command""createlink"
                
}).on("click", function() {
                    if (!$.
suitUp.hasSelectedNodeParent("a")) {
                        
suitUpBlock.focus();
                        var 
oldSelection = $.suitUp.getSelection();

                        var 
floatBox PEEPM.ajaxFloatBox("BASE_MCMP_InsertLink", [{ "linkText" : $.suitUp.getSelectedText() }], {
                            
"title" PEEP.getLanguageText('base''ws_button_label_link'),
                            
"scope" : {
                                
"success" : function(data) {
                                    
floatBox.close();
                                    
suitUpBlock.focus();
                                    $.
suitUp.restoreSelection(oldSelection);

                                    
document.execCommand('insertHTML'false'<a href="' data.link '" rel="nofollow" target="_blank">' data.title '</a>');
                                    
saveHtml(textareasuitUpBlock);
                                }
                            }
                        });
                    } else {
                        
doc.execCommand('unlink'falsenull);
                        
saveHtml(textareasuitUpBlock);
                    }
                });
            },
            
video: function(textareasuitUpBlock
            {
                return 
create("a", {
                    
className"peepm_suitup-control",
                    
href"javascript://"
                
}).attr({
                    
"data-command""insertVideo"
                
}).on("click", function() {
                    
suitUpBlock.focus();
                    var 
oldSelection = $.suitUp.getSelection();

                    var 
floatBox PEEPM.ajaxFloatBox("BASE_MCMP_InsertVideo", [], {
                         
"title" PEEP.getLanguageText('base''ws_button_label_video'),
                         
"scope" : {
                            
"success" : function(data) {
                                $.
ajax({
                                    
url: $.suitUp.embedUrl,
                                    
data: { urldata.link },
                                    
cachefalse,
                                    
success: function(response) {
                                        var 
data jQuery.parseJSON(response);

                                        if (
typeof data.type == "undefined" 
                                            
|| typeof data.html == "undefined" 
                                            
|| data.type != "video") {

                                            
PEEPM.message(PEEP.getLanguageText('base''ws_error_video'), 'error');
                                            return;
                                        }

                                        
floatBox.close();
                                        
suitUpBlock.focus();
                                        $.
suitUp.restoreSelection(oldSelection);

                                        
document.execCommand('insertHTML'false'<br />' data.html '<br /><br />');
                                        
saveHtml(textareasuitUpBlock);
                                    },
                                    
'error' : function() {
                                        
PEEPM.message(PEEP.getLanguageText('base''ws_error_video'), 'error');
                                    }
                                });
                            }
                         }
                     });
                });
            }
        },

        
/**
         * Get selected text in editable area
         *
         * @return string
         */
        
getSelectedText: function() 
        {
            return 
this.getSelection().toString();
        },

        
/**
         * Get image upload url
         * 
         * @return string
         */
        
getImageUploadUrl: function() 
        {
            return 
this.imageUploadUrl;
        },

        
/**
         * Get current selection
         * 
         * @return object
         */
        
getSelection: function() 
        {
            var 
range;

            if (
win.getSelection) {
                try {
                    
range win.getSelection().getRangeAt);
                } catch(
e) {
                  
                }
            } else if (
doc.selection) { 
                
range doc.selection.createRange();  
            }

            return 
range;
        },

        
/**
         * Restore selection
         * 
         * @param object range
         * @retrun void
         */
        
restoreSelection: function(range
        {
            var 
s;

            if (
range) {
                if (
win.getSelection) {
                    
win.getSelection();
                    if (
s.rangeCount 0) {
                        
s.removeAllRanges();
                    }

                    
s.addRange(range);
                } else if (
doc.createRange) {
                    
win.getSelection().addRange(range);
                } else if (
doc.selection) {
                    
range.select();
                }
            }
        },

        
/**
         * Get selected node
         * 
         * @return object
         */
        
getSelectedNode: function() 
        {
            if (
doc.selection) {
                return 
doc.selection.createRange().parentElement();
            } else {
                var 
selection win.getSelection();
 
                if (
selection.rangeCount 0) {
                    return 
selection.getRangeAt(0).endContainer;
                }
            }
        },

        
/**
         * Has selected node parent
         * 
         * @param strign tagName
         * @return boolean
         */
        
hasSelectedNodeParent: function(tagName
        {
            var 
node this.getSelectedNode(), has false;
            
tagName tagName.toUpperCase();
 
            while (
node && node.tagName !== 'BODY') {
                if (
node.tagName === tagName) {
                    
has true;
                    break;
                }
 
                
node node.parentNode;
            }

            return 
has;
        }
    };

/**
 * Suitup jquery plugin
 * 
 * @param array controls
 * @param string imageUploadUrl
 * @param string embedUrl
 */
$.fn.suitUp = function(controlsimageUploadUrlembedUrl
{
    var 
suitUp = $.suitUp,
            
lastSelectionRange,
            
lastSelectionElement,
            
commands = $.suitUp.commands,
            
custom   = $.suitUp.custom,
            
restoreSelection suitUp.restoreSelection;

    
controls controls || $.suitUp.controls;
    
controls controls instanceof Array ? controls : Array.prototype.slice.callarguments ); // IE changes the arguments object when one of the arguments is redefined

    
$.suitUp.imageUploadUrl imageUploadUrl || '';
    $.
suitUp.embedUrl embedUrl || '';
 
    return 
this.each( function() {
            var 
that this,
                    
self = $( this ).hide(),
                    
buttonControls,
                    
selectControls,
                    
typeofCommandValue,
                    
commandValue,
                    
select,

                    
mainBlock create'div', {
                            
className'peepm_suitup'
                    
}),

                    
containerBlock create'div', {
                            
className'peepm_suitup-editor',
                            
contentEditabletrue
                    
}).keyup( function(){ 
                            
updateTextarea();
                            
highlightActiveControls();
                    }).
focus( function(){
                            
lastSelectionElement this;
                            
//document.execCommand('styleWithCSS', null, false);
                    
}).mouseup( function(){
                            
highlightActiveControls();
                    })
                    .
htmlthat.value )
                    .
appendTomainBlock ),

                    
controlsBlock create'div', {
                            
className'peepm_suitup-controls'
                    
}).appendTomainBlock ),

                    
updateTextarea = function() {
                         
saveHtml(thatcontainerBlock);
                    },

                    
highlightActiveControls = function() {
                            
buttonControls buttonControls || $( 'a.peepm_suitup-control'controlsBlock );
                            
buttonControls
                             
.removeClass'active' )
                             .
each( function(){
                                    var 
self = $( this ),
                                            
command self.data'command' ),
                                            
value self.data'value' );

                                    try {
                                            
value value value.replace'<''' ).replace'>''' ) : value// for formatBlock
                                            
doc.queryCommandValuecommand ) === ( value || 'true' ) && self.addClass'active' );
                                    } catch( 
) {}
                                    try {
                                            
doc.queryCommandStatecommand ) && self.addClass'active' );
                                    } catch( 
) {}
                            });

                            
selectControls selectControls || $( 'select.peepm_suitup-control'controlsBlock );
                            
selectControls.each( function(){
                                    var 
self = $( this ),
                                            
command self.data'command' ),
                                            
value doc.queryCommandValuecommand ),
                                            
option self.children'option' ).filter( function() {
                                                    return 
value && this.value.toLowerCase() === value.toLowerCase();
                                            });

                                    if( 
option.length ) {
                                            
this.value option.val();
                                    }
                            });
                    }

            for( var 
splittedControl0control controls]; controls.lengthcontrol controls[ ++] ) {
                    
splittedControl control.split'#' );
                    
control splittedControl];
                    
commandValue splittedControl];

                    if( 
control === '|' ) {
                            
create'span', {
                                    
className'peepm_suitup-separator'
                            
}).appendTocontrolsBlock );

                    } else if( 
control in custom ) {
                        
customcontrol ]( thatcontainerBlock ).appendTocontrolsBlock );
                    } else {
                            
commandValue commandValue || commandscontrol ] || null;
                            
typeofCommandValue typeof commandValue;

                            if( 
commandValue && typeofCommandValue === 'object' ) {
                                    
select create'select', {
                                            
className'peepm_suitup-control'
                                    
})
                                    .
attr'data-command'control )
                                    .
appendTocontrolsBlock )
                                    .
on'change', { commandcontrol }, function( event ) {
                                            var 
command event.data.command;
                                            
doc.execCommandcommandnullthis.value );
                                            
updateTextarea();
                                    });

                                    $.
eachcommandValue, function( displayNamecommandValue ) {
                                            
create'option', {
                                                    
valuecommandValue
                                            
}).htmldisplayName )
                                            .
appendToselect );
                                    });
                            } else {
                                    
create'a', {
                                            
href'#',
                                            
className'peepm_suitup-control'
                                    
})
                                    .
attr({
                                            
'data-command'control,
                                            
'data-value'typeofCommandValue === 'function' '_DYNAMIC_' commandValue
                                    
})
                                    .
appendTocontrolsBlock )
                                    .
on'click', { commandcontrolvaluecommandValuetypeofValuetypeofCommandValue }, function( event ){
                                            var 
command event.data.command,
                                                    
value event.data.value,
                                                    
typeofValue event.data.typeofValue,
                                                    
resultValue;

                                            if( 
lastSelectionElement !== containerBlock] || !lastSelectionRange ) {
                                                
containerBlock.focus();
                                            }

                                            if( 
typeofValue === 'function' ) {                                               
                                                    
lastSelectionRange getSelection();
                                                    
value( function( resultValue ) {
                                                            
lastSelectionElement.focus();
                                                            
restoreSelectionlastSelectionRange );
                                                            
doc.execCommandcommandnullresultValue );
                                                            
updateTextarea();
                                                    });
                                            } else {
                                                    
resultValue value;
                                                    
doc.execCommandcommandnullresultValue );
                                                    
updateTextarea();
                                                    
highlightActiveControls();
                                            }

                                            return 
false;
                                    });
                            }

                    } 
            }

            
mainBlock.insertBefore(that);
    });
};
})( 
windowdocumentjQuery );
?>
Онлайн: 1
Реклама