@@ -18,22 +18,16 @@ var wpTerm = function(termElem, url) {
18
18
self . historyCon [ 0 ] . scrollTop = self . historyCon [ 0 ] . scrollHeight ;
19
19
} ;
20
20
21
+ self . appendToHistoryArray = function ( list ) {
22
+ for ( var i = 0 ; i < list . length ; i ++ ) {
23
+ self . appendToHistory ( list [ i ] ) ;
24
+ }
25
+ }
26
+
21
27
self . appendCommand = function ( string ) {
22
28
self . appendToHistory ( self . promptText + ' ' + string ) ;
23
29
} ;
24
30
25
- self . appendResponse = function ( response ) {
26
- if ( response . list && response . list . length ) {
27
- for ( var i = 0 ; i < response . list . length ; i ++ ) {
28
- self . appendToHistory ( response . list [ i ] ) ;
29
- }
30
- } else if ( response . msg ) {
31
- self . appendToHistory ( response . msg ) ;
32
- } else if ( response . url ) {
33
- self . appendToHistory ( 'Redirecting to: ' + response . url ) ;
34
- }
35
- } ;
36
-
37
31
self . executeCommand = function ( command ) {
38
32
// Save the command if not empty string
39
33
// and is different from last command
@@ -63,22 +57,31 @@ var wpTerm = function(termElem, url) {
63
57
}
64
58
} ;
65
59
66
- self . sendRequest = function ( command ) {
67
- jQuery . get ( self . url , { cmd : command } , self . handleResponse ) ;
60
+ self . sendRequest = function ( command , callback ) {
61
+ if ( typeof callback == 'undefined' ) {
62
+ callback = self . handleResponse ;
63
+ }
64
+
65
+ jQuery . get ( self . url , { cmd : command } , callback ) ;
68
66
}
69
67
70
68
self . showHistory = function ( ) {
71
- for ( var i in self . history . list ) {
72
- self . appendToHistory ( self . history . list [ i ] ) ;
73
- }
69
+ self . appendToHistoryArray ( self . history . list ) ;
74
70
}
75
71
76
72
self . clearHistoryCon = function ( ) {
77
73
self . historyCon . html ( '' ) ;
78
74
}
79
75
80
76
self . handleResponse = function ( response ) {
81
- self . appendResponse ( response ) ;
77
+ if ( response . list && response . list . length ) {
78
+ self . appendToHistoryArray ( response . list ) ;
79
+ } else if ( response . msg ) {
80
+ self . appendToHistory ( response . msg ) ;
81
+ } else if ( response . url ) {
82
+ self . appendToHistory ( 'Redirecting to: ' + response . url ) ;
83
+ }
84
+
82
85
if ( response . url ) {
83
86
var url = response . url ;
84
87
setTimeout ( function ( ) {
@@ -87,6 +90,45 @@ var wpTerm = function(termElem, url) {
87
90
}
88
91
} ;
89
92
93
+ self . handleCompleteResponse = function ( response , input ) {
94
+ if ( response . complete ) {
95
+ // New cursor position: complete + blank space
96
+ var newCurPos = input . selectionStart + response . complete . length + 1 ;
97
+ // Beginning part
98
+ input . value = input . value . substr ( 0 , input . selectionStart )
99
+ // complete + blank space
100
+ + response . complete + ' '
101
+ // ending part
102
+ + input . value . substr ( input . selectionEnd ) ;
103
+
104
+ // Move the cursor
105
+ input . selectionStart = newCurPos ;
106
+ input . selectionEnd = newCurPos ;
107
+ } else if ( response . list && response . list . length > 0 ) {
108
+ // Remove complete from command before adding to list
109
+ self . appendCommand ( response . cmd . substr ( 9 ) ) ;
110
+ // Show the list
111
+ self . appendToHistoryArray ( response . list ) ;
112
+ }
113
+ } ;
114
+
115
+ self . handleKeyDown = function ( e ) {
116
+ var KEY_TAB = 9 ,
117
+ inputElem = this ;
118
+ if ( e . keyCode != KEY_TAB ) {
119
+ return ;
120
+ }
121
+
122
+ self . sendRequest (
123
+ 'complete ' + inputElem . value . substr ( 0 , inputElem . selectionStart ) ,
124
+ function ( response ) {
125
+ self . handleCompleteResponse ( response , inputElem ) ;
126
+ }
127
+ ) ;
128
+
129
+ return false ;
130
+ } ;
131
+
90
132
self . handleKeyUp = function ( e ) {
91
133
var KEY_ENTER = 13 ,
92
134
KEY_UPARR = 40 ,
0 commit comments