'use strict'; /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../lib/@waca/core-client/js/core-client/ui/core/Class', 'jquery'], function (Class, $) { 'use strict'; var CHARS = { upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', lower: 'abcdefghijklmnopqrstuvwxyz', number: '0123456789', other: '!\'#$*&()%+=,-./:;<=>?@[]^_`{}|~' }; // values in ms var timeBetweenUpdates = 25; var durationLowerBound = 485; // 500ms minus reasonable execution time of update function var numFramesUntilFullLength = 15; var TextScrambler = Class.extend({ init: function init(options) { this.ignoreSet = options.ignoreSet; this.el = options.el; this.text = options.el.innerText; }, scramble: function scramble() { var toScramble = this._applyIgnoreSet(); this.queue = []; for (var i = 1; i <= toScramble.length; i++) { this.queue.push({ 'char': toScramble[i - 1], start: Math.floor(i / this.text.length * numFramesUntilFullLength) }); } this.frame = 0; this._update(); }, _applyIgnoreSet: function _applyIgnoreSet() { var text = this.text; if (this.ignoreSet) { $.each(this.ignoreSet, function (index, ch) { text = text.replace(new RegExp('\\' + ch, 'g'), ''); }); } return text; }, _update: function _update(highResTimestamp) { if (!this.startTime && highResTimestamp) { this.startTime = highResTimestamp; } var output = ''; var complete = false; $.each(this.queue, function (index, elem) { if (this.frame >= elem.start) { output += this._getRandomChar(elem['char']); } }.bind(this)); if (highResTimestamp - this.startTime > durationLowerBound) { output = this.text; complete = true; } this.el.innerHTML = output; if (!complete) { this.frame++; this._setTimeout(function () { this.frameRequest = this._requestAnimationFrame(this._update.bind(this)); }.bind(this), timeBetweenUpdates); } }, _requestAnimationFrame: function _requestAnimationFrame(func) { return window.requestAnimationFrame(func); }, _getRandomChar: function _getRandomChar(oldChar) { if (oldChar === ' ') { return oldChar; } else if (oldChar >= '0' && oldChar <= '9') { return CHARS.number[Math.floor(this._random() * CHARS.number.length)]; } else if (oldChar >= 'A' && oldChar <= 'Z') { return CHARS.upper[Math.floor(this._random() * CHARS.upper.length)]; } else if (oldChar >= 'a' && oldChar <= 'z') { return CHARS.lower[Math.floor(this._random() * CHARS.lower.length)]; } else { return CHARS.other[Math.floor(this._random() * CHARS.lower.length)]; } }, _random: function _random() { return Math.random(); }, _setTimeout: function _setTimeout(func, timeout) { return window.setTimeout(func, timeout); } }); return TextScrambler; }); //# sourceMappingURL=TextScrambler.js.map