Вход Регистрация
Файл: static/plugins/base/js/component_drag_and_drop.js
Строк: 854
<?php
componentDragAndDrop 
= function( prototypeObject ) {

    for ( var 
prop in prototypeObject ) {

        if ( 
this[prop] !== undefined ) {
            
this['parent_' prop] = this[prop];
        }

        
this[prop] = prototypeObject[prop];
    }

}


componentDragAndDrop.prototype = {

    
prepare: function()
        {
            
PEEP.WidgetPanel this;
        var 
self this;

        
this.sectionSelector '.place_section';
        
this.$panel = $('#place_components');
        
this.$sections = $(this.sectionSelector);
        
this.$clonablePanel = $('#clonable_components');
        
this.transfered false;

        
this.defaultSettings = {

            
placeholder'hidden-placeholder',
            
connectWiththis.sectionSelector,

            
helper: function(eui) {
                return 
self.getHelper(thiseui);
            },

            
start: function(eui) {
                return 
self.handlStart(thiseui);
            },

            
stop: function(eui) {
                
self.handleStop(thiseui);
                
self.complete();
            },

            
beforeStop: function(eui) {
                if (
ui.item.parent().hasClass('peep_dnd_locked_section'))
                {
                    $(
this).sortable('cancel');
                    
self.handleStop(thiseui);
                }
            }
        };

        
this.events = {
                
complete: [],
                
update: [],
                clone: [],
                
remove: [],
                
loadSettings: [],
                
saveSettings: [],
                
saveScheme: [],
                
moveToPanel: [],
                
reload: [],
                
cloneComplete: [],
                        
move: []
        };
    },

    
initialize: function() {
        
this.initializePanel();
        
this.initializeSections();
        
this.initializeActions();
        
this.initializeClonablePnael();
        
this.initializeScheme();
    },

    
setHandler: function(handler) {

        
this.bind('update', function(sectionstate) {
            
handler.changeState(sectionstate);
        });

        
this.bind('complete', function(successFunction) {
            
handler.complete(successFunction);
        });

        
this.bind('clone', function(sectionstackidsuccess) {
            
handler.clone(sectionstackidsuccess);
        });

        
this.bind('remove', function(id) {
            
handler.remove(id);
        });

        
this.bind('loadSettings', function(idsuccessFunction) {
            
handler.loadSettings(idsuccessFunction);
        });

        
this.bind('saveSettings', function(idsettingssuccessFunction) {
            
handler.saveSettings(idsettingssuccessFunction);
        });

        
this.bind('saveScheme', function(scheme) {
            
handler.saveScheme(scheme);
        });

        
this.bind('moveToPanel', function(cmpId) {
            
handler.moveToPanel(cmpId);
        });

            
this.bind('reload', function(cmpIdrenderViewsuccessFunction)
            {
                
handler.reload(cmpIdrenderViewsuccessFunction);
            });
    },

    
initializePanel: function() {

        var 
self this;
        var 
panelSettings this.extendSettings({
            
stop: function(eui) {
                if ( !
self.transfered ) {
                    $(
this).sortable('cancel');
                    
self.handleStop(thiseui);
                    return;
                }
                
self.handleStop(thiseui);
                
self.complete();
                
self.actionComplete();
            },

            
over: function(eui) {
                
ui.placeholder.removeClass('peep_dnd_placeholder');
                
ui.placeholder.addClass('placeholder-hidden');
            }
        });

        
this.$panel.sortable(panelSettings).disableSelection();
    },

    
initializeSections: function() {
        var 
self this;
        var 
sectionSettings this.extendSettings({
            
cancel'.peep_dnd_freezed',
            
items'.component:not(.peep_dnd_freezed)',
            
receive: function(eui){
                if (
ui.placeholder.hasClass('peep_dnd_placeholder')) {
                    
self.transfered true;
                }
            },

            
over: function(eui){
                
ui.placeholder.removeClass('placeholder-hidden');
                
ui.placeholder.addClass('peep_dnd_placeholder');
            },

            
change: function(eui) {
               
self.handleSectionChange(thiseui);
            },

            
update: function(eui) {
                if (
ui.item.cloning && self.transfered) {
                    
ui.item.clone = self.clone(thisui.item);
                } else {
                    
self.update(this);
                }

            }

        });

        
this.$sections.sortable(sectionSettings).disableSelection();
    },


    
initializeClonablePnael: function() {
        var 
self this;
        var 
settings this.extendSettings({

            
stop: function(eui) {
                if (
self.transfered) {
                    
self.handleClone(thiseui);
                    
ui.item.cloning false;
                    
self.complete();
                }

                $(
this).sortable('cancel');
                
//self.handleStop(this, e, ui);
                
self.actionComplete();
            },

            
start: function(eui){
                 
self.handlStart(thiseui);
                 $(
ui.item).css('opacity''1');
                 
ui.item.cloning true;
            },

            
over: function(eui) {
                
ui.placeholder.removeClass('peep_dnd_placeholder');
                
ui.placeholder.addClass('placeholder-hidden');
            }

        });

        
this.$clonablePanel.sortable(settings).disableSelection();
    },


    
initializeActions: function() {
        var 
self this;

        var 
$allContainers this.$sections.add(this.$panel);

        
$allContainers.find('.component').hover(function(){
            $(
this).find('.action').show();
        }, function(){
            $(
this).find('.action').hide();
        });

        
this.$sections.find('.dd_delete').unbind().click(function() {
           var 
$component = $(this).parents('.component:eq(0)');
           
self.deleteFromSection($component);
        });

        
this.$panel.find('.dd_edit').unbind().click(function() {
           var 
$component = $(this).parents('.component:eq(0)');

           
self.showSettings($component);

           return 
false;
        });

        
this.$sections.find('.dd_edit').unbind().click(function() {
           var 
$component = $(this).parents('.component:eq(0)');

           
self.showSettings($component);

           return 
false;
        });

        
this.$panel.find('.dd_delete').unbind().click(function() {
           if ( !
confirmPEEP.getLanguageText('base''widgets_delete_component_confirm') ) ) {
               return 
false;
           }

           var 
$component = $(this).parents('.component:eq(0)');
           if (!
$component.hasClass('clone')) {
               return 
false;
           }
           
self.deleteFromPanel($component.attr('id'));
           
self.handleRemoveFromPanel($component);
        });
    },

    
