Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var WidgetController, Widget, Brick;
- var widgetControls = [];
- 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.bricks = [];
- for (var brick_name in this.logic)
- {
- var brick;
- brick = new Brick( brick_name, self);
- this.bricks[ brick_name ] = brick;
- //console.log( brick );
- }
- } // 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.template.find( '.' + name ) ).clone();
- this.template = widget.template.find( '.' + name );
- this.logic = widget.logic[ name ];
- this.bindEvents();
- this.bindActions();
- } // Constructor
- 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 ]()
- };
- }( self, event ) );
- self[ event ] = events[ event ];
- }
- self.template.bind( bind_list );
- bind_list = null
- }
- Brick.prototype.bindActions = function ()
- {
- var actions = self.logic.actions;
- for ( var action in actions )
- {
- self[ action ] = actions[ action ];
- }
- if ( typeof self.logic.actions.__before == 'function' )
- {
- //self.__before( DOMElement.element );
- }
- }
- Brick.prototype.bricks = function ( name /* String */ )
- {
- return self.widget.bricks[ name ];
- }
- return Brick;
- })(); // class Brick
- $(function ()
- {
- var wc = new WidgetController();
- wc.loadXMLContent
- (
- '/widgets/layouts.xml',
- function ()
- {
- var widget;
- widget = wc.buildWidget( 'input_control' );
- $( '.' + 'source_selector' ).each
- (
- function ( index, value )
- {
- $( value ).replaceWith( widget.template );
- // console.log( value );
- }
- )
- //$( value ).replaceWith( DOMElement.widgetTemplate );
- //console.log( widget );
- }
- )
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement