comparison hip.js @ 14:a26372118854

Created a primitive form of noun-first suggestion.
author jonathandicarlo@localhost
date Wed, 02 Jul 2008 16:50:15 -0700
parents 8dcc88f93829
children 5fce4c8f3ebd
comparison
equal deleted inserted replaced
13:8dcc88f93829 14:a26372118854
68 return this._verb.getDescription( this._DO, this._modifiers ); 68 return this._verb.getDescription( this._DO, this._modifiers );
69 } 69 }
70 70
71 }; 71 };
72 72
73 function QuerySource(verbList) { 73
74 this._init(verbList); 74 var gQs = new QuerySource([ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ],
75 } 75 [city, language, tab, person, application]);
76 QuerySource.prototype = {
77 _init: function(verbList) {
78 this._verbList = verbList; //arrayof Verb objects to use for completions
79 this._lockedInSentence = null;
80 this._hilitedSuggestion = 0;
81 this._suggestionList = []; // a list of ParsedSentences.
82 },
83
84 updateSuggestionList: function( query ) {
85 this._suggestionList = [];
86 var completions = [];
87 var words = query.split( " " );
88 for ( var x in this._verbList ) {
89 var verb = this._verbList[x];
90 if ( verb.match( words[0] ) ) {
91 completions = verb.getCompletions( words.slice(1) );
92 this._suggestionList = this._suggestionList.concat( completions );
93 }
94 }
95 // TODO sort in order of match quality
96 this._hilitedSuggestion = 1; // hilight the first suggestion by default
97 },
98
99 getSuggestionsAsHtml : function() {
100 return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ];
101 },
102
103 getDescriptionText: function() {
104 if ( this._suggestionList.length == 0 ) {
105 return "You got the magic stick. Type some commands!";
106 }
107 var h = this._hilitedSuggestion;
108 if ( h == 0 ) {
109 return "Executes your input literally, with no autocompletion.";
110 } else {
111 h = h - 1;
112 }
113 var sentence = this._suggestionList[h];
114 return sentence.getDescription();
115 },
116
117 indicationDown: function( ) {
118 this._hilitedSuggestion ++;
119 if ( this._hilitedSuggestion > this._suggestionList.length ) {
120 this._hilitedSuggestion = 0;
121 }
122 },
123
124 indicationUp: function() {
125 this._hilitedSuggestion --;
126 if ( this._hilitedSuggestion < 0 ) {
127 this._hilitedSuggestion = this._suggestionList.length;
128 }
129 },
130
131 getHilitedSuggestion: function() {
132 return this._hilitedSuggestion - 1; // because 0 means no hilite
133 // and the suggestion list starts at 1... fencepost!
134 },
135
136 autocomplete: function( query ) {
137 var newText;
138 var hilited = this.getHilitedSuggestion();
139 if ( hilited > -1 ) {
140 newText = this._suggestionList[ hilited ].getCompletionText() + " ";
141 } else {
142 newText = query;
143 }
144 return newText;
145 },
146
147 clear: function() {
148 this._suggestionList = [];
149 this._hilitedSuggestion = 0;
150 this._lockedInSentence = null;
151 }
152 };
153
154 var gQs = new QuerySource([ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ]);
155 76
156 function makeSuggestionHtml( tagName, list, hilitedNumber ) { 77 function makeSuggestionHtml( tagName, list, hilitedNumber ) {
157 var result = ""; 78 var result = "";
158 var openingTag = ""; 79 var openingTag = "";
159 var closingTag = "</" + tagName + ">"; 80 var closingTag = "</" + tagName + ">";