JAVASCRIPT   31
cas
Guest on 26th May 2023 12:55:34 PM


  1. var wrs_int_opener;
  2. var appletObject;
  3. var initialXML = '';
  4. var closeFunction;
  5.  
  6. if (window.opener) {                            // For popup mode.
  7.     wrs_int_opener = window.opener;
  8.     closeFunction = window.close;
  9. }
  10. // FCKeditor integration begin
  11. else {                                          // For iframe mode.
  12.     wrs_int_opener = window.parent;
  13.  
  14.     while (wrs_int_opener.InnerDialogLoaded) {
  15.         wrs_int_opener = wrs_int_opener.parent;
  16.     }
  17. }
  18.  
  19. if (window.parent.InnerDialogLoaded) {          // Iframe mode.
  20.     window.parent.InnerDialogLoaded();
  21.     closeFunction = window.parent.Cancel;
  22. }
  23. else if (window.opener.parent.FCKeditorAPI) {   // Popup mode.
  24.     wrs_int_opener = window.opener.parent;
  25. }
  26. // FCKeditor integration end.
  27.  
  28. function getMathmlFromAppletCode(appletCode) {
  29.     var optionForm = document.getElementById('optionForm');
  30.     appletObject = wrs_int_opener.wrs_createObject(appletCode, document);
  31.  
  32.     optionForm.width.value = parseInt(appletObject.width);
  33.     optionForm.height.value = parseInt(appletObject.height);
  34.  
  35.     var params = appletObject.childNodes;
  36.     var mathml = '';
  37.  
  38.     for (var i = 0; i < params.length; ++i) {
  39.         if (params[i].name == 'xmlinitialtext') {
  40.             mathml = params[i].value;
  41.         }
  42.         else if (params[i].name == 'requestfirstevaluation') {
  43.             optionForm.executeonload.checked = (params[i].value == 'true') ? true : false;
  44.         }
  45.         else if (params[i].name == 'toolbar') {
  46.             optionForm.toolbar.checked = (params[i].value == 'floating') ? false : true;
  47.         }
  48.         else if (params[i].name == 'requestfocus') {
  49.             optionForm.focusonload.checked = (params[i].value == 'true') ? true : false;
  50.         }
  51.         else if (params[i].name == 'level') {
  52.             optionForm.level.checked = (params[i].value == 'primary') ? true : false;
  53.         }
  54.     }
  55.  
  56.     return mathml;
  57. }
  58.  
  59. function createIframePath(params) {
  60.     var iframePath = wrs_int_opener._wrs_conf_CASPath;
  61.     iframePath += ((iframePath.indexOf('?') == -1) ? '?' : '&') + 'mode=applet&';
  62.  
  63.     for (var i in params) {
  64.         iframePath += wrs_int_opener.wrs_urlencode(i) + '=' + wrs_int_opener.wrs_urlencode(params[i]);
  65.     }
  66.  
  67.     return iframePath;
  68. }
  69.  
  70. function createIframe(params) {
  71.     var iframe = document.createElement('iframe');
  72.     iframe.id = 'appletContainerIframe';
  73.     iframe.src = createIframePath(params);
  74.     iframe.width = '100%';
  75.     iframe.height = '100%';
  76.     iframe.frameBorder = 0;
  77.  
  78.     wrs_int_opener.wrs_addEvent(iframe, 'load', function () {
  79.         if (initialXML.length > 0) {
  80.             var applet = iframe.contentWindow.document.getElementById('applet');
  81.  
  82.             function setAppletMathml() {
  83.                 // Internet explorer fails on "applet.isActive". It only supports "applet.isActive()".
  84.  
  85.                 try {
  86.                     if (applet.isActive()) {
  87.                         applet.setXML(initialXML);
  88.                     }
  89.                     else {
  90.                         setTimeout(setAppletMathml, 50);
  91.                     }
  92.                 }
  93.                 catch (e) {
  94.                     setTimeout(setAppletMathml, 50);
  95.                 }
  96.             }
  97.  
  98.             setAppletMathml();
  99.         }
  100.     });
  101.  
  102.     document.getElementById('appletContainer').appendChild(iframe);
  103. }
  104.  
  105. function reloadIframe(params) {
  106.     var iframe = document.getElementById('appletContainerIframe');
  107.     var applet = iframe.contentWindow.document.getElementById('applet');
  108.     initialXML = applet.getXML();
  109.     iframe.src = createIframePath(params);
  110. }
  111.  
  112. wrs_int_opener.wrs_addEvent(window, 'load', function () {
  113.     // Getting language list <select> object.
  114.     var languageList = document.getElementById('languageList');
  115.  
  116.     // When the language list <select> object changes its value, the iframe should be refreshed.
  117.  
  118.     wrs_int_opener.wrs_addEvent(languageList, 'change', function () {
  119.         reloadIframe({
  120.             'lang': languageList.value
  121.         });
  122.     });
  123.  
  124.     // Setting iframe language.
  125.     var language;
  126.  
  127.     if (wrs_int_opener._wrs_isNewElement) {
  128.         var queryParams = wrs_int_opener.wrs_getQueryParams(window);
  129.  
  130.         var availableLangs = new Array();
  131.         for (var i = 0; i < languageList.options.length; i++){
  132.             availableLangs[i] = languageList.options[i].value;
  133.         }
  134.  
  135.         if (typeof queryParams['lang'] != 'undefined' && wrs_int_opener.wrs_arrayContains(availableLangs, queryParams['lang']) != -1){
  136.             language = queryParams['lang'];
  137.         }else if (typeof queryParams['lang'] != 'undefined' && wrs_int_opener.wrs_arrayContains(availableLangs, queryParams['lang'].substr(0,2)) != -1){
  138.             language = queryParams['lang'].substr(0,2);
  139.         }else{
  140.             language = wrs_int_opener._wrs_int_language
  141.         }
  142.     }
  143.     else {
  144.         var appletCode = wrs_int_opener._wrs_temporalImage.getAttribute(wrs_int_opener._wrs_conf_CASMathmlAttribute);
  145.         initialXML = getMathmlFromAppletCode(wrs_int_opener.wrs_mathmlDecode(appletCode));
  146.  
  147.         var language = '';
  148.  
  149.         // We can convert initialXML to an object and get its "lang" value. However, IE does not support this functionability, so we use string parsing.
  150.         var languageStart = initialXML.indexOf('lang="');
  151.  
  152.         if (languageStart != -1) {
  153.             var languageEnd = initialXML.indexOf('"', languageStart + 6);       // 6+ because 'lang="'.length is 6.
  154.  
  155.             if (languageEnd != -1) {
  156.                 language = initialXML.substring(languageStart + 6, languageEnd);
  157.             }
  158.         }
  159.     }
  160.  
  161.     // Creating the iframe.
  162.  
  163.     createIframe({
  164.         'lang': language
  165.     });
  166.  
  167.     // Selecting the language on the <select> object.
  168.  
  169.     for (var i = languageList.options.length - 1; i >= 0; --i) {
  170.         if (languageList.options[i].value == language) {
  171.             languageList.selectedIndex = i;
  172.             i = 0;
  173.         }
  174.     }
  175.  
  176.     // More events.
  177.  
  178.     wrs_int_opener.wrs_addEvent(document.getElementById('submit'), 'click', function () {
  179.         var applet = document.getElementById('appletContainerIframe').contentWindow.document.getElementById('applet');
  180.  
  181.         // Creating new applet code.
  182.         var optionForm = document.getElementById('optionForm');
  183.         var newWidth = parseInt(optionForm.width.value);
  184.         var newHeight = parseInt(optionForm.height.value);
  185.  
  186.         var appletCode = '<applet alt="WIRIS cas" class="Wiriscas" align="middle" ';
  187.         appletCode += 'codebase="' + applet.getAttribute('codebase') + '" ';
  188.         appletCode += 'archive="' + applet.getAttribute('archive') + '" ';
  189.         appletCode += 'code="' + applet.getAttribute('code') + '" ';
  190.         appletCode += 'width="' + newWidth + '" height="' + newHeight + '">';
  191.  
  192.         appletCode += '<param name="requestfirstevaluation" value="' + (optionForm.executeonload.checked ? 'true' : 'false') + '"></param>';
  193.         appletCode += '<param name="toolbar" value="' + (optionForm.toolbar.checked ? 'true' : 'floating') + '"></param>';
  194.         appletCode += '<param name="requestfocus" value="' + (optionForm.focusonload.checked ? 'true' : 'false') + '"></param>';
  195.         appletCode += '<param name="level" value="' + (optionForm.level.checked ? 'primary' : 'false') + '"></param>';
  196.         appletCode += '<param name="xmlinitialtext" value="' + wrs_int_opener.wrs_htmlentities(applet.getXML()) + '"></param>';
  197.         appletCode += '<param name="interface" value="false"></param><param name="commands" value="false"></param><param name="command" value="false"></param>';
  198.  
  199.         appletCode += '</applet>';
  200.  
  201.         // Getting the image.
  202.         // First, resize applet.
  203.         applet.style.width = newWidth + 'px';
  204.         applet.style.height = newHeight + 'px';
  205.  
  206.         // Waiting for applet resizing.
  207.         function finish() {
  208.             if (applet.getSize().width != newWidth || applet.getSize().height != newHeight) {
  209.                 setTimeout(finish, 100);
  210.             }
  211.             else {
  212.                 // Getting the image.
  213.                 var image = applet.getImageBase64('png');
  214.  
  215.                 // FCKeditor integration begin
  216.                 if (window.parent.InnerDialogLoaded && window.parent.FCKBrowserInfo.IsIE) {         // On IE, we must close the dialog for push the caret on the correct position.
  217.                     closeFunction();
  218.                     wrs_int_opener.wrs_int_updateCAS(appletCode, image, newWidth, newHeight);
  219.                 }
  220.                 // FCKeditor integration end.
  221.                 else {
  222.                     wrs_int_opener.wrs_int_updateCAS(appletCode, image, newWidth, newHeight);
  223.                     closeFunction();
  224.                 }
  225.             }
  226.         }
  227.  
  228.         finish();
  229.     });
  230.  
  231.     wrs_int_opener.wrs_addEvent(document.getElementById('cancel'), 'click', function () {
  232.         closeFunction();
  233.     });
  234.  
  235.     var acceptButton = document.getElementById('submit');
  236.     if (strings['accept'] != null){
  237.         acceptButton.value = strings['accept'];
  238.     }
  239.  
  240.     var cancelButton = document.getElementById('cancel');
  241.     if (strings['cancel'] != null){
  242.         cancelButton.value = strings['cancel'];
  243.     }
  244.  
  245.     // Auto resizing.
  246.     setInterval(function () {
  247.         document.getElementById('appletContainer').style.height = (document.getElementById('optionForm').offsetHeight - document.getElementById('controls').offsetHeight - 5) + 'px';
  248.     }, 100);
  249. });
  250.  
  251. wrs_int_opener.wrs_addEvent(window, 'unload', function () {
  252.     wrs_int_opener.wrs_int_notifyWindowClosed();
  253. });

Raw Paste

Login or Register to edit or fork this paste. It's free.