"use strict"; function save_form_text_data(win_id, code_editor_type) { var code_editor_text_content = null; var code_editor_iframe_javascript_visibility = null; var code_editor_iframe_html_visibility = null; var code_editor_iframe_css_visibility = null; var win = null; var q_str = null; var b = null; var f = null; var function_error_string = null; ////var code_editor_type = null; var editor_type = null; var txt = null; var u = null; ////function_error_string = "Cannot save code editor iframe text content.\nNo code editor is visible\nfunction name: 'save_form_text_data'\nfile: 'code_editor_functions.js'"; ////code_editor_type = get_visible_code_editor_type(win_id); editor_type = code_editor_type; if (!is_code_editor_type_valid(editor_type)) { return; } update_stored_editor_code_text(win_id, editor_type); ////global_code_editor_http_save_data_timer_id = setTimeout(function () { ////code_editor_type = get_visible_code_editor_type(win_id); //editor_type = code_editor_type; //if (!is_code_editor_type_valid(editor_type)) { // return; //} //alert(editor_type); win = document.getElementById(win_id); if (win !== null) { q_str = win.getAttribute("data-content_id"); if (q_str !== null) { b = return_base_path(); f = new FormData(); } else { console.log(function_error_string); return; } } else { console.log(function_error_string); return; } code_editor_text_content = get_code_editor_text_contents(win_id, editor_type); code_editor_iframe_javascript_visibility = get_code_editor_iframe_is_visible_attribute(win_id, editor_type); code_editor_iframe_html_visibility = get_code_editor_iframe_is_visible_attribute(win_id, editor_type); code_editor_iframe_css_visibility = get_code_editor_iframe_is_visible_attribute(win_id, editor_type); ////alert(editor_type); ////if (code_editor_iframe_javascript_visibility === true || code_editor_iframe_javascript_visibility === "true") { if (code_editor_type === "javascript") { //alert("javascript"); u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/set_js?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); txt = code_editor_text_content; f.append("jsTextEditor", txt); http_post_form(u, f); } else if (code_editor_type === "html") { //else if (code_editor_iframe_html_visibility === true || code_editor_iframe_html_visibility === "true") { ////alert("fucking html"); u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/set_content?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); txt = code_editor_text_content; f.append("contentDataTextEditor", txt); http_post_form(u, f); } //else if (code_editor_iframe_css_visibility === true || code_editor_iframe_css_visibility === "true") { // //alert("css"); // u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/set_css?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); // txt = code_editor_text_content; // f.append("cssTextEditor", txt); // http_post_form(u, f); //} //else { // console.log(function_error_string); //} ////clearTimeout(global_code_editor_http_save_data_timer_id); ////}, 250); } async function http_post_form(u, form_data) { var xhr = new XMLHttpRequest(); var response_text = null; xhr.open("POST", u, true); ////xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); ////xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8"); xhr.onreadystatechange = async function () { // Call a function when the state changes. if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { ////alert("Code editor text has been saved."); response_text = await xhr.responseText; return response_text; } } xhr.send(form_data); } //this function is called by a function in the //file: 'javascript_code_editor.js'. //it removes the existing iframe element, and //then creates a new one. the src attribute of //the newly created iframe is set to the URL of //the ASP.NET page to load the iframe with the //respective code type (javascript, html, css). async function place_code_editor_iframe_contents(win_id, code_editor_type = "javascript") { var iframe = null; var iframe_top = null; var iframe_height = null; var iframe_width = null; var code_container = null; var editor_type = null; var base_url = null; var code_text = null; editor_type = code_editor_type; if (!is_code_editor_type_valid(editor_type)) { return; } base_url = return_base_path(); iframe = document.createElement("iframe"); iframe.setAttribute("id", win_id + "_code_editor_iframe_" + editor_type); iframe.setAttribute("data-win_id", win_id); iframe.setAttribute("data-code_editor_type", editor_type); if (document.getElementById(win_id + "_code_editor_iframe_" + editor_type) !== null) { ////document.getElementById(win_id + "_code_editor_iframe_" + editor_type).parentNode.removeChild(document.getElementById(win_id + "_code_editor_iframe_" + editor_type)); } iframe.setAttribute("frameborder", "0"); iframe.style.position = "relative"; iframe.style.left = "0"; iframe_top = get_code_editor_iframe_offset_top(win_id); iframe_height = get_code_editor_iframe_avail_height(win_id); iframe_width = get_dev_panel_width(win_id); iframe.style.top = iframe_top + "px"; iframe.style.height = iframe_height + "px"; iframe.style.width = iframe_width + "px"; //Keep This. this event listener gets //the text content from the code editor /*iframe.addEventListener("load", function () { setTimeout(function () { get_code_editor_text_contents(win_id, editor_type); }, 300); });*/ code_container = document.getElementById(win_id + "_code_editor_container"); if (code_container !== null) { code_container.appendChild(iframe); } //the following code has been placed within the code //editor iframe page, and executes these tasks when //the iframe's page loads. /*iframe.addEventListener("load", async function () { setTimeout(async function () { code_text = await get_code_editor_text_from_server(win_id, editor_type); setTimeout(async function () { insert_new_text_into_code_editor(win_id, editor_type); setTimeout(async function () { update_stored_editor_code_text(win_id, editor_type); }, 500); }, 500); }, 500); });*/ if (editor_type === "javascript") { iframe.setAttribute("src", base_url + "/chromosphere/scripts/js/ui/coding_editors/javascript/code_editor_javascript?r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString()); } else if (editor_type === "html") { iframe.setAttribute("src", base_url + "/chromosphere/scripts/js/ui/coding_editors/html/code_editor_html?r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString()); } else if (editor_type === "css") { iframe.setAttribute("src", base_url + "/chromosphere/scripts/js/ui/coding_editors/css/code_editor_css?r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString()); } return code_text; } //accesses the code editor iframe element and gets //the stored code editor text located in the iframe's //document body. the text is stored within a hidden //input type form element. function get_code_editor_text_contents(win_id, code_editor_type = "javascript") { var editor_type = null; var iframe_id = null; var iframe = null; var imported_element = null; var text_storage_element = null; var code_editor_text_contents = null; editor_type = code_editor_type; if (!is_code_editor_type_valid(editor_type)) { return; } iframe_id = win_id + "_code_editor_iframe_" + editor_type; iframe = document.getElementById(iframe_id); if (iframe !== null) { if (iframe.contentWindow !== null) { text_storage_element = iframe.contentWindow.document.getElementById("store_code_text"); imported_element = document.importNode(text_storage_element, true); if (imported_element !== null) { if (imported_element.hasAttribute("value") === true) { code_editor_text_contents = imported_element.getAttribute("value"); imported_element.remove(); } } } } return code_editor_text_contents; } function return_base_path() { var host = null; var port = null; var protocol = null; var base_url = null; host = window.location.hostname; port = window.location.port; if (port !== "" && port !== null && port !== undefined) { port = ":" + port; } protocol = window.location.protocol + "//"; base_url = protocol + host + port; return base_url; } //this function retrieves the code text (javascript, html, css) //from the database via HTTP. once the code text has been recieved //from the database, the code editor text is removed, then the new //code text is inserted into the code editor. another function is //called to sync the stored code text -- which is stored as a hidden form //element within the iframe's document -- to match the newly retrieved //code text. async function get_code_editor_text_from_server(win_id, code_editor_type = "javascript") { //alert(code_editor_type); var u = null; var b = null; var q_str = null; var win = null; var editor_type = null; var d = null; editor_type = code_editor_type; if (!is_code_editor_type_valid(editor_type)) { return; } win = document.getElementById(win_id); if (win !== null) { q_str = win.getAttribute("data-content_id"); b = return_base_path(); if (editor_type === "javascript") { u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/get_js?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); d = await getDataCodeEditorHTTP(u); return (await d); } else if (editor_type === "html") { u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/get_content?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); d = await getDataCodeEditorHTTP(u); return (await d); } else if (editor_type === "css") { u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/get_css?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); d = await getDataCodeEditorHTTP(u); return (await d); } } } async function getDataCodeEditorHTTP(url) { let d = await fetch(url); let t = await d.text(); return t; } //this function reloads a blank image located inside //the iframe's document element. the image has an "onload" //event listener that calls a function. the function updates //the stored code editor's text to sync with the text inside //the code editor. function update_stored_editor_code_text(win_id, code_editor_type = "javascript") { var editor_type = null; var iframe_id = null; var iframe = null; var update_stored_code_text_image = null; var image_path = null; var image_querystring = null; var new_image_path_and_querystring = null; editor_type = code_editor_type; if (!is_code_editor_type_valid(editor_type)) { return; } iframe_id = win_id + "_code_editor_iframe_" + editor_type; iframe = document.getElementById(iframe_id); if (iframe !== null) { if (iframe.contentWindow !== null) { update_stored_code_text_image = iframe.contentWindow.document.getElementById("update_stored_code_text_image"); if (update_stored_code_text_image !== null) { if (update_stored_code_text_image.hasAttribute("src") === true) { image_path = update_stored_code_text_image.getAttribute("src"); if (typeof (image_path) === "string" && image_path.length > 0) { if (image_path.indexOf("?") > -1.0) { image_path = image_path.split("?")[0]; image_querystring = image_path.split("?")[1]; if (typeof (image_path) === "string" && image_path.length > 0) { image_querystring = parseFloat(Math.floor(Math.random() * 9000000000000) + 1000000000000); if (typeof (image_querystring) === "number") { image_querystring = image_querystring.toString(); new_image_path_and_querystring = (image_path + "?r=" + image_querystring); update_stored_code_text_image.setAttribute("src", new_image_path_and_querystring); } } } } } } } } } //gets the code editor's text (javascript, html, css) that's //stored in the database via HTTP. Get's the unique ID of the //iframe's document that is set using ASP.NET. The unique ID //is stored in the iframe's document as a hidden input form field. //get the names of the functions inside the iframe's document. //call the first function to remove all text inside the iframe's //code editor. next, call the second function which places the //new text from the database into the text editor. async function insert_new_text_into_code_editor(win_id, code_editor_type = "javascript") { var editor_type = null; var iframe_id = null; var iframe = null; var win = null; var q_str = null; var iframe_win = null; var u = null; var b = null; var d = null; editor_type = code_editor_type; if (!is_code_editor_type_valid(editor_type)) { return; } win = document.getElementById(win_id); if (win !== null) { q_str = win.getAttribute("data-content_id"); iframe_id = win_id + "_code_editor_iframe_" + editor_type; iframe = document.getElementById(iframe_id); if (iframe !== null) { if (iframe.contentWindow !== null) { iframe_win = iframe.contentWindow; b = return_base_path(); if (editor_type === "javascript") { u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/get_js?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); d = await getDataCodeEditorHTTP(u); } else if (editor_type === "html") { u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/get_content?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); d = await getDataCodeEditorHTTP(u); } else if (editor_type === "css") { u = b + "/chromosphere/scripts/js/ui/ss/contents_and_js/get_css?i=" + q_str + "&r=" + (Math.floor(Math.random() * 900000000) + 1000000000).toString(); d = await getDataCodeEditorHTTP(u); } else { return; } iframe_win.editor_remove_all_code_editor_text(); iframe_win.editor_insert_code_editor_text(d); } } } } //gets the unique ID of the iframe window's document //that is assigned to the ASP.NET page inside the iframe. //the unique ID is stored in the iframe document and inside //a hidden input form field within the "value" HTML attribute. function get_code_editor_iframe_unique_id(win_id, code_editor_type = "javascript") { var editor_type = null; var iframe_id = null; var iframe = null; var win = null; var iframe_win = null; var hidden_input_obj = null; var unique_id = null; editor_type = code_editor_type; if (!is_code_editor_type_valid(editor_type)) { return; } win = document.getElementById(win_id); if (win !== null) { q_str = win.getAttribute("data-content_id"); iframe_id = win_id + "_code_editor_iframe_" + editor_type; iframe = document.getElementById(iframe_id); if (iframe !== null) { if (iframe.contentWindow !== null) { iframe_win = iframe.contentWindow; hidden_input_obj = iframe_win.document.getElementById("code_editor_unique_id"); if (hidden_input_obj !== null) { if (hidden_input_obj.hasAttribute("value") === true) { unique_id = hidden_input_obj.getAttribute("value"); return unique_id; } } } } } }