blob: 10709b8801b0dd76ca8657d43778eacd8c14972b [file] [log] [blame]
Leo Reppd162b2e2021-06-30 13:51:07 +02001/**
2 * API/ skeleton/ base class for an item contained within a container.
3 * Here we see which functions container supports for containerItems.
4 *
5 * @author Leo Repp
6 */
7
8
9//"use strict";
10
11define({
12
13 /**
14 * API for an item contained within a container
15 */
16 create : function () {
17 return Object.create(this); //._init();
18 },
19
20 /**
21 * Upgrade this object to another object,
22 * while private data stays intact.
23 *
24 * @param {Object} An object with properties.
25 */
26 upgradeTo : function (props) {
27 for (let prop in props) {
28 this[prop] = props[prop];
29 };
30 return this;
31 },
32
33 /**
34 * Check or set if the item is active
35 *
36 * @param {boolean|null} State of activity
37 */
38 active : function (bool) {
39 const cl = this.element().classList;
40 if (bool === undefined) return cl.contains("active");
41 else if (bool) cl.add("active");
42 else cl.remove("active"); //allows for setting it to inactive if not (equal to undefined or truthy)
43 },
44
45 /**
46 * Get/create the document element of the container item. Can be overwritten. Standard class: li
47 */
48 element : function () {
49 // already defined
50 if (this._el !== undefined) return this._el;
51
52 // Create list item
53 const li = document.createElement("li");
54
55 // Connect action
56 if (this["onclick"] !== undefined) {
57 li["onclick"] = this.onclick.bind(this);
58 };
59 return this._el = li;
60 },
61
62 /**
63 * Expected to be overwritten
64 * @returns whether the item is currently an option to be selected, or if it should just be skipped
65 */
66 isSelectable : function () {
67 return true;
68 },
69
70 /**
71 * API skeleton for reading letters. Expected to be overwritten.
72 * @param {String} letter The letter to be read
73 */
74 add : function (letter) {},
75
76
77 /**
78 * API skeleton for clearing whole contents. Expected to be overwritten.
79 */
80 clear : function () {},
81
82
83 /**
84 * API skeleton method for execution. Expected to be overwritten.
85 * @param {Event} event Event passed down by menu.
86 */
87 onclick : function (e) {},
88
89
90 /**
91 * API skeleton method for when backspace is pressed. Expected to be overwritten.
92 */
93 chop : function () {},
94
95 /**
96 * API skeleton method for pressing "right". Expected to be overwritten.
97 * @param {Event} event Event passed down by menu.
98 */
99 further : function (e) {},
100
101 /**
102 * Return menu list. This._menu gets written by the container class
103 */
104 menu : function () {
105 return this._menu;
106 }
107
108});