TextScrambler.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', '../../lib/@waca/core-client/js/core-client/ui/core/Class'], function ($, Class) {
  8. var CHARS = {
  9. upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  10. lower: 'abcdefghijklmnopqrstuvwxyz',
  11. number: '0123456789',
  12. other: '!\'#$*&()%+=,-./:;<=>?@[]^_`{}|~'
  13. };
  14. // values in ms
  15. var timeBetweenUpdates = 25;
  16. var durationLowerBound = 485; // 500ms minus reasonable execution time of update function
  17. var numFramesUntilFullLength = 15;
  18. var TextScrambler = Class.extend({
  19. init: function init(options) {
  20. this.ignoreSet = options.ignoreSet;
  21. this.el = options.el;
  22. this.text = options.el.innerText;
  23. },
  24. scramble: function scramble() {
  25. var toScramble = this._applyIgnoreSet();
  26. this.queue = [];
  27. for (var i = 1; i <= toScramble.length; i++) {
  28. this.queue.push({
  29. char: toScramble[i - 1],
  30. start: Math.floor(i / this.text.length * numFramesUntilFullLength)
  31. });
  32. }
  33. this.frame = 0;
  34. this._update();
  35. },
  36. _applyIgnoreSet: function _applyIgnoreSet() {
  37. var text = this.text;
  38. if (this.ignoreSet) {
  39. $.each(this.ignoreSet, function (index, char) {
  40. text = text.replace(new RegExp('\\' + char, 'g'), '');
  41. });
  42. }
  43. return text;
  44. },
  45. _update: function _update(highResTimestamp) {
  46. if (!this.startTime && highResTimestamp) {
  47. this.startTime = highResTimestamp;
  48. }
  49. var output = '';
  50. var complete = false;
  51. $.each(this.queue, function (index, elem) {
  52. if (this.frame >= elem.start) {
  53. output += this._getRandomChar(elem.char);
  54. }
  55. }.bind(this));
  56. if (highResTimestamp - this.startTime > durationLowerBound) {
  57. output = this.text;
  58. complete = true;
  59. }
  60. this.el.innerHTML = output;
  61. if (!complete) {
  62. this.frame++;
  63. setTimeout(function () {
  64. this.frameRequest = window.requestAnimationFrame(this._update.bind(this));
  65. }.bind(this), timeBetweenUpdates);
  66. }
  67. },
  68. _getRandomChar: function _getRandomChar(oldChar) {
  69. if (oldChar === ' ') {
  70. return oldChar;
  71. } else if (oldChar >= '0' && oldChar <= '9') {
  72. return CHARS.number[Math.floor(Math.random() * CHARS.number.length)];
  73. } else if (oldChar >= 'A' && oldChar <= 'Z') {
  74. return CHARS.upper[Math.floor(Math.random() * CHARS.upper.length)];
  75. } else if (oldChar >= 'a' && oldChar <= 'z') {
  76. return CHARS.lower[Math.floor(Math.random() * CHARS.lower.length)];
  77. } else {
  78. return CHARS.other[Math.floor(Math.random() * CHARS.lower.length)];
  79. }
  80. }
  81. });
  82. return TextScrambler;
  83. });
  84. //# sourceMappingURL=TextScrambler.js.map