1 /* 2 Copyright 2008-2011 3 Matthias Ehmann, 4 Michael Gerhaeuser, 5 Carsten Miller, 6 Bianca Valentin, 7 Alfred Wassermann, 8 Peter Wilfahrt 9 10 This file is part of JSXGraph. 11 12 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 13 14 You can redistribute it and/or modify it under the terms of the 15 16 * GNU Lesser General Public License as published by 17 the Free Software Foundation, either version 3 of the License, or 18 (at your option) any later version 19 OR 20 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 21 22 JSXGraph is distributed in the hope that it will be useful, 23 but WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 GNU Lesser General Public License for more details. 26 27 You should have received a copy of the GNU Lesser General Public License and 28 the MIT License along with JSXGraph. If not, see <http://www.gnu.org/licenses/> 29 and <http://opensource.org/licenses/MIT/>. 30 */ 31 32 33 /*global JXG: true, document:true, jQuery:true, define: true, window: true*/ 34 /*jslint nomen: true, plusplus: true*/ 35 36 /* depends: 37 jxg 38 utils/env 39 utils/type 40 base/board 41 reader/file 42 options 43 renderer/svg 44 renderer/vml 45 renderer/canvas 46 renderer/no 47 */ 48 49 /** 50 * @fileoverview The JSXGraph object is defined in this file. JXG.JSXGraph controls all boards. 51 * It has methods to create, save, load and free boards. Additionally some helper functions are 52 * defined in this file directly in the JXG namespace. 53 * @version 0.99 54 */ 55 56 define([ 57 'jxg', 'utils/env', 'utils/type', 'base/board', 'reader/file', 'options', 58 'renderer/svg', 'renderer/vml', 'renderer/canvas', 'renderer/no' 59 ], function (JXG, Env, Type, Board, FileReader, Options, SVGRenderer, VMLRenderer, CanvasRenderer, NoRenderer) { 60 61 "use strict"; 62 63 /** 64 * Constructs a new JSXGraph singleton object. 65 * @class The JXG.JSXGraph singleton stores all properties required 66 * to load, save, create and free a board. 67 */ 68 JXG.JSXGraph = { 69 /** 70 * Stores the renderer that is used to draw the boards. 71 * @type String 72 */ 73 rendererType: (function () { 74 Options.board.renderer = 'no'; 75 76 if (Env.supportsVML()) { 77 Options.board.renderer = 'vml'; 78 // Ok, this is some real magic going on here. IE/VML always was so 79 // terribly slow, except in one place: Examples placed in a moodle course 80 // was almost as fast as in other browsers. So i grabbed all the css and 81 // lib scripts from our moodle, added them to a jsxgraph example and it 82 // worked. next step was to strip all the css/lib code which didn't affect 83 // the VML update speed. The following five lines are what was left after 84 // the last step and yes - it basically does nothing but reads two 85 // properties of document.body on every mouse move. why? we don't know. if 86 // you know, please let us know. 87 // 88 // If we want to use the strict mode we have to refactor this a little bit. Let's 89 // hope the magic isn't gone now. Anywho... it's only useful in old versions of IE 90 // which should not be used anymore. 91 document.onmousemove = function () { 92 var t; 93 94 if (document.body) { 95 t = document.body.scrollLeft; 96 t += document.body.scrollTop; 97 } 98 99 return t; 100 }; 101 } 102 103 if (Env.supportsCanvas()) { 104 Options.board.renderer = 'canvas'; 105 } 106 107 if (Env.supportsSVG()) { 108 Options.board.renderer = 'svg'; 109 } 110 111 // we are inside node 112 if (Env.isNode() && Env.supportsCanvas()) { 113 Options.board.renderer = 'canvas'; 114 } 115 116 if (Env.isNode() || Options.renderer === 'no') { 117 Options.text.display = 'internal'; 118 Options.infobox.display = 'internal'; 119 } 120 121 return Options.board.renderer; 122 }()), 123 124 initRenderer: function (box, dim, doc, attrRenderer) { 125 var boxid, renderer; 126 127 // Former version: 128 // doc = doc || document 129 if ((!Type.exists(doc) || doc === false) && typeof document === 'object') { 130 doc = document; 131 } 132 133 if (typeof doc === 'object' && box !== null) { 134 boxid = doc.getElementById(box); 135 136 // Remove everything from the container before initializing the renderer and the board 137 while (boxid.firstChild) { 138 boxid.removeChild(boxid.firstChild); 139 } 140 } else { 141 boxid = box; 142 } 143 144 // create the renderer 145 if (attrRenderer === 'svg') { 146 renderer = new SVGRenderer(boxid, dim); 147 } else if (attrRenderer === 'vml') { 148 renderer = new VMLRenderer(boxid); 149 } else if (attrRenderer === 'canvas') { 150 renderer = new CanvasRenderer(boxid, dim); 151 } else { 152 renderer = new NoRenderer(); 153 } 154 155 return renderer; 156 }, 157 158 /** 159 * Initialise a new board. 160 * @param {String} box Html-ID to the Html-element in which the board is painted. 161 * @param {Object} attributes An object that sets some of the board properties. Most of these properties can be set via JXG.Options. Valid properties are 162 * <ul> 163 * <li><b>boundingbox</b>: An array containing four numbers describing the left, top, right and bottom boundary of the board in user coordinates</li> 164 * <li><b>keepaspectratio</b>: If <tt>true</tt>, the bounding box is adjusted to the same aspect ratio as the aspect ratio of the div containing the board.</li> 165 * <li><b>showCopyright</b>: Show the copyright string in the top left corner.</li> 166 * <li><b>showNavigation</b>: Show the navigation buttons in the bottom right corner.</li> 167 * <li><b>zoom</b>: Allow the user to zoom with the mouse wheel or the two-fingers-zoom gesture.</li> 168 * <li><b>pan</b>: Allow the user to pan with shift+drag mouse or two-fingers-pan gesture.</li> 169 * <li><b>axis</b>: If set to true, show the axis. Can also be set to an object that is given to both axes as an attribute object.</li> 170 * <li><b>grid</b>: If set to true, shows the grid. Can also bet set to an object that is given to the grid as its attribute object.</li> 171 * <li><b>registerEvents</b>: Register mouse / touch events.</li> 172 * </ul> 173 * @returns {JXG.Board} Reference to the created board. 174 */ 175 initBoard: function (box, attributes) { 176 var originX, originY, unitX, unitY, 177 renderer, 178 w, h, dimensions, 179 bbox, attr, axattr, 180 selectionattr, 181 board; 182 183 attributes = attributes || {}; 184 185 // merge attributes 186 attr = Type.copyAttributes(attributes, Options, 'board'); 187 attr.zoom = Type.copyAttributes(attr, Options, 'board', 'zoom'); 188 attr.pan = Type.copyAttributes(attr, Options, 'board', 'pan'); 189 attr.selection = Type.copyAttributes(attr, Options, 'board', 'selection'); 190 191 dimensions = Env.getDimensions(box, attr.document); 192 193 if (attr.unitx || attr.unity) { 194 originX = Type.def(attr.originx, 150); 195 originY = Type.def(attr.originy, 150); 196 unitX = Type.def(attr.unitx, 50); 197 unitY = Type.def(attr.unity, 50); 198 } else { 199 bbox = attr.boundingbox; 200 w = parseInt(dimensions.width, 10); 201 h = parseInt(dimensions.height, 10); 202 203 if (Type.exists(bbox) && attr.keepaspectratio) { 204 /* 205 * If the boundingbox attribute is given and the ratio of height and width of the 206 * sides defined by the bounding box and the ratio of the dimensions of the div tag 207 * which contains the board do not coincide, then the smaller side is chosen. 208 */ 209 unitX = w / (bbox[2] - bbox[0]); 210 unitY = h / (bbox[1] - bbox[3]); 211 212 if (Math.abs(unitX) < Math.abs(unitY)) { 213 unitY = Math.abs(unitX) * unitY / Math.abs(unitY); 214 } else { 215 unitX = Math.abs(unitY) * unitX / Math.abs(unitX); 216 } 217 } else { 218 unitX = w / (bbox[2] - bbox[0]); 219 unitY = h / (bbox[1] - bbox[3]); 220 } 221 originX = -unitX * bbox[0]; 222 originY = unitY * bbox[1]; 223 } 224 225 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 226 227 // create the board 228 board = new Board(box, renderer, attr.id, [originX, originY], attr.zoomfactor * attr.zoomx, attr.zoomfactor * attr.zoomy, unitX, unitY, dimensions.width, dimensions.height, attr); 229 230 JXG.boards[board.id] = board; 231 232 board.keepaspectratio = attr.keepaspectratio; 233 board.resizeContainer(dimensions.width, dimensions.height, true, true); 234 235 // create elements like axes, grid, navigation, ... 236 board.suspendUpdate(); 237 board.initInfobox(); 238 239 if (attr.axis) { 240 axattr = typeof attr.axis === 'object' ? attr.axis : {ticks: {drawZero: true}}; 241 board.defaultAxes = {}; 242 board.defaultAxes.x = board.create('axis', [[0, 0], [1, 0]], axattr); 243 board.defaultAxes.y = board.create('axis', [[0, 0], [0, 1]], axattr); 244 } 245 246 if (attr.grid) { 247 board.create('grid', [], (typeof attr.grid === 'object' ? attr.grid : {})); 248 } 249 250 board._createSelectionPolygon(attr); 251 /* 252 selectionattr = Type.copyAttributes(attr, Options, 'board', 'selection'); 253 if (selectionattr.enabled === true) { 254 board.selectionPolygon = board.create('polygon', [[0, 0], [0, 0], [0, 0], [0, 0]], selectionattr); 255 } 256 */ 257 258 board.renderer.drawZoomBar(board); 259 board.unsuspendUpdate(); 260 261 return board; 262 }, 263 264 /** 265 * Load a board from a file containing a construction made with either GEONExT, 266 * Intergeo, Geogebra, or Cinderella. 267 * @param {String} box HTML-ID to the HTML-element in which the board is painted. 268 * @param {String} file base64 encoded string. 269 * @param {String} format containing the file format: 'Geonext' or 'Intergeo'. 270 * @param {Object} [attributes] 271 * @returns {JXG.Board} Reference to the created board. 272 * @see JXG.FileReader 273 * @see JXG.GeonextReader 274 * @see JXG.GeogebraReader 275 * @see JXG.IntergeoReader 276 * @see JXG.CinderellaReader 277 */ 278 loadBoardFromFile: function (box, file, format, attributes, callback) { 279 var attr, renderer, board, dimensions, 280 selectionattr; 281 282 attributes = attributes || {}; 283 284 // merge attributes 285 attr = Type.copyAttributes(attributes, Options, 'board'); 286 attr.zoom = Type.copyAttributes(attributes, Options, 'board', 'zoom'); 287 attr.pan = Type.copyAttributes(attributes, Options, 'board', 'pan'); 288 attr.selection = Type.copyAttributes(attr, Options, 'board', 'selection'); 289 290 dimensions = Env.getDimensions(box, attr.document); 291 renderer = this.initRenderer(box, dimensions, attr.document); 292 293 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 294 board = new Board(box, renderer, '', [150, 150], 1, 1, 50, 50, dimensions.width, dimensions.height, attr); 295 board.initInfobox(); 296 board.resizeContainer(dimensions.width, dimensions.height, true, true); 297 298 FileReader.parseFileContent(file, board, format, true, callback); 299 300 selectionattr = Type.copyAttributes(attr, Options, 'board', 'selection'); 301 board.selectionPolygon = board.create('polygon', [[0, 0], [0, 0], [0, 0], [0, 0]], selectionattr); 302 303 board.renderer.drawZoomBar(board); 304 JXG.boards[board.id] = board; 305 306 return board; 307 }, 308 309 /** 310 * Load a board from a base64 encoded string containing a construction made with either GEONExT, 311 * Intergeo, Geogebra, or Cinderella. 312 * @param {String} box HTML-ID to the HTML-element in which the board is painted. 313 * @param {String} string base64 encoded string. 314 * @param {String} format containing the file format: 'Geonext' or 'Intergeo'. 315 * @param {Object} [attributes] 316 * @returns {JXG.Board} Reference to the created board. 317 * @see JXG.FileReader 318 * @see JXG.GeonextReader 319 * @see JXG.GeogebraReader 320 * @see JXG.IntergeoReader 321 * @see JXG.CinderellaReader 322 */ 323 loadBoardFromString: function (box, string, format, attributes, callback) { 324 var attr, renderer, dimensions, board, 325 selectionattr; 326 327 attributes = attributes || {}; 328 329 // merge attributes 330 attr = Type.copyAttributes(attributes, Options, 'board'); 331 attr.zoom = Type.copyAttributes(attributes, Options, 'board', 'zoom'); 332 attr.pan = Type.copyAttributes(attributes, Options, 'board', 'pan'); 333 attr.selection = Type.copyAttributes(attr, Options, 'board', 'selection'); 334 335 dimensions = Env.getDimensions(box, attr.document); 336 renderer = this.initRenderer(box, dimensions, attr.document); 337 338 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 339 board = new Board(box, renderer, '', [150, 150], 1.0, 1.0, 50, 50, dimensions.width, dimensions.height, attr); 340 board.initInfobox(); 341 board.resizeContainer(dimensions.width, dimensions.height, true, true); 342 343 FileReader.parseString(string, board, format, true, callback); 344 345 selectionattr = Type.copyAttributes(attr, Options, 'board', 'selection'); 346 board.selectionPolygon = board.create('polygon', [[0, 0], [0, 0], [0, 0], [0, 0]], selectionattr); 347 348 board.renderer.drawZoomBar(board); 349 JXG.boards[board.id] = board; 350 351 return board; 352 }, 353 354 /** 355 * Delete a board and all its contents. 356 * @param {JXG.Board,String} board HTML-ID to the DOM-element in which the board is drawn. 357 */ 358 freeBoard: function (board) { 359 var el; 360 361 if (typeof board === 'string') { 362 board = JXG.boards[board]; 363 } 364 365 board.removeEventHandlers(); 366 board.suspendUpdate(); 367 368 // Remove all objects from the board. 369 for (el in board.objects) { 370 if (board.objects.hasOwnProperty(el)) { 371 board.objects[el].remove(); 372 } 373 } 374 375 // Remove all the other things, left on the board, XHTML save 376 while (board.containerObj.firstChild) { 377 board.containerObj.removeChild(board.containerObj.firstChild); 378 } 379 380 // Tell the browser the objects aren't needed anymore 381 for (el in board.objects) { 382 if (board.objects.hasOwnProperty(el)) { 383 delete board.objects[el]; 384 } 385 } 386 387 // Free the renderer and the algebra object 388 delete board.renderer; 389 390 // clear the creator cache 391 board.jc.creator.clearCache(); 392 delete board.jc; 393 394 // Finally remove the board itself from the boards array 395 delete JXG.boards[board.id]; 396 }, 397 398 /** 399 * @deprecated Use JXG#registerElement 400 * @param element 401 * @param creator 402 */ 403 registerElement: function (element, creator) { 404 JXG.deprecated('JXG.JSXGraph.registerElement()', 'JXG.registerElement()'); 405 JXG.registerElement(element, creator); 406 } 407 }; 408 409 // JessieScript/JessieCode startup: Search for script tags of type text/jessiescript and interprete them. 410 if (Env.isBrowser && typeof window === 'object' && typeof document === 'object') { 411 Env.addEvent(window, 'load', function () { 412 var type, i, j, div, id, board, width, height, bbox, axis, grid, code, 413 scripts = document.getElementsByTagName('script'), 414 init = function (code, type, bbox) { 415 var board = JXG.JSXGraph.initBoard(id, {boundingbox: bbox, keepaspectratio: true, grid: grid, axis: axis, showReload: true}); 416 417 if (type.toLowerCase().indexOf('script') > -1) { 418 board.construct(code); 419 } else { 420 try { 421 board.jc.parse(code); 422 } catch (e2) { 423 JXG.debug(e2); 424 } 425 } 426 427 return board; 428 }, 429 makeReload = function (board, code, type, bbox) { 430 return function () { 431 var newBoard; 432 433 JXG.JSXGraph.freeBoard(board); 434 newBoard = init(code, type, bbox); 435 newBoard.reload = makeReload(newBoard, code, type, bbox); 436 }; 437 }; 438 439 for (i = 0; i < scripts.length; i++) { 440 type = scripts[i].getAttribute('type', false); 441 442 if (Type.exists(type) && (type.toLowerCase() === 'text/jessiescript' || type.toLowerCase() === 'jessiescript' || type.toLowerCase() === 'text/jessiecode' || type.toLowerCase() === 'jessiecode')) { 443 width = scripts[i].getAttribute('width', false) || '500px'; 444 height = scripts[i].getAttribute('height', false) || '500px'; 445 bbox = scripts[i].getAttribute('boundingbox', false) || '-5, 5, 5, -5'; 446 id = scripts[i].getAttribute('container', false); 447 448 bbox = bbox.split(','); 449 if (bbox.length !== 4) { 450 bbox = [-5, 5, 5, -5]; 451 } else { 452 for (j = 0; j < bbox.length; j++) { 453 bbox[j] = parseFloat(bbox[j]); 454 } 455 } 456 axis = Type.str2Bool(scripts[i].getAttribute('axis', false) || 'false'); 457 grid = Type.str2Bool(scripts[i].getAttribute('grid', false) || 'false'); 458 459 if (!Type.exists(id)) { 460 id = 'jessiescript_autgen_jxg_' + i; 461 div = document.createElement('div'); 462 div.setAttribute('id', id); 463 div.setAttribute('style', 'width:' + width + '; height:' + height + '; float:left'); 464 div.setAttribute('class', 'jxgbox'); 465 try { 466 document.body.insertBefore(div, scripts[i]); 467 } catch (e) { 468 // there's probably jquery involved... 469 if (typeof jQuery === 'object') { 470 jQuery(div).insertBefore(scripts[i]); 471 } 472 } 473 } else { 474 div = document.getElementById(id); 475 } 476 477 if (document.getElementById(id)) { 478 code = scripts[i].innerHTML; 479 code = code.replace(/<!\[CDATA\[/g, '').replace(/\]\]>/g, ''); 480 scripts[i].innerHTML = code; 481 482 board = init(code, type, bbox); 483 board.reload = makeReload(board, code, type, bbox); 484 } else { 485 JXG.debug('JSXGraph: Apparently the div injection failed. Can\'t create a board, sorry.'); 486 } 487 } 488 } 489 }, window); 490 } 491 492 return JXG.JSXGraph; 493 }); 494