Skip to content

Commit 4c448c3

Browse files
committed
Fix review suggestions/specs.
1 parent f9694b9 commit 4c448c3

File tree

3 files changed

+87
-15
lines changed

3 files changed

+87
-15
lines changed

src/pat/auto-suggest/auto-suggest.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ parser.addArgument("ajax-data-type", "JSON");
1313
parser.addArgument("ajax-search-index", "");
1414
parser.addArgument("ajax-timeout", 400);
1515
parser.addArgument("ajax-url", "");
16-
parser.addArgument("max-initial-size", 10); // AJAX search results limit for the first page.
17-
parser.addArgument("ajax-batch-size", 10); // AJAX search results limit for subsequent pages.
16+
parser.addArgument("max-initial-size", 0); // AJAX search results limit for the first page.
17+
parser.addArgument("ajax-batch-size", 0); // AJAX search results limit for subsequent pages.
1818
parser.addArgument("allow-new-words", true); // Should custom tags be allowed?
1919
parser.addArgument("max-selection-size", 0);
2020
parser.addArgument("minimum-input-length"); // Don't restrict by default so that all results show
@@ -259,12 +259,18 @@ export default Base.extend({
259259
type: "GET",
260260
quietMillis: this.options.ajax.timeout,
261261
data: (term, page) => {
262-
return {
262+
const request_data = {
263263
index: this.options.ajax["search-index"],
264264
q: term, // search term
265-
page_limit: this.page_limit(page),
266265
page: page,
267266
};
267+
268+
const page_limit = this.page_limit(page);
269+
if (page_limit > 0) {
270+
request_data.page_limit = page_limit;
271+
}
272+
273+
return request_data;
268274
},
269275
results: (data, page) => {
270276
// Parse the results into the format expected by Select2.
@@ -289,12 +295,14 @@ export default Base.extend({
289295
},
290296

291297
page_limit(page) {
298+
// Page limit for subsequent pages.
299+
const batch_size = this.options.ajax["batch-size"] || 0;
292300
// Page limit for the first page of a batch.
293-
let page_limit = this.options.max["initial-size"];
294-
if (page > 1) {
295-
// Page limit for subsequent pages.
296-
page_limit = this.options.ajax["batch-size"];
297-
}
301+
const initial_size = batch_size > 0 ? this.options.max["initial-size"] || 0 : 0;
302+
303+
// Fall back to batch_size if initial_size is not set.
304+
const page_limit = page > 1 ? batch_size : initial_size || batch_size;
305+
298306
return page_limit;
299307
},
300308

src/pat/auto-suggest/auto-suggest.test.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ describe("pat-autosuggest", function () {
580580
type="text"
581581
class="pat-autosuggest"
582582
data-pat-autosuggest="
583-
ajax-url: http://test.org/test;
583+
ajax-url: http://test.org/test;
584584
ajax-timeout: 1;
585585
" />
586586
`;
@@ -707,5 +707,67 @@ describe("pat-autosuggest", function () {
707707
);
708708
expect(load_more_3.length).toBe(0);
709709
});
710+
711+
describe("6.3 - Test the page_limit logic.", function () {
712+
713+
it("6.3.1 - page_limit set only by ajax-batch-size.", async function () {
714+
document.body.innerHTML = `
715+
<input
716+
type="text"
717+
class="pat-autosuggest"
718+
data-pat-autosuggest="
719+
ajax-url: http://test.org/test;
720+
ajax-batch-size: 2;
721+
" />
722+
`;
723+
724+
const input = document.querySelector("input");
725+
const instance = new pattern(input);
726+
await utils.timeout(1); // wait a tick for async to settle.
727+
728+
expect(instance.page_limit(1)).toBe(2);
729+
expect(instance.page_limit(2)).toBe(2);
730+
});
731+
732+
it("6.3.2 - page_limit set by ajax-batch-size and max-initial-size.", async function () {
733+
document.body.innerHTML = `
734+
<input
735+
type="text"
736+
class="pat-autosuggest"
737+
data-pat-autosuggest="
738+
ajax-url: http://test.org/test;
739+
ajax-batch-size: 2;
740+
max-initial-size: 4;
741+
" />
742+
`;
743+
744+
const input = document.querySelector("input");
745+
const instance = new pattern(input);
746+
await utils.timeout(1); // wait a tick for async to settle.
747+
748+
expect(instance.page_limit(1)).toBe(4);
749+
expect(instance.page_limit(2)).toBe(2);
750+
});
751+
752+
it("6.3.3 - page_limit set only by max-initial-size and batching not activated.", async function () {
753+
document.body.innerHTML = `
754+
<input
755+
type="text"
756+
class="pat-autosuggest"
757+
data-pat-autosuggest="
758+
ajax-url: http://test.org/test;
759+
max-initial-size: 4;
760+
" />
761+
`;
762+
763+
const input = document.querySelector("input");
764+
const instance = new pattern(input);
765+
await utils.timeout(1); // wait a tick for async to settle.
766+
767+
expect(instance.page_limit(1)).toBe(0);
768+
expect(instance.page_limit(2)).toBe(0);
769+
});
770+
771+
});
710772
});
711773
});

src/pat/auto-suggest/documentation.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ Pre-fill the input element with words in JSON format and don't allow the user to
3737
### Batching support
3838

3939
This pattern can load data in batches via AJAX.
40-
The following example demonstrates how define batch sizes for the initial load (`max-initial-size`) and for subsequent loads (`ajax-batch-size`).
41-
Both values default to 10.
40+
The following example demonstrates how to define batch sizes for the initial load (`max-initial-size`) and for subsequent loads (`ajax-batch-size`).
41+
If the initial load is not defined or set to 0, the `ajax-batch-size` is used for the first load as well.
42+
Both values default to `null` - batching support is disabled by default.
43+
Setting `ajax-batch-size` allone will enable batching support.
4244

4345
<input
4446
type="text"
4547
class="pat-auto-suggest"
4648
data-pat-auto-suggest="
4749
ajax-url: /path/to/data.json;
4850
ajax-batch-size: 10;
49-
max-initial-size: 10;
51+
max-initial-size: 20;
5052
"
5153
/>
5254

@@ -73,13 +75,13 @@ You can customise the behaviour of a gallery through options in the `data-pat-au
7375

7476
| Property | Type | Default Value | Description |
7577
| -------------------- | ------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
76-
| ajax-batch-size | Number | 10 | Batch size for subsequent pages of a bigger result set. For the first page, `max-initial-size` is used. |
78+
| ajax-batch-size | Number | null | Batch size for subsequent pages of a bigger result set. For the first page, `max-initial-size` is used. |
7779
| ajax-data-type | String | "json" | In what format will AJAX fetched data be returned in? |
7880
| ajax-search-index | String | | The index or key which must be used to determine the value from the returned data. |
7981
| ajax-timeout | Number | 400 | Timeout before new ajax requests are sent. The default value is set to `400` milliseconds and prevents querying the server too often while typing. |
8082
| ajax-url | URL | | The URL which must be called via AJAX to fetch remote data. |
8183
| allow-new-words | Boolean | true | Besides the suggested words, also allow custom user-defined words to be entered. |
82-
| max-initial-size | Number | 10 | Initial batch size. Display `max-initial-size` items on the first page of a bigger result set. |
84+
| max-initial-size | Number | null | Initial batch size. Display `max-initial-size` items on the first page of a bigger result set. |
8385
| max-selection-size | Number | 0 | How many values are allowed? Provide a positive number or 0 for unlimited. |
8486
| placeholder | String | Enter text | The placeholder text for the form input. The `placeholder` attribute of the form element can also be used. |
8587
| prefill | List | | A comma separated list of values with which the form element must be filled in with. The `value-separator` option does not have an effect here. |

0 commit comments

Comments
 (0)