Message.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/App/Message", ["exports", "Site"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports, require("Site"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports, global.Site);
  11. global.AppMessage = mod.exports;
  12. }
  13. })(this, function (_exports, _Site2) {
  14. "use strict";
  15. Object.defineProperty(_exports, "__esModule", {
  16. value: true
  17. });
  18. _exports.run = run;
  19. _exports.getInstance = getInstance;
  20. _exports.default = _exports.AppMessage = void 0;
  21. _Site2 = babelHelpers.interopRequireDefault(_Site2);
  22. var ChatsWrap =
  23. /*#__PURE__*/
  24. function () {
  25. function ChatsWrap($el) {
  26. var _this = this;
  27. babelHelpers.classCallCheck(this, ChatsWrap);
  28. this.$el = $el;
  29. this.$historyBtn = $('#historyBtn');
  30. this.scrollChatsToBottom();
  31. $(window).on('resize', function () {
  32. _this.scrollChatsToBottom();
  33. });
  34. }
  35. babelHelpers.createClass(ChatsWrap, [{
  36. key: "scrollChatsToBottom",
  37. value: function scrollChatsToBottom() {
  38. var $el = this.$el;
  39. var chatsWrapH = $el.height();
  40. var chatsH = $('.chats', $el).outerHeight();
  41. var historyBtnH = this.$historyBtn.outerHeight();
  42. $el.scrollTop(chatsH + historyBtnH - chatsWrapH);
  43. }
  44. }]);
  45. return ChatsWrap;
  46. }();
  47. var AppMessage =
  48. /*#__PURE__*/
  49. function (_Site) {
  50. babelHelpers.inherits(AppMessage, _Site);
  51. function AppMessage() {
  52. babelHelpers.classCallCheck(this, AppMessage);
  53. return babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(AppMessage).apply(this, arguments));
  54. }
  55. babelHelpers.createClass(AppMessage, [{
  56. key: "initialize",
  57. value: function initialize() {
  58. babelHelpers.get(babelHelpers.getPrototypeOf(AppMessage.prototype), "initialize", this).call(this);
  59. this.newChatLists = [];
  60. this.$chatsWrap = $('.app-message-chats');
  61. this.chatApi = new ChatsWrap(this.$chatsWrap);
  62. this.$textArea = $('.message-input textarea');
  63. this.$textareaWrap = $('.app-message-input');
  64. this.$msgEdit = $('.message-input>.form-control');
  65. this.$sendBtn = $('.message-input-btn'); // states
  66. this.states = {
  67. chatListsLength: 0
  68. };
  69. }
  70. }, {
  71. key: "process",
  72. value: function process() {
  73. babelHelpers.get(babelHelpers.getPrototypeOf(AppMessage.prototype), "process", this).call(this);
  74. this.steupMessage();
  75. this.setupTextarea();
  76. }
  77. }, {
  78. key: "chatListsLength",
  79. value: function chatListsLength(length) {
  80. if (this.newChatLists[length - 1]) {
  81. var $newMsg = $("<div class='chat-content'><p>".concat(this.newChatLists[length - 1], "</p></div>"));
  82. $('.chat').last().find('.chat-body').append($newMsg);
  83. this.$msgEdit.attr('placeholder', '');
  84. this.$msgEdit.val('');
  85. } else {
  86. this.$msgEdit.attr('placeholder', 'type text here...');
  87. }
  88. this.chatApi.scrollChatsToBottom();
  89. this.states.chatListsLength = length;
  90. }
  91. }, {
  92. key: "setupTextarea",
  93. value: function setupTextarea() {
  94. var _this2 = this;
  95. autosize($('.message-input textarea'));
  96. this.$textArea.on('autosize:resized', function () {
  97. _this2.$chatsWrap.css('height', "calc(100% - ".concat(_this2.$textareaWrap.outerHeight(), "px)"));
  98. _this2.triggerResize();
  99. });
  100. }
  101. }, {
  102. key: "steupMessage",
  103. value: function steupMessage() {
  104. var _this3 = this;
  105. this.$sendBtn.on('click', function () {
  106. var num = _this3.states.chatListsLength;
  107. _this3.newChatLists.push(_this3.getMsg());
  108. _this3.chatListsLength(++num);
  109. });
  110. }
  111. }, {
  112. key: "getMsg",
  113. value: function getMsg() {
  114. return this.$msgEdit.val();
  115. }
  116. }]);
  117. return AppMessage;
  118. }(_Site2.default);
  119. _exports.AppMessage = AppMessage;
  120. var instance = null;
  121. function getInstance() {
  122. if (!instance) {
  123. instance = new AppMessage();
  124. }
  125. return instance;
  126. }
  127. function run() {
  128. var app = getInstance();
  129. app.run();
  130. }
  131. var _default = AppMessage;
  132. _exports.default = _default;
  133. });