shell, wip
[plstackapi.git] / planetstack / core / dashboard / shell / opencloud_shell.js
index 634788a..8d850be 100644 (file)
@@ -95,7 +95,7 @@ ReadLine.prototype = {
   },
 
   insertResponse: function(response) {
-    if(response.length < 3) {
+    if((response.length < 1) || (response=='"donotprintme"')) {
       this.activeLine.parent().append("<p class='response'></p>");
     }
     else {
@@ -116,7 +116,7 @@ var MongoHandler = function() {
   this._rawCommand     = "";
   this._commandStack   = 0;
   this._tutorialPtr    = 0;
-  this._tutorialMax    = 10;
+  this._tutorialMax    = 2;
 
   this._mongo          = {};
   this._mongo.test     = [];
@@ -128,7 +128,7 @@ MongoHandler.prototype = {
   _process: function(inputString, errorCheck) {
     this._rawCommand += ' ' + inputString;
 
-//    try {
+    try {
       inputString += '  '; // fixes certain bugs with the tokenizer.
       var tokens    = inputString.tokens();
       var mongoFunc = this._getCommand(tokens);
@@ -142,19 +142,22 @@ MongoHandler.prototype = {
       else {
         return this._evaluator(tokens);
       }
-//    }
+    }
 
-//    catch(err) {
-//        this._resetCurrentCommand();
-//        return {stack: 0, result: "JS Error: " + err};
-//    }
+    catch(err) {
+        this._resetCurrentCommand();
+        console.trace();
+        return {stack: 0, result: "JS Error: " + err};
+    }
   },
 
   // Calls eval on the input string when ready.
   _evaluator: function(tokens) {
+    isAssignment = tokens.length>=2 && tokens[0].type=="name" && tokens[1].type=="operator" && tokens[1].value=="=";
+
     this._currentCommand += " " + this._massageTokens(tokens);
     if(this._shouldEvaluateCommand(tokens))  {
-        db = "scott";
+        opencloud = new OpenCloud();
         print = this.print;
 
         // So this eval statement is the heart of the REPL.
@@ -165,8 +168,11 @@ MongoHandler.prototype = {
           result = $htmlFormat(result);
         }
         this._resetCurrentCommand();
-        console.log(result);
-        return {stack: this._commandStack, result: result};
+        if (isAssignment) {
+            return {stack: this._commandStack, result: ""};
+        } else {
+            return {stack: this._commandStack, result: result};
+        }
       }
 
     else {
@@ -233,8 +239,8 @@ MongoHandler.prototype = {
   // print output to the screen, e.g., in a loop
   // TODO: remove dependency here
   print: function() {
-   $('.readLine.active').parent().append('<p>' + arguments[0] + '</p>');
-   return "";
+   $('.readLine.active').parent().append('<p>' + JSON.stringify(arguments[0]) + '</p>');
+   return "donotprintme";
   },
 
   /* MongoDB     */
@@ -242,8 +248,58 @@ MongoHandler.prototype = {
 
   // help command
   _help: function() {
-      return PTAG('HELP');
+      return PTAG('HELP') +
+             PTAG('opencloud.slices.listAll()       get all slices');
+
+  },
+
+  _tutorial: function() {
+    this._tutorialPtr = 0;\r
+    return PTAG("This is a self-guided tutorial on the OpenCloud shell.") +\r
+           PTAG("The tutorial is simple, more or less a few basic commands to try.") +\r
+           PTAG("To go directly to any part tutorial, enter one of the commands t0, t1, t2...t10") +\r
+           PTAG("Otherwise, use 'next' and 'back'. Start by typing 'next' and pressing enter.");\r
+  },
 
+  // go to the next step in the tutorial.
+  _next: function() {\r
+    if(this._tutorialPtr < this._tutorialMax) {\r
+      return this['_t' + (this._tutorialPtr + 1)]();\r
+    }\r
+    else {\r
+      return "You've reached the end of the tutorial. To go to the beginning, type 'tutorial'";\r
+    }\r
+  },\r
+\r
+  // go to the previous step in the tutorial.\r
+  _back: function() {\r
+    if(this._tutorialPtr > 1) {\r
+      return this['_t' + (this._tutorialPtr - 1)]();\r
+    }\r
+    else {\r
+      return this._tutorial();\r
+    }\r
+  },
+
+  _t1: function() {
+    this._tutorialPtr = 1;\r
+    return PTAG('1. JavaScript Shell') +\r
+           PTAG('The first thing to notice is that the MongoDB shell is JavaScript-based.') +\r
+           PTAG('So you can do things like:') +\r
+           PTAG('  a = 5; ') +\r
+           PTAG('  a * 10; ') +\r
+           PTAG('  print(a); ') +\r
+           PTAG("  for(i=0; i<10; i++) { print('hello'); }; ") +\r
+           PTAG("Try a few JS commands; when you're ready to move on, enter 'next'");\r
+\r
+  },
+
+  _t2: function() {
+    this._tutorialPtr = 2;\r
+    return PTAG('2. List some slices') +\r
+           PTAG('Type this:') +\r
+           PTAG('    opencloud.slices.listAll();');\r
+\r
   },
 
   _getCommand: function(tokens) {
@@ -251,18 +307,39 @@ MongoHandler.prototype = {
       switch(tokens[0].value.toLowerCase()) {
         case 'help':
           return this._help;
+
+        case 'tutorial':
+          return this._tutorial;\r
+        case 'next':\r
+          return this._next;\r
+        case 'back':\r
+          return this._back;\r
+        case 't0':\r
+          return this._tutorial;\r
+        case 't1':\r
+          return this._t1;
+        case 't2':
+          return this._t2;
       }
     }
   }
 };
 
 $htmlFormat = function(obj) {
-  return tojson(obj, ' ', ' ', true);
+  result=tojson(obj, ' ', ' ', true);
+  return result;
 }
 
-$(document).ready(function() {
+function startTerminal() {
   var mongo       = new MongoHandler();
   var terminal    = new ReadLine({htmlForInput: DefaultInputHtml,
                                   handler: mongo._process,
                                   scoper: mongo});
+  $("#terminal_help1").show();
+  $("#terminal_help2").show();
+  $("#terminal_wait").hide();
+};
+
+$(document).ready(function() {
+    updateOpenCloud(onLoaded = startTerminal);
 });