chat-room-message.js 9.74 KB
Newer Older
Kim Peace committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const userMessageTemplate = getTemplate(TemplateURL.USER_MESSAGE);
const myMessageTemplate = getTemplate(TemplateURL.MY_MESSAGE);
const systemMessageTemplate = getTemplate(TemplateURL.SYSTEM_MESSAGE);
const collaborationMessageTemplate = getTemplate(
  TemplateURL.OPEN_COLLABORATION_MESSAGE
);
/***********************
 *  Search Messages
 ***********************/
ChatRoom.appendSearchMessages = function (messages, unwrappedWorkVal = "") {
  let workVal = unwrappedWorkVal;
  messages.forEach(function (message) {
    let html = ChatRoom.renderMessageForSearchMessage(message);
    workVal = html + workVal;
  });
16
  $(".overlay_src_msg").append(workVal);
Kim Peace committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
};

ChatRoom.renderMessageForSearchMessage = function (message) {
  let template = ChatRoom.getChatMessageTemplate(
    message.messageType,
    message.shopMemberId
  );

  let messageTime = CHAT_UTIL.formatDate(message.insertDate);

  message.profileUrl = ChatRoom.parseUserProfileURL(message.profileUrl);

  message.message = message.message
    .toString()
    .replaceAll("?fileName=", "?sid=" + currentUserInfo.sid + "&fileName=");

  let html = Mustache.render(template, {
    text: message.message,
    from: message.loginId,
    shopMemberId: message.shopMemberId,
    profileImage: message.profileUrl,
    createdAtDay: messageTime.createdAtDay,
    createdAtTime: messageTime.createdAtTime,
  });

  return message.message.includes("attachedImages") ||
    message.message.includes("attachedVideos")
    ? CHAT_UTIL.htmlDecode(html)
    : html;
};

/***********************
 *  Send Messages
 ***********************/
51 52 53 54 55 56 57 58
// Input recognize
$("#messageInput").on("keypress", function (event) {
  if (event.which == 13) {
    // Enterキーの処理
    $("#messageSend").click();
  }
});

Kim Peace committed
59 60 61 62 63 64 65 66 67
ChatRoom.sendMessage = function (e) {
  const messageTextBox = $("#messageInput");
  const message =
    messageTextBox.val().length > 0
      ? encodeURIComponent(messageTextBox.val())
      : "";
  messageTextBox.val("");

  if (message.length > 0) {
68 69
    const sendData = message;
    CHAT_SOCKET.emitCreateText(sendData);
Kim Peace committed
70 71 72 73 74 75 76 77
  }
  $(".message_input_form").focus();
};

/***********************
 *  Load Messages
 ***********************/
