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 '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 attr.navbar = Type.copyAttributes(attr.navbar, options, "navbar"); 193 194 // Treat moveTarget separately, because deepCopy will not work here. 195 // Reason: moveTarget will be an HTML node and it is prevented that Type.deepCopy will copy it. 196 attr.movetarget = 197 attributes.moveTarget || attributes.movetarget || options.board.moveTarget; 198 199 return attr; 200 }, 201 202 /** 203 * Further initialization of the board. Set some properties from attribute values. 204 * 205 * @param {JXG.Board} board 206 * @param {Object} attr attributes object 207 * @param {Object} dimensions Object containing dimensions of the canvas 208 * 209 * @private 210 */ 211 _fillBoard: function (board, attr, dimensions) { 212 board.initInfobox(attr.infobox); 213 board.maxboundingbox = attr.maxboundingbox; 214 board.resizeContainer(dimensions.width, dimensions.height, true, true); 215 board._createSelectionPolygon(attr); 216 board.renderer.drawNavigationBar(board, attr.navbar); 217 JXG.boards[board.id] = board; 218 }, 219 220 /** 221 * 222 * @param {String|Object} container id of or reference to the HTML element in which the board is painted. 223 * @param {Object} attr An object that sets some of the board properties. 224 * 225 * @private 226 */ 227 _setARIA: function (container, attr) { 228 var doc = attr.document, 229 node_jsx; 230 // Unused variables, made obsolete in db3e50f4dfa8b86b1ff619b578e243a97b41151c 231 // doc_glob, 232 // newNode, 233 // parent, 234 // id_label, 235 // id_description; 236 237 if (typeof doc !== 'object') { 238 if (!Env.isBrowser) { 239 return; 240 } 241 doc = document; 242 } 243 244 node_jsx = (Type.isString(container)) ? doc.getElementById(container) : container; 245 node_jsx.setAttribute("role", "region"); 246 node_jsx.setAttribute("aria-label", attr.title); // set by initBoard( {title:}) 247 248 // doc_glob = node_jsx.ownerDocument; // This is the window.document element, needed below. 249 // parent = node_jsx.parentNode; 250 251 }, 252 253 /** 254 * Remove the two corresponding ARIA divs when freeing a board 255 * 256 * @param {JXG.Board} board 257 * 258 * @private 259 */ 260 _removeARIANodes: function (board) { 261 var node, id, doc; 262 263 doc = board.document || document; 264 if (typeof doc !== "object") { 265 return; 266 } 267 268 id = board.containerObj.getAttribute("aria-labelledby"); 269 node = doc.getElementById(id); 270 if (node && node.parentNode) { 271 node.parentNode.removeChild(node); 272 } 273 id = board.containerObj.getAttribute("aria-describedby"); 274 node = doc.getElementById(id); 275 if (node && node.parentNode) { 276 node.parentNode.removeChild(node); 277 } 278 }, 279 280 /** 281 * Initialize a new board. 282 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 283 * @param {Object} attributes An object that sets some of the board properties. Most of these properties can be set via JXG.Options. 284 * @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 285 * @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. 286 * @param {Boolean} [attributes.showCopyright=false] Show the copyright string in the top left corner. 287 * @param {Boolean} [attributes.showNavigation=false] Show the navigation buttons in the bottom right corner. 288 * @param {Object} [attributes.zoom] Allow the user to zoom with the mouse wheel or the two-fingers-zoom gesture. 289 * @param {Object} [attributes.pan] Allow the user to pan with shift+drag mouse or two-fingers-pan gesture. 290 * @param {Object} [attributes.drag] Allow the user to drag objects with a pointer device. 291 * @param {Object} [attributes.keyboard] Allow the user to drag objects with arrow keys on keyboard. 292 * @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. 293 * @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. 294 * @param {Boolean} [attributes.registerEvents=true] Register mouse / touch events. 295 * @returns {JXG.Board} Reference to the created board. 296 * 297 * @see JXG.AbstractRenderer#drawNavigationBar 298 */ 299 initBoard: function (box, attributes) { 300 var originX, originY, unitX, unitY, w, h, 301 offX = 0, offY = 0, 302 renderer, dimensions, bbox, 303 attr, axattr, axattr_x, axattr_y, 304 options, 305 theme = {}, 306 board; 307 308 attributes = attributes || {}; 309 // Merge a possible theme 310 if (attributes.theme !== 'default' && Type.exists(JXG.themes[attributes.theme])) { 311 theme = JXG.themes[attributes.theme]; 312 } 313 options = Type.deepCopy(Options, theme, true); 314 attr = this._setAttributes(attributes, options); 315 316 dimensions = Env.getDimensions(box, attr.document); 317 318 if (attr.unitx || attr.unity) { 319 originX = Type.def(attr.originx, 150); 320 originY = Type.def(attr.originy, 150); 321 unitX = Type.def(attr.unitx, 50); 322 unitY = Type.def(attr.unity, 50); 323 } else { 324 bbox = attr.boundingbox; 325 if (bbox[0] < attr.maxboundingbox[0]) { 326 bbox[0] = attr.maxboundingbox[0]; 327 } 328 if (bbox[1] > attr.maxboundingbox[1]) { 329 bbox[1] = attr.maxboundingbox[1]; 330 } 331 if (bbox[2] > attr.maxboundingbox[2]) { 332 bbox[2] = attr.maxboundingbox[2]; 333 } 334 if (bbox[3] < attr.maxboundingbox[3]) { 335 bbox[3] = attr.maxboundingbox[3]; 336 } 337 338 // Size of HTML div. 339 // If zero, the size is set to a small value to avoid 340 // division by zero. 341 // w = Math.max(parseInt(dimensions.width, 10), Mat.eps); 342 // h = Math.max(parseInt(dimensions.height, 10), Mat.eps); 343 w = parseInt(dimensions.width, 10); 344 h = parseInt(dimensions.height, 10); 345 346 if (Type.exists(bbox) && attr.keepaspectratio) { 347 /* 348 * If the boundingbox attribute is given and the ratio of height and width of the 349 * sides defined by the bounding box and the ratio of the dimensions of the div tag 350 * which contains the board do not coincide, then the smaller side is chosen. 351 */ 352 unitX = w / (bbox[2] - bbox[0]); 353 unitY = h / (bbox[1] - bbox[3]); 354 355 if (Math.abs(unitX) < Math.abs(unitY)) { 356 unitY = (Math.abs(unitX) * unitY) / Math.abs(unitY); 357 // Add the additional units in equal portions above and below 358 offY = (h / unitY - (bbox[1] - bbox[3])) * 0.5; 359 } else { 360 unitX = (Math.abs(unitY) * unitX) / Math.abs(unitX); 361 // Add the additional units in equal portions left and right 362 offX = (w / unitX - (bbox[2] - bbox[0])) * 0.5; 363 } 364 } else { 365 unitX = w / (bbox[2] - bbox[0]); 366 unitY = h / (bbox[1] - bbox[3]); 367 } 368 originX = -unitX * (bbox[0] - offX); 369 originY = unitY * (bbox[1] + offY); 370 } 371 372 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 373 this._setARIA(box, attr); 374 375 // Create the board. 376 // board.options will contain the user supplied board attributes 377 board = new Board( 378 box, 379 renderer, 380 attr.id, 381 [originX, originY], 382 /*attr.zoomfactor * */ attr.zoomx, 383 /*attr.zoomfactor * */ attr.zoomy, 384 unitX, 385 unitY, 386 dimensions.width, 387 dimensions.height, 388 attr 389 ); 390 391 board.keepaspectratio = attr.keepaspectratio; 392 393 this._fillBoard(board, attr, dimensions); 394 395 // Create elements like axes, grid, navigation, ... 396 board.suspendUpdate(); 397 attr = board.attr; 398 if (attr.axis) { 399 axattr = typeof attr.axis === "object" ? attr.axis : {}; 400 401 // The defaultAxes attributes are overwritten by user supplied axis object. 402 axattr_x = Type.deepCopy(options.board.defaultaxes.x, axattr); 403 axattr_y = Type.deepCopy(options.board.defaultaxes.y, axattr); 404 405 // The user supplied defaultAxes attributes are merged in. 406 if (attr.defaultaxes.x) { 407 axattr_x = Type.deepCopy(axattr_x, attr.defaultaxes.x); 408 } 409 if (attr.defaultaxes.y) { 410 axattr_y = Type.deepCopy(axattr_y, attr.defaultaxes.y); 411 } 412 413 board.defaultAxes = {}; 414 board.defaultAxes.x = board.create("axis", [[0, 0], [1, 0]], axattr_x); 415 board.defaultAxes.y = board.create("axis", [[0, 0], [0, 1]], axattr_y); 416 } 417 if (attr.grid) { 418 board.create("grid", [], typeof attr.grid === "object" ? attr.grid : {}); 419 } 420 board.unsuspendUpdate(); 421 422 return board; 423 }, 424 425 /** 426 * Load a board from a file containing a construction made with either GEONExT, 427 * Intergeo, Geogebra, or Cinderella. 428 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 429 * @param {String} file base64 encoded string. 430 * @param {String} format containing the file format: 'Geonext' or 'Intergeo'. 431 * @param {Object} attributes Attributes for the board and 'encoding'. 432 * Compressed files need encoding 'iso-8859-1'. Otherwise it probably is 'utf-8'. 433 * @param {Function} callback 434 * @returns {JXG.Board} Reference to the created board. 435 * @see JXG.FileReader 436 * @see JXG.GeonextReader 437 * @see JXG.GeogebraReader 438 * @see JXG.IntergeoReader 439 * @see JXG.CinderellaReader 440 * 441 * @example 442 * // Uncompressed file 443 * var board = JXG.JSXGraph.loadBoardFromFile('jxgbox', 'filename', 'geonext', 444 * {encoding: 'utf-8'}, 445 * function (board) { console.log("Done loading"); } 446 * ); 447 * // Compressed file 448 * var board = JXG.JSXGraph.loadBoardFromFile('jxgbox', 'filename', 'geonext', 449 * {encoding: 'iso-8859-1'}, 450 * function (board) { console.log("Done loading"); } 451 * ); 452 * 453 * @example 454 * // From <input type="file" id="localfile" /> 455 * var file = document.getElementById('localfile').files[0]; 456 * JXG.JSXGraph.loadBoardFromFile('jxgbox', file, 'geonext', 457 * {encoding: 'utf-8'}, 458 * function (board) { console.log("Done loading"); } 459 * ); 460 */ 461 loadBoardFromFile: function (box, file, format, attributes, callback) { 462 var attr, renderer, board, dimensions, encoding; 463 464 attributes = attributes || {}; 465 attr = this._setAttributes(attributes); 466 467 dimensions = Env.getDimensions(box, attr.document); 468 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 469 this._setARIA(box, attr); 470 471 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 472 board = new Board( 473 box, 474 renderer, 475 "", 476 [150, 150], 477 1, 478 1, 479 50, 480 50, 481 dimensions.width, 482 dimensions.height, 483 attr 484 ); 485 this._fillBoard(board, attr, dimensions); 486 encoding = attr.encoding || "iso-8859-1"; 487 FileReader.parseFileContent(file, board, format, true, encoding, callback); 488 489 return board; 490 }, 491 492 /** 493 * Load a board from a base64 encoded string containing a construction made with either GEONExT, 494 * Intergeo, Geogebra, or Cinderella. 495 * @param {String|Object} box id of or reference to the HTML element in which the board is painted. 496 * @param {String} string base64 encoded string. 497 * @param {String} format containing the file format: 'Geonext', 'Intergeo', 'Geogebra'. 498 * @param {Object} attributes Attributes for the board and 'encoding'. 499 * Compressed files need encoding 'iso-8859-1'. Otherwise it probably is 'utf-8'. 500 * @param {Function} callback 501 * @returns {JXG.Board} Reference to the created board. 502 * @see JXG.FileReader 503 * @see JXG.GeonextReader 504 * @see JXG.GeogebraReader 505 * @see JXG.IntergeoReader 506 * @see JXG.CinderellaReader 507 */ 508 loadBoardFromString: function (box, string, format, attributes, callback) { 509 var attr, renderer, board, dimensions; 510 511 attributes = attributes || {}; 512 attr = this._setAttributes(attributes); 513 514 dimensions = Env.getDimensions(box, attr.document); 515 renderer = this.initRenderer(box, dimensions, attr.document, attr.renderer); 516 this._setARIA(box, attr); 517 518 /* User default parameters, in parse* the values in the gxt files are submitted to board */ 519 board = new Board( 520 box, 521 renderer, 522 "", 523 [150, 150], 524 1.0, 525 1.0, 526 50, 527 50, 528 dimensions.width, 529 dimensions.height, 530 attr 531 ); 532 this._fillBoard(board, attr, dimensions); 533 FileReader.parseString(string, board, format, true, callback); 534 535 return board; 536 }, 537 538 /** 539 * Delete a board and all its contents. 540 * @param {JXG.Board|String} board id of or reference to the DOM element in which the board is drawn. 541 * 542 */ 543 freeBoard: function (board) { 544 var el; 545 546 if (typeof board === "string") { 547 board = JXG.boards[board]; 548 } 549 550 this._removeARIANodes(board); 551 board.removeEventHandlers(); 552 board.suspendUpdate(); 553 554 // Remove all objects from the board. 555 for (el in board.objects) { 556 if (board.objects.hasOwnProperty(el)) { 557 board.objects[el].remove(); 558 } 559 } 560 561 // Remove all the other things, left on the board, XHTML save 562 while (board.containerObj.firstChild) { 563 board.containerObj.removeChild(board.containerObj.firstChild); 564 } 565 566 // Tell the browser the objects aren't needed anymore 567 for (el in board.objects) { 568 if (board.objects.hasOwnProperty(el)) { 569 delete board.objects[el]; 570 } 571 } 572 573 // Free the renderer and the algebra object 574 delete board.renderer; 575 576 // clear the creator cache 577 board.jc.creator.clearCache(); 578 delete board.jc; 579 580 // Finally remove the board itself from the boards array 581 delete JXG.boards[board.id]; 582 }, 583 584 /** 585 * @deprecated Use JXG#registerElement 586 * @param element 587 * @param creator 588 */ 589 registerElement: function (element, creator) { 590 JXG.deprecated("JXG.JSXGraph.registerElement()", "JXG.registerElement()"); 591 JXG.registerElement(element, creator); 592 } 593 }; 594 595 // JessieScript/JessieCode startup: 596 // Search for script tags of type text/jessiecode and execute them. 597 if (Env.isBrowser && typeof window === 'object' && typeof document === 'object') { 598 Env.addEvent(window, 'load', 599 function () { 600 var type, i, j, div, id, 601 board, txt, width, height, maxWidth, aspectRatio, 602 cssClasses, bbox, axis, grid, code, src, request, 603 postpone = false, 604 605 scripts = document.getElementsByTagName("script"), 606 init = function (code, type, bbox) { 607 var board = JXG.JSXGraph.initBoard(id, { 608 boundingbox: bbox, 609 keepaspectratio: true, 610 grid: grid, 611 axis: axis, 612 showReload: true 613 }); 614 615 if (type.toLowerCase().indexOf("script") > -1) { 616 board.construct(code); 617 } else { 618 try { 619 board.jc.parse(code); 620 } catch (e2) { 621 JXG.debug(e2); 622 } 623 } 624 625 return board; 626 }, 627 makeReload = function (board, code, type, bbox) { 628 return function () { 629 var newBoard; 630 631 JXG.JSXGraph.freeBoard(board); 632 newBoard = init(code, type, bbox); 633 newBoard.reload = makeReload(newBoard, code, type, bbox); 634 }; 635 }; 636 637 for (i = 0; i < scripts.length; i++) { 638 type = scripts[i].getAttribute("type", false); 639 640 if ( 641 Type.exists(type) && 642 (type.toLowerCase() === "text/jessiescript" || 643 type.toLowerCase() === "jessiescript" || 644 type.toLowerCase() === "text/jessiecode" || 645 type.toLowerCase() === "jessiecode") 646 ) { 647 cssClasses = scripts[i].getAttribute("class", false) || ""; 648 width = scripts[i].getAttribute("width", false) || ""; 649 height = scripts[i].getAttribute("height", false) || ""; 650 maxWidth = scripts[i].getAttribute("maxwidth", false) || "100%"; 651 aspectRatio = scripts[i].getAttribute("aspectratio", false) || "1/1"; 652 bbox = scripts[i].getAttribute("boundingbox", false) || "-5, 5, 5, -5"; 653 id = scripts[i].getAttribute("container", false); 654 src = scripts[i].getAttribute("src", false); 655 656 bbox = bbox.split(","); 657 if (bbox.length !== 4) { 658 bbox = [-5, 5, 5, -5]; 659 } else { 660 for (j = 0; j < bbox.length; j++) { 661 bbox[j] = parseFloat(bbox[j]); 662 } 663 } 664 axis = Type.str2Bool(scripts[i].getAttribute("axis", false) || "false"); 665 grid = Type.str2Bool(scripts[i].getAttribute("grid", false) || "false"); 666 667 if (!Type.exists(id)) { 668 id = "jessiescript_autgen_jxg_" + i; 669 div = document.createElement("div"); 670 div.setAttribute("id", id); 671 672 txt = width !== "" ? "width:" + width + ";" : ""; 673 txt += height !== "" ? "height:" + height + ";" : ""; 674 txt += maxWidth !== "" ? "max-width:" + maxWidth + ";" : ""; 675 txt += aspectRatio !== "" ? "aspect-ratio:" + aspectRatio + ";" : ""; 676 677 div.setAttribute("style", txt); 678 div.setAttribute("class", "jxgbox " + cssClasses); 679 try { 680 document.body.insertBefore(div, scripts[i]); 681 } catch (e) { 682 // there's probably jquery involved... 683 if (typeof jQuery === "object") { 684 jQuery(div).insertBefore(scripts[i]); 685 } 686 } 687 } else { 688 div = document.getElementById(id); 689 } 690 691 code = ""; 692 693 if (Type.exists(src)) { 694 postpone = true; 695 request = new XMLHttpRequest(); 696 request.open("GET", src); 697 request.overrideMimeType("text/plain; charset=x-user-defined"); 698 /* jshint ignore:start */ 699 request.addEventListener("load", function () { 700 if (this.status < 400) { 701 code = this.responseText + "\n" + code; 702 board = init(code, type, bbox); 703 board.reload = makeReload(board, code, type, bbox); 704 } else { 705 throw new Error( 706 "\nJSXGraph: failed to load file", 707 src, 708 ":", 709 this.responseText 710 ); 711 } 712 }); 713 request.addEventListener("error", function (e) { 714 throw new Error("\nJSXGraph: failed to load file", src, ":", e); 715 }); 716 /* jshint ignore:end */ 717 request.send(); 718 } else { 719 postpone = false; 720 } 721 722 if (document.getElementById(id)) { 723 code = scripts[i].innerHTML; 724 code = code.replace(/<!\[CDATA\[/g, "").replace(/\]\]>/g, ""); 725 scripts[i].innerHTML = code; 726 727 if (!postpone) { 728 // Do no wait for data from "src" attribute 729 board = init(code, type, bbox); 730 board.reload = makeReload(board, code, type, bbox); 731 } 732 } else { 733 JXG.debug( 734 "JSXGraph: Apparently the div injection failed. Can't create a board, sorry." 735 ); 736 } 737 } 738 } 739 }, 740 window 741 ); 742 } 743 744 export default JXG.JSXGraph; 745