2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-09-19 16:59:01 +00:00

Format code using prettier

This commit is contained in:
Tomas Kirda 2023-10-10 13:19:56 -07:00
parent b2403e9ed6
commit 16ad576b8c
3 changed files with 177 additions and 120 deletions

18
package-lock.json generated
View File

@ -14,7 +14,8 @@
}, },
"devDependencies": { "devDependencies": {
"grunt": "^1.0.1", "grunt": "^1.0.1",
"grunt-contrib-uglify": "^1.0.1" "grunt-contrib-uglify": "^1.0.1",
"prettier": "^3.0.3"
} }
}, },
"node_modules/@types/jquery": { "node_modules/@types/jquery": {
@ -1485,6 +1486,21 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/prettier": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz",
"integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==",
"dev": true,
"bin": {
"prettier": "bin/prettier.cjs"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
},
"node_modules/pretty-bytes": { "node_modules/pretty-bytes": {
"version": "1.0.4", "version": "1.0.4",
"resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-1.0.4.tgz", "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-1.0.4.tgz",

View File

@ -7,6 +7,9 @@
"main": "dist/jquery.autocomplete.js", "main": "dist/jquery.autocomplete.js",
"types": "./typings/jquery-autocomplete/jquery.autocomplete.d.ts", "types": "./typings/jquery-autocomplete/jquery.autocomplete.d.ts",
"license": "MIT", "license": "MIT",
"scripts": {
"format": "prettier --write ./src/**"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/devbridge/jQuery-Autocomplete.git" "url": "git://github.com/devbridge/jQuery-Autocomplete.git"
@ -17,7 +20,13 @@
}, },
"devDependencies": { "devDependencies": {
"grunt": "^1.0.1", "grunt": "^1.0.1",
"grunt-contrib-uglify": "^1.0.1" "grunt-contrib-uglify": "^1.0.1",
"prettier": "^3.0.3"
},
"prettier": {
"printWidth": 100,
"singleQuote": true,
"tabWidth": 4
}, },
"files": [ "files": [
"dist/", "dist/",

View File

@ -11,7 +11,7 @@
// Expose plugin as an AMD module if AMD loader is present: // Expose plugin as an AMD module if AMD loader is present:
(function (factory) { (function (factory) {
"use strict"; 'use strict';
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module. // AMD. Register as an anonymous module.
define(['jquery'], factory); define(['jquery'], factory);
@ -22,14 +22,13 @@
// Browser globals // Browser globals
factory(jQuery); factory(jQuery);
} }
}(function ($) { })(function ($) {
'use strict'; 'use strict';
var var utils = (function () {
utils = (function () {
return { return {
escapeRegExChars: function (value) { escapeRegExChars: function (value) {
return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&"); return value.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}, },
createNode: function (containerClass) { createNode: function (containerClass) {
var div = document.createElement('div'); var div = document.createElement('div');
@ -37,10 +36,9 @@
div.style.position = 'absolute'; div.style.position = 'absolute';
div.style.display = 'none'; div.style.display = 'none';
return div; return div;
} },
}; };
}()), })(),
keys = { keys = {
ESC: 27, ESC: 27,
TAB: 9, TAB: 9,
@ -48,9 +46,8 @@
LEFT: 37, LEFT: 37,
UP: 38, UP: 38,
RIGHT: 39, RIGHT: 39,
DOWN: 40 DOWN: 40,
}, },
noop = $.noop; noop = $.noop;
function Autocomplete(el, options) { function Autocomplete(el, options) {
@ -73,7 +70,7 @@
that.options = $.extend(true, {}, Autocomplete.defaults, options); that.options = $.extend(true, {}, Autocomplete.defaults, options);
that.classes = { that.classes = {
selected: 'autocomplete-selected', selected: 'autocomplete-selected',
suggestion: 'autocomplete-suggestion' suggestion: 'autocomplete-suggestion',
}; };
that.hint = null; that.hint = null;
that.hintValue = ''; that.hintValue = '';
@ -123,16 +120,16 @@
showNoSuggestionNotice: false, showNoSuggestionNotice: false,
noSuggestionNotice: 'No results', noSuggestionNotice: 'No results',
orientation: 'bottom', orientation: 'bottom',
forceFixPosition: false forceFixPosition: false,
}; };
function _lookupFilter(suggestion, originalQuery, queryLowerCase) { function _lookupFilter(suggestion, originalQuery, queryLowerCase) {
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1; return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
}; }
function _transformResult(response) { function _transformResult(response) {
return typeof response === 'string' ? $.parseJSON(response) : response; return typeof response === 'string' ? $.parseJSON(response) : response;
}; }
function _formatResult(suggestion, currentValue) { function _formatResult(suggestion, currentValue) {
// Do not replace anything if the current value is empty // Do not replace anything if the current value is empty
@ -143,20 +140,19 @@
var pattern = '(' + utils.escapeRegExChars(currentValue) + ')'; var pattern = '(' + utils.escapeRegExChars(currentValue) + ')';
return suggestion.value return suggestion.value
.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>') .replace(new RegExp(pattern, 'gi'), '<strong>$1</strong>')
.replace(/&/g, '&amp;') .replace(/&/g, '&amp;')
.replace(/</g, '&lt;') .replace(/</g, '&lt;')
.replace(/>/g, '&gt;') .replace(/>/g, '&gt;')
.replace(/"/g, '&quot;') .replace(/"/g, '&quot;')
.replace(/&lt;(\/?strong)&gt;/g, '<$1>'); .replace(/&lt;(\/?strong)&gt;/g, '<$1>');
}; }
function _formatGroup(suggestion, category) { function _formatGroup(suggestion, category) {
return '<div class="autocomplete-group">' + category + '</div>'; return '<div class="autocomplete-group">' + category + '</div>';
}; }
Autocomplete.prototype = { Autocomplete.prototype = {
initialize: function () { initialize: function () {
var that = this, var that = this,
suggestionSelector = '.' + that.classes.suggestion, suggestionSelector = '.' + that.classes.suggestion,
@ -168,7 +164,8 @@
// html() deals with many types: htmlString or Element or Array or jQuery // html() deals with many types: htmlString or Element or Array or jQuery
that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>') that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>')
.html(this.options.noSuggestionNotice).get(0); .html(this.options.noSuggestionNotice)
.get(0);
that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass); that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass);
@ -199,7 +196,7 @@
container.on('click.autocomplete', function () { container.on('click.autocomplete', function () {
clearTimeout(that.blurTimeoutId); clearTimeout(that.blurTimeoutId);
}) });
that.fixPositionCapture = function () { that.fixPositionCapture = function () {
if (that.visible) { if (that.visible) {
@ -209,12 +206,24 @@
$(window).on('resize.autocomplete', that.fixPositionCapture); $(window).on('resize.autocomplete', that.fixPositionCapture);
that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); }); that.el.on('keydown.autocomplete', function (e) {
that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); }); that.onKeyPress(e);
that.el.on('blur.autocomplete', function () { that.onBlur(); }); });
that.el.on('focus.autocomplete', function () { that.onFocus(); }); that.el.on('keyup.autocomplete', function (e) {
that.el.on('change.autocomplete', function (e) { that.onKeyUp(e); }); that.onKeyUp(e);
that.el.on('input.autocomplete', function (e) { that.onKeyUp(e); }); });
that.el.on('blur.autocomplete', function () {
that.onBlur();
});
that.el.on('focus.autocomplete', function () {
that.onFocus();
});
that.el.on('change.autocomplete', function (e) {
that.onKeyUp(e);
});
that.el.on('input.autocomplete', function (e) {
that.onKeyUp(e);
});
}, },
onFocus: function () { onFocus: function () {
@ -271,14 +280,13 @@
// Adjust height, width and z-index: // Adjust height, width and z-index:
$(that.suggestionsContainer).css({ $(that.suggestionsContainer).css({
'max-height': options.maxHeight + 'px', 'max-height': options.maxHeight + 'px',
'width': options.width + 'px', width: options.width + 'px',
'z-index': options.zIndex 'z-index': options.zIndex,
}); });
this.options = options; this.options = options;
}, },
clearCache: function () { clearCache: function () {
this.cachedResponse = {}; this.cachedResponse = {};
this.badQueries = []; this.badQueries = [];
@ -318,15 +326,17 @@
containerHeight = $container.outerHeight(), containerHeight = $container.outerHeight(),
height = that.el.outerHeight(), height = that.el.outerHeight(),
offset = that.el.offset(), offset = that.el.offset(),
styles = { 'top': offset.top, 'left': offset.left }; styles = { top: offset.top, left: offset.left };
if (orientation === 'auto') { if (orientation === 'auto') {
var viewPortHeight = $(window).height(), var viewPortHeight = $(window).height(),
scrollTop = $(window).scrollTop(), scrollTop = $(window).scrollTop(),
topOverflow = -scrollTop + offset.top - containerHeight, topOverflow = -scrollTop + offset.top - containerHeight,
bottomOverflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); bottomOverflow =
scrollTop + viewPortHeight - (offset.top + height + containerHeight);
orientation = (Math.max(topOverflow, bottomOverflow) === topOverflow) ? 'top' : 'bottom'; orientation =
Math.max(topOverflow, bottomOverflow) === topOverflow ? 'top' : 'bottom';
} }
if (orientation === 'top') { if (orientation === 'top') {
@ -503,7 +513,10 @@
isExactMatch: function (query) { isExactMatch: function (query) {
var suggestions = this.suggestions; var suggestions = this.suggestions;
return (suggestions.length === 1 && suggestions[0].value.toLowerCase() === query.toLowerCase()); return (
suggestions.length === 1 &&
suggestions[0].value.toLowerCase() === query.toLowerCase()
);
}, },
getQuery: function (value) { getQuery: function (value) {
@ -528,7 +541,7 @@
data = { data = {
suggestions: $.grep(options.lookup, function (suggestion) { suggestions: $.grep(options.lookup, function (suggestion) {
return filter(suggestion, query, queryLowerCase); return filter(suggestion, query, queryLowerCase);
}) }),
}; };
if (limit && data.suggestions.length > limit) { if (limit && data.suggestions.length > limit) {
@ -585,18 +598,20 @@
url: serviceUrl, url: serviceUrl,
data: params, data: params,
type: options.type, type: options.type,
dataType: options.dataType dataType: options.dataType,
}; };
$.extend(ajaxSettings, options.ajaxSettings); $.extend(ajaxSettings, options.ajaxSettings);
that.currentRequest = $.ajax(ajaxSettings).done(function (data) { that.currentRequest = $.ajax(ajaxSettings)
.done(function (data) {
var result; var result;
that.currentRequest = null; that.currentRequest = null;
result = options.transformResult(data, q); result = options.transformResult(data, q);
that.processResponse(result, q, cacheKey); that.processResponse(result, q, cacheKey);
options.onSearchComplete.call(that.element, q, result.suggestions); options.onSearchComplete.call(that.element, q, result.suggestions);
}).fail(function (jqXHR, textStatus, errorThrown) { })
.fail(function (jqXHR, textStatus, errorThrown) {
options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown); options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
}); });
} else { } else {
@ -681,7 +696,14 @@
html += formatGroup(suggestion, value, i); html += formatGroup(suggestion, value, i);
} }
html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value, i) + '</div>'; html +=
'<div class="' +
className +
'" data-index="' +
i +
'">' +
formatResult(suggestion, value, i) +
'</div>';
}); });
this.adjustContainerWidth(); this.adjustContainerWidth();
@ -700,7 +722,10 @@
if (options.autoSelectFirst) { if (options.autoSelectFirst) {
that.selectedIndex = 0; that.selectedIndex = 0;
container.scrollTop(0); container.scrollTop(0);
container.children('.' + className).first().addClass(classSelected); container
.children('.' + className)
.first()
.addClass(classSelected);
} }
that.visible = true; that.visible = true;
@ -874,7 +899,10 @@
} }
if (that.selectedIndex === 0) { if (that.selectedIndex === 0) {
$(that.suggestionsContainer).children('.' + that.classes.suggestion).first().removeClass(that.classes.selected); $(that.suggestionsContainer)
.children('.' + that.classes.suggestion)
.first()
.removeClass(that.classes.selected);
that.selectedIndex = -1; that.selectedIndex = -1;
that.ignoreValueChange = false; that.ignoreValueChange = false;
that.el.val(that.currentValue); that.el.val(that.currentValue);
@ -888,7 +916,7 @@
moveDown: function () { moveDown: function () {
var that = this; var that = this;
if (that.selectedIndex === (that.suggestions.length - 1)) { if (that.selectedIndex === that.suggestions.length - 1) {
return; return;
} }
@ -915,7 +943,9 @@
if (offsetTop < upperBound) { if (offsetTop < upperBound) {
$(that.suggestionsContainer).scrollTop(offsetTop); $(that.suggestionsContainer).scrollTop(offsetTop);
} else if (offsetTop > lowerBound) { } else if (offsetTop > lowerBound) {
$(that.suggestionsContainer).scrollTop(offsetTop - that.options.maxHeight + heightDelta); $(that.suggestionsContainer).scrollTop(
offsetTop - that.options.maxHeight + heightDelta,
);
} }
if (!that.options.preserveInput) { if (!that.options.preserveInput) {
@ -967,7 +997,9 @@
return value; return value;
} }
return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value; return (
currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value
);
}, },
dispose: function () { dispose: function () {
@ -975,7 +1007,7 @@
that.el.off('.autocomplete').removeData('autocomplete'); that.el.off('.autocomplete').removeData('autocomplete');
$(window).off('resize.autocomplete', that.fixPositionCapture); $(window).off('resize.autocomplete', that.fixPositionCapture);
$(that.suggestionsContainer).remove(); $(that.suggestionsContainer).remove();
} },
}; };
// Create chainable jQuery plugin: // Create chainable jQuery plugin:
@ -1010,4 +1042,4 @@
if (!$.fn.autocomplete) { if (!$.fn.autocomplete) {
$.fn.autocomplete = $.fn.devbridgeAutocomplete; $.fn.autocomplete = $.fn.devbridgeAutocomplete;
} }
})); });