Skip to content

Instantly share code, notes, and snippets.

@xiaojue
Created November 15, 2012 09:37
Show Gist options
  • Save xiaojue/4077675 to your computer and use it in GitHub Desktop.
Save xiaojue/4077675 to your computer and use it in GitHub Desktop.
define(function(require, exports, module) {
var $ = require('jquery'),
face = require('mods/face/face'),
Mu = require('mustache');
require('vendor/jquery.jscrollbar');
var chatWindow = function() {
//发言间隔time object
this.timer = null;
//最后一句发言
this.lastSay = null;
this.limit = {
time: 5000,
size: 200
};
};
$.extend(chatWindow.prototype, {
constructor: chatWindow,
//发布消息方法
publicLog: function(msg) {
this.send({
cmd: 'msg',
msg: msg,
pid: 'all'
});
},
publicLogTo: function(pid, msg) {
this.send({
cmd: 'msg',
msg: msg,
pid: pid
});
},
privateLog: function(pid, msg) {
this.send({
cmd: 'msg_private',
username: pid,
msg: msg
});
},
//获取历史消息
getHistory: function(cb) {
},
_setTimestamp: function(value) {
var D = new Date(parseInt(value, 10));
return D.getHours() + ':' + D.getSeconds();
},
//放置msgs
putPublicMsgs: function(data) {
var self = this,
tpl = $('#tpl_msgs_public').html();
data.msgs.forEach(function(msg) {
msg.time = self._setTimestamp(msg.time);
msg.msg = self.face.drawUrl(msg.msg);
msg.msg = self.face.drawImg(msg.msg);
});
var html = Mu.render(tpl, data);
$('div[node-type=mainwindow]').append(html);
},
//放置登入系统消息
putSysLog: function(data) {
var self = this,
typeMap = {
join: "#tpl_join",
exit: "#tpl_exit",
kick: "#tpl_kick"
},
tpl = $(typeMap[data.type]).html();
var html = Mu.render(tpl, data);
$('div[node-type=mainwindow]').append(html);
},
//委派事件
_bind: function(type, hook, fn) {
$(this.config.target).on(type, hook, fn);
},
//仿backbone绑定事件 - 移除需要off
_initBind: function() {
var self = this;
for (var i in this.events) {
var imap = i.split(/\s/g),
type = imap.shift(),
hooks = imap.join(','),
fnname = this.events[i],
fn = self[fnname];
if (!$.isFunction(fn)) continue;
(function(fn, hooks, type) {
self._bind(type, hooks, function(e) {
fn.call(self, e);
});
})(fn, hooks, type);
}
for (var j in this.channelEvents) {
var f = self[this.channelEvents[j]];
if (!$.isFunction(f)) continue;
(function(fn) {
self.on(j, function(data) {
fn.call(self, data);
self.box.resize();
});
})(f);
}
},
say: function() {
var msg = $('input[node-type=say-input]').val();
this.publicLog(msg);
},
channelEvents: {
"msg": "putPublicMsgs",
"join": "putSysLog",
"exit": "putSysLog"
},
events: {
"click [action-type=say]": "say"
},
initWindow: function() {
var self = this;
this._initBind();
this.face = new face({
parent: self.config.target,
target: '[action-type=face]'
});
$('#mychatbar').jScrollbar({
mask:'div[node-type=mainwindow]',
scrollStep:5,
allowMouseWheel:true,
position:['right','bottom']
});
self.box = $('#mychatbar').data('scrollbar');
}
});
module.exports = chatWindow;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment