fix donotprintme printed by print()
[plstackapi.git] / planetstack / core / static / shell / opencloud_shell.js
index 8d850be..b08dbfa 100644 (file)
@@ -95,7 +95,7 @@ ReadLine.prototype = {
   },
 
   insertResponse: function(response) {
-    if((response.length < 1) || (response=='"donotprintme"')) {
+    if((response.length < 1) || (response=='"donotprintme"') || (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    = 2;
+  this._tutorialMax    = 4;
 
   this._mongo          = {};
   this._mongo.test     = [];
@@ -249,84 +249,144 @@ MongoHandler.prototype = {
   // help command
   _help: function() {
       return PTAG('HELP') +
-             PTAG('opencloud.slices.listAll()       get all slices');
+             PTAG('opencloud                                 list opencloud API object types') +
+             PTAG('opencloud.slices                          list methods to can call on slices') +
+             PTAG('opencloud.slices.all()                    get all slices') +
+             PTAG('opencloud.slices.filter({key: "value"})   filter using dictionary') +
+             PTAG('opencloud.slices.get({key: "value"}))     get using dictionary')
 
   },
 
   _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
+    this._tutorialPtr = 0;
+    return PTAG("This is a self-guided tutorial on the OpenCloud shell.") +
+           PTAG("The tutorial is simple, more or less a few basic commands to try.") +
+           PTAG("To go directly to any part tutorial, enter one of the commands t0, t1, t2...t10") +
+           PTAG("Otherwise, use 'next' and 'back'. Start by typing 'next' and pressing enter.");
   },
 
   // 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
+  _next: function() {
+    if(this._tutorialPtr < this._tutorialMax) {
+      return this['_t' + (this._tutorialPtr + 1)]();
+    }
+    else {
+      return "You've reached the end of the tutorial. To go to the beginning, type 'tutorial'";
+    }
+  },
+
+  // go to the previous step in the tutorial.
+  _back: function() {
+    if(this._tutorialPtr > 1) {
+      return this['_t' + (this._tutorialPtr - 1)]();
+    }
+    else {
+      return this._tutorial();
+    }
   },
 
   _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
+    this._tutorialPtr = 1;
+    return PTAG('1. JavaScript Shell') +
+           PTAG('The first thing to notice is that the MongoDB shell is JavaScript-based.') +
+           PTAG('So you can do things like:') +
+           PTAG('  a = 5; ') +
+           PTAG('  a * 10; ') +
+           PTAG('  print(a); ') +
+           PTAG("  for(i=0; i<10; i++) { print('hello'); }; ") +
+           PTAG("Try a few JS commands; when you're ready to move on, enter 'next'");
+
   },
 
   _t2: function() {
-    this._tutorialPtr = 2;\r
-    return PTAG('2. List some slices') +\r
-           PTAG('Type this:') +\r
-           PTAG('    opencloud.slices.listAll();');\r
-\r
+    this._tutorialPtr = 2;
+    return PTAG('2. List some objects') +
+           PTAG('Try these:') +
+           PTAG('    opencloud.slices.all();') +
+           PTAG('    opencloud.slivers.all();') +
+           PTAG('    opencloud.sites.all();') +
+           PTAG('    opencloud.nodes.all();');
+
+  },
+
+  _t3: function() {
+    this._tutorialPtr = 3;
+    return PTAG('3. Filter some objects') +
+           PTAG('Try these:') +
+           PTAG('    opencloud.slices.get({name: "HyperCache"});');
+           PTAG('    opencloud.nodes.filter({site_id: opencloud.sites.get({name: "Princeton"})["id"]});');
+
+  },
+
+  _t4: function() {
+    this._tutorialPtr = 4;
+    return PTAG('4. Available OpenCloud objects and methods') +
+           PTAG('Try these:') +
+           PTAG('    opencloud;') +
+           PTAG('    opencloud.nodes;');
+
   },
 
   _getCommand: function(tokens) {
-    if(tokens[0] && MongoKeywords.include((tokens[0].value + '').toLowerCase())) {
+    if(tokens[0] && ArrayInclude(MongoKeywords,(tokens[0].value + '').toLowerCase())) {
       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._tutorial;
+        case 'next':
+          return this._next;
+        case 'back':
+          return this._back;
+        case 't0':
+          return this._tutorial;
+        case 't1':
           return this._t1;
         case 't2':
           return this._t2;
+        case 't3':
+          return this._t3;
+        case 't4':
+          return this._t4;
       }
     }
   }
 };
 
+function replaceAll(find, replace, str) {
+  return str.replace(new RegExp(find, 'g'), replace);\r
+}
+
+/* stackoverflow: http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript */
+function syntaxHighlight(json) {
+    if ( json.hasOwnProperty("__str__")) {
+        return syntaxHighlight(json.__str__());
+    }
+    if (typeof json != 'string') {\r
+         json = JSON.stringify(json, undefined, "\t");\r
+    }\r
+    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\r
+    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {\r
+        var cls = 'terminal_number';\r
+        if (/^"/.test(match)) {\r
+            if (/:$/.test(match)) {\r
+                cls = 'terminal_key';\r
+            } else {\r
+                cls = 'terminal_string';\r
+            }\r
+        } else if (/true|false/.test(match)) {\r
+            cls = 'terminal_boolean';\r
+        } else if (/null/.test(match)) {\r
+            cls = 'terminal_null';\r
+        }\r
+        return '<span class="' + cls + '">' + match + '</span>';\r
+    });\r
+}
+
 $htmlFormat = function(obj) {
-  result=tojson(obj, ' ', ' ', true);
+  //JSON.stringify(obj,undefined,2)
+  result=replaceAll("\t","&nbsp;",replaceAll("\n","<br>",syntaxHighlight(obj))); //tojson(obj, ' ', ' ', true);
   return result;
 }
 
@@ -338,6 +398,8 @@ function startTerminal() {
   $("#terminal_help1").show();
   $("#terminal_help2").show();
   $("#terminal_wait").hide();
+
+  $("#terminal").bind('click', function() { $(".readLine.active").focus(); });
 };
 
 $(document).ready(function() {