TextScrambler.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(['../lib/@waca/core-client/js/core-client/ui/core/Class', 'jquery'], function (Class, $) {
  8. 'use strict';
  9. var CHARS = {
  10. upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  11. lower: 'abcdefghijklmnopqrstuvwxyz',
  12. number: '0123456789',
  13. other: '!\'#$*&()%+=,-./:;<=>?@[]^_`{}|~'
  14. };
  15. // values in ms
  16. var timeBetweenUpdates = 25;
  17. var durationLowerBound = 485; // 500ms minus reasonable execution time of update function
  18. var numFramesUntilFullLength = 15;
  19. var TextScrambler = Class.extend({
  20. init: function init(options) {
  21. this.ignoreSet = options.ignoreSet;
  22. this.el = options.el;
  23. this.text = options.el.innerText;
  24. },
  25. scramble: function scramble() {
  26. var toScramble = this._applyIgnoreSet();
  27. this.queue = [];
  28. for (var i = 1; i <= toScramble.length; i++) {
  29. this.queue.push({
  30. 'char': toScramble[i - 1],
  31. start: Math.floor(i / this.text.length * numFramesUntilFullLength)
  32. });
  33. }
  34. this.frame = 0;
  35. this._update();
  36. },
  37. _applyIgnoreSet: function _applyIgnoreSet() {
  38. var text = this.text;
  39. if (this.ignoreSet) {
  40. $.each(this.ignoreSet, function (index, ch) {
  41. text = text.replace(new RegExp('\\' + ch, 'g'), '');
  42. });
  43. }
  44. return text;
  45. },
  46. _update: function _update(highResTimestamp) {
  47. if (!this.startTime && highResTimestamp) {
  48. this.startTime = highResTimestamp;
  49. }
  50. var output = '';
  51. var complete = false;
  52. $.each(this.queue, function (index, elem) {
  53. if (this.frame >= elem.start) {
  54. output += this._getRandomChar(elem['char']);
  55. }
  56. }.bind(this));
  57. if (highResTimestamp - this.startTime > durationLowerBound) {
  58. output = this.text;
  59. complete = true;
  60. }
  61. this.el.innerHTML = output;
  62. if (!complete) {
  63. this.frame++;
  64. this._setTimeout(function () {
  65. this.frameRequest = this._requestAnimationFrame(this._update.bind(this));
  66. }.bind(this), timeBetweenUpdates);
  67. }
  68. },
  69. _requestAnimationFrame: function _requestAnimationFrame(func) {
  70. return window.requestAnimationFrame(func);
  71. },
  72. _getRandomChar: function _getRandomChar(oldChar) {
  73. if (oldChar === ' ') {
  74. return oldChar;
  75. } else if (oldChar >= '0' && oldChar <= '9') {
  76. return CHARS.number[Math.floor(this._random() * CHARS.number.length)];
  77. } else if (oldChar >= 'A' && oldChar <= 'Z') {
  78. return CHARS.upper[Math.floor(this._random() * CHARS.upper.length)];
  79. } else if (oldChar >= 'a' && oldChar <= 'z') {
  80. return CHARS.lower[Math.floor(this._random() * CHARS.lower.length)];
  81. } else {
  82. return CHARS.other[Math.floor(this._random() * CHARS.lower.length)];
  83. }
  84. },
  85. _random: function _random() {
  86. return Math.random();
  87. },
  88. _setTimeout: function _setTimeout(func, timeout) {
  89. return window.setTimeout(func, timeout);
  90. }
  91. });
  92. return TextScrambler;
  93. });
  94. //# sourceMappingURL=TextScrambler.js.map