| 1 | /* |
| 2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
| 3 | |
| 4 | This program is free software; |
| 5 | you can redistribute it and/or modify it under the terms of the |
| 6 | GNU General Public License as published by the Free Software Foundation; |
| 7 | either version 3 of the License, or any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | /* |
| 19 | Changes: |
| 20 | - v4.0.0: |
| 21 | - created from v3.6.0 |
| 22 | Doc reviewed 202208 |
| 23 | */ |
| 24 | |
| 25 | import theHTMLSanitizer from '../../core/htmlSanitizer/HTMLSanitizer.js'; |
| 26 | import ObjId from '../../data/ObjId.js'; |
| 27 | import { NOT_FOUND, INVALID_OBJ_ID } from '../../main/Constants.js'; |
| 28 | |
| 29 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 30 | /** |
| 31 | This class is used to represent a branch of the dictionary tree. |
| 32 | */ |
| 33 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 34 | |
| 35 | class DictionaryItem { |
| 36 | |
| 37 | /** |
| 38 | The name displayed to the user |
| 39 | @type {String} |
| 40 | */ |
| 41 | |
| 42 | #name = ''; |
| 43 | |
| 44 | /** |
| 45 | A boolean indicating when the item is the root item. |
| 46 | @type {Boolean} |
| 47 | */ |
| 48 | |
| 49 | #isRoot = false; |
| 50 | |
| 51 | /** |
| 52 | An array with subitems if any |
| 53 | @type {Array.<DictionaryItem>} |
| 54 | */ |
| 55 | |
| 56 | #items = []; |
| 57 | |
| 58 | /** |
| 59 | The elements type used for the item |
| 60 | @type {Array.<string>} |
| 61 | */ |
| 62 | |
| 63 | #elementTypes = [ 'node', 'way', 'relation' ]; |
| 64 | |
| 65 | /** |
| 66 | An array of arrays of objects. This is used to filter the results received from osm. |
| 67 | Each sub array is a line in the TravelNotesSearchDictionary |
| 68 | Each object in a sub array is a cell in the TravelNotesSearchDictionary |
| 69 | @type {Array.<Array.<Object>>} |
| 70 | */ |
| 71 | |
| 72 | #filterTagsArray = []; |
| 73 | |
| 74 | /** |
| 75 | A boolean indicating when the item is selected by the user |
| 76 | @type {Boolean} |
| 77 | */ |
| 78 | |
| 79 | #isSelected = false; |
| 80 | |
| 81 | /** |
| 82 | A boolean indicating when the item is expanded by the user |
| 83 | @type {Boolean} |
| 84 | */ |
| 85 | |
| 86 | #isExpanded = false; |
| 87 | |
| 88 | /** |
| 89 | A unique identifier given to the DictionaryItem |
| 90 | @type {Number} |
| 91 | */ |
| 92 | |
| 93 | #objId = INVALID_OBJ_ID; |
| 94 | |
| 95 | /** |
| 96 | The constructor |
| 97 | @param {String} itemName The name of the item |
| 98 | @param {Boolean} isRoot True when the item is the root item ( = the dictionary ) |
| 99 | */ |
| 100 | |
| 101 | constructor ( itemName, isRoot ) { |
| 102 | |
| 103 | this.#name = theHTMLSanitizer.sanitizeToJsString ( itemName ); |
| 104 | if ( isRoot ) { |
| 105 | this.#isExpanded = true; |
| 106 | this.#isRoot = true; |
| 107 | } |
| 108 | |
| 109 | this.#objId = ObjId.nextObjId; |
| 110 | |
| 111 | Object.freeze ( this ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | The name displayed to the user |
| 116 | @type {String} |
| 117 | */ |
| 118 | |
| 119 | get name ( ) { return this.#name; } |
| 120 | |
| 121 | /** |
| 122 | A boolean indicating when the item is the root item. |
| 123 | @type {Boolean} |
| 124 | */ |
| 125 | |
| 126 | get isRoot ( ) { return this.#isRoot; } |
| 127 | |
| 128 | /** |
| 129 | An array with subitems if any |
| 130 | @type {Array.<DictionaryItem>} |
| 131 | */ |
| 132 | |
| 133 | get items ( ) { return this.#items; } |
| 134 | |
| 135 | /** |
| 136 | The element types used for the item |
| 137 | @type {Array.<string>} |
| 138 | */ |
| 139 | |
| 140 | get elementTypes ( ) { return this.#elementTypes; } |
| 141 | |
| 142 | /** |
| 143 | Change the value of elementTypes to only one value |
| 144 | @param {String} elementType The element type to set must be one of 'node', 'way' or 'relation' |
| 145 | */ |
| 146 | |
| 147 | setElementType ( elementType ) { |
| 148 | if ( NOT_FOUND !== [ 'node', 'way', 'relation' ].indexOf ( elementType ) ) { |
| 149 | this.#elementTypes = [ elementType ]; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | An array of arrays of objects. This is used to filter the results received from osm. |
| 155 | Each sub array is a line in the TravelNotesSearchDictionary |
| 156 | Each object in a sub array is a cell in the TravelNotesSearchDictionary |
| 157 | @type {Array.<Array.<Object>>} |
| 158 | */ |
| 159 | |
| 160 | get filterTagsArray ( ) { return this.#filterTagsArray; } |
| 161 | |
| 162 | /** |
| 163 | A boolean indicating when the item is selected by the user |
| 164 | @type {Boolean} |
| 165 | */ |
| 166 | |
| 167 | get isSelected ( ) { return this.#isSelected; } |
| 168 | |
| 169 | set isSelected ( isSelected ) { |
| 170 | this.#isSelected = isSelected; |
| 171 | this.items.forEach ( |
| 172 | item => { |
| 173 | item.isSelected = isSelected; |
| 174 | } |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | A boolean indicating when the item is expanded by the user |
| 180 | @type {Boolean} |
| 181 | */ |
| 182 | |
| 183 | get isExpanded ( ) { return this.#isExpanded; } |
| 184 | |
| 185 | set isExpanded ( isExpanded ) { this.#isExpanded = isExpanded; } |
| 186 | |
| 187 | /** |
| 188 | A unique identifier given to the DictionaryItem |
| 189 | @type {Number} |
| 190 | */ |
| 191 | |
| 192 | get objId ( ) { return this.#objId; } |
| 193 | } |
| 194 | |
| 195 | export default DictionaryItem; |
| 196 | |
| 197 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 198 |