/**
* 入力した定義をダウンロードする
*/
function downloadbuttonClick () {
let element = document.getElementById('code');
var code = element.value;
const fileContent = "" + code;
const fileNameElement = document.getElementById('filename');
const fileName = fileNameElement.value + '.txt';
const a = document.createElement('a');
a.href = 'data:text/plain,' + encodeURIComponent(fileContent);
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a); // ※ DOM が構築されてからでないとエラーになる
a.click();
document.body.removeChild(a);
}
/* 読み込み中マーク (spinner) を表示 */
function openModal() {
var element =
'
' + /* gray background */
'';
var target = document.getElementById('div2');
target.insertAdjacentHTML('beforebegin',element);
}
/* 読み込み中マーク (spinner) を削除 */
function closeModal() {
document.getElementById("container").remove();
document.getElementById('container-loader').remove();
}
function sleep(milliSeconds) {
return new Promise((resolve) => {
setTimeout(() => resolve(), milliSeconds);
});
}
/**
* ファイルをロードしてワークスペースにそのプログラムを追加する
*/
function loadbuttonClick () {
// https://kuroeveryday.blogspot.com/2015/07/javascript-upload-download.html より
console.log("button-down");
const uploadFile = document.getElementById('upload-file');
const uploadcode = document.getElementById('upload-code');
uploadFile.accept = ".txt"
const file = uploadFile.files[0];
if (!file) { // ファイルが選ばれていなかった場合
uploadFile.click(); // ファイル選択ボタンをクリックしたことにする
} else if (file.name.slice(-3) !== 'txt') {
console.log (file.name.slice(-3))
alert('.txt ファイルを選択してください。');
}else {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = (() => {
// 以下 onClickConvert と同じ処理
const code = reader.result;
if (code) {
openModal();
setTimeout(function() {
uploadcode.value = code;
closeModal();
}, 10);
};
});
const fileName = file.name.slice(0, -3);
const fileNameElement = document.getElementById('filename');
fileNameElement.value = fileName;
}
}
/*download tex file*/
function texdownloadbuttonClick () {
let element = document.getElementById('tex_code');
var code = element.value;
const fileContent = "" + code;
const fileName = 'proof_tree.tex';
const a = document.createElement('a');
a.href = 'data:text/plain,' + encodeURIComponent(fileContent);
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a); // ※ DOM が構築されてからでないとエラーになる
a.click();
document.body.removeChild(a);
}
/**
* すでにある規則を使う
*/
function loadExistRule () {
const uploadFile = document.getElementById('select_exist_rule');
const button = document.getElementById('b3');
const uploadcode = document.getElementById('upload-existcode');
index = uploadFile.options[uploadFile.selectedIndex].value;
//console.log (index);
path = "../rule/" + index + ".txt";
console.log(path);
fetch (path)
.then((data) => data.text())
.then((code) =>{
console.log(code)
uploadcode.value = code
})
.catch(error => {
console.log("失敗しました");
})}
window.addEventListener('load', function () {
let downloadbutton = document.getElementById('b1');
downloadbutton.addEventListener('click', downloadbuttonClick);});
window.addEventListener('load', function () {
let uploadbutton = document.getElementById('b2');
uploadbutton.addEventListener('mousedown', loadbuttonClick, true)});
window.addEventListener('load', function () {
let downloadbutton = document.getElementById('tex');
downloadbutton.addEventListener('click',texdownloadbuttonClick);})
window.addEventListener('load', function () {
let downloadbutton = document.getElementById('b3');
console.log("load");
downloadbutton.addEventListener('click', loadExistRule);})