You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Commands.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* global APP */
  2. import UIUtil from '../../util/UIUtil';
  3. import UIEvents from '../../../../service/UI/UIEvents';
  4. /**
  5. * List with supported commands. The keys are the names of the commands and
  6. * the value is the function that processes the message.
  7. * @type {{String: function}}
  8. */
  9. const commands = {
  10. "topic" : processTopic
  11. };
  12. /**
  13. * Extracts the command from the message.
  14. * @param message the received message
  15. * @returns {string} the command
  16. */
  17. function getCommand(message) {
  18. if(message) {
  19. for(var command in commands) {
  20. if(message.indexOf("/" + command) === 0)
  21. return command;
  22. }
  23. }
  24. return "";
  25. }
  26. /**
  27. * Processes the data for topic command.
  28. * @param commandArguments the arguments of the topic command.
  29. */
  30. function processTopic(commandArguments, emitter) {
  31. var topic = UIUtil.escapeHtml(commandArguments);
  32. emitter.emit(UIEvents.SUBJECT_CHANGED, topic);
  33. }
  34. /**
  35. * Constructs a new CommandProccessor instance from a message that
  36. * handles commands received via chat messages.
  37. * @param message the message
  38. * @constructor
  39. */
  40. function CommandsProcessor(message, emitter) {
  41. var command = getCommand(message);
  42. this.emitter = emitter;
  43. /**
  44. * Returns the name of the command.
  45. * @returns {String} the command
  46. */
  47. this.getCommand = function() {
  48. return command;
  49. };
  50. var messageArgument = message.substr(command.length + 2);
  51. /**
  52. * Returns the arguments of the command.
  53. * @returns {string}
  54. */
  55. this.getArgument = function() {
  56. return messageArgument;
  57. };
  58. }
  59. /**
  60. * Checks whether this instance is valid command or not.
  61. * @returns {boolean}
  62. */
  63. CommandsProcessor.prototype.isCommand = function() {
  64. if (this.getCommand())
  65. return true;
  66. return false;
  67. };
  68. /**
  69. * Processes the command.
  70. */
  71. CommandsProcessor.prototype.processCommand = function() {
  72. if(!this.isCommand())
  73. return;
  74. commands[this.getCommand()](this.getArgument(), this.emitter);
  75. };
  76. export default CommandsProcessor;