Skip to content

Commit bafd330

Browse files
committed
Add react and lodash deps, Mensuration points view is now react based
1 parent 15c943c commit bafd330

File tree

5 files changed

+149
-79
lines changed

5 files changed

+149
-79
lines changed

index.html

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,8 @@ <h3>Data</h3>
147147
<h4>Mensuration Points</h4>
148148
<h5>Set of points in mensuration view</h5>
149149
<button type="button" class="btn btn-sm btn-default btn-block" id="mensuration-reset">Reset Points</button>
150-
<table class="table">
151-
<thead>
152-
<tr>
153-
<td>Index</td>
154-
<td style="text-align: right">Length</td>
155-
</tr>
156-
</thead>
157-
<tbody></tbody>
158-
</table>
150+
<div id="points-list-table">
151+
</div>
159152
</div>
160153
<div class="labeled-controls">
161154
<h4>Particle Size</h4>
@@ -301,6 +294,7 @@ <h4 style="color:#999">Please indicate what you'd like to do:</h4>
301294
</div>
302295

303296

297+
<script src="http://fb.me/react-0.10.0.js"></script>
304298
<script src="bad.js"></script>
305299
<script src="client.js"></script>
306300
<script type="x-shader/x-vertex" id="vertexshader">

js/ui.js

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
var Promise = require("bluebird"),
66
$ = require('jquery'),
77
render = require("./render"),
8-
laslaz = require('./laslaz');
8+
laslaz = require('./laslaz'),
9+
React = require('react'),
10+
_ = require('lodash');
911

1012
require("jqueryui");
1113
require("jquery-layout");
@@ -1143,73 +1145,6 @@ var Promise = require("bluebird"),
11431145

11441146

