Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. "use strict";
  2.  
  3. var LETTER_TIMEOUT = 35, PROMPT_TIMEOUT = 300;
  4.  
  5. var
  6. bubble = document.getElementById("bubble");
  7.  
  8. var config = {
  9. init: {
  10. text:"Hai! Ask me questions!"
  11. }
  12. };
  13.  
  14. function Bubble(elem) {
  15. this.elem = elem;
  16. this.mode = "text";
  17. this.text = "";
  18. this.options = [];
  19. this.callback = null;
  20. this.timeout = null;
  21. this.offset = 0;
  22. }
  23. Bubble.prototype.displayText = function(text, cb) {
  24. this.mode = "text";
  25. this.text = text;
  26. this.callback = cb;
  27. this.render();
  28. };
  29. Bubble.prototype.displayOptions = function(options, cb) {
  30. this.mode = "options";
  31. this.options = options;
  32. this.callback = cb;
  33. this.render();
  34. };
  35. Bubble.prototype.render = function() {
  36. var self = this;
  37. clearTimeout(this.timeout);
  38. this.elem.innerHTML = "";
  39. if(this.mode === "options") {
  40. var cont = document.createElement("div");
  41. cont.className = "bubble_options";
  42. this.options.forEach(function(opt) {
  43. var elem = document.createElement("div");
  44. elem.className = "bubble_option";
  45. elem.textContent = opt.label;
  46. elem.addEventListener("click", function(e) {
  47. self.elem.innerHTML = "";
  48. setTimeout(function() {
  49. self.callback(opt);
  50. }, PROMPT_TIMEOUT);
  51. });
  52. cont.appendChild(elem);
  53. });
  54. this.elem.appendChild(cont);
  55. }
  56. else if(this.mode === "text") {
  57. self.offset = 0;
  58. var looper = function() {
  59. self.offset++;
  60. self.elem.textContent += self.text[self.offset - 1];
  61. if(self.offset < self.text.length) {
  62. self.timeout = setTimeout(looper, LETTER_TIMEOUT);
  63. }
  64. else {
  65. setTimeout(function() {
  66. self.callback();
  67. }, PROMPT_TIMEOUT);
  68. }
  69. };
  70. this.timeout = setTimeout(looper, LETTER_TIMEOUT);
  71. }
  72. };
  73.  
  74. var myBubble = new Bubble(bubble);
  75.  
  76. function main() {
  77. var entry = config[globalState];
  78. myBubble.displayText(entry.text, function() {
  79. yourBubble.displayOptions(entry.options, function(opt) {
  80. yourBubble.displayText(opt.label, function() {
  81. globalState = opt.state;
  82. main();
  83. });
  84. });
  85. });
  86. }
  87. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement