2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
const {mock_esm, set_global, zrequire} = require("./lib/namespace.js");
|
|
|
|
const {run_test, noop} = require("./lib/test.js");
|
|
|
|
const $ = require("./lib/zjquery.js");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", {});
|
2023-10-25 02:23:11 +02:00
|
|
|
class ClipboardEvent {}
|
|
|
|
set_global("ClipboardEvent", ClipboardEvent);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/ui_util", {
|
2018-03-07 20:46:13 +01:00
|
|
|
place_caret_at_end: noop,
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("getSelection", () => ({
|
2020-07-02 01:41:40 +02:00
|
|
|
anchorOffset: 0,
|
|
|
|
}));
|
2018-08-24 19:09:09 +02:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const input_pill = zrequire("input_pill");
|
|
|
|
|
2024-07-30 00:22:57 +02:00
|
|
|
function pill_html(value) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {
|
2018-03-31 13:22:29 +02:00
|
|
|
display_value: value,
|
|
|
|
};
|
2023-02-22 23:04:10 +01:00
|
|
|
return require("../templates/input_pill.hbs")(opts);
|
2018-03-07 20:46:13 +01:00
|
|
|
}
|
|
|
|
|
2022-07-11 04:34:15 +02:00
|
|
|
run_test("basics", ({mock_template}) => {
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.equal(data.display_value, "JavaScript");
|
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_input = $.create("pill_input");
|
|
|
|
const $container = $.create("container");
|
|
|
|
$container.set_find_results(".input", $pill_input);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2023-10-27 18:29:03 +02:00
|
|
|
const widget = input_pill.create({
|
|
|
|
$container,
|
|
|
|
create_item_from_text: noop,
|
|
|
|
get_text_from_item: noop,
|
2024-07-30 06:13:31 +02:00
|
|
|
get_display_value_from_item: (item) => item.language,
|
2023-10-27 18:29:03 +02:00
|
|
|
});
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2021-04-27 16:56:20 +02:00
|
|
|
// type for a pill can be any string but it needs to be
|
|
|
|
// defined while creating any pill.
|
2019-11-02 00:06:25 +01:00
|
|
|
const item = {
|
2024-07-30 06:13:31 +02:00
|
|
|
language: "JavaScript",
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "language",
|
2018-03-07 20:46:13 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let inserted_before;
|
2024-07-30 00:22:57 +02:00
|
|
|
const expected_html = pill_html("JavaScript");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_input.before = ($elem) => {
|
2018-03-07 20:46:13 +01:00
|
|
|
inserted_before = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($elem.html(), expected_html);
|
2018-03-07 20:46:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
widget.appendValidatedData(item);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(inserted_before);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
assert.deepEqual(widget.items(), [item]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2018-08-24 17:47:26 +02:00
|
|
|
function set_up() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = {
|
2018-03-07 20:46:13 +01:00
|
|
|
blue: {
|
2024-07-30 06:13:31 +02:00
|
|
|
color_name: "BLUE",
|
2020-07-15 01:29:15 +02:00
|
|
|
description: "color of the sky",
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "color",
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
red: {
|
2024-07-30 06:13:31 +02:00
|
|
|
color_name: "RED",
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "color",
|
2020-07-15 01:29:15 +02:00
|
|
|
description: "color of stop signs",
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
yellow: {
|
2024-07-30 06:13:31 +02:00
|
|
|
color_name: "YELLOW",
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "color",
|
2020-07-15 01:29:15 +02:00
|
|
|
description: "color of bananas",
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_input = $.create("pill_input");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_input[0] = {};
|
2024-08-20 07:18:02 +02:00
|
|
|
$pill_input.length = 1;
|
2023-12-14 23:51:33 +01:00
|
|
|
$pill_input.before = noop;
|
2018-08-26 13:40:22 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const create_item_from_text = (text) => items[text];
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = $.create("container");
|
|
|
|
$container.set_find_results(".input", $pill_input);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const config = {
|
2022-01-25 11:36:19 +01:00
|
|
|
$container,
|
2020-07-20 22:18:43 +02:00
|
|
|
create_item_from_text,
|
2024-07-30 06:13:31 +02:00
|
|
|
get_text_from_item: (item) => item.color_name,
|
|
|
|
get_display_value_from_item: (item) => item.color_name,
|
2018-03-07 20:46:13 +01:00
|
|
|
};
|
|
|
|
|
2018-08-24 17:47:26 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
config,
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_input,
|
2020-07-20 22:18:43 +02:00
|
|
|
items,
|
2022-01-25 11:36:19 +01:00
|
|
|
$container,
|
2018-08-24 17:47:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-11 04:34:15 +02:00
|
|
|
run_test("copy from pill", ({mock_template}) => {
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.ok(["BLUE", "RED"].includes(data.display_value));
|
2022-07-11 04:27:00 +02:00
|
|
|
$(html)[0] = `<pill-stub ${data.display_value}>`;
|
2024-08-20 07:18:02 +02:00
|
|
|
$(html).length = 1;
|
2021-06-27 15:13:19 +02:00
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
2018-08-26 17:01:06 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
2018-08-26 17:01:06 +02:00
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-26 17:01:06 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const copy_handler = $container.get_on_handler("copy", ".pill");
|
2018-08-26 17:01:06 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let copied_text;
|
2018-08-26 17:01:06 +02:00
|
|
|
|
2023-04-08 09:48:21 +02:00
|
|
|
const $pill_stub = "<pill-stub RED>";
|
2018-08-26 17:01:06 +02:00
|
|
|
|
2023-10-25 02:23:11 +02:00
|
|
|
const originalEvent = new ClipboardEvent();
|
|
|
|
originalEvent.clipboardData = {
|
|
|
|
setData(format, text) {
|
|
|
|
assert.equal(format, "text/plain");
|
|
|
|
copied_text = text;
|
|
|
|
},
|
|
|
|
};
|
2018-08-26 17:01:06 +02:00
|
|
|
const e = {
|
2023-10-25 02:23:11 +02:00
|
|
|
originalEvent,
|
2018-08-26 17:01:06 +02:00
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
2024-05-03 22:37:10 +02:00
|
|
|
copy_handler.call($pill_stub, e);
|
2018-08-26 17:01:06 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(copied_text, "RED");
|
2018-08-26 17:01:06 +02:00
|
|
|
});
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
run_test("paste to input", ({mock_template}) => {
|
2024-07-30 00:22:57 +02:00
|
|
|
mock_template("input_pill.hbs", true, (_data, html) => html);
|
2021-06-27 15:13:19 +02:00
|
|
|
|
2018-08-26 17:15:12 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
2018-08-26 17:15:12 +02:00
|
|
|
const items = info.items;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const paste_handler = $container.get_on_handler("paste", ".input");
|
2018-08-26 17:15:12 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const paste_text = "blue,yellow";
|
2018-08-26 17:15:12 +02:00
|
|
|
|
2023-10-25 02:23:11 +02:00
|
|
|
const originalEvent = new ClipboardEvent();
|
|
|
|
originalEvent.clipboardData = {
|
|
|
|
getData(format) {
|
|
|
|
assert.equal(format, "text/plain");
|
|
|
|
return paste_text;
|
2018-08-26 17:15:12 +02:00
|
|
|
},
|
2023-10-25 02:23:11 +02:00
|
|
|
};
|
|
|
|
const e = {
|
|
|
|
originalEvent,
|
2018-08-26 17:15:12 +02:00
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
document.execCommand = (cmd, _, text) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(cmd, "insertText");
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.find(".input").text(text);
|
2018-08-26 17:15:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
paste_handler(e);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.yellow]);
|
2020-05-15 01:27:15 +02:00
|
|
|
|
|
|
|
let entered = false;
|
2020-07-02 01:45:54 +02:00
|
|
|
widget.createPillonPaste(() => {
|
2020-05-15 01:27:15 +02:00
|
|
|
entered = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
paste_handler(e);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(entered);
|
2018-08-26 17:15:12 +02:00
|
|
|
});
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
run_test("arrows on pills", ({mock_template}) => {
|
2024-07-30 00:22:57 +02:00
|
|
|
mock_template("input_pill.hbs", true, (_data, html) => html);
|
2021-06-27 15:13:19 +02:00
|
|
|
|
2018-08-26 21:16:46 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
2018-08-26 21:16:46 +02:00
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-26 21:16:46 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const key_handler = $container.get_on_handler("keydown", ".pill");
|
2018-08-26 21:16:46 +02:00
|
|
|
|
|
|
|
function test_key(c) {
|
|
|
|
key_handler({
|
2021-05-29 22:06:13 +02:00
|
|
|
key: c,
|
2018-08-26 21:16:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let prev_focused = false;
|
|
|
|
let next_focused = false;
|
2018-08-26 21:16:46 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_stub = {
|
2020-07-02 01:41:40 +02:00
|
|
|
prev: () => ({
|
2022-11-17 23:33:43 +01:00
|
|
|
trigger(type) {
|
2020-07-20 21:24:26 +02:00
|
|
|
if (type === "focus") {
|
|
|
|
prev_focused = true;
|
|
|
|
}
|
2020-07-02 01:41:40 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
next: () => ({
|
2022-11-17 23:33:43 +01:00
|
|
|
trigger(type) {
|
2020-07-20 21:24:26 +02:00
|
|
|
if (type === "focus") {
|
|
|
|
next_focused = true;
|
|
|
|
}
|
2020-07-02 01:41:40 +02:00
|
|
|
},
|
|
|
|
}),
|
2018-08-26 21:16:46 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.set_find_results(".pill:focus", $pill_stub);
|
2018-08-26 21:16:46 +02:00
|
|
|
|
|
|
|
// We use the same stub to test both arrows, since we don't
|
|
|
|
// actually cause any real state changes here. We stub out
|
|
|
|
// the only interaction, which is to move the focus.
|
2021-05-29 22:06:13 +02:00
|
|
|
test_key("ArrowLeft");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(prev_focused);
|
2018-08-26 21:16:46 +02:00
|
|
|
|
2021-05-29 22:06:13 +02:00
|
|
|
test_key("ArrowRight");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(next_focused);
|
2018-08-26 21:16:46 +02:00
|
|
|
});
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
run_test("left arrow on input", ({mock_template}) => {
|
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.equal(typeof data.display_value, "string");
|
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
2018-08-24 19:09:09 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
2018-08-24 19:09:09 +02:00
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-24 19:09:09 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const key_handler = $container.get_on_handler("keydown", ".input");
|
2018-08-24 19:09:09 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let last_pill_focused = false;
|
2018-08-24 19:09:09 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.set_find_results(".pill", {
|
2020-07-02 01:41:40 +02:00
|
|
|
last: () => ({
|
2022-11-17 23:33:43 +01:00
|
|
|
trigger(type) {
|
2020-07-20 21:24:26 +02:00
|
|
|
if (type === "focus") {
|
|
|
|
last_pill_focused = true;
|
|
|
|
}
|
2020-07-02 01:41:40 +02:00
|
|
|
},
|
|
|
|
}),
|
2018-08-24 19:09:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
key_handler({
|
2021-05-29 22:06:13 +02:00
|
|
|
key: "ArrowLeft",
|
2018-08-24 19:09:09 +02:00
|
|
|
});
|
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(last_pill_focused);
|
2018-08-24 19:09:09 +02:00
|
|
|
});
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
run_test("comma", ({mock_template}) => {
|
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.equal(typeof data.display_value, "string");
|
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
2018-08-24 18:58:02 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const items = info.items;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_input = info.$pill_input;
|
|
|
|
const $container = info.$container;
|
2018-08-24 18:58:02 +02:00
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 18:58:02 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const key_handler = $container.get_on_handler("keydown", ".input");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_input.text(" yel");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
|
|
|
key_handler({
|
2021-05-29 22:06:13 +02:00
|
|
|
key: ",",
|
2018-08-24 18:58:02 +02:00
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 18:58:02 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_input.text(" yellow");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
|
|
|
key_handler({
|
2021-05-29 22:06:13 +02:00
|
|
|
key: ",",
|
2020-06-01 02:42:46 +02:00
|
|
|
preventDefault: noop,
|
2018-08-24 18:58:02 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red, items.yellow]);
|
2018-08-24 18:58:02 +02:00
|
|
|
});
|
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
run_test("Enter key with text", ({mock_template}) => {
|
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.equal(typeof data.display_value, "string");
|
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
2018-08-24 18:09:57 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const items = info.items;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
2018-08-24 18:09:57 +02:00
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-24 18:09:57 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 18:09:57 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const key_handler = $container.get_on_handler("keydown", ".input");
|
2018-08-24 18:09:57 +02:00
|
|
|
|
2024-05-03 22:37:10 +02:00
|
|
|
key_handler.call(
|
|
|
|
{
|
2020-10-07 10:15:47 +02:00
|
|
|
textContent: " yellow ",
|
2018-08-24 18:09:57 +02:00
|
|
|
},
|
2024-05-03 22:37:10 +02:00
|
|
|
{
|
|
|
|
key: "Enter",
|
|
|
|
preventDefault: noop,
|
|
|
|
stopPropagation: noop,
|
|
|
|
},
|
|
|
|
);
|
2018-08-24 18:09:57 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red, items.yellow]);
|
2018-08-24 18:09:57 +02:00
|
|
|
});
|
|
|
|
|
2022-07-11 04:34:15 +02:00
|
|
|
run_test("insert_remove", ({mock_template}) => {
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.equal(typeof data.display_value, "string");
|
|
|
|
assert.ok(html.startsWith, "<div class='pill'");
|
2024-08-20 07:18:02 +02:00
|
|
|
$(html).length = 1;
|
2022-07-11 04:27:00 +02:00
|
|
|
$(html)[0] = `<pill-stub ${data.display_value}>`;
|
2021-06-27 15:13:19 +02:00
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
2018-08-24 17:47:26 +02:00
|
|
|
const info = set_up();
|
|
|
|
|
|
|
|
const config = info.config;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_input = info.$pill_input;
|
2018-08-24 17:47:26 +02:00
|
|
|
const items = info.items;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
2018-08-24 17:47:26 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const inserted_html = [];
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_input.before = ($elem) => {
|
|
|
|
inserted_html.push($elem.html());
|
2018-08-24 17:47:26 +02:00
|
|
|
};
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget = input_pill.create(config);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let created;
|
|
|
|
let removed;
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
widget.onPillCreate(() => {
|
2018-03-07 20:46:13 +01:00
|
|
|
created = true;
|
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
widget.onPillRemove(() => {
|
2018-03-07 20:46:13 +01:00
|
|
|
removed = true;
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,chartreuse,red,yellow,mauve");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(created);
|
|
|
|
assert.ok(!removed);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2024-07-30 00:22:57 +02:00
|
|
|
assert.deepEqual(inserted_html, [pill_html("BLUE"), pill_html("RED"), pill_html("YELLOW")]);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red, items.yellow]);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($pill_input.text(), "chartreuse, mauve");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2018-09-18 01:25:38 +02:00
|
|
|
assert.equal(widget.is_pending(), true);
|
2018-03-07 20:46:13 +01:00
|
|
|
widget.clear_text();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($pill_input.text(), "");
|
2018-09-18 01:25:38 +02:00
|
|
|
assert.equal(widget.is_pending(), false);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
let color_removed;
|
|
|
|
function set_colored_removed_func(color) {
|
|
|
|
return () => {
|
|
|
|
color_removed = color;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-09-27 03:17:08 +02:00
|
|
|
let color_focused;
|
|
|
|
function handle_event(color) {
|
|
|
|
return (event) => {
|
|
|
|
assert.equal(event, "focus");
|
|
|
|
color_focused = color;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
const pills = widget._get_pills_for_testing();
|
|
|
|
for (const pill of pills) {
|
2024-07-30 06:13:31 +02:00
|
|
|
pill.$element.remove = set_colored_removed_func(pill.item.color_name);
|
2024-09-27 03:17:08 +02:00
|
|
|
pill.$element.trigger = handle_event(pill.item.color_name);
|
2021-02-25 16:07:04 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
let key_handler = $container.get_on_handler("keydown", ".input");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2024-05-03 22:37:10 +02:00
|
|
|
key_handler.call(
|
|
|
|
{
|
2020-10-07 10:15:47 +02:00
|
|
|
textContent: "",
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
2024-05-03 22:37:10 +02:00
|
|
|
{
|
|
|
|
key: "Backspace",
|
|
|
|
preventDefault: noop,
|
|
|
|
},
|
|
|
|
);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2024-09-27 03:17:08 +02:00
|
|
|
// The first backspace focuses the pill, the second removes it.
|
|
|
|
assert.ok(!removed);
|
|
|
|
assert.equal(color_focused, "YELLOW");
|
|
|
|
const yellow_pill = pills.find((pill) => pill.item.color_name === "YELLOW");
|
|
|
|
$container.set_find_results(".pill:focus", yellow_pill.$element);
|
2018-08-24 15:42:48 +02:00
|
|
|
|
2024-09-28 22:28:05 +02:00
|
|
|
let prev_pill_focused = false;
|
|
|
|
const $prev_pill_stub = $("<prev-stub>");
|
|
|
|
$prev_pill_stub.trigger = (type) => {
|
|
|
|
if (type === "focus") {
|
|
|
|
prev_pill_focused = true;
|
|
|
|
}
|
2018-08-24 15:42:48 +02:00
|
|
|
};
|
2024-09-28 22:28:05 +02:00
|
|
|
yellow_pill.$element.prev = () => $prev_pill_stub;
|
|
|
|
yellow_pill.$element.next = () => $("<next-stub>");
|
2024-09-27 03:17:08 +02:00
|
|
|
|
|
|
|
key_handler = $container.get_on_handler("keydown", ".pill");
|
|
|
|
key_handler.call(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
key: "Backspace",
|
|
|
|
preventDefault: noop,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assert.ok(removed);
|
|
|
|
assert.equal(color_removed, "YELLOW");
|
2024-09-28 22:28:05 +02:00
|
|
|
assert.ok(prev_pill_focused);
|
2024-09-27 03:17:08 +02:00
|
|
|
|
2024-09-28 22:28:05 +02:00
|
|
|
prev_pill_focused = false;
|
2024-09-27 03:17:08 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 15:42:48 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $focus_pill_stub = {
|
2024-09-28 22:28:05 +02:00
|
|
|
prev: () => $prev_pill_stub,
|
|
|
|
next: () => $("<next-stub>"),
|
|
|
|
[0]: "<pill-stub RED>",
|
2024-08-20 07:18:02 +02:00
|
|
|
length: 1,
|
2018-08-24 15:42:48 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.set_find_results(".pill:focus", $focus_pill_stub);
|
2018-08-24 15:42:48 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
key_handler = $container.get_on_handler("keydown", ".pill");
|
2018-08-24 15:42:48 +02:00
|
|
|
key_handler({
|
2021-05-29 22:06:13 +02:00
|
|
|
key: "Backspace",
|
2018-08-24 15:42:48 +02:00
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
|
2024-09-28 22:28:05 +02:00
|
|
|
assert.equal(color_removed, "RED");
|
|
|
|
assert.ok(prev_pill_focused);
|
2018-08-25 23:22:46 +02:00
|
|
|
});
|
|
|
|
|
2022-07-11 04:34:15 +02:00
|
|
|
run_test("exit button on pill", ({mock_template}) => {
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.equal(typeof data.display_value, "string");
|
|
|
|
assert.ok(html.startsWith, "<div class='pill'");
|
2022-07-11 04:27:00 +02:00
|
|
|
$(html)[0] = `<pill-stub ${data.display_value}>`;
|
2024-08-20 07:18:02 +02:00
|
|
|
$(html).length = 1;
|
2021-06-27 15:13:19 +02:00
|
|
|
return html;
|
|
|
|
});
|
2022-09-26 21:01:43 +02:00
|
|
|
$(".narrow_to_compose_recipients").toggleClass = noop;
|
2021-06-27 15:13:19 +02:00
|
|
|
|
2018-08-25 23:22:46 +02:00
|
|
|
const info = set_up();
|
|
|
|
|
|
|
|
const config = info.config;
|
|
|
|
const items = info.items;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
2018-08-25 23:22:46 +02:00
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-25 23:22:46 +02:00
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
const pills = widget._get_pills_for_testing();
|
|
|
|
for (const pill of pills) {
|
2023-12-14 23:51:33 +01:00
|
|
|
pill.$element.remove = noop;
|
2021-02-25 16:07:04 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $curr_pill_stub = {
|
2022-07-11 04:27:00 +02:00
|
|
|
[0]: "<pill-stub BLUE>",
|
2024-08-20 07:18:02 +02:00
|
|
|
length: 1,
|
2018-08-25 23:22:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const exit_button_stub = {
|
2020-07-02 01:41:40 +02:00
|
|
|
to_$: () => ({
|
2022-11-17 23:33:43 +01:00
|
|
|
closest(sel) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sel, ".pill");
|
2022-01-25 11:36:19 +01:00
|
|
|
return $curr_pill_stub;
|
2020-07-02 01:41:40 +02:00
|
|
|
},
|
|
|
|
}),
|
2018-08-25 23:22:46 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const e = {
|
2018-11-02 16:00:43 +01:00
|
|
|
stopPropagation: noop,
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
const exit_click_handler = $container.get_on_handler("click", ".exit");
|
2018-08-25 23:22:46 +02:00
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
exit_click_handler.call(exit_button_stub, e);
|
2018-08-25 23:22:46 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.red]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-08-26 21:29:25 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("misc things", () => {
|
2018-08-26 21:29:25 +02:00
|
|
|
const info = set_up();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = info.$container;
|
|
|
|
const $pill_input = info.$pill_input;
|
2024-07-30 07:21:29 +02:00
|
|
|
input_pill.create(info.config);
|
2018-08-26 21:29:25 +02:00
|
|
|
|
|
|
|
// animation
|
2022-01-25 11:36:19 +01:00
|
|
|
const animation_end_handler = $container.get_on_handler("animationend", ".input");
|
2018-08-26 21:29:25 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let shake_class_removed = false;
|
2018-08-26 21:29:25 +02:00
|
|
|
|
|
|
|
const input_stub = {
|
2020-07-02 01:41:40 +02:00
|
|
|
to_$: () => ({
|
2022-11-17 23:33:43 +01:00
|
|
|
removeClass(cls) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(cls, "shake");
|
2020-07-02 01:41:40 +02:00
|
|
|
shake_class_removed = true;
|
|
|
|
},
|
|
|
|
}),
|
2018-08-26 21:29:25 +02:00
|
|
|
};
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
animation_end_handler.call(input_stub);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(shake_class_removed);
|
2018-08-26 21:29:25 +02:00
|
|
|
|
|
|
|
// click on container
|
2022-01-25 11:36:19 +01:00
|
|
|
const container_click_handler = $container.get_on_handler("click");
|
2018-08-26 21:29:25 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $stub = $.create("the-pill-container");
|
|
|
|
$stub.set_find_results(".input", $pill_input);
|
|
|
|
$stub.is = (sel) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sel, ".pill-container");
|
2018-08-26 21:29:25 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const this_ = {
|
2022-01-25 11:36:19 +01:00
|
|
|
to_$: () => $stub,
|
2018-08-26 21:29:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
container_click_handler.call(this_, {target: this_});
|
|
|
|
});
|
2021-02-25 17:31:33 +01:00
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
run_test("appendValue/clear", ({mock_template}) => {
|
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
2021-06-27 15:13:19 +02:00
|
|
|
assert.equal(typeof data.display_value, "string");
|
|
|
|
assert.ok(html.startsWith, "<div class='pill'");
|
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_input = $.create("pill_input");
|
|
|
|
const $container = $.create("container");
|
|
|
|
$container.set_find_results(".input", $pill_input);
|
2021-02-25 17:31:33 +01:00
|
|
|
|
|
|
|
const config = {
|
2022-01-25 11:36:19 +01:00
|
|
|
$container,
|
2024-07-30 06:13:31 +02:00
|
|
|
create_item_from_text: (s) => ({type: "color", color_name: s}),
|
|
|
|
get_text_from_item: /* istanbul ignore next */ (s) => s.color_name,
|
|
|
|
get_display_value_from_item: (s) => s.color_name,
|
2021-02-25 17:31:33 +01:00
|
|
|
};
|
|
|
|
|
2023-12-14 23:51:33 +01:00
|
|
|
$pill_input.before = noop;
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_input[0] = {};
|
2024-08-20 07:18:02 +02:00
|
|
|
$pill_input.length = 1;
|
2021-02-25 17:31:33 +01:00
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
|
2021-03-05 17:48:22 +01:00
|
|
|
// First test some early-exit code.
|
|
|
|
widget.appendValue("");
|
|
|
|
assert.deepEqual(widget._get_pills_for_testing(), []);
|
|
|
|
|
|
|
|
// Now set up real data.
|
2021-02-25 17:31:33 +01:00
|
|
|
widget.appendValue("red,yellow,blue");
|
|
|
|
|
|
|
|
const pills = widget._get_pills_for_testing();
|
|
|
|
|
|
|
|
const removed_colors = [];
|
|
|
|
for (const pill of pills) {
|
|
|
|
pill.$element.remove = () => {
|
2024-07-30 06:13:31 +02:00
|
|
|
removed_colors.push(pill.item.color_name);
|
2021-02-25 17:31:33 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.clear();
|
|
|
|
|
|
|
|
// Note that we remove colors in the reverse order that we inserted.
|
|
|
|
assert.deepEqual(removed_colors, ["blue", "yellow", "red"]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($pill_input[0].textContent, "");
|
2021-02-25 17:31:33 +01:00
|
|
|
});
|
2024-03-23 19:36:45 +01:00
|
|
|
|
2024-05-03 10:36:28 +02:00
|
|
|
run_test("getCurrentText", ({mock_template}) => {
|
2024-03-23 19:36:45 +01:00
|
|
|
mock_template("input_pill.hbs", true, (data, html) => {
|
|
|
|
assert.equal(typeof data.display_value, "string");
|
|
|
|
return html;
|
|
|
|
});
|
|
|
|
|
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const items = info.items;
|
|
|
|
const $pill_input = info.$pill_input;
|
|
|
|
const $container = info.$container;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
widget.appendValue("blue,red");
|
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
|
|
|
|
|
|
|
$pill_input.text("yellow");
|
|
|
|
assert.equal(widget.getCurrentText(), "yellow");
|
|
|
|
|
2024-05-03 10:36:28 +02:00
|
|
|
const key_handler = $container.get_on_handler("keydown", ".input");
|
2024-03-23 19:36:45 +01:00
|
|
|
key_handler({
|
|
|
|
key: " ",
|
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
key_handler({
|
|
|
|
key: ",",
|
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
|
2024-05-03 10:36:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red, items.yellow]);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test("onTextInputHook", () => {
|
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
const $container = info.$container;
|
|
|
|
const $pill_input = info.$pill_input;
|
|
|
|
|
|
|
|
let hookCalled = false;
|
|
|
|
let currentText = "re";
|
|
|
|
const onTextInputHook = () => {
|
|
|
|
hookCalled = true;
|
|
|
|
|
|
|
|
// Test that the hook always gets the correct updated text.
|
|
|
|
assert.equal(widget.getCurrentText(), currentText);
|
|
|
|
};
|
|
|
|
|
|
|
|
widget.onTextInputHook(onTextInputHook);
|
|
|
|
|
|
|
|
const input_handler = $container.get_on_handler("input", ".input");
|
|
|
|
|
|
|
|
$pill_input.text(currentText);
|
|
|
|
input_handler();
|
|
|
|
|
|
|
|
currentText += "d";
|
|
|
|
$pill_input.text(currentText);
|
|
|
|
input_handler();
|
|
|
|
|
|
|
|
assert.ok(hookCalled);
|
2024-03-23 19:36:45 +01:00
|
|
|
});
|