ChatRoom.loadMessages = function (joinRoomID, joinRoomName) {
78
  let roomID = joinRoomID.toString().replace(/['"]+/g, "");
Kim Peace committed
79 80
  roomName = joinRoomName;
  // 画面更新
81
  if (serverInfo.isOnline == true) {
Kim Peace committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    Common.refreshForOnline();
    NativeBridgeDelegate.updateMessages(roomID);
  } else {
    Common.refreshForOffline();
  }

  // setup ルームタイトル
  $("#roomTitle").text(roomName).data("roomName", roomName);

  // ルームメニュー表示設定
  ChatRoom.configureRoomMenu();

  // ユーザー表示
  ChatRoom.appendAttendedUsers(roomID);

  // メッセージ表示
Kim Peace committed
98 99
  let messages = NativeBridgeDataSource.getMessagesByRoomID(roomID);
  ChatRoom.prependMessage(messages);
Kim Peace committed
100

101 102
  CHAT_SOCKET.cleanUpCollaborationMessage(true);

103 104 105 106 107 108 109
  window.addEventListener(
    "load",
    function (event) {
      ChatRoom.scrollToBottom(false);
    },
    false
  );
Kim Peace committed
110 111 112
};

ChatRoom.configureRoomMenu = function () {
113
  if (roomInfo.roomType == ChatRoomType.DM) {
Kim Peace committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    $("#roomMenu").removeClass("none");
  } else {
    $("#roomMenu").addClass("none");
  }
};

ChatRoom.appendAttendedUsers = function (roomID) {
  const topUserListTemplate = getTemplate(TemplateURL.CHATROOM_USER_LIST);
  const filterUserListTemplate = getTemplate(
    TemplateURL.CHATROOM_USER_FILTER_LIST
  );

  const users = NativeBridgeDataSource.getUsersInRoom(roomID);

  // ユーザーリスト表示
  users.forEach(function (user) {
    user.profileUrl = Common.getProfileImgUrl(user.profileUrl);
  });
  let html = Mustache.render(topUserListTemplate, {
    userList: users,
  });
  let obj = jQuery.parseHTML(html);
  $("#user_list").append(obj);

  // フィルタリングユーザーリスト表示
  let filterHtml = Mustache.render(filterUserListTemplate, {
    userList: users,
  });
  let filterObj = jQuery.parseHTML(filterHtml);
  $("#filter").append(filterObj);
};

Kim Peace committed
146
ChatRoom.prependMessage = function (messages) {
Kim Peace committed
147 148
  const now = new Date();

149
  const messageElement = $("#messages");
150 151
  let checkBeforeDate = "";
  let beforeDate = "";
Kim Peace committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  messages.forEach(function (message) {
    // プロフィールURL設定
    message.profileUrl = ChatRoom.parseUserProfileURL(message.profileUrl);

    const messageCreatedTime = CHAT_UTIL.formatDate(
      message.insertDate
    ).createdAt;

    // #36147
    message.message = message.message
      .toString()
      .replaceAll("?fileName=", "?sid=" + currentUserInfo.sid + "&fileName=");

    // 日付表示のため、今年なのか、本日なのかを判定
    const isOtherYear = now.getFullYear() != message.insertDate.substring(0, 4);
    const isToday = messageCreatedTime.includes(":") ? true : false;

    // 日付ラーベル表示
170
    if (
171 172 173
      messageCreatedTime != checkBeforeDate &&
      checkBeforeDate != "" &&
      !isToday
174
    ) {
175
      const html = ChatRoom.renderDateLabelMessage(beforeDate);
176
      messageElement.append(html);
Kim Peace committed
177 178 179 180 181 182
    }

    checkBeforeDate = messageCreatedTime;
    beforeDate = message.insertDate;

    const html = ChatRoom.renderMessage(message, isToday, isOtherYear);
183
    messageElement.append(html);
Kim Peace committed
184 185
  });

186 187 188 189 190 191 192 193 194 195 196
  // messageが存在する場合、最上段にそのメッセージの日付を表示
  ChatRoom.appendInitialDateMessage(messages);
};

ChatRoom.appendInitialDateMessage = function (messages) {
  if ($(".chat_message").length != 0 && messages.length != 0) {
    $("#initial-date-message").detach();
    const html = ChatRoom.renderDateLabelMessage(
      messages[messages.length - 1].insertDate,
      true
    );
197
    $("#messages").append(html);
198
  }
Kim Peace committed
199 200
};

201 202 203 204
ChatRoom.renderDateLabelMessage = function (
  beforeDate,
  isInitialDateMessage = false
) {
Kim Peace committed
205
  let messageDay = CHAT_UTIL.systemDay(beforeDate);
206
  return Mustache.render(systemMessageTemplate, {
Kim Peace committed
207 208 209 210
    year: messageDay.year + getLocalizedString("year"),
    month: messageDay.month + getLocalizedString("month"),
    day: messageDay.day + getLocalizedString("day"),
    dow: CHAT_UTIL.findDow(messageDay.dow),
211
    isInitalDateMessage: isInitialDateMessage,
Kim Peace committed
212 213 214 215
  });
};

ChatRoom.getChatMessageTemplate = function (messageType, senderID) {
216
  if (senderID == currentUserInfo.shopMemberID) {
Kim Peace committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    return myMessageTemplate;
  } else if (messageType == MessageType.SYSTEM) {
    return systemMessageTemplate;
  } else {
    return userMessageTemplate;
  }
};

ChatRoom.parseUserProfileURL = function (profileURL) {
  if (profileURL) {
    return Common.getProfileImgUrl(profileURL);
  } else {
    return Common.getProfileImgUrl("");
  }
};

ChatRoom.renderMessage = function (message, isToday, isOtherYear) {
  if (
    message.messageType == MessageType.COMMUNICATIONSTART ||
    message.messageType == MessageType.COMMUNICATIONEND
  ) {
    return ChatRoom.renderCollaborationMessage(message, isToday, isOtherYear);
  } else {
    return ChatRoom.renderChatMessage(message, isToday, isOtherYear);
  }
};

ChatRoom.renderCollaborationMessage = function (message, isToday, isOtherYear) {
  const collaborationInfo = JSON.parse(message.message);
  const userInCollaboration = JSON.parse(
    NativeBridgeDataSource.getUserInfoList(collaborationInfo.userList)
  );
  const displayUserList = ChatRoom.getDisplayUserList(userInCollaboration);
250
  const meetingID = ChatRoom.getMeetingID(collaborationInfo.meetingId);
Kim Peace committed
251 252 253
  const messageTime = CHAT_UTIL.formatDate(message.insertDate);
  const createdAtYear =
    message.insertDate.substring(0, 4) + getLocalizedString("year") + " ";
254
  // 協業のタイプを数値から文字列に変換
Kim Peace committed
255 256 257
  const strCollaborationType = CHAT_UTIL.getCollaborationType(
    collaborationInfo.collaborationType
  );
Kim Peace committed
258

259 260 261 262 263
  const collaborationJoinMessage =
    message.messageType == MessageType.COMMUNICATIONEND
      ? getLocalizedString("message_ended")
      : getLocalizedString("message_join");

Kim Peace committed
264 265 266 267 268 269
  const html = Mustache.render(collaborationMessageTemplate, {
    messageId: message.messageId,
    roomName: roomName,
    userCount: userInCollaboration.length,
    userList: displayUserList,
    insertDate: message.insertDate,
270
    collaborationType: strCollaborationType,
Kim Peace committed
271 272 273 274 275 276 277
    meetingId: meetingID,
    isToday: isToday,
    createdAtDay: messageTime.createdAtDay,
    createdAtTime: messageTime.createdAtTime,
    createdAtYear: createdAtYear,
    isOtherYear: isOtherYear,
    isEnded: message.messageType == MessageType.COMMUNICATIONEND ? true : false,
278
    collaborationJoinMessage: collaborationJoinMessage,
Kim Peace committed
279 280 281 282 283 284 285
  });
  return message.message.includes("attachedImages") ||
    message.message.includes("attachedVideos")
    ? CHAT_UTIL.htmlDecode(html)
    : html;
};

286
ChatRoom.getMeetingID = function (meetingID) {
287
  return typeof meetingID == "undefined" ? 0 : meetingID;
Kim Peace committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
};

ChatRoom.getDisplayUserList = function (userList) {
  userList.forEach(function (user) {
    user.profileUrl = Common.getProfileImgUrl(user.profileUrl);
  });
  return userList.length > 3 ? userList.slice(0, 3) : userList;
};

ChatRoom.renderChatMessage = function (message, isToday, isOtherYear) {
  const template = ChatRoom.getChatMessageTemplate(
    message.messageType,
    message.shopMemberId
  );

  const messageTime = CHAT_UTIL.formatDate(message.insertDate);

  const createdAtYear =
    message.insertDate.substring(0, 4) + getLocalizedString("year") + " ";

  const html = Mustache.render(template, {
    text: message.message,
    from: message.loginId,
    messageId: message.messageId,
    shopMemberId: message.shopMemberId,
    profileImage: message.profileUrl,
    createdAtDay: messageTime.createdAtDay,
    createdAtTime: messageTime.createdAtTime,
    createdAtYear: createdAtYear,
    unreadCount: message.unreadCount,
    isOtherYear: isOtherYear,
    isToday: isToday,
  });
  return message.message.includes("attachedImages") ||
    message.message.includes("attachedVideos")
    ? CHAT_UTIL.htmlDecode(html)
    : html;
};