2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-09-19 08:49: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": {
"grunt": "^1.0.1",
"grunt-contrib-uglify": "^1.0.1"
"grunt-contrib-uglify": "^1.0.1",
"prettier": "^3.0.3"
}
},
"node_modules/@types/jquery": {
@ -1485,6 +1486,21 @@
"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": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-1.0.4.tgz",

View File

@ -7,6 +7,9 @@
"main": "dist/jquery.autocomplete.js",
"types": "./typings/jquery-autocomplete/jquery.autocomplete.d.ts",
"license": "MIT",
"scripts": {
"format": "prettier --write ./src/**"
},
"repository": {
"type": "git",
"url": "git://github.com/devbridge/jQuery-Autocomplete.git"
@ -17,7 +20,13 @@
},
"devDependencies": {
"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": [
"dist/",

View File

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