Mercurial > hg > digilib
comparison webapp/src/main/webapp/jquery/jquery.digilib.measure.js @ 1331:f50d5517ea52
another try to restore
author | hertzhaft |
---|---|
date | Tue, 27 Jan 2015 10:22:06 +0100 |
parents | dc94ed3f60ff |
children | 1d6dfca1709f |
comparison
equal
deleted
inserted
replaced
1326:08fffd540a75 | 1331:f50d5517ea52 |
---|---|
699 lineColor : 'red', | 699 lineColor : 'red', |
700 // color while the line is drawn | 700 // color while the line is drawn |
701 drawColor : 'green', | 701 drawColor : 'green', |
702 // color of selected objects | 702 // color of selected objects |
703 selectColor : 'red', | 703 selectColor : 'red', |
704 // drawing shape types | 704 // implemented measuring shape types, for select widget |
705 shapeTypes : [ | 705 implementedShapes : ['Line', 'LineString', 'Rectangle', 'Polygon'], |
706 { name : 'line', type : 'Line' }, | 706 // all measuring shape types |
707 { name : 'linestring', type : 'LineString' }, | 707 shapeTypes : { |
708 { name : 'rectangle', type : 'Rectangle' }, | 708 Line : { name : 'line' }, |
709 { name : 'square', type : 'Square' }, | 709 LineString : { name : 'linestring' }, |
710 { name : 'polygon', type : 'Polygon' }, | 710 Rectangle : { name : 'rectangle' }, |
711 { name : 'circle', type : 'Circle' }, | 711 Square : { name : 'square' }, |
712 { name : 'arch', type : 'Arch' }, | 712 Polygon : { name : 'polygon' }, |
713 { name : 'ratio', type : 'Ratio' }, | 713 Circle : { name : 'circle' }, |
714 { name : 'intercolumnium', type : 'InterCol' }, | 714 Arch : { name : 'arch' }, |
715 { name : 'line grid', type : 'Grid' } | 715 Ratio : { name : 'ratio' }, |
716 ], | 716 InterCol : { name : 'intercolumnium' }, |
717 // index of default shape | 717 Grid : { name : 'linegrid' } |
718 selectedShape : 0, | 718 }, |
719 // most recently selected shape | |
720 activeShapeType : 'Line', | |
721 // last measured distance | |
722 lastMeasuredDistance : 0, | |
719 // measuring unit (index into unit list) | 723 // measuring unit (index into unit list) |
720 unitFrom : 17, | 724 unitFrom : 17, |
721 // converted unit (index into unit list) | 725 // converted unit (index into unit list) |
722 unitTo : 0, | 726 unitTo : 0, |
723 // maximal denominator for mixed fractions | 727 // maximal denominator for mixed fractions |
757 }; | 761 }; |
758 | 762 |
759 var actions = { | 763 var actions = { |
760 measurebar : function(data) { | 764 measurebar : function(data) { |
761 var $measureBar = data.$measureBar; | 765 var $measureBar = data.$measureBar; |
762 if (!$measureBar) { | 766 if ($measureBar == null) { |
763 $measureBar = setupMeasureBar(data); | 767 $measureBar = setupMeasureBar(data); |
764 }; | 768 }; |
765 $measureBar.toggle(); | 769 $measureBar.toggle(); |
766 setScreenPosition(data, $measureBar); | 770 setScreenPosition(data, $measureBar); |
767 return; | 771 return; |
768 }, | 772 }, |
769 drawshape : function(data) { | 773 drawshape : function(data) { |
770 var shape = getSelectedShape(data); | 774 var shape = newShape(data); |
771 data.measureWidgets.startb.addClass('dl-drawing'); | 775 $(data).trigger('createShape', shape); |
772 digilib.actions.addShape(data, shape, shapeCompleted); | 776 digilib.actions.addShape(data, shape, shapeCompleted); |
773 console.debug('measure: action drawshape', shape); | 777 console.debug('measure: action drawshape', shape); |
774 } | 778 } |
775 }; | |
776 | |
777 | |
778 // round to 4 decimal places after point | |
779 var mRound = function (num) { | |
780 return Math.round(num * 10000 + 0.00001) / 10000 | |
781 }; | 779 }; |
782 | 780 |
783 // callback for vector.drawshape | 781 // callback for vector.drawshape |
784 var shapeCompleted = function(data, shape) { | 782 var shapeCompleted = function(data, shape) { |
785 console.debug('measure: shapeCompleted', shape); | 783 console.debug('measure: shapeCompleted', shape); |
789 }; | 787 }; |
790 $(data).trigger('changeShape', shape); // update widgets | 788 $(data).trigger('changeShape', shape); // update widgets |
791 return false; | 789 return false; |
792 }; | 790 }; |
793 | 791 |
792 // callback for vector.drawshape | |
793 var onCreateShape = function(event, shape) { | |
794 var data = this; | |
795 data.measureWidgets.startb.addClass('dl-drawing'); | |
796 console.debug('measure: onCreateShape', shape); | |
797 }; | |
798 | |
794 // event handler for changeShape | 799 // event handler for changeShape |
795 var onChangeShape = function(event, shape) { | 800 var onChangeShape = function(event, shape) { |
796 var data = this; | 801 var data = this; |
802 updateInfo(data, shape); | |
797 console.debug('measure: onChangeShape', data, shape); | 803 console.debug('measure: onChangeShape', data, shape); |
798 updateInfo(data, shape); | 804 }; |
799 }; | |
800 | 805 |
801 // event handler for renderShape | 806 // event handler for renderShape |
802 var onRenderShape = function(event, shape) { | 807 var onRenderShape = function(event, shape) { |
803 // event handler for updating shape info | 808 // event handler for updating shape info |
804 var shapeInfo = function(event) { | 809 var shapeInfo = function(event) { |
806 }; | 811 }; |
807 var data = this; | 812 var data = this; |
808 var $elem = shape.$elem; | 813 var $elem = shape.$elem; |
809 $elem.on('click.measure', shapeInfo); | 814 $elem.on('click.measure', shapeInfo); |
810 console.debug('measure: onRenderShape', data, shape); | 815 console.debug('measure: onRenderShape', data, shape); |
816 }; | |
817 | |
818 // round to 4 decimal places after point | |
819 var mRound = function (num) { | |
820 return Math.round(num * 10000 + 0.00001) / 10000 | |
811 }; | 821 }; |
812 | 822 |
813 // calculate the distance of the first 2 points of a shape (rectified digilib coords) | 823 // calculate the distance of the first 2 points of a shape (rectified digilib coords) |
814 var rectifiedDist = function(data, shape) { | 824 var rectifiedDist = function(data, shape) { |
815 var coords = shape.geometry.coordinates; | 825 var coords = shape.geometry.coordinates; |
844 var convertLength = function(data, val) { | 854 var convertLength = function(data, val) { |
845 var widgets = data.measureWidgets; | 855 var widgets = data.measureWidgets; |
846 var u1 = parseFloat(widgets.unit1.val()); | 856 var u1 = parseFloat(widgets.unit1.val()); |
847 var u2 = parseFloat(widgets.unit2.val()); | 857 var u2 = parseFloat(widgets.unit2.val()); |
848 return val * u1 / u2; | 858 return val * u1 / u2; |
849 } | 859 }; |
850 | 860 |
851 // convert area to second unit | 861 // convert area to second unit |
852 var convertArea = function(data, val) { | 862 var convertArea = function(data, val) { |
853 var widgets = data.measureWidgets; | 863 var widgets = data.measureWidgets; |
854 var u1 = parseFloat(widgets.unit1.val()); | 864 var u1 = parseFloat(widgets.unit1.val()); |
855 var u2 = parseFloat(widgets.unit2.val()); | 865 var u2 = parseFloat(widgets.unit2.val()); |
856 return val * u1 * u1 / (u2 * u2); | 866 return val * u1 * u1 / (u2 * u2); |
857 } | 867 }; |
858 | 868 |
859 // recalculate factor after entering a new value in input element "value1" | 869 // recalculate factor after entering a new value in input element "value1" |
860 var updateFactor = function(data) { | 870 var changeFactor = function(data) { |
861 var widgets = data.measureWidgets; | 871 var widgets = data.measureWidgets; |
862 var val = parseFloat(widgets.value1.val()); | 872 var val = parseFloat(widgets.value1.val()); |
863 var dist = data.lastMeasuredDistance; | 873 var dist = data.lastMeasuredDistance; |
864 var fac = val / dist; | 874 var fac = val / dist; |
875 data.measureFactor = fac; | |
865 // widgets.fac.text(fn.cropFloatStr(fac)); | 876 // widgets.fac.text(fn.cropFloatStr(fac)); |
866 var conv = convertLength(data, val); | 877 var conv = convertLength(data, val); |
867 widgets.value2.text(fn.cropFloatStr(mRound(conv))); | 878 widgets.value2.text(fn.cropFloatStr(mRound(conv))); |
868 data.lastMeasureFactor = fac; | |
869 data.lastMeasuredValue = val; | |
870 }; | 879 }; |
871 | 880 |
872 // info window for line | 881 // info window for line |
873 var infoLine = function(data, shape) { | 882 var infoLine = function(data, shape) { |
874 var dist = rectifiedDist(data, shape); | 883 var dist = rectifiedDist(data, shape); |
875 var fac = data.lastMeasureFactor; | 884 var fac = data.measureFactor; |
876 var val = dist * fac; | 885 var val = dist * fac; |
877 var conv = convertLength(data, val); | 886 var conv = convertLength(data, val); |
878 var widgets = data.measureWidgets; | 887 var widgets = data.measureWidgets; |
879 widgets.value1.val(fn.cropFloatStr(mRound(val))); | 888 widgets.value1.val(fn.cropFloatStr(mRound(val))); |
880 widgets.value2.text(fn.cropFloatStr(mRound(conv))); | 889 widgets.value2.text(fn.cropFloatStr(mRound(conv))); |
881 widgets.info.text('length'); | 890 widgets.info.text('length'); |
882 data.lastMeasuredValue = val; | |
883 data.lastMeasuredDistance = dist; | 891 data.lastMeasuredDistance = dist; |
884 }; | 892 }; |
885 | 893 |
886 // info window for polygon | 894 // info window for polygon |
887 var infoPolygon = function(data, shape) { | 895 var infoPolygon = function(data, shape) { |
888 var area = rectifiedArea(data, shape); | 896 var area = rectifiedArea(data, shape); |
889 var fac = data.lastMeasureFactor; | 897 var fac = data.measureFactor; |
890 var val = area * fac * fac; | 898 var val = area * fac * fac; |
891 var conv = convertArea(data, val); | 899 var conv = convertArea(data, val); |
892 var widgets = data.measureWidgets; | 900 var widgets = data.measureWidgets; |
893 widgets.value1.val(fn.cropFloatStr(mRound(val))); | 901 widgets.value1.val(fn.cropFloatStr(mRound(val))); |
894 widgets.value2.text(fn.cropFloatStr(mRound(conv))); | 902 widgets.value2.text(fn.cropFloatStr(mRound(conv))); |
902 widgets.value2.text('rect 2'); | 910 widgets.value2.text('rect 2'); |
903 }; | 911 }; |
904 | 912 |
905 // recalculate after measuring | 913 // recalculate after measuring |
906 var updateInfo = function(data, shape) { | 914 var updateInfo = function(data, shape) { |
907 setSelectedShape(data, shape); | |
908 var type = shape.geometry.type; | 915 var type = shape.geometry.type; |
909 console.debug('measure: updateInfo', type); | 916 data.measureWidgets.shape.val(type); |
910 if (type === 'Line') { | 917 if (type === 'Line') { |
911 return infoLine(data, shape); | 918 return infoLine(data, shape); |
912 } | 919 } |
913 if (type === 'LineString') { | 920 if (type === 'LineString') { |
914 return infoLine(data, shape); | 921 return infoLine(data, shape); |
915 } | 922 } |
919 if (type === 'Polygon') { | 926 if (type === 'Polygon') { |
920 return infoPolygon(data, shape); | 927 return infoPolygon(data, shape); |
921 } | 928 } |
922 }; | 929 }; |
923 | 930 |
931 // recalculate after unit change | |
932 var updateConversion = function(data) { | |
933 } | |
924 // return a shape of the currently selected shape type | 934 // return a shape of the currently selected shape type |
925 var getSelectedShape = function(data) { | 935 var newShape = function(data) { |
926 var selection = getSelectedShapeType(data); | 936 return { |
927 var stroke = getSelectedStroke(data); | |
928 var shape = { | |
929 geometry : { | 937 geometry : { |
930 type : selection.type | 938 type : getSelectedShapeType(data) |
931 }, | 939 }, |
932 properties : { | 940 properties : { |
933 stroke : stroke, | 941 stroke : getSelectedStroke(data), |
934 editable : true, | 942 editable : true, |
935 cssclass : 'dl-measure-item' | 943 cssclass : 'dl-measure-item' |
936 } | 944 } |
937 }; | 945 }; |
938 return shape; | 946 }; |
939 }; | 947 |
940 | 948 // return the currently selected shape type |
941 // return a shape of the currently selected shape type | |
942 var setSelectedShape = function(data, shape) { | |
943 var type = shape.geometry.type; | |
944 var types = data.settings.shapeTypes; | |
945 for (var i = 0; i < types.length; i++) { | |
946 if (types[i].type === type) break; | |
947 } | |
948 if (i === types.length) i = 0; | |
949 data.settings.selectedShape = i; | |
950 data.measureWidgets.shape.val(i); | |
951 }; | |
952 | |
953 // return shape type selected by user (on the toolbar) | |
954 var getSelectedShapeType = function(data) { | 949 var getSelectedShapeType = function(data) { |
955 var val = data.measureWidgets.shape.val(); | 950 return data.settings.activeShapeType; |
956 return data.settings.shapeTypes[val]; | 951 }; |
957 }; | 952 |
953 // set the currently selected shape type | |
954 var setActiveShapeType = function(data) { | |
955 data.settings.activeShapeType = data.measureWidgets.shape.val(); | |
956 }; | |
957 | |
958 // set the currently selected shape type | |
959 var changeActiveShapeType = function(data, type) { | |
960 data.settings.activeShapeType = type; | |
961 data.measureWidgets.shape.val(type); | |
962 }; | |
958 | 963 |
959 // return line color chosen by user | 964 // return line color chosen by user |
960 var getSelectedStroke = function(data) { | 965 var getSelectedStroke = function(data) { |
961 // TODO: colorpicker | 966 // TODO: colorpicker |
962 return data.settings.linecolor; | 967 return data.settings.linecolor; |
963 }; | 968 }; |
964 | 969 |
965 // load shapes into select element | 970 // load shapes into select element |
966 var loadShapeTypes = function(data) { | 971 var loadShapeTypes = function(data) { |
967 var $shape = data.measureWidgets.shape; | 972 var $shape = data.measureWidgets.shape; |
968 $.each(data.settings.shapeTypes, function(index, item) { | 973 var shapeTypes = data.settings.shapeTypes; |
969 var $opt = $('<option value="'+ index + '">' + item.name + '</option>'); | 974 var addOption = function(index, type) { |
970 $shape.append($opt); | 975 $shape.append($('<option value="'+ type + '">' + shapeTypes[type].name + '</option>')); |
971 }); | 976 }; |
972 $shape.children(':not(:disabled)')[data.settings.selectedShape].selected = true; | 977 $.each(data.settings.implementedShapes, addOption); |
978 $shape.children()[0].selected = true; | |
973 }; | 979 }; |
974 | 980 |
975 // load units into select elements | 981 // load units into select elements |
976 var loadSections = function(data) { | 982 var loadSections = function(data) { |
977 var widgets = data.measureWidgets; | 983 var widgets = data.measureWidgets; |
996 // initial position of measure bar (bottom left of browser window) | 1002 // initial position of measure bar (bottom left of browser window) |
997 var setScreenPosition = function(data, $div) { | 1003 var setScreenPosition = function(data, $div) { |
998 if ($div == null) return; | 1004 if ($div == null) return; |
999 var h = geom.rectangle($div).height; | 1005 var h = geom.rectangle($div).height; |
1000 var s = fn.getFullscreenRect(data); | 1006 var s = fn.getFullscreenRect(data); |
1001 geom.position(0, s.height - h).adjustDiv($div); | 1007 geom.position(10, s.height - h).adjustDiv($div); |
1002 }; | 1008 }; |
1003 | 1009 |
1004 // drag measureBar around | 1010 // drag measureBar around |
1005 var dragMeasureBar = function(event) { | 1011 var dragMeasureBar = function(event) { |
1006 var $div = $(this).parent(); | 1012 var $div = $(this).parent(); |
1036 // eq1 : $('<span class="dl-measure-label">=</span>'), | 1042 // eq1 : $('<span class="dl-measure-label">=</span>'), |
1037 eq2 : $('<span class="dl-measure-label">=</span>'), | 1043 eq2 : $('<span class="dl-measure-label">=</span>'), |
1038 // len : $('<span id="dl-measure-len" class="dl-measure-number">0.0</span>'), | 1044 // len : $('<span id="dl-measure-len" class="dl-measure-number">0.0</span>'), |
1039 info : $('<span id="dl-measure-shapeinfo" class="dl-measure-label">length</span>'), | 1045 info : $('<span id="dl-measure-shapeinfo" class="dl-measure-label">length</span>'), |
1040 fac : $('<span id="dl-measure-factor" class="dl-measure-number" />'), | 1046 fac : $('<span id="dl-measure-factor" class="dl-measure-number" />'), |
1041 value1 : $('<input id="dl-measure-value1" class="dl-measure-input" title="last measured distance - click to change the value" value="0.0" />'), | 1047 value1 : $('<input id="dl-measure-value1" class="dl-measure-input" title="last measured value - click to change the value" value="0.0" />'), |
1042 value2 : $('<span id="dl-measure-value2" class="dl-measure-label" title="last measured distance, converted to the secondary unit" value="0.0"/>'), | 1048 value2 : $('<span id="dl-measure-value2" class="dl-measure-label" title="last measured value, converted to the secondary unit" value="0.0"/>'), |
1043 unit1 : $('<select id="dl-measure-unit1" title="current measuring unit - click to change" />'), | 1049 unit1 : $('<select id="dl-measure-unit1" title="current measuring unit - click to change" />'), |
1044 unit2 : $('<select id="dl-measure-unit2" title="secondary measuring unit - click to change" />'), | 1050 unit2 : $('<select id="dl-measure-unit2" title="secondary measuring unit - click to change" />'), |
1045 angle : $('<span id="dl-measure-angle" class="dl-measure-number" title="last measured angle" />') | 1051 angle : $('<span id="dl-measure-angle" class="dl-measure-number" title="last measured angle" />') |
1046 }; | 1052 }; |
1047 var $measureBar = $('<div id="dl-measure-toolbar" />'); | 1053 var $measureBar = $('<div id="dl-measure-toolbar" />'); |
1048 $.each(widgets.names, function(index, item) { | 1054 $.each(widgets.names, function(index, item) { |
1049 $measureBar.append(widgets[item]); | 1055 $measureBar.append(widgets[item]); |
1050 }); | 1056 }); |
1051 data.$elem.append($measureBar); | 1057 data.$elem.append($measureBar); |
1052 data.$measureBar = $measureBar; | 1058 data.$measureBar = $measureBar; |
1053 widgets.fac.text(fn.cropFloatStr(data.lastMeasureFactor)); | 1059 widgets.fac.text(fn.cropFloatStr(data.measureFactor)); |
1054 data.measureWidgets = widgets; | 1060 data.measureWidgets = widgets; |
1055 | 1061 |
1056 loadShapeTypes(data); | 1062 loadShapeTypes(data); |
1057 loadSections(data); | 1063 loadSections(data); |
1058 setupMeasureWidgets(data); | 1064 setupMeasureWidgets(data); |
1075 // prevent mousedown event ot bubble up to measureBar (no dragging!) | 1081 // prevent mousedown event ot bubble up to measureBar (no dragging!) |
1076 console.debug('measure: startb mousedown=', action, ' evt=', evt); | 1082 console.debug('measure: startb mousedown=', action, ' evt=', evt); |
1077 $elem.digilib(action); | 1083 $elem.digilib(action); |
1078 return false; | 1084 return false; |
1079 }); | 1085 }); |
1080 widgets.value1.on('change.measure', function(evt) { updateFactor(data) }); | 1086 widgets.shape.on('change.measure', function(evt) { setActiveShapeType(data) }); |
1081 widgets.unit1.on('change.measure', function(evt) { updateInfo(data) }); // TODO: recalculate | 1087 widgets.value1.on('change.measure', function(evt) { changeFactor(data) }); |
1082 widgets.unit2.on('change.measure', function(evt) { updateInfo(data) }); // TODO: recalculate | 1088 widgets.unit1.on('change.measure', function(evt) { updateInfo(data) }); |
1089 widgets.unit2.on('change.measure', function(evt) { updateInfo(data) }); | |
1083 }; | 1090 }; |
1084 | 1091 |
1085 // event handler | 1092 // event handler |
1086 var handleSetup = function (evt) { | 1093 var handleSetup = function (evt) { |
1087 console.debug("measure: handleSetup"); | 1094 console.debug("measure: handleSetup"); |
1088 var data = this; | 1095 var data = this; |
1089 data.lastMeasuredDistance = 0; | 1096 data.lastMeasuredDistance = 0; |
1090 data.lastMeasuredValue = 0; | |
1091 data.lastMeasuredAngle = 0; | 1097 data.lastMeasuredAngle = 0; |
1092 data.lastMeasureFactor = 1.0, | 1098 data.measureFactor = 1.0, |
1093 setupMeasureBar(data); | 1099 setupMeasureBar(data); |
1094 }; | 1100 }; |
1095 | 1101 |
1096 // event handler | 1102 // event handler |
1097 var handleUpdate = function (evt) { | 1103 var handleUpdate = function (evt) { |
1131 FULL_AREA = geom.rectangle(0, 0, 1, 1); | 1137 FULL_AREA = geom.rectangle(0, 0, 1, 1); |
1132 // install event handlers | 1138 // install event handlers |
1133 var $data = $(data); | 1139 var $data = $(data); |
1134 $data.on('setup', handleSetup); | 1140 $data.on('setup', handleSetup); |
1135 $data.on('update', handleUpdate); | 1141 $data.on('update', handleUpdate); |
1142 $data.on('createShape', onCreateShape); | |
1136 $data.on('renderShape', onRenderShape); | 1143 $data.on('renderShape', onRenderShape); |
1137 $data.on('changeShape', onChangeShape); | 1144 $data.on('changeShape', onChangeShape); |
1138 | |
1139 }; | 1145 }; |
1140 | 1146 |
1141 // plugin object with name and init | 1147 // plugin object with name and init |
1142 // shared objects filled by digilib on registration | 1148 // shared objects filled by digilib on registration |
1143 var pluginProperties = { | 1149 var pluginProperties = { |