"use strict"; var FileStore = function(storage, prefix) { var self = this; this.saveFile = function(name, text) { storage.setItem(prefix + name, text); } this.loadFile = function(name) { return storage.getItem(prefix + name) || storage.getItem(name); } this.getFiles = function(prefix2) { var files = []; for (var i = 0; i < storage.length; i++) { var key = storage.key(i); if (key.startsWith(prefix + prefix2)) { var name = key.substring(prefix.length + prefix2.length); files.push(name); } else if (key.startsWith(prefix2)) { var name = key.substring(prefix2.length); files.push(name); } } return files; } this.deleteFile = function(name) { storage.removeItem(name); storage.removeItem('local/' + name); } } // 8bitworkshop IDE user interface var worker = new Worker("./src/worker/workermain.js"); var current_output = null; var current_preset_index = -1; // TODO: use URL var current_preset_id = null; var offset2line = null; var line2offset = null; var pcvisits; var trace_pending_at_pc; var store; var PRESETS, platform, platform_id; var CODE = 'code1'; var editor = CodeMirror(document.getElementById('editor'), { mode: '6502', theme: 'mbo', lineNumbers: true, tabSize: 8, gutters: ["CodeMirror-linenumbers", "gutter-offset", "gutter-bytes", "gutter-clock", "gutter-info"], }); //editor.setSize("100%", "95%"); // TODO editor.on('changes', function(ed, changeobj) { var text = editor.getValue() || ""; setCode(text); }); function getCurrentPresetTitle() { if (current_preset_index < 0) return "ROM"; else return PRESETS[current_preset_index].title || PRESETS[current_preset_index].name || "ROM"; } function setLastPreset(id) { localStorage.setItem("__lastid", id); localStorage.setItem("__lastplatform", platform_id); } function updatePreset(current_preset_id, text) { if (text.trim().length) { store.saveFile(current_preset_id, text); } } function loadCode(text) { editor.setValue(text); current_output = null; setCode(text); } function loadFile(fileid, filename, index) { current_preset_id = fileid; current_preset_index = index; var text = store.loadFile(fileid)|| ""; if (text) { loadCode(text); setLastPreset(fileid); } else if (!text && index >= 0) { filename += ".a"; console.log("Loading preset", fileid, filename, index, PRESETS[index]); if (text.length == 0) { console.log("Fetching", filename); $.get( filename, function( text ) { console.log("GET",text.length,'bytes'); loadCode(text); setLastPreset(fileid); }, 'text'); } } else { $.get( "presets/"+platform_id+"/skeleton.a", function( text ) { loadCode(text); setLastPreset(fileid); updatePreset(fileid, text); }, 'text'); } } function loadPreset(preset_id) { // TODO var index = parseInt(preset_id+""); for (var i=0; i= 0) { // load the preset loadFile(preset_id, "presets/" + platform_id + "/" + PRESETS[index].id, index); } else { // no preset found? load local loadFile(preset_id, "local/" + platform_id + "/" + preset_id, -1); } } function gotoPresetAt(index) { var index = (index + PRESETS.length) % PRESETS.length; qs['file'] = PRESETS[index].id; window.location = "?" + $.param(qs); } function gotoPresetNamed(id) { if (id.startsWith("_")) { var result = eval(id+"()"); console.log(id, result); if (!result) { updateSelector(); } } else { qs['platform'] = platform_id; qs['file'] = id; window.location = "?" + $.param(qs); } } function _createNewFile(e) { var filename = prompt("Create New File", "newfile.a"); if (filename && filename.length) { if (filename.indexOf(".") < 0) { filename += ".a"; } qs['file'] = "local/" + filename; window.location = "?" + $.param(qs); } return true; } function _shareFile(e) { if (current_output == null) { alert("Please fix errors before sharing."); return true; } var text = editor.getValue(); console.log("POST",text.length,'bytes'); $.post({ url: 'share.php', data: { 'platform':'vcs', /// TODO 'filename':current_preset_id.split('/').pop(), 'text':text, }, error: function(e) { console.log(e); alert("Error sharing file."); }, success: function(result) { var sharekey = result['key']; var url = "http://8bitworkshop.com/?sharekey=" + sharekey; window.prompt("Copy link to clipboard (Ctrl+C, Enter)", url); } }); return true; } function _resetPreset(e) { if (current_preset_index < 0) { alert("Can only reset built-in file examples.") } else if (confirm("Reset '" + PRESETS[current_preset_index].name + "' to default?")) { qs['reset'] = '1'; window.location = "?" + $.param(qs); } return true; } function populateExamples(sel) { sel.append($("