1 /* 2 Copyright 2008-2023 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 <https://www.gnu.org/licenses/> 29 and <https://opensource.org/licenses/MIT/>. 30 */ 31 32 /*global JXG: true, document:true, jQuery:true, define: true, window: true*/ 33 /*jslint nomen: true, plusplus: true*/ 34 35 /** 36 * @fileoverview The JSXGraph object is defined in this file. JXG.JSXGraph controls all boards. 37 * It has methods to create, save, load and free boards. Additionally some helper functions are 38 * defined in this file directly in the JXG namespace. 39 * 40 */ 41 42 import JXG from "./jxg"; 43 import Env from "./utils/env"; 44 import Type from "./utils/type"; 45 import Mat from "./math/math"; 46 import Board from "./base/board"; 47 import FileReader from "./reader/file"; 48 import Options from "./options"; 49 import SVGRenderer from "./renderer/svg"; 50 import VMLRenderer from "./renderer/vml"; 51 import CanvasRenderer from "./renderer/canvas"; 52 import NoRenderer from "./renderer/no"; 53 54 /** 55 * Constructs a new JSXGraph singleton object. 56 * @class The JXG.JSXGraph singleton stores all properties required 57 * to load, save, create and free a board. 58 */ 59 JXG.JSXGraph = { 60 /** 61 * Stores the renderer that is used to draw the boards. 62 * @type String 63 */ 64 rendererType: (function () { 65 Options.board.renderer = "no"; 66 67 if (Env.supportsVML()) { 68 Options.board.renderer = "vml"; 69 // Ok, this is some real magic going on here. IE/VML always was so 70 // terribly slow, except in one place: Examples placed in a moodle course 71 // was almost as fast as in other browsers. So i grabbed all the css and 72 // lib scripts from our moodle, added them to a jsxgraph example and it 73 // worked. next step was to strip all the css/lib code which didn't affect 74 // the VML update speed. The following five lines are what was left after 75 // the last step and yes - it basically does nothing but reads two 76 // properties of document.body on every mouse move. why? we don't know. if 77 // you know, please let us know. 78 // 79 // If we want to use the strict mode we have to refactor this a little bit. Let's 80 // hope the magic isn't gone now. Anywho... it's only useful in old versions of IE 81 // which should not be used anymore. 82 document.onmousemove = function () { 83 var t; 84 85 if (document.body) { 86 t = document.body.scrollLeft; 87 t += document.body.scrollTop; 88 } 89 90 return t; 91 }; 92 } 93 94 if (Env.supportsCanvas()) { 95 Options.board.renderer = "canvas"; 96 } 97 98 if (Env.supportsSVG()) { 99 Options.board.renderer = "svg"; 100 } 101 102 // we are inside node 103 if (Env.isNode() && Env.supportsCanvas()) { 104 Options.board.renderer = "canvas"; 105 } 106 107 if (Env.isNode() || Options.renderer === "no") { 108 Options.text.display = "internal"; 109 Options.infobox.display = "internal"; 110 } 111 112 return Options.board.renderer; 113 })(), 114 115 /** 116 * Initialize the rendering engine 117 * 118 * @param {String} box id of or reference to the div element which hosts the JSXGraph construction 119 * @param {Object} dim The dimensions of the board 120 * @param {Object} doc Usually, this is document object of the browser window. If false or null, this defaults 121 * to the document object of the browser. 122 * @param {Object} attrRenderer Attribute 'renderer', specifies the rendering engine. Possible values are 'auto', 'svg', 123 * 'canvas', 'no', and 'vml'. 124 * @returns {Object} Reference to the rendering engine object. 125 * @private 126 */ 127 initRenderer: function (box, dim, doc, attrRenderer) { 128 var boxid, renderer; 129 130 // Former version: 131 // doc = doc || document 132 if ((!Type.exists(doc) || doc === false) && typeof document === "object") { 133 doc = document; 134 } 135 136 if (typeof doc === "object" && box !== null) { 137 boxid = (Type.isString(box)) ? doc.getElementById(box) : box; 138 139 // Remove everything from the container before initializing the renderer and the board 140 while (boxid.firstChild) { 141 boxid.removeChild(boxid.firstChild); 142 } 143 } else { 144 boxid = box; 145 } 146 147 // If attrRenderer is not supplied take the first available renderer 148 if (attrRenderer === undefined || attrRenderer === "auto") { 149 attrRenderer = this.rendererType; 150 } 151 // create the renderer 152 if (attrRenderer === "svg") { 153 renderer = new SVGRenderer(boxid, dim); 154 } else if (attrRenderer === "vml") { 155 renderer = new VMLRenderer(boxid); 156 } else if (attrRenderer === "canvas") { 157 renderer = new CanvasRenderer(boxid, dim); 158 } else { 159 renderer = new NoRenderer(); 160 } 161 162 return renderer; 163 }, 164 165 /** 166 * Merge the user supplied attributes with the attributes in options.js 167 * 168 * @param {Object} attributes User supplied attributes 169 * @returns {Object} Merged attributes for the board 170 * 171 * @private 172 */ 173 _setAttributes: function (attributes, options) { 174 // merge attributes 175 var attr = Type.copyAttributes(attributes, options, 'board'), 176 177 // These attributes - which are objects - have to be copied separately. 178 list = [ 179 'drag', 'fullscreen', 180 'intl', 181 'keyboard', 'logging', 182 'navbar', 'pan', 'resize', 183 'screenshot', 'selection', 184 'zoom' 185 ], 186 len = list.length, i, key; 187 188 for (i = 0; i < len; i++) { 189 key = list[i]; 190 attr[key] = Type.copyAttributes(attr, options, 'board', key); 191 } 192 193 // Treat moveTarget separately, because deepCopy will not work here. 194 // Reason: moveTarget will be an HTML node and it is prevented that Type.deepCopy will copy it. 195 attr.movetarget = 196 attributes.moveTarget || attributes.movetarget || options.board.moveTarget; 197 198 return attr; 199 }, 200 201 /** 202 * Further initialization of the board. Set some properties from attribute values. 203 * 204 * @param {JXG.Board} board 205 * @param {Object} attr attributes object 206 * @param {Object} dimensions Object containing dimensions of the canvas 207 * 208 * @private 209 */ 210 _fillBoard: function (board, attr, dimensions) { 211 board.initInfobox(attr.infobox); 212 board.maxboundingbox = attr.maxboundingbox; 213 board.resizeContainer(dimensions.width, dimensions.height, true, true); 214 board._createSelectionPolygon(attr); 215 board.renderer.drawNavigationBar(board, attr.navbar); 216 JXG.boards[board.id] = board; 217 }, 218 219 /** 220 * 221 * @param {String|Object} container id of or reference to the HTML element in which the board is painted. 222 * @param {Object} attr An object that sets some of the board properties. 223 * 224 * @private 225 */ 226 _setARIA: function (container, attr) { 227 var doc = attr.document, 228 doc_glob, 229 node_jsx, 230 newNode, 231 parent, 232 id_label, 233 id_description; 234 235 if (typeof doc !== 'object') { 236 if (!Env.isBrowser) { 237 return; 238 } 239 doc = document; 240 } 241 242 node_jsx = (Type.isString(container)) ? doc.getElementById(container) : container; 243 doc_glob = node_jsx.ownerDocument; // This is the window.document element, needed below. 244 parent = node_jsx.parentNode; 245 246 id_label = container + "_ARIAlabel"; 247 id_description = container + "_ARIAdescription"; 248 249 newNode = doc_glob.createElement("div"); 250 newNode.innerHTML = attr.title; 251 newNode.setAttribute("id", id_label); 252 newNode.style.display = "none"; 253 parent.insertBefore(newNode, node_jsx); 254 255 newNode = doc_glob.createElement("div"); 256 newNode.innerHTML = attr.description; 257 newNode.setAttribute("id", id_description); 258 newNode.style.display = "none"; 259 parent.insertBefore(newNode, node_jsx); 260 261 node_jsx.setAttribute("aria-labelledby", id_label); 262 node_jsx.setAttribute("aria-describedby", id_description); 263 }, 264 265 /** 266 * Remove the two corresponding ARIA divs when freeing a board 267 * 268 * @param {JXG.Board} board 269 * 270 * @private 271 */ 272 _removeARIANodes: function (board) { 273 var node, id, doc; 274 275 doc = board.document || document; 276 if (typeof doc !== "object") { 277 return; 278 } 279 280 id = board.containerObj.getAttribute("aria-labelledby"); 281 node = doc.getElementById(id); 282 if (node && node.parentNode) { 283 node.parentNode.removeChild(node); 284 } 285 id = board.containerObj.getAttribute("aria-describedby"); 286 node = doc.getElementById(id); 287 if (node && node.parentNode) { 288 node.parentNode.removeChild(node); 289 } 290 }, 291 292 /** 293 * Initialize a new board. 294 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 295 * @param {Object} attributes An object that sets some of the board properties. Most of these properties can be set via JXG.Options. 296 * @param {Array} [attributes.boundingbox=[-5, 5, 5, -5]] An array containing four numbers describing the left, top, right and bottom boundary of the board in user coordinates 297 * @param {Boolean} [attributes.keepaspectratio=false] If <tt>true</tt>, the bounding box is adjusted to the same aspect ratio as the aspect ratio of the div containing the board. 298 * @param {Boolean} [attributes.showCopyright=false] Show the copyright string in the top left corner. 299 * @param {Boolean} [attributes.showNavigation=false] Show the navigation buttons in the bottom right corner. 300 * @param {Object} [attributes.zoom] Allow the user to zoom with the mouse wheel or the two-fingers-zoom gesture. 301 * @param {Object} [attributes.pan] Allow the user to pan with shift+drag mouse or two-fingers-pan gesture. 302 * @param {Object} [attributes.drag] Allow the user to drag objects with a pointer device. 303 * @param {Object} [attributes.keyboard] Allow the user to drag objects with arrow keys on keyboard. 304 * @param {Boolean} [attributes.axis=false] If set to true, show the axis. Can also be set to an object that is given to both axes as an attribute object. 305 * @param {Boolean|Object} [attributes.grid] If set to true, shows the grid. Can also be set to an object that is given to the grid as its attribute object. 306 * @param {Boolean} [attributes.registerEvents=true] Register mouse / touch events. 307 * @returns {JXG.Board} Reference to the created board. 308 * 309 * @see JXG.AbstractRenderer#drawNavigationBar 310 */ 311 initBoard: function (box, attributes) { 312 var originX, originY, unitX, unitY, w, h, 313 offX = 0, offY = 0, 314 renderer, dimensions, bbox, 315 attr, axattr, axattr_x, axattr_y, 316 options, 317 theme = {}, 318 board; 319 320 attributes = attributes || {}; 321 // Merge a possible theme 322 if (attributes.theme !== 'default' && Type.exists(JXG.themes[attributes.theme])) { 323 theme = JXG.themes[attributes.theme]; 324 } 325 options = Type.deepCopy(Options, theme, true); 326 attr = this._setAttributes(attributes, options); 327 328 dimensions = Env.getDimensions(box, attr.document); 329 330 if (attr.unitx || attr.unity) { 331 originX = Type.def(attr.originx, 150); 332 originY = Type.def(attr.originy, 150); 333 unitX = Type.def(attr.unitx, 50); 334 unitY = Type.def(attr.unity, 50); 335 } else { 336 bbox = attr.boundingbox; 337 if (bbox[0] < attr.maxboundingbox[0]) { 338 bbox[0] = attr.maxboundingbox[0]; 339 } 340 if (bbox[1] > attr.maxboundingbox[1]) { 341 bbox[1] = attr.maxboundingbox[1]; 342 } 343 if (bbox[2] > attr.maxboundingbox[2]) { 344 bbox[2] = attr.maxboundingbox[2]; 345 } 346 if (bbox[3] < attr.maxboundingbox[3]) { 347 bbox[3] = attr.maxboundingbox[3]; 348 } 349 350 // Size of HTML div. 351 // If zero, the size is set to a small value to avoid 352 // division by zero. 353 w = Math.max(parseInt(dimensions.width, 10), Mat.eps); 354 h = Math.max(parseInt(dimensions.height, 10), Mat.eps); 355 356 if (Type.exists(bbox) && attr.keepaspectratio) { 357 /* 358 * If the boundingbox attribute is given and the ratio of height and width of the 359 * sides defined by the bounding box and the ratio of the dimensions of the div tag 360 * which contains the board do not coincide, then the smaller side is chosen. 361 */ 362 unitX = w / (bbox[2] - bbox[0]); 363 unitY = h / (bbox[1] - bbox[3]); 364 365 if (Math.abs(unitX) < Math.abs(unitY)) { 366 unitY = (Math.abs(unitX) * unitY) / Math.abs(unitY); 367 // Add the additional units in equal portions above and below 368 offY = (h / unitY - (bbox[1] - bbox[3])) * 0.5; 369 } else { 370 unitX = (Math.abs(unitY) * unitX) / Math.abs(unitX); 371 // Add the additional units in equal portions left and right 372 offX = (w / unitX - (bbox[2] - bbox[0])) * 0.5; 373 } 374 } else { 375 unitX = w / (bbox[2] - bbox[0]); 376 unitY = h / (bbox[1] - bbox[3]); 377 } 378 originX = -unitX * (bbox[0] - offX); 379 originY = unitY * (bbox[1] + offY); 380 } 381 382 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 383 this._setARIA(box, attr); 384 385 // Create the board. 386 // board.options will contain the user supplied board attributes 387 board = new Board( 388 box, 389 renderer, 390 attr.id, 391 [originX, originY], 392 /*attr.zoomfactor * */ attr.zoomx, 393 /*attr.zoomfactor * */ attr.zoomy, 394 unitX, 395 unitY, 396 dimensions.width, 397 dimensions.height, 398 attr 399 ); 400 401 board.keepaspectratio = attr.keepaspectratio; 402 403 this._fillBoard(board, attr, dimensions); 404 405 // Create elements like axes, grid, navigation, ... 406 board.suspendUpdate(); 407 attr = board.attr; 408 if (attr.axis) { 409 axattr = typeof attr.axis === "object" ? attr.axis : {}; 410 411 // The defaultAxes attributes are overwritten by user supplied axis object. 412 axattr_x = Type.deepCopy(options.board.defaultaxes.x, axattr); 413 axattr_y = Type.deepCopy(options.board.defaultaxes.y, axattr); 414 415 // The user supplied defaultAxes attributes are merged in. 416 if (attr.defaultaxes.x) { 417 axattr_x = Type.deepCopy(axattr_x, attr.defaultaxes.x); 418 } 419 if (attr.defaultaxes.y) { 420 axattr_y = Type.deepCopy(axattr_y, attr.defaultaxes.y); 421 } 422 423 board.defaultAxes = {}; 424 board.defaultAxes.x = board.create("axis", [[0, 0], [1, 0]], axattr_x); 425 board.defaultAxes.y = board.create("axis", [[0, 0], [0, 1]], axattr_y); 426 } 427 if (attr.grid) { 428 board.create("grid", [], typeof attr.grid === "object" ? attr.grid : {}); 429 } 430 board.unsuspendUpdate(); 431 432 return board; 433 }, 434 435 /** 436 * Load a board from a file containing a construction made with either GEONExT, 437 * Intergeo, Geogebra, or Cinderella. 438 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 439 * @param {String} file base64 encoded string. 440 * @param {String} format containing the file format: 'Geonext' or 'Intergeo'. 441 * @param {Object} attributes Attributes for the board and 'encoding'. 442 * Compressed files need encoding 'iso-8859-1'. Otherwise it probably is 'utf-8'. 443 * @param {Function} callback 444 * @returns {JXG.Board} Reference to the created board. 445 * @see JXG.FileReader 446 * @see JXG.GeonextReader 447 * @see JXG.GeogebraReader 448 * @see JXG.IntergeoReader 449 * @see JXG.CinderellaReader 450 * 451 * @example 452 * // Uncompressed file 453 * var board = JXG.JSXGraph.loadBoardFromFile('jxgbox', 'filename', 'geonext', 454 * {encoding: 'utf-8'}, 455 * function (board) { console.log("Done loading"); } 456 * ); 457 * // Compressed file 458 * var board = JXG.JSXGraph.loadBoardFromFile('jxgbox', 'filename', 'geonext', 459 * {encoding: 'iso-8859-1'}, 460 * function (board) { console.log("Done loading"); } 461 * ); 462 * 463 * @example 464 * // From <input type="file" id="localfile" /> 465 * var file = document.getElementById('localfile').files[0]; 466 * JXG.JSXGraph.loadBoardFromFile('jxgbox', file, 'geonext', 467 * {encoding: 'utf-8'}, 468 * function (board) { console.log("Done loading"); } 469 * ); 470 */ 471 loadBoardFromFile: function (box, file, format, attributes, callback) { 472 var attr, renderer, board, dimensions, encoding; 473 474 attributes = attributes || {}; 475 attr = this._setAttributes(attributes); 476 477 dimensions = Env.getDimensions(box, attr.document); 478 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 479 this._setARIA(box, attr); 480 481 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 482 board = new Board( 483 box, 484 renderer, 485 "", 486 [150, 150], 487 1, 488 1, 489 50, 490 50, 491 dimensions.width, 492 dimensions.height, 493 attr 494 ); 495 this._fillBoard(board, attr, dimensions); 496 encoding = attr.encoding || "iso-8859-1"; 497 FileReader.parseFileContent(file, board, format, true, encoding, callback); 498 499 return board; 500 }, 501 502 /** 503 * Load a board from a base64 encoded string containing a construction made with either GEONExT, 504 * Intergeo, Geogebra, or Cinderella. 505 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 506 * @param {String} string base64 encoded string. 507 * @param {String} format containing the file format: 'Geonext', 'Intergeo', 'Geogebra'. 508 * @param {Object} attributes Attributes for the board and 'encoding'. 509 * Compressed files need encoding 'iso-8859-1'. Otherwise it probably is 'utf-8'. 510 * @param {Function} callback 511 * @returns {JXG.Board} Reference to the created board. 512 * @see JXG.FileReader 513 * @see JXG.GeonextReader 514 * @see JXG.GeogebraReader 515 * @see JXG.IntergeoReader 516 * @see JXG.CinderellaReader 517 */ 518 loadBoardFromString: function (box, string, format, attributes, callback) { 519 var attr, renderer, board, dimensions; 520 521 attributes = attributes || {}; 522 attr = this._setAttributes(attributes); 523 524 dimensions = Env.getDimensions(box, attr.document); 525 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 526 this._setARIA(box, attr); 527 528 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 529 board = new Board( 530 box, 531 renderer, 532 "", 533 [150, 150], 534 1.0, 535 1.0, 536 50, 537 50, 538 dimensions.width, 539 dimensions.height, 540 attr 541 ); 542 this._fillBoard(board, attr, dimensions); 543 FileReader.parseString(string, board, format, true, callback); 544 545 return board; 546 }, 547 548 /** 549 * Delete a board and all its contents. 550 * @param {JXG.Board|String} board id of or reference to the DOM element in which the board is drawn. 551 * 552 */ 553 freeBoard: function (board) { 554 var el; 555 556 if (typeof board === "string") { 557 board = JXG.boards[board]; 558 } 559 560 this._removeARIANodes(board); 561 board.removeEventHandlers(); 562 board.suspendUpdate(); 563 564 // Remove all objects from the board. 565 for (el in board.objects) { 566 if (board.objects.hasOwnProperty(el)) { 567 board.objects[el].remove(); 568 } 569 } 570 571 // Remove all the other things, left on the board, XHTML save 572 while (board.containerObj.firstChild) { 573 board.containerObj.removeChild(board.containerObj.firstChild); 574 } 575 576 // Tell the browser the objects aren't needed anymore 577 for (el in board.objects) { 578 if (board.objects.hasOwnProperty(el)) { 579 delete board.objects[el]; 580 } 581 } 582 583 // Free the renderer and the algebra object 584 delete board.renderer; 585 586 // clear the creator cache 587 board.jc.creator.clearCache(); 588 delete board.jc; 589 590 // Finally remove the board itself from the boards array 591 delete JXG.boards[board.id]; 592 }, 593 594 /** 595 * @deprecated Use JXG#registerElement 596 * @param element 597 * @param creator 598 */ 599 registerElement: function (element, creator) { 600 JXG.deprecated("JXG.JSXGraph.registerElement()", "JXG.registerElement()"); 601 JXG.registerElement(element, creator); 602 } 603 }; 604 605 // JessieScript/JessieCode startup: 606 // Search for script tags of type text/jessiecode and execute them. 607 if (Env.isBrowser && typeof window === 'object' && typeof document === 'object') { 608 Env.addEvent(window, 'load', 609 function () { 610 var type, i, j, div, id, 611 board, txt, width, height, maxWidth, aspectRatio, 612 cssClasses, bbox, axis, grid, code, src, request, 613 postpone = false, 614 615 scripts = document.getElementsByTagName("script"), 616 init = function (code, type, bbox) { 617 var board = JXG.JSXGraph.initBoard(id, { 618 boundingbox: bbox, 619 keepaspectratio: true, 620 grid: grid, 621 axis: axis, 622 showReload: true 623 }); 624 625 if (type.toLowerCase().indexOf("script") > -1) { 626 board.construct(code); 627 } else { 628 try { 629 board.jc.parse(code); 630 } catch (e2) { 631 JXG.debug(e2); 632 } 633 } 634 635 return board; 636 }, 637 makeReload = function (board, code, type, bbox) { 638 return function () { 639 var newBoard; 640 641 JXG.JSXGraph.freeBoard(board); 642 newBoard = init(code, type, bbox); 643 newBoard.reload = makeReload(newBoard, code, type, bbox); 644 }; 645 }; 646 647 for (i = 0; i < scripts.length; i++) { 648 type = scripts[i].getAttribute("type", false); 649 650 if ( 651 Type.exists(type) && 652 (type.toLowerCase() === "text/jessiescript" || 653 type.toLowerCase() === "jessiescript" || 654 type.toLowerCase() === "text/jessiecode" || 655 type.toLowerCase() === "jessiecode") 656 ) { 657 cssClasses = scripts[i].getAttribute("class", false) || ""; 658 width = scripts[i].getAttribute("width", false) || ""; 659 height = scripts[i].getAttribute("height", false) || ""; 660 maxWidth = scripts[i].getAttribute("maxwidth", false) || "100%"; 661 aspectRatio = scripts[i].getAttribute("aspectratio", false) || "1/1"; 662 bbox = scripts[i].getAttribute("boundingbox", false) || "-5, 5, 5, -5"; 663 id = scripts[i].getAttribute("container", false); 664 src = scripts[i].getAttribute("src", false); 665 666 bbox = bbox.split(","); 667 if (bbox.length !== 4) { 668 bbox = [-5, 5, 5, -5]; 669 } else { 670 for (j = 0; j < bbox.length; j++) { 671 bbox[j] = parseFloat(bbox[j]); 672 } 673 } 674 axis = Type.str2Bool(scripts[i].getAttribute("axis", false) || "false"); 675 grid = Type.str2Bool(scripts[i].getAttribute("grid", false) || "false"); 676 677 if (!Type.exists(id)) { 678 id = "jessiescript_autgen_jxg_" + i; 679 div = document.createElement("div"); 680 div.setAttribute("id", id); 681 682 txt = width !== "" ? "width:" + width + ";" : ""; 683 txt += height !== "" ? "height:" + height + ";" : ""; 684 txt += maxWidth !== "" ? "max-width:" + maxWidth + ";" : ""; 685 txt += aspectRatio !== "" ? "aspect-ratio:" + aspectRatio + ";" : ""; 686 687 div.setAttribute("style", txt); 688 div.setAttribute("class", "jxgbox " + cssClasses); 689 try { 690 document.body.insertBefore(div, scripts[i]); 691 } catch (e) { 692 // there's probably jquery involved... 693 if (typeof jQuery === "object") { 694 jQuery(div).insertBefore(scripts[i]); 695 } 696 } 697 } else { 698 div = document.getElementById(id); 699 } 700 701 code = ""; 702 703 if (Type.exists(src)) { 704 postpone = true; 705 request = new XMLHttpRequest(); 706 request.open("GET", src); 707 request.overrideMimeType("text/plain; charset=x-user-defined"); 708 /* jshint ignore:start */ 709 request.addEventListener("load", function () { 710 if (this.status < 400) { 711 code = this.responseText + "\n" + code; 712 board = init(code, type, bbox); 713 board.reload = makeReload(board, code, type, bbox); 714 } else { 715 throw new Error( 716 "\nJSXGraph: failed to load file", 717 src, 718 ":", 719 this.responseText 720 ); 721 } 722 }); 723 request.addEventListener("error", function (e) { 724 throw new Error("\nJSXGraph: failed to load file", src, ":", e); 725 }); 726 /* jshint ignore:end */ 727 request.send(); 728 } else { 729 postpone = false; 730 } 731 732 if (document.getElementById(id)) { 733 code = scripts[i].innerHTML; 734 code = code.replace(/<!\[CDATA\[/g, "").replace(/\]\]>/g, ""); 735 scripts[i].innerHTML = code; 736 737 if (!postpone) { 738 // Do no wait for data from "src" attribute 739 board = init(code, type, bbox); 740 board.reload = makeReload(board, code, type, bbox); 741 } 742 } else { 743 JXG.debug( 744 "JSXGraph: Apparently the div injection failed. Can't create a board, sorry." 745 ); 746 } 747 } 748 } 749 }, 750 window 751 ); 752 } 753 754 export default JXG.JSXGraph; 755