Light12306/Web12306/js/ui/chat/roomsession.js

257 lines
8.2 KiB
JavaScript
Raw Normal View History

2014-08-13 00:14:00 +08:00
define(function (require, exports, module) {
var EventObject = require("../../platform/EventObject.js");
var ep = require("../../platform/extensionPort.js");
var port = ep.port;
2014-08-14 21:33:47 +08:00
var mp = require("../widget_message_popup.js");
var sessMgr = require("../../account/sessionMgr.js");
var param = require("../../data.js");
var page = $("#chat_container");
var user = sessMgr.current;
2014-08-18 23:12:35 +08:00
var utility = require("../../utility.js");
2014-08-08 20:46:37 +08:00
2014-08-14 21:33:47 +08:00
sessMgr.on("sessionChanged", function () {
user = sessMgr.current;
});
2014-08-13 00:14:00 +08:00
var RoomSession = function () {
EventObject.apply(this);
var that = this;
var room;
2014-08-14 21:33:47 +08:00
var chatListContainer = page.find("article.chat-items");
var chatListContainerDom = chatListContainer[0];
var roomStateTemplate = $("#chat_connect_server").doT();
var roomSystemNoticeTemplate = $("#chat_sys_alert").doT();
2014-08-18 23:12:35 +08:00
var chatMsgItem = $("#chat_msg_item").doT();
2014-08-13 00:14:00 +08:00
2014-08-30 01:32:27 +08:00
this.clearConnectingState = function () {
chatListContainer.find(".chat-state").remove();
};
2014-08-13 00:14:00 +08:00
this.enterRoom = function (targetRoom) {
2014-08-14 21:33:47 +08:00
if (targetRoom === room)
2014-08-13 00:14:00 +08:00
return;
room = targetRoom;
2014-08-14 21:33:47 +08:00
//UI
$("#chat_frame>section").removeClass("chat-page-current");
page.addClass("chat-page-current");
page.find(">header>span:eq(0)").html(room.name);
chatListContainer.empty();
that.refreshOnlineCount();
page.find("button.button-primary").prop("disabled", true);
that.fireEvent("enterRoom");
2014-08-30 01:32:27 +08:00
if (!room.url) {
that.appendMessageItem(roomStateTemplate({ state: "connecting", stateIcon: "fa-spin fa-spinner", msg: "正在获得房间地址..." }));
$.post(param.chatServerGetAddressApi, { roomId: room.id }, "json").done(function (result) {
if (!result.url) {
that.clearConnectingState();
that.appendMessageItem(roomStateTemplate({ state: "disconnect", stateIcon: "fa-times", msg: "无法进入房间,请重试。", exinfo: "<a href='javascript:;' class='chat-frame-reconnect'>重新连接</a>" }));
that.exitRoom();
mp.showMessagePopup("error", "无法进入房间,请稍后重试。");
} else {
room.url = result.url;
room.heartbeat = result.heartbeat;
that.clearConnectingState();
port.postMessage("enterChatRoom", room);
}
}).fail(function () {
that.clearConnectingState();
that.appendMessageItem(roomStateTemplate({ state: "disconnect", stateIcon: "fa-times", msg: "无法进入房间,请重试。", exinfo: "<a href='javascript:;' class='chat-frame-reconnect'>重新连接</a>" }));
that.exitRoom();
mp.showMessagePopup("error", "无法进入房间,请稍后重试。");
});
} else {
port.postMessage("enterChatRoom", room);
}
2014-08-14 21:33:47 +08:00
};
this.refreshOnlineCount = function () {
page.find(">header>span:eq(1)").html("当前在线 " + room.onlinecount + " 人");
2014-08-19 21:33:26 +08:00
page.find(".chat-online-count").html(room.onlinecount);
2014-08-13 00:14:00 +08:00
};
this.exitRoom = function () {
2014-08-14 21:33:47 +08:00
room = null;
2014-08-13 00:14:00 +08:00
port.postMessage("disconnectChatRoom");
that.fireEvent("exitRoom");
};
2014-08-14 21:33:47 +08:00
this.appendMessageItem = function (html) {
chatListContainer.append(html);
2014-08-18 23:12:35 +08:00
chatListContainerDom.scrollTop = chatListContainerDom.scrollHeight;
2014-08-14 21:33:47 +08:00
};
this.clearDisplay = function () {
chatListContainer.empty();
};
this.sendMsg = function (msg) {
var data = {
items: [msg],
from: {
realname: user.dispname,
username: user.username
},
2014-08-18 23:12:35 +08:00
target: msg.target || [],
time: new Date().getTime()
2014-08-14 21:33:47 +08:00
}
port.postMessage("chatRoomSendMsg", data);
};
2014-08-19 21:33:26 +08:00
this.reconnect = function () {
if (!room)
return;
port.postMessage("enterChatRoom", room);
};
2014-08-13 00:14:00 +08:00
Object.defineProperty(this, "room", {
get: function () {
return room;
}
});
port.on("chatRoomConnecting", function () {
2014-08-14 21:33:47 +08:00
that.appendMessageItem(roomStateTemplate({ state: "connecting", stateIcon: "fa-spin fa-spinner", msg: "正在连接服务器中..." }));
2014-08-13 00:14:00 +08:00
that.fireEvent("chatRoomConnecting");
});
port.on("chatRoomConnected", function () {
2014-08-14 21:33:47 +08:00
page.find("button.button-primary").prop("disabled", false);
2014-08-30 01:32:27 +08:00
that.clearConnectingState();
2014-08-14 21:33:47 +08:00
that.appendMessageItem(roomSystemNoticeTemplate(room));
2014-08-13 00:14:00 +08:00
that.fireEvent("chatRoomConnected");
});
2014-08-14 21:33:47 +08:00
port.on("chatRoomReceiveMsg", function (e, data) {
that.fireEvent("chatRoomReceiveMsg");
2014-08-18 23:12:35 +08:00
if (data.time) {
data.time = new Date(data.time);
data.timeStr = utility.formatDate(data.time, "hh:mm:ss");
}
data.self = data.from.username == sessMgr.current.username;
2014-08-30 01:32:27 +08:00
data.sys = data.from.username || false;
2014-08-18 23:12:35 +08:00
data.tome = false;
if (data.target && data.target.length) {
var selfTarget = _.findWhere(data.target, { username: sessMgr.current.username });
if (selfTarget) {
data.tome = true;
selfTarget.username = null;
selfTarget.realname = "我";
}
}
that.appendMessageItem(chatMsgItem(data));
2014-08-13 00:14:00 +08:00
});
port.on("chatRoomDisconnected", function () {
2014-08-30 01:32:27 +08:00
that.clearConnectingState();
2014-08-18 23:12:35 +08:00
that.appendMessageItem(roomStateTemplate({ state: "disconnect", stateIcon: "fa-times", msg: "服务器已断开连接。", exinfo: "<a href='javascript:;' class='chat-frame-reconnect'>重新连接</a>" }));
2014-08-13 00:14:00 +08:00
that.fireEvent("chatRoomDisconnected");
2014-08-14 21:33:47 +08:00
page.find("button.button-primary").prop("disabled", true);
2014-08-13 00:14:00 +08:00
});
2014-08-18 23:12:35 +08:00
port.on("chatUpdateOnline", function () {
if (!room)
return;
room.onlinecount = this.detail.count;
2014-08-19 21:33:26 +08:00
//更新显示
that.refreshOnlineCount();
2014-08-18 23:12:35 +08:00
});
2014-09-02 15:35:51 +08:00
port.on("sendMessageFailed", function (e, data) {
2014-09-01 20:24:57 +08:00
that.appendMessageItem(roomStateTemplate({ state: "error", stateIcon: "fa-times", msg: "消息发送失败:" + data, exinfo: "" }));
2014-08-30 01:32:27 +08:00
});
2014-08-13 00:14:00 +08:00
return this;
};
RoomSession.prototype = Object.create(EventObject);
RoomSession.constructor = RoomSession;
var session = new RoomSession();
2014-08-14 21:33:47 +08:00
(function chatEditor() {
var editor = page.find("section.chat-editor");
var sendBtn = editor.find(">footer>button.button-primary");
2014-08-18 23:12:35 +08:00
var editorArea = editor.find(".chat-editor-container");
2014-08-14 21:33:47 +08:00
var initEditor = function () {
if (!sessMgr.current)
return;
var color = sessMgr.current.options.chatColor || "#000000";
editor.find(">header .chat-editor-color").val(color);
editor.find(".chat-editor-container").css("color", color);
};
var delayEnableSend = function () {
sendBtn.prop("disabled", true);
setTimeout(function () {
sendBtn.prop("disabled", false);
}, param.chatSendMsgDelay);
};
initEditor();
2014-08-18 23:12:35 +08:00
editor.find(".chat-editor-container").keyup(function (e) {
2014-08-14 21:33:47 +08:00
if (e.keyCode === 13 && e.ctrlKey) {
sendBtn.click();
}
});
sessMgr.on("sessionChanged", function () {
initEditor();
});
editor.find(">header .chat-btn-cls").click(function () {
session.clearDisplay();
});
editor.find(">header .chat-editor-color").change(function () {
user.options.chatColor = this.value;
editor.find(".chat-editor-container").css("color", this.value);
sessMgr.save();
});
editor.find(">footer>button.button-default").click(function () {
session.exitRoom();
});
editor.find(">footer>button.button-primary").click(function () {
2014-08-18 23:12:35 +08:00
var editorObj = editor.find(".chat-editor-container");
var target = [];
var addedUser = [];
editorObj.find("span.chat-editor-at").each(function () {
var username = this.dataset.un;
if (addedUser[username])
return;
addedUser[username] = true;
target.push({
realname: $.trim($(this).text()).substr(1),
username: username
});
}).remove();
2014-08-14 21:33:47 +08:00
var msg = {
color: user.options.chatColor,
2014-09-02 15:35:51 +08:00
text: $.trim(editorObj.text()).replace(/\u00a0/g, ""),
2014-08-18 23:12:35 +08:00
target: target
2014-08-14 21:33:47 +08:00
}
if (!msg.text)
return;
editor.find(".chat-editor-container").empty();
session.sendMsg(msg);
delayEnableSend();
});
2014-08-18 23:12:35 +08:00
$(document).on("click", "a.chat-item-at", function () {
var un = this.dataset.un;
var name = this.innerHTML;
2014-08-19 21:33:26 +08:00
if (!un)
return;
2014-08-18 23:12:35 +08:00
var html = "<span class='chat-editor-at' contenteditable='false' data-un='" + un + "'>@" + name + "</span>&nbsp;";
editorArea.append(html);
var range = document.createRange();
range.selectNodeContents(editorArea[0]);
range.collapse(false);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
editorArea[0].focus();
});
2014-08-30 01:32:27 +08:00
$(document).on("click", "a.chat-frame-reconnect", function () {
2014-08-19 21:33:26 +08:00
$(".chat-item").remove();
session.reconnect();
});
2014-08-14 21:33:47 +08:00
})();
2014-08-13 00:14:00 +08:00
return {
session: session,
enterRoom: session.enterRoom
}
2014-08-08 20:46:37 +08:00
});