Skip to content

Commit 16ae0a4

Browse files
committed
Merge pull request #275 from jpbackman/show-child-sections-variables
Feature: if section does not use variables, list sub-sections' variables
2 parents 31f7a13 + 56c28d7 commit 16ae0a4

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lib/app/js/directives/design.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ angular.module('sgApp')
77
restrict: 'A',
88
templateUrl: 'views/partials/design.html',
99
link: function(scope) {
10+
11+
var parentRef;
12+
1013
scope.showRelated = true;
1114

12-
scope.$watch('currentReference.section', function(newVal, oldVal) {
13-
scope.relatedVariables = scope.currentReference.section.variables
15+
scope.$watch('currentReference.section', function() {
16+
var relatedVariables = scope.currentReference.section.variables || [];
17+
if (scope.showRelated && relatedVariables.length === 0 && scope.sections.data) {
18+
parentRef = scope.currentReference.section.reference;
19+
scope.relatedChildVariableNames = scope.sections.data.filter(isSubSection)
20+
.map(getVariables)
21+
.reduce(concat, [])
22+
.filter(unique);
23+
}
1424
});
1525

1626
scope.saveVariables = function() {
@@ -19,7 +29,26 @@ angular.module('sgApp')
1929

2030
scope.resetLocal = function() {
2131
Variables.resetLocal();
32+
};
33+
34+
function isSubSection(section) {
35+
var ref = section.parentReference;
36+
return (typeof ref === 'string') &&
37+
(ref === parentRef || ref.substring(0, ref.indexOf('.')) === parentRef);
38+
}
39+
40+
function getVariables(section) {
41+
return section.variables;
2242
}
43+
44+
function concat(a, b) {
45+
return a.concat(b);
46+
}
47+
48+
function unique(a, idx, arr) {
49+
return arr.indexOf(a) === idx && a !== undefined;
50+
}
51+
2352
}
2453
};
2554
});

lib/app/js/directives/section.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ angular.module('sgApp')
1010
function updateCurrentReference() {
1111
var topOffset = element[0].offsetTop,
1212
bottomOffset = element[0].offsetTop + element[0].offsetHeight,
13-
buffer = 100;
13+
buffer = 50;
1414

1515
if (this.pageYOffset > topOffset - buffer && this.pageYOffset < bottomOffset - buffer) {
1616
if ($rootScope.currentReference.section.reference !== scope.section.reference) {

lib/app/views/partials/design.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@
1414
<a class="sg" ng-click="showRelated = false" ng-show="showRelated">Show all variables</a>
1515
<h3 class="sg" ng-show="showRelated">{{currentReference.section.reference}} {{currentReference.section.header}}</h3>
1616
<h3 class="sg" ng-hide="showRelated">All variables</h3>
17-
<ul>
17+
<ul class="sg">
1818
<li ng-hide="showRelated" ng-repeat="variable in variables">
1919
<div sg-variable></div>
2020
</li>
2121
<li ng-if="showRelated" ng-repeat="variable in filteredItems = (variables | filterRelated: currentReference.section.variables)">
2222
<div sg-variable></div>
2323
</li>
24-
<li ng-if="showRelated" ng-show="!filteredItems.length">This section does not contain related variables.</li>
24+
25+
<li ng-if="showRelated" ng-show="filteredItems.length === 0"><b>This section does not contain any related variables.</b></li>
26+
<li ng-if="showRelated" ng-show="relatedChildVariableNames.length > 0">
27+
<p>Sub sections use the following variables:</p>
28+
</li>
29+
<li ng-if="showRelated && filteredItems.length === 0" ng-repeat="variable in childVariables = (variables | filterRelated: relatedChildVariableNames)">
30+
<div sg-variable></div>
31+
</li>
32+
2533
</ul>
2634
<div class="sg action-footer" ng-if="socketService.isAvailable()">
2735
<button class="sg button primary" ng-click="saveVariables()">Save changes</button>

0 commit comments

Comments
 (0)