Skip to content

Commit

Permalink
Add select2 features
Browse files Browse the repository at this point in the history
  • Loading branch information
avdata99 authored and amercader committed Dec 3, 2021
1 parent 8290f2e commit 37d88bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
22 changes: 9 additions & 13 deletions ckan/public/base/javascript/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ this.ckan.module('autocomplete', function (jQuery) {
tokensep: ',',
interval: 300,
dropdownClass: '',
containerClass: ''
containerClass: '',
minimumInputLength: 0
},

/* Sets up the module, binding methods, creating elements etc. Called
Expand All @@ -42,7 +43,7 @@ this.ckan.module('autocomplete', function (jQuery) {
this.setupAutoComplete();
},

/* Sets up the auto complete plugin.
/* Sets up the auto complete plugin.
*
* Returns nothing.
*/
Expand All @@ -54,7 +55,8 @@ this.ckan.module('autocomplete', function (jQuery) {
formatInputTooShort: this.formatInputTooShort,
dropdownCssClass: this.options.dropdownClass,
containerCssClass: this.options.containerClass,
tokenSeparators: this.options.tokensep.split('')
tokenSeparators: this.options.tokensep.split(''),
minimumInputLength: this.options.minimumInputLength
};

// Different keys are required depending on whether the select is
Expand Down Expand Up @@ -154,10 +156,10 @@ this.ckan.module('autocomplete', function (jQuery) {
// Kills previous timeout
clearTimeout(this._debounced);

// OK, wipe the dropdown before we start ajaxing the completions
fn({results:[]});

if (string) {
if (!string) {
// Wipe the dropdown for empty calls.
fn({results:[]});
} else {
// Set a timer to prevent the search lookup occurring too often.
this._debounced = setTimeout(function () {
var term = module._lastTerm;
Expand Down Expand Up @@ -224,12 +226,6 @@ this.ckan.module('autocomplete', function (jQuery) {
);
},

/* Takes a string and converts it into an object used by the select2 plugin.
*
* term - The term to convert.
*
* Returns an object for use in select2.
*/
formatTerm: function (term) {
term = jQuery.trim(term || '');

Expand Down
2 changes: 1 addition & 1 deletion ckan/public/base/vendor/select2/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ the specific language governing permissions and limitations under the Apache Lic
return;
}

if (opts.formatSearching && this.findHighlightableChoices().length === 0) {
if (opts.formatSearching) {
render("<li class='select2-searching'>" + evaluate(opts.formatSearching, opts.element) + "</li>");
}

Expand Down
33 changes: 29 additions & 4 deletions cypress/integration/modules/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ describe('ckan.modules.AutocompleteModule()', function () {
formatInputTooShort: this.module.formatInputTooShort,
createSearchChoice: this.module.formatTerm, // Not used by tags.
initSelection: this.module.formatInitialValue,
tokenSeparators: [',']
tokenSeparators: [','],
minimumInputLength: 0
});
});

Expand All @@ -92,7 +93,8 @@ describe('ckan.modules.AutocompleteModule()', function () {
formatNoMatches: this.module.formatNoMatches,
formatInputTooShort: this.module.formatInputTooShort,
initSelection: this.module.formatInitialValue,
tokenSeparators: [',']
tokenSeparators: [','],
minimumInputLength: 0
});
})
it('should watch the keydown event on the select2 input');
Expand All @@ -112,7 +114,8 @@ describe('ckan.modules.AutocompleteModule()', function () {
formatInputTooShort: this.module.formatInputTooShort,
createSearchChoice: this.module.formatTerm, // Not used by tags.
initSelection: this.module.formatInitialValue,
tokenSeparators: [',']
tokenSeparators: [','],
minimumInputLength: 0
});
});

Expand All @@ -131,9 +134,31 @@ describe('ckan.modules.AutocompleteModule()', function () {
formatInputTooShort: this.module.formatInputTooShort,
createSearchChoice: this.module.formatTerm, // Not used by tags.
initSelection: this.module.formatInitialValue,
tokenSeparators: [',']
tokenSeparators: [','],
minimumInputLength: 0
});
});

it('should allow a changing minimumInputLength', function () {
this.module.options.minimumInputLength = 3;
this.module.setupAutoComplete();

expect(this.select2).to.be.called;
expect(this.select2).to.be.calledWith({
width: 'resolve',
query: this.module._onQuery,
dropdownCssClass: '',
containerCssClass: '',
formatResult: this.module.formatResult,
formatNoMatches: this.module.formatNoMatches,
formatInputTooShort: this.module.formatInputTooShort,
createSearchChoice: this.module.formatTerm, // Not used by tags.
initSelection: this.module.formatInitialValue,
tokenSeparators: [','],
minimumInputLength: 3
});
});

});

describe('.getCompletions(term, fn)', function () {
Expand Down

0 comments on commit 37d88bd

Please sign in to comment.