User:Ciencia Al Poder-fduser/LinkSuggest Improvement.js

//// LinkSuggest improvements. Original code modified by w:User:Ciencia Al Poder if (window.YAHOO && YAHOO.example && YAHOO.example.AutoCompleteTextArea) { YAHOO.example.AutoCompleteTextArea.prototype._localisations = [ ['Talk','Discusión'], ['User','Usuario'], ['User talk','Usuario Discusión'], ['Project','WikiDex'], ['Project talk','WikiDex Discusión'], ['Image','Imagen'], ['Image talk','Imagen Discusión'], ['MediaWiki','MediaWiki'], ['MediaWiki talk','MediaWiki Discusión'], ['Template','Plantilla'], ['Template talk','Plantilla Discusión'], ['Help','Ayuda'], ['Help talk','Ayuda Discusión'], ['Category','Categoría'], ['Category talk','Categoría Discusión'] ];

YAHOO.example.AutoCompleteTextArea.prototype._localize = function(sItem, bEngToLang){ if (!sItem || !sItem.length || !this._localisations || !this._localisations.length) return sItem; for (var i=0; i<this._localisations.length; i++){ var sIn = this._localisations[i][(bEngToLang?0:1)].toString+':'; var sOut = this._localisations[i][(bEngToLang?1:0)].toString+':'; if (sItem.toLowerCase.indexOf(sIn.toLowerCase)==0){ sItem = sOut+sItem.substr(sIn.length, sItem.length-1); break; }		}		return sItem; };

YAHOO.example.AutoCompleteTextArea.prototype._updateValue = function(oItem) {

this.track('success'); this._suggestionSuccessful = true;

this._elTextbox.focus;

var scrollTop = this._elTextbox.scrollTop; var text = this._elTextbox.value.replace(/\r/g, ""); var caret = this.getCaret(this._elTextbox);

for(var i = caret; i >= 0; i--) { if(text.charAt(i - 1) == "[" || text.charAt(i - 1) == "{") { break; }		}

var textBefore = text.substr(0, i); var newVal = textBefore + ((this._bIsTemplate && this._bIsSubstTemplate) ? 'subst:' :  ) + (this._bIsColon ? ':' : ) + oItem._oResultData[1] + (text.charAt(i - 1) == "{" ? "}}" : "]]") + text.substr(i + this._originalQuery.length); this._elTextbox.value = newVal;

if(YAHOO.env.ua.ie > 0) { caret = caret - this.row + 1; }

this.setCaret(this._elTextbox, i + (this._bIsColon ? 1 : 0) + ((this._bIsTemplate && this._bIsSubstTemplate) ? 6 : 0 ) + oItem._oResultData[1].length + 2); this._oCurItem = oItem; this._elTextbox.scrollTop = scrollTop; };

YAHOO.example.AutoCompleteTextArea.prototype._sendQuery = function(sQuery) { var text = this._elTextbox.value.replace(/\r/g, ""); var caret = this.getCaret(this._elTextbox); var sQueryStartAt;

// also look forward, to see if we closed this one for(var i = caret; i < text.length; i++) { var c = text.charAt (i) ; if((c == "[") && (text.charAt(i - 1) == "[")) { break ; }			if((c == "{") && (text.charAt(i - 1) == "{")) { break ; }			if((c == "]") && (text.charAt(i - 1) == "]")) { return ; }			if((c == "}") && (text.charAt(i - 1) == "}")) { return ; }		}		for(var i = caret; i >= 0; i--) { var c = text.charAt(i); if(c == "]" || c == "|") { if ( (c == "|") || ( (c == "]") && (text.charAt(i-1) == "]") ) ) { this._toggleContainer(false) ; }				return; }			if(c == "}" || c == "|") { if ( (c == "|") || ( (c == "}") && (text.charAt(i-1) == "}") ) ) { this._toggleContainer(false) ; }				return; }			if((c == "[") && (text.charAt(i - 1) == "[")) { this._originalQuery = text.substr(i + 1, (caret - i - 1)); sQueryReal = this._originalQuery if (this._originalQuery.indexOf(':')==0){ this._bIsColon = true; sQueryReal = sQueryReal.replace(':',''); } else { this._bIsColon = false; }				sQueryReal = this._localize(sQueryReal, false); this._bIsTemplate = false; sQueryStartAt = i;				break; }			if((c == "{") && (text.charAt(i - 1) == "{")) { this._originalQuery = text.substr(i + 1, (caret - i - 1)); this._bIsColon = false; if (this._originalQuery.length >= 6 && this._originalQuery.toLowerCase.indexOf('subst:') == 0){ sQueryReal = "Template:"+this._originalQuery.replace(/subst:/i,''); this._bIsSubstTemplate = true; } else if (this._originalQuery.indexOf(':')==0){ sQueryReal = this._localize(this._originalQuery.replace(':',''), false); this._bIsColon = true; } else { sQueryReal = "Template:"+this._originalQuery; this._bIsSubstTemplate = false; }				this._bIsTemplate = true; sQueryStartAt = i;				break; }		}

if(sQueryStartAt >= 0 && sQueryReal.length > 2) { YAHOO.example.AutoCompleteTextArea.superclass._sendQuery.call(this, encodeURI(sQueryReal).replace(/%[0-9A-F]{2}/g,'_')); }	};

YAHOO.example.AutoCompleteTextArea.prototype.doBeforeExpandContainer = function(elTextbox, elContainer, sQuery, aResults) { for (var i=0, aList=elContainer.getElementsByTagName('li'); i<aList.length; i++){ if (aList[i]._sResultKey){ if (this._bIsTemplate){ aList[i].innerHTML = aList[i].innerHTML.replace('Template:',''); aList[i]._sResultKey = aList[i]._sResultKey.replace('Template:',''); for (var j=0; j<aList[i]._oResultData.length; j++){ aList[i]._oResultData[j] = aList[i]._oResultData[j].replace('Template:',''); }				} else { aList[i].innerHTML = this._localize(aList[i].innerHTML, true); aList[i]._sResultKey = this._localize(aList[i]._sResultKey, true); for (var j=0; j<aList[i]._oResultData.length; j++){ aList[i]._oResultData[j] = this._localize(aList[i]._oResultData[j], true); }				}			}		}

var position = this.getCaretPosition(elTextbox); elContainer.style.left = position[0] + 'px' elContainer.style.top = position[1] + 'px'

/* #3378 */		var maxLen = 20;

for (var n=0; n<aResults.length; n++) { var len = aResults[n][0].length; if (maxLen < len) maxLen = len; }

elContainer.style.width = Math.round((maxLen*7.5) < 400 ? maxLen*7.5 : 400) +'px';

if (!this.isContainerOpen) { this.track('open'); this._suggestionSuccessful = false; }

return true; }; }