5
5
var Promise = require ( "bluebird" ) ,
6
6
$ = require ( 'jquery' ) ,
7
7
render = require ( "./render" ) ,
8
- laslaz = require ( './laslaz' ) ;
8
+ laslaz = require ( './laslaz' ) ,
9
+ React = require ( 'react' ) ,
10
+ _ = require ( 'lodash' ) ;
9
11
10
12
require ( "jqueryui" ) ;
11
13
require ( "jquery-layout" ) ;
@@ -1143,73 +1145,6 @@ var Promise = require("bluebird"),
1143
1145
1144
1146
1145
1147
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
-
1213
1148
$ ( "#mensuration-reset" ) . on ( "click" , function ( e ) {
1214
1149
e . preventDefault ( ) ;
1215
1150
$ . event . trigger ( {
@@ -1233,7 +1168,67 @@ var Promise = require("bluebird"),
1233
1168
}
1234
1169
} ) ;
1235
1170
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 ) ) ;
1237
1232
} ;
1238
1233
1239
1234
function nameToScale ( name ) {
0 commit comments