Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var widgetControls = [];
- var WidgetController, Widget, Brick;
- WidgetController = (function() {
- var self;
- /**
- * Конструктор
- */
- function WidgetController()
- {
- self = this;
- this.templates = [];
- this.scripts = [];
- } // Constructor
- /**
- * loadXMLContent
- * загружает шаблон виджетов из xml-файла
- */
- WidgetController.prototype.loadXMLContent = function( file_name /* String */, fn /* Callback */)
- {
- $.get
- (
- file_name ,
- function( data ) // data = xml content
- {
- self.xml_content = data;
- self.loadTemplate();
- fn();
- } // function( data )
- ) // $.get
- } // prototype.loadXMLContent
- /**
- * addTemplate
- * Оборачивает тело шаблона в <div> и
- * добавляет в глобальный массив templates
- *
- * <- name - имя виджета
- * <- temp - xml-узел шаблона виджета
- */
- WidgetController.prototype.addTemplate = function ( name /* String */, temp /* XMLElement */ )
- {
- var widget_body, temp_text;
- temp_text = $( temp ).text();
- widget_body = $( '<div>' )
- .attr( 'class' , name )
- .append( temp_text );
- self.templates[ name ] = widget_body /* HTMLElement */;
- } // prototype.addTemplate
- /**
- * getTemplate
- * Возвращаем шаблан виджета по его имени
- *
- * <- name - имя виджета
- * -> HTMLDivElement
- */
- WidgetController.prototype.getTemplate = function ( name /* String */ )
- {
- return this.templates[ name ]; /* HTMLDivElement */
- }; // prototype.getTemplate
- /**
- * getScript
- * Возвращаем скрипт виджета по его имени
- *
- * <- name - имя виджета
- * -> WidgetScript
- */
- WidgetController.prototype.getScript = function ( name /* String */ )
- {
- return this.scripts[name]; /* WidgetScript */
- }; // prototype.getScript
- /**
- * loadTemplate
- * Загружает все:
- * - шаблоны виджетов в массив templates[]
- * - скрипты виджетов в массив scripts[]
- */
- WidgetController.prototype.loadTemplate = function ()
- {
- var self = this; /* global context */
- $( self.xml_content ).find( 'widget' ).each
- (
- function ( index, value )
- {
- var name;
- name = $( value ).attr( 'name' );
- self.addTemplate( name, value );
- self.scripts[ name ] = widgetControls [ name ];
- } // .each function
- ) // .each
- /**
- * Обработка вложенных виджетов
- */
- for ( var widgetName in self.templates )
- {
- var script, template;
- console.log( 'neo: ' + widgetName );
- script = widgetControls[ widgetName ];
- template = self.getTemplate( widgetName );
- $( template ).find( 'widget' ).each
- (
- function ( index, item )
- {
- var include_name, include_template, include_script;
- include_name = $( item ).attr( 'include' );
- include_template = self.getTemplate( include_name );
- $( item ).replaceWith( $( include_template ).clone() );
- include_script = widgetControls[ include_name ];
- $.extend( true, script, include_script );
- console.log( script );
- } // function()
- ) // .each
- } // foreach templates
- }; // prototype.loadTemplate
- /**
- * buildWidget
- * Создает и возвращает экземпляр виджета заданного именем name
- *
- * <- name - имя виджета
- * -> Widget
- */
- WidgetController.prototype.buildWidget = function ( name /* String */ )
- {
- var obj = new Widget( name, self );
- return obj /* Widget */
- } // prototype.buildWidget
- return WidgetController;
- })(); // class WidgetController
- Widget = (function()
- {
- var self;
- /**
- * Конструктор
- */
- function Widget( name /* String */, wc /* WidgetController */ )
- {
- self = this;
- this.name = name;
- this.template = wc.getTemplate( name );
- this.logic = wc.getScript( name );
- this.etalon = [];
- this.brick = [];
- for (var brick_name in this.logic)
- {
- this.etalon[ brick_name ] = this.template.find('.' + brick_name).clone();
- } // foreach logic
- for (var brick_name in this.logic)
- {
- var brick;
- brick = new Brick( brick_name, self );
- this.template.find( '.' + brick_name ).replaceWith( brick.template );
- this.brick[ brick_name ] = brick;
- } // foreach logic
- } // Constructor
- return Widget;
- })(); // class Widget
- Brick = (function()
- {
- var self;
- /**
- * Конструктор
- */
- function Brick( name /* String */, widget /* Widget */ )
- {
- self = this;
- this.name = name;
- this.widget = widget;
- this.template = $( widget.etalon[ name ] ).clone();
- this.logic = widget.logic[ name ];
- this.bindEvents();
- this.bindActions();
- // console.log( widget );
- } // Constructor
- /**
- * getInstance
- * Создает и возвращает экземпляр кирпича
- *
- * -> Brick
- */
- Brick.prototype.getInstance = function ()
- {
- var obj = new Brick( self.name, self.widget );
- obj.bindEvents();
- obj.bindActions();
- return obj; /* Widget */
- } // prototype.buildWidget
- /**
- * bindEvents
- * Устанавливаем свой обработчик внешних событий logic.events
- * делаем замыкание на свой обработчик
- */
- Brick.prototype.bindEvents = function ()
- {
- var bind_list = {};
- var events = self.logic.events;
- for ( var event in events )
- {
- ( function( __obj, event )
- {
- bind_list[ event ] = function()
- {
- __obj[ event ]()
- }; // function
- }( self, event ) ); // function
- self[ event ] = events[ event ];
- } // foreach events
- self.template.bind( bind_list );
- bind_list = null
- } // prototype.bindEvents
- /**
- * bindActions
- * Устанавливаем дополнительные методы logic.actions
- * Обарбатываем предустановленные функции
- */
- Brick.prototype.bindActions = function ()
- {
- var actions = self.logic.actions;
- for ( var action in actions )
- {
- self[ action ] = actions[ action ];
- } // foreach actions
- if ( typeof self.logic.actions.__before == 'function' )
- {
- //self.__before( DOMElement.element );
- } // if
- } // prototype.bindActions
- /**
- * bricks
- * Получаем объект из коллекции по имени
- * <- name - имя элемента
- * -> Brick
- */
- Brick.prototype.bricks = function ( name /* String */ )
- {
- return self.widget.brick[ name ];
- } // prototype.bricks
- return Brick; // Instance
- })(); // class Brick
- $(function ()
- {
- var wc = new WidgetController();
- wc.loadXMLContent
- (
- '/widgets/layouts.xml',
- function ()
- {
- var widget;
- widget = wc.buildWidget( 'source_selector' );
- $( '.' + 'source_selector' ).each
- (
- function ( index, value )
- {
- $( value ).replaceWith( widget.template );
- } // each function
- ) // each
- } // loadXMLContent callback
- )
- }) // windows.onload
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement