Advertisement
Adam_Martin

Untitled

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