-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblocks.js
135 lines (127 loc) · 4.38 KB
/
blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(function(){
var DEFAULT_COLOR = "gray";
angular.module("blocks", ["ngRoute"])
.config(function($routeProvider) {
$routeProvider.when("/:set", {
redirectTo: "/:set/0"
}).when("/:set/:challengeNum", {
template:
'<header>\
<button class="prev-button" ng-click="vm.prev()" ng-disabled="!vm.hasPrev()">Previous</button>\
<div class="challengeNumbers">\
<button ng-repeat="challenge in vm.challenges" ng-class="{current: $index === vm.challengeNum - 1}" ng-click="vm.goTo($index)">{{$index + 1}}</button>\
</div>\
<button class="prev-button" ng-click="vm.next()" ng-disabled="!vm.hasNext()">Next</button>\
</header>\
<challenge challenge="vm.challenge" allow-see-solution="true"></challenge>',
controller: "challengeController",
controllerAs: "vm"
});
})
.controller("challengeController", function(sets, $routeParams, $location) {
var vm = this;
var set = sets[$routeParams.set];
var challengeNum = parseInt($routeParams.challengeNum) || 0;
vm.challenges = set.challenges;
vm.challenge = set.challenges[challengeNum];
vm.challengeNum = challengeNum + 1;
vm.challengeCount = set.challenges.length;
vm.hasPrev = function() {
return challengeNum > 0;
}
vm.hasNext = function() {
return challengeNum < set.challenges.length - 1;
}
vm.prev = function() {
vm.goTo(challengeNum - 1);
}
vm.next = function() {
vm.goTo(challengeNum + 1);
}
vm.goTo = function(num) {
$location.path("/" + $routeParams.set + "/" + num);
}
})
.component("challenge", {
bindings: { challenge: "<", allowSeeSolution: "<" },
controllerAs: "vm",
template:
'<div class="challenge">\
<div class="code-area">\
<pre class="code"><code>{{vm.challengeToCode(vm.challenge)}}</code></pre>\
<button ng-if="vm.allowSeeSolution && !vm.isShowSolution" ng-click="vm.showSolution()" type="button">Show Solution</button>\
</div>\
<solution ng-if="vm.isShowSolution" challenge="vm.challenge" delay="150"></solution>\
</div>',
controller: function(challengeToCode) {
var vm = this;
vm.challengeToCode = challengeToCode;
vm.isShowSolution = false;
vm.showSolution = function() {
vm.isShowSolution = true;
}
}
})
.component("block", {
bindings: { color: "@" },
controllerAs: "vm",
template: '<div class="block" style="background-color: {{vm.color}}"></div>'
})
.component("solution", {
bindings: { "challenge": "<", "delay": "<" },
template:
'<div class="solution">\
<div class="stack" ng-repeat="stack in $ctrl.stacks track by $index">\
<block color="{{block}}" ng-repeat="block in stack track by $index"></block>\
</div>\
</div>',
controller: function($scope, challengeToSteps, $timeout) {
var vm = this;
var pendingTimeouts = [];
$scope.$watch("vm.challenge", function() {
buildSolution();
});
$scope.$watch("vm.delay", function() {
buildSolution();
});
function buildSolution() {
// empty stacks and clear timeouts
pendingTimeouts.forEach(function(timeout) { $timeout.cancel(timeout) });
pendingTimeouts = [];
vm.stacks = [[],[],[],[],[],[]];
// build stacks
var steps = challengeToSteps(vm.challenge);
var delay = vm.delay || 0;
steps.forEach(function(step, i) {
pendingTimeouts.push($timeout(function() {
console.log(vm.stacks, step);
vm.stacks[step.stack].push(step.color);
}, delay * i));
});
}
}
})
.factory("challengeToSteps", function() {
return function(fn) {
if (!fn) {
return [];
}
var steps = [];
function addBlock(stack, color) {
steps.push({
stack: stack,
color: color || "gray"
});
}
fn(addBlock);
return steps;
};
})
.factory("challengeToCode", function() {
return function(fn) {
return fn.toString()
.replace(/(^.*?\{\s*)|(\s*\}.*?$)/g, "") // only keep function body
.replace(/\n(\t| {4})/g, "\n"); // remove first indent
}
})
})();