11451147
var setupMensurationHandlers = function() {
1146-
var currentPoints = [];
1147-
var $control = $("#points-list");
1148-
var $table = $("#points-list table");
1149-
1150-
console.log("Mensuration views:");
1151-
console.log($control, $table);
1152-
1153-
var _formatVector = function(v) {
1154-
return "(" +
1155-
v.x.toFixed(1) + ", " +
1156-
v.y.toFixed(1) + ", " +
1157-
v.z.toFixed(1) + ")";
1158-
};
1159-
1160-
var _distance = function(a, b) {
1161-
var d = a.distanceTo(b);
1162-
return d.toFixed(1);
1163-
};
1164-
1165-
var _updateTable = function() {
1166-
var html = "";
1167-
for (var i = 0, il = currentPoints.length - 1 ; i < il ; i ++) {
1168-
if (currentPoints[i].id !== currentPoints[i+1].id)
1169-
continue; // if the next point is a starting point for the next one
1170-
1171-
1172-
html +=
1173-
"<tr style='background-color:#" + currentPoints[i].color.getHexString() + "'>" +
1174-
"<td>" + (i+1) + "</td>" +
1175-
// "<td>" + _formatVector(currentPoints[i]) + "</td>" +
1176-
//"<td>" + _formatVector(currentPoints[i+1]) + "</td>" +
1177-
"<td style='text-align: right'>" + _distance(currentPoints[i], currentPoints[i+1]) + "</td>" +
1178-
"</tr>";
1179-
}
1180-
1181-
$table.find("tbody").html(html);
1182-
1183-
if (html.length === 0)
1184-
$table.hide();
1185-
else
1186-
$table.show();
1187-
};
1188-
1189-
$(document).on('plasio.mensuration.pointAdded', function(e) {
1190-
console.log("Adding new point");
1191-
currentPoints.push(e.point);
1192-
_updateTable();
1193-
});
1194-
1195-
$(document).on('plasio.mensuration.pointRemoved', function(e) {
1196-
console.log("Removing a point");
1197-
for (var i = 0, il = currentPoints.length ; i < il ; i ++) {
1198-
if (currentPoints[i] == e.point) {
1199-
currentPoints.splice(i, 1);
1200-
break;
1201-
}
1202-
}
1203-
1204-
_updateTable();
1205-
});
1206-
1207-
$(document).on('plasio.mensuration.pointsReset', function(e) {
1208-
console.log("Resetting all points");
1209-
currentPoints = [];
1210-
_updateTable();
1211-
});
1212-
12131148
$("#mensuration-reset").on("click", function(e) {
12141149
e.preventDefault();
12151150
$.event.trigger({
@@ -1233,7 +1168,67 @@ var Promise = require("bluebird"),
12331168
}
12341169
});
12351170

1236-
_updateTable();
1171+
var LineSegment = React.createClass({
1172+
render: function() {
1173+
return React.DOM.tr({
1174+
style: { backgroundColor: '#' + this.props.start.color.getHexString() },
1175+
children: [
1176+
React.DOM.td(null, this.props.lineIndex),
1177+
React.DOM.td({ style: { textAlign: 'right' }}, this.props.start.distanceTo(this.props.end).toFixed(1))
1178+
]
1179+
});
1180+
}
1181+
});
1182+
1183+
var LineSegmentsBox = React.createClass({
1184+
getInitialState: function() {
1185+
return { points: [] };
1186+
},
1187+
componentWillMount: function() {
1188+
var c = this;
1189+
$(document).on('plasio.mensuration.pointAdded', function(e) {
1190+
c.setState({ points: c.state.points.concat([e.point])});
1191+
});
1192+
1193+
$(document).on('plasio.mensuration.pointRemoved', function(e) {
1194+
c.setState({ points: _.without(c.state.points, e.point) });
1195+
});
1196+
1197+
$(document).on('plasio.mensuration.pointsReset', function(e) {
1198+
console.log("Resetting all points");
1199+
c.setState({ points: [] });
1200+
});
1201+
},
1202+
render: function() {
1203+
var lines = [];
1204+
var index = 0;
1205+
for (var i = 0 ; i < this.state.points.length - 1 ; i ++) {
1206+
var p1 = this.state.points[i],
1207+
p2 = this.state.points[i+1];
1208+
if (p1.id === p2.id) {
1209+
lines.push(LineSegment({ lineIndex: index+1, start: p1, end: p2 }));
1210+
index ++;
1211+
}
1212+
}
1213+
1214+
if (lines.length === 0)
1215+
return React.DOM.div({ className: "its-empty" },
1216+
"No Mensuration Points");
1217+
return React.DOM.table({
1218+
class: "table"
1219+
}, [
1220+
React.DOM.thead(null, [
1221+
React.DOM.tr(null, [
1222+
React.DOM.td(null, 'Index'),
1223+
React.DOM.td(null, 'Length')
1224+
])
1225+
]),
1226+
React.DOM.tbody(null, lines)
1227+
]);
1228+
}
1229+
});
1230+
1231+
React.renderComponent(LineSegmentsBox({}), $("#points-list-table").get(0));
12371232
};
12381233

12391234
function nameToScale(name) {

less/style.less

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,26 @@ body {
341341
h5.not-first {
342342
margin-top: 20px;
343343
}
344+
345+
#points-list-table {
346+
width: 100%;
347+
table {
348+
width: 100%;
349+
350+
td {
351+
padding: 5px;
352+
}
353+
354+
td:last-child {
355+
text-align: right;
356+
}
357+
}
358+
359+
.its-empty {
360+
padding-top: 5px;
361+
width: 100%;
362+
text-align: center;
363+
font-style: italic;
364+
color: #999;
365+
}
366+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@
4545
"jquery-layout": "./vendor/jquery.layout.js",
4646
"bootstrap": "./vendor/bootstrap.min.js",
4747
"trackball-controls": "./vendor/controls/TrackballControls.js",
48-
"binary-loader": "./vendor/loaders/BinaryLoader.js"
48+
"binary-loader": "./vendor/loaders/BinaryLoader.js",
49+
"lodash": "./vendor/lodash.min.js"
4950
},
5051
"browserify-shim": {
5152
"bluebird": "global:Promise",
53+
"react": "global:React",
5254
"jquery": "$",
5355
"jqueryui": {
5456
"depends": "jquery:$"

0 commit comments

Comments
 (0)