Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. /**
  2. * Temporary page-specific state of text and all its words.
  3. *
  4. * @constructor
  5. * @param {boolean} failOnDuplicatedDOMElements
  6. * TODO: Remove failOnDuplicatedDOMElements when creating DOM elements for each word is done.
  7. *
  8. */
  9. ReadingState = function(failOnDuplicatedDOMElements) {
  10. /** @private {!Map<!Element, string>} */
  11. this.words_ = new Map();
  12. /** @private {!Map<string, !WordUpdates>} */
  13. this.wordsUpdates_ = new Map();
  14. // TODO: Extend this to maintain/lookup context (within reading-state) of each word.
  15. this.failOnDuplicatedDOMElements_ = failOnDuplicatedDOMElements
  16. };
  17.  
  18. /**
  19. * Adds the given (sequential) word to the reading state and associates it with the provided
  20. * DOM Element.
  21. *
  22. * @param {!WordKey} wordKey
  23. * @param {!Element} domElement
  24. * @param {?WordContext} context
  25. */
  26. ReadingState.prototype.addWord = function(wordKey, domElement, context) {
  27. if (this.failOnDuplicatedDOMElements_ && this.words_.has(domElement)) {
  28. var message = 'DOMElement is already registered in ReadingState';
  29. console.error(message, domElement, wordKey);
  30. throw Error(message);
  31. }
  32. var keyStr = wordKey.valueOf();
  33. this.words_.set(domElement, keyStr);
  34. var updates = this.wordsUpdates_.get(keyStr);
  35. if (!updates) {
  36. updates = new WordUpdates(wordKey);
  37. // We don't know the status.
  38. updates.status = WordStatus.NONE;
  39. this.wordsUpdates_.set(keyStr, updates);
  40. }
  41. if (context) {
  42. updates.new_contexts.push(context);
  43. // Defence to not blow up the reading-state.
  44. if (updates.new_contexts.length > Consts.MAX_CONTEXTS_PER_WORD) {
  45. updates.new_contexts.shift();
  46. }
  47. }
  48. };
  49.  
  50. /**
  51. * Updates the status of the given word key.
  52. *
  53. * @param {!WordKey} wordKey
  54. * @param {!WordStatus} status
  55. */
  56. ReadingState.prototype.updateWordStatus = function(wordKey, status) {
  57. var updates = this.wordsUpdates_.get(wordKey.valueOf());
  58. if (!updates) {
  59. updates = new WordUpdates(wordKey);
  60. this.wordsUpdates_.set(wordKey.valueOf(), updates);
  61. }
  62. updates.status = status;
  63. };
  64.  
  65. /**
  66. * Return the status of the given word key.
  67. * @param {!WordKey} wordKey
  68. * @return {!WordStatus}
  69. */
  70. ReadingState.prototype.getWordUpdates = function(wordKey) {
  71. return this.wordsUpdates_.get(wordKey.valueOf());
  72. };
  73.  
  74. /**
  75. * Returns all the words it holds.
  76. *
  77. * @return {!Array<string>} each element is a string representation of a WordKey.
  78. */
  79. ReadingState.prototype.getWordsKeyStrs = function() {
  80. var result = [];
  81. for (var word of this.wordsUpdates_.keys()) {
  82. result.push(word);
  83. }
  84. return result;
  85. };
  86.  
  87. /**
  88. * Updates words in reading state with the provided mapping to statuses.
  89. *
  90. * @param {!Object<string, !WordStatus>} wordKeyStrToStatus - word is string repr of WordKey.
  91. */
  92. ReadingState.prototype.setWordsStatuses = function(wordKeyStrToStatus) {
  93. for (var wordKeyStr of Object.keys(wordKeyStrToStatus)) {
  94. this.updateWordStatus(WordKey.parse(wordKeyStr), wordKeyStrToStatus[wordKeyStr]);
  95. }
  96. };
  97.  
  98. /**
  99. * Returns stats of how many words we have per status.
  100. *
  101. * @return {!Map<!WordStatus, number>}
  102. */
  103. ReadingState.prototype.getWordStats = function() {
  104. var result = new Map();
  105. for (var update of this.wordsUpdates_.values()) {
  106. var status = update.status != null ? update.status : WordStatus.UNKNOWN;
  107. result.set(status, (result.get(status) || 0) + 1);
  108. }
  109. return result;
  110. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement