      1 function CommandoAssistant() {
      2 };
      3
      4 CommandoAssistant.prototype.setup = function() {
      5   this.controller.setupWidget(
      6     'commandList',
      7     {itemTemplate: 'commando/command', listTemplate: 'commando/command-list'},
      8     this.commands = {items: []}
      9   );
     10   Mojo.Event.listen(this.controller.get('commandList'), Mojo.Event.listTap, this.handleCommandSelected.bind(this));
     11
     12   this.controller.setupWidget(
     13     'commandText',
     14     { multiline: true, focus: true },
     15     this.currentCommand = { value: "", disabled: false }
     16   );
     17
     18   this.controller.setupWidget(
     19     "executeButton",
     20     {},
     21     { buttonLabel : "Run", buttonClass: '', disabled: false }
     22   );
     23   Mojo.Event.listen(this.controller.get("executeButton"), Mojo.Event.tap, this.handleExecuteCommand.bind(this));
     24 };
     25
     26
     27 CommandoAssistant.prototype.handleCommandSelected = function(event) {
     28   this.currentCommand.value = event.item.text;
     29   this.controller.modelChanged(this.currentCommand, this.controller.get('commandText'));
     30 };
     31
     32
     33 CommandoAssistant.prototype.handleExecuteCommand = function(event) {
     34   var assistant = this;
     35
     36   var display = function(prefix, obj) {
     37     var objAsString;
     38     try {
     39       objAsString = Object.toJSON(obj);
     40     } catch (e) {
     41       Mojo.Log.error(e)
     42       objAsString = obj.toString();
     43     }
     44
     45     var result = prefix + ': ' + objAsString;
     46     assistant.controller.get('commandResult').innerHTML += '<BR/>' + result;
     47     Mojo.Log.info(result);
     48   };
     49
     50   var success = function(response) {
     51     display("success", response);
     52   };
     53
     54   var failure = function(response) {
     55     display("failure", response);
     56   };
     57
     58   var result;
     59   try {
     60     assistant.controller.get('commandResult').innerHTML = "";
     61
     62     result = eval(this.currentCommand.value);
     63
     64     this.commands.items.push({text: this.currentCommand.value});
     65     this.controller.modelChanged(this.commands, this.controller.get('commandList'));
     66
     67     this.currentCommand.value = "";
     68     this.controller.modelChanged(this.currentCommand, this.controller.get('commandText'));
     69   } catch(e) {
     70     result = e.toString();
     71   }
     72
     73   display("result", result);
     74 };