blob: 6d0ef59daa270dd7adcc9ca91af4ab4dbb31564f [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 /**
Leo Reppd162b2e2021-06-30 13:51:07 +020021 * Check or set if the item is active
22 *
23 * @param {boolean|null} State of activity
24 */
25 active : function (bool) {
26 const cl = this.element().classList;
27 if (bool === undefined) return cl.contains("active");
28 else if (bool) cl.add("active");
29 else cl.remove("active"); //allows for setting it to inactive if not (equal to undefined or truthy)
30 },
31
32 /**
33 * Get/create the document element of the container item. Can be overwritten. Standard class: li
Leo Repp8e21cbe2021-08-18 16:37:52 +020034 * If you wish to change the textNode please overwrite the content function instead.
Leo Reppd162b2e2021-06-30 13:51:07 +020035 */
36 element : function () {
Leo Repp8e21cbe2021-08-18 16:37:52 +020037 //Call Order: First this class is created and then upgraded To whatever object is passed to container
38 //Then container calls first element() and then container()
Leo Reppd162b2e2021-06-30 13:51:07 +020039 if (this._el !== undefined) return this._el;
40
41 // Create list item
42 const li = document.createElement("li");
43
44 // Connect action
45 if (this["onclick"] !== undefined) {
46 li["onclick"] = this.onclick.bind(this);
47 };
48 return this._el = li;
49 },
50
51 /**
Leo Repp8e21cbe2021-08-18 16:37:52 +020052 * Get/create a TextNode with text "content". If content is left blank it gets set to this.defaultTextValue,
53 * or the empty string if it does not exists
54 * @param {String} content String to which to set the text
55 * @returns textNode with content or undefined
56 */
57 content : function (content) {
58 var newText; //set textNode to this
59 if (arguments.length === 1) { //new value!
60 newText = content;
61 } else { //use default
62 if (this.defaultTextValue === undefined) { //default default is ""
63 this.defaultTextValue = "";
64 }
65 newText = this.defaultTextValue;
66 };
67 if (this._content === undefined) { //no Element until now
68 this._content = document.createTextNode(newText); //create one
69 this.element().appendChild(this._content);
70 } else { //just change it
71 this._content.nodeValue = newText; // use nodeValue instead of _innerHTML
72 };
73 return this._content;
74 },
75
76 /**
77 * Expected to be overwritten. Default returns true always.
Leo Reppd162b2e2021-06-30 13:51:07 +020078 * @returns whether the item is currently an option to be selected, or if it should just be skipped
79 */
80 isSelectable : function () {
81 return true;
82 },
83
84 /**
85 * API skeleton for reading letters. Expected to be overwritten.
86 * @param {String} letter The letter to be read
87 */
88 add : function (letter) {},
89
90
91 /**
92 * API skeleton for clearing whole contents. Expected to be overwritten.
93 */
94 clear : function () {},
95
96
97 /**
98 * API skeleton method for execution. Expected to be overwritten.
99 * @param {Event} event Event passed down by menu.
100 */
101 onclick : function (e) {},
102
103
104 /**
105 * API skeleton method for when backspace is pressed. Expected to be overwritten.
106 */
107 chop : function () {},
108
109 /**
110 * API skeleton method for pressing "right". Expected to be overwritten.
111 * @param {Event} event Event passed down by menu.
112 */
113 further : function (e) {},
114
115 /**
116 * Return menu list. This._menu gets written by the container class
117 */
118 menu : function () {
119 return this._menu;
120 }
121
Akronb7a005a2021-09-21 17:43:02 +0200122});