--- /dev/null
+/**\r
+ * Autocompletion class\r
+ * \r
+ * An auto completion box appear while you're writing. It's possible to force it to appear with Ctrl+Space short cut\r
+ * \r
+ * Loaded as a plugin inside editArea (everything made here could have been made in the plugin directory)\r
+ * But is definitly linked to syntax selection (no need to do 2 different files for color and auto complete for each syntax language)\r
+ * and add a too important feature that many people would miss if included as a plugin\r
+ * \r
+ * - init param: autocompletion_start\r
+ * - Button name: "autocompletion"\r
+ */ \r
+\r
+var EditArea_autocompletion= {\r
+ \r
+ /**\r
+ * Get called once this file is loaded (editArea still not initialized)\r
+ *\r
+ * @return nothing \r
+ */ \r
+ init: function(){ \r
+ // alert("test init: "+ this._someInternalFunction(2, 3));\r
+ \r
+ if(editArea.settings["autocompletion"])\r
+ this.enabled= true;\r
+ else\r
+ this.enabled= false;\r
+ this.current_word = false;\r
+ this.shown = false;\r
+ this.selectIndex = -1;\r
+ this.forceDisplay = false;\r
+ this.isInMiddleWord = false;\r
+ this.autoSelectIfOneResult = false;\r
+ this.delayBeforeDisplay = 100;\r
+ this.checkDelayTimer = false;\r
+ this.curr_syntax_str = '';\r
+ \r
+ this.file_syntax_datas = {};\r
+ }\r
+ /**\r
+ * Returns the HTML code for a specific control string or false if this plugin doesn't have that control.\r
+ * A control can be a button, select list or any other HTML item to present in the EditArea user interface.\r
+ * Language variables such as {$lang_somekey} will also be replaced with contents from\r
+ * the language packs.\r
+ * \r
+ * @param {string} ctrl_name: the name of the control to add \r
+ * @return HTML code for a specific control or false.\r
+ * @type string or boolean\r
+ */ \r
+ /*,get_control_html: function(ctrl_name){\r
+ switch( ctrl_name ){\r
+ case 'autocompletion':\r
+ // Control id, button img, command\r
+ return parent.editAreaLoader.get_button_html('autocompletion_but', 'autocompletion.gif', 'toggle_autocompletion', false, this.baseURL);\r
+ break;\r
+ }\r
+ return false;\r
+ }*/\r
+ /**\r
+ * Get called once EditArea is fully loaded and initialised\r
+ * \r
+ * @return nothing\r
+ */ \r
+ ,onload: function(){ \r
+ if(this.enabled)\r
+ {\r
+ var icon= document.getElementById("autocompletion");\r
+ if(icon)\r
+ editArea.switchClassSticky(icon, 'editAreaButtonSelected', true);\r
+ }\r
+ \r
+ this.container = document.createElement('div');\r
+ this.container.id = "auto_completion_area";\r
+ editArea.container.insertBefore( this.container, editArea.container.firstChild );\r
+ \r
+ // add event detection for hiding suggestion box\r
+ parent.editAreaLoader.add_event( document, "click", function(){ editArea.plugins['autocompletion']._hide();} );\r
+ parent.editAreaLoader.add_event( editArea.textarea, "blur", function(){ editArea.plugins['autocompletion']._hide();} );\r
+ \r
+ }\r
+ \r
+ /**\r
+ * Is called each time the user touch a keyboard key.\r
+ * \r
+ * @param (event) e: the keydown event\r
+ * @return true - pass to next handler in chain, false - stop chain execution\r
+ * @type boolean \r
+ */\r
+ ,onkeydown: function(e){\r
+ if(!this.enabled)\r
+ return true;\r
+ \r
+ if (EA_keys[e.keyCode])\r
+ letter=EA_keys[e.keyCode];\r
+ else\r
+ letter=String.fromCharCode(e.keyCode); \r
+ // shown\r
+ if( this._isShown() )\r
+ { \r
+ // if escape, hide the box\r
+ if(letter=="Esc")\r
+ {\r
+ this._hide();\r
+ return false;\r
+ }\r
+ // Enter\r
+ else if( letter=="Entrer")\r
+ {\r
+ var as = this.container.getElementsByTagName('A');\r
+ // select a suggested entry\r
+ if( this.selectIndex >= 0 && this.selectIndex < as.length )\r
+ {\r
+ as[ this.selectIndex ].onmousedown();\r
+ return false\r
+ }\r
+ // simply add an enter in the code\r
+ else\r
+ {\r
+ this._hide();\r
+ return true;\r
+ }\r
+ }\r
+ else if( letter=="Tab" || letter=="Down")\r
+ {\r
+ this._selectNext();\r
+ return false;\r
+ }\r
+ else if( letter=="Up")\r
+ {\r
+ this._selectBefore();\r
+ return false;\r
+ }\r
+ }\r
+ // hidden\r
+ else\r
+ {\r
+ \r
+ }\r
+ \r
+ // show current suggestion list and do autoSelect if possible (no matter it's shown or hidden)\r
+ if( letter=="Space" && CtrlPressed(e) )\r
+ {\r
+ //parent.console.log('SHOW SUGGEST');\r
+ this.forceDisplay = true;\r
+ this.autoSelectIfOneResult = true;\r
+ this._checkLetter();\r
+ return false;\r
+ }\r
+ \r
+ // wait a short period for check that the cursor isn't moving\r
+ setTimeout("editArea.plugins['autocompletion']._checkDelayAndCursorBeforeDisplay();", editArea.check_line_selection_timer +5 );\r
+ this.checkDelayTimer = false;\r
+ return true;\r
+ } \r
+ /**\r
+ * Executes a specific command, this function handles plugin commands.\r
+ *\r
+ * @param {string} cmd: the name of the command being executed\r
+ * @param {unknown} param: the parameter of the command \r
+ * @return true - pass to next handler in chain, false - stop chain execution\r
+ * @type boolean \r
+ */\r
+ ,execCommand: function(cmd, param){\r
+ switch( cmd ){\r
+ case 'toggle_autocompletion':\r
+ var icon= document.getElementById("autocompletion");\r
+ if(!this.enabled)\r
+ {\r
+ if(icon != null){\r
+ editArea.restoreClass(icon);\r
+ editArea.switchClassSticky(icon, 'editAreaButtonSelected', true);\r
+ }\r
+ this.enabled= true;\r
+ }\r
+ else\r
+ {\r
+ this.enabled= false;\r
+ if(icon != null)\r
+ editArea.switchClassSticky(icon, 'editAreaButtonNormal', false);\r
+ }\r
+ return true;\r
+ }\r
+ return true;\r
+ }\r
+ ,_checkDelayAndCursorBeforeDisplay: function()\r
+ {\r
+ this.checkDelayTimer = setTimeout("if(editArea.textarea.selectionStart == "+ editArea.textarea.selectionStart +") EditArea_autocompletion._checkLetter();", this.delayBeforeDisplay - editArea.check_line_selection_timer - 5 );\r
+ }\r
+ // hide the suggested box\r
+ ,_hide: function(){\r
+ this.container.style.display="none";\r
+ this.selectIndex = -1;\r
+ this.shown = false;\r
+ this.forceDisplay = false;\r
+ this.autoSelectIfOneResult = false;\r
+ }\r
+ // display the suggested box\r
+ ,_show: function(){\r
+ if( !this._isShown() )\r
+ {\r
+ this.container.style.display="block";\r
+ this.selectIndex = -1;\r
+ this.shown = true;\r
+ }\r
+ }\r
+ // is the suggested box displayed?\r
+ ,_isShown: function(){\r
+ return this.shown;\r
+ }\r
+ // setter and getter\r
+ ,_isInMiddleWord: function( new_value ){\r
+ if( typeof( new_value ) == "undefined" )\r
+ return this.isInMiddleWord;\r
+ else\r
+ this.isInMiddleWord = new_value;\r
+ }\r
+ // select the next element in the suggested box\r
+ ,_selectNext: function()\r
+ {\r
+ var as = this.container.getElementsByTagName('A');\r
+ \r
+ // clean existing elements\r
+ for( var i=0; i<as.length; i++ )\r
+ {\r
+ if( as[i].className )\r
+ as[i].className = as[i].className.replace(/ focus/g, '');\r
+ }\r
+ \r
+ this.selectIndex++; \r
+ this.selectIndex = ( this.selectIndex >= as.length || this.selectIndex < 0 ) ? 0 : this.selectIndex;\r
+ as[ this.selectIndex ].className += " focus";\r
+ }\r
+ // select the previous element in the suggested box\r
+ ,_selectBefore: function()\r
+ {\r
+ var as = this.container.getElementsByTagName('A');\r
+ \r
+ // clean existing elements\r
+ for( var i=0; i<as.length; i++ )\r
+ {\r
+ if( as[i].className )\r
+ as[i].className = as[ i ].className.replace(/ focus/g, '');\r
+ }\r
+ \r
+ this.selectIndex--;\r
+ \r
+ this.selectIndex = ( this.selectIndex >= as.length || this.selectIndex < 0 ) ? as.length-1 : this.selectIndex;\r
+ as[ this.selectIndex ].className += " focus";\r
+ }\r
+ ,_select: function( content )\r
+ {\r
+ cursor_forced_position = content.indexOf( '{@}' );\r
+ content = content.replace(/{@}/g, '' );\r
+ editArea.getIESelection();\r
+ \r
+ // retrive the number of matching characters\r
+ var start_index = Math.max( 0, editArea.textarea.selectionEnd - content.length );\r
+ \r
+ line_string = editArea.textarea.value.substring( start_index, editArea.textarea.selectionEnd + 1);\r
+ limit = line_string.length -1;\r
+ nbMatch = 0;\r
+ for( i =0; i<limit ; i++ )\r
+ {\r
+ if( line_string.substring( limit - i - 1, limit ) == content.substring( 0, i + 1 ) )\r
+ nbMatch = i + 1;\r
+ }\r
+ // if characters match, we should include them in the selection that will be replaced\r
+ if( nbMatch > 0 )\r
+ parent.editAreaLoader.setSelectionRange(editArea.id, editArea.textarea.selectionStart - nbMatch , editArea.textarea.selectionEnd);\r
+ \r
+ parent.editAreaLoader.setSelectedText(editArea.id, content );\r
+ range= parent.editAreaLoader.getSelectionRange(editArea.id);\r
+ \r
+ if( cursor_forced_position != -1 )\r
+ new_pos = range["end"] - ( content.length-cursor_forced_position );\r
+ else\r
+ new_pos = range["end"]; \r
+ parent.editAreaLoader.setSelectionRange(editArea.id, new_pos, new_pos);\r
+ this._hide();\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Parse the AUTO_COMPLETION part of syntax definition files\r
+ */\r
+ ,_parseSyntaxAutoCompletionDatas: function(){\r
+ //foreach syntax loaded\r
+ for(var lang in parent.editAreaLoader.load_syntax)\r
+ {\r
+ if(!parent.editAreaLoader.syntax[lang]['autocompletion']) // init the regexp if not already initialized\r
+ {\r
+ parent.editAreaLoader.syntax[lang]['autocompletion']= {};\r
+ // the file has auto completion datas\r
+ if(parent.editAreaLoader.load_syntax[lang]['AUTO_COMPLETION'])\r
+ {\r
+ // parse them\r
+ for(var i in parent.editAreaLoader.load_syntax[lang]['AUTO_COMPLETION'])\r
+ {\r
+ datas = parent.editAreaLoader.load_syntax[lang]['AUTO_COMPLETION'][i];\r
+ tmp = {};\r
+ if(datas["CASE_SENSITIVE"]!="undefined" && datas["CASE_SENSITIVE"]==false)\r
+ tmp["modifiers"]="i";\r
+ else\r
+ tmp["modifiers"]="";\r
+ tmp["prefix_separator"]= datas["REGEXP"]["prefix_separator"];\r
+ tmp["match_prefix_separator"]= new RegExp( datas["REGEXP"]["prefix_separator"] +"$", tmp["modifiers"]);\r
+ tmp["match_word"]= new RegExp("(?:"+ datas["REGEXP"]["before_word"] +")("+ datas["REGEXP"]["possible_words_letters"] +")$", tmp["modifiers"]);\r
+ tmp["match_next_letter"]= new RegExp("^("+ datas["REGEXP"]["letter_after_word_must_match"] +")$", tmp["modifiers"]);\r
+ tmp["keywords"]= {};\r
+ //console.log( datas["KEYWORDS"] );\r
+ for( var prefix in datas["KEYWORDS"] )\r
+ {\r
+ tmp["keywords"][prefix]= {\r
+ prefix: prefix,\r
+ prefix_name: prefix,\r
+ prefix_reg: new RegExp("(?:"+ parent.editAreaLoader.get_escaped_regexp( prefix ) +")(?:"+ tmp["prefix_separator"] +")$", tmp["modifiers"] ),\r
+ datas: []\r
+ };\r
+ for( var j=0; j<datas["KEYWORDS"][prefix].length; j++ )\r
+ {\r
+ tmp["keywords"][prefix]['datas'][j]= {\r
+ is_typing: datas["KEYWORDS"][prefix][j][0],\r
+ // if replace with is empty, replace with the is_typing value\r
+ replace_with: datas["KEYWORDS"][prefix][j][1] ? datas["KEYWORDS"][prefix][j][1].replace('§', datas["KEYWORDS"][prefix][j][0] ) : '',\r
+ comment: datas["KEYWORDS"][prefix][j][2] ? datas["KEYWORDS"][prefix][j][2] : '' \r
+ };\r
+ \r
+ // the replace with shouldn't be empty\r
+ if( tmp["keywords"][prefix]['datas'][j]['replace_with'].length == 0 )\r
+ tmp["keywords"][prefix]['datas'][j]['replace_with'] = tmp["keywords"][prefix]['datas'][j]['is_typing'];\r
+ \r
+ // if the comment is empty, display the replace_with value\r
+ if( tmp["keywords"][prefix]['datas'][j]['comment'].length == 0 )\r
+ tmp["keywords"][prefix]['datas'][j]['comment'] = tmp["keywords"][prefix]['datas'][j]['replace_with'].replace(/{@}/g, '' );\r
+ }\r
+ \r
+ }\r
+ tmp["max_text_length"]= datas["MAX_TEXT_LENGTH"];\r
+ parent.editAreaLoader.syntax[lang]['autocompletion'][i] = tmp;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ ,_checkLetter: function(){\r
+ // check that syntax hasn't changed\r
+ if( this.curr_syntax_str != editArea.settings['syntax'] )\r
+ {\r
+ if( !parent.editAreaLoader.syntax[editArea.settings['syntax']]['autocompletion'] )\r
+ this._parseSyntaxAutoCompletionDatas();\r
+ this.curr_syntax= parent.editAreaLoader.syntax[editArea.settings['syntax']]['autocompletion'];\r
+ this.curr_syntax_str = editArea.settings['syntax'];\r
+ //console.log( this.curr_syntax );\r
+ }\r
+ \r
+ if( editArea.is_editable )\r
+ {\r
+ time=new Date;\r
+ t1= time.getTime();\r
+ editArea.getIESelection();\r
+ this.selectIndex = -1;\r
+ start=editArea.textarea.selectionStart;\r
+ var str = editArea.textarea.value;\r
+ var results= [];\r
+ \r
+ \r
+ for(var i in this.curr_syntax)\r
+ {\r
+ var last_chars = str.substring(Math.max(0, start-this.curr_syntax[i]["max_text_length"]), start);\r
+ var matchNextletter = str.substring(start, start+1).match( this.curr_syntax[i]["match_next_letter"]);\r
+ // if not writting in the middle of a word or if forcing display\r
+ if( matchNextletter || this.forceDisplay )\r
+ {\r
+ // check if the last chars match a separator\r
+ var match_prefix_separator = last_chars.match(this.curr_syntax[i]["match_prefix_separator"]);\r
+ \r
+ // check if it match a possible word\r
+ var match_word= last_chars.match(this.curr_syntax[i]["match_word"]);\r
+ \r
+ //console.log( match_word );\r
+ if( match_word )\r
+ {\r
+ var begin_word= match_word[1];\r
+ var match_curr_word= new RegExp("^"+ parent.editAreaLoader.get_escaped_regexp( begin_word ), this.curr_syntax[i]["modifiers"]);\r
+ //console.log( match_curr_word );\r
+ for(var prefix in this.curr_syntax[i]["keywords"])\r
+ {\r
+ // parent.console.log( this.curr_syntax[i]["keywords"][prefix] );\r
+ for(var j=0; j<this.curr_syntax[i]["keywords"][prefix]['datas'].length; j++)\r
+ {\r
+ // parent.console.log( this.curr_syntax[i]["keywords"][prefix]['datas'][j]['is_typing'] );\r
+ // the key word match or force display \r
+ if( this.curr_syntax[i]["keywords"][prefix]['datas'][j]['is_typing'].match(match_curr_word) )\r
+ {\r
+ // parent.console.log('match');\r
+ hasMatch = false;\r
+ var before = last_chars.substr( 0, last_chars.length - begin_word.length );\r
+ \r
+ // no prefix to match => it's valid\r
+ if( !match_prefix_separator && this.curr_syntax[i]["keywords"][prefix]['prefix'].length == 0 )\r
+ {\r
+ if( ! before.match( this.curr_syntax[i]["keywords"][prefix]['prefix_reg'] ) )\r
+ hasMatch = true;\r
+ }\r
+ // we still need to check the prefix if there is one\r
+ else if( this.curr_syntax[i]["keywords"][prefix]['prefix'].length > 0 )\r
+ {\r
+ if( before.match( this.curr_syntax[i]["keywords"][prefix]['prefix_reg'] ) )\r
+ hasMatch = true;\r
+ }\r
+ \r
+ if( hasMatch )\r
+ results[results.length]= [ this.curr_syntax[i]["keywords"][prefix], this.curr_syntax[i]["keywords"][prefix]['datas'][j] ];\r
+ } \r
+ }\r
+ }\r
+ }\r
+ // it doesn't match any possible word but we want to display something\r
+ // we'll display to list of all available words\r
+ else if( this.forceDisplay || match_prefix_separator )\r
+ {\r
+ for(var prefix in this.curr_syntax[i]["keywords"])\r
+ {\r
+ for(var j=0; j<this.curr_syntax[i]["keywords"][prefix]['datas'].length; j++)\r
+ {\r
+ hasMatch = false;\r
+ // no prefix to match => it's valid\r
+ if( !match_prefix_separator && this.curr_syntax[i]["keywords"][prefix]['prefix'].length == 0 )\r
+ {\r
+ hasMatch = true;\r
+ }\r
+ // we still need to check the prefix if there is one\r
+ else if( match_prefix_separator && this.curr_syntax[i]["keywords"][prefix]['prefix'].length > 0 )\r
+ {\r
+ var before = last_chars; //.substr( 0, last_chars.length );\r
+ if( before.match( this.curr_syntax[i]["keywords"][prefix]['prefix_reg'] ) )\r
+ hasMatch = true;\r
+ } \r
+ \r
+ if( hasMatch )\r
+ results[results.length]= [ this.curr_syntax[i]["keywords"][prefix], this.curr_syntax[i]["keywords"][prefix]['datas'][j] ]; \r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ // there is only one result, and we can select it automatically\r
+ if( results.length == 1 && this.autoSelectIfOneResult )\r
+ {\r
+ // console.log( results );\r
+ this._select( results[0][1]['replace_with'] );\r
+ }\r
+ else if( results.length == 0 )\r
+ {\r
+ this._hide();\r
+ }\r
+ else\r
+ {\r
+ // build the suggestion box content\r
+ var lines=[];\r
+ for(var i=0; i<results.length; i++)\r
+ {\r
+ var line= "<li><a href=\"#\" class=\"entry\" onmousedown=\"EditArea_autocompletion._select('"+ results[i][1]['replace_with'].replace(new RegExp('"', "g"), """) +"');return false;\">"+ results[i][1]['comment'];\r
+ if(results[i][0]['prefix_name'].length>0)\r
+ line+='<span class="prefix">'+ results[i][0]['prefix_name'] +'</span>';\r
+ line+='</a></li>';\r
+ lines[lines.length]=line;\r
+ }\r
+ // sort results\r
+ this.container.innerHTML = '<ul>'+ lines.sort().join('') +'</ul>';\r
+ \r
+ var cursor = _$("cursor_pos");\r
+ this.container.style.top = ( cursor.cursor_top + editArea.lineHeight ) +"px";\r
+ this.container.style.left = ( cursor.cursor_left + 8 ) +"px";\r
+ this._show();\r
+ }\r
+ \r
+ this.autoSelectIfOneResult = false;\r
+ time=new Date;\r
+ t2= time.getTime();\r
+ \r
+ //parent.console.log( begin_word +"\n"+ (t2-t1) +"\n"+ html );\r
+ }\r
+ }\r
+};\r
+\r
+// Load as a plugin\r
+editArea.settings['plugins'][ editArea.settings['plugins'].length ] = 'autocompletion';\r
+editArea.add_plugin('autocompletion', EditArea_autocompletion);
\ No newline at end of file