/
deployggb-template.js
1822 lines (1612 loc) · 67 KB
/
deployggb-template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
@author: GeoGebra - Dynamic Mathematics for Everyone, http://www.geogebra.org
@license: This file is subject to the GeoGebra Non-Commercial License Agreement, see http://www.geogebra.org/license. For questions please write us at office@geogebra.org.
*/
/*global renderGGBElement, console */
(function() {
if (typeof window.GGBApplet == "function") {
console.warn("deployggb.js was loaded twice");
return;
}
var isRenderGGBElementEnabled = false;
var scriptLoadStarted = false;
var html5AppletsToProcess = null;
var ggbHTML5LoadedCodebaseIsWebSimple = false;
var ggbHTML5LoadedCodebaseVersion = null;
var ggbHTML5LoadedScript = null;
/**
* @param ggbVersion GeoGebra version; deprecated
* @param parameters An object containing parameters that are passed to the applet.
* @param views An object containing information about which views are used in the GeoGebra worksheet. Each variable is boolean.
* E.g.: {"is3D":false,"AV":false,"SV":false,"CV":false,"EV2":false,"CP":false,"PC":false,"DA":false,"FI":false,"PV":false,"macro":false};
* @param html5NoWebSimple Set to true to avoid using web Simple for simple html5 applets. In this case the full version is used always.
*/
var GGBApplet = function() {
"use strict";
var applet = {};
// Define the parameters
var ggbVersion = '5.0';
var parameters = {};
var views = null;
var html5NoWebSimple = false;
var html5NoWebSimpleParamExists = false;
var appletID = null;
var initComplete = false;
var html5OverwrittenCodebaseVersion = null;
var html5OverwrittenCodebase = null;
for(var i=0; i<arguments.length; i++) {
var p = arguments[i];
if (p !== null) {
switch(typeof(p)) {
case 'number':
ggbVersion = p.toFixed(1);
break;
case 'string':
// Check for a version number
if (p.match(new RegExp("^[0-9]\\.[0-9]+$"))) {
ggbVersion = p;
} else {
appletID = p;
}
break;
case 'object':
if (typeof p.is3D !== "undefined") {
views = p;
} else {
parameters = p;
}
break;
case 'boolean':
html5NoWebSimple = p;
html5NoWebSimpleParamExists = true;
break;
}
}
}
if (views === null) {
views = {"is3D":false,"AV":false,"SV":false,"CV":false,"EV2":false,"CP":false,"PC":false,"DA":false,"FI":false,"PV":false,"macro":false};
// don't use web simple when material is loaded from tube, because we don't know which views are used.
if (parameters.material_id !== undefined && !html5NoWebSimpleParamExists) {
html5NoWebSimple = true;
}
}
if (appletID !== null && parameters.id === undefined) {
parameters.id = appletID;
}
// Private members
var jnlpFilePath = "";
var html5Codebase = "";
var isHTML5Offline = false;
var loadedAppletType = null;
var html5CodebaseVersion = null;
var html5CodebaseScript = null;
var html5CodebaseIsWebSimple = false;
var previewImagePath = null;
var previewLoadingPath = null;
var previewPlayPath = null;
var fonts_css_url = null;
var jnlpBaseDir = null;
if (parameters.height !== undefined) {
parameters.height = Math.round(parameters.height);
}
if (parameters.width !== undefined) {
parameters.width = Math.round(parameters.width);
}
var parseVersion =function(d){
return parseFloat(d)>4.0 ? parseFloat(d) : 5;
};
/**
* Overrides the codebase for HTML5.
* @param codebase Can be an URL or a local file path.
* @param offline Set to true, if the codebase is a local URL and no web URL
*/
applet.setHTML5Codebase = function(codebase, offline) {
html5OverwrittenCodebase = codebase;
setHTML5CodebaseInternal(codebase, offline);
};
/**
* Java / Compiled codebase settings: not supported, empty implementation for compatibility
*/
applet.setJavaCodebase = applet.setJavaCodebaseVersion = applet.isCompiledInstalled
= applet.setPreCompiledScriptPath = applet.setPreCompiledResourcePath = function() {};
/**
* Overrides the codebase version for HTML5.
* If another codebase than the default codebase should be used, this method has to be called before setHTML5Codebase.
* @param version The version of the codebase that should be used for HTML5 applets.
*/
applet.setHTML5CodebaseVersion = function(version, offline) {
var numVersion = parseFloat(version);
if (numVersion !== NaN && numVersion < 5.0) {
console.log("The GeoGebra HTML5 codebase version "+numVersion+" is deprecated. Using version latest instead.");
return;
} // Version 4.2 is not working properly
html5OverwrittenCodebaseVersion = version;
setDefaultHTML5CodebaseForVersion(version, offline);
};
applet.getHTML5CodebaseVersion = function() {
return html5CodebaseVersion;
};
applet.getParameters = function() {
return parameters;
};
applet.setFontsCSSURL = function(url) {
fonts_css_url = url;
};
/**
* This function is not needed anymore. Keep it for downward compatibility of the API.
*/
applet.setGiacJSURL = function(url) {
};
/**
* Overrides the JNLP file to use.
* By default (if this method is not called), the jnlp file in the codebase directory is used.
* Cannot be used in combination with setJNLPBaseDir
* @param newJnlpFilePath The absolute path to the JNLP file.
*/
applet.setJNLPFile = function(newJnlpFilePath) {
jnlpFilePath = newJnlpFilePath;
};
/**
* Sets an alternative base directory for the JNLP File. The path must not include the version number.
* @param baseDir
*/
applet.setJNLPBaseDir = function(baseDir) {
//not needed, for comaptibility only
};
/**
* Injects the applet;
* @param containerID The id of the HTML element that is the parent of the new applet.
* All other content (innerText) of the container will be overwritten with the new applet.
* @param type Can be 'preferJava', 'preferHTML5', 'java', 'html5', 'auto' or 'screenshot'. Default='auto';
* @param boolean noPreview. Set to true if no preview image should be shown
* @return The type of the applet that was injected or null if the applet could not be injected.
*/
applet.inject = function() {
function isOwnIFrame() {
return window.frameElement && window.frameElement.getAttribute("data-singleton");
}
var type = 'auto';
var container_ID = parameters.id;
var container;
var noPreview = false;
for(var i=0; i<arguments.length; i++) {
var p = arguments[i];
if (typeof(p) === "string") {
p = p.toLowerCase();
if (p.match(/^(prefer)?(java|html5|compiled|auto|screenshot)$/)) {
type = p;
} else {
container_ID = arguments[i];
}
} else if (typeof(p) === "boolean") {
noPreview = p;
} else if (p instanceof HTMLElement) {
container = p;
}
}
continueInject();
function continueInject() {
// Check if the initialization is complete
if (! initComplete) {
// Try again in 200 ms.
setTimeout(continueInject, 200);
return;
}
// Use the container id as appletid, if it was not defined.
type = detectAppletType(type); // Sets the type to either 'java' or 'html5'
var appletElem = container || document.getElementById(container_ID);
if (!appletElem) {
console.log("possibly bug on ajax loading? ");
return;
}
// Remove an existing applet
applet.removeExistingApplet(appletElem, false);
// Read the applet dimensions from the container, if they were not defined in the params
//it is okay, but sadly no height of the container, so we must take care of this too - geogebraweb won't wet widht and height if one if it 0
if (parameters.width === undefined && appletElem.clientWidth) {
parameters.width = appletElem.clientWidth;
}
if (parameters.height === undefined && appletElem.clientHeight) {
parameters.height = appletElem.clientHeight;
}
if (!(parameters.width && parameters.height) && type === "html5") {
delete parameters.width;
delete parameters.height;
}
// Inject the new applet
loadedAppletType = type;
if (type === "screenshot") {
injectScreenshot(appletElem, parameters);
} else {
// Check if applets should be loaded instantly or with a play button
var playButton = false;
if (parameters.hasOwnProperty("playButton") && parameters.playButton || parameters.hasOwnProperty("clickToLoad") && parameters.clickToLoad) {
playButton = true;
} else if (parameters.hasOwnProperty("playButtonAutoDecide") && parameters.playButtonAutoDecide) {
playButton = (!isInIframe() || isOwnIFrame()) && isMobileDevice();
}
if (playButton) {
loadedAppletType = "screenshot";
injectPlayButton(appletElem, parameters, noPreview, type);
} else {
injectHTML5Applet(appletElem, parameters, noPreview);
}
}
}
return;
};
function isInIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
function isMobileDevice() {
if (parameters.hasOwnProperty("screenshotGenerator") && parameters.screenshotGenerator) {
return false;
}
return (Math.max(screen.width, screen.height) < 800);
}
applet.getViews = function() {
return views;
};
/**
* @returns boolean Whether the system is capable of showing the GeoGebra Java applet
*/
applet.isJavaInstalled = function() {
return false;
};
var fetchParametersFromApi = function(successCallback) {
var onSuccess = function(text) {
var jsonData= JSON.parse(text);
// handle either worksheet or single element format
var isGeoGebra = function(element) {return element.type == 'G' || element.type == 'E'};
var item = jsonData.elements ? jsonData.elements.filter(isGeoGebra)[0] : jsonData;
if (!item || !item.url) {
onError();
return;
}
parameters.fileName = item.url;
updateAppletSettings(item.settings || {});
views.is3D = true;
// user setting of preview URL has precedence
var imageDir = 'https://www.geogebra.org/images/';
applet.setPreviewImage(previewImagePath || item.previewUrl,
imageDir + 'GeoGebra_loading.png', imageDir + 'applet_play.png');
successCallback();
};
var onError = function() {
parameters.onError && parameters.onError();
log('Error: Fetching material (id ' + parameters.material_id + ') failed.', parameters);
};
var host = location.host.match(/(www|stage|beta|groot|alpha).geogebra.(org|net)/) ? location.host
: 'www.geogebra.org';
var path = '/materials/' + parameters.material_id + '?scope=basic';
sendCorsRequest(
'https://' + host + '/api/proxy.php?path=' + encodeURIComponent(path),
onSuccess,
onError
);
};
function updateAppletSettings(settings) {
var parameterNames = ['width', 'height', 'showToolBar', 'showMenuBar',
'showAlgebraInput', 'allowStyleBar', 'showResetIcon', 'enableLabelDrags',
'enableShiftDragZoom', 'enableRightClick', 'appName'];
// different defaults in API and web3d
['enableLabelDrags', 'enableShiftDragZoom', 'enableRightClick'].forEach(function(name) {
settings[name] = !!settings[name];
});
parameterNames.forEach(function(name) {
if (parameters[name] === undefined && settings[name] !== undefined) {
parameters[name] = settings[name];
}
});
if (parameters.showToolBarHelp === undefined) {
parameters.showToolBarHelp = parameters.showToolBar;
}
}
// Create the XHR object.
function sendCorsRequest(url, onSuccess, onError) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
// Response handlers.
xhr.onload = function() {
onSuccess(xhr.responseText);
}
xhr.onerror = onError;
xhr.send();
}
/**
* @returns boolean Whether the system is capable of showing the GeoGebra HTML5 applet
*/
applet.isHTML5Installed = function() {
return true;
};
/**
* @returns The type of the loaded applet or null if no applet was loaded yet.
*/
applet.getLoadedAppletType = function() {
return loadedAppletType;
};
applet.setPreviewImage = function(previewFilePath, loadingFilePath, playFilePath) {
previewImagePath = previewFilePath;
previewLoadingPath = loadingFilePath;
previewPlayPath = playFilePath;
};
applet.removeExistingApplet = function(appletParent, showScreenshot) {
var i;
if (typeof appletParent === 'string') {
appletParent = document.getElementById(appletParent);
}
loadedAppletType = null;
var removedID = null;
for (i=0; i<appletParent.childNodes.length; i++) {
var currentChild = appletParent.childNodes[i];
var className = currentChild.className;
if (className === "applet_screenshot") {
if (showScreenshot) {
// Show the screenshot instead of the removed applet
currentChild.style.display = "block";
loadedAppletType = "screenshot";
} else {
// Hide the screenshot
currentChild.style.display = "none";
}
} else if (className !== "applet_scaler prerender") {
// Remove the applet
appletParent.removeChild(currentChild);
removedID = (className && className.indexOf("appletParameters") != -1) ? currentChild.id : null;
i--;
}
}
var appName = parameters.id !== undefined ? parameters.id : removedID;
var app = window[appName];
if (app && typeof app.getBase64 === "function") { // Check if the variable is a GeoGebra Applet and remove it
app.remove();
window[appName] = null;
}
};
applet.refreshHitPoints = function() {
if (parseVersion(ggbHTML5LoadedCodebaseVersion) >= 5.0) {
return true; // Not necessary anymore in 5.0
}
var app = applet.getAppletObject();
if (app) {
if (typeof app.recalculateEnvironments === "function") {
app.recalculateEnvironments();
return true;
}
}
return false;
};
applet.startAnimation = function() {
var app = applet.getAppletObject();
if (app) {
if (typeof app.startAnimation === "function") {
app.startAnimation();
return true;
}
}
return false;
};
applet.stopAnimation = function() {
var app = applet.getAppletObject();
if (app) {
if (typeof app.stopAnimation === "function") {
app.stopAnimation();
return true;
}
}
return false;
};
applet.getAppletObject = function() {
var appName = (parameters.id !== undefined ? parameters.id : "ggbApplet");
return window[appName];
};
applet.resize = function() {};
var appendParam = function(applet, name, value) {
var param = document.createElement("param");
param.setAttribute("name", name);
param.setAttribute("value", value);
applet.appendChild(param);
};
var valBoolean = function(value) {
return (value && value !== "false");
};
var injectHTML5Applet = function(appletElem, parameters, noPreview) {
if (parseVersion(html5CodebaseVersion) <= 4.2) {
noPreview = true;
}
// Decide if the script has to be (re)loaded or renderGGBElement can be used to load the applet
var loadScript = !isRenderGGBElementEnabled && !scriptLoadStarted;
// Reload the script when not loaded yet, or currently the wrong version is loaded
if ((!isRenderGGBElementEnabled && !scriptLoadStarted) || (ggbHTML5LoadedCodebaseVersion !== html5CodebaseVersion || (ggbHTML5LoadedCodebaseIsWebSimple && !html5CodebaseIsWebSimple))) {
loadScript = true ;
isRenderGGBElementEnabled = false;
scriptLoadStarted = false;
}
var article = document.createElement("div");
// don't add geogebraweb here, as we don't want to parse it out of the box.
article.classList.add("appletParameters", "notranslate");
var oriWidth = parameters.width;
var oriHeight = parameters.height;
parameters.disableAutoScale = parameters.disableAutoScale === undefined ? GGBAppletUtils.isFlexibleWorksheetEditor() : parameters.disableAutoScale;
// The HTML5 version 4.4 changes the height depending on which bars are shown. So we have to correct it here.
if (parameters.width !== undefined) {
if (parseVersion(html5CodebaseVersion) <= 4.4) {
if (valBoolean(parameters.showToolBar)) {
parameters.height -= 7;
}
if (valBoolean(parameters.showAlgebraInput)) {
parameters.height -= 37;
}
if (parameters.width < 605 && valBoolean(parameters.showToolBar)) {
parameters.width = 605;
oriWidth = 605;
}
} else {
// calculate the minWidth
var minWidth = 100;
if (valBoolean(parameters.showToolBar) || valBoolean(parameters.showMenuBar)) {
if (parameters.hasOwnProperty("customToolBar")) {
parameters.customToolbar = parameters.customToolBar;
}
minWidth = (valBoolean(parameters.showMenuBar) ? 245 : 155);
}
if (oriWidth < minWidth) {
parameters.width = minWidth;
oriWidth = minWidth;
}
}
}
article.style.border = 'none';
article.style.display = 'inline-block';
for (var key in parameters) {
if (parameters.hasOwnProperty(key) && key !== "appletOnLoad") {
article.setAttribute("data-param-"+key, parameters[key]);
}
}
if(fonts_css_url){
article.setAttribute("data-param-fontscssurl",fonts_css_url);
}
// Resize the applet when the window is resized
applet.resize = function() {
GGBAppletUtils.responsiveResize(appletElem, parameters);
};
window.addEventListener('resize', function(evt) {applet.resize()});
var oriAppletOnload = (typeof parameters.appletOnLoad === "function") ?
parameters.appletOnLoad : function(){};
// Add the tag for the preview image
if (!noPreview && parameters.width !== undefined) {
// Prevent GeoGebraWeb from showing the splash
if (!parameters.hasOwnProperty('showSplash')) {
article.setAttribute("data-param-showSplash", 'false');
}
// Check if the screenshot is already there
var previewPositioner = appletElem.querySelector(".applet_scaler.prerender");
var preRendered = (previewPositioner !== null);
if (!preRendered) {
var previewContainer = createScreenShotDiv(oriWidth, oriHeight, parameters.borderColor, false);
// This div is needed to have an element with position relative as origin for the absolute positioned image
previewPositioner = document.createElement("div");
previewPositioner.className = "applet_scaler";
previewPositioner.style.position = "relative";
previewPositioner.style.display = 'block';
previewPositioner.style.width = oriWidth+'px';
previewPositioner.style.height = oriHeight+'px';
} else {
var previewContainer = previewPositioner.querySelector(".ggb_preview");
}
if (window.GGBT_spinner) {
window.GGBT_spinner.attachSpinner(previewPositioner, '66%');
}
if (parseVersion(html5CodebaseVersion)>=5.0) {
// Workaround: Remove the preview image when the applet is fully loaded
parameters.appletOnLoad = function(api) {
var preview = appletElem.querySelector(".ggb_preview");
if (preview) {
preview.parentNode.removeChild(preview);
}
if (window.GGBT_spinner) {
window.GGBT_spinner.removeSpinner(previewPositioner);
}
if (window.GGBT_wsf_view) {
$(window).trigger("resize");
}
oriAppletOnload(api);
};
if (!preRendered) {
previewPositioner.appendChild(previewContainer);
}
} else {
article.appendChild(previewContainer);
}
previewPositioner.appendChild(article);
if (!preRendered) {
appletElem.appendChild(previewPositioner);
}
// Redo resizing when screenshot is loaded to recalculate it after scrollbars are gone
setTimeout(function() {
applet.resize();
}, 1);
} else {
var appletScaler = document.createElement("div");
appletScaler.className = "applet_scaler";
appletScaler.style.position = "relative";
appletScaler.style.display = 'block';
appletScaler.appendChild(article);
appletElem.appendChild(appletScaler);
// Apply scaling
parameters.appletOnLoad = function(api){
applet.resize();
oriAppletOnload(api);
}
}
function renderGGBElementWithParams(article, parameters) {
if (parameters && typeof parameters.appletOnLoad === "function" && typeof renderGGBElement === "function") {
renderGGBElement(article, parameters.appletOnLoad);
} else {
renderGGBElement(article);
}
log("GeoGebra HTML5 applet injected and rendered with previously loaded codebase.", parameters);
}
function renderGGBElementOnTube(a, parameters) {
if (typeof renderGGBElement === "undefined") {
//it is possible, that we get here many times, before script are loaded. So best here to save the article element for later - otherwise only last article processed :-)
if (html5AppletsToProcess === null) {
html5AppletsToProcess = [];
}
html5AppletsToProcess.push({
article : a,
params : parameters
});
window.renderGGBElementReady = function() {
isRenderGGBElementEnabled = true;
if (html5AppletsToProcess !== null && html5AppletsToProcess.length) {
html5AppletsToProcess.forEach(function(obj) {
renderGGBElementWithParams(obj.article, obj.params);
});
html5AppletsToProcess = null;
}
};
//TODO: remove this hack, because it is a hack!
if (parseVersion(html5CodebaseVersion) < 5.0) {
a.className += " geogebraweb";
}
} else {
renderGGBElementWithParams(a, parameters);
}
}
// Load the web script
if (loadScript) {
scriptLoadStarted = true;
// Remove all table tags within an article tag if there are any
for (var i=0; i<article.childNodes.length;i++) {
var tag = article.childNodes[i].tagName;
if (tag === "TABLE") {
article.removeChild(article.childNodes[i]);
i--;
}
}
// Remove old script tags
if (ggbHTML5LoadedScript !== null) {
var el = document.querySelector('script[src="'+ggbHTML5LoadedScript+'"]');
if (el !== undefined && el !== null) {
el.parentNode.removeChild(el);
}
}
var script = document.createElement("script");
var scriptLoaded = function() {
renderGGBElementOnTube(article, parameters);
};
script.src = html5Codebase + html5CodebaseScript;
ggbHTML5LoadedCodebaseIsWebSimple = html5CodebaseIsWebSimple;
ggbHTML5LoadedCodebaseVersion = html5CodebaseVersion;
ggbHTML5LoadedScript = script.src;
log("GeoGebra HTML5 codebase loaded: '"+html5Codebase+"'.", parameters);
if (!html5OverwrittenCodebase && (!html5OverwrittenCodebaseVersion || html5OverwrittenCodebaseVersion == '5.0')) {
if(html5CodebaseIsWebSimple){
webSimple.succeeded = webSimple.succeeded || webSimple();
}else {
web3d.succeeded = web3d.succeeded || web3d();
}
scriptLoaded();
} else if (html5Codebase.requirejs) {
require(['geogebra/runtime/js/web3d/web3d.nocache'], scriptLoaded);
} else {
script.onload = scriptLoaded;
appletElem.appendChild(script);
}
} else {
renderGGBElementOnTube(article, parameters);
}
parameters.height = oriHeight;
parameters.width = oriWidth;
};
var injectScreenshot = function(appletElem, parameters, showPlayButton) {
// Add the tag for the preview image
var previewContainer = createScreenShotDiv(parameters.width, parameters.height, parameters.borderColor, showPlayButton);
// This div is needed to have an element with position relative as origin for the absolute positioned image
var previewPositioner = document.createElement("div");
previewPositioner.style.position = "relative";
previewPositioner.style.display = 'block';
previewPositioner.style.width = parameters.width+'px';
previewPositioner.style.height = parameters.height+'px';
previewPositioner.className = "applet_screenshot applet_scaler" + (showPlayButton ? " applet_screenshot_play" : "");
previewPositioner.appendChild(previewContainer);
var scale = GGBAppletUtils.getScale(parameters, appletElem, showPlayButton);
if(showPlayButton) {
appletElem.appendChild(getPlayButton());
if (!window.GGBT_wsf_view) {
appletElem.style.position = "relative";
}
} else if (window.GGBT_spinner) {
window.GGBT_spinner.attachSpinner(previewPositioner, '66%');
}
appletElem.appendChild(previewPositioner);
// Set the scale for the preview image
if (scale !== 1 && !isNaN(scale)) {
// Set the scale factor for the preview image
GGBAppletUtils.scaleElement(previewPositioner, scale);
previewPositioner.style.width = (parameters.width)+'px';
previewPositioner.style.height = (parameters.height)+'px';
previewPositioner.parentNode.style.width = (parameters.width*scale)+'px';
previewPositioner.parentNode.style.height = (parameters.height*scale)+'px';
}
applet.resize = function() {
resizeScreenshot(appletElem, previewContainer, previewPositioner, showPlayButton);
};
window.addEventListener('resize', function(evt) {applet.resize()});
applet.resize();
};
function resizeScreenshot(appletElem, previewContainer, previewPositioner, showPlayButton) {
if (!appletElem.contains(previewContainer)) { // Don't resize the screenshot if it is not visible (anymore)
return;
}
if (typeof window.GGBT_wsf_view === "object" && window.GGBT_wsf_view.isFullscreen()) {
if (appletElem.id !== "fullscreencontent") {
return;
}
window.GGBT_wsf_view.setCloseBtnPosition(appletElem);
}
var scale = GGBAppletUtils.getScale(parameters, appletElem, showPlayButton);
if (previewPositioner.parentNode !== null) {
if (!isNaN(scale) && scale !== 1) {
GGBAppletUtils.scaleElement(previewPositioner, scale);
previewPositioner.parentNode.style.width = (parameters.width * scale) + 'px';
previewPositioner.parentNode.style.height = (parameters.height * scale) + 'px';
} else {
GGBAppletUtils.scaleElement(previewPositioner, 1);
previewPositioner.parentNode.style.width = (parameters.width) + 'px';
previewPositioner.parentNode.style.height = (parameters.height) + 'px';
}
}
// positions the applet in the center of the popup
if(typeof window.GGBT_wsf_view === 'object' && window.GGBT_wsf_view.isFullscreen()) {
GGBAppletUtils.positionCenter(appletElem);
}
if (typeof window.GGBT_ws_header_footer === "object") {
window.GGBT_ws_header_footer.setWsScrollerHeight();
}
}
applet.onExitFullscreen = function(fullscreenContainer, appletElem) {
appletElem.appendChild(fullscreenContainer);
};
var injectPlayButton = function(appletElem, parameters, noPreview, type) {
injectScreenshot (appletElem, parameters, true);
// Load applet on play button click
var play = function() {
// Remove the screenshot after the applet is injected
var elems = [];
for (i=0; i<appletElem.childNodes.length;i++) {
elems.push(appletElem.childNodes[i]);
}
if (window.GGBT_wsf_view) {
var content = window.GGBT_wsf_view.renderFullScreen(appletElem, parameters.id);
var container = document.getElementById("fullscreencontainer");
var oldcontent = jQuery(appletElem).find('.fullscreencontent');
if (oldcontent.length > 0) {
// Reuse the previously rendered applet
content.remove();
oldcontent.attr("id", "fullscreencontent").show();
jQuery(container).append(oldcontent);
window.dispatchEvent(new Event("resize"));
} else {
// Render a new applet
injectHTML5Applet(content, parameters, false);
}
window.GGBT_wsf_view.launchFullScreen(container);
} else {
loadedAppletType = type;
injectHTML5Applet(appletElem, parameters, false);
}
if (!window.GGBT_wsf_view) {
for (i = 0; i < elems.length; i++) {
appletElem.removeChild(elems[i]);
}
}
};
// Find the play button and add the click handler
var imgs = appletElem.getElementsByClassName("ggb_preview_play");
for (var i = 0; i < imgs.length; i++) {
imgs[i].addEventListener('click', play, false);
imgs[i].addEventListener('ontouchstart', play, false);
}
// Call onload
if (typeof window.ggbAppletPlayerOnload === 'function') {
window.ggbAppletPlayerOnload(appletElem);
}
//remove fullscreen button if not needed
if (isMobileDevice() && window.GGBT_wsf_view) {
$(".wsf-element-fullscreen-button").remove();
}
};
var getPlayButton = function() {
var playButtonContainer = document.createElement("div");
playButtonContainer.className = 'ggb_preview_play icon-applet-play';
if (!window.GGBT_wsf_view) { // on tube, the play button image is defined in a css file
var css = '' +
'.icon-applet-play {' +
' width: 100%;' +
' height: 100%;box-sizing: border-box;position: absolute;z-index: 1001;cursor: pointer;border-width: 0px;' +
' background-color: transparent;background-repeat: no-repeat;left: 0;top: 0;background-position: center center;' +
' background-image: url("https://www.geogebra.org/images/worksheet/icon-start-applet.png");' +
'}' +
'.icon-applet-play:hover {' +
'background-image: url("https://www.geogebra.org/images/worksheet/icon-start-applet-hover.png");' +
'}';
var style = document.createElement('style');
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
document.getElementsByTagName('head')[0].appendChild(style);
}
return playButtonContainer;
};
var createScreenShotDiv = function(oriWidth, oriHeight, borderColor, showPlayButton) {
var previewContainer = document.createElement("div");
previewContainer.className = "ggb_preview";
previewContainer.style.position = "absolute";
//previewContainer.style.zIndex = "1000001";
// too high z-index causes various problems
// overlaps fixed header
// overlaps popups
previewContainer.style.zIndex = "90";
previewContainer.style.width = oriWidth-2+'px'; // Remove 2 pixel for the border
previewContainer.style.height = oriHeight-2+'px'; // Remove 2 pixel for the border
previewContainer.style.top = "0px";
previewContainer.style.left = "0px";
previewContainer.style.overflow = "hidden";
previewContainer.style.backgroundColor = "white";
var bc = 'lightgrey';
if (borderColor !== undefined) {
if (borderColor === "none") {
bc = "transparent";
} else {
bc = borderColor;
}
}
previewContainer.style.border = "1px solid " + bc;
var preview = document.createElement("img");
preview.style.position = "relative";
preview.style.zIndex = "1000";
preview.style.top = "-1px"; // Move up/left to hide the border on the image
preview.style.left = "-1px";
if (previewImagePath !== null) {
preview.setAttribute("src", previewImagePath);
}
preview.style.opacity = 0.7;
if (previewLoadingPath !== null) {
var previewOverlay;
var pWidth, pHeight;
if (!showPlayButton) {
previewOverlay = document.createElement("img");
previewOverlay.style.position = "absolute";
previewOverlay.style.zIndex = "1001";
previewOverlay.style.opacity = 1.0;
preview.style.opacity = 0.3;
pWidth = 360;
if (pWidth > (oriWidth/4*3)) {
pWidth = oriWidth/4*3;
}
pHeight = pWidth/5.8;
previewOverlay.setAttribute("src", previewLoadingPath);
previewOverlay.setAttribute("width", pWidth);
previewOverlay.setAttribute("height", pHeight);
var pX = (oriWidth - pWidth) / 2;
var pY = (oriHeight - pHeight) / 2;
previewOverlay.style.left = pX + "px";
previewOverlay.style.top = pY + "px";
previewContainer.appendChild(previewOverlay);
}
}
previewContainer.appendChild(preview);
return previewContainer;
};
/**
* Detects the type of the applet (java or html5).
* If a fixed type is passed in preferredType (java or html5), this type is forced.
* Otherwise the method tries to find out which types are supported by the system.
* If a preferredType is passed, this type is used if it is supported.
* If auto is passed, the preferred type is html5 for versions >= 4.4 and java for all versions < 4.4.
* @param preferredType can be 'preferJava', 'preferHTML5', 'java', 'html5', 'auto' or 'screenshot'. Default='auto'
*/
var detectAppletType = function(preferredType) {
preferredType = preferredType.toLowerCase();
if ((preferredType === "html5") || (preferredType === "screenshot")) {
return preferredType;
}
return "html5";
};
var modules = ["web", "webSimple", "web3d", "tablet", "tablet3d", "phone"];
/**
* @param version Can be: 3.2, 4.0, 4.2, 4.4, 5.0, test, test42, test44, test50
*/
var setDefaultHTML5CodebaseForVersion = function(version, offline) {
html5CodebaseVersion = version;
if (offline) {
setHTML5CodebaseInternal(html5CodebaseVersion, true);
return;
}
// Set the codebase URL for the version
var hasWebSimple = ! html5NoWebSimple;
if (hasWebSimple) {
var v = parseVersion(html5CodebaseVersion);
if ((!isNaN(v) && v < 4.4)) {
hasWebSimple = false;
}
}
var protocol,
codebase;
if (window.location.protocol.substr(0,4) === 'http') {