123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- '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(['jquery', '../../lib/@waca/core-client/js/core-client/ui/core/Class'], function ($, Class) {
- 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, char) {
- text = text.replace(new RegExp('\\' + char, '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++;
- setTimeout(function () {
- this.frameRequest = window.requestAnimationFrame(this._update.bind(this));
- }.bind(this), timeBetweenUpdates);
- }
- },
- _getRandomChar: function _getRandomChar(oldChar) {
- if (oldChar === ' ') {
- return oldChar;
- } else if (oldChar >= '0' && oldChar <= '9') {
- return CHARS.number[Math.floor(Math.random() * CHARS.number.length)];
- } else if (oldChar >= 'A' && oldChar <= 'Z') {
- return CHARS.upper[Math.floor(Math.random() * CHARS.upper.length)];
- } else if (oldChar >= 'a' && oldChar <= 'z') {
- return CHARS.lower[Math.floor(Math.random() * CHARS.lower.length)];
- } else {
- return CHARS.other[Math.floor(Math.random() * CHARS.lower.length)];
- }
- }
- });
- return TextScrambler;
- });
- //# sourceMappingURL=TextScrambler.js.map
|