Skip to content

Commit 29bf1e6

Browse files
author
Gastón I. Silva E
committed
closes #1 treeConfig => service:svg-environment
1 parent 9753483 commit 29bf1e6

File tree

11 files changed

+201
-526
lines changed

11 files changed

+201
-526
lines changed

DESIGN.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
General design of the application.
44

5+
## Services
6+
7+
svg-environment
8+
Has all the values used to configure the SVG root and
9+
the grid design being used to place each node.
10+
511
## Models
612

713
grid-node
@@ -12,13 +18,10 @@ General design of the application.
1218
## Views
1319

1420
* svg
15-
Has the `treeConfig` property used to configure the SVG root and
16-
the grid design being used to place each node.
1721
* svg-g
1822
Every node in the grid uses this view and the content changes by setting
1923
its `templateName` property.
2024

21-
2225
## Mixins
2326

2427
* tree-node

app/controllers/index.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,41 @@ export default Ember.Controller.extend({
1919
}
2020
return 0;
2121
});
22-
}.property('model')
22+
}.property('model'),
23+
24+
gridLines: function() {
25+
var svgenv = this.get('svgenv'),
26+
w = svgenv.viewBoxW,
27+
h = svgenv.viewBoxH,
28+
gridLines = [];
29+
30+
for (var x = 0; x < w; x += svgenv.colW) {
31+
gridLines.push('M' + x + ' 0 V' + h + ' Z');
32+
}
33+
for (var y = 0; y < h; y += svgenv.rowH) {
34+
gridLines.push('M0 ' + y + ' H' + w + ' Z');
35+
}
36+
37+
return gridLines;
38+
}.property(),
39+
40+
yearLines: function() {
41+
return [
42+
this._buildYearLine(1980, 2),
43+
this._buildYearLine(1990, 5),
44+
this._buildYearLine(2000, 9),
45+
this._buildYearLine(2010, 13)
46+
];
47+
}.property(),
48+
49+
_buildYearLine: function(year, row) {
50+
var svgenv = this.get('svgenv'),
51+
x = svgenv.yearLineFontSize * 2,
52+
y = row * svgenv.rowH,
53+
xLine = svgenv.yearLineFontSize * 4;
54+
55+
return {year: year, x: x, y: y,
56+
path: 'M'+xLine+' '+y+' H' + svgenv.viewBoxW};
57+
}
2358

2459
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function initialize(container, application) {
2+
application.inject('controller', 'svgenv', 'service:svg-environment');
3+
}
4+
5+
export default {
6+
name: 'svg-environment-service',
7+
initialize: initialize
8+
};

app/services/svg-environment.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Object.extend({
4+
showGrid: false,
5+
6+
paddingT: 6,
7+
paddingR: 6,
8+
paddingB: 12,
9+
paddingL: 6,
10+
11+
colW: 170 + 12,
12+
rowH: 64 + 18,
13+
14+
maxCols: 7,
15+
maxRows: 20,
16+
17+
viewBoxW: null,
18+
viewBoxH: null,
19+
viewBox: null,
20+
21+
yearLineFontSize: 12, // from CSS rule .year_line_txt
22+
23+
_calcViewBox: function() {
24+
var viewBoxW = this.get('colW') * this.get('maxCols'),
25+
viewBoxH = this.get('rowH') * this.get('maxRows');
26+
27+
this.set('viewBoxW', viewBoxW);
28+
this.set('viewBoxH', viewBoxH);
29+
this.set('viewBox', '0 0 ' + viewBoxW + ' ' + viewBoxH);
30+
}.on('init')
31+
32+
});

app/templates/svg.hbs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
<!-- Debug grid -->
3-
{{#if view.treeConfig.showGrid}}
4-
{{#each line in view.gridLines}}
3+
{{#if svgenv.showGrid}}
4+
{{#each line in gridLines}}
55
<path d="{{unbound line}}" fill="none" stroke="#909090"></path>
66
{{/each}}
77
{{/if}}
88

99
<!-- Years lines -->
10-
{{#each line in view.yearLines}}
10+
{{#each line in yearLines}}
1111
<text x="{{unbound line.x}}" y="{{unbound line.y}}" class="year_line_txt">
1212
<tspan dy="4.233003616333008">
1313
{{line.year}}
@@ -19,15 +19,15 @@
1919
<!-- Design Patterns -->
2020
{{#each elem in model.dpatterns}}
2121
{{view 'svg-g' templateName='node-dpattern'
22-
treeConfig=view.treeConfig
22+
treeConfig=svgenv
2323
elem=elem}}
2424
{{/each}}
2525

2626
<!-- Technologies -->
2727
{{#each elem in model.technologies}}
2828
{{#each className in elem.classNames}}
2929
{{view 'svg-g' templateName='node-technology'
30-
treeConfig=view.treeConfig
30+
treeConfig=svgenv
3131
elem=elem
3232
classNameTech=className}}
3333
{{/each}}

app/views/svg.js

+3-65
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export default Ember.View.extend({
66

77
elementId: 'mvc_tree',
88

9-
109
templateName: 'svg',
1110

1211
attributeBindings: ['xmlns',
@@ -22,69 +21,8 @@ export default Ember.View.extend({
2221
preserveAspectRatio: 'xMinYMin',
2322
viewBox: null,
2423

25-
treeConfig: {
26-
showGrid: false,
27-
28-
paddingT: 6,
29-
paddingR: 6,
30-
paddingB: 12,
31-
paddingL: 6,
32-
33-
colW: 170 + 12,
34-
rowH: 64 + 18,
35-
36-
maxCols: 7,
37-
maxRows: 20,
38-
39-
viewBoxW: null,
40-
viewBoxH: null,
41-
42-
yearLineFontSize: 12 // see CSS rules .year_line_txt
43-
},
44-
45-
calcViewBox: function() {
46-
var tc = this.get('treeConfig');
47-
tc.viewBoxW = tc.colW * tc.maxCols;
48-
tc.viewBoxH = tc.rowH * tc.maxRows;
49-
var viewBox = '0 0 ' + tc.viewBoxW + ' ' + tc.viewBoxH;
50-
51-
this.set('treeConfig', tc);
52-
this.set('viewBox', viewBox);
53-
}.observes('treeConfig').on('init'),
54-
55-
gridLines: function() {
56-
var tc = this.get('treeConfig'),
57-
w = tc.viewBoxW,
58-
h = tc.viewBoxH,
59-
gridLines = [];
60-
61-
for (var x = 0; x < w; x += tc.colW) {
62-
gridLines.push('M' + x + ' 0 V' + h + ' Z');
63-
}
64-
for (var y = 0; y < h; y += tc.rowH) {
65-
gridLines.push('M0 ' + y + ' H' + w + ' Z');
66-
}
67-
68-
return gridLines;
69-
}.property('treeConfig'),
70-
71-
yearLines: function() {
72-
var tc = this.get('treeConfig');
73-
74-
return [
75-
this._buildYearLine(1980, 2, tc),
76-
this._buildYearLine(1990, 5, tc),
77-
this._buildYearLine(2000, 9, tc),
78-
this._buildYearLine(2010, 13, tc)
79-
];
80-
}.property('treeConfig'),
81-
82-
_buildYearLine: function(year, row, tc) {
83-
var x = tc.yearLineFontSize * 2,
84-
y = row * tc.rowH,
85-
xLine = tc.yearLineFontSize * 4;
24+
_setViewBox: function() {
25+
this.set('viewBox', this.get('controller.svgenv.viewBox'));
26+
}.on('init'),
8627

87-
return {year: year, x: x, y: y,
88-
path: 'M'+xLine+' '+y+' H' + tc.viewBoxW};
89-
}
9028
});

tests/acceptance/index-test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ test('visiting /#pac', function(assert) {
2222
assert.ok( ! $panel.isInView(), 'panel is not visible on screen');
2323

2424
visit('/#pac');
25-
window.location.hash = '#pac'; // TODO: revisit Ember url fragments support
25+
// TODO: revisit Ember url fragments support
26+
// https://github.com/emberjs/ember.js/issues/4098
27+
window.location.hash = '#pac';
2628

2729
andThen(function() {
2830
assert.equal(currentPath(), 'index');

tests/unit/controllers/index-test.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {
2+
moduleFor,
3+
test
4+
} from 'ember-qunit';
5+
6+
moduleFor('controller:index', {
7+
// Specify the other units that are required for this test.
8+
// needs: ['controller:foo']
9+
});
10+
11+
// Replace this with your real tests.
12+
test('it exists', function(assert) {
13+
var controller = this.subject();
14+
assert.ok(controller);
15+
});
16+
17+
test('gridLines 1x1', function(assert) {
18+
var controller = this.subject({
19+
svgenv: {
20+
colW: 40, rowH: 50,
21+
maxCols: 1, maxRows: 1,
22+
viewBoxW: 40, viewBoxH: 50,
23+
}
24+
});
25+
26+
var gridLines = [
27+
'M0 0 V50 Z',
28+
'M0 0 H40 Z',
29+
];
30+
assert.deepEqual(controller.get('gridLines'), gridLines, '1x1');
31+
});
32+
33+
test('gridLines 2x2', function(assert) {
34+
var controller = this.subject({
35+
svgenv: {
36+
colW: 20, rowH: 30,
37+
maxCols: 2, maxRows: 2,
38+
viewBoxW: 40, viewBoxH: 60
39+
}
40+
});
41+
var gridLines = [
42+
'M0 0 V60 Z',
43+
'M20 0 V60 Z',
44+
'M0 0 H40 Z',
45+
'M0 30 H40 Z',
46+
];
47+
assert.deepEqual(controller.get('gridLines'), gridLines, '2x2');
48+
});
49+
50+
test('gridLines 0x0', function(assert) {
51+
var controller = this.subject({
52+
svgenv: {
53+
colW: 0, rowH: 0,
54+
maxCols: 2, maxRows: 2,
55+
viewBoxW: 0, viewBoxH: 0
56+
}
57+
});
58+
var gridLines = [];
59+
assert.deepEqual(controller.get('gridLines'), gridLines, 'colW and rowH are 0');
60+
});
61+
62+
test('_buildYearLine', function(assert) {
63+
var controller = this.subject({
64+
svgenv: {
65+
colW: 30, rowH: 60,
66+
maxCols: 1, maxRows: 1,
67+
viewBoxW: 30, viewBoxH: 60,
68+
yearLineFontSize: 12
69+
}
70+
});
71+
72+
var yearLine = {year: 1514, x: 24, y: 0,
73+
path: 'M48 0 H30'};
74+
assert.deepEqual(controller._buildYearLine(1514, 0), yearLine);
75+
76+
yearLine = {year: 1514, x: 24, y: 60,
77+
path: 'M48 60 H30'};
78+
assert.deepEqual(controller._buildYearLine(1514, 1), yearLine);
79+
});
80+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {
2+
moduleFor,
3+
test
4+
} from 'ember-qunit';
5+
6+
moduleFor('service:svg-environment', {
7+
// Specify the other units that are required for this test.
8+
// needs: ['service:foo']
9+
});
10+
11+
// Replace this with your real tests.
12+
test('it exists', function(assert) {
13+
var service = this.subject();
14+
assert.ok(service);
15+
});
16+
17+
test('calcViewBox', function(assert) {
18+
var service = this.subject({
19+
colW: 10,
20+
rowH: 5,
21+
maxCols: 10,
22+
maxRows: 10
23+
});
24+
25+
assert.equal(service.viewBoxW, 100);
26+
assert.equal(service.viewBoxH, 50);
27+
assert.equal(service.viewBox, '0 0 100 50');
28+
});

0 commit comments

Comments
 (0)