1 /* 2 Copyright 2008-2024 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.js"; 43 import Env from "./utils/env.js"; 44 import Type from "./utils/type.js"; 45 // import Mat from "./math/math.js"; 46 import Board from "./base/board.js"; 47 import FileReader from "./reader/file.js"; 48 import Options from "./options.js"; 49 import SVGRenderer from "./renderer/svg.js"; 50 import VMLRenderer from "./renderer/vml.js"; 51 import CanvasRenderer from "./renderer/canvas.js"; 52 import NoRenderer from "./renderer/no.js"; 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 w = parseInt(dimensions.width, 10); 356 h = parseInt(dimensions.height, 10); 357 358 if (Type.exists(bbox) && attr.keepaspectratio) { 359 /* 360 * If the boundingbox attribute is given and the ratio of height and width of the 361 * sides defined by the bounding box and the ratio of the dimensions of the div tag 362 * which contains the board do not coincide, then the smaller side is chosen. 363 */ 364 unitX = w / (bbox[2] - bbox[0]); 365 unitY = h / (bbox[1] - bbox[3]); 366 367 if (Math.abs(unitX) < Math.abs(unitY)) { 368 unitY = (Math.abs(unitX) * unitY) / Math.abs(unitY); 369 // Add the additional units in equal portions above and below 370 offY = (h / unitY - (bbox[1] - bbox[3])) * 0.5; 371 } else { 372 unitX = (Math.abs(unitY) * unitX) / Math.abs(unitX); 373 // Add the additional units in equal portions left and right 374 offX = (w / unitX - (bbox[2] - bbox[0])) * 0.5; 375 } 376 } else { 377 unitX = w / (bbox[2] - bbox[0]); 378 unitY = h / (bbox[1] - bbox[3]); 379 } 380 originX = -unitX * (bbox[0] - offX); 381 originY = unitY * (bbox[1] + offY); 382 } 383 384 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 385 this._setARIA(box, attr); 386 387 // Create the board. 388 // board.options will contain the user supplied board attributes 389 board = new Board( 390 box, 391 renderer, 392 attr.id, 393 [originX, originY], 394 /*attr.zoomfactor * */ attr.zoomx, 395 /*attr.zoomfactor * */ attr.zoomy, 396 unitX, 397 unitY, 398 dimensions.width, 399 dimensions.height, 400 attr 401 ); 402 403 board.keepaspectratio = attr.keepaspectratio; 404 405 this._fillBoard(board, attr, dimensions); 406 407 // Create elements like axes, grid, navigation, ... 408 board.suspendUpdate(); 409 attr = board.attr; 410 if (attr.axis) { 411 axattr = typeof attr.axis === "object" ? attr.axis : {}; 412 413 // The defaultAxes attributes are overwritten by user supplied axis object. 414 axattr_x = Type.deepCopy(options.board.defaultaxes.x, axattr); 415 axattr_y = Type.deepCopy(options.board.defaultaxes.y, axattr); 416 417 // The user supplied defaultAxes attributes are merged in. 418 if (attr.defaultaxes.x) { 419 axattr_x = Type.deepCopy(axattr_x, attr.defaultaxes.x); 420 } 421 if (attr.defaultaxes.y) { 422 axattr_y = Type.deepCopy(axattr_y, attr.defaultaxes.y); 423 } 424 425 board.defaultAxes = {}; 426 board.defaultAxes.x = board.create("axis", [[0, 0], [1, 0]], axattr_x); 427 board.defaultAxes.y = board.create("axis", [[0, 0], [0, 1]], axattr_y); 428 } 429 if (attr.grid) { 430 board.create("grid", [], typeof attr.grid === "object" ? attr.grid : {}); 431 } 432 board.unsuspendUpdate(); 433 434 return board; 435 }, 436 437 /** 438 * Load a board from a file containing a construction made with either GEONExT, 439 * Intergeo, Geogebra, or Cinderella. 440 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 441 * @param {String} file base64 encoded string. 442 * @param {String} format containing the file format: 'Geonext' or 'Intergeo'. 443 * @param {Object} attributes Attributes for the board and 'encoding'. 444 * Compressed files need encoding 'iso-8859-1'. Otherwise it probably is 'utf-8'. 445 * @param {Function} callback 446 * @returns {JXG.Board} Reference to the created board. 447 * @see JXG.FileReader 448 * @see JXG.GeonextReader 449 * @see JXG.GeogebraReader 450 * @see JXG.IntergeoReader 451 * @see JXG.CinderellaReader 452 * 453 * @example 454 * // Uncompressed file 455 * var board = JXG.JSXGraph.loadBoardFromFile('jxgbox', 'filename', 'geonext', 456 * {encoding: 'utf-8'}, 457 * function (board) { console.log("Done loading"); } 458 * ); 459 * // Compressed file 460 * var board = JXG.JSXGraph.loadBoardFromFile('jxgbox', 'filename', 'geonext', 461 * {encoding: 'iso-8859-1'}, 462 * function (board) { console.log("Done loading"); } 463 * ); 464 * 465 * @example 466 * // From <input type="file" id="localfile" /> 467 * var file = document.getElementById('localfile').files[0]; 468 * JXG.JSXGraph.loadBoardFromFile('jxgbox', file, 'geonext', 469 * {encoding: 'utf-8'}, 470 * function (board) { console.log("Done loading"); } 471 * ); 472 */ 473 loadBoardFromFile: function (box, file, format, attributes, callback) { 474 var attr, renderer, board, dimensions, encoding; 475 476 attributes = attributes || {}; 477 attr = this._setAttributes(attributes); 478 479 dimensions = Env.getDimensions(box, attr.document); 480 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 481 this._setARIA(box, attr); 482 483 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 484 board = new Board( 485 box, 486 renderer, 487 "", 488 [150, 150], 489 1, 490 1, 491 50, 492 50, 493 dimensions.width, 494 dimensions.height, 495 attr 496 ); 497 this._fillBoard(board, attr, dimensions); 498 encoding = attr.encoding || "iso-8859-1"; 499 FileReader.parseFileContent(file, board, format, true, encoding, callback); 500 501 return board; 502 }, 503 504 /** 505 * Load a board from a base64 encoded string containing a construction made with either GEONExT, 506 * Intergeo, Geogebra, or Cinderella. 507 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 508 * @param {String} string base64 encoded string. 509 * @param {String} format containing the file format: 'Geonext', 'Intergeo', 'Geogebra'. 510 * @param {Object} attributes Attributes for the board and 'encoding'. 511 * Compressed files need encoding 'iso-8859-1'. Otherwise it probably is 'utf-8'. 512 * @param {Function} callback 513 * @returns {JXG.Board} Reference to the created board. 514 * @see JXG.FileReader 515 * @see JXG.GeonextReader 516 * @see JXG.GeogebraReader 517 * @see JXG.IntergeoReader 518 * @see JXG.CinderellaReader 519 */ 520 loadBoardFromString: function (box, string, format, attributes, callback) { 521 var attr, renderer, board, dimensions; 522 523 attributes = attributes || {}; 524 attr = this._setAttributes(attributes); 525 526 dimensions = Env.getDimensions(box, attr.document); 527 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 528 this._setARIA(box, attr); 529 530 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 531 board = new Board( 532 box, 533 renderer, 534 "", 535 [150, 150], 536 1.0, 537 1.0, 538 50, 539 50, 540 dimensions.width, 541 dimensions.height, 542 attr 543 ); 544 this._fillBoard(board, attr, dimensions); 545 FileReader.parseString(string, board, format, true, callback); 546 547 return board; 548 }, 549 550 /** 551 * Delete a board and all its contents. 552 * @param {JXG.Board|String} board id of or reference to the DOM element in which the board is drawn. 553 * 554 */ 555 freeBoard: function (board) { 556 var el; 557 558 if (typeof board === "string") { 559 board = JXG.boards[board]; 560 } 561 562 this._removeARIANodes(board); 563 board.removeEventHandlers(); 564 board.suspendUpdate(); 565 566 // Remove all objects from the board. 567 for (el in board.objects) { 568 if (board.objects.hasOwnProperty(el)) { 569 board.objects[el].remove(); 570 } 571 } 572 573 // Remove all the other things, left on the board, XHTML save 574 while (board.containerObj.firstChild) { 575 board.containerObj.removeChild(board.containerObj.firstChild); 576 } 577 578 // Tell the browser the objects aren't needed anymore 579 for (el in board.objects) { 580 if (board.objects.hasOwnProperty(el)) { 581 delete board.objects[el]; 582 } 583 } 584 585 // Free the renderer and the algebra object 586 delete board.renderer; 587 588 // clear the creator cache 589 board.jc.creator.clearCache(); 590 delete board.jc; 591 592 // Finally remove the board itself from the boards array 593 delete JXG.boards[board.id]; 594 }, 595 596 /** 597 * @deprecated Use JXG#registerElement 598 * @param element 599 * @param creator 600 */ 601 registerElement: function (element, creator) { 602 JXG.deprecated("JXG.JSXGraph.registerElement()", "JXG.registerElement()"); 603 JXG.registerElement(element, creator); 604 } 605 }; 606 607 // JessieScript/JessieCode startup: 608 // Search for script tags of type text/jessiecode and execute them. 609 if (Env.isBrowser && typeof window === 'object' && typeof document === 'object') { 610 Env.addEvent(window, 'load', 611 function () { 612 var type, i, j, div, id, 613 board, txt, width, height, maxWidth, aspectRatio, 614 cssClasses, bbox, axis, grid, code, src, request, 615 postpone = false, 616 617 scripts = document.getElementsByTagName("script"), 618 init = function (code, type, bbox) { 619 var board = JXG.JSXGraph.initBoard(id, { 620 boundingbox: bbox, 621 keepaspectratio: true, 622 grid: grid, 623 axis: axis, 624 showReload: true 625 }); 626 627 if (type.toLowerCase().indexOf("script") > -1) { 628 board.construct(code); 629 } else { 630 try { 631 board.jc.parse(code); 632 } catch (e2) { 633 JXG.debug(e2); 634 } 635 } 636 637 return board; 638 }, 639 makeReload = function (board, code, type, bbox) { 640 return function () { 641 var newBoard; 642 643 JXG.JSXGraph.freeBoard(board); 644 newBoard = init(code, type, bbox); 645 newBoard.reload = makeReload(newBoard, code, type, bbox); 646 }; 647 }; 648 649 for (i = 0; i < scripts.length; i++) { 650 type = scripts[i].getAttribute("type", false); 651 652 if ( 653 Type.exists(type) && 654 (type.toLowerCase() === "text/jessiescript" || 655 type.toLowerCase() === "jessiescript" || 656 type.toLowerCase() === "text/jessiecode" || 657 type.toLowerCase() === "jessiecode") 658 ) { 659 cssClasses = scripts[i].getAttribute("class", false) || ""; 660 width = scripts[i].getAttribute("width", false) || ""; 661 height = scripts[i].getAttribute("height", false) || ""; 662 maxWidth = scripts[i].getAttribute("maxwidth", false) || "100%"; 663 aspectRatio = scripts[i].getAttribute("aspectratio", false) || "1/1"; 664 bbox = scripts[i].getAttribute("boundingbox", false) || "-5, 5, 5, -5"; 665 id = scripts[i].getAttribute("container", false); 666 src = scripts[i].getAttribute("src", false); 667 668 bbox = bbox.split(","); 669 if (bbox.length !== 4) { 670 bbox = [-5, 5, 5, -5]; 671 } else { 672 for (j = 0; j < bbox.length; j++) { 673 bbox[j] = parseFloat(bbox[j]); 674 } 675 } 676 axis = Type.str2Bool(scripts[i].getAttribute("axis", false) || "false"); 677 grid = Type.str2Bool(scripts[i].getAttribute("grid", false) || "false"); 678 679 if (!Type.exists(id)) { 680 id = "jessiescript_autgen_jxg_" + i; 681 div = document.createElement("div"); 682 div.setAttribute("id", id); 683 684 txt = width !== "" ? "width:" + width + ";" : ""; 685 txt += height !== "" ? "height:" + height + ";" : ""; 686 txt += maxWidth !== "" ? "max-width:" + maxWidth + ";" : ""; 687 txt += aspectRatio !== "" ? "aspect-ratio:" + aspectRatio + ";" : ""; 688 689 div.setAttribute("style", txt); 690 div.setAttribute("class", "jxgbox " + cssClasses); 691 try { 692 document.body.insertBefore(div, scripts[i]); 693 } catch (e) { 694 // there's probably jquery involved... 695 if (typeof jQuery === "object") { 696 jQuery(div).insertBefore(scripts[i]); 697 } 698 } 699 } else { 700 div = document.getElementById(id); 701 } 702 703 code = ""; 704 705 if (Type.exists(src)) { 706 postpone = true; 707 request = new XMLHttpRequest(); 708 request.open("GET", src); 709 request.overrideMimeType("text/plain; charset=x-user-defined"); 710 /* jshint ignore:start */ 711 request.addEventListener("load", function () { 712 if (this.status < 400) { 713 code = this.responseText + "\n" + code; 714 board = init(code, type, bbox); 715 board.reload = makeReload(board, code, type, bbox); 716 } else { 717 throw new Error( 718 "\nJSXGraph: failed to load file", 719 src, 720 ":", 721 this.responseText 722 ); 723 } 724 }); 725 request.addEventListener("error", function (e) { 726 throw new Error("\nJSXGraph: failed to load file", src, ":", e); 727 }); 728 /* jshint ignore:end */ 729 request.send(); 730 } else { 731 postpone = false; 732 } 733 734 if (document.getElementById(id)) { 735 code = scripts[i].innerHTML; 736 code = code.replace(/<!\[CDATA\[/g, "").replace(/\]\]>/g, ""); 737 scripts[i].innerHTML = code; 738 739 if (!postpone) { 740 // Do no wait for data from "src" attribute 741 board = init(code, type, bbox); 742 board.reload = makeReload(board, code, type, bbox); 743 } 744 } else { 745 JXG.debug( 746 "JSXGraph: Apparently the div injection failed. Can't create a board, sorry." 747 ); 748 } 749 } 750 } 751 }, 752 window 753 ); 754 } 755 756 export default JXG.JSXGraph; 757