Advertisement
Adam_Martin

Untitled

Feb 13th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var widgetControls = [];
  2. var WidgetController, Widget, Brick;
  3.  
  4. WidgetController = (function() {
  5.  
  6.     var self;
  7.  
  8.     /**
  9.      * Конструктор
  10.      */
  11.     function WidgetController()
  12.     {
  13.         self = this;
  14.         this.templates = [];
  15.         this.scripts   = [];
  16.     } // Constructor
  17.  
  18.  
  19.     /**
  20.      * loadXMLContent
  21.      * загружает шаблон виджетов из xml-файла
  22.      */
  23.     WidgetController.prototype.loadXMLContent = function( file_name /* String */, fn  /* Callback */)
  24.     {
  25.         $.get
  26.         (
  27.             file_name ,
  28.             function( data )  // data = xml content
  29.             {
  30.                 self.xml_content = data;
  31.                 self.loadTemplate();
  32.                 fn();
  33.             } // function( data )
  34.         ) // $.get
  35.     } // prototype.loadXMLContent
  36.  
  37.  
  38.     /**
  39.      * addTemplate
  40.      * Оборачивает тело шаблона в <div> и
  41.      * добавляет в глобальный массив templates
  42.      *
  43.      * <- name - имя виджета
  44.      * <- temp - xml-узел шаблона виджета
  45.      */
  46.     WidgetController.prototype.addTemplate = function ( name /* String */, temp  /* XMLElement */ )
  47.     {
  48.         var widget_body, temp_text;
  49.  
  50.         temp_text   = $( temp ).text();
  51.         widget_body = $( '<div>' )
  52.                         .attr( 'class' , name )
  53.                         .append( temp_text );
  54.  
  55.         self.templates[ name ] = widget_body /* HTMLElement */;
  56.  
  57.     } // prototype.addTemplate
  58.  
  59.  
  60.     /**
  61.      * getTemplate
  62.      * Возвращаем шаблан виджета по его имени
  63.      *
  64.      * <- name - имя виджета
  65.      * -> HTMLDivElement
  66.      */
  67.     WidgetController.prototype.getTemplate = function ( name /* String */ )
  68.     {
  69.         return this.templates[ name ]; /* HTMLDivElement */
  70.     }; // prototype.getTemplate
  71.  
  72.  
  73.     /**
  74.      * getScript
  75.      * Возвращаем скрипт виджета по его имени
  76.      *
  77.      * <- name - имя виджета
  78.      * -> WidgetScript
  79.      */
  80.     WidgetController.prototype.getScript = function ( name /* String */ )
  81.     {
  82.         return  this.scripts[name]; /* WidgetScript */
  83.     }; // prototype.getScript
  84.  
  85.  
  86.     /**
  87.      * loadTemplate
  88.      * Загружает все:
  89.      * - шаблоны виджетов в массив templates[]
  90.      * - скрипты виджетов в массив scripts[]
  91.      */
  92.     WidgetController.prototype.loadTemplate = function ()
  93.     {
  94.         var self = this; /* global context */
  95.  
  96.         $( self.xml_content ).find( 'widget' ).each
  97.         (
  98.             function ( index, value )
  99.             {
  100.                 var name;
  101.  
  102.                 name = $( value ).attr( 'name' );
  103.                 self.addTemplate( name, value );
  104.                 self.scripts[ name ] = widgetControls [ name ];
  105.  
  106.             } // .each function
  107.         ) // .each
  108.  
  109.         /**
  110.          * Обработка вложенных виджетов
  111.          */
  112.         for ( var widgetName in self.templates )
  113.         {
  114.             var script, template;
  115.             console.log( 'neo: ' + widgetName );
  116.  
  117.             script   = widgetControls[ widgetName ];
  118.             template = self.getTemplate( widgetName );
  119.  
  120.             $( template ).find( 'widget' ).each
  121.             (
  122.                 function ( index, item )
  123.                 {
  124.                     var include_name, include_template, include_script;
  125.  
  126.                     include_name     = $( item ).attr( 'include' );
  127.                     include_template = self.getTemplate( include_name );
  128.                     $( item ).replaceWith( $( include_template ).clone() );
  129.  
  130.                     include_script = widgetControls[ include_name ];
  131.                     $.extend( true, script, include_script );
  132.  
  133.                     console.log( script );
  134.  
  135.                 } // function()
  136.             ) // .each
  137.         } // foreach templates
  138.     }; // prototype.loadTemplate
  139.  
  140.  
  141.     /**
  142.      * buildWidget
  143.      * Создает и возвращает экземпляр виджета заданного именем name
  144.      *
  145.      * <- name - имя виджета
  146.      * -> Widget
  147.      */
  148.     WidgetController.prototype.buildWidget = function ( name /* String */ )
  149.     {
  150.         var obj = new Widget( name, self );
  151.  
  152.         return obj /* Widget */
  153.  
  154.     } // prototype.buildWidget
  155.  
  156.     return WidgetController;
  157.  
  158. })(); // class WidgetController
  159.  
  160.  
  161. Widget = (function()
  162. {
  163.     var  self;
  164.  
  165.     /**
  166.      * Конструктор
  167.      */
  168.     function Widget( name /* String */, wc /* WidgetController */ )
  169.     {
  170.         self = this;
  171.  
  172.         this.name      = name;
  173.         this.template  = wc.getTemplate( name );
  174.         this.logic     = wc.getScript( name );
  175.         this.etalon    = [];
  176.         this.brick     = [];
  177.  
  178.         for (var brick_name in this.logic)
  179.         {
  180.             this.etalon[ brick_name ] = this.template.find('.' + brick_name).clone();
  181.         } // foreach logic
  182.  
  183.         for (var brick_name in this.logic)
  184.         {
  185.             var brick;
  186.  
  187.             brick = new Brick( brick_name, self );
  188.  
  189.             this.template.find( '.' + brick_name ).replaceWith( brick.template );
  190.             this.brick[ brick_name ] = brick;
  191.         } // foreach logic
  192.  
  193.     } // Constructor
  194.  
  195.     return Widget;
  196.  
  197. })(); // class Widget
  198.  
  199. Brick = (function()
  200. {
  201.     var  self;
  202.  
  203.     /**
  204.      * Конструктор
  205.      */
  206.     function Brick( name /* String */, widget /* Widget */ )
  207.     {
  208.         self = this;
  209.  
  210.         this.name      = name;
  211.         this.widget    = widget;
  212.         this.template  = $( widget.etalon[ name ] ).clone();
  213.         this.logic     = widget.logic[ name ];
  214.  
  215.         this.bindEvents();
  216.         this.bindActions();
  217.  
  218.        // console.log( widget );
  219.  
  220.  
  221.     } // Constructor
  222.  
  223.  
  224.     /**
  225.      * getInstance
  226.      * Создает и возвращает экземпляр кирпича
  227.      *
  228.      * -> Brick
  229.      */
  230.     Brick.prototype.getInstance = function ()
  231.     {
  232.         var obj = new Brick( self.name, self.widget );
  233.  
  234.         obj.bindEvents();
  235.         obj.bindActions();
  236.  
  237.  
  238.         return obj; /* Widget */
  239.  
  240.     } // prototype.buildWidget
  241.  
  242.  
  243.     /**
  244.      * bindEvents
  245.      * Устанавливаем свой обработчик внешних событий logic.events
  246.      * делаем замыкание на свой обработчик
  247.      */
  248.     Brick.prototype.bindEvents = function ()
  249.     {
  250.         var bind_list = {};
  251.         var events = self.logic.events;
  252.  
  253.         for ( var event in events )
  254.         {
  255.           ( function( __obj, event )
  256.             {
  257.                 bind_list[ event ] = function()
  258.                 {
  259.                     __obj[ event ]()
  260.                 }; // function
  261.             }(  self, event ) ); // function
  262.  
  263.             self[ event ] = events[ event ];
  264.  
  265.         } // foreach events
  266.         self.template.bind( bind_list );
  267.  
  268.         bind_list = null
  269.     } // prototype.bindEvents
  270.  
  271.  
  272.     /**
  273.      * bindActions
  274.      * Устанавливаем дополнительные методы logic.actions
  275.      * Обарбатываем предустановленные функции
  276.      */
  277.     Brick.prototype.bindActions = function ()
  278.     {
  279.         var actions = self.logic.actions;
  280.  
  281.         for ( var action in actions )
  282.         {
  283.             self[ action ] = actions[ action ];
  284.         } // foreach actions
  285.  
  286.         if ( typeof self.logic.actions.__before == 'function' )
  287.         {
  288.             //self.__before( DOMElement.element );
  289.         } // if
  290.     } // prototype.bindActions
  291.  
  292.  
  293.     /**
  294.      * bricks
  295.      * Получаем объект из коллекции по имени
  296.      * <- name - имя элемента
  297.      * -> Brick
  298.      */
  299.     Brick.prototype.bricks = function ( name /* String */ )
  300.     {
  301.         return self.widget.brick[ name ];
  302.     } // prototype.bricks
  303.  
  304.     return Brick; // Instance
  305.  
  306. })(); // class Brick
  307.  
  308. $(function ()
  309. {
  310.     var wc = new WidgetController();
  311.  
  312.     wc.loadXMLContent
  313.     (
  314.         '/widgets/layouts.xml',
  315.         function ()
  316.         {
  317.             var widget;
  318.             widget = wc.buildWidget( 'source_selector' );
  319.             $( '.' + 'source_selector' ).each
  320.             (
  321.                 function ( index, value )
  322.                 {
  323.                     $( value ).replaceWith( widget.template );
  324.                 } // each function
  325.             ) // each
  326.         } // loadXMLContent callback
  327.     )
  328.  
  329. }) // windows.onload
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement