2014-03-06 33 views
2

我使用http://terminal.jcubic.pl/來創建一個jQuery終端。如何從另一個函數更改JQuery終端提示?

我想在用戶成功登錄後更改提示。例如,我想從#改變提示符root @ mac:

我對jQuery有點新鮮。請幫忙。

//這是我如何創建我的終端

var terminal = $('#jq_terminal').terminal(function(command, term) { 
     term.echo("you just typed " + command); 
    }, { 
     login: f_login, 
     greetings: "You are authenticated", 
     prompt: '#', 
     name: 'shell', 
      onBlur: function() { 
        return false; 
       } 
    }); 

// Login 
function f_login(user, password, callback) {  
    // make a soket.IO call here. SocketIO will call f_login_response 
} 

// Login Response here 
function f_login_response(user, password, callback) { 
    // How do I change the terminal prompt here ? 
    // I tried 
    terminal.prompt = '$'; // Does not work 
} 

回答

2

提示可以是一個函數:

var login = false; 
var terminal = $('#jq_terminal').terminal(function(command, term) { 
    term.echo("you just typed " + command); 
}, { 
    login: f_login, 
    greetings: "You are authenticated", 
    prompt: function(callback) { 
     callback(login ? '#' : '$'); 
    }, 
    name: 'shell', 
    onBlur: function() { 
     return false; 
    } 
}); 

你也可以使用terminal.set_prompt('#');。你不能訪問終端登錄功能的代碼應該做的authenticate.apply(self, ....)所以你可以做this.set_prompt(我需要解決這個問題),但你應該能夠訪問終端,如果你像它一樣篡改它(和我在示例代碼)。所以這應該工作:

function f_login_response(user, password, callback) { 
    terminal.set_prompt('$'); 
    //or 
    login = true; 
} 

此外,如果你想有兩種類型的命令爲登錄用戶和來賓,那麼你可以這樣做:

function(commad, term) { 
    var cmd = $.terminal.parseCommand(command); // parseCommand is helper that process command line (you have also splitCommand that don't convert to numbers and regexes 
    if (cmd.name == 'login') { 
     term.push(function(command) { 
      term.echo("You type normal command"); 
     }, { 
      prompt: '#' 
      // I need to add login option to `push` so you can do term.push('foo.php', {login:true}); 
     }).login(function(user, password, callback) { 
      if (<< User ok >>) { 
      callback('TOKEN'); 
      } else { 
      callback(null); // callback have also second parameter if you use true it will not give error message 
      } 
     }); 
    } else { 
     term.echo("You type guest command"); 
    } 
} 

PS:在UNIX $對於普通用戶和#是爲根。

編輯:從0.8.3版本,你可以撥打:

term.push('rpc_service.php', {login: true}); 

login(user, pass, callback) { 
    this.set_prompt('#'); 
}