Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
| 11 | define({ |
| 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 Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 21 | * 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 | |
Leo Repp | 8453916 | 2021-10-25 12:06:07 +0200 | [diff] [blame^] | 32 | /** |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 33 | * Get/create the document element of the container item. Can be overwritten. Standard class: li |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 34 | * If you wish to change the textNode please overwrite the content function instead. |
Leo Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 35 | */ |
| 36 | element : function () { |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 37 | //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 Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 39 | 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 Repp | 8453916 | 2021-10-25 12:06:07 +0200 | [diff] [blame^] | 52 | * Set the items text content to "content". If content is left blank it gets set to this.defaultTextValue, |
| 53 | * or the empty string if a default value does not exists. Also decides how the text is represented. |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 54 | * @param {String} content String to which to set the text |
| 55 | * @returns textNode with content or undefined |
| 56 | */ |
Leo Repp | 8453916 | 2021-10-25 12:06:07 +0200 | [diff] [blame^] | 57 | initContent : function (content) { |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 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 Repp | d162b2e | 2021-06-30 13:51:07 +0200 | [diff] [blame] | 78 | * @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 | |
Akron | b7a005a | 2021-09-21 17:43:02 +0200 | [diff] [blame] | 122 | }); |