778
|
1 /* Copyright (c) 2011 Martin Raspe, Robert Casties
|
|
2
|
|
3 This program is free software: you can redistribute it and/or modify
|
|
4 it under the terms of the GNU Lesser General Public License as published by
|
|
5 the Free Software Foundation, either version 2 of the License, or
|
|
6 (at your option) any later version.
|
|
7
|
|
8 This program is distributed in the hope that it will be useful,
|
|
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 GNU Lesser General Public License for more details.
|
|
12
|
|
13 You should have received a copy of the GNU Lesser General Public License
|
|
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
15
|
|
16 Authors:
|
|
17 Martin Raspe, Robert Casties, 9.2.2011
|
|
18 */
|
|
19
|
|
20 /**
|
|
21 * digilib SVG plugin (measuring tool for use within the digilib jQuery plugin)
|
|
22 **/
|
|
23
|
|
24
|
|
25 /* jslint browser: true, debug: true, forin: true
|
|
26 */
|
|
27
|
|
28 // fallback for console.log calls
|
|
29 if (typeof(console) === 'undefined') {
|
|
30 var console = {
|
|
31 log : function(){},
|
|
32 debug : function(){},
|
|
33 error : function(){}
|
|
34 };
|
|
35 var customConsole = true;
|
|
36 }
|
|
37
|
|
38 (function($) {
|
|
39 console.debug('installing jquery.digilibSVG');
|
|
40 var pluginName = 'digilibSVG';
|
|
41 var defaults = {
|
|
42 // choice of colors offered by toolbar
|
|
43 lineColors : ['white', 'red', 'yellow', 'green', 'blue', 'black'],
|
|
44 // default color
|
|
45 lineColor : 'white',
|
|
46 // color while the line is drawn
|
|
47 drawColor : 'green',
|
|
48 // color of selected objects
|
|
49 selectColor : 'red',
|
|
50 // drawing shapes
|
|
51 shapes : ['line', 'polyline', 'rectangle', 'square', 'circle', 'arch',
|
|
52 'ratio', 'intercolumn', 'line grid'],
|
|
53 // default shape
|
|
54 shape : 'line',
|
|
55 // measuring unit (index into list)
|
|
56 unit : 1,
|
|
57 // converted unit (index into list)
|
|
58 converted : 2,
|
|
59 // last measured distance
|
|
60 lastDistance : 0,
|
|
61 // last measured angle
|
|
62 lastAngle : 0,
|
|
63 // maximal denominator for mixed fractions
|
|
64 maxDenominator : 20,
|
|
65 // number of decimal places for convert results
|
|
66 maxDecimals : 3,
|
|
67 // show convert result as mixed fraction?
|
|
68 showMixedFraction : false,
|
|
69 // show angle relative to last line?
|
|
70 showRelativeAngle : false,
|
|
71 // show distance numbers?
|
|
72 showDistanceNumbers : true,
|
|
73 // show ratio of rectangle sides?
|
|
74 showRectangleRatios : false,
|
|
75 // draw line ends as small crosses
|
|
76 drawEndPoints : true,
|
|
77 // draw mid points of lines
|
|
78 drawMidPoints : false,
|
|
79 // draw circle centers
|
|
80 drawCenters : false,
|
|
81 // draw rectangles from the diagonal and one point
|
|
82 drawFromDiagonal : false,
|
|
83 // draw circles from center
|
|
84 drawFromCenter : false,
|
|
85 // snap to endpoints
|
|
86 snapEndPoints : false,
|
|
87 // snap to mid points of lines
|
|
88 snapMidPoints : false,
|
|
89 // snap to circle centers
|
|
90 snapCenters : false,
|
|
91 // keep original object when moving/scaling/rotating
|
|
92 keepOriginal : false,
|
|
93 // number of copies when drawing grids
|
|
94 gridCopies : 10
|
|
95 };
|
|
96
|
|
97 // setup a div for accessing the main SVG functionality
|
|
98 var setupToolBar = function(settings) {
|
|
99 var $toolbar = $('<div id="svg-toolbar"/>');
|
|
100 // shapes select
|
|
101 var $shape = $('<select id="svg-shapes"/>');
|
|
102 for (var i = 0; i < settings.shapes.length; i++) {
|
|
103 var name = settings.shapes[i];
|
|
104 var $opt = $('<option value="' + i + '">' + name + '</option>');
|
|
105 $shape.append($opt);
|
|
106 }
|
|
107 // console.debug($xml);
|
|
108 var $xml = $(settings.xml);
|
|
109 var units = [];
|
|
110 $xml.find("unit").each(function() {
|
|
111 units.push({
|
|
112 'name' : $(this).attr("name"),
|
|
113 'factor' : $(this).attr("factor"),
|
|
114 'add' : $(this).attr("add"),
|
|
115 'subunits' : $(this).attr("subunits")
|
|
116 });
|
|
117 });
|
|
118 settings.units = units;
|
|
119 // unit selects
|
781
|
120 var $unit1 = $('<select id="svg-convert1"/>');
|
|
121 var $unit2 = $('<select id="svg-convert2"/>');
|
778
|
122 for (var i = 0; i < units.length; i++) {
|
|
123 var name = units[i].name;
|
|
124 var $opt = $('<option value="' + i + '">' + name + '</option>');
|
781
|
125 $unit1.append($opt);
|
|
126 $unit2.append($opt.clone());
|
778
|
127 }
|
|
128 // other elements
|
781
|
129 var $la1 = $('<span class="svg-label">pixel</span>');
|
|
130 var $la2 = $('<span class="svg-label">factor</span>');
|
|
131 var $la3 = $('<span class="svg-label">=</span>');
|
|
132 var $la4 = $('<span class="svg-label">=</span>');
|
|
133 var $px = $('<span id="svg-pixel" class="svg-number">0.0</span>');
|
|
134 var $factor = $('<span id="svg-factor" class="svg-number">0.0</span>');
|
|
135 var $result1 = $('<input id="svg-unit1" class="svg-input" value="0.0"/>');
|
|
136 var $result2 = $('<input id="svg-unit2" class="svg-input" value="0.0"/>');
|
|
137 var $angle = $('<span id="svg-angle" class="svg-number">0.0</span>');
|
778
|
138 $('body').append($toolbar);
|
|
139 $toolbar.append($shape);
|
781
|
140 $toolbar.append($la1);
|
778
|
141 $toolbar.append($px);
|
781
|
142 $toolbar.append($la2);
|
|
143 $toolbar.append($factor);
|
|
144 $toolbar.append($la3);
|
|
145 $toolbar.append($result1);
|
|
146 $toolbar.append($unit1);
|
|
147 $toolbar.append($la4);
|
|
148 $toolbar.append($result2);
|
|
149 $toolbar.append($unit2);
|
|
150 $toolbar.append($angle);
|
778
|
151 };
|
|
152
|
|
153 var actions = {
|
|
154 "init" : function(options) {
|
|
155 var $digilib = this;
|
|
156 var settings = $.extend({}, defaults, options);
|
|
157 // prepare the AJAX call back
|
|
158 var onLoadXML = function (xml) {
|
|
159 settings.xml = xml;
|
|
160 setupToolBar(settings);
|
|
161 $digilib.each(function() {
|
|
162 var $elem = $(this);
|
|
163 $elem.data(pluginName, settings);
|
|
164 });
|
|
165 };
|
|
166 // fetch the XML measuring unit list
|
|
167 $.ajax({
|
|
168 type : "GET",
|
|
169 url : "svg/archimedes.xml",
|
|
170 dataType : "xml",
|
|
171 success : onLoadXML
|
|
172 });
|
|
173 return this;
|
|
174 }
|
|
175
|
|
176 };
|
|
177
|
|
178 // hook plugin into jquery
|
|
179 $.fn[pluginName] = function(action) {
|
|
180 if (actions[action]) {
|
|
181 // call action on this with the remaining arguments (inserting data as first argument)
|
|
182 var $elem = $(this);
|
|
183 var data = $elem.data('digilib');
|
|
184 if (!data) {
|
|
185 return $.error(pluginName + ' action ' + action + ' needs a digilib element');
|
|
186 }
|
|
187 var args = Array.prototype.slice.call(arguments, 1);
|
|
188 args.unshift(data);
|
|
189 return actions[action].apply(this, args);
|
|
190 } else if (typeof(action) === 'object' || !action) {
|
|
191 // call init on this
|
|
192 return actions.init.apply(this, arguments);
|
|
193 } else {
|
|
194 $.error('action ' + action + ' does not exist on jQuery.' + pluginName);
|
|
195 }
|
|
196 };
|
|
197
|
|
198 })(jQuery);
|
|
199
|