Skip to content

Commit

Permalink
[FEATURE] Support for Ember Engines routes
Browse files Browse the repository at this point in the history
  • Loading branch information
cyril-sf committed Feb 1, 2019
1 parent a31c214 commit ff5f266
Show file tree
Hide file tree
Showing 24 changed files with 216 additions and 26 deletions.
11 changes: 11 additions & 0 deletions app/components/container-picker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Component from '@ember/component';

export default Component.extend({
containers: null,

actions: {
onSelect(containerId) {
this.sendAction('selectContainer', containerId);
}
}
});
7 changes: 7 additions & 0 deletions app/controllers/containers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';
import { sort } from '@ember/object/computed';

export default Controller.extend({
sortProperties: ['container_type_id', 'container_instance_id'],
sorted: sort('model', 'sortProperties')
});
9 changes: 6 additions & 3 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ Router.map(function() {
});

this.route('render-tree', { resetNamespace: true });
this.route('container-types', { resetNamespace: true }, function() {
this.route('container-type', { path: '/:type_id', resetNamespace: true });
this.route('containers', { path: '/containers', resetNamespace: true }, function() {
this.route('container', { path: '/:container_type_id/instance/:container_instance_id' }, function() {
this.route('container-types', { path: '/type' }, function() {
this.route('container-type', { path: '/:type_id' });
});
});
});

this.route('deprecations', { resetNamespace: true });
});

});

export default Router;
2 changes: 0 additions & 2 deletions app/routes/container-types/index.js

This file was deleted.

29 changes: 29 additions & 0 deletions app/routes/containers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import TabRoute from "ember-inspector/routes/tab";

export default TabRoute.extend({
model() {
const port = this.get('port');
return new Promise(resolve => {
port.one('container:all', function(message) {
let container_type_id, container_instance_id, containers;
containers = message.containers.map(function(containerRef) {
[container_type_id, container_instance_id] = containerRef.split(':');
return {
container_type_id,
container_instance_id
}
});
resolve(containers);
});
port.send('container:all');
});
},

actions: {
selectContainer(containerIndex) {
let containerRef;
containerRef = this.get('controller.model').objectAt(containerIndex);
this.transitionTo('containers.container', containerRef);
}
}
});
7 changes: 7 additions & 0 deletions app/routes/containers/container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Route from '@ember/routing/route';

export default Route.extend({
model(params) {
return params;
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import Route from '@ember/routing/route';
import { Promise } from 'rsvp';

export default Route.extend({
controllerName: 'container-types',

model() {
let containerRef = this.modelFor('containers.container');
const port = this.get('port');
return new Promise(resolve => {
port.one('container:types', function(message) {
resolve(message.types);
});
port.send('container:getTypes');
containerRef = containerRef.container_type_id+':'+containerRef.container_instance_id;
port.send('container:getTypes', { containerRef });
});
},
actions: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Promise } from 'rsvp';
import { get } from '@ember/object';
import TabRoute from "ember-inspector/routes/tab";
import Route from '@ember/routing/route';

export default Route.extend({
controllerName: 'container-type',

export default TabRoute.extend({
setupController(controller) {
controller.setProperties({
search: '',
Expand All @@ -11,8 +13,9 @@ export default TabRoute.extend({
this._super(...arguments);
},
model(params) {
const type = params.type_id;
const containerType = params.type_id;
const port = this.get('port');
let containerRef = this.modelFor('containers.container');
return new Promise((resolve, reject) => {
port.one('container:instances', message => {
if (message.status === 200) {
Expand All @@ -21,15 +24,16 @@ export default TabRoute.extend({
reject(message);
}
});
port.send('container:getInstances', { containerType: type });
containerRef = containerRef.container_type_id+':'+containerRef.container_instance_id;
port.send('container:getInstances', { containerRef, containerType });
});
},


actions: {
error(err) {
if (err && err.status === 404) {
this.transitionTo('container-types.index');
this.transitionTo('containers.container.containers-types.container-type.index');
return false;
}
},
Expand Down
8 changes: 8 additions & 0 deletions app/routes/containers/container/container-types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Route from '@ember/routing/route';

export default Route.extend({
afterModel() {
let containerType = this.modelFor('containers.container.container-types').firstObject;
return this.transitionTo('containers.container.container-types.container-type', containerType.name);
}
});
7 changes: 7 additions & 0 deletions app/routes/containers/container/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Route from '@ember/routing/route';

export default Route.extend({
beforeModel() {
this.transitionTo('containers.container.container-types');
}
});
11 changes: 11 additions & 0 deletions app/routes/containers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Route from '@ember/routing/route';

export default Route.extend({
beforeModel() {
let containers = this.modelFor('containers');
if (containers) {
this.transitionTo('containers.container', containers[0]);
}
}
});

8 changes: 8 additions & 0 deletions app/templates/components/container-picker.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="dropdown">
<select class="dropdown__select" onchange={{action "onSelect" value="target.value"}}>
{{#each containers as |containerRef index|}}
<option value={{index}}>{{containerRef.container_type_id}}:{{containerRef.container_instance_id}}</option>
{{/each}}
</select>
{{svg-jar "dropdown-arrow" class="dropdown__arrow"}}
</div>
3 changes: 2 additions & 1 deletion app/templates/components/item-types.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
{{#each sorted as |itemType|}}
<li class={{typeClass}}>
{{#link-to
(if (eq type "model") "records" "container-type")
(if (eq type "model") "records" "containers.container.container-types.container-type")
(escape-url itemType.name)
}}
<span class={{concat typeClass "-name"}}>
{{itemType.name}}
</span>
(<span class={{concat typeClass "-count"}}>{{itemType.count}}</span>)

{{/link-to}}
</li>
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<div class="toolbar">
{{container-picker containers=sorted selectContainer='selectContainer'}}

{{reload-button
action="reload"
classNames="toolbar__icon-button js-reload-container-btn"
Expand Down
1 change: 1 addition & 0 deletions app/templates/containers.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{outlet}}
1 change: 1 addition & 0 deletions app/templates/containers/container.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{outlet}}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/templates/nav.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{{/link-to}}
</li>
<li>
{{#link-to "container-types"}}
{{#link-to "containers"}}
{{svg-jar "nav-container" width="20px" height="20px"}}
Container
{{/link-to}}
Expand Down
50 changes: 41 additions & 9 deletions ember_debug/container-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,34 @@ export default EmberObject.extend(PortMixin, {
return type[0] === '-' || this.get('TYPES_TO_SKIP').indexOf(type) !== -1;
},

instancesByType() {
all() {
let containers = [
'app:main'
];

const router = this.get('container').lookup('router:main');
containers.pushObjects( Object.keys(router._engineInfoByRoute).map(function(key) {
return router._engineInfoByRoute[key].name + ':' + router._engineInfoByRoute[key].instanceId;}
).uniq());

return containers;
},

getContainer(containerRef) {
if (containerRef === 'app:main') {
return this.get('container');
} else {
const router = this.get('container').lookup('router:main');
let container_type_id, container_instance_id;
[container_type_id, container_instance_id] = containerRef.split(':');
return router._engineInstances[container_type_id][container_instance_id].__container__;
}
},

instancesByType(container) {
let key;
let instancesByType = {};
let cache = this.get('container').cache;
let cache = container.cache;
// Detect if InheritingDict (from Ember < 1.8)
if (typeof cache.dict !== 'undefined' && typeof cache.eachLocal !== 'undefined') {
cache = cache.dict;
Expand All @@ -59,18 +83,20 @@ export default EmberObject.extend(PortMixin, {
return instancesByType;
},

getTypes() {
getTypes(containerRef) {
let key;
let types = [];
const instancesByType = this.instancesByType();
const container = this.getContainer(containerRef);
const instancesByType = this.instancesByType(container);
for (key in instancesByType) {
types.push({ name: key, count: instancesByType[key].length });
}
return types;
},

getInstances(type) {
const instances = this.instancesByType()[type];
getInstances(containerRef, type) {
const container = this.getContainer(containerRef);
const instances = this.instancesByType(container)[type];
if (!instances) {
return null;
}
Expand All @@ -82,13 +108,19 @@ export default EmberObject.extend(PortMixin, {
},

messages: {
getTypes() {
all() {
this.sendMessage('all', {
containers: this.all()
});
},

getTypes(message) {
this.sendMessage('types', {
types: this.getTypes()
types: this.getTypes(message.containerRef)
});
},
getInstances(message) {
let instances = this.getInstances(message.containerType);
let instances = this.getInstances(message.containerRef, message.containerType);
if (instances) {
this.sendMessage('instances', {
instances,
Expand Down
16 changes: 12 additions & 4 deletions ember_debug/route-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,18 @@ function buildSubTree(routeTree, route) {

const router = this.get('router');
const routerLib = router._routerMicrolib || router.router;
routeHandler = routerLib.getHandler(handler);
controllerName = routeHandler.get('controllerName') || routeHandler.get('routeName');
controllerFactory = owner.factoryFor ? owner.factoryFor(`controller:${controllerName}`) : owner._lookupFactory(`controller:${controllerName}`);
controllerClassName = this.getClassName(controllerName, 'controller');
let isWithinEngine = router._engineInfoByRoute[handler];
let hasBeenLoaded = router._seenHandlers[handler];
if (isWithinEngine && !hasBeenLoaded) {
controllerName = 'Unloaded controller name';
controllerFactory = true;
controllerClassName = 'Unloaded controller class name';
} else {
routeHandler = routerLib.getHandler(handler);
controllerName = routeHandler.get('controllerName') || routeHandler.get('routeName');
controllerFactory = owner.factoryFor ? owner.factoryFor(`controller:${controllerName}`) : owner._lookupFactory(`controller:${controllerName}`);
controllerClassName = this.getClassName(controllerName, 'controller');
}
templateName = this.getClassName(handler, 'template');

subTree[handler] = {
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/components/container-picker-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Component | container-picker', function(hooks) {
setupRenderingTest(hooks);

test('it renders', async function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.set('myAction', function(val) { ... });

await render(hbs`{{container-picker}}`);

assert.equal(this.element.textContent.trim(), '');

// Template block usage:
await render(hbs`
{{#container-picker}}
template block text
{{/container-picker}}
`);

assert.equal(this.element.textContent.trim(), 'template block text');
});
});
11 changes: 11 additions & 0 deletions tests/unit/routes/container-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Route | container', function(hooks) {
setupTest(hooks);

test('it exists', function(assert) {
let route = this.owner.lookup('route:container');
assert.ok(route);
});
});
11 changes: 11 additions & 0 deletions tests/unit/routes/containers-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Route | containers', function(hooks) {
setupTest(hooks);

test('it exists', function(assert) {
let route = this.owner.lookup('route:containers');
assert.ok(route);
});
});

0 comments on commit ff5f266

Please sign in to comment.