1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | 370× 370× 370× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 351× 19× 9× 6061× 2417× 5373× 5373× 5373× 5382× 5382× 5382× 5382× 5382× 5382× 1084× 5382× 5382× 679× 670× 670× 670× 670× 659× 240× 659× 670× 670× 679× 2× 2× 677× 688× 23× 11× 2× 2× 2× 2× 2× 2× 9× 11× 5382× 670× 162× 864× 5364× 162× 9× 9× 9× 9× 9× 144× 4777× 4777× 144× 144× 153× 153× | import Validate from './helper/Validation.class'; /** * @fileOverview Randrix Class. * @author Simon Gattner <npm@0x38.de> * @license MIT * @version 1.0.2 * @module {es6} Randrix */ export default class Randrix { /** * Display a random matrix panel. * @class Randrix * @classdesc Class to display a Randrix. * @param {object} options The Randrix options. * @param {string} options.message The Randrix message to display. * @param {string} options.selector The Randrix element selector. * Default is '[data-randrix]'. * @param {number} options.width The Randrix width to display. * Default is 5. * @param {number} options.height The Randrix height to display. * Default is 6. * @param {number} options.interval The Randrix interval to display. * Default is 100. * @param {string} options.possible The Randrix possible Chars to display. * Default is 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.'. * @param {array} options.style The Randrix random Char styles to display. * Default is undefined. * @param {function} options.callback The Randrix callback to * run after stop. Default is null. */ constructor(options) { this._defaults = { message: undefined, selector: '[data-randrix]', width: 5, height: 6, interval: 100, possible: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', style: undefined, callback: null, }; this._options = Validate.options(options); if (this._options.error) return this.throwError(); // ES2018 this._settings = {...this._defaults, ...this._options}; this._settings = Object.assign({}, this._defaults, this._options); this._message = this._settings.message.split(''); this._element = document.querySelector(this._settings.selector); this._width = this._settings.width; this._height = this._settings.height; this._interval = this._settings.interval; this._possible = this._settings.possible; this._possibleLength = this._possible.length; this._style = this._settings.style; this._styleLength = (this._style) ? this._style.length : 0; this._styleEnable = this._styleLength > 0; this._index = 0; this._matrix = []; this._x = 0; this._y = 0; this._callback = this._settings.callback; this._calledback = false; } /** * Throw Errors if Randrix has invalid options. * @function Randrix.throwErrors */ throwError() { throw new Error(this._options.error); } /** * Is there a callback and not executed yet? * @function Randrix.isCallback * @return {boolean} is there a callback or not and is it executed or not. */ isCallback() { return typeof this._callback === 'function' && this._calledback === false; } /** * Random Char from Possible Chars * @function Randrix.char * @return {string} the Char. */ char() { return this._possible.charAt( Math.floor( Math.random() * this._possibleLength ) ); } /** * Style the Char from Possible Styles * @function Randrix.charStyle * @return {string} the Char Style. */ charStyle() { return (this._style) ? this._style[ Math.floor(Math.random() * this._styleLength) ] : ''; } /** * Create a Char on the Randrix * @function Randrix.charCreate * @param {number} x position of the Char. * @param {number} y position of the Char. * @return {boolean} is the Char created. */ charCreate(x, y) { const id = `x${x}-y${y}`; this._matrix[id] = { element: this.charCreateElement(id) || {}, x: x, y: y, interval: this.charStart(id) || (() => false), }; return true; } /** * Create the Char Element on the Randrix * @function Randrix.charCreateElement * @param {object} id of the Char. * @return {object} element of the Char. */ charCreateElement(id) { const char = this.char(); const element = document.createElement('span'); const text = document.createTextNode(char); element.appendChild(text); element.setAttribute('data-randrix-char', id); if (this._styleEnable && this.charStyle()) { element.setAttribute('data-randrix-style', this.charStyle()); } this._element.appendChild(element); return element; } /** * Char is the Last Child on the Randrix * @function Randrix.charIsLastChild * @param {string} id of the Char. * @return {boolean} Char is the last child or not. */ charIsLastChild(id) { return this._x === this._width - 1 && this._y === this._height - 1; } /** * Update the Char on the Randrix * @function Randrix.charUpdate * @param {string} id of the Char. */ charUpdate(id) { const char = this.char(); this.charUpdateElement(id, char); this.charUpdateStatus(id, char); } /** * Update the Char on the Randrix * @function Randrix.charUpdateElement * @param {string} id of the Char. * @param {string} char to update. */ charUpdateElement(id, char) { if (this._matrix[id] && this._matrix[id].element) { if (this._styleEnable) { this._matrix[id].element .setAttribute('data-randrix-style', this.charStyle()); } this._matrix[id].element.innerHTML = char; } } /** * Update the Char on the Randrix * @function Randrix.charUpdateStatus * @param {string} id of the Char. * @param {string} char to update. */ charUpdateStatus(id, char) { Eif (this._message.length !== this._index) { (this.charIsLastChild()) ? this.reset() : (!this.charMatch(id, char)) || this.charActivate(id); } else { this.charStop(id); if (this.isCallback()) this._calledback = true && this._callback(); } } /** * Match the Char on the Randrix * @function Randrix.charMatch * @param {object} id of the Char. * @param {string} char to match. * @return {boolean} char has matched or not. */ charMatch(id, char) { if (this.charMatchIndex(char) && this.charMatchPosition(id)) { Iif (this._debug) { // eslint-disable-next-line no-console console.log( 'next', 'x' + this._x, 'y' + this._y, 'c' + char ); } return true; } else { return false; } } /** * Match the Char Position on the Randrix * @function Randrix.charMatchIndex * @param {string} char to match. * @return {boolean} char has index matched or not. */ charMatchIndex(char) { return this._message[this._index] === char; } /** * Match the Char Position on the Randrix * @function Randrix.charMatchPosition * @param {object} id of the Char. * @return {boolean} char has position matched or not. */ charMatchPosition(id) { return (this._matrix[id]) ? // start in the first lines (this._index === 0 && this._matrix[id].x === 0) || // forward in the current line (this._x === this._matrix[id].x && this._y + 1 === this._matrix[id].y) || // forward to the next line (this._x + 1 === this._matrix[id].x && this._matrix[id].y === 0) : false; } /** * Activate the Char on the Randrix * @function Randrix.charActivate * @param {string} id of the Char. * @return {boolean} is the Char successful activated. */ charActivate(id) { if (this._matrix[id]) { this._index = this._index + 1; this._x = this._matrix[id].x; this._y = this._matrix[id].y; this.charStop(id); this._matrix[id].element .setAttribute('data-randrix-char-active', 'true'); return true; } else { return false; } } /** * Stop the Char on the Randrix * @function Randrix.charStop * @param {string} id of the Char. * @return {function} clearInterval function of the Char. */ charStop(id) { return (this._matrix[id]) ? clearInterval(this._matrix[id].interval) : () => false; } /** * Start the Char on the Randrix * @function Randrix.charStart * @param {string} id of the Char. * @return {function} setInterval function of the Char. */ charStart(id) { return setInterval(() => { this.charUpdate(id); }, this._interval); } /** * Create the Randrix * @function Randrix.create * @return {boolean} done. */ create() { for (let x = 0; x < this._width; x++) { for (let y = 0; y < this._height; y++) { this.charCreate(x, y); } } return true; } /** * Reset the Randrix * @function Randrix.reset * @return {boolean} done. */ reset() { this._element.innerHTML = ''; this.stop( () => { this.constructor(this._settings); this.start(); } ); return true; } /** * Stop the Randrix * @function Randrix.stop * @param {function} callback to execute. * @return {boolean} done. */ stop(callback) { for (const item in this._matrix) { Eif (this._matrix.hasOwnProperty(item)) { clearInterval(this._matrix[item].interval); } } if (typeof callback === 'function') callback(); return true; } /** * Start the Randrix * @function Randrix.start * @return {boolean} done. */ start() { (this._index === 0) ? this.create() : this.reset(); return true; } } |