Advertisement
Guest User

Untitled

a guest
Nov 6th, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(namespace) {
  2.     'use strict';
  3. })(window.$dhx = window.$dhx || {});
  4.  
  5. (function(namespace) {
  6.     'use strict';
  7. })($dhx.ui = $dhx.ui || {});
  8.  
  9. (function(namespace) {
  10.     'use strict';
  11.     var root,
  12.         _application,
  13.         _router,
  14.         _registered_events = [],
  15.         _options = {},
  16.         /**
  17.          * [application private application bootstrap constructor]
  18.          * @param  {[Object]} stash [xxxxxxxxxxx]
  19.          * @return {[Object]}       [returns an router object]
  20.          */
  21.         application = function(stash) {
  22.             var appId;
  23.             if (typeof stash.appId === 'undefined') {
  24.                 stash.appId = "" + Math.random() + "";
  25.             }
  26.  
  27.             this.appId = stash.appId;
  28.             root = stash.root;
  29.             //this.options = {};
  30.             _options.from = 'super';
  31.  
  32.             var isAllEventsReturninOk = namespace.triggerMethod('before:start', _options);
  33.             if( ! isAllEventsReturninOk )
  34.             {
  35.                 throw ' application will not initialize due a onBeforeStart event returning false';
  36.             }
  37.  
  38.             this.initialize(_options);
  39.  
  40.             _application = this;
  41.         },
  42.         /**
  43.          * [router private router constructor]
  44.          * @param  {[Object]} stash [xxxxxxxxxxx]
  45.          * @return {[Object]}       [returns an router object]
  46.          */
  47.         router = function(stash) {
  48.             var self = this;
  49.  
  50.             // lets listen for browser navigate actions
  51.             window.addEventListener("popstate", function(e) {
  52.                 //console.log(e);
  53.                 //console.log(location.hash.replace(/#/gi, ''));
  54.  
  55.                 self.dispatch(e.state.url, false);
  56.             });
  57.  
  58.  
  59.             _router = this;
  60.  
  61.  
  62.         },
  63.         /**
  64.          * [active_routes list of routes that are being active. Active routes are displayed on browser's URL bar]
  65.          * @type {Array}
  66.          */
  67.         active_routes = [];
  68.  
  69.     /**
  70.      * [application.prototype MVP aplication bootstrap constructor class prototype chain]
  71.      * @type {Object}
  72.      */
  73.     application.prototype = {
  74.         initialize: function(options) {
  75.             console.log('method from application.prototype');
  76.             //console.log('app initialized from ' + options.from);
  77.             //namespace.triggerMethod('start', options);
  78.         },
  79.         start : function(){
  80.             var hash = window.location.hash;
  81.             /**
  82.              * [if hash equal empty it means application is starting]
  83.              * @param  {[string]} hash [reference to window.location.hash]
  84.              */
  85.             if (hash === '') {
  86.                 /**
  87.                  * then dispatch the root route and call the associated Presenter method ( presenter.start() )
  88.                  */
  89.                 _router.dispatch('#', true);
  90.  
  91.                 console.log('start');
  92.  
  93.                 namespace.triggerMethod('start', _options);
  94.             }
  95.         }
  96.     };
  97.  
  98.  
  99.     /**
  100.      * [router.prototype MVP router constructor class prototype chain]
  101.      * @type {Object}
  102.      */
  103.     router.prototype = {
  104.         dispatch: function(url, addEntry) {
  105.  
  106.             var method_name = this.appRoutes[url] || this.routes[url];
  107.             if (typeof method_name === 'undefined') {
  108.                 throw 'can not dispatch to a not declared route. URL: ' + url;
  109.             }
  110.  
  111.             console.log('dispatching ' + url);
  112.  
  113.  
  114.             if (addEntry === true) {
  115.                 // Add History Entry using pushState
  116.                 if (!active_routes.contains(url)) {
  117.                     var data = {
  118.                             url: url
  119.                         },
  120.                         hash = window.location.hash,
  121.                         title = url;
  122.                     history.pushState(data, title, (url == '#' ? url : (hash === '' ? '#' : hash) + url));
  123.                     active_routes.push(url);
  124.                     //window.location.replace("http://www.w3schools.com");
  125.                 }
  126.             }
  127.  
  128.             if (this.presenter[method_name]) {
  129.                 this.presenter[method_name]();
  130.             } else if (_router[method_name]) {
  131.                 _router[method_name]();
  132.             }
  133.         },
  134.  
  135.         route: function(stash) {
  136.             console.log( 'route' );
  137.         }
  138.     };
  139.  
  140.     /**
  141.      * [$dhx.ui.mvp.router Public access to MVP router features]
  142.      * @type {Object}
  143.      */
  144.     namespace.router = {
  145.         /**
  146.          * [extend generate a new router constructor by inheriting the mvp router and append the methods from factory]
  147.          * @param  {[Object]} factory [a collection of public properties and methods]
  148.          * @return {[constructor]}         [MVP router constructor]
  149.          */
  150.         extend: function(factory) {
  151.             var start = function() {
  152.                 console.log('start from extend factory');
  153.             };
  154.             return namespace.extend({
  155.                 base : router,
  156.                 factory : factory,
  157.                 onBeforeExtend : function ( factory ){
  158.                     /**
  159.                      * [factory a collection of public properties and methods]
  160.                      * @type {[Object}
  161.                      */
  162.                     factory = factory || {};
  163.                     // set a presenter class if it was not set when extending the router
  164.                     factory.presenter = factory.presenter || {
  165.                         start: start
  166.                     };
  167.                     // set a start method for the presenter if it was not set when extending the router
  168.                     factory.presenter.start = factory.presenter.start || start;
  169.  
  170.                     // set a empty collection of application routes if it was not set when extending the router
  171.                     factory.appRoutes = factory.appRoutes || {};
  172.                    
  173.                     //set a empty collection of routes created on the fly if it was not set when extending the router
  174.                     factory.routes = factory.routes || {};
  175.  
  176.                     // map root route that will call the presenter.start();
  177.                     if (!factory.appRoutes.hasOwnProperty('#')) {
  178.                         factory.appRoutes['#'] = 'start';
  179.                     }
  180.                 }
  181.             });
  182.         }
  183.     };
  184.  
  185.     /**
  186.      * [$dhx.ui.mvp.application public access to MVP application bootstrap]
  187.      * @type {Object}
  188.      */
  189.     namespace.application = {
  190.         /**
  191.          * [extend generate a new application bootrap constructor by inheriting the mvp application schema and append the methods from factory]
  192.          * @param  {[Object]} factory [a collection of public properties and methods]
  193.          * @return {[constructor]}         [MVP router constructor]
  194.          */
  195.         extend: function(factory) {
  196.             return namespace.extend({
  197.                 base : application,
  198.                 factory : factory,
  199.                 onBeforeExtend : function (){
  200.  
  201.                 }
  202.             });
  203.         }  
  204.     };
  205.  
  206.     /**
  207.      * [$dhx.ui.mvp.extend generate a new constructor by inheriting a base class and appending the methods from a factory]
  208.      * @param  {[Object]} c [A JSON object containing the following properties and methods]
  209.      * @param  {[Constructor]}      c.base [a constructor function to be used as base class ]
  210.      * @param  {[Object]}           c.factory [a collection of public properties and methods to be appended to the new generated constructor]
  211.      * @return {[Constructor]} constructor [returns a new object constructor that inherits the base class and the factory]
  212.      */
  213.     namespace.extend = function( c ) {
  214.         var base = c.base,
  215.             factory = c.factory,
  216.             sub = null;
  217.  
  218.         if( c.onBeforeExtend )
  219.         {
  220.             c.onBeforeExtend(factory);
  221.         }
  222.  
  223.         sub = function(stash) {
  224.             stash = stash || {};
  225.             base.call(this, stash);
  226.         };
  227.  
  228.         sub.prototype = Object.create(base.prototype);
  229.         sub.prototype.constructor = sub;
  230.  
  231.         for (var name in factory) {
  232.             if (factory.hasOwnProperty(name)) {
  233.                 sub.prototype[name] = factory[name];
  234.             }
  235.         }
  236.  
  237.         sub.on = function(pattern, fn) {
  238.             _registered_events.push({
  239.                 pattern : pattern,
  240.                 fn : fn,
  241.                 base : base
  242.             });
  243.         };
  244.  
  245.         sub.start = function(options) {
  246.             _options = options;
  247.         };
  248.  
  249.         return sub;
  250.     };
  251.  
  252.     namespace.triggerMethod = function(){
  253.             var event = arguments[0],
  254.             parameter = arguments[1] || false,
  255.             tests = [];
  256.  
  257.         _registered_events.forEach( function( evtObject ){
  258.            
  259.             if( evtObject.pattern === event )
  260.             {
  261.                 if( parameter )
  262.                 {
  263.                     tests.push(evtObject.fn(parameter));
  264.                 }
  265.                 else
  266.                 {
  267.                     tests.push(evtObject.fn());
  268.                 }
  269.             }
  270.         } );
  271.  
  272.         return tests.join('.').indexOf('false') > -1 ? false : true ;
  273.     };
  274.  
  275. })($dhx.ui.mvp = $dhx.ui.mvp || {});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement