-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
267 lines (229 loc) · 11.2 KB
/
index.html
File metadata and controls
267 lines (229 loc) · 11.2 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/jquery-1.10.2.min.js"></script>
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/rxjs/dist/rx.all.js"></script>
<script src="bower_components/angular-rx/dist/rx.angular.js"></script>
<script type="text/javascript">
($(function() {
}));
</script>
<script type="text/javascript">
var app = angular.module('app', ['rx']);
app.directive('canvas', function () {
return {
templateNamespace: 'svg',
template: '<svg ng-click="add()" height="400" style="display: inline-block" width="60%" ng-transclude></svg>',
restrict: 'E',
replace: true,
transclude: true,
link: function(scope, element, attr) {
scope.add = function() {
if (scope.addDevice) {
scope.devices.push({ name: 'new device ' + scope.devices.length, X: 100, Y: 100 });
}
else if (scope.addLinkstation) {
scope.linkstations.push({ X: 100, Y: 100, reach: 80 });
scope.devices.forEach(function(device) { scope.relink(device)});
}
};
}
}
});
app.directive('linkStation', function () {
return {
templateNamespace: 'svg',
template: '<circle r=5></circle>',
restrict: 'E',
replace: true
};
});
app.directive('device', function () {
return {
templateNamespace: 'svg',
template: '<circle r=10 style="background-color: #105cb6"></circle>',
restrict: 'E',
replace: true
};
});
app.directive('link', function () {
return {
templateNamespace: 'svg',
template: '<g>' +
'<line x1="{{link.d.X}}" x2="{{link.l.X}}" y1="{{link.d.Y}}" y2="{{link.l.Y}}" stroke="5" style="stroke:rgb(255,0,0);stroke-width:2">' +
'</line>' +
'<text x="{{link.d.X}}" y="{{link.d.Y}}" fill="red">power:{{link.currPower}}</text>' +
'</g>',
restrict: 'E',
scope: {
link: '=link'
},
replace: true
};
});
app.directive('rxDrag', ['$document', function($document) {
return function(scope, element, attr) {
var device = scope.devices.filter(function(elem) { return elem.name == attr.name })[0];
var offsetX = Math.round($('#canvas').position().left);
var offsetY = Math.round($('#canvas').position().top);
element.css({
position: 'relative',
border: '1px solid red',
backgroundColor: 'lightgrey',
cursor: 'pointer'
});
element.on('mousedown', function(event) {
event.preventDefault();
$document.on('mousemove', mousemove);
$document.on('mouseup', mouseup);
});
function mousemove(event) {
device.Y = event.pageY - offsetY;
device.X = event.pageX - offsetX;
scope.$apply();
}
function mouseup() {
$document.off('mousemove', mousemove);
$document.off('mouseup', mouseup);
}
};
}]);
app.controller('main', ['$scope', 'observeOnScope', function($scope, observeOnScope) {
var linkstations = [
{ X: 400, Y: 400, reach: 300 },
{ X: 300, Y: 300, reach: 200 },
{ X: 150, Y: 200, reach: 100 },
{ X: 600, Y: 90, reach: 50 }
];
var devices = [
{ name: 'device 1', X: 200, Y: 250 },
{ name: 'device 2', X: 300, Y: 90 },
{ name: 'device 3', X: 150, Y: 150 },
{ name: 'device 4', X: 125, Y: 60 }
];
var links = [
{ d: devices[0], l: linkstations[0], currPower: 0 },
{ d: devices[1], l: linkstations[0], currPower: 0 },
{ d: devices[2], l: linkstations[0], currPower: 0 },
{ d: devices[3], l: linkstations[0], currPower: 0 }
];
var getCurrentLink = function(device) {
var currLink = $scope.links.filter(function(link) { return link.d === device; })[0];
if (!currLink) {
currLink = {
d: device,
l: { X:0, Y: 0, reach: 0 }
}
}
return currLink;
};
var getDist = function(elem1, elem2) {
return Math.sqrt(Math.pow(Math.abs(elem1.X - elem2.X), 2) +
Math.pow(Math.abs(elem1.Y - elem2.Y), 2));
};
var getPower = function(dist, reach) {
return dist > reach ? 0.0 : Math.pow((reach - dist), 2);
};
$scope.relink = function(device) {
var currLink = getCurrentLink(device);
var currLinkstation = currLink.l;
var currDist = getDist(device, currLinkstation);
var currPower = getPower(currDist, currLinkstation.reach);
var preferedLinkstationAndPower = $scope.linkstations.reduce(function(acc, curr) {
var dist = getDist(device, curr);
var power = getPower(dist, curr.reach);
if (power > 0 && power > currPower) {
return { l: curr, p: currPower };
}
else {
return acc;
}
}, { l: currLinkstation, p: currPower });
currLink.currPower = currPower;
if (preferedLinkstationAndPower.l !== currLinkstation)
{
currLink.l = preferedLinkstationAndPower.l;
currLink.currPower = preferedLinkstationAndPower.p;
if($scope.links.indexOf(currLink) < 0) {
$scope.links.push(currLink);
}
}
else if (currPower == 0)
{
var index = $scope.links.indexOf(currLink);
if (index >= 0)
{
$scope.links.splice(index, 1);
}
}
};
$scope.getCurrentPower = function(device) {
var currLink = getCurrentLink(device);
return currLink.currPower;
};
$scope.devices = devices;
$scope.linkstations = linkstations;
$scope.links = links;
$scope.addDevice = false;
$scope.addLinkstation = false;
devices.forEach(function(device) { $scope.relink(device)});
var datachanges = observeOnScope($scope, function($scope) {
return $scope.devices.map(function(device) {
return { d: device, x: device.X, y: device.Y };
})
}, true);
datachanges.subscribe(function(change) {
for (var i = 0; i < change.newValue.length; i++){
var newD = change.newValue[i].d;
if (i >= change.oldValue.length)
{
$scope.relink(newD);
break;
}
var oldD = change.oldValue[i].d;
if (newD.X != oldD.X || newD.Y != oldD.Y) {
$scope.relink(newD);
break;
}
}
});
}]);
</script>
</head>
<body ng-app="app">
<p>This page shows link stations, devices and the preferred link station for each device.</p>
<div ng-controller="main">
<div style="display: inline-block;margin-right:5px;width: 20%;vertical-align: top">
<div id="deviceControls" ng-repeat="device in devices">
<input type="text" ng-model="device.name "/>
<input type="number" ng-model="device.X" style="display: block"/>
<input type="number" ng-model="device.Y" style="display: block"/>
<p>current power: {{getCurrentPower(device)}}</p>
</div>
</div>
<div id="buttons" style="display: inline-block;width:10%;margin-right:5px">
<input type="button" class="toggleButton" ng-class="{'pressed' : addDevice}" ng-click="addDevice=!addDevice" value="Add device" />
<input type="button" class="toggleButton" ng-class="{'pressed' : addLinkstation}" ng-click="addLinkstation=!addLinkstation" value="Add link station" />
</div>
<canvas ng-cloak id="canvas" style="margin-left: 10px;border: 5px solid;">
<link-station ng-repeat="device in linkstations" ng-attr-cx="{{device.X}}" ng-attr-cy="{{device.Y}}"></link-station>
<device rx-drag ng-repeat="device in devices" data-name="{{device.name}}" ng-attr-cx="{{device.X}}" ng-attr-cy="{{device.Y}}"></device>
<link ng-repeat="link in links" link="link" />
</canvas>
</div>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
</body>
</html>