~makyo/juju-gui/trunk

« back to all changes in this revision

Viewing changes to app/assets/javascripts/app-subapp-extension.js

Created app subapp extension

R=bcsaller, gary.poster
CC=
https://codereview.appspot.com/7381055

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'use strict';
 
2
 
 
3
YUI.add('app-subapp-extension', function(Y) {
 
4
 
 
5
  /**
 
6
  Adds the ability to register sub applications with a parent Y.App instance by
 
7
  recording all instances in a subApps attribute and augmenting the parent
 
8
  routes with the sub applications.
 
9
 
 
10
  @class SubAppRegistration
 
11
  @extension App
 
12
  */
 
13
  function SubAppRegistration() {}
 
14
 
 
15
  SubAppRegistration.ATTRS = {
 
16
    subApps: {
 
17
      value: {}
 
18
    }
 
19
  };
 
20
 
 
21
  SubAppRegistration.prototype = {
 
22
 
 
23
    /**
 
24
      A list of sub applications to be instantiated after initialization
 
25
      of the parent app.
 
26
 
 
27
      [{
 
28
        type: path.to.subapp
 
29
        config: {}
 
30
      }]
 
31
 
 
32
      @property subApplications
 
33
      @type {array}
 
34
    */
 
35
    subApplications: [],
 
36
 
 
37
    /**
 
38
      Adds all of the sub applications listed in the subApplications property.
 
39
 
 
40
      @method addSubApplications
 
41
    */
 
42
    addSubApplications: function() {
 
43
      this.addSubApps(this.subApplications);
 
44
    },
 
45
 
 
46
    /**
 
47
      Adds the sub application and its routes to the parent application.
 
48
 
 
49
      @method addSubApp
 
50
      @param {object} SubApp instantiable Y.SubApp object.
 
51
      @param {object} config configuration properties for the subapp.
 
52
    */
 
53
    addSubApp: function(SubApp, config) {
 
54
      var subApps = this.get('subApps'),
 
55
          routes;
 
56
 
 
57
      SubApp = new SubApp(config);
 
58
      subApps[SubApp.get('urlNamespace')] = SubApp;
 
59
 
 
60
      routes = this._extractRoutes(SubApp);
 
61
      this._augmentRoutes(routes);
 
62
    },
 
63
 
 
64
    /**
 
65
      Wrapper for addSubApp to add multiple sub apps at once.
 
66
 
 
67
      [{
 
68
        type: 'path.to.subapplication'
 
69
        config: {}
 
70
      }]
 
71
 
 
72
      @method addSubApps
 
73
      @param {array} subApps an array of sub app objects and configs.
 
74
    */
 
75
    addSubApps: function(subApps) {
 
76
      Y.Array.each(subApps, function(subApp) {
 
77
        this.addSubApp(subApp.type, subApp.config);
 
78
      }, this);
 
79
    },
 
80
 
 
81
    /**
 
82
      Extract the routes out of the sub apps.
 
83
 
 
84
      @method _extractRoutes
 
85
      @protected
 
86
      @param {object | integer | undefined} subApp will extract the routes
 
87
        out of the supplied subApp, index, or all subApps if undefined.
 
88
      @return {array} array of sub app route data.
 
89
    */
 
90
    _extractRoutes: function(subApp) {
 
91
      var subApps = this.get('subApps'),
 
92
          routes, subRoutes, i, j;
 
93
 
 
94
      switch (typeof subApp) {
 
95
        case 'number': // If an index is passed in grab that subapp
 
96
          subApp = subApps[subApp];
 
97
        /* falls through */
 
98
        case 'object': // If subapp is passed in fetch it's routes
 
99
          routes = subApp.getSubAppRoutes();
 
100
          break;
 
101
        case 'undefined':
 
102
          routes = [];
 
103
          for (i = 0; i < subApps.length; i += 1) {
 
104
            subRoutes = subApps[i].getSubAppRoutes();
 
105
            for (j = 0; j < subRoutes.length; j += 1) {
 
106
              routes.push(subRoutes[j]);
 
107
            }
 
108
          }
 
109
          break;
 
110
        default:
 
111
          throw 'subApp valid types are integer, object, or undefined';
 
112
      }
 
113
      return routes;
 
114
    },
 
115
 
 
116
    /**
 
117
      Adds the sub app routes to the parent routes after the middleware
 
118
 
 
119
      @method _augmentRoutes
 
120
      @protected
 
121
      @param {array} array of route objects.
 
122
    */
 
123
    _augmentRoutes: function(routes) {
 
124
      var parentRoutes = this.get('routes'),
 
125
          middlewareIndex;
 
126
 
 
127
      Y.Array.some(parentRoutes, function(value, index, array) {
 
128
        if (value.path !== '*') {
 
129
          middlewareIndex = index;
 
130
          return true;
 
131
        }
 
132
      });
 
133
 
 
134
      routes.unshift(middlewareIndex, 0);
 
135
      Array.prototype.splice.apply(parentRoutes, routes);
 
136
 
 
137
      this.set('routes', parentRoutes);
 
138
      return parentRoutes;
 
139
    }
 
140
  };
 
141
 
 
142
  Y.namespace('juju').SubAppRegistration = SubAppRegistration;
 
143
 
 
144
}, '0.1.0');