--- /dev/null
+/**\r
+* Ajax Autocomplete for jQuery, version 1.1.3\r
+* (c) 2010 Tomas Kirda\r
+*\r
+* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.\r
+* For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/\r
+*\r
+* Last Review: 04/19/2010\r
+* Heavily modified for contact completion in Friendica (add photos, hover tips. etc.) 11-May-2012 mike@macgirvin.com\r
+*/\r
+\r
+/*jslint onevar: true, evil: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */\r
+/*global window: true, document: true, clearInterval: true, setInterval: true, jQuery: true */\r
+\r
+(function($) {\r
+\r
+ var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');\r
+\r
+ function fnFormatResult(value, data, currentValue) {\r
+ var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';\r
+ return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');\r
+ }\r
+\r
+ function Autocomplete(el, options) {\r
+ this.el = $(el);\r
+ this.el.attr('autocomplete', 'off');\r
+ this.suggestions = [];\r
+ this.data = [];\r
+ this.badQueries = [];\r
+ this.selectedIndex = -1;\r
+ this.currentValue = this.el.val();\r
+ this.intervalId = 0;\r
+ this.cachedResponse = [];\r
+ this.onChangeInterval = null;\r
+ this.ignoreValueChange = false;\r
+ this.serviceUrl = options.serviceUrl;\r
+ this.isLocal = false;\r
+ this.options = {\r
+ autoSubmit: false,\r
+ minChars: 1,\r
+ maxHeight: 300,\r
+ deferRequestBy: 0,\r
+ width: 0,\r
+ highlight: true,\r
+ params: {},\r
+ fnFormatResult: fnFormatResult,\r
+ delimiter: null,\r
+ zIndex: 9999\r
+ };\r
+ this.initialize();\r
+ this.setOptions(options);\r
+ }\r
+ \r
+ $.fn.autocomplete = function(options) {\r
+ return new Autocomplete(this.get(0)||$('<input />'), options);\r
+ };\r
+\r
+\r
+ Autocomplete.prototype = {\r
+\r
+ killerFn: null,\r
+\r
+ initialize: function() {\r
+\r
+ var me, uid, autocompleteElId;\r
+ me = this;\r
+ uid = Math.floor(Math.random()*0x100000).toString(16);\r
+ autocompleteElId = 'Autocomplete_' + uid;\r
+\r
+ this.killerFn = function(e) {\r
+ if ($(e.target).parents('.autocomplete').size() === 0) {\r
+ me.killSuggestions();\r
+ me.disableKillerFn();\r
+ }\r
+ };\r
+\r
+ if (!this.options.width) { this.options.width = this.el.width(); }\r
+ this.mainContainerId = 'AutocompleteContainter_' + uid;\r
+\r
+ $('<div id="' + this.mainContainerId + '" style="position:absolute;z-index:9999;"><div class="autocomplete-w1"><div class="autocomplete" id="' + autocompleteElId + '" style="display:none; width:300px;"></div></div></div>').appendTo('body');\r
+\r
+ this.container = $('#' + autocompleteElId);\r
+ this.fixPosition();\r
+ if (window.opera) {\r
+ this.el.keypress(function(e) { me.onKeyPress(e); });\r
+ } else {\r
+ this.el.keydown(function(e) { me.onKeyPress(e); });\r
+ }\r
+ this.el.keyup(function(e) { me.onKeyUp(e); });\r
+ this.el.blur(function() { me.enableKillerFn(); });\r
+ this.el.focus(function() { me.fixPosition(); });\r
+ },\r
+ \r
+ setOptions: function(options){\r
+ var o = this.options;\r
+ $.extend(o, options);\r
+ if(o.lookup){\r
+ this.isLocal = true;\r
+ if($.isArray(o.lookup)){ o.lookup = { suggestions:o.lookup, data:[] }; }\r
+ }\r
+ $('#'+this.mainContainerId).css({ zIndex:o.zIndex });\r
+ this.container.css({ maxHeight: o.maxHeight + 'px', width:o.width });\r
+ },\r
+ \r
+ clearCache: function(){\r
+ this.cachedResponse = [];\r
+ this.badQueries = [];\r
+ },\r
+ \r
+ disable: function(){\r
+ this.disabled = true;\r
+ },\r
+ \r
+ enable: function(){\r
+ this.disabled = false;\r
+ },\r
+\r
+ fixPosition: function() {\r
+ var offset = this.el.offset();\r
+ $('#' + this.mainContainerId).css({ top: (offset.top + this.el.innerHeight()) + 'px', left: offset.left + 'px' });\r
+ },\r
+\r
+ enableKillerFn: function() {\r
+ var me = this;\r
+ $(document).bind('click', me.killerFn);\r
+ },\r
+\r
+ disableKillerFn: function() {\r
+ var me = this;\r
+ $(document).unbind('click', me.killerFn);\r
+ },\r
+\r
+ killSuggestions: function() {\r
+ var me = this;\r
+ this.stopKillSuggestions();\r
+ this.intervalId = window.setInterval(function() { me.hide(); me.stopKillSuggestions(); }, 300);\r
+ },\r
+\r
+ stopKillSuggestions: function() {\r
+ window.clearInterval(this.intervalId);\r
+ },\r
+\r
+ onKeyPress: function(e) {\r
+ if (this.disabled || !this.enabled) { return; }\r
+ // return will exit the function\r
+ // and event will not be prevented\r
+ switch (e.keyCode) {\r
+ case 27: //KEY_ESC:\r
+ this.el.val(this.currentValue);\r
+ this.hide();\r
+ break;\r
+ case 9: //KEY_TAB:\r
+ case 13: //KEY_RETURN:\r
+ if (this.selectedIndex === -1) {\r
+ this.hide();\r
+ return;\r
+ }\r
+ this.select(this.selectedIndex);\r
+ if(e.keyCode === 9){ return; }\r
+ break;\r
+ case 38: //KEY_UP:\r
+ this.moveUp();\r
+ break;\r
+ case 40: //KEY_DOWN:\r
+ this.moveDown();\r
+ break;\r
+ default:\r
+ return;\r
+ }\r
+ e.stopImmediatePropagation();\r
+ e.preventDefault();\r
+ },\r
+\r
+ onKeyUp: function(e) {\r
+ if(this.disabled){ return; }\r
+ switch (e.keyCode) {\r
+ case 38: //KEY_UP:\r
+ case 40: //KEY_DOWN:\r
+ return;\r
+ }\r
+ clearInterval(this.onChangeInterval);\r
+ if (this.currentValue !== this.el.val()) {\r
+ if (this.options.deferRequestBy > 0) {\r
+ // Defer lookup in case when value changes very quickly:\r
+ var me = this;\r
+ this.onChangeInterval = setInterval(function() { me.onValueChange(); }, this.options.deferRequestBy);\r
+ } else {\r
+ this.onValueChange();\r
+ }\r
+ }\r
+ },\r
+\r
+ onValueChange: function() {\r
+ clearInterval(this.onChangeInterval);\r
+ this.currentValue = this.el.val();\r
+ var q = this.getQuery(this.currentValue);\r
+ this.selectedIndex = -1;\r
+ if (this.ignoreValueChange) {\r
+ this.ignoreValueChange = false;\r
+ return;\r
+ }\r
+ if (q === '' || q.length < this.options.minChars) {\r
+ this.hide();\r
+ } else {\r
+ this.getSuggestions(q);\r
+ }\r
+ },\r
+\r
+ getQuery: function(val) {\r
+ var d, arr;\r
+ d = this.options.delimiter;\r
+ if (!d) { return $.trim(val); }\r
+ arr = val.split(d);\r
+ return $.trim(arr[arr.length - 1]);\r
+ },\r
+\r
+ getSuggestionsLocal: function(q) {\r
+ var ret, arr, len, val, i;\r
+ arr = this.options.lookup;\r
+ len = arr.suggestions.length;\r
+ ret = { suggestions:[], data:[] };\r
+ q = q.toLowerCase();\r
+ for(i=0; i< len; i++){\r
+ val = arr.suggestions[i];\r
+ if(val.toLowerCase().indexOf(q) === 0){\r
+ ret.suggestions.push(val);\r
+ ret.data.push(arr.data[i]);\r
+ }\r
+ }\r
+ return ret;\r
+ },\r
+ \r
+ getSuggestions: function(q) {\r
+ var cr, me;\r
+ cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q];\r
+ if (cr && $.isArray(cr.suggestions)) {\r
+ this.suggestions = cr.suggestions;\r
+ this.data = cr.data;\r
+ this.suggest();\r
+ } else if (!this.isBadQuery(q)) {\r
+ me = this;\r
+ me.options.params.query = q;\r
+ $.get(this.serviceUrl, me.options.params, function(txt) { me.processResponse(txt); }, 'text');\r
+ }\r
+ },\r
+\r
+ isBadQuery: function(q) {\r
+ var i = this.badQueries.length;\r
+ while (i--) {\r
+ if (q.indexOf(this.badQueries[i]) === 0) { return true; }\r
+ }\r
+ return false;\r
+ },\r
+\r
+ hide: function() {\r
+ this.enabled = false;\r
+ this.selectedIndex = -1;\r
+ this.container.hide();\r
+ },\r
+\r
+ suggest: function() {\r
+ if (this.suggestions.length === 0) {\r
+ this.hide();\r
+ return;\r
+ }\r
+\r
+ var me, len, div, f, v, i, s, mOver, mClick, l, img;\r
+ me = this;\r
+ len = this.suggestions.length;\r
+ f = this.options.fnFormatResult;\r
+ v = this.getQuery(this.currentValue);\r
+ mOver = function(xi) { return function() { me.activate(xi); }; };\r
+ mClick = function(xi) { return function() { me.select(xi); }; };\r
+ this.container.hide().empty();\r
+ for (i = 0; i < len; i++) {\r
+ s = this.suggestions[i];\r
+ l = this.links[i];\r
+ img = '<img height="24" width="24" src="' + this.photos[i] + '" alt="' + s + '" /> ';\r
+ div = $((me.selectedIndex === i ? '<div class="selected"' : '<div') + ' title="' + l + '">' + img + f(s, this.data[i], v) + '</div>');\r
+ div.mouseover(mOver(i));\r
+ div.click(mClick(i));\r
+ this.container.append(div);\r
+ }\r
+ this.enabled = true;\r
+ this.container.show();\r
+ },\r
+\r
+ processResponse: function(text) {\r
+ var response;\r
+ try {\r
+ response = eval('(' + text + ')');\r
+ } catch (err) { return; }\r
+ if (!$.isArray(response.data)) { response.data = []; }\r
+ if(!this.options.noCache){\r
+ this.cachedResponse[response.query] = response;\r
+ if (response.suggestions.length === 0) { this.badQueries.push(response.query); }\r
+ }\r
+ if (response.query === this.getQuery(this.currentValue)) {\r
+ this.photos = response.photos;\r
+ this.links = response.links;\r
+ this.suggestions = response.suggestions;\r
+ this.data = response.data;\r
+ this.suggest(); \r
+ }\r
+ },\r
+\r
+ activate: function(index) {\r
+ var divs, activeItem;\r
+ divs = this.container.children();\r
+ // Clear previous selection:\r
+ if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) {\r
+ $(divs.get(this.selectedIndex)).removeClass();\r
+ }\r
+ this.selectedIndex = index;\r
+ if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) {\r
+ activeItem = divs.get(this.selectedIndex);\r
+ $(activeItem).addClass('selected');\r
+ }\r
+ return activeItem;\r
+ },\r
+\r
+ deactivate: function(div, index) {\r
+ div.className = '';\r
+ if (this.selectedIndex === index) { this.selectedIndex = -1; }\r
+ },\r
+\r
+ select: function(i) {\r
+ var selectedValue, f;\r
+ selectedValue = this.suggestions[i];\r
+ if (selectedValue) {\r
+ this.el.val(selectedValue);\r
+ if (this.options.autoSubmit) {\r
+ f = this.el.parents('form');\r
+ if (f.length > 0) { f.get(0).submit(); }\r
+ }\r
+ this.ignoreValueChange = true;\r
+ this.hide();\r
+ this.onSelect(i);\r
+ }\r
+ },\r
+\r
+ moveUp: function() {\r
+ if (this.selectedIndex === -1) { return; }\r
+ if (this.selectedIndex === 0) {\r
+ this.container.children().get(0).className = '';\r
+ this.selectedIndex = -1;\r
+ this.el.val(this.currentValue);\r
+ return;\r
+ }\r
+ this.adjustScroll(this.selectedIndex - 1);\r
+ },\r
+\r
+ moveDown: function() {\r
+ if (this.selectedIndex === (this.suggestions.length - 1)) { return; }\r
+ this.adjustScroll(this.selectedIndex + 1);\r
+ },\r
+\r
+ adjustScroll: function(i) {\r
+ var activeItem, offsetTop, upperBound, lowerBound;\r
+ activeItem = this.activate(i);\r
+ offsetTop = activeItem.offsetTop;\r
+ upperBound = this.container.scrollTop();\r
+ lowerBound = upperBound + this.options.maxHeight - 25;\r
+ if (offsetTop < upperBound) {\r
+ this.container.scrollTop(offsetTop);\r
+ } else if (offsetTop > lowerBound) {\r
+ this.container.scrollTop(offsetTop - this.options.maxHeight + 25);\r
+ }\r
+ this.el.val(this.getValue(this.suggestions[i]));\r
+ },\r
+\r
+ onSelect: function(i) {\r
+ var me, fn, s, d;\r
+ me = this;\r
+ fn = me.options.onSelect;\r
+ s = me.suggestions[i];\r
+ d = me.data[i];\r
+ me.el.val(me.getValue(s));\r
+ if ($.isFunction(fn)) { fn(s, d, me.el); }\r
+ },\r
+ \r
+ getValue: function(value){\r
+ var del, currVal, arr, me;\r
+ me = this;\r
+ del = me.options.delimiter;\r
+ if (!del) { return value; }\r
+ currVal = me.currentValue;\r
+ arr = currVal.split(del);\r
+ if (arr.length === 1) { return value; }\r
+ return currVal.substr(0, currVal.length - arr[arr.length - 1].length) + value;\r
+ }\r
+\r
+ };\r
+\r
+}(jQuery));\r