2 * interface.js - The Atlantis user interface
3 * (c) 2008 Sander Dijkhuis <sander.dijkhuis@gmail.com>
4 * Still need to decide about licensing. Ask if interested.
15 commandElts: { // very bad probably
25 commands: new Array(),
26 initFunctions: new Array(),
30 this.contentElement = document.getElementById('content');
32 this.commandElts.block = document.getElementById('command-block');
33 this.commandElts.string = document.getElementById('command-string');
34 this.commandElts.alternatives =
35 document.getElementById('command-alternatives');
37 this.messageElt = document.getElementById('message');
39 this.addDefaultCommands();
41 this.toggleCommandMode(false);
44 this.contentElement.focus();
46 for (i in this.initFunctions)
47 this.initFunctions[i]();
49 this.initialized = true;
52 addInitFunction: function(f) {
56 this.initFunctions.push(f);
59 toggleCommandMode: function(on) {
61 this.commandElts.block.style.visibility = on? 'visible' : 'hidden';
63 this.setCommandAlternatives('');
64 this.setCommandString('');
68 addCommandChar: function(c) {
69 this.setCommandString(this.commandString + c);
70 this.updateCommandMatches();
73 updateCommandMatches: function() {
74 this.commandMatches = new Array();
77 for (i in this.commands) {
78 match = this.matchCommand(this.commands[i].name,
82 this.commandMatches.push(this.commands[i].name);
86 this.setCommandAlternatives(s);
89 matchCommand: function(command, input) {
97 if (input[i] == c && !foundIt) {
99 if (i == input.length)
101 html += '<b>' + c + '</b>';
113 deleteCommandChar: function() {
114 this.setCommandString(this.commandString.substring(0,
117 this.updateCommandMatches();
120 setCommandString: function(s) {
121 this.commandString = this.commandElts.string.innerHTML = s;
124 setCommandAlternatives: function(s) {
125 this.commandElts.alternatives.style.visibility =
126 s? 'visible' : 'hidden';
127 this.commandElts.alternatives.innerHTML = s;
130 execute: function(command) {
132 for (i in this.commands)
133 if (command == this.commands[i].name)
134 return this.commands[i].run();
137 showUnknownCommand: function(command) {
138 this.showMessage('Unknown command: <b>' + command + '</b>');
141 showMessage: function(s) {
142 this.messageShown = true;
143 this.messageElt.style.visibility = 'visible';
144 this.messageElt.childNodes[0].innerHTML = s;
147 hideMessage: function() {
148 this.messageShown = false;
149 this.messageElt.style.visibility = 'hidden';
152 insertHTML: function(s) {
153 document.execCommand('insertHTML', undefined, s);
156 addCommand: function(command) {
157 this.commands.push(command);
158 this.commands.sort();
161 getXMLHttpRequestObject: function() {
164 if (window.XMLHttpRequest)
165 obj = new XMLHttpRequest();
166 else if (window.ActiveXObject)
167 obj = new ActiveXObject('Microsoft.XMLHTTP');
172 loadService: function(serviceId) {
173 this.loadScript('/-/' + serviceId + '/main.js');
176 loadScript: function(url) {
177 var xhr = this.getXMLHttpRequestObject();
178 xhr.onreadystatechange = function() {
179 if (xhr.readyState != 4)
181 if (xhr.status == 200)
182 (function() { eval(xhr.responseText); })();
184 xhr.open('GET', url, true);
189 /* temporary function for adding temporary commands quickly and uglily */
190 Atlantis.addDefaultCommands = function() {
195 ['undo', '', function() {
196 document.execCommand('undo', undefined, undefined);
198 ['redo', '', function() {
199 document.execCommand('redo', undefined, undefined);
201 ['new document', '', function() {
202 // doesn't work if in <ul>
203 Atlantis.insertHTML('<hr class=doc-char>');
205 ['commands', '', function() {
208 s = '<h3>Commands</h3><ul>';
209 for (i in Atlantis.commands) {
210 command = Atlantis.commands[i];
211 s += '<li><b>' + command.name.replace(/_/g, ' ') + '</b>';
213 s += ' - ' + command.doc;
216 Atlantis.insertHTML(s);
218 ['hello world', '', function() {
219 Atlantis.insertHTML('Hello, World!');
224 cmd = new Atlantis.Command();
225 cmd.name = cmds[i][0];
226 cmd.doc = cmds[i][1];
227 cmd.run = cmds[i][2];
228 cmd.data = cmds[i][3];
229 Atlantis.addCommand(cmd);
232 cmd = new Atlantis.Command();
233 cmd.name = 'data test';
234 cmd.doc = 'Testing data.';
236 cmd.run = function() {
237 Atlantis.insertHTML(cmd.data.i++);
239 Atlantis.addCommand(cmd);
242 Atlantis.Command = function() {
245 Atlantis.Command.prototype = {
250 toString: function() {
255 function onkeydown(evt) {
256 if (Atlantis.messageShown)
257 Atlantis.hideMessage();
259 if (evt.keyCode == Atlantis.CTRL_KEY_CODE) {
260 Atlantis.toggleCommandMode(true);
265 function onkeyup(evt) {
266 if (evt.keyCode == Atlantis.CTRL_KEY_CODE) {
267 if (Atlantis.commandString) {
268 if (Atlantis.commandMatches.length > 0)
269 Atlantis.execute(Atlantis.commandMatches[0]);
271 Atlantis.showUnknownCommand(Atlantis.commandString);
273 Atlantis.toggleCommandMode(false);
277 function onkeypress(evt) {
278 if (Atlantis.commanding) {
279 if (evt.which == Atlantis.BACKSP_KEY_CODE)
280 Atlantis.deleteCommandChar();
282 Atlantis.addCommandChar(String.fromCharCode(evt.charCode));
284 } else if (evt.keyCode == Atlantis.DOC_KEY_CODE)
285 Atlantis.execute('new document');
288 function oncontextmenu(evt) {
292 function onload(evt) {