initializeScheme: function() {
        var 
$sliderNode = $('.peep_dnd_slider');
        if (!
$sliderNode.length) {
            return;
        }
        var 
self this;
        var 
$left = $('.left_section''#place_sections');
        var 
$right = $('.right_section''#place_sections');

        
$sliderNode.schemeSwitcher({
            
change: function(event){

                
$left.removeClass( $(event.lastMarker).attr('dd_leftclass') )
                    .
addClass( $(event.marker).attr('dd_leftclass') );
                
$right.removeClass( $(event.lastMarker).attr('dd_rightclass') )
                .
addClass( $(event.marker).attr('dd_rightclass') );

            },

            
update: function(event){
                var 
schemeId = $(event.marker).attr('peep_scheme');
                
self.changeScheme(schemeId);
            }
        });
    },

    
changeScheme: function( schemeId ) {
        
this.trigger('saveScheme', [schemeId]);
    },

    
actionComplete: function() {
        
this.enableSections($('.place_section''#place_sections '));
        
this.initializeActions();
    },

    
/* Animation */

    
getHelper: function(sortableeui) {
        var 
itemWidth ui.outerWidth();

        if (
itemWidth 160)
        {
            var 
160 ui.outerWidth();
            var 
offsetWindow ui.offset();
            var 
offset * (e.pageX offsetWindow.left);

            $(
sortable).sortable'option''cursorAt', {leftoffset } );
        }

        return $(
'<div class="peep_dnd_helper" style="width: 160px; height: 30px"></div>');
    },

    
handlStart: function(sortableeui) {
        
this.transfered false;
        $(
ui.item).show().css('opacity''0.3');

        var 
within = $(ui.item).attr('peep_avaliable_sections');
        if (
typeof within == 'undefined') {
            return;
        }

        var 
withinItmes within.split(',');
        var 
lockSelectors = [];

        $.
each(withinItmes, function (iitem){
            var 
str '.' item '_section';
            
lockSelectors.push(str);
        });
        var 
selector lockSelectors.join(',');

        
this.disableSections($('.place_section:not(' selector ')''#place_sections '));
    },

    
handleStop: function(sortableeui)
    {
        var 
$item = $(ui.item);
        
this.enableSections($('.place_section''#place_sections '));
        
$item.show().css('opacity''1');

        var 
event = {};
        var 
toSection $item.parents(this.sectionSelector);
        var 
fromSection = $(sortable);

        
event.widgetName $item.attr('id');
        
event.widget $item;
        
event.to = {
            
sectiontoSection,
            
sectionNametoSection.attr('peep_place_section')
        };
        
event.from = {
            
sectionfromSection,
            
sectionNamefromSection.attr('peep_place_section')
        };
        
event.ui ui;
        
event.originalEvent e;

        
this.trigger('move', [event]);
    },

    
handleClone: function(sortableeui) {
        
ui.item.after(ui.item.clone);
        $(
ui.item.clone).show().css('opacity''1');
        $(
sortable).sortable('cancel');
        
this.handleStop(sortableeui);
    },

    
handleSectionChange: function(sortableeui)
    {
        var 
id = $(ui.item).attr('id');
        var 
$ph = $(ui.placeholder);

        if ( 
$ph.next().is('#' id) || $ph.prev().is('#' id)    ) {
                
$ph.removeClass('peep_dnd_placeholder');
                
$ph.addClass('hidden-placeholder');
        }
        else {
                
$ph.removeClass('hidden-placeholder');
                
$ph.addClass('peep_dnd_placeholder');
        }
    },

    
handleRemoveFromSection: function($itemcompleteFnc) {
        var 
self this;
        
$item.find('.action').hide();
        
$item.removeClass('peep_dnd_freezed');
        
$item.fadeOut('fast', function(){
            
$item.appendTo(self.$panel);
            
self.actionComplete();
            
$item.fadeIn('fast'completeFnc);
        });

    },

    
handleRemoveFromPanel: function($item) {
        
$item.fadeOut('fast');
    },

    
disableSections: function($sections) {
        $.
each($sections, function(iitem) {
            $(
item).addClass('peep_dnd_locked_section');
        });
    },

    
enableSections: function($sections) {
        $.
each($sections, function(iitem){
            $(
item).removeClass('peep_dnd_locked_section');
        });
    },

    
/* / Animation */

    
deleteFromSection: function($item) {
         var 
self this;
         var 
sortable $item.parents(this.sectionSelector).get(0);
         
this.handleRemoveFromSection($item, function(){
            
self.update(sortable);
            
self.trigger('moveToPanel', [$item.attr('id')]);
            
self.complete();
         });

    },

    clone: function(
sortable$cloningItem) {

        var 
self this;
        var 
sourceId $cloningItem.attr('id');
        var 
stack = $(sortable).sortable('toArray');
        var 
section = $(sortable).attr('peep_place_section');
        var 
$destItem $cloningItem.clone().removeAttr('id').addClass('clone');

        
self.trigger('clone', [sectionstacksourceId, function(id){
            
$destItem.attr('id'id);
            
self.trigger('cloneComplete', [id]);
        }]);
        return 
$destItem;
    },

    
deleteFromPanel: function(id) {
        
this.trigger('remove', [id]);
    },

    
update: function(sortable) {

        var 
$selfNode = $(sortable);
        var 
section $selfNode.attr('peep_place_section');
        var 
stack = [];
        
$selfNode.find('.component').each(function(){
            
stack.push(this.id);
        });

        
this.changeState(sectionstack);
    },

    
changeState: function(sectionNameitemStack) {
        
this.trigger('update', [
            
sectionName,
            
itemStack
        
]);
    },

    
complete: function(successFunction) {
            
this.trigger('complete', [successFunction]);
    },

    
showSettings: function($component)
    {
        var 
self this;
        var 
$title = $('.settings_title''#fb_settings');
        var 
$content = $('.settings_content''#fb_settings').addClass('peep_preloader_content');

        var 
$controls = $('.settings_controls''#fb_settings');
        
$controls.find('input.dd_save').unbind();

        
this.settingBox = new PEEP_FloatBox({
            
$title$title,
            
$contents$content,
            
$controls$controls,
            
width500
        
});

        
this.settingBox.bind('close', function(){
            
$content.empty();
        });
        var 
cmpId $component.attr('id');
        
this.trigger('loadSettings', [cmpId, function(settingMarkup){
                
$content.empty();
                
$content.removeClass('peep_preloader_content');
                var 
$settingMarkup = $('<form class="settings_form">' settingMarkup.content '</form>');
                
$content.html($settingMarkup);

                if (
settingMarkup.styleSheets) {
                    $.
each(settingMarkup.styleSheets, function(io){
                        
PEEP.addCssFile(o);
                    });
                }

                if (
settingMarkup.styleDeclarations) {
                    
PEEP.addCss(settingMarkup.styleDeclarations)
                }

                if (
settingMarkup.scriptFiles) {
                    
PEEP.addScriptFiles(settingMarkup.scriptFiles, function(){
                        if (
settingMarkup.onloadScript) {
                            
PEEP.addScript(settingMarkup.onloadScript);
                        }
                    });
                }
                else
                {
                    if (
settingMarkup.onloadScript) {
                        
PEEP.addScript(settingMarkup.onloadScript);
                    }
                }

                var 
$submitControl $controls.show().find('input.dd_save');
                var 
$form $content.find('.settings_form').submit(function(){
                    
self.saveSettings(cmpIdthis$submitControl.get(0));
                    return 
false;
                });

                
$submitControl.click(function(){
                    
$form.submit();
                });

        }]);
    },

    
saveSettings: function(cmpIdformsubmitControl) {
        var 
self this;
        var 
formState this.formToArray(form);
        var 
renderComponent this.isComponentRenderable(cmpId);
        
PEEP.inProgressNode(submitControl);
        $(
form).find('.setting_error').empty();
        var 
error = function(messagefieldName) {
            if (
fieldName) {
                $(
'#error_' fieldName ).html(message);
            } else {
                
PEEP.error(message);
            }
        };

        
this.trigger('saveSettings', [cmpIdformState, function( resultsharedData ) {
            if ( 
result.error ) {
                
error(result.error.messageresult.error.field);
                
sharedData.stop true;

                return;
            }
            
self.applyComponentSettings(cmpIdresult.settingList);
        }]);

        
this.trigger('reload', [cmpIdrenderComponent, function( markupsharedData ){
            if (
markup && !sharedData.stop) {
                
self.drawComponent(cmpIdmarkup);
            }
        }]);

        
this.complete(function(rsharedData) {
            
PEEP.activateNode(submitControl);
            
            if ( !
sharedData.stop ) {
                
self.settingBox.close();
            }
        });
    },

    
reloadWidget: function(cmpIdcallBackrender)
    {
        var 
self this;

        
render render === false false true;

        
this.trigger('reload', [cmpIdrender, function( markupsharedData )
        {
            var 
callBackResult true;

            if ($.
isFunction(callBack))
            {
                
callBackResult callBack.call(thismarkupsharedData);
            }

            if (
callBackResult !== false && markup)
            {
                
self.drawComponent(cmpIdmarkup);
            }
        }]);

        
this.trigger('complete');
    },

    
drawComponent: function(cmpIdmarkup) {
        var 
$component = $('#' cmpId);
        var 
$newComponent = $(markup.content);
        try {
            
$component.replaceWith($newComponent);
        } catch(
e) {}

        if (
markup.styleSheets) {
               $.
each(markup.styleSheets, function(io){
                   
PEEP.addCssFile(o);
            });
        }

        if (
markup.styleDeclarations) {
            
PEEP.addCss(markup.styleDeclarations)
        }

        if (
markup.scriptFiles) {
            
PEEP.addScriptFiles(markup.scriptFiles, function(){
                if (
markup.onloadScript) {
                        
PEEP.addScript(markup.onloadScript);
                }
            });
        }
        else
        {
            if (
markup.onloadScript) {
                
PEEP.addScript(markup.onloadScript);
            }
        }

        
this.actionComplete();
    },

    
applyComponentSettings: function(cmpIdsettings) {

        var 
$component = $('#' cmpId);
        var 
$section $component.parents('.place_section:eq(0)');

        if (
settings.freeze 0) {
            
$component.addClass('peep_dnd_freezed');
            
this.arrangeFreezed($section);
        }
    },

    
arrangeFreezed: function(section) {
        var 
$section = $(section);

        
/*var components = $section.find('.component').sort(function(a, b){
            var x = $(a).is('.peep_dnd_freezed') ? 0 : 1;
            var y = $(b).is('.peep_dnd_freezed') ? 0 : 1;

            return x - y;
        });*/
        
var iteration = function(){
            
$section.append(this);
        };
        var 
components = [];
        
$section.find('.peep_dnd_freezed').each(iteration);
        
$section.find('.component:not(.peep_dnd_freezed)').each(iteration);


        
/*$.each(components, function(){
            $section.append(this);
        });*/
    
},

    
isComponentInSection: function(id) {
        return $(
'#' id).is('#place_sections .component');
    },

    
isComponentRenderable: function(id) {
        return 
false;
    },

    
formToArray: function(form) {

            var 
nameRegex = /^([w-]+)([([^]]+)?])?/i;

        var 
state = {};
        $.
each(form.elements, function(iitem){
            if ( !$.
trim(item.name).length ) return;

            var 
$item = $(item);
                var 
beforeValue $item.attr('beforevalue');
                var 
nameParts nameRegex.exec(item.name);
                var 
name nameParts[1];
                var 
isMultiple = !!nameParts[2];
                var 
arrayKey = !!nameParts[3];
                var 
value;

                if (
$item.is('input:checkbox, input:radio')) {
                    
value $item.attr('checked') ? true false;
                } else {
                    
value $item.val();
                }

                if (  
beforeValue && beforeValue == value )
                {
                    return;
                }

                if ( 
isMultiple ) {
                    
state[name] = state[name] || (arrayKey ? {} : []);

                    if ( 
arrayKey ) {
                        
state[name][arrayKey] = value;
                    }
                    else if (
value) {
                        
state[name].push($item.attr('value') || true);
                    }
                }
                else {
                    
state[name] = value;
                }

        });

        return 
state;
    },

    
bind: function(typefunc) {
        if (
this.events[type] == undefined) {
            throw 
'undefined form event type "'+type+'"';
        }

        
this.events[type].push(func);
    },

    
unbind: function(type) {
        if (
this.events[type] == undefined) {
            throw 
'undefined form event type "'+type+'"';
        }

        
this.events[type] = [];
    },

    
trigger: function(typeparams) {

        if (
this.events[type] == undefined) {
            throw 
'undefined form event type "'+type+'"';
        }

        
params params || [];

        for (var 
0funcfunc this.events[type][i]; i++) {
            if (
func.apply(thisparams) === false) {
                return 
false;
            }
        }

        return 
true;
    },

    
extendSettings: function(obj) {
        $.
each(this.defaultSettings, function(propvalue) {
            if (
obj[prop] === undefined) {
                
obj[prop] = value;
            }
        });
        return 
obj;
    }

}
?>
Онлайн: 1
Реклама