comparison client/digitallibrary/jquery/svg/jquery.digilibSVG.js @ 778:f8235c42f4a0 jquery

more preparations for SVG measuring tool
author hertzhaft
date Sun, 13 Feb 2011 01:12:45 +0100
parents
children d5f47dfaf0ce
comparison
equal deleted inserted replaced
777:34bba748004d 778:f8235c42f4a0
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
120 var $unit = $('<select id="svg-unit"/>');
121 var $conv = $('<select id="svg-convert"/>');
122 for (var i = 0; i < units.length; i++) {
123 var name = units[i].name;
124 var $opt = $('<option value="' + i + '">' + name + '</option>');
125 $unit.append($opt);
126 $conv.append($opt.clone());
127 }
128 // other elements
129 var $px = $('<span id="svg-pixel"/>');
130 var $pxlabel = $('<span id="svg-pixellabel"> px x </span>');
131 var $pxfactor = $('<span id="svg-pixelfactor"/>');
132 $('body').append($toolbar);
133 $toolbar.append($shape);
134 $toolbar.append($px);
135 $toolbar.append($pxlabel);
136 $toolbar.append($pxfactor);
137 $toolbar.append($unit);
138 $toolbar.append($conv);
139 };
140
141 var actions = {
142 "init" : function(options) {
143 var $digilib = this;
144 var settings = $.extend({}, defaults, options);
145 // prepare the AJAX call back
146 var onLoadXML = function (xml) {
147 settings.xml = xml;
148 setupToolBar(settings);
149 $digilib.each(function() {
150 var $elem = $(this);
151 $elem.data(pluginName, settings);
152 });
153 };
154 // fetch the XML measuring unit list
155 $.ajax({
156 type : "GET",
157 url : "svg/archimedes.xml",
158 dataType : "xml",
159 success : onLoadXML
160 });
161 return this;
162 }
163
164 };
165
166 // hook plugin into jquery
167 $.fn[pluginName] = function(action) {
168 if (actions[action]) {
169 // call action on this with the remaining arguments (inserting data as first argument)
170 var $elem = $(this);
171 var data = $elem.data('digilib');
172 if (!data) {
173 return $.error(pluginName + ' action ' + action + ' needs a digilib element');
174 }
175 var args = Array.prototype.slice.call(arguments, 1);
176 args.unshift(data);
177 return actions[action].apply(this, args);
178 } else if (typeof(action) === 'object' || !action) {
179 // call init on this
180 return actions.init.apply(this, arguments);
181 } else {
182 $.error('action ' + action + ' does not exist on jQuery.' + pluginName);
183 }
184 };
185
186 })(jQuery);
187