forked from kasperpeulen/euclidthegame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployggb.js
1301 lines (1127 loc) · 50.8 KB
/
deployggb.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
/**
* (c) International GeoGebra Institute 2013
* Licence for use: http://creativecommons.org/licenses/by-nc-nd/3.0/
* For commercial use please see: http://www.geogebra.org/license
*
*/
var ggbHTML5ScriptLoadInProgress = false;
var ggbHTML5ScriptLoadFinished = false;
var ggbHTML5LoadedCodebaseIsWebSimple = false;
var ggbHTML5LoadedCodebaseVersion = null;
var ggbHTML5LoadedScript = null;
var ggbCompiledResourcesLoadFinished = false;
var ggbCompiledResourcesLoadInProgress = false;
var ggbCompiledAppletsLoaded = false;
/**
* @param ggbVersion The version of the GeoGebraFile as string in the format x.x (e.g. '4.4'). If the version is not specified, the latest stable GeoGebraVersion is used (currently 4.4).
* @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() {
var applet = {};
// Define the parameters
var ggbVersion = '4.4';
var parameters = {};
var views = null;
var html5NoWebSimple = false;
var html5NoWebSimpleParamExists = false;
var appletID = null;
var initComplete = false;
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 javaCodebase = "";
var isOverriddenJavaCodebase = false;
var isHTML5Offline = false;
var isJavaOffline = false;
var loadedAppletType = null;
var javaCodebaseVersion = null;
var html5CodebaseVersion = null;
var html5CodebaseScript = null;
var html5CodebaseIsWebSimple = false;
var previewImagePath = null;
var previewLoadingPath = null;
var fonts_css_url = null;
var giac_js_url = null;
var jnlpBaseDir = null;
var preCompiledScriptPath = null;
var preCompiledResourcePath = null;
if (parameters.height != undefined)
parameters.height = Math.round(parameters.height);
if (parameters.width != undefined)
parameters.width = Math.round(parameters.width);
/**
* 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) {
html5Codebase = codebase;
if (offline == null) {
offline = (codebase.indexOf("http") === -1);
}
isHTML5Offline = offline;
// Set the scriptname (web or webSimple)
html5CodebaseScript = "web.nocache.js";
html5CodebaseIsWebSimple = false;
var folders = html5Codebase.split("/");
if (folders.length>0) {
if (! offline && folders[folders.length-2] == 'webSimple') { // Currently we don't use webSimple for offline worksheets
html5CodebaseScript = "webSimple.nocache.js";
html5CodebaseIsWebSimple = true;
} else if (folders[folders.length-2] == 'web3d') {
html5CodebaseScript = "web3d.nocache.js";
}
}
// Extract the version from the codebase folder
if (codebase.slice(-1) != '/') {
codebase += '/';
}
var folders = codebase.split('/');
html5CodebaseVersion = folders[folders.length-3];
if (html5CodebaseVersion.substr(0,4) == 'test') {
html5CodebaseVersion = html5CodebaseVersion.substr(4,1) + '.' + html5CodebaseVersion.substr(5,1);
} else if (html5CodebaseVersion.substr(0,3) == 'war' || html5CodebaseVersion.substr(0,4) == 'beta') {
html5CodebaseVersion = '5.0';
}
};
/**
* Overrides the codebase version for Java.
* @param version The version of the codebase that shoudl be used for java applets.
*/
applet.setJavaCodebaseVersion = function(version) {
javaCodebaseVersion = version;
setDefaultJavaCodebaseForVersion(version);
};
/**
* 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) {
setDefaultHTML5CodebaseForVersion(version);
};
applet.getHTML5CodebaseVersion = function() {
return html5CodebaseVersion;
};
applet.getParameters = function() {
return parameters;
};
/**
* Overrides the codebase for Java.
* @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.setJavaCodebase = function(codebase, offline) {
isOverriddenJavaCodebase = true;
if (codebase.slice(-1) == '/') {
javaCodebaseVersion = codebase.slice(-4,-1);
} else {
javaCodebaseVersion = codebase.slice(-3);
}
if (offline == null) {
offline = (codebase.indexOf("http") === -1);
}
if (offline && jnlpBaseDir != null) {
jnlpBaseDir = null;
}
doSetJavaCodebase(codebase, offline);
};
applet.setFontsCSSURL = function(url) {
fonts_css_url = url;
};
applet.setGiacJSURL = function(url) {
giac_js_url = url;
};
applet.toggleAppletTypeControls = function(parentSelector) {
var currentAppletType = applet.getLoadedAppletType();
var displayJava = "none"; displayHTML5 = "none";
if (currentAppletType == "java" && applet.isHTML5Installed()) {
displayHTML5 = "inline";
} else if (currentAppletType == "html5" && applet.isJavaInstalled()) {
displayJava = "inline";
}
var elem = document.querySelector(parentSelector + ' #view_as_Java');
if (elem != null) elem.style.display = displayJava;
elem = document.querySelector(parentSelector + ' #view_as_separator_Java');
if (elem != null) elem.style.display = displayJava;
elem = document.querySelector(parentSelector + ' #view_as_HTML5');
if (elem != null) elem.style.display = displayHTML5;
elem = document.querySelector(parentSelector + ' #view_as_separator_HTML5');
if (elem != null) elem.style.display = displayHTML5;
};
var doSetJavaCodebase = function(codebase, offline) {
javaCodebase = codebase;
// Check if the codebase is online or local
isJavaOffline = offline;
// Set the name of the JNLP file to the codebase directory
if (jnlpBaseDir == null) {
var dir='';
if (isJavaOffline) {
var loc = window.location.pathname;
dir = loc.substring(0, loc.lastIndexOf('/'))+'/';
}
applet.setJNLPFile(dir+codebase+'/'+buildJNLPFileName(isJavaOffline));
} else {
applet.setJNLPFile(jnlpBaseDir+javaCodebaseVersion+'/'+buildJNLPFileName(isJavaOffline));
}
};
/**
* 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) {
jnlpBaseDir = baseDir;
applet.setJNLPFile(jnlpBaseDir+javaCodebaseVersion+'/'+buildJNLPFileName(isJavaOffline));
};
/**
* 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() {
var type = 'auto';
var container_ID = parameters.id;
var noPreview = false;
for(var i=0; i<arguments.length; i++) {
var p = arguments[i];
if (typeof(p) == "string") {
p = p.toLowerCase();
if (p == 'preferjava' || p == 'preferhtml5' || p == 'java' || p == 'html5' || p == 'auto' || p == 'screenshot' || p == 'prefercompiled' || p == 'compiled') {
type = p;
} else {
container_ID = arguments[i];
}
} else if (typeof(p) == "boolean")
noPreview = 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 = document.getElementById(container_ID);
// Remove an existing applet
applet.removeExistingApplet(appletElem, false);
// Read the applet dimensions from the container, if they were not defined in the params
if (parameters.width == undefined) {
parameters.width = appletElem.clientWidth;
}
if (parameters.height == undefined) {
parameters.height = appletElem.clientHeight;
}
// Inject the new applet
loadedAppletType = type;
if (type === "java") {
injectJavaApplet(appletElem, parameters);
} else if (type === "compiled") {
injectCompiledApplet(appletElem, parameters, true);
} else if (type === "screenshot") {
injectScreenshot(appletElem, parameters);
} else {
injectHTML5Applet(appletElem, parameters, noPreview);
}
}
return;
};
function getWidthHeight() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
//window.alert( 'Width = ' + myWidth );
//window.alert( 'Height = ' + myHeight );
return {width: myWidth, height: myHeight};
}
applet.getViews = function() {
return views;
}
/**
* @returns boolean Whether the system is capable of showing the GeoGebra Java applet
*/
applet.isJavaInstalled = function() {
if (typeof deployJava === 'undefined') {
// incase deployJava.js not available
if (navigator.javaEnabled()) {
// Check if IE is in metro mode
if (navigator.appName == 'Microsoft Internet Explorer' && getIEVersion() >= 10) {
if(window.innerWidth == screen.width && window.innerHeight == screen.height) {
return false;
}
}
// Check if on Android device
if (navigator.userAgent.indexOf('Android ') > -1) {
return false;
}
return true;
}
} else {
return (deployJava.versionCheck("1.6.0+") || deployJava.versionCheck("1.4") || deployJava.versionCheck("1.5.0*"));
}
};
var fetchParametersFromTube = function(successCallback) {
// Determine the url for the tube API
if (parameters.tubeurl != undefined) {
// Url was specified in parameters
var tubeurl = parameters.tubeurl;
} else if (window.location.host.indexOf("geogebratube.org") > -1 || window.location.host.indexOf("tube.geogebra.org") > -1
|| window.location.host.indexOf("tube-test.geogebra.org") > -1 || window.location.host.indexOf("tube-beta.geogebra.org") > -1) {
// if the script is used on a tube site, use this site for the api url.
var tubeurl = window.location.protocol + "//" + window.location.host;
} else {
// Use main tube url
if (window.location.protocol.substr(0,4) == 'http')
var protocol = window.location.protocol;
else
var protocol = 'http:';
var tubeurl = protocol+"//tube.geogebra.org";
}
// load ggbbase64 string and settings from API
var api_request = {
"request": {
"-api": "1.0.0",
"task": {
"-type": "fetch",
"fields": {
"field": [
{ "-name": "id" },
{ "-name": "geogebra_format" },
// { "-name": "prefapplettype" },
{ "-name": "width" },
{ "-name": "height" },
{ "-name": "toolbar" },
{ "-name": "menubar" },
{ "-name": "inputbar" },
{ "-name": "reseticon" },
{ "-name": "labeldrags" },
{ "-name": "shiftdragzoom" },
{ "-name": "rightclick" },
{ "-name": "ggbbase64" }
]
},
"filters" : {
"field": [{
"-name":"id", "#text": ""+parameters.material_id+""
}]
},
"order": {
"-by": "id",
"-type": "asc"
},
"limit": { "-num": "1" }
}
}
};
// TODO: add prefapplet type (params:'type' API:'prefapplettype')
success = function() {
var text = xhr.responseText;
var jsondata=eval("("+text+")"); //retrieve result as an JSON object
var item = jsondata.responses.response.item;
if (item == undefined) {
onError();
return;
}
ggbVersion = item.geogebra_format;
if (parameters.ggbBase64 == undefined)
parameters.ggbBase64 = item.ggbBase64;
if (parameters.width == undefined)
parameters.width = item.width;
if (parameters.height == undefined)
parameters.height = item.height;
if (parameters.showToolBar == undefined)
parameters.showToolBar = item.toolbar == "true";
if (parameters.showMenuBar == undefined)
parameters.showMenuBar = item.menubar == "true";
if (parameters.showAlgebraInput == undefined)
parameters.showAlgebraInput = item.inputbar == "true";
if (parameters.showResetIcon == undefined)
parameters.showResetIcon = item.reseticon == "true";
if (parameters.enableLabelDrags == undefined)
parameters.enableLabelDrags = item.labeldrags == "true";
if (parameters.enableShiftDragZoom == undefined)
parameters.enableShiftDragZoom = item.shiftdragzoom == "true";
if (parameters.enableRightClick == undefined)
parameters.enableRightClick = item.rightclick == "true";
if (parameters.showToolBarHelp == undefined)
parameters.showToolBarHelp = parameters.showToolBar;
// var views = {"is3D":false,"AV":false,"SV":false,"CV":false,"EV2":false,"CP":false,"PC":false,"DA":false,"FI":false,"PV":false,"macro":false};
applet.setPreviewImage(tubeurl+"/files/material-"+item.id+".png", tubeurl+"/images/GeoGebra_loading.png");
successCallback();
}
var url = tubeurl+"/api/json.php";
var xhr = createCORSRequest('POST', url);
var onError = function() {
log("Error: The request for fetching material_id " + parameters.material_id + " from tube was not successful.");
};
if (!xhr) {
onError();
return;
}
// Response handlers.
xhr.onload = success;
xhr.onerror = onError;
// Send request
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(JSON.stringify(api_request));
};
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
/**
* @return NULL if no version found. Else return some things like: '1.6.0_31'
*/
var JavaVersion = function() {
var resutl = null;
// Walk through the full list of mime types.
for( var i=0,size=navigator.mimeTypes.length; i<size; i++ )
{
// The jpi-version is the plug-in version. This is the best
// version to use.
if( (resutl = navigator.mimeTypes[i].type.match(/^application\/x-java-applet;jpi-version=(.*)$/)) !== null )
return resutl[1];
}
return null;
};
/**
* @returns boolean Whether the system is capable of showing the GeoGebra HTML5 applet
*/
applet.isHTML5Installed = function() {
if (navigator.appName == 'Microsoft Internet Explorer') {
if (views.is3D && getIEVersion() < 11) { // WebGL is supported since IE 11
return false;
} else if (getIEVersion() < 10) {
return false;
}
}
return true;
};
/**
* @returns boolean Whether the system is capable of showing precompiled HTML5 applets
*/
applet.isCompiledInstalled = function() {
if (navigator.appName == 'Microsoft Internet Explorer') {
if (views.is3D && getIEVersion() < 11) { // WebGL is supported since IE 11
return false;
} else if (getIEVersion() < 9) {
return false;
}
}
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) {
previewImagePath = previewFilePath;
previewLoadingPath = loadingFilePath;
};
applet.removeExistingApplet = function(appletParent, showScreenshot) {
if (typeof appletParent == 'string') {
appletParent = document.getElementById(appletParent);
}
if (loadedAppletType == 'compiled' && window[parameters.id] != undefined) {
// Stop/remove the applet
if (typeof window[parameters.id].remove == "stopAnimation") {
window[parameters.id].stopAnimation();
}
if (typeof window[parameters.id].remove == "function") {
window[parameters.id].remove();
}
// Set the applet objects to undefined
if (ggbApplets != undefined) {
for (var i=0; i<ggbApplets.length;i++) {
if (ggbApplets[i] == window[parameters.id]) {
ggbApplets.splice(i, 1);
}
}
}
window[parameters.id] = undefined;
}
loadedAppletType = null;
for (var i=0; i<appletParent.childNodes.length;i++) {
var tag = appletParent.childNodes[i].tagName;
if (appletParent.childNodes[i].className == "applet_screenshot") {
if (showScreenshot) {
// Show the screenshot instead of the removed applet
appletParent.childNodes[i].style.display = "block";
loadedAppletType = "screenshot";
} else {
// Hide the screenshot
appletParent.childNodes[i].style.display = "none";
}
} else if (tag == "APPLET" || tag == "ARTICLE" || tag == "DIV" || (loadedAppletType == 'compiled' && (tag == "SCRIPT" || tag == "STYLE"))) {
// Remove the applet
appletParent.removeChild(appletParent.childNodes[i]);
i--;
}
}
var appName = (parameters.id != undefined ? parameters.id : "ggbApplet");
var app = window[appName];
if (app != undefined) {
eval(appName + "=null;");
}
};
applet.refreshHitPoints = function() {
var app = getAppletObject();
if (app != undefined) {
if (typeof app.recalculateEnvironments == "function") {
app.recalculateEnvironments();
return true;
}
}
return false;
};
applet.startAnimation = function() {
var app = getAppletObject();
if (app != undefined) {
if (typeof app.startAnimation == "function") {
app.startAnimation();
return true;
}
}
return false;
}
applet.stopAnimation = function() {
var app = getAppletObject();
if (app != undefined) {
if (typeof app.stopAnimation == "function") {
app.stopAnimation();
return true;
}
}
return false;
}
applet.setPreCompiledScriptPath = function(path) {
preCompiledScriptPath = path;
if (preCompiledResourcePath == null) {
preCompiledResourcePath = preCompiledScriptPath;
}
}
applet.setPreCompiledResourcePath = function(path) {
preCompiledResourcePath = path;
}
var getAppletObject = function() {
var appName = (parameters.id != undefined ? parameters.id : "ggbApplet");
return window[appName];
}
// var validateJavaApplet = function(appletElem, container_ID) {
// if ((typeof appletElem.isAnimationRunning) === 'undefined') {
// log("Error: The GeoGebra Java applet could not be started. Used JNLP file = '"+jnlpFilePath+"'. Switching to HTML5 instead.");
//
// // Try html5 instead
// applet.inject(container_ID, 'html5');
// }
// }
var injectJavaApplet = function(appletElem, parameters) {
if (views.CV) {
// if (views.CV && (navigator.appVersion.indexOf("Mac")!=-1 || navigator.appVersion.indexOf("Linux")!=-1 || navigator.appVersion.indexOf("X11")!=-1)) {
// Load the javascript version of giac
if (giac_js_url != null) {
giac_url = giac_js_url;
} else {
giac_url = javaCodebase+'/giac.js';
}
var script = document.createElement("script");
script.setAttribute("src", giac_url);
setupGIAC = function() {
_GIAC_caseval = __ggb__giac.cwrap('_ZN4giac7casevalEPKc', 'string', ['string']);
}
script.onload = setupGIAC;
appletElem.appendChild(script);
script = document.createElement("script");
script.innerHTML = "" +
" var _GIAC_caseval = 'nD';" +
" function _ggbCallGiac(exp) {" +
" var ret = _GIAC_caseval(exp);" +
" return ret;" +
" }";
appletElem.appendChild(script);
}
var applet = document.createElement("applet");
applet.setAttribute("name", (parameters.id != undefined ? parameters.id : "ggbApplet"));
applet.setAttribute("height", parameters.height);
applet.setAttribute("width", parameters.width);
applet.setAttribute("code", "dummy");
appendParam(applet, "jnlp_href", jnlpFilePath);
if (isOverriddenJavaCodebase) {
appendParam(applet, "codebase", javaCodebase);
}
appendParam(applet, "boxborder", "false");
appendParam(applet, "centerimage", "true");
if(ggbVersion === "5.0")
appendParam(applet, "java_arguments", "-Xmx1024m -Djnlp.packEnabled=false");
else
appendParam(applet, "java_arguments", "-Xmx1024m -Djnlp.packEnabled=true");
// Add dynamic parameters
for (var key in parameters) {
if (key != 'width' && key != 'height') {
appendParam(applet, key, parameters[key]);
}
}
appendParam(applet, "framePossible", "false");
if (! isJavaOffline)
appendParam(applet, "image", "http://www.geogebra.org/webstart/loading.gif");
appendParam(applet, "codebase_lookup", "false");
if (navigator.appName != 'Microsoft Internet Explorer' || getIEVersion() > 9) {
applet.appendChild(document.createTextNode("This is a Java Applet created using GeoGebra from www.geogebra.org - it looks like you don't have Java installed, please go to www.java.com"));
}
applet.style.display = "block";
appletElem.appendChild(applet);
// setTimeout(validateJavaApplet(appletElem, container_ID),5000);
log("GeoGebra Java applet injected. Used JNLP file = '"+jnlpFilePath+"'"+(isOverriddenJavaCodebase?" with overridden codebase '"+javaCodebase+"'." : "."));
};
var appendParam = function(applet, name, value) {
var param = document.createElement("param");
param.setAttribute("name", name);
param.setAttribute("value", value);
applet.appendChild(param);
};
var injectHTML5Applet = function(appletElem, parameters, noPreview) {
// Decide if the script has to be (re)loaded or renderGGBElement can be used to load the applet
var loadScript = false;
if (ggbHTML5ScriptLoadInProgress) { // Never reload the script when the script load is currently in progress
loadScript = false;
} else if (!ggbHTML5ScriptLoadFinished) { // Script was not loaded yet
loadScript = true;
} else if (ggbHTML5LoadedCodebaseVersion != html5CodebaseVersion || (ggbHTML5LoadedCodebaseIsWebSimple && !html5CodebaseIsWebSimple)) {
// Reload the script when currently the wrong version is loaded
loadScript = true;
}
var renderWithoutReload = ggbHTML5ScriptLoadFinished && typeof(renderGGBElement) == 'function';
var article = document.createElement("article");
var oriWidth = parameters.width;
var oriHeight = parameters.height;
// 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 (parseFloat(html5CodebaseVersion) <= 4.4) {
if (parameters.showToolBar && parameters.showToolBar !== "false") {
parameters.height -= 7;
}
if (parameters.showAlgebraInput && parameters.showAlgebraInput !== "false") {
parameters.height -= 37;
}
}
}
article.className = "geogebraweb notranslate";
article.style.border = 'none';
article.style.display = 'inline-block';
for (var key in parameters) {
article.setAttribute("data-param-"+key, parameters[key]);
}
// Add the tag for the preview image
if (!noPreview && previewImagePath != null && parseFloat(html5CodebaseVersion)>=4.4 && parameters.width != undefined) {
var previewContainer = createScreenShotDiv(oriWidth, oriHeight, parameters.borderColor);
article.appendChild(previewContainer);
// 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 = oriWidth+'px';
previewPositioner.style.height = oriHeight+'px';
previewPositioner.appendChild(article);
appletElem.appendChild(previewPositioner);
} else {
appletElem.appendChild(article);
}
// Load the web script
if (loadScript) {
if (parseFloat(html5CodebaseVersion)>=4.4) {
if (fonts_css_url == null) {
var f_c_u = html5Codebase+"css/fonts.css";
} else {
var f_c_u = fonts_css_url;
}
var fontscript1 = document.createElement("script");
fontscript1.type = 'text/javascript';
fontscript1.innerHTML = '\n' +
'//<![CDATA[\n' +
'WebFontConfig = {\n' +
' loading: function() {},\n' +
' active: function() {},\n' +
' inactive: function() {},\n' +
' fontloading: function(familyName, fvd) {},\n' +
' fontactive: function(familyName, fvd) {},\n' +
' fontinactive: function(familyName, fvd) {},\n' +
' custom: {\n' +
' families: ["geogebra-sans-serif", "geogebra-serif"],\n' +
' urls: [ "'+f_c_u+'" ]\n' +
' }\n' +
'};\n' +
'//]]>\n' +
'\n';
var fontscript2 = document.createElement("script");
fontscript2.type = 'text/javascript';
fontscript2.src = html5Codebase+'/js/webfont.js';
appletElem.appendChild(fontscript1);
appletElem.appendChild(fontscript2);
}
// 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.parentNode.removeChild(el);
}
}
var script = document.createElement("script");
var scriptLoaded = function() {
// log("GeoGebra Web Script loaded. Src = "+script.src);
ggbHTML5ScriptLoadInProgress = false;
ggbHTML5ScriptLoadFinished = true;
}
script.src=html5Codebase + html5CodebaseScript;
script.onload = scriptLoaded;
ggbHTML5ScriptLoadInProgress = true;
ggbHTML5ScriptLoadFinished = false;
ggbHTML5LoadedCodebaseIsWebSimple = html5CodebaseIsWebSimple;
ggbHTML5LoadedCodebaseVersion = html5CodebaseVersion;
ggbHTML5LoadedScript = script.src;
log("GeoGebra HTML5 applet injected. Codebase = '"+html5Codebase+"'.");
appletElem.appendChild(script);
} else if (renderWithoutReload) {
renderGGBElement(article);
log("GeoGebra HTML5 applet injected and rendered with previously loaded codebase.")
} else {
log("GeoGebra HTML5 applet injected without reloading web codebase.");
}
parameters.height = oriHeight;
parameters.width = oriWidth;
};
var injectCompiledApplet = function(appletElem, parameters, noPreview) {
var appletObjectName = parameters.id;
// Auto scale the applet size
windowSize = getWidthHeight();
var xscale = Math.min(1, windowSize.width / parameters.width);
var yscale = Math.min(1, windowSize.height / parameters.height);
var scale = Math.min(xscale, yscale);
if (scale != 1) {
parameters.scale = scale;
appletElem.style.minWidth = parameters.width * scale+"px";
appletElem.style.minHeight = parameters.height * scale+"px";
}
var viewContainer = document.createElement("div");
viewContainer.id = "view-container-"+appletObjectName;
viewContainer.setAttribute("width", parameters.width);
viewContainer.setAttribute("height", parameters.height);
viewContainer.style.width = parameters.width*scale+"px";
viewContainer.style.height = parameters.height*scale+"px";
// viewContainer.style.border = "1px solid black";
if (parameters.showSplash == undefined) {
parameters.showSplash = true;
}
var viewImages = document.createElement("div");
viewImages.id = '__ggb__images';
// Add the tag for the preview image
if (!noPreview && previewImagePath != null && parseFloat(html5CodebaseVersion)>=4.4 && parameters.width != undefined) {
var previewContainer = createScreenShotDiv(parameters.width, parameters.height, parameters.borderColor);
// 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.className = "ggb_preview_container";
previewPositioner.style.display = 'block';
previewPositioner.style.width = parameters.width*scale+'px';
previewPositioner.style.height = parameters.height*scale+'px';
previewPositioner.appendChild(previewContainer);
appletElem.appendChild(previewPositioner);
}
// Load the web fonts
if (!ggbCompiledResourcesLoadFinished && !ggbCompiledResourcesLoadInProgress) {
// var resource1 = document.createElement("link");
// resource1.type = 'text/css';
// resource1.rel = 'stylesheet';
// resource1.href = preCompiledResourcePath+'/mathquillggb.css';
//
// var resource2 = document.createElement("script");
// resource2.type = 'text/javascript';
// resource2.src = preCompiledResourcePath+'/jquery-1.7.2.min.js';
//
// var resource3 = document.createElement("script");
// resource3.type = 'text/javascript';
// resource3.src = preCompiledResourcePath+'/mathquillggb.js';
var resource4 = document.createElement("script");
resource4.type = 'text/javascript';
resource4.innerHTML = '\n' +
'//<![CDATA[\n' +
'WebFontConfig = {\n' +
' loading: function() {},\n' +
' active: function() {},\n' +
' inactive: function() {},\n' +
' fontloading: function(familyName, fvd) {console.log("font fontloading: "+familyName);},\n' +
' fontactive: function(familyName, fvd) {' +
' console.log("font active: "+familyName);' +
' if (!ggbCompiledAppletsLoaded) {' +
' ggbCompiledAppletsLoaded = true;' +
' ' +
' setTimeout(function() {' +
' ggbCompiledResourcesLoadFinished = true;' +
' ggbCompiledResourcesLoadInProgress = false;' +
' if (window.ggbApplets != undefined) {' +
' for (var i = 0 ; i < window.ggbApplets.length ; i++) {' +
' window.ggbApplets[i].init({scale:window.ggbApplets[i].scaleParameter, url:window.ggbApplets[i].preCompiledScriptPath+"/", ss:'+(parameters.showSplash?'true':'false')+'});' +
' }' +
' }' +
' if (typeof window.ggbCompiledAppletsOnLoad == "function") {' +
' window.ggbCompiledAppletsOnLoad();' +
' }' +
' },1);' +
' }' +
' },\n' +
' fontinactive: function(familyName, fvd) {console.log("font inactive: "+familyName);},\n' +
' custom: {\n' +
' families: ["geogebra-sans-serif", "geogebra-serif"],\n' +
' urls: [ "'+preCompiledResourcePath+"/fonts/fonts.css"+'" ]\n' +
' }\n' +
'};\n' +
'//]]>\n' +
'\n';
var resource5 = document.createElement("script");