diff options
author | David Lu <david.lu@hotmail.com> | 2016-06-06 10:01:35 -0700 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2016-06-06 13:01:35 -0400 |
commit | 2c42294bbcab3cd5cfdce9604e5872fe4a12e538 (patch) | |
tree | 60b20100acbd0efd6ed6293d88b222ef3f904088 /webapp/stores/message_history_store.jsx | |
parent | 96e8fc165fab315b48f55e9ca54e689b34119967 (diff) | |
download | chat-2c42294bbcab3cd5cfdce9604e5872fe4a12e538.tar.gz chat-2c42294bbcab3cd5cfdce9604e5872fe4a12e538.tar.bz2 chat-2c42294bbcab3cd5cfdce9604e5872fe4a12e538.zip |
PLT-3101 Added message history (#3205)
* Added message history
* Minor logical changes
* Fixed indexes resetting
* Fixed double messages
* Fixed resetting main history when RHS opened
Diffstat (limited to 'webapp/stores/message_history_store.jsx')
-rw-r--r-- | webapp/stores/message_history_store.jsx | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/webapp/stores/message_history_store.jsx b/webapp/stores/message_history_store.jsx new file mode 100644 index 000000000..51f673d9e --- /dev/null +++ b/webapp/stores/message_history_store.jsx @@ -0,0 +1,79 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import Constants from 'utils/constants.jsx'; + +const TYPE_POST = 'post'; +const TYPE_COMMENT = 'comment'; + +class MessageHistoryStoreClass { + constructor() { + this.messageHistory = []; + this.index = []; + this.index[TYPE_POST] = 0; + this.index[TYPE_COMMENT] = 0; + } + + getMessageInHistory(type) { + if (this.index[type] >= this.messageHistory.length) { + return ''; + } else if (this.index[type] < 0) { + return null; + } + + return this.messageHistory[this.index[type]]; + } + + getHistoryLength() { + if (this.messageHistory === null) { + return 0; + } + return this.messageHistory.length; + } + + storeMessageInHistory(message) { + this.messageHistory.push(message); + this.resetAllHistoryIndex(); + if (this.messageHistory.length > Constants.MAX_PREV_MSGS) { + this.messageHistory = this.messageHistory.slice(1, Constants.MAX_PREV_MSGS + 1); + } + } + + storeMessageInHistoryByIndex(index, message) { + this.messageHistory[index] = message; + } + + resetAllHistoryIndex() { + this.index[TYPE_POST] = this.messageHistory.length; + this.index[TYPE_COMMENT] = this.messageHistory.length; + } + + resetHistoryIndex(type) { + this.index[type] = this.messageHistory.length; + } + + nextMessageInHistory(keyCode, messageText, type) { + if (messageText !== '' && messageText !== this.getMessageInHistory(type)) { + return null; + } + + if (keyCode === Constants.KeyCodes.UP) { + this.index[type]--; + } else if (keyCode === Constants.KeyCodes.DOWN) { + this.index[type]++; + } + + if (this.index[type] < 0) { + this.index[type] = 0; + return null; + } else if (this.index[type] >= this.getHistoryLength()) { + this.index[type] = this.getHistoryLength(); + } + + return this.getMessageInHistory(type); + } +} + +var MessageHistoryStore = new MessageHistoryStoreClass(); + +export default MessageHistoryStore;
\ No newline at end of file |