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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
/**
 * 群组相关
 */
'use strict'

YX.fn.team = function () {
	this.$teamInfo = $('#teamInfo')
	this.$teamInfoContainer = $('#teamInfoContainer')

	//搜索群面板
	this.$searchBox = $('#searchTeamBox')
	this.$searchBox.delegate('.j-close', 'click', this.hideTeamSearch.bind(this))
	this.$searchBox.delegate('.j-search', 'click', this.doTeamSearch.bind(this))
	this.$searchBox.delegate('.j-back', 'click', this.resetTeamSearch.bind(this))
	this.$searchBox.delegate('.j-chat', 'click', this.doTeamChat.bind(this))
	this.$searchBox.delegate('.j-add', 'click', this.doTeamAdd.bind(this))
	// 左侧面板群组中的三个按钮
	$('#createTeam').on('click', this.showTeamView.bind(this, 0, 0))
	$('#createAdvanceTeam').on('click', this.showTeamView.bind(this, 0, 1))
	$('#searchAdvanceTeam').on('click', this.showTeamSearch.bind(this, 0, 1))
	//群资料
	this.$teamInfo.on('click', this.showTeamInfo.bind(this))
	this.$teamInfoContainer.delegate('.j-backBtn', 'click', this.hideTeamInfo.bind(this))
	this.$teamInfoContainer.delegate('#teamMemberList .first', 'click', this.showTeamView.bind(this, 1))
	this.$teamInfoContainer.delegate('#teamMemberList .hover', 'click', this.removeTeamMember.bind(this))
	this.$teamInfoContainer.delegate('#teamName', 'click', this.editTeamInfo)
	this.$teamInfoContainer.delegate('#teamDesc', 'click', this.editTeamInfo)
	this.$teamInfoContainer.delegate('.j-exitTeam', 'click', this.exitTeam.bind(this))
	this.$teamInfoContainer.delegate('.j-dismissTeam', 'click', this.doDismissTeam.bind(this))
	this.$teamInfoContainer.delegate('#teamNameInput', 'blur', this.saveTeamName.bind(this))
	this.$teamInfoContainer.delegate('#teamDescInput', 'blur', this.saveTeamDesc.bind(this))
	this.$teamInfoContainer.delegate('.j-teamAvatar', 'click', this.showModifyAvatar.bind(this, "team"))
	this.$teamInfoContainer.delegate('.j-joinMode', 'change', this.setJoinMode.bind(this))
	this.$teamInfoContainer.delegate('.j-inviteMode', 'change', this.setInviteMode.bind(this))
	this.$teamInfoContainer.delegate('.j-beInviteMode', 'change', this.setBeInviteMode.bind(this))
	this.$teamInfoContainer.delegate('.j-updateTeamMode', 'change', this.setUpdateTeamMode.bind(this))
}
/**
 * 最近联系人显示
 * @return {void}
 */
YX.fn.buildTeams = function (id) {
	var data = {
		teams: this.cache.getTeamlist()
	}
	if (!this.teams) {
		var options = {
			data: data,
			infoprovider: this.infoProvider.bind(this),
			onclickavatar: this.clickTeamAvatar.bind(this),
			onclickitem: this.openChatBox.bind(this)
		}
		this.teams = new NIMUIKit.TeamList(options)
		this.teams.inject($('#teams').get(0))
	} else {
		this.teams.update(data)
	}
	this.doPoint()
}

/**
 * 主动去拿群列表
 */
YX.fn.getTeamMembers = function (id, callback) {
	if (!(callback instanceof Function)) {
		callback = function () {}
	}
 	var that = this
	this.mysdk.getTeamMembers(id, function (err, obj) {
		if (!err) {
			that.cache.setTeamMembers(id, obj)
			callback()
		} else {
			alert(err.message)
		}
	})
}
/**
 * 点击群组列表头像
 * demo里跟点击列表处理一致了
 */
YX.fn.clickTeamAvatar = function (account, type) {
	$("#teams").find("li.active").removeClass("active")
	this.openChatBox(account, type)
}
/**
 * 联系人选择界面展示
 * @param  {int} type 0为新建群添加成员,1为在已有群的情况下添加成员
 * @param  {int} teamType 高级群1 普通群 0
 * @return {void}      
 */
YX.fn.showTeamView = function (type, teamType) {
	var that = this
	this.teamType = teamType
	var list = that.cache.getFriendslistOnShow()

	if (type !== 0) {
		var teamId = that.crtSessionAccount
		that.mysdk.getTeamMembers(
			teamId,
			function (error, obj) {
				//给已经在群的好友标记
				var members = error ? [] : obj.members

				//开启弹框 
				that.dialog.open({ list: list, cbConfirm: that.createTeam, env: that, yx: that, selectedlist: members })
			}
		)
		//用来区分是否创群
		that.addTeamMemberTag = true
	} else {
		//开启弹框
		that.dialog.open({ list: list, cbConfirm: that.createTeam, env: that, yx: that })
		that.addTeamMemberTag = false
	}

}

/**
 * 
 * 
 */
/**
 * 回调:创建群
 * @param {object} list 已选中的人的列表
 */
YX.fn.createTeam = function (list) {
	var accounts = [],
		names = [],
		teamId = this.addTeamMemberTag && this.crtSessionAccount,
		that = this

	for (var i in list) {
		accounts.push(i)
		names.push(list[i])
	}

	// 如果存在群id,则为新添加成员
	if (!!teamId) {
		this.mysdk.addTeamMembers({
			teamId: teamId,
			accounts: accounts,
			ps: '',
			done: function (error, params) {
				if (error) { alert('添加成员失败') }
			}
		})
		this.$teamInfoContainer.addClass('hide')
		return;
	}

	// 创建普通群
	var owner = this.cache.getUserById(userUID).nick
	names = [owner].concat(names).join('、').slice(0, 20)
	if (that.teamType === 0) {
		this.mysdk.createTeam({
			type: 'normal',
			name: names + '等人',
			accounts: accounts,
			done: function (error, t) {
				if (!error) {
					that.cache.addTeam(t.team)
					that.buildTeams()
					$('#teamsWrap .j-normalTeam li[data-account="' + t.team.teamId + '"]').click()

				} else {
					alert("创建失败")
				}
			}
		})
	} else {
		this.mysdk.createTeam({
			type: 'advanced',
			name: names + '等人',
			accounts: accounts,
			joinMode: 'needVerify',
			done: function (error, t) {
				if (!error) {
					that.cache.addTeam(t.team)
					that.buildTeams()
					$('#teamsWrap .j-advanceTeam li[data-account="' + t.team.teamId + '"]').click()

				} else {
					alert(error.message)
				}
			}
		})
	}

	this.$teamInfoContainer.addClass('hide')
}
/***************************
 * 搜索高级群
 ***************************/
YX.fn.showTeamSearch = function () {
	this.searchData = null
	this.$searchBox.removeClass("hide")
	this.$mask.removeClass('hide')
	this.$searchBox.find(".j-account").focus()
}
YX.fn.hideTeamSearch = function () {
	this.resetTeamSearch()
	this.$searchBox.addClass("hide")
	this.$mask.addClass('hide')
}
YX.fn.doTeamSearch = function () {
	var account = $.trim(this.$searchBox.find(".j-account").val())
	if (/^\d+$/.test(account)) {
		this.mysdk.getTeam(account, this.cbDoTeamSearch.bind(this))
	} else {
		alert("输入有误(群ID必须是数字)")
	}
}
YX.fn.cbDoTeamSearch = function (err, data) {
	if (err) {
		alert(err.message)
	} else {
		if (data.type === "normal" || data.valid === false) {
			alert("群不存在")
			return
		}
		var $info = this.$searchBox.find(".info")
		var teamId = data.teamId
		$info.find(".j-name").html(data.name)
		$info.find(".j-teamId").html(data.teamId)
		if (this.cache.hasTeam(teamId)) {
			this.$searchBox.addClass("inTeam")
		} else {
			this.$searchBox.addClass("notInTeam")
		}
	}
}
YX.fn.resetTeamSearch = function () {
	this.$searchBox.attr('class', "m-dialog")
	this.$searchBox.find(".j-account").val("")
}
YX.fn.doTeamChat = function () {
	var account = $.trim(this.$searchBox.find(".j-account").val())
	this.openChatBox(account, "team")
	this.hideTeamSearch()
}
YX.fn.doTeamAdd = function () {
	var account = $.trim(this.$searchBox.find(".j-account").val())
	this.mysdk.applyTeam(account)
}
/********************************************************************
 * 群资料
********************************************************************/
YX.fn.showTeamInfo = function () {
	var that = this
	this.$teamInfoContainer.load('./teamInfo.html', function () {
		// 获取群成员
		var teamId = that.crtSessionAccount,
			teamInfo = that.cache.getTeamById(teamId)
		if (!teamInfo) {
			return
		}
		var type = teamInfo.type,
			teamName = teamInfo.name,
			teamOwner = teamInfo.owner,
			intro = teamInfo.intro || "",
			joinMode = teamInfo.joinMode || "",
			beInviteMode = teamInfo.beInviteMode || "",
			inviteMode = teamInfo.inviteMode || "",
			updateTeamMode = teamInfo.updateTeamMode || "",
			myTeamInfo,
			html = '',
			$userList = $('#teamMemberList'),
			$teamId = that.$teamInfoContainer.find(".j-teamId"),
			$teamAvatar = that.$teamInfoContainer.find(".j-teamAvatar"),
			$teamName = $('#teamName'),
			$teamDesc = $('#teamDesc')
		var members;
		// 高级群
		var showRole = function () {
			myTeamInfo = that.cache.getTeamMemberInfo(userUID, teamId)
			if (type === "advanced") {
				//其本信息
				that.$teamInfoContainer.removeClass("normal").find('.j-advanced').removeClass("hide")
				$teamDesc.find('.name').text(intro)
				var avatarUrl = teamInfo.avatar ? (teamInfo.avatar + "?imageView&thumbnail=40y40") : "images/advanced.png"
				$teamAvatar[0].src = avatarUrl
				if (myTeamInfo.type === "owner" || (myTeamInfo.type === "manager")) {
					that.$teamInfoContainer.find('.j-joinMode[value=' + joinMode + ']').attr("checked", 'checked')
					that.$teamInfoContainer.find('.j-beInviteMode[value=' + beInviteMode + ']').attr("checked", 'checked')
					that.$teamInfoContainer.find('.j-inviteMode[value=' + inviteMode + ']').attr("checked", 'checked')
					that.$teamInfoContainer.find('.j-updateTeamMode[value=' + updateTeamMode + ']').attr("checked", 'checked')
					$teamName.addClass('owner')
					$teamDesc.addClass('owner')
					$teamAvatar.addClass('active')
				} else {
					that.$teamInfoContainer.find('.j-joinMode').attr("disabled", 'disabled').filter('[value=' + joinMode + ']').attr("checked", 'checked')
					that.$teamInfoContainer.find('.j-beInviteMode').attr("disabled", 'disabled').filter('[value=' + beInviteMode + ']').attr("checked", 'checked')
					that.$teamInfoContainer.find('.j-inviteMode').attr("disabled", 'disabled').filter('[value=' + inviteMode + ']').attr("checked", 'checked')
					that.$teamInfoContainer.find('.j-updateTeamMode').attr("disabled", 'disabled').filter('[value=' + updateTeamMode + ']').attr("checked", 'checked')
				}
				if (userUID === teamOwner) {
					that.$teamInfoContainer.find('.j-dismissTeam').removeClass("hide")
					that.$teamInfoContainer.find('.j-exitTeam').addClass("hide")
				}
				if (updateTeamMode === "all") {
					$teamName.addClass('owner')
					$teamDesc.addClass('owner')
					$teamAvatar.addClass('active')
				}
			} else {
				//讨论组
				that.$teamInfoContainer.addClass("normal").find('.j-exitTeam').text("退出讨论组")
				$teamName.addClass('owner')
				$teamDesc.addClass('owner')
				$teamAvatar[0].src = "images/normal.png"
			}
		}
		$teamName.find('.name').text(teamName)
		$teamId.text(teamId)
		var showMember = function () {
			members = that.cache.getTeamMembers(teamId).members
			var array = []
			for (var i = 0; i < members.length; i++) {
				if (!that.cache.getUserById(members[i].account)) {
					array.push(members[i].account)
				}
			}
			if (array.length > 0) {
				getInfo(array)
			} else {
				showUI()
			}
		}
		var showUI = function () {
			showRole()
			that.sortTeamMembers(members)
			if (type === 'normal' || myTeamInfo.type !== "normal" || inviteMode === "all") { // 是群主
				html += '<li class="first add-item tc radius-circle" data-team-type="' + type + '" data-team-id="' + teamId + '"><i class="icon icon-plus"></i><p></p></li>'
			}
			for (var i = 0, l = members.length; i < l; ++i) {
				var member = members[i],
					account = member.account,
					avatar = getAvatar(that.cache.getUserById(account) ? that.cache.getUserById(account).avatar : ""),
					nick = getNick(account)
				html += '<li data-account="' + account + '"><a href="javascript:"><img src="' + avatar + '"/>'
				if (member.type === 'owner') {
					html += '<i class="icon radius-circle icon-user"></i>'
				} else {
					html += '<span class="hover" data-nick="' + nick + '" data-team-name="' + teamName + '" data-account="' + account + '" data-team-id="' + teamId + '">移除</span>'
				}
				html += '</a><p class="text">' + nick + '</p></li>'
			}
			$userList.html(html)
			if (myTeamInfo.type !== "normal") {
				$userList.addClass('owner')
			}
		}
		var getInfo = function (array) {
			that.mysdk.getUsers(array, function (err, data) {
				if (!err) {
					for (var j = 0; j < data.length; j++) {
						that.cache.updatePersonlist(data[j])
					}
					showUI()
				} else {
					alert("获取用户信息失败")
				}
			})
		}
		that.getTeamMembers(teamId, showMember)
		that.$teamInfoContainer.removeClass('hide')

		/** 通话中的设置 */
		var tmp = that.myNetcall;
		tmp.$goNetcall.toggleClass("hide", !tmp.netcallActive);
	})
}
YX.fn.sortTeamMembers = function (members) {
	if (!members || !members.length) return;
	members.sort(function (x, y) {
		if (x.type === "owner") {
			return -1
		} else if (y.type === "owner") {
			return 1
		} else if (x.type === "manager") {
			return -1
		} else if (y.type === "manager") {
			return 1
		} else {
			return 0
		}
	})
}
YX.fn.hideTeamInfo = function () {
	this.$teamInfoContainer.addClass('hide')
	this.$mask.addClass('hide')

	/** 通话中的设置 */
	var tmp = this.myNetcall;
	tmp.$goNetcall.toggleClass("hide", true);
}
YX.fn.removeTeamMember = function (event) {
	var ev = event || window.event,
		$target = $(ev.target),
		that = this,
		account = $target.attr('data-account'),
		teamId = $target.data('team-id'),
		teamType = $('#teamMemberList .first').data('team-type')
	this.mysdk.removeTeamMembers({
		teamId: teamId,
		accounts: [account],
		done: function (error, params) {
			if (error) {
				alert('移除成员失败')
			} else {
				$target.parents('li').remove()
			}

		}
	})
}

YX.fn.editTeamInfo = function () {
	var $this = $(this)
	if ($this.hasClass('owner')) {
		$this.parent('.wrap').addClass('active')
		$this.parent('.wrap').find("input").focus()
	}
}

YX.fn.saveTeamName = function () {
	var $input = $('#teamNameInput'),
		name = $input.val().trim(),
		that = this,
		teamId = this.crtSessionAccount
	if (name.length > 0) {
		this.mysdk.updateTeam({
			teamId: teamId,
			name: name,
			done: function (error, params) {
				if (!error) {
					var name = params.name
					$('#teamName .name').text(name)
					$input.val('').parents('.wrap').removeClass('active')
				} else {
					alert('群名称修改失败')
				}
			}
		})
	} else {
		$input.parents('.wrap').removeClass('active')
	}
}

YX.fn.saveTeamDesc = function () {
	var $input = $('#teamDescInput'),
		name = $input.val().trim(),
		that = this,
		teamId = this.crtSessionAccount
	if (name.length > 0) {
		this.mysdk.updateTeam({
			teamId: teamId,
			intro: name,
			done: function (error, params) {
				if (!error) {
					var name = params.intro
					$('#teamDesc .name').text(name)
					$input.val('').parents('.wrap').removeClass('active')
				} else {
					alert('修改群介绍失败')
				}
			}
		})
	} else {
		$input.parents('.wrap').removeClass('active')
	}
}
YX.fn.setJoinMode = function (evt) {
	var joinMode = $(evt.target).val(),
		teamId = this.crtSessionAccount
	this.mysdk.updateTeam({
		teamId: teamId,
		joinMode: joinMode,
		done: function (error, params) {
			if (!error) {
			} else {
				alert(error.message)
			}
		}
	})
}
YX.fn.setInviteMode = function (evt) {
	var inviteMode = $(evt.target).val(),
		teamId = this.crtSessionAccount
	this.mysdk.updateTeam({
		teamId: teamId,
		inviteMode: inviteMode,
		done: function (error, params) {
			if (!error) {
			} else {
				alert(error.message)
			}
		}
	})
}
YX.fn.setBeInviteMode = function (evt) {
	var beInviteMode = $(evt.target).val(),
		teamId = this.crtSessionAccount
	this.mysdk.updateTeam({
		teamId: teamId,
		beInviteMode: beInviteMode,
		done: function (error, params) {
			if (!error) {
			} else {
				alert(error.message)
			}
		}
	})
}
YX.fn.setUpdateTeamMode = function (evt) {
	var updateTeamMode = $(evt.target).val(),
		teamId = this.crtSessionAccount
	this.mysdk.updateTeam({
		teamId: teamId,
		updateTeamMode: updateTeamMode,
		done: function (error, params) {
			if (!error) {
			} else {
				alert(error.message)
			}
		}
	})
}
// 普通群(任何人)/高级群(非群主),退出群
YX.fn.exitTeam = function () {
	var that = this,
		teamId = this.crtSessionAccount
	this.mysdk.leaveTeam({
		teamId: teamId,
		done: function (error, params) {
			if (!error) {
				that.$teamInfoContainer.addClass('hide')
				that.$rightPanel.addClass('hide')
				removeChatVernier(teamId)
			} else {
				alert(error.message)
			}
		}
	})
}
//高级群解散
YX.fn.doDismissTeam = function () {
	var that = this,
		teamId = this.crtSessionAccount
	this.mysdk.dismissTeam({
		teamId: teamId,
		done: function (error, params) {
			if (!error) {
				that.$teamInfoContainer.addClass('hide')
				that.$rightPanel.addClass('hide')
				removeChatVernier(teamId)
			} else {
				alert(error.message)
			}
		}
	});
}