/*
*
* Author Jean-Philippe SARDA https://twitter.com/jpsarda
* Code released under Common Creative license
* Framework mootools https://mootools.net/
*
*/
var _zo___initVar=0;
var _zo___cursorBlinkVar=0;
var _zo___backspaceLastTime=0;
var _zo___UDarrowLastTime=0;
var _zo___booting=1;
var _zo___currentContext=0;
var _zo___dynamic_load_commands=[];
var _zo___commands=[];
var _zo___history=[];
var _zo___historyIndex=0;
var _zo___historyCurrentLine='';
var _zo___saveRenew=1;
var _zo___initRestore=0;
var _zo___space='\u00A0';
var _zo___server_root='';
window.addEvent('domready', function() {_zo___initEngineOnce(); _zo___startEngine();});
window.addEvent ('resize', function() {_zo___windowResize();});
window.addEvent ('click', function() {_zo___focusForce();});
//window.addEvent ('keypress', function() {focusForce();});
//window.addEvent ('keydown', function() {focusForce();});
//window.addEvent ('keyup', function() {focusForce();});
window.addEvent ('focus', function() {_zo___focusForce();});
// Command base class
//
var BaseCommand = new Class({
//We're using options
Implements: Options,
//Set our default options
options : {
context : 0 //object of class Context
},
//Command name and shortcut
name : '',
shortcut : '',
//Command shows up on commands list
hidden : false,
//Have our initialization function
//set the options using setOptions
initialize : function(options){
this.setOptions(options);
},
//call this method to release all allocations
release : function(){
//clean timers, memory, listeners
//...
},
//call this method to end the command execution
over : function(){
this.release();
//inform the OS that the command is over.
this.options.context.over();
},
//call this method when ending the command execution
output : function(str){
this.options.context.output(str);
},
//call this method to get user input
input : function(){
this.options.context.input();
},
//to override to implement the command logic
execute : function(arg){
//output a line on the console
//this.output('Unknown command, type "help"');
this.over();
},
//Common message when a server request fails
command_ooo : function(){
this.output('Sorry this command is temporarly out of service');
},
//
// Called by the OS
//
//called by the OS, execute the command with the arguments arg (string)
start : function(ctxt,arg){
this.options.context=ctxt;
this.execute(arg);
},
//called by the OS, new user entry
user_entry : function(entry) {
//...
},
//called by the OS, returns a one line help about the command
help : function() {
return '';
},
//called by the OS, force exit (the context is inactive after this call)
force_exit : function(arg){
this.release();
}
});
// Commands
//
var AboutCommand = new Class({
Extends : BaseCommand,
name : 'about',
shortcut : '??',
execute : function(arg){
this.output('zeOracle.com version 0.11');
this.output("zeOracle is a Javascript OS, designed and developed by JP SARDA powered by mootools, follow us on twitter.com/zeOracle");
this.over();
},
help : function() {
return 'Output informations about zeOracle and its authors';
}
});
_zo___commands.push(new BaseCommand({}));
_zo___commands.push(new AboutCommand({}));
var _zo___addCommand = function(cmd) {
var index=-1;
var i=0;
_zo___commands.each(function(item) {
if (item.name==cmd.name) {
index=i;
}
i++;
});
if (index>=0) {
_zo___commands[index]=cmd;
} else {
_zo___commands.push(cmd);
}
}
// Context class
//
var Context = new Class({
//We're using options
Implements: Options,
//Set our default options
options : {
active : false, //is context active or not
cmd : 0 //corresponding BaseCommand (or extended) object
},
//Have our initialization function
//set the options using setOptions
initialize : function(options){
this.setOptions(options);
},
//call this method to release the context and force_exit the command
force_exit : function(){
cmd.force_exit();
this.over();
},
//call this method to start the command execution
start : function(arg){
this.options.active=true;
this.options.cmd.start(this,arg);
},
//call this method to end the command execution
over : function(){
this.input();
this.options.active=false;
_zo___currentContext=0;
},
//call this method when ending the command execution
output : function(str){
if (!this.options.active) return;
_zo___appendLine(str,0);
},
//call this method to get user input
input : function(){
if (!this.options.active) return;
_zo___inputVisible(1);
}
});
// Starting engine
//
var _zo___startEngine = function() {
_zo___booting=1;
_zo___inputVisible(0);
_zo___appendLine('zeOracle.com version 0.11',0);
clearTimeout(_zo___initVar);
var timerDelay=60;
if (_zo___initCmds.length>0) timerDelay=20;
_zo___initVar = _zo___init.periodical(timerDelay);
}
var _zo___keyDownTest = function(event) {
if (event.key=='backspace') _zo___backspaceEnter();
else if (event.key=='up') _zo___UDarrowEnter(-1);
else if (event.key=='down') _zo___UDarrowEnter(1);
}
var _zo___initEngineOnce = function() {
$('userentry').setStyle('width', 1);
_zo___windowResize();
_zo___blinkActive(1);
$('userform').addEvent('submit', function(event) {event.stopPropagation(); _zo___enter(); return false;});
$('userentry').addEvent('focus', function(event) {_zo___blinkActive(1);});
$('userentry').addEvent('blur', function(event) {_zo___blinkActive(0);});
$('userentry').addEvent('keyup', function(event) {_zo___updateNewKey(event);});
$('userentry').addEvent('keydown', function(event) {_zo___updateNewKey(event); _zo___keyDownTest(event);});
$('userentry').addEvent('keypress', function(event) {_zo___updateNewKey(event); _zo___keyDownTest(event);});
}
var _zo___clear = function() {
_zo___saveRenew=1;
$('currententry').set('html','');
$('userentry').set('value','');
$('toplines').set('html','');
_zo___inputVisible(1);
}
var _zo___blinkActive = function(b) {
if (b==1) {
if (_zo___cursorBlinkVar==0) {
$('cursor').removeClass('invisible');
_zo___cursorBlinkVar=_zo___cursorBlink.periodical(500);
}
} else {
if (_zo___cursorBlinkVar!=0) {
$('cursor').addClass('invisible');
clearTimeout(_zo___cursorBlinkVar); _zo___cursorBlinkVar=0;
}
}
}
// Keyboard
//
var _zo___cursorBlink = function() {
$('cursor').toggleClass('invisible');
}
var _zo___backspaceEnter = function() {
var timestamp=new Date().getTime();
if (timestamp-_zo___backspaceLastTime>20) {
var current=$('currententry').get('html');
//debugLog('backspaceEnter current.length='+current.length);
if (current.length>0) {
var cut=1;
var charToDelete=current.substr(current.length-1,1);
if (charToDelete==';') {
var special;
if (current.length>=6) {
special=current.substr(current.length-6,6);
if ((special==' ')) cut=6;
}
if (current.length>=5) {
special=current.substr(current.length-5,5);
if ((special=='&')) cut=5;
}
if (current.length>=4) {
special=current.substr(current.length-4,4);
if ((special=='>')||(special=='<')) cut=4;
}
//debugLog('backspaceEnter '+special+' '+cut);
}
$('currententry').set('html',current.substr(0,current.length-cut));
_zo___historyIndex=_zo___history.length;
}
_zo___backspaceLastTime=timestamp;
}
}
var _zo___UDarrowEnter = function(add) {
var timestamp=new Date().getTime();
if (timestamp-_zo___UDarrowLastTime>20) {
if (_zo___historyIndex==_zo___history.length) {
_zo___historyCurrentLine=$('currententry').get('html');
}
var newHistoryIndex=_zo___historyIndex+add;
//_zo___debugLog(newHistoryIndex+' '+_zo___history.length);
if ((newHistoryIndex>=0)&&(newHistoryIndex<_zo___history.length)) {
$('currententry').set('html',_zo___history[newHistoryIndex]);
_zo___historyIndex=newHistoryIndex;
} else if (newHistoryIndex==_zo___history.length) {
$('currententry').set('html',_zo___historyCurrentLine);
_zo___historyIndex=newHistoryIndex;
}
_zo___UDarrowLastTime=timestamp;
}
}
var _zo___updateNewKey = function(event) {
if (_zo___booting==0) {
var usercurrententry=$('currententry');
//console.log("["+$('userentry').get('value')+"]");
var key=$('userentry').get('value');
if (key==' ') key=_zo___space;
var before=usercurrententry.get('html');
usercurrententry.appendText(key);
//console.log(usercurrententry);
if (_zo___historyIndex<_zo___history.length) {
if (before.length<(usercurrententry.get('html')).length) {
_zo___historyIndex=_zo___history.length;
}
}
}
$('userentry').set('value','')
//debugLog('updateEnter');
}
// Input
//
var _zo___nextRestoreCommandFunctionVar=0;
var _zo___nextRestoreCommand = '';
var _zo___nextRestoreCommandFunction = function() {
clearTimeout(_zo___nextRestoreCommandFunctionVar); _zo___nextRestoreCommandFunctionVar=0;
_zo___initRestore++;
_zo___enterEntry(_zo___nextRestoreCommand);
}
var _zo___inputVisible = function(b) {
if (b==1) {
$('input').setStyle('visibility','');
_zo___focusForce();
_zo___waitAnim(0);
//Command restore ?
if (_zo___initRestore>=0) {
if (_zo___initRestore>=_zo___initCmds.length) {
_zo___initRestore=-1;
} else {
if (_zo___nextRestoreCommandFunctionVar==0) {
_zo___nextRestoreCommand=_zo___initCmds[_zo___initRestore];
_zo___nextRestoreCommandFunctionVar=_zo___nextRestoreCommandFunction.bind(this).periodical(1);
}
}
}
} else {
$('input').setStyle('visibility','hidden');
_zo___waitAnim(1);
}
window.scrollBy(0,100000); // horizontal and vertical scroll increments
}
var _zo___enter = function() {
if (_zo___booting!=0) return;
//prepare
_zo___updateNewKey(null);
var entry=$('currententry').get('html')+$('userentry').get('value');
//console.log(entry);
_zo___enterEntry(entry);
}
var _zo___enterEntry = function(entry) {
entry=entry.clean();
entry=entry.replace(' ',' ');
_zo___inputVisible(0);
//erase
$('currententry').set('html','');
_zo___appendLine(entry,1);
_zo___history.push(entry);
_zo___historyIndex=_zo___history.length;
if (_zo___currentContext!=0) {
_zo___currentContext.options.cmd.user_entry(entry);
} else {
//entry=(entry.toLowerCase());
var cmd=(entry.split(' ',1))[0];
cmd=cmd.toLowerCase();
var commandFound=false;
//now you call the myArrayFunction for EACH item within the array
_zo___commands.each(function(item) {
//debugLog('item.cmd='+item.cmd);
if (_zo___currentContext==0) {
if ((item.name==cmd)||((item.shortcut==cmd)&&(cmd!=''))) {
commandFound=true;
var arg=entry.substr(cmd.length).clean();
if ((arg=="?")||(arg=="help")) {
_zo___result(item.help());
} else {
arg=arg.replace(/>/gi,'>').replace(/</gi,'<').replace(/&/gi,'&');
_zo___currentContext=new Context({cmd:item});
_zo___currentContext.start(arg);
}
//break;
}
}
});
if (!commandFound) {
_zo___result("Unknown command (type \"help\")");
}
}
}
var _zo___result = function(txt) {
_zo___appendLine(txt,0);
_zo___inputVisible(1);
}
_zo___dynamic_load_commands.push({
js:_zo___server_root+'/js/twitter.js' ,
fn : function(){ _zo___commands.push( new TwitterCommand({}) ); }
});
_zo___dynamic_load_commands.push({
js:_zo___server_root+'/js/translate.js' ,
fn : function(){ _zo___commands.push( new TranslateCommand({}) ); }
});
_zo___dynamic_load_commands.push({
js:_zo___server_root+'/js/google.js' ,
fn : function(){ _zo___commands.push( new GoogleCommand({}) ); }
});
_zo___dynamic_load_commands.push({
js:_zo___server_root+'/js/will.js' ,
fn : function(){ _zo___commands.push( new WillCommand({}) ); }
});
_zo___dynamic_load_commands.push({
js:_zo___server_root+'/js/sdk.js' ,
fn : function(){ _zo___commands.push( new SdkCommand({}) ); }
});
_zo___dynamic_load_commands.push({
js:_zo___server_root+'/js/generic.js' ,
fn : function(){ _zo___commands.push( new HelpCommand({}) ); _zo___commands.push( new ClearCommand({}) ); _zo___commands.push( new RebootCommand({}) ); }
});
_zo___dynamic_load_commands.push({
js:_zo___server_root+'/js/hotsave.js' ,
fn : function(){ _zo___commands.push( new HotCommand({}) ); _zo___commands.push( new SaveCommand({}) ); }
});
var _zo___unloaded_commands_count=_zo___dynamic_load_commands.count;
var _zo___load_commands = function() {
_zo___dynamic_load_commands.each(function(item) {
var loadcmd = Asset.javascript(item.js, {
id: 'loadcmd_'+item.js,
onLoad: function(){ item.fn(); _zo___unloaded_commands_count--; }
});
});
}
// Initialization
//
var _zo___initIndex=0;
var _zo___initIndexReady=0;
var _zo___initReadyIndex=0;
var _zo___initReady = function () {
//alert('toto');
_zo___initReadyIndex++;
if (_zo___initReadyIndex==1) {
if (_zo___initIndexReady==0) {
_zo___appendInLine('Core ',0);
} else if (_zo___initIndexReady==1) {
_zo___appendInLine('Interface ',0);
} else if (_zo___initIndexReady==2) {
_zo___appendInLine('Commands ',0);
_zo___load_commands();
}
} else if ((_zo___initReadyIndex>=1)&&(_zo___initReadyIndex<=Number.random(5, 10))) {
_zo___appendInLine('.',0);
} else {
if (_zo___initIndexReady==2) { //loading commands
if (_zo___unloaded_commands_count>0) return; //still wait
}
_zo___appendLine(' ready',0);
_zo___initReadyIndex=0;
_zo___initIndexReady++;
_zo___initIndex++; //next init line
}
}
var _zo___initLines=[];
_zo___initLines.push(_zo___initReady);
_zo___initLines.push(_zo___initReady);
_zo___initLines.push(_zo___initReady);
_zo___initLines.push(' ____ _ ');
_zo___initLines.push(' / __ \\ | | ');
_zo___initLines.push(' _______| | | |_ __ __ _ ___| | ___ ');
_zo___initLines.push(' |_ / _ \\ | | | \'__/ _` |/ __| |/ _ \\');
_zo___initLines.push(' / / __/ |__| | | | (_| | (__| | __/');
_zo___initLines.push(' /___\\___|\\____/|_| \\__,_|\\___|_|\\___|');
_zo___initLines.push('');
_zo___initLines.push('Type "help" for commands list');
_zo___initLines.push('zeOracle is listening to you');
var _zo___init = function() {
if (Number.random(0, 100) < 30) return;
//_zo___debugLog('init() _zo___initIndex='+_zo___initIndex+' '+_zo___initLines[_zo___initIndex]);
if (_zo___initLines[_zo___initIndex] !== undefined) {
var nextLine=_zo___initLines[_zo___initIndex];
if (typeof(nextLine)=='function') {
nextLine();
} else {
_zo___appendLine(nextLine,0,1);
_zo___initIndex++;
}
} else {
clearTimeout(_zo___initVar); _zo___initVar=0;
_zo___booting=0;
_zo___inputVisible(1);
}
}
// Utils
//
var _zo___appendInLine = function(line,prompt) {
var toplines=$('toplines');
if (prompt==1) {
toplines.appendText('> '+line);
} else {
toplines.appendText(line);
}
}
var _zo___appendLine = function(line,prompt,transform) {
//$('toplines').appendText('> '+line+'\n');
var toplines=$('toplines');
if (transform==1) {
line=line.replace(/ /gi,' ');
}
if (prompt==1) {
toplines.set('html',toplines.get('html')+'> '+line+'
');
} else {
toplines.set('html',toplines.get('html')+line+'
');
}
//debugLog(line);
window.scrollBy(0,100000); // horizontal and vertical scroll increments
}
var _zo___replaceTweet = function(str) {
var toret=str.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"$1");
toret=toret.replace(/@([A-Z0-9_]+)/gi,"@$1");
return toret;
}
var _zo___windowResize = function() {
//alert('Engine started!');
var size = window.getSize();
//$('userentry').setStyle('width', size.x-30);
//alert("The window is "+size.x+" pixels wide and "+size.y+"pixels high.");
}
var _zo___focusForce = function() {
$('userentry').focus();
}
var _zo___waitAnimChars=['-','\\','|','/'];
var _zo___waitAnimCharsIndex=0;
var _zo___waitAnimVar=0;
var _zo___waitAnim=function(b) {
if (_zo___waitAnimVar!=0) {
clearTimeout(_zo___waitAnimVar);
_zo___waitAnimVar=0;
$('waitanim').set('html','');
$('waitanim').addClass('invisible');
}
if (b==1) {
$('waitanim').removeClass('invisible');
_zo___waitAnimVar=_zo___waitAnimStep.periodical(50);
}
}
var _zo___waitAnimStep=function() {
_zo___waitAnimCharsIndex++;
if (_zo___waitAnimCharsIndex>=_zo___waitAnimChars.length) _zo___waitAnimCharsIndex=0;
$('waitanim').set('html',_zo___waitAnimChars[_zo___waitAnimCharsIndex]);
}
var _zo___debugLog = function(line) {
//$('toplines').appendText('> '+line+'\n');
var debug=$('debug');
debug.set('html',debug.get('html')+'> '+line+'
');
}