| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 01:40:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 22:34:36 +03:00
										 |  |  | define('chat', [ | 
					
						
							|  |  |  | 	'components', | 
					
						
							|  |  |  | 	'taskbar', | 
					
						
							|  |  |  | 	'string', | 
					
						
							|  |  |  | 	'sounds', | 
					
						
							|  |  |  | 	'forum/chats', | 
					
						
							|  |  |  | 	'forum/chats/messages', | 
					
						
							| 
									
										
										
										
											2016-12-29 11:25:05 -08:00
										 |  |  | 	'translator', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 	'scrollStop', | 
					
						
							| 
									
										
										
										
											2017-08-30 11:32:23 -06:00
										 |  |  | 	'benchpress', | 
					
						
							|  |  |  | ], function (components, taskbar, S, sounds, Chats, ChatsMessages, translator, scrollStop, Benchpress) { | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 	var module = {}; | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 	var newMessage = false; | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.prepareDOM = function () { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:41:51 +02:00
										 |  |  | 		var chatsToggleEl = components.get('chat/dropdown'); | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 		var chatsListEl = components.get('chat/list'); | 
					
						
							| 
									
										
										
										
											2014-01-13 13:50:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		chatsToggleEl.on('click', function () { | 
					
						
							| 
									
										
										
										
											2014-03-12 01:40:31 -04:00
										 |  |  | 			if (chatsToggleEl.parent().hasClass('open')) { | 
					
						
							| 
									
										
										
										
											2014-01-13 10:06:00 -05:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 14:30:18 -04:00
										 |  |  | 			module.loadChatsDropdown(chatsListEl); | 
					
						
							| 
									
										
										
										
											2014-01-13 10:06:00 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		chatsListEl.on('click', '[data-roomid]', function (ev) { | 
					
						
							| 
									
										
										
										
											2016-09-28 18:29:38 +03:00
										 |  |  | 			if ($(ev.target).parents('.user-link').length) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			var roomId = $(this).attr('data-roomid'); | 
					
						
							| 
									
										
										
										
											2016-08-18 14:39:51 -04:00
										 |  |  | 			if (!ajaxify.currentPage.match(/^chats\//)) { | 
					
						
							|  |  |  | 				app.openChat(roomId); | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2016-09-20 14:26:26 +03:00
										 |  |  | 				ajaxify.go('user/' + app.user.userslug + '/chats/' + roomId); | 
					
						
							| 
									
										
										
										
											2016-08-18 14:39:51 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		$('[component="chats/mark-all-read"]').on('click', function () { | 
					
						
							|  |  |  | 			socket.emit('modules.chats.markAllRead', function (err) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return app.alertError(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		socket.on('event:chats.receive', function (data) { | 
					
						
							| 
									
										
										
										
											2014-04-14 18:04:12 -04:00
										 |  |  | 			var username = data.message.fromUser.username; | 
					
						
							| 
									
										
										
										
											2015-12-16 15:09:14 +02:00
										 |  |  | 			var isSelf = data.self === 1; | 
					
						
							| 
									
										
										
										
											2014-09-02 16:01:45 -04:00
										 |  |  | 			data.message.self = data.self; | 
					
						
							| 
									
										
										
										
											2015-12-16 15:09:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 15:50:02 -04:00
										 |  |  | 			newMessage = data.self === 0; | 
					
						
							| 
									
										
										
										
											2015-12-16 15:09:14 +02:00
										 |  |  | 			if (module.modalExists(data.roomId)) { | 
					
						
							|  |  |  | 				var modal = module.getModal(data.roomId); | 
					
						
							| 
									
										
										
										
											2015-09-18 14:30:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 22:34:36 +03:00
										 |  |  | 				ChatsMessages.appendChatMessage(modal.find('.chat-content'), data.message); | 
					
						
							| 
									
										
										
										
											2014-01-13 13:50:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-21 10:14:44 +02:00
										 |  |  | 				if (modal.is(':visible')) { | 
					
						
							| 
									
										
										
										
											2014-01-13 13:50:33 -05:00
										 |  |  | 					taskbar.updateActive(modal.attr('UUID')); | 
					
						
							| 
									
										
										
										
											2016-04-30 22:34:36 +03:00
										 |  |  | 					ChatsMessages.scrollToBottom(modal.find('.chat-content')); | 
					
						
							| 
									
										
										
										
											2014-01-13 10:06:00 -05:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2015-07-28 10:24:46 -04:00
										 |  |  | 					module.toggleNew(modal.attr('UUID'), true, true); | 
					
						
							| 
									
										
										
										
											2014-01-13 10:06:00 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-01-13 13:50:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-21 10:14:44 +02:00
										 |  |  | 				if (!isSelf && (!modal.is(':visible') || !app.isFocused)) { | 
					
						
							| 
									
										
										
										
											2014-05-09 17:57:39 -04:00
										 |  |  | 					app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]'); | 
					
						
							| 
									
										
										
										
											2017-02-16 22:56:25 -07:00
										 |  |  | 					sounds.play('chat-incoming', 'chat.incoming:' + data.message.mid); | 
					
						
							| 
									
										
										
										
											2015-04-27 12:41:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					taskbar.push('chat', modal.attr('UUID'), { | 
					
						
							| 
									
										
										
										
											2017-01-23 13:02:35 +03:00
										 |  |  | 						title: data.roomName || username, | 
					
						
							| 
									
										
										
										
											2016-03-21 12:57:23 -04:00
										 |  |  | 						touid: data.message.fromUser.uid, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 						roomId: data.roomId, | 
					
						
							| 
									
										
										
										
											2015-04-27 12:41:16 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-01-13 13:50:33 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2017-04-07 11:35:24 -07:00
										 |  |  | 				socket.emit('modules.chats.loadRoom', { | 
					
						
							|  |  |  | 					roomId: data.roomId, | 
					
						
							|  |  |  | 				}, function (err, roomData) { | 
					
						
							| 
									
										
										
										
											2015-12-21 10:14:44 +02:00
										 |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return app.alertError(err.message); | 
					
						
							| 
									
										
										
										
											2014-05-09 17:46:10 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					roomData.users = roomData.users.filter(function (user) { | 
					
						
							| 
									
										
										
										
											2015-12-21 10:14:44 +02:00
										 |  |  | 						return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | 					roomData.silent = true; | 
					
						
							| 
									
										
										
										
											2016-10-02 15:17:22 +03:00
										 |  |  | 					roomData.uid = app.user.uid; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					module.createModal(roomData, function (modal) { | 
					
						
							| 
									
										
										
										
											2016-01-16 11:01:06 +02:00
										 |  |  | 						module.toggleNew(modal.attr('UUID'), !isSelf, true); | 
					
						
							| 
									
										
										
										
											2015-12-21 10:14:44 +02:00
										 |  |  | 						if (!isSelf) { | 
					
						
							|  |  |  | 							app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + username + ']]'); | 
					
						
							| 
									
										
										
										
											2017-02-16 22:56:25 -07:00
										 |  |  | 							sounds.play('chat-incoming', 'chat.incoming:' + data.message.mid); | 
					
						
							| 
									
										
										
										
											2015-12-21 10:14:44 +02:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-01-13 13:50:33 -05:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-13 10:06:00 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-04-15 12:48:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		socket.on('event:user_status_change', function (data) { | 
					
						
							| 
									
										
										
										
											2014-11-04 16:49:02 -05:00
										 |  |  | 			var modal = module.getModal(data.uid); | 
					
						
							| 
									
										
										
										
											2015-04-07 13:45:46 -04:00
										 |  |  | 			app.updateUserStatus(modal.find('[component="user/status"]'), data.status); | 
					
						
							| 
									
										
										
										
											2014-09-02 05:04:39 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		socket.on('event:chats.roomRename', function (data) { | 
					
						
							| 
									
										
										
										
											2017-01-23 13:02:35 +03:00
										 |  |  | 			var newTitle = $('<div/>').html(data.newName).text(); | 
					
						
							|  |  |  | 			var modal = module.getModal(data.roomId); | 
					
						
							|  |  |  | 			modal.find('[component="chat/room/name"]').val(newTitle); | 
					
						
							|  |  |  | 			taskbar.updateTitle('chat', modal.attr('UUID'), newTitle); | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-02-01 21:22:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 22:34:36 +03:00
										 |  |  | 		ChatsMessages.onChatMessageEdit(); | 
					
						
							| 
									
										
										
										
											2014-03-12 01:40:31 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-13 10:06:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.loadChatsDropdown = function (chatsListEl) { | 
					
						
							| 
									
										
										
										
											2017-04-07 11:35:24 -07:00
										 |  |  | 		socket.emit('modules.chats.getRecentChats', { | 
					
						
							|  |  |  | 			uid: app.user.uid, | 
					
						
							|  |  |  | 			after: 0, | 
					
						
							|  |  |  | 		}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2015-08-26 16:32:32 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return app.alertError(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			var rooms = data.rooms.filter(function (room) { | 
					
						
							| 
									
										
										
										
											2017-02-18 14:42:15 -07:00
										 |  |  | 				return room.teaser; | 
					
						
							| 
									
										
										
										
											2016-02-29 06:19:43 +00:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-08-26 16:32:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 11:32:23 -06:00
										 |  |  | 			Benchpress.parse('partials/chats/dropdown', { | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				rooms: rooms, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			}, function (html) { | 
					
						
							|  |  |  | 				translator.translate(html, function (translated) { | 
					
						
							| 
									
										
										
										
											2016-08-25 16:05:02 +03:00
										 |  |  | 					chatsListEl.empty().html(translated); | 
					
						
							|  |  |  | 					app.createUserTooltips(chatsListEl, 'right'); | 
					
						
							| 
									
										
										
										
											2015-09-14 13:19:21 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-08-26 16:32:32 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.bringModalToTop = function (chatModal) { | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 		var topZ = 0; | 
					
						
							| 
									
										
										
										
											2014-11-07 18:38:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		taskbar.updateActive(chatModal.attr('UUID')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-02 20:21:43 -04:00
										 |  |  | 		if ($('.chat-modal').length === 1) { | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		$('.chat-modal').each(function () { | 
					
						
							| 
									
										
										
										
											2014-01-06 17:37:42 -05:00
										 |  |  | 			var thisZ = parseInt($(this).css('zIndex'), 10); | 
					
						
							|  |  |  | 			if (thisZ > topZ) { | 
					
						
							|  |  |  | 				topZ = thisZ; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-10-02 20:21:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:20 -04:00
										 |  |  | 		chatModal.css('zIndex', topZ + 1); | 
					
						
							| 
									
										
										
										
											2014-02-26 21:55:29 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.getModal = function (roomId) { | 
					
						
							| 
									
										
										
										
											2015-12-16 10:09:00 +02:00
										 |  |  | 		return $('#chat-modal-' + roomId); | 
					
						
							| 
									
										
										
										
											2014-02-26 21:55:29 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-27 15:55:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.modalExists = function (roomId) { | 
					
						
							| 
									
										
										
										
											2015-12-16 10:09:00 +02:00
										 |  |  | 		return $('#chat-modal-' + roomId).length !== 0; | 
					
						
							| 
									
										
										
										
											2014-02-26 21:55:29 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-10-11 13:06:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.createModal = function (data, callback) { | 
					
						
							|  |  |  | 		app.parseAndTranslate('chat', data, function (chatModal) { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			var uuid = utils.generateUUID(); | 
					
						
							|  |  |  | 			var dragged = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			chatModal.attr('id', 'chat-modal-' + data.roomId); | 
					
						
							| 
									
										
										
										
											2017-04-07 11:35:24 -07:00
										 |  |  | 			chatModal.attr('data-roomid', data.roomId); | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			chatModal.attr('intervalId', 0); | 
					
						
							|  |  |  | 			chatModal.attr('UUID', uuid); | 
					
						
							|  |  |  | 			chatModal.css('position', 'fixed'); | 
					
						
							|  |  |  | 			chatModal.css('zIndex', 100); | 
					
						
							|  |  |  | 			chatModal.appendTo($('body')); | 
					
						
							|  |  |  | 			chatModal.find('.timeago').timeago(); | 
					
						
							|  |  |  | 			module.center(chatModal); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			app.loadJQueryUI(function () { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				chatModal.find('.modal-content').resizable({ | 
					
						
							|  |  |  | 					handles: 'n, e, s, w, se', | 
					
						
							|  |  |  | 					minHeight: 250, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					minWidth: 400, | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-05-02 18:07:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				chatModal.find('.modal-content').on('resize', function (event, ui) { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 					if (ui.originalSize.height === ui.size.height) { | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-09-10 17:13:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 13:47:45 -05:00
										 |  |  | 					chatModal.find('.modal-body').css('height', module.calculateChatListHeight(chatModal)); | 
					
						
							| 
									
										
										
										
											2014-05-02 18:07:12 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				chatModal.draggable({ | 
					
						
							| 
									
										
										
										
											2017-02-18 01:27:46 -07:00
										 |  |  | 					start: function () { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 						module.bringModalToTop(chatModal); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:27:46 -07:00
										 |  |  | 					stop: function () { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 						chatModal.find('#chat-message-input').focus(); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					distance: 10, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					handle: '.modal-header', | 
					
						
							| 
									
										
										
										
											2014-01-05 22:09:13 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-23 13:02:35 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 11:25:05 -08:00
										 |  |  | 			scrollStop.apply(chatModal.find('[component="chat/messages"]')); | 
					
						
							| 
									
										
										
										
											2014-01-05 22:09:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			chatModal.find('#chat-close-btn').on('click', function () { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				module.close(chatModal); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-09-10 17:13:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			function gotoChats() { | 
					
						
							|  |  |  | 				var text = components.get('chat/input').val(); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				$(window).one('action:ajaxify.end', function () { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 					components.get('chat/input').val(text); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-09-25 10:30:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 11:35:24 -07:00
										 |  |  | 				ajaxify.go('user/' + app.user.userslug + '/chats/' + chatModal.attr('data-roomid')); | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				module.close(chatModal); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-07-09 11:03:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			chatModal.find('.modal-header').on('dblclick', gotoChats); | 
					
						
							|  |  |  | 			chatModal.find('button[data-action="maximize"]').on('click', gotoChats); | 
					
						
							| 
									
										
										
										
											2016-10-23 17:39:40 -04:00
										 |  |  | 			chatModal.find('button[data-action="minimize"]').on('click', function () { | 
					
						
							| 
									
										
										
										
											2016-10-23 17:24:51 -04:00
										 |  |  | 				var uuid = chatModal.attr('uuid'); | 
					
						
							|  |  |  | 				module.minimize(uuid); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-11-07 18:38:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			chatModal.on('click', function () { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				module.bringModalToTop(chatModal); | 
					
						
							| 
									
										
										
										
											2014-11-07 18:38:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				if (dragged) { | 
					
						
							|  |  |  | 					dragged = false; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-05 22:09:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			chatModal.on('mousemove', function (e) { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				if (e.which === 1) { | 
					
						
							|  |  |  | 					dragged = true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-07-09 11:03:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			chatModal.on('mousemove keypress click', function () { | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				if (newMessage) { | 
					
						
							|  |  |  | 					socket.emit('modules.chats.markRead', data.roomId); | 
					
						
							|  |  |  | 					newMessage = false; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 15:01:05 -05:00
										 |  |  | 			Chats.addActionHandlers(chatModal.find('[component="chat/messages"]'), data.roomId); | 
					
						
							| 
									
										
										
										
											2017-12-21 16:51:16 -05:00
										 |  |  | 			Chats.addRenameHandler(chatModal.attr('data-roomid'), chatModal.find('[data-action="rename"]'), chatModal.attr('data-name')); | 
					
						
							| 
									
										
										
										
											2018-01-09 16:30:16 -05:00
										 |  |  | 			Chats.addLeaveHandler(chatModal.attr('data-roomid'), chatModal.find('[data-action="leave"]')); | 
					
						
							| 
									
										
										
										
											2018-01-05 14:40:38 -05:00
										 |  |  | 			Chats.addSendHandlers(chatModal.attr('data-roomid'), chatModal.find('.chat-input'), chatModal.find('[data-action="send"]')); | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			Chats.createTagsInput(chatModal.find('.users-tag-input'), data); | 
					
						
							|  |  |  | 			Chats.createAutoComplete(chatModal.find('[component="chat/input"]')); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 11:35:24 -07:00
										 |  |  | 			Chats.addScrollHandler(chatModal.attr('data-roomid'), data.uid, chatModal.find('.chat-content')); | 
					
						
							| 
									
										
										
										
											2014-01-05 22:09:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 16:32:33 -04:00
										 |  |  | 			Chats.addCharactersLeftHandler(chatModal); | 
					
						
							| 
									
										
										
										
											2017-04-17 18:59:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			taskbar.push('chat', chatModal.attr('UUID'), { | 
					
						
							| 
									
										
										
										
											2016-11-29 20:33:33 +03:00
										 |  |  | 				title: data.roomName || (data.users.length ? data.users[0].username : ''), | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 				roomId: data.roomId, | 
					
						
							|  |  |  | 				icon: 'fa-comment', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				state: '', | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-05 22:09:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			$(window).trigger('action:chat.loaded', chatModal); | 
					
						
							| 
									
										
										
										
											2014-07-01 15:05:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 22:18:47 +03:00
										 |  |  | 			if (typeof callback === 'function') { | 
					
						
							|  |  |  | 				callback(chatModal); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-28 22:08:46 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-26 21:55:29 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.focusInput = function (chatModal) { | 
					
						
							| 
									
										
										
										
											2014-12-21 00:08:01 -05:00
										 |  |  | 		chatModal.find('#chat-message-input').focus(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.close = function (chatModal) { | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 		clearInterval(chatModal.attr('intervalId')); | 
					
						
							|  |  |  | 		chatModal.attr('intervalId', 0); | 
					
						
							| 
									
										
										
										
											2014-04-15 12:48:28 -04:00
										 |  |  | 		chatModal.remove(); | 
					
						
							|  |  |  | 		chatModal.data('modal', null); | 
					
						
							|  |  |  | 		taskbar.discard('chat', chatModal.attr('UUID')); | 
					
						
							| 
									
										
										
										
											2015-02-26 11:03:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (chatModal.attr('data-mobile')) { | 
					
						
							|  |  |  | 			module.disableMobileBehaviour(chatModal); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-04-15 12:48:28 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.center = function (chatModal) { | 
					
						
							| 
									
										
										
										
											2014-11-04 16:49:02 -05:00
										 |  |  | 		var hideAfter = false; | 
					
						
							|  |  |  | 		if (chatModal.hasClass('hide')) { | 
					
						
							|  |  |  | 			chatModal.removeClass('hide'); | 
					
						
							|  |  |  | 			hideAfter = true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		chatModal.css('left', Math.max(0, (($(window).width() - $(chatModal).outerWidth()) / 2) + $(window).scrollLeft()) + 'px'); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:51:11 -07:00
										 |  |  | 		chatModal.css('top', Math.max(0, ($(window).height() / 2) - ($(chatModal).outerHeight() / 2)) + 'px'); | 
					
						
							| 
									
										
										
										
											2014-12-21 00:08:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-04 16:49:02 -05:00
										 |  |  | 		if (hideAfter) { | 
					
						
							|  |  |  | 			chatModal.addClass('hide'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-10-11 13:06:21 -04:00
										 |  |  | 		return chatModal; | 
					
						
							| 
									
										
										
										
											2014-02-26 21:55:29 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-10-11 13:06:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.load = function (uuid) { | 
					
						
							| 
									
										
										
										
											2015-12-16 10:09:00 +02:00
										 |  |  | 		var chatModal = $('div[UUID="' + uuid + '"]'); | 
					
						
							| 
									
										
										
										
											2014-01-05 22:09:13 -05:00
										 |  |  | 		chatModal.removeClass('hide'); | 
					
						
							| 
									
										
										
										
											2013-12-05 17:35:44 -05:00
										 |  |  | 		taskbar.updateActive(uuid); | 
					
						
							| 
									
										
										
										
											2016-04-30 22:34:36 +03:00
										 |  |  | 		ChatsMessages.scrollToBottom(chatModal.find('.chat-content')); | 
					
						
							| 
									
										
										
										
											2014-01-30 14:04:20 -05:00
										 |  |  | 		module.bringModalToTop(chatModal); | 
					
						
							| 
									
										
										
										
											2014-12-21 00:08:01 -05:00
										 |  |  | 		module.focusInput(chatModal); | 
					
						
							| 
									
										
										
										
											2017-04-07 11:35:24 -07:00
										 |  |  | 		socket.emit('modules.chats.markRead', chatModal.attr('data-roomid')); | 
					
						
							| 
									
										
										
										
											2015-02-26 11:03:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		var env = utils.findBootstrapEnvironment(); | 
					
						
							| 
									
										
										
										
											2016-02-23 11:07:32 -05:00
										 |  |  | 		if (env === 'xs' || env === 'sm') { | 
					
						
							| 
									
										
										
										
											2015-02-26 11:03:15 -05:00
										 |  |  | 			module.enableMobileBehaviour(chatModal); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.enableMobileBehaviour = function (modalEl) { | 
					
						
							| 
									
										
										
										
											2015-02-26 11:03:15 -05:00
										 |  |  | 		app.toggleNavbar(false); | 
					
						
							|  |  |  | 		modalEl.attr('data-mobile', '1'); | 
					
						
							| 
									
										
										
										
											2015-07-29 12:58:06 -04:00
										 |  |  | 		var messagesEl = modalEl.find('.chat-content'); | 
					
						
							| 
									
										
										
										
											2015-02-26 11:03:15 -05:00
										 |  |  | 		messagesEl.css('height', module.calculateChatListHeight(modalEl)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		$(window).on('resize', function () { | 
					
						
							| 
									
										
										
										
											2015-02-26 11:03:15 -05:00
										 |  |  | 			messagesEl.css('height', module.calculateChatListHeight(modalEl)); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.disableMobileBehaviour = function () { | 
					
						
							| 
									
										
										
										
											2015-02-26 11:03:15 -05:00
										 |  |  | 		app.toggleNavbar(true); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.calculateChatListHeight = function (modalEl) { | 
					
						
							| 
									
										
										
										
											2017-12-20 13:47:45 -05:00
										 |  |  | 		// Formula: modal height minus header height. Simple(tm).
 | 
					
						
							|  |  |  | 		return modalEl.find('.modal-content').outerHeight() - modalEl.find('.modal-header').outerHeight(); | 
					
						
							| 
									
										
										
										
											2014-02-26 21:55:29 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-06-18 12:11:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	module.minimize = function (uuid) { | 
					
						
							| 
									
										
										
										
											2014-04-15 12:48:28 -04:00
										 |  |  | 		var chatModal = $('div[UUID="' + uuid + '"]'); | 
					
						
							| 
									
										
										
										
											2014-01-05 22:09:13 -05:00
										 |  |  | 		chatModal.addClass('hide'); | 
					
						
							| 
									
										
										
										
											2013-06-18 12:11:48 -04:00
										 |  |  | 		taskbar.minimize('chat', uuid); | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 		clearInterval(chatModal.attr('intervalId')); | 
					
						
							|  |  |  | 		chatModal.attr('intervalId', 0); | 
					
						
							| 
									
										
										
										
											2014-02-26 21:55:29 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-06-18 12:11:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 14:25:23 -04:00
										 |  |  | 	module.toggleNew = taskbar.toggleNew; | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 14:25:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 11:26:57 -04:00
										 |  |  | 	return module; | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }); |