Skip to content
Code-Schnipsel Gruppen Projekte
Category.js 4,46 KiB
Newer Older

/**
 * Area manager for the site object
 *
Henning Leutz's avatar
Henning Leutz committed
 * @module package/quiqqer/bricks/bin/Site/Category
 * @author www.pcsg.de (Henning Leutz)
 *
 * @event onLoaded
 */

Henning Leutz's avatar
Henning Leutz committed
define('package/quiqqer/bricks/bin/Site/Category', [

    'qui/QUI',
    'qui/controls/Control',
    'qui/controls/loader/Loader',
    'Ajax',
    'Locale',
Henning Leutz's avatar
Henning Leutz committed
    'package/quiqqer/bricks/bin/Site/Area',
Henning Leutz's avatar
Henning Leutz committed
    'css!package/quiqqer/bricks/bin/Site/Category.css'

], function (QUI, QUIControl, QUILoader, QUIAjax, QUILocale, Area)
{
    "use strict";

    return new Class({

        Extends : QUIControl,
Henning Leutz's avatar
Henning Leutz committed
        Type    : 'package/quiqqer/bricks/bin/Site/Category',
        initialize: function(options)
Henning Leutz's avatar
Henning Leutz committed
            this.parent( options );

            this.Loader = new QUILoader();
            this.areas  = [];

            this.addEvents({
                onInject  : this.$onInject,
                onDestroy : this.$onDestroy
            });
        },

        /**
         * Return the domnode element
         * @return {HTMLElement}
         */
        create: function ()
        {
            this.$Elm = new Element('div', {
Henning Leutz's avatar
Henning Leutz committed
                'class' : 'quiqqer-bricks-site-category'
            });

            this.Loader.inject( this.$Elm );


            return this.$Elm;
        },

        /**
         * event : on inject
         */
        $onInject : function()
        {
            var self = this;

            this.Loader.show();

Henning Leutz's avatar
Henning Leutz committed
            this.getBrickAreas(function(bricks)
                if ( !bricks.length )
                {
                    self.$Elm.set(
                        'html',
                        QUILocale.get( 'quiqqer/bricks', 'bricks.message.no.areas.found' )
                    );

                    return;
                }

Henning Leutz's avatar
Henning Leutz committed
                var Site  = self.getAttribute( 'Site' ),
Henning Leutz's avatar
Henning Leutz committed
                    areas = Site.getAttribute( 'quiqqer.bricks.areas' );
Henning Leutz's avatar
Henning Leutz committed
                    areas = JSON.decode( areas );
Henning Leutz's avatar
Henning Leutz committed
                for ( i = 0, len = bricks.length; i < len; i++ )
Henning Leutz's avatar
Henning Leutz committed
                    AC = self.$insertBrickAreaEdit( bricks[ i ] );

                    if ( typeof areas[ AC.getAttribute('name') ] === 'undefined' ) {
                        continue;
                    }

                    data = areas[ AC.getAttribute('name') ];

Henning Leutz's avatar
Henning Leutz committed
                    data.each(function(brickData) {
                        AC.addBrick( brickData );
                    });
                }

                self.Loader.hide();
                self.fireEvent( 'loaded' );
            });
        },

        /**
         * event : on destroy
         */
        $onDestroy : function()
        {
            this.updateSite();
        },

        /**
         * Update the internal site object
         */
        updateSite : function()
Henning Leutz's avatar
Henning Leutz committed
            var Site  = this.getAttribute( 'Site' ),
                areas = {};

            for ( i = 0, len = this.areas.length; i < len; i++ )
            {
                AC = this.areas[ i ];

                areas[ AC.getAttribute( 'name' ) ] = AC.getData();
            }
Henning Leutz's avatar
Henning Leutz committed
            Site.setAttribute( 'quiqqer.bricks.areas', JSON.encode( areas ) );
Henning Leutz's avatar
Henning Leutz committed
         * Return the available areas for the site
         * @param {Function} callback - callback function
         */
Henning Leutz's avatar
Henning Leutz committed
        getBrickAreas : function(callback)
Henning Leutz's avatar
Henning Leutz committed
            var Site    = this.getAttribute( 'Site' ),
Henning Leutz's avatar
Henning Leutz committed
            QUIAjax.get('package_quiqqer_bricks_ajax_project_getAreas', callback, {
                'package' : 'quiqqer/bricks',
                project   : Project.encode(),
Henning Leutz's avatar
Henning Leutz committed
                layout    : Site.getAttribute( 'layout' )
Henning Leutz's avatar
Henning Leutz committed
         * Create a brick area edit container
         *
         * @param {Object} area - Data of the area
         * @return Area
         */
Henning Leutz's avatar
Henning Leutz committed
        $insertBrickAreaEdit : function(area)
Henning Leutz's avatar
Henning Leutz committed
            var Site    = this.getAttribute( 'Site' ),
                Project = Site.getProject(),
                Control = new Area();

            Control.setAttribute( 'Project', Project );
            Control.setAttribute( 'Site', Site );
            Control.setAttributes( area );

            Control.inject( this.$Elm );

            this.areas.push( Control );

            return Control;
        }
    });
Henning Leutz's avatar
Henning Leutz committed
});