Main.js 5.6 KB
Newer Older
NoSubject's avatar
NoSubject 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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
MWF.xApplication.IMV2.options.multitask = true;
MWF.xApplication.IMV2.Main = new Class({
	Extends: MWF.xApplication.Common.Main,
	Implements: [Options, Events],

	options: {
		"style": "default",
		"name": "IMV2",
		"mvcStyle": "style.css",
		"icon": "icon.png",
		"width": "1200",
		"height": "700",
		"isResize": true,
		"isMax": true,
		"title": MWF.xApplication.IMV2.LP.title
	},
	onQueryLoad: function () {
		this.lp = MWF.xApplication.IMV2.LP;
		this.conversationList = [];
	},
	loadApplication: function (callback) {
		var url = this.path + this.options.style + "/im.html";
		this.content.loadHtml(url, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function () {
			//获取会话列表
			o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
				if (json.data && json.data instanceof Array) {
					this.conversationList = json.data;
					this.loadConversationList(json.data);
				}
			}.bind(this));

		}.bind(this));
	},
	//加载会话列表
	loadConversationList: function (list) {
		for (var i = 0; i < list.length; i++) {
			var chat = list[i];
			var url = this.path + this.options.style + "/conversationItem.html";
			var avatarDefault = this._getIcon();
			var data = {
				"id": chat.id,
				"avatarUrl": avatarDefault,
				"title": chat.title,
				"time": "",
				"lastMessage": ""
			};
			var distinguishedName = layout.session.user.distinguishedName;
			if (chat.type && chat.type === "single") {
				var chatPerson = "";
				if (chat.personList && chat.personList instanceof Array) {
F
fancy 已提交
51 52
					for (var j = 0; j < chat.personList.length; j++) {
						var person = chat.personList[j];
NoSubject's avatar
NoSubject 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
						if (person !== distinguishedName) {
							chatPerson = person;
						}
					}
				}
				data.avatarUrl = this._getIcon(chatPerson);
				var name = chatPerson;
				if (chatPerson.indexOf("@") != -1) {
					name = name.substring(0, chatPerson.indexOf("@"));
				}
				data.title = name;
			}
			if (chat.lastMessage) {
				//todo 其它消息类型
				var mBody = JSON.parse(chat.lastMessage.body);
				data.lastMessage = mBody.body;
				if (chat.lastMessage.createTime) {
					var time = this._friendlyTime(o2.common.toDate(chat.lastMessage.createTime));
					data.time = time;
				}
			}
F
fancy 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
			var chatItemNode = new Element("div", {"class": "item"}).inject(this.chatItemListNode);
			var baseClass = "base";
			if (i == 0) {
				baseClass = "base check";
			}
			var chatItemBaseNode = new Element("div", {"class": baseClass}).inject(chatItemNode);
			var avatarNode = new Element("div", {"class": "avatar"}).inject(chatItemBaseNode);
			var avatarImg = new Element("img", {"src": data.avatarUrl, "class": "img"}).inject(avatarNode);
			var bodyNode = new Element("div", {"class": "body"}).inject(chatItemBaseNode);
			var bodyUpNode = new Element("div", {"class": "body_up"}).inject(bodyNode);
			var bodyTitleNode = new Element("div", {"class": "body_title", "text": data.title}).inject(bodyUpNode);
			var bodyTimeNode = new Element("div", {"class": "body_time", "text": data.time}).inject(bodyUpNode);
			var bodyDownNode = new Element("div", {"class": "body_down", "text": data.lastMessage}).inject(bodyNode);
			var _self = this;
			chatItemNode.addEvents({
				"click": function(){
					_self.tapConv(chat);
				}
			});
NoSubject's avatar
NoSubject 已提交
93 94 95 96 97

		}
		console.log("结束");
	},
	//点击
F
fancy 已提交
98
	tapConv: function (conv) {
NoSubject's avatar
NoSubject 已提交
99 100
		console.log("clickConversationvvvvvv");
		var url = this.path + this.options.style + "/chat.html";
F
fancy 已提交
101 102 103
		var data = {"convName": conv.title};
		this.chatNode.loadHtml(url, { "bind": data, "module": this }, function () {
			
NoSubject's avatar
NoSubject 已提交
104 105
		}.bind(this));
	},
F
fancy 已提交
106 107 108 109 110 111
	//发送消息
	sendMsg: function() {
		console.log("click send Msg btn................");
		var text = this.chatBottomAreaTextareaNode.value;
		console.log(text);
	},
NoSubject's avatar
NoSubject 已提交
112 113 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180


	_getIcon: function (id) {
		var orgAction = MWF.Actions.get("x_organization_assemble_control")
		var url = (id) ? orgAction.getPersonIcon(id) : "/x_component_IMV2/$Main/default/icons/group.png";
		return url + "?" + (new Date().getTime());
	},
	//输出特殊的时间格式
	_friendlyTime: function (date) {
		var day = date.getDate();
		var monthIndex = date.getMonth();
		var year = date.getFullYear();
		var time = date.getTime();
		var today = new Date();
		var todayDay = today.getDate();
		var todayMonthIndex = today.getMonth();
		var todayYear = today.getFullYear();
		var todayTime = today.getTime();

		var retTime = "";
		//同一天
		if (day === todayDay && monthIndex === todayMonthIndex && year === todayYear) {
			var hour = 0;
			if (todayTime > time) {
				hour = parseInt((todayTime - time) / 3600000);
				if (hour == 0) {
					retTime = Math.max(parseInt((todayTime - time) / 60000), 1) + "分钟前"
				} else {
					retTime = hour + "小时前"
				}

			}
			return retTime;
		}
		var dates = parseInt(time / 86400000);
		var todaydates = parseInt(todayTime / 86400000);
		if (todaydates > dates) {
			var days = (todaydates - dates);
			if (days == 1) {
				retTime = "昨天";
			} else if (days == 2) {
				retTime = "前天 ";
			} else if (days > 2 && days < 31) {
				retTime = days + "天前";
			} else if (days >= 31 && days <= 2 * 31) {
				retTime = "一个月前";
			} else if (days > 2 * 31 && days <= 3 * 31) {
				retTime = "2个月前";
			} else if (days > 3 * 31 && days <= 4 * 31) {
				retTime = "3个月前";
			} else {
				retTime = this._formatDate(date);
			}
		}

		return retTime;

	},
	//yyyy-MM-dd
	_formatDate: function (date) {
		var month = date.getMonth() + 1;
		var day = date.getDate();
		month = (month.toString().length == 1) ? ("0" + month) : month;
		day = (day.toString().length == 1) ? ("0" + day) : day;
		return date.getFullYear() + '-' + month + '-' + day;
	}


});