CommunitiesList.lua 32.3 KB
Newer Older
TOM_RUS's avatar
TOM_RUS committed
1 2 3
local COMMUNITIES_LIST_EVENTS = {
	"CLUB_ADDED",
	"CLUB_REMOVED",
TOM_RUS's avatar
TOM_RUS committed
4
	"CLUB_UPDATED",
TOM_RUS's avatar
TOM_RUS committed
5 6
	"CLUB_INVITATION_ADDED_FOR_SELF",
	"CLUB_INVITATION_REMOVED_FOR_SELF",
TOM_RUS's avatar
TOM_RUS committed
7 8
	"CLUB_FINDER_PLAYER_PENDING_LIST_RECIEVED",
	"GUILD_ROSTER_UPDATE",
TOM_RUS's avatar
TOM_RUS committed
9
	"CLUB_FINDER_APPLICANT_INVITE_RECIEVED",
TOM_RUS's avatar
TOM_RUS committed
10 11 12 13
};
	
local COMMUNITIES_LIST_INITLAL_TOP_BORDER_OFFSET = -28;
local COMMUNITIES_LIST_INITLAL_BOTTOM_BORDER_OFFSET = 40;
TOM_RUS's avatar
TOM_RUS committed
14
local COMMUNITIES_LIST_GUILD_FINDER_OFFSET = 28;
TOM_RUS's avatar
TOM_RUS committed
15

TOM_RUS's avatar
TOM_RUS committed
16 17
local NEW_COMMUNITY_FLASH_DURATION = 6.0;

TOM_RUS's avatar
TOM_RUS committed
18 19 20 21 22 23 24 25
CommunitiesListMixin = {};

function CommunitiesListMixin:GetCommunitiesFrame()
	return self:GetParent();
end

function CommunitiesListMixin:OnEvent(event, ...)
	if event == "CLUB_ADDED" then
TOM_RUS's avatar
TOM_RUS committed
26
		self:UpdateCommunitiesList();
TOM_RUS's avatar
TOM_RUS committed
27 28
		self:Update();
	elseif event == "CLUB_REMOVED" then
TOM_RUS's avatar
TOM_RUS committed
29
		self:UpdateCommunitiesList();
TOM_RUS's avatar
TOM_RUS committed
30
		self:Update();
TOM_RUS's avatar
TOM_RUS committed
31 32 33 34 35 36
	elseif event == "CLUB_UPDATED" then
		local clubId = ...;
		local clubInfo = C_Club.GetClubInfo(clubId);
		if clubInfo then
			self:UpdateClub(clubInfo);
		end
TOM_RUS's avatar
TOM_RUS committed
37
	elseif event == "CLUB_INVITATION_ADDED_FOR_SELF" then
TOM_RUS's avatar
TOM_RUS committed
38
		self:UpdateInvitations();
TOM_RUS's avatar
TOM_RUS committed
39 40 41 42
		self:Update();
	elseif event == "CLUB_INVITATION_REMOVED_FOR_SELF" then
		local invitationId = ...;
		tDeleteItem(self.declinedInvitationIds, invitationId);
TOM_RUS's avatar
TOM_RUS committed
43
		self:UpdateInvitations();
TOM_RUS's avatar
TOM_RUS committed
44
		self:Update();
TOM_RUS's avatar
TOM_RUS committed
45
	elseif event == "CLUB_FINDER_PLAYER_PENDING_LIST_RECIEVED" or event == "CLUB_FINDER_APPLICANT_INVITE_RECIEVED" then
TOM_RUS's avatar
TOM_RUS committed
46 47 48
		self:UpdateFinderInvitations(); 
		self:Update(); 
	elseif event == "GUILD_ROSTER_UPDATE" then 
TOM_RUS's avatar
TOM_RUS committed
49
		self:UpdateCommunitiesList();
TOM_RUS's avatar
TOM_RUS committed
50
		self:Update();
TOM_RUS's avatar
TOM_RUS committed
51 52 53
	end
end

TOM_RUS's avatar
TOM_RUS committed
54 55
function CommunitiesListMixin:UpdateInvitations()
	self.invitations = C_Club.GetInvitationsForSelf();
TOM_RUS's avatar
TOM_RUS committed
56 57 58
	
	-- Remove all invites that have been declined.
	for i, declinedInvitationId in ipairs(self.declinedInvitationIds) do
TOM_RUS's avatar
TOM_RUS committed
59
		for j, inviteInfo in ipairs(self.invitations) do
TOM_RUS's avatar
TOM_RUS committed
60
			if declinedInvitationId == inviteInfo.invitationId then
TOM_RUS's avatar
TOM_RUS committed
61
				table.remove(self.invitations, j);
TOM_RUS's avatar
TOM_RUS committed
62 63 64 65
				break;
			end
		end
	end
TOM_RUS's avatar
TOM_RUS committed
66 67
end

TOM_RUS's avatar
TOM_RUS committed
68 69 70 71 72 73 74 75
function CommunitiesListMixin:UpdateFinderInvitations()
	self.finderInvitations = C_ClubFinder.PlayerGetClubInvitationList();
end 

function CommunitiesListMixin:GetClubFinderInvitations()
	return self.finderInvitations;
end

TOM_RUS's avatar
TOM_RUS committed
76 77
function CommunitiesListMixin:GetInvitations()
	return self.invitations;
TOM_RUS's avatar
TOM_RUS committed
78 79
end

TOM_RUS's avatar
TOM_RUS committed
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
function CommunitiesListMixin:GetTickets()
	return self.tickets or {};
end

function CommunitiesListMixin:GetTicketInfoForClubId(clubId)
	for i, ticketInfo in ipairs(self:GetTickets()) do
		if ticketInfo.clubInfo.clubId == clubId then
			return ticketInfo;
		end
	end

	return nil;
end

function CommunitiesListMixin:AlreadyInClubOrHaveInvitation(clubId)
	local communities = self:GetCommunitiesList();
	local invitations = self:GetInvitations();

	for _, community in ipairs(communities) do
		if community.clubId == clubId then
			return true;
		end
	end
	for _, invitation in ipairs(invitations) do
		if invitation.club.clubId == clubId then
			return true;
		end
	end

	return false;
end

function CommunitiesListMixin:AlreadyHaveTicket(ticketId)
	local tickets = self:GetTickets();

	for _, ticketInfo in ipairs(tickets) do
		if ticketInfo.ticketId == ticketId then
			return true;
		end
	end

	return false;
end

function CommunitiesListMixin:ValidateTickets()
	-- Remove any tickets for clubs that we're already in or have an invite for
	local tickets = self:GetTickets();
	for i = #tickets, 1, -1 do
		local ticket = tickets[i];
		if self:AlreadyInClubOrHaveInvitation(ticket.clubInfo.clubId) then
			table.remove(tickets, i);
		end
	end
end

function CommunitiesListMixin:AddTicket(ticketId, clubInfo)
TOM_RUS's avatar
TOM_RUS committed
136
	ShowUIPanel(CommunitiesFrame);
TOM_RUS's avatar
TOM_RUS committed
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
	if self:AlreadyHaveTicket(ticketId) then
		return;
	end
	if not self:AlreadyInClubOrHaveInvitation(clubInfo.clubId) then
		local ticketInfo = {};
		ticketInfo.ticketId = ticketId;
		ticketInfo.clubInfo = clubInfo;

		if self.tickets == nil then
			self.tickets = {};
		end
		table.insert(self.tickets, ticketInfo);
	end

	local forceUpdate = true;
	local communitiesFrame = self:GetParent();
	communitiesFrame:SelectClub(clubInfo.clubId, forceUpdate)
end

function CommunitiesListMixin:RemoveTicket(ticketId)
	for i, ticketInfo in ipairs(self.tickets) do
		if ticketInfo.ticketId == ticketId then
			table.remove(self.tickets, i);
			self:Update();
			return;
		end
	end
end

function CommunitiesListMixin:ClearTickets(ticketId)
	self.ticket = nil;
end

TOM_RUS's avatar
TOM_RUS committed
170 171 172 173 174 175 176 177 178 179 180
function CommunitiesListMixin:GetFirstMatchingClubEntry(predicate)
	local buttons = self.ListScrollFrame.buttons;
	for i, button in ipairs(buttons) do
		if predicate(button) then
			return button;
		end
	end
	
	return nil;
end

TOM_RUS's avatar
TOM_RUS committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
function CommunitiesListMixin:SortCommunitiesList()
	CommunitiesUtil.SortClubs(self:GetCommunitiesList());
end

function CommunitiesListMixin:UpdateCommunitiesList()
	local clubs = C_Club.GetSubscribedClubs();
	self.communitiesList = clubs;
	self:PredictFavorites(clubs);
	self:SortCommunitiesList();
end

function CommunitiesListMixin:GetCommunitiesList()
	return self.communitiesList;
end

TOM_RUS's avatar
TOM_RUS committed
196 197 198 199
function CommunitiesListMixin:Update()
	local scrollFrame = self.ListScrollFrame;
	local offset = HybridScrollFrame_GetOffset(scrollFrame);
	local buttons = scrollFrame.buttons;
TOM_RUS's avatar
TOM_RUS committed
200 201
	
	local selectedClubId = self:GetCommunitiesFrame():GetSelectedClubId();
TOM_RUS's avatar
TOM_RUS committed
202
	local clubs = self:GetCommunitiesList();
TOM_RUS's avatar
TOM_RUS committed
203
	self:ValidateTickets();
TOM_RUS's avatar
TOM_RUS committed
204
	local isInGuild = IsInGuild();
TOM_RUS's avatar
TOM_RUS committed
205
	local invitations = self:GetInvitations();
TOM_RUS's avatar
TOM_RUS committed
206
	local clubFinderInvitations = self:GetClubFinderInvitations(); 
TOM_RUS's avatar
TOM_RUS committed
207
	local clubFinderEnabled = C_ClubFinder.IsEnabled();
TOM_RUS's avatar
TOM_RUS committed
208
	local numFinderInvitations = 0; 
TOM_RUS's avatar
TOM_RUS committed
209 210
	
	if	(clubFinderInvitations and clubFinderEnabled) then 
TOM_RUS's avatar
TOM_RUS committed
211 212 213
		numFinderInvitations = #clubFinderInvitations
	end 

TOM_RUS's avatar
TOM_RUS committed
214
	local tickets = self:GetTickets();
TOM_RUS's avatar
TOM_RUS committed
215
	local totalNumClubs = numFinderInvitations + #invitations + #tickets + #clubs;
TOM_RUS's avatar
TOM_RUS committed
216 217

	if not isInGuild and clubFinderEnabled then
TOM_RUS's avatar
TOM_RUS committed
218 219
		totalNumClubs = totalNumClubs + 1;
	end
TOM_RUS's avatar
TOM_RUS committed
220

TOM_RUS's avatar
TOM_RUS committed
221
	local height = buttons[1]:GetHeight();
TOM_RUS's avatar
TOM_RUS committed
222 223 224
	-- TODO:: Determine if this player is at the maximum number of allowed clubs or not.
	-- We probably need to change the create flow as well, since it's possible you are
	-- allowed to create more bnet groups, but not more wow communities or vice versa.
TOM_RUS's avatar
TOM_RUS committed
225
	local shouldAddJoinCommunityEntry = C_Club.ShouldAllowClubType(Enum.ClubType.Character) or C_Club.ShouldAllowClubType(Enum.ClubType.BattleNet); 
TOM_RUS's avatar
TOM_RUS committed
226
	local shouldFindCommunityEntry = clubFinderEnabled;
TOM_RUS's avatar
TOM_RUS committed
227 228 229 230 231
	
	-- We need 1 for the blank entry at the top of the list.
	local clubsHeight = height * (totalNumClubs + 1);
	if shouldAddJoinCommunityEntry then
		clubsHeight = clubsHeight + height;
TOM_RUS's avatar
TOM_RUS committed
232 233 234
		if(shouldFindCommunityEntry) then 
			clubsHeight = clubsHeight + height; 
		end
TOM_RUS's avatar
TOM_RUS committed
235 236
	end
	
TOM_RUS's avatar
TOM_RUS committed
237
	local usedHeight = height;
TOM_RUS's avatar
TOM_RUS committed
238
	local shownGuildFinderButton = false; 
TOM_RUS's avatar
TOM_RUS committed
239 240 241 242 243 244 245 246 247 248 249
	for i=1, #buttons do
		local button = buttons[i];
		local displayIndex = i + offset;

		-- We leave a space at the top of the scroll frame. This is accomplished most easily with a blank entry.
		if displayIndex == 1 then
			buttons[displayIndex]:SetClubInfo(nil);
			buttons[displayIndex]:Hide();
		else
			displayIndex = displayIndex - 1;
			local clubInfo = nil;
TOM_RUS's avatar
TOM_RUS committed
250 251
			local isTicket = displayIndex <= #tickets;
			local isInvitation = displayIndex > #tickets and displayIndex <= #tickets + #invitations;
TOM_RUS's avatar
TOM_RUS committed
252
			local isClubFinderInvitation = displayIndex > #tickets + #invitations and displayIndex <= numFinderInvitations + #tickets + #invitations; 
TOM_RUS's avatar
TOM_RUS committed
253 254 255 256 257

			if isTicket then
				clubInfo = tickets[displayIndex].clubInfo;
			elseif isInvitation then
				displayIndex = displayIndex - #tickets;
TOM_RUS's avatar
TOM_RUS committed
258
				clubInfo = invitations[displayIndex].club;
TOM_RUS's avatar
TOM_RUS committed
259 260 261
			elseif isClubFinderInvitation then 
				displayIndex = displayIndex - #tickets - #invitations; 
				clubInfo = clubFinderInvitations[displayIndex];
TOM_RUS's avatar
TOM_RUS committed
262
			else
TOM_RUS's avatar
TOM_RUS committed
263
				displayIndex = displayIndex - #tickets - #invitations - numFinderInvitations;
TOM_RUS's avatar
TOM_RUS committed
264
				if not isInGuild and clubFinderEnabled then
TOM_RUS's avatar
TOM_RUS committed
265 266 267 268 269 270
					displayIndex = displayIndex - 1;
				end
				if displayIndex > 0 and displayIndex <= #clubs then
					clubInfo = clubs[displayIndex];
				end
			end
TOM_RUS's avatar
TOM_RUS committed
271
			if (not isInGuild and displayIndex == 0 and clubFinderEnabled) then
TOM_RUS's avatar
TOM_RUS committed
272
				button:SetGuildFinder();
TOM_RUS's avatar
TOM_RUS committed
273 274 275
				local communitiesFrame = self:GetCommunitiesFrame();
				communitiesFrame.GuildFinderFrame.isGuildType = true;
				communitiesFrame.GuildFinderFrame:UpdateType(); 
TOM_RUS's avatar
TOM_RUS committed
276
				button:Show();
TOM_RUS's avatar
TOM_RUS committed
277
				shownGuildFinderButton = true;
TOM_RUS's avatar
TOM_RUS committed
278 279
				usedHeight = usedHeight + height;
			elseif clubInfo then
TOM_RUS's avatar
TOM_RUS committed
280
				button:SetClubInfo(clubInfo, isInvitation, isTicket, isClubFinderInvitation);
TOM_RUS's avatar
TOM_RUS committed
281 282
				button:Show();
				usedHeight = usedHeight + height;
TOM_RUS's avatar
TOM_RUS committed
283 284
			elseif shouldFindCommunityEntry then
				button:SetFindCommunity(); 
TOM_RUS's avatar
TOM_RUS committed
285 286
				button:Show();
				usedHeight = usedHeight + height;
TOM_RUS's avatar
TOM_RUS committed
287 288 289 290 291
				shouldFindCommunityEntry = false;
			elseif shouldAddJoinCommunityEntry then 
				button:SetAddCommunity();
				button:Show(); 
				usedHeight = usedHeight + height; 
TOM_RUS's avatar
TOM_RUS committed
292
				shouldAddJoinCommunityEntry = false;
TOM_RUS's avatar
TOM_RUS committed
293
			elseif isInGuild and not shownGuildFinderButton  and clubFinderEnabled then 
TOM_RUS's avatar
TOM_RUS committed
294 295 296 297
				button:SetGuildFinder();
				button:Show();
				shownGuildFinderButton = true;
				usedHeight = usedHeight + height + COMMUNITIES_LIST_GUILD_FINDER_OFFSET;
TOM_RUS's avatar
TOM_RUS committed
298 299 300 301 302 303 304
			else
				button:SetClubInfo(nil);
				button:Hide();
			end
		end
	end
	local totalHeight = clubsHeight + COMMUNITIES_LIST_INITLAL_TOP_BORDER_OFFSET + COMMUNITIES_LIST_INITLAL_BOTTOM_BORDER_OFFSET;
TOM_RUS's avatar
TOM_RUS committed
305

TOM_RUS's avatar
TOM_RUS committed
306
	if (isInGuild and clubFinderEnabled) then 
TOM_RUS's avatar
TOM_RUS committed
307 308 309
		totalHeight = totalHeight + COMMUNITIES_LIST_GUILD_FINDER_OFFSET;
	end 

TOM_RUS's avatar
TOM_RUS committed
310 311 312
	HybridScrollFrame_Update(scrollFrame, totalHeight, usedHeight);
end

TOM_RUS's avatar
TOM_RUS committed
313 314 315 316 317 318 319 320 321 322 323 324
function CommunitiesListMixin:UpdateClubInfo(newClubInfo)
	local clubs = self:GetCommunitiesList();
	if clubs then
		for i, club in ipairs(clubs) do
			if club.clubId == newClubInfo.clubId then
				clubs[i] = newClubInfo;
				break;
			end
		end
	end
end

TOM_RUS's avatar
TOM_RUS committed
325
function CommunitiesListMixin:UpdateClub(clubInfo)
TOM_RUS's avatar
TOM_RUS committed
326 327
	self:UpdateClubInfo(clubInfo);
	
TOM_RUS's avatar
TOM_RUS committed
328 329 330 331 332 333
	local scrollFrame = self.ListScrollFrame;
	local offset = HybridScrollFrame_GetOffset(scrollFrame);
	local buttons = scrollFrame.buttons;
	for i, button in ipairs(buttons) do
		if button:GetClubId() == clubInfo.clubId then
			local isInvitation = false;
TOM_RUS's avatar
TOM_RUS committed
334
			local isTicket = false;
TOM_RUS's avatar
TOM_RUS committed
335 336
			local isClubFinderInvitation = false;
			button:SetClubInfo(clubInfo, isInvitation, isTicket, isClubFinderInvitation);
TOM_RUS's avatar
TOM_RUS committed
337 338 339 340 341
			return;
		end
	end
end

TOM_RUS's avatar
TOM_RUS committed
342 343 344 345
function CommunitiesListMixin:OnLoad()
	self.ListScrollFrame.update = function() 
		self:Update(); 
	end;
TOM_RUS's avatar
TOM_RUS committed
346
	C_ClubFinder.PlayerRequestPendingClubsList(Enum.ClubFinderRequestType.All);
TOM_RUS's avatar
TOM_RUS committed
347 348
	self.ListScrollFrame.ScrollBar.doNotHide = true;
	self.ListScrollFrame.ScrollBar:SetValue(0);
TOM_RUS's avatar
TOM_RUS committed
349 350 351 352 353 354
	
	self.declinedInvitationIds = {};
	self.pendingFavorites = {};
end

function CommunitiesListMixin:RegisterEventCallbacks()
TOM_RUS's avatar
TOM_RUS committed
355
	local communitiesFrame = self:GetCommunitiesFrame();
TOM_RUS's avatar
TOM_RUS committed
356 357 358
	communitiesFrame:RegisterCallback(CommunitiesFrameMixin.Event.InviteDeclined, self.OnCommunityInviteDeclined, self);
	communitiesFrame:RegisterCallback(CommunitiesFrameMixin.Event.DisplayModeChanged, self.OnCommunitiesFrameDisplayModeChanged, self);
end
TOM_RUS's avatar
TOM_RUS committed
359

TOM_RUS's avatar
TOM_RUS committed
360 361 362
function CommunitiesListMixin:OnCommunitiesFrameDisplayModeChanged()
	self:Update();
end
TOM_RUS's avatar
TOM_RUS committed
363

TOM_RUS's avatar
TOM_RUS committed
364
function CommunitiesListMixin:OnCommunityInviteDeclined(invitationId, clubId)
TOM_RUS's avatar
TOM_RUS committed
365
	local communitiesFrame = self:GetCommunitiesFrame(); 
TOM_RUS's avatar
TOM_RUS committed
366
	self.declinedInvitationIds[#self.declinedInvitationIds + 1] = invitationId;
TOM_RUS's avatar
TOM_RUS committed
367
	self:GetCommunitiesFrame():UpdateClubSelection();
TOM_RUS's avatar
TOM_RUS committed
368 369
	self:UpdateInvitations();
	self:Update();
TOM_RUS's avatar
TOM_RUS committed
370 371 372 373
end

function CommunitiesListMixin:OnShow()
	FrameUtil.RegisterFrameForEvents(self, COMMUNITIES_LIST_EVENTS);
TOM_RUS's avatar
TOM_RUS committed
374

TOM_RUS's avatar
TOM_RUS committed
375
	HybridScrollFrame_CreateButtons(self.ListScrollFrame, "CommunitiesListEntryTemplate", 0, -COMMUNITIES_LIST_INITLAL_TOP_BORDER_OFFSET);
TOM_RUS's avatar
TOM_RUS committed
376 377
	self:UpdateCommunitiesList();
	self:UpdateInvitations();
TOM_RUS's avatar
TOM_RUS committed
378 379
	self:Update();
	
TOM_RUS's avatar
TOM_RUS committed
380
	self:RegisterEventCallbacks();
TOM_RUS's avatar
TOM_RUS committed
381 382 383
end

function CommunitiesListMixin:OnHide()
TOM_RUS's avatar
TOM_RUS committed
384 385
	self:GetCommunitiesFrame():UnregisterCallback(CommunitiesFrameMixin.Event.InviteDeclined, self);
	self:GetCommunitiesFrame():UnregisterCallback(CommunitiesFrameMixin.Event.DisplayModeChanged, self);
TOM_RUS's avatar
TOM_RUS committed
386 387 388
	FrameUtil.UnregisterFrameForEvents(self, COMMUNITIES_LIST_EVENTS);
end

TOM_RUS's avatar
TOM_RUS committed
389
function CommunitiesListMixin:ScrollToClub(clubId)
TOM_RUS's avatar
TOM_RUS committed
390 391 392 393 394
	local hasScrollBar = self.ListScrollFrame.totalHeight > self.ListScrollFrame:GetHeight();
	if not hasScrollBar then
		return;
	end
	
TOM_RUS's avatar
TOM_RUS committed
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
	local clubs = self:GetCommunitiesList();
	if clubs ~= nil then
		local clubIndex = nil;
		for i, club in ipairs(clubs) do
			if club.clubId == clubId then
				clubIndex = i;
				break;
			end
		end
		
		if clubIndex ~= nil then
			local invitations = self:GetInvitations();
			local numInvitiations = invitations ~= nil and #invitations or 0;
			clubIndex = clubIndex + numInvitiations;
			
			-- Count the blank entry at the top of the scroll frame.
			clubIndex = clubIndex + 1;
			
			-- Count the guild finder.
			if not IsInGuild() then
				clubIndex = clubIndex + 1;
			end
			
			local buttons = self.ListScrollFrame.buttons;
			local buttonHeight = buttons[1]:GetHeight();
			
TOM_RUS's avatar
TOM_RUS committed
421
			local height = math.max(0, math.floor(buttonHeight * (clubIndex - (#buttons)/2.5)));
TOM_RUS's avatar
TOM_RUS committed
422 423 424 425 426 427
			HybridScrollFrame_SetOffset(self.ListScrollFrame, height);
			self.ListScrollFrame.ScrollBar:SetValue(height);
		end
	end
end

TOM_RUS's avatar
TOM_RUS committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
function CommunitiesListMixin:OnClubSelected(clubId)
	self:Update();
end

function CommunitiesListMixin:SetSelectedEntryForDropDown(entry)
	self.selectedEntryForDropDown = entry;
end

function CommunitiesListMixin:GetSelectedEntryForDropDown()
	return self.selectedEntryForDropDown;
end

function CommunitiesListMixin:SetFavorite(clubId, isFavorite)
	C_Club.SetFavorite(clubId, isFavorite);
	if isFavorite then
		self.pendingFavorites[clubId] = GetServerTime();
	else
		self.pendingFavorites[clubId] = 0;
	end
TOM_RUS's avatar
TOM_RUS committed
447
	self:PredictFavorites(self.communitiesList);
TOM_RUS's avatar
TOM_RUS committed
448
	self:SortCommunitiesList();
TOM_RUS's avatar
TOM_RUS committed
449
	self:Update();
TOM_RUS's avatar
TOM_RUS committed
450
	ChannelFrame:OnCommunityFavoriteChanged(clubId);
TOM_RUS's avatar
TOM_RUS committed
451 452
end

TOM_RUS's avatar
TOM_RUS committed
453 454 455 456 457 458 459 460
function CommunitiesListMixin:IsClubFavorite(clubInfo)
	if self.pendingFavorites[clubInfo.clubId] then
		return self.pendingFavorites[clubInfo.clubId] ~= 0;
	else
		return clubInfo.favoriteTimeStamp ~= nil;
	end
end

TOM_RUS's avatar
TOM_RUS committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
function CommunitiesListMixin:PredictFavorites(clubs)
	local remainingPredictions = {};
	for clubId, predictedFavoriteEntry in pairs(self.pendingFavorites) do
		for i, clubInfo in ipairs(clubs) do
			if clubInfo.clubId == clubId then
				if clubInfo.favoriteTimeStamp ~= predictedFavoriteEntry then
					clubInfo.favoriteTimeStamp = predictedFavoriteEntry ~= 0 and predictedFavoriteEntry or nil;
					remainingPredictions[clubId] = predictedFavoriteEntry;
				end
			end
		end
	end
	
	self.pendingFavorites = remainingPredictions;
end

TOM_RUS's avatar
TOM_RUS committed
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
function CommunitiesListMixin:IsFinderVisible()
	local buttons = self.ListScrollFrame.buttons;
	local numVisibleButtons = #buttons - 1;
	for i = 1, numVisibleButtons do
		local button = buttons[i];
		if button.Name:GetText() == COMMUNITY_FINDER_FIND_COMMUNITY then
			return true;
		end
	end

	return false;
end

function CommunitiesListMixin:OnNewCommunityFlashStarted()
	if not self.newCommunityFlashTime then
		self.newCommunityFlashTime = GetTime();
	end
end

function CommunitiesListMixin:ShouldShowNewCommunityFlash(clubId)
	if self.newCommunityFlashTime and (GetTime() - self.newCommunityFlashTime) > NEW_COMMUNITY_FLASH_DURATION then
		return false;
	end

	return clubId == GuildMicroButton:GetNewClubId();
end

TOM_RUS's avatar
TOM_RUS committed
504
local COMMUNITIES_LIST_ENTRY_EVENTS = {
TOM_RUS's avatar
TOM_RUS committed
505 506
	"STREAM_VIEW_MARKER_UPDATED",
	"PLAYER_GUILD_UPDATE",
TOM_RUS's avatar
TOM_RUS committed
507 508
}

TOM_RUS's avatar
TOM_RUS committed
509 510
CommunitiesListEntryMixin = {};

TOM_RUS's avatar
TOM_RUS committed
511 512 513 514 515 516 517 518 519 520 521 522
local function GetFontColor(isBattleNet, isGuild, isInvitation)
	if isBattleNet then
		return BATTLENET_FONT_COLOR;
	elseif isGuild then
		return GREEN_FONT_COLOR;
	elseif isInvitation then
		return HIGHLIGHT_FONT_COLOR;
	end

	return NORMAL_FONT_COLOR;
end

TOM_RUS's avatar
TOM_RUS committed
523
function CommunitiesListEntryMixin:SetClubInfo(clubInfo, isInvitation, isTicket, isInviteFromFinder)
TOM_RUS's avatar
TOM_RUS committed
524 525
	self:SetEntryEnabled(true);

TOM_RUS's avatar
TOM_RUS committed
526 527 528 529
	if isInvitation then
		self.overrideOnClick = function(self, button)
			if button == "LeftButton" then
				PlaySound(SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON);
TOM_RUS's avatar
TOM_RUS committed
530 531
				local communitiesFrame = self:GetCommunitiesFrame(); 
				communitiesFrame:SelectClub(self.clubId);
TOM_RUS's avatar
TOM_RUS committed
532 533 534 535 536 537
				self:UpdateUnreadNotification();
			end
		end;
	else
		self.overrideOnClick = nil;
	end
TOM_RUS's avatar
TOM_RUS committed
538

TOM_RUS's avatar
TOM_RUS committed
539 540 541 542 543 544 545 546 547 548
	local communitiesList = self:GetCommunitiesList();
	local shouldShowFlash = clubInfo and communitiesList:ShouldShowNewCommunityFlash(clubInfo.clubId);
	local isFlashing = UIFrameIsFlashing(self.NewCommunityFlash);
	if shouldShowFlash and not isFlashing then
		UIFrameFlash(self.NewCommunityFlash, 1.0, 1.0, NEW_COMMUNITY_FLASH_DURATION, false, 0, 0);
		communitiesList:OnNewCommunityFlashStarted();
	elseif not shouldShowFlash and isFlashing then
		UIFrameFlashStop(self.NewCommunityFlash);
	end

TOM_RUS's avatar
TOM_RUS committed
549
	if clubInfo then
TOM_RUS's avatar
TOM_RUS committed
550 551 552 553 554 555 556 557 558 559 560 561 562
		if(isInviteFromFinder) then 
			self.Name:SetText(COMMUNITIES_LIST_INVITATION_DISPLAY:format(clubInfo.name));		
			self.clubInfo = clubInfo;
			self.overrideOnClick = function(self, button)
				if button == "LeftButton" then
					PlaySound(SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON);
					local communitiesFrame = self:GetCommunitiesFrame();
					communitiesFrame:SetDisplayMode(COMMUNITIES_FRAME_DISPLAY_MODES.INVITATION);
					communitiesFrame.ClubFinderInvitationFrame:DisplayInvitation(self.clubInfo);
					communitiesFrame:SelectClub(nil);
				end
			end;

TOM_RUS's avatar
TOM_RUS committed
563
			if clubInfo.isGuild then
TOM_RUS's avatar
TOM_RUS committed
564 565 566 567 568 569 570 571 572 573 574
				self.Background:SetAtlas("communities-nav-button-green-normal");
				self.Background:SetTexCoord(0, 1, 0, 1);
				self.Selection:SetAtlas("communities-nav-button-green-pressed");
				self.Selection:SetTexCoord(0, 1, 0, 1);
			else
				self.Background:SetTexture("Interface\\Common\\bluemenu-main");
				self.Background:SetTexCoord(0.00390625, 0.87890625, 0.75195313, 0.83007813);
				self.Selection:SetTexture("Interface\\Common\\bluemenu-main");
				self.Selection:SetTexCoord(0.00390625, 0.87890625, 0.59179688, 0.66992188);
			end
		
TOM_RUS's avatar
TOM_RUS committed
575 576
			local isBattleNet = false;
			self.Name:SetTextColor(GetFontColor(isBattleNet, clubInfo.isGuild, isInvitation):GetRGB());
TOM_RUS's avatar
TOM_RUS committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
			self.Name:SetPoint("LEFT", self.Icon, "RIGHT", 11, 0);
			self.isInvitation = isInvitation;
			self.isTicket = isTicket;
			self.Selection:SetShown(true);
			self.FavoriteIcon:SetShown(false);
			self.InvitationIcon:SetShown(isInviteFromFinder);
			SetLargeGuildTabardTextures("player", self.GuildTabardEmblem, self.GuildTabardBackground, self.GuildTabardBorder);
			self.GuildTabardEmblem:SetShown(false);
			self.GuildTabardBackground:SetShown(false);
			self.GuildTabardBorder:SetShown(false);
			self.Icon:SetShown(false);
			self.Icon:SetSize(38, 38);
			self.Icon:SetPoint("TOPLEFT", 11, -15);
			self.CircleMask:SetShown(false);
			self.IconRing:SetShown(false);
			return; 
		end 

TOM_RUS's avatar
TOM_RUS committed
595 596 597 598 599 600 601
		if isInvitation then
			self.Name:SetText(COMMUNITIES_LIST_INVITATION_DISPLAY:format(clubInfo.name));
		else
			self.Name:SetText(clubInfo.name);
		end
		
		local isGuild = clubInfo.clubType == Enum.ClubType.Guild;
TOM_RUS's avatar
TOM_RUS committed
602 603 604 605 606 607 608 609 610 611 612 613
		if isGuild then
			self.Background:SetAtlas("communities-nav-button-green-normal");
			self.Background:SetTexCoord(0, 1, 0, 1);
			self.Selection:SetAtlas("communities-nav-button-green-pressed");
			self.Selection:SetTexCoord(0, 1, 0, 1);
		else
			self.Background:SetTexture("Interface\\Common\\bluemenu-main");
			self.Background:SetTexCoord(0.00390625, 0.87890625, 0.75195313, 0.83007813);
			self.Selection:SetTexture("Interface\\Common\\bluemenu-main");
			self.Selection:SetTexCoord(0.00390625, 0.87890625, 0.59179688, 0.66992188);
		end
		
TOM_RUS's avatar
TOM_RUS committed
614 615
		local isBattleNet = clubInfo.clubType == Enum.ClubType.BattleNet;
		self.Name:SetTextColor(GetFontColor(isBattleNet, isGuild, isInvitation):GetRGB());
TOM_RUS's avatar
TOM_RUS committed
616
		self.Name:SetPoint("LEFT", self.Icon, "RIGHT", 11, 0);
TOM_RUS's avatar
TOM_RUS committed
617
		self.clubId = clubInfo.clubId;
TOM_RUS's avatar
TOM_RUS committed
618
		self.isInvitation = isInvitation;
TOM_RUS's avatar
TOM_RUS committed
619
		self.isTicket = isTicket;
TOM_RUS's avatar
TOM_RUS committed
620
		self.Selection:SetShown(clubInfo.clubId == self:GetCommunitiesFrame():GetSelectedClubId());
TOM_RUS's avatar
TOM_RUS committed
621
		self.FavoriteIcon:SetShown(self:GetCommunitiesFrame().CommunitiesList:IsClubFavorite(clubInfo));
TOM_RUS's avatar
TOM_RUS committed
622
		self.InvitationIcon:SetShown(isInvitation or isTicket);
TOM_RUS's avatar
TOM_RUS committed
623 624 625 626
		SetLargeGuildTabardTextures("player", self.GuildTabardEmblem, self.GuildTabardBackground, self.GuildTabardBorder);
		self.GuildTabardEmblem:SetShown(isGuild);
		self.GuildTabardBackground:SetShown(isGuild);
		self.GuildTabardBorder:SetShown(isGuild);
TOM_RUS's avatar
TOM_RUS committed
627
		self.Icon:SetShown(not isInvitation and not isGuild and not isTicket);
TOM_RUS's avatar
TOM_RUS committed
628 629
		self.Icon:SetSize(38, 38);
		self.Icon:SetPoint("TOPLEFT", 11, -15);
TOM_RUS's avatar
TOM_RUS committed
630
		self.CircleMask:SetShown(not isInvitation and not isGuild);
TOM_RUS's avatar
TOM_RUS committed
631
		self.IconRing:SetShown(not isInvitation and not isGuild and not isTicket);
TOM_RUS's avatar
TOM_RUS committed
632
		self.IconRing:SetAtlas(isBattleNet and "communities-ring-blue" or "communities-ring-gold");
TOM_RUS's avatar
TOM_RUS committed
633
		C_Club.SetAvatarTexture(self.Icon, clubInfo.avatarId, clubInfo.clubType);
TOM_RUS's avatar
TOM_RUS committed
634
		self:UpdateUnreadNotification();
TOM_RUS's avatar
TOM_RUS committed
635 636 637 638 639 640 641 642 643 644
	else
		self.Name:SetText(nil);
		self.clubId = nil;
		self.Selection:Hide();
		self.Icon:SetTexture(nil);
		self.UnreadNotificationIcon:Hide();
		self:Hide();
	end
end

TOM_RUS's avatar
TOM_RUS committed
645
function CommunitiesListEntryMixin:UpdateUnreadNotification()
TOM_RUS's avatar
TOM_RUS committed
646 647 648
	local isNewInvitation = self.isInvitation and not DISPLAYED_COMMUNITIES_INVITATIONS[self.clubId];
	local hasUnread = not self.isTicket and self.clubId and CommunitiesUtil.DoesCommunityHaveUnreadMessages(self.clubId);
	self.UnreadNotificationIcon:SetShown(isNewInvitation or hasUnread);
TOM_RUS's avatar
TOM_RUS committed
649 650
end

TOM_RUS's avatar
TOM_RUS committed
651 652 653 654 655 656 657 658 659 660
function CommunitiesListEntryMixin:CheckForDisabledReason(clubType)
	local disabledReason = C_ClubFinder.GetClubFinderDisableReason();
	local disabled = disabledReason or not C_Club.ShouldAllowClubType(clubType);
	self:SetEntryEnabled(not disabled);

	if disabled then
		if disabledReason == Enum.ClubFinderDisableReason.Muted then
			self:SetDisabledTooltip(COMMUNITY_FEATURE_UNAVAILABLE_MUTED);
		elseif disabledReason == Enum.ClubFinderDisableReason.Silenced then
			self:SetDisabledTooltip(COMMUNITY_FEATURE_UNAVAILABLE_SILENCED);
TOM_RUS's avatar
TOM_RUS committed
661 662
		elseif disabledReason == Enum.ClubFinderDisableReason.VeteranTrial then 
			self:SetDisabledTooltip(CLUB_FINDER_DISABLE_REASON_VETERAN_TRIAL);
TOM_RUS's avatar
TOM_RUS committed
663 664 665 666 667 668 669 670 671 672
		else
			self:SetDisabledTooltip(COMMUNITY_TYPE_UNAVAILABLE);
		end
	else
		self:SetDisabledTooltip(nil);
	end

	return disabled;
end

TOM_RUS's avatar
TOM_RUS committed
673
function CommunitiesListEntryMixin:SetFindCommunity()
TOM_RUS's avatar
TOM_RUS committed
674 675
	local disabled = self:CheckForDisabledReason(Enum.ClubType.Character);

TOM_RUS's avatar
TOM_RUS committed
676 677
	self.overrideOnClick = function()
		PlaySound(SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON);
TOM_RUS's avatar
TOM_RUS committed
678
		local communitiesFrame = self:GetCommunitiesFrame();
TOM_RUS's avatar
TOM_RUS committed
679
		communitiesFrame:SelectClub(nil);
TOM_RUS's avatar
TOM_RUS committed
680 681 682
		communitiesFrame:SetDisplayMode(COMMUNITIES_FRAME_DISPLAY_MODES.COMMUNITY_FINDER);

		communitiesFrame.CommunityFinderFrame.isGuildType = false;
TOM_RUS's avatar
TOM_RUS committed
683
		communitiesFrame.CommunityFinderFrame.selectedTab = 1; 
TOM_RUS's avatar
TOM_RUS committed
684
		communitiesFrame.CommunityFinderFrame:UpdateType(); 
TOM_RUS's avatar
TOM_RUS committed
685 686 687 688
	end;
	
	self.clubId = nil;
	self.Name:SetText(COMMUNITY_FINDER_FIND_COMMUNITY);
TOM_RUS's avatar
TOM_RUS committed
689 690 691 692 693

	if not disabled then
		self.Name:SetTextColor(GREEN_FONT_COLOR:GetRGB());
	end

TOM_RUS's avatar
TOM_RUS committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	self.Name:SetPoint("LEFT", self.Icon, "RIGHT", 13, 0);
	self.Selection:SetShown(self:GetCommunitiesFrame():GetDisplayMode() == COMMUNITIES_FRAME_DISPLAY_MODES.COMMUNITY_FINDER);

	self.Background:SetTexture("Interface\\Common\\bluemenu-main");
	self.Background:SetTexCoord(0.00390625, 0.87890625, 0.75195313, 0.83007813);
	self.Selection:SetTexture("Interface\\Common\\bluemenu-main");
	self.Selection:SetTexCoord(0.00390625, 0.87890625, 0.59179688, 0.66992188);
	self.FavoriteIcon:Hide();
	self.InvitationIcon:Hide();
	self.Icon:Show();
	self.CircleMask:Hide();
	self.IconRing:Hide();
	self.GuildTabardEmblem:Hide();
	self.GuildTabardBackground:Hide();
	self.GuildTabardBorder:Hide();
	self.UnreadNotificationIcon:Hide();

	self.Icon:SetAtlas("communities-icon-searchmagnifyingglass");
	self.Icon:SetSize(30, 30);
	self.Icon:SetPoint("TOPLEFT", 17, -18);
TOM_RUS's avatar
TOM_RUS committed
714 715

	UIFrameFlashStop(self.NewCommunityFlash);
TOM_RUS's avatar
TOM_RUS committed
716 717
end

TOM_RUS's avatar
TOM_RUS committed
718
function CommunitiesListEntryMixin:SetAddCommunity()
TOM_RUS's avatar
TOM_RUS committed
719 720
	self:SetEntryEnabled(true);

TOM_RUS's avatar
TOM_RUS committed
721
	self.overrideOnClick = function()
TOM_RUS's avatar
TOM_RUS committed
722
		PlaySound(SOUNDKIT.IG_CHARACTER_INFO_OPEN);
TOM_RUS's avatar
TOM_RUS committed
723 724 725 726 727
		if not AddCommunitiesFlow_IsShown() then
			self:GetCommunitiesFrame():CloseActiveDialogs();
		end
		AddCommunitiesFlow_Toggle();
	end;
TOM_RUS's avatar
TOM_RUS committed
728 729 730 731
	
	self.clubId = nil;
	self.Name:SetText(COMMUNITIES_JOIN_COMMUNITY);
	self.Name:SetTextColor(GREEN_FONT_COLOR:GetRGB());
TOM_RUS's avatar
TOM_RUS committed
732
	self.Name:SetPoint("LEFT", self.Icon, "RIGHT", 13, 0);
TOM_RUS's avatar
TOM_RUS committed
733 734
	self.Selection:Hide();
	
TOM_RUS's avatar
TOM_RUS committed
735 736 737 738
	self.Background:SetTexture("Interface\\Common\\bluemenu-main");
	self.Background:SetTexCoord(0.00390625, 0.87890625, 0.75195313, 0.83007813);
	self.Selection:SetTexture("Interface\\Common\\bluemenu-main");
	self.Selection:SetTexCoord(0.00390625, 0.87890625, 0.59179688, 0.66992188);
TOM_RUS's avatar
TOM_RUS committed
739
	self.FavoriteIcon:Hide();
TOM_RUS's avatar
TOM_RUS committed
740 741 742 743
	self.InvitationIcon:Hide();
	self.Icon:Show();
	self.CircleMask:Hide();
	self.IconRing:Hide();
TOM_RUS's avatar
TOM_RUS committed
744 745 746 747
	self.GuildTabardEmblem:Hide();
	self.GuildTabardBackground:Hide();
	self.GuildTabardBorder:Hide();
	self.UnreadNotificationIcon:Hide();
TOM_RUS's avatar
TOM_RUS committed
748 749

	self.Icon:SetAtlas("communities-icon-addgroupplus");
TOM_RUS's avatar
TOM_RUS committed
750
	self.Icon:SetSize(30, 30);
TOM_RUS's avatar
TOM_RUS committed
751
	self.Icon:SetPoint("TOPLEFT", 17, -18);
TOM_RUS's avatar
TOM_RUS committed
752 753

	UIFrameFlashStop(self.NewCommunityFlash);
TOM_RUS's avatar
TOM_RUS committed
754 755
end

TOM_RUS's avatar
TOM_RUS committed
756
function CommunitiesListEntryMixin:SetGuildFinder()
TOM_RUS's avatar
TOM_RUS committed
757 758
	local disabled = self:CheckForDisabledReason(Enum.ClubType.Character);

TOM_RUS's avatar
TOM_RUS committed
759
	self.overrideOnClick = function ()
TOM_RUS's avatar
TOM_RUS committed
760
		PlaySound(SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON);
TOM_RUS's avatar
TOM_RUS committed
761

TOM_RUS's avatar
TOM_RUS committed
762
		local communitiesFrame = self:GetCommunitiesFrame();
TOM_RUS's avatar
TOM_RUS committed
763
		communitiesFrame:SelectClub(nil);	
TOM_RUS's avatar
TOM_RUS committed
764
		communitiesFrame:SetDisplayMode(COMMUNITIES_FRAME_DISPLAY_MODES.GUILD_FINDER);
TOM_RUS's avatar
TOM_RUS committed
765 766

		communitiesFrame.GuildFinderFrame.isGuildType = true;
TOM_RUS's avatar
TOM_RUS committed
767
		communitiesFrame.GuildFinderFrame.selectedTab = 1;
TOM_RUS's avatar
TOM_RUS committed
768 769
		communitiesFrame.GuildFinderFrame:UpdateType(); 

TOM_RUS's avatar
TOM_RUS committed
770
		communitiesFrame.Inset:Hide();
TOM_RUS's avatar
TOM_RUS committed
771 772
	end;
	
TOM_RUS's avatar
TOM_RUS committed
773 774
	self.clubId = nil;
	self.Name:SetText(COMMUNITIES_GUILD_FINDER);
TOM_RUS's avatar
TOM_RUS committed
775 776 777 778 779

	if not disabled then
		self.Name:SetTextColor(GREEN_FONT_COLOR:GetRGB());
	end

TOM_RUS's avatar
TOM_RUS committed
780
	self.Name:SetPoint("LEFT", self.Icon, "RIGHT", 10, 0);
TOM_RUS's avatar
TOM_RUS committed
781
	self.Selection:SetShown(self:GetCommunitiesFrame():GetDisplayMode() == COMMUNITIES_FRAME_DISPLAY_MODES.GUILD_FINDER);
TOM_RUS's avatar
TOM_RUS committed
782 783 784 785 786

	self.Background:SetAtlas("communities-nav-button-green-normal");
	self.Background:SetTexCoord(0, 1, 0, 1);
	self.Selection:SetAtlas("communities-nav-button-green-pressed");
	self.Selection:SetTexCoord(0, 1, 0, 1);
TOM_RUS's avatar
TOM_RUS committed
787
	self.FavoriteIcon:Hide();
TOM_RUS's avatar
TOM_RUS committed
788 789
	self.InvitationIcon:Hide();
	self.Icon:Show();
TOM_RUS's avatar
TOM_RUS committed
790
	self.Icon:SetSize(35, 35);
TOM_RUS's avatar
TOM_RUS committed
791
	self.Icon:SetPoint("TOPLEFT", 15, -15);
TOM_RUS's avatar
TOM_RUS committed
792 793
	self.CircleMask:Show();
	self.IconRing:Hide();
TOM_RUS's avatar
TOM_RUS committed
794
	self.GuildTabardEmblem:Hide();
TOM_RUS's avatar
TOM_RUS committed
795 796 797 798
	self.GuildTabardBackground:Show();
	self.GuildTabardBackground:SetVertexColor(0.7, 0.7, 0.7);
	self.GuildTabardBorder:Show();
	self.GuildTabardBorder:SetVertexColor(0.7, 0.7, 0.7);
TOM_RUS's avatar
TOM_RUS committed
799
	self.UnreadNotificationIcon:Hide();
TOM_RUS's avatar
TOM_RUS committed
800 801 802 803 804 805 806

	local factionGroup = UnitFactionGroup("player");
	if factionGroup == "Alliance" then
		self.Icon:SetTexture("Interface\\FriendsFrame\\PlusManz-Alliance");
	else
		self.Icon:SetTexture("Interface\\FriendsFrame\\PlusManz-Horde");
	end
TOM_RUS's avatar
TOM_RUS committed
807 808

	UIFrameFlashStop(self.NewCommunityFlash);
TOM_RUS's avatar
TOM_RUS committed
809 810 811 812 813 814
end

function CommunitiesListEntryMixin:GetClubId()
	return self.clubId;
end

TOM_RUS's avatar
TOM_RUS committed
815 816 817 818
function CommunitiesListEntryMixin:IsInvitation()
	return self.isInvitation;
end

TOM_RUS's avatar
TOM_RUS committed
819 820 821 822 823
function CommunitiesListEntryMixin:IsTicket()
	return self.isTicket;
end

function CommunitiesListEntryMixin:GetCommunitiesList()
TOM_RUS's avatar
TOM_RUS committed
824 825 826 827
	return self:GetParent():GetParent():GetParent();
end

function CommunitiesListEntryMixin:GetCommunitiesFrame()
TOM_RUS's avatar
TOM_RUS committed
828
	return self:GetCommunitiesList():GetCommunitiesFrame();
TOM_RUS's avatar
TOM_RUS committed
829 830
end

TOM_RUS's avatar
TOM_RUS committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844
function CommunitiesListEntryMixin:SetEntryEnabled(enabled)
	local desaturated = not enabled;
	self.Background:SetDesaturated(desaturated);
	self.Icon:SetDesaturated(desaturated);
	self.GuildTabardBorder:SetDesaturated(desaturated);
	self.GuildTabardBackground:SetDesaturated(desaturated);
	self.Name:SetFontObject(GameFontDisable);
	self:SetEnabled(enabled);
end

function CommunitiesListEntryMixin:SetDisabledTooltip(disabledTooltip)
	self.disabledTooltip = disabledTooltip;
end

TOM_RUS's avatar
TOM_RUS committed
845 846 847 848 849 850 851 852 853 854 855 856 857 858
function CommunitiesListEntryMixin:OnShow()
	FrameUtil.RegisterFrameForEvents(self, COMMUNITIES_LIST_ENTRY_EVENTS);
end

function CommunitiesListEntryMixin:OnHide()
	FrameUtil.UnregisterFrameForEvents(self, COMMUNITIES_LIST_ENTRY_EVENTS);
end

function CommunitiesListEntryMixin:OnEvent(event, ...)
	if event == "STREAM_VIEW_MARKER_UPDATED" then
		local clubId, streamId, lastUnreadTime = ...;
		if clubId == self.clubId then
			self:UpdateUnreadNotification();
		end
TOM_RUS's avatar
TOM_RUS committed
859 860
	elseif event == "PLAYER_GUILD_UPDATE" then
		SetLargeGuildTabardTextures("player", self.GuildTabardEmblem, self.GuildTabardBackground, self.GuildTabardBorder);
TOM_RUS's avatar
TOM_RUS committed
861 862 863
	end
end

TOM_RUS's avatar
TOM_RUS committed
864
function CommunitiesListEntryMixin:OnEnter()
TOM_RUS's avatar
TOM_RUS committed
865 866 867
	if not self:IsEnabled() and self.disabledTooltip then
		GameTooltip_ShowDisabledTooltip(GameTooltip, self, self.disabledTooltip);
	elseif self.Name:IsTruncated() then
TOM_RUS's avatar
TOM_RUS committed
868
		GameTooltip:SetOwner(self);
TOM_RUS's avatar
TOM_RUS committed
869
		GameTooltip_SetTitle(GameTooltip, self.Name:GetText());
TOM_RUS's avatar
TOM_RUS committed
870 871 872 873
		GameTooltip:Show();
	end
end

TOM_RUS's avatar
TOM_RUS committed
874
function CommunitiesListEntryMixin:OnClick(button)
TOM_RUS's avatar
TOM_RUS committed
875 876 877 878 879
	if self.overrideOnClick then
		self.overrideOnClick(self, button);
		return;
	end
	
TOM_RUS's avatar
TOM_RUS committed
880
	if button == "LeftButton" then
TOM_RUS's avatar
TOM_RUS committed
881
		PlaySound(SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON);
TOM_RUS's avatar
TOM_RUS committed
882
		self:GetCommunitiesFrame():SelectClub(self.clubId);
TOM_RUS's avatar
TOM_RUS committed
883
	elseif button == "RightButton" then
TOM_RUS's avatar
TOM_RUS committed
884
		local clubInfo = C_Club.GetClubInfo(self:GetClubId());
TOM_RUS's avatar
TOM_RUS committed
885
		if not clubInfo then
TOM_RUS's avatar
TOM_RUS committed
886 887 888
			return;
		end

TOM_RUS's avatar
TOM_RUS committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
		local communitiesList = self:GetParent():GetParent():GetParent();
		communitiesList:SetSelectedEntryForDropDown(self);
		ToggleDropDownMenu(1, nil, communitiesList.EntryDropDown, self, 0, 0);
	end
end

function CommunitiesListEntryDropDown_Initialize(self, level)
	local communitiesList = self:GetParent();
	local selectedCommunitiesListEntry = communitiesList:GetSelectedEntryForDropDown();
	if not selectedCommunitiesListEntry then
		return;
	end
	
	local clubId = selectedCommunitiesListEntry:GetClubId();
	if clubId then
		local clubInfo = C_Club.GetClubInfo(clubId);
TOM_RUS's avatar
TOM_RUS committed
905 906 907 908
		local memberInfo = C_Club.GetMemberInfoForSelf(clubId);
		if clubInfo and memberInfo then
			self.clubMemberInfo = memberInfo;
			self.clubInfo = clubInfo;
TOM_RUS's avatar
TOM_RUS committed
909 910 911 912 913
			if clubInfo.clubType == Enum.ClubType.Guild then 
				UnitPopup_ShowMenu(self, "GUILDS_GUILD", nil, clubInfo.name);
			else 
				UnitPopup_ShowMenu(self, "COMMUNITIES_COMMUNITY", nil, clubInfo.name);
			end
TOM_RUS's avatar
TOM_RUS committed
914 915 916 917 918 919 920 921 922 923 924
		end
	end
end

function CommunitiesListEntryDropDown_OnLoad(self)
	UIDropDownMenu_Initialize(self, CommunitiesListEntryDropDown_Initialize, "MENU");
end

function CommunitiesListEntryDropDown_OnHide(self)
	local communitiesList = self:GetParent();
	communitiesList:SetSelectedEntryForDropDown(nil);
TOM_RUS's avatar
TOM_RUS committed
925 926
	self.clubMemberInfo = nil;
	self.clubInfo = nil;
TOM_RUS's avatar
TOM_RUS committed
927 928 929 930
end

CommunitiesListDropDownMenuMixin = {};

TOM_RUS's avatar
TOM_RUS committed
931
function CommunitiesListDropDownMenuMixin:OnLoad()
TOM_RUS's avatar
TOM_RUS committed
932 933
	UIDropDownMenu_SetWidth(self, self.width or 115);
	self.Text:SetJustifyH("LEFT");
TOM_RUS's avatar
TOM_RUS committed
934 935
end

TOM_RUS's avatar
TOM_RUS committed
936 937
function CommunitiesListDropDownMenuMixin:OnShow()
	UIDropDownMenu_Initialize(self, CommunitiesListDropDownMenu_Initialize);
TOM_RUS's avatar
TOM_RUS committed
938 939
	local parent = self:GetParent();
	UIDropDownMenu_SetSelectedValue(self, parent:GetSelectedClubId());
TOM_RUS's avatar
TOM_RUS committed
940
	self:UpdateUnreadNotification();
TOM_RUS's avatar
TOM_RUS committed
941

TOM_RUS's avatar
TOM_RUS committed
942
	if parent.RegisterCallback then
TOM_RUS's avatar
TOM_RUS committed
943
		parent:RegisterCallback(CommunitiesFrameMixin.Event.ClubSelected, self.OnCommunitiesClubSelected, self);
TOM_RUS's avatar
TOM_RUS committed
944 945 946 947
	end
end

function CommunitiesListDropDownMenuMixin:OnHide()
TOM_RUS's avatar
TOM_RUS committed
948 949
	local parent = self:GetParent();
	if parent.RegisterCallback then
TOM_RUS's avatar
TOM_RUS committed
950 951 952 953 954 955 956
		parent:UnregisterCallback(CommunitiesFrameMixin.Event.ClubSelected, self);
	end
end

function CommunitiesListDropDownMenuMixin:OnCommunitiesClubSelected(clubId)
	if clubId and self:IsVisible() then
		self:OnClubSelected();
TOM_RUS's avatar
TOM_RUS committed
957
	end
TOM_RUS's avatar
TOM_RUS committed
958 959
end

TOM_RUS's avatar
TOM_RUS committed
960 961
function CommunitiesListDropDownMenuMixin:OnClubSelected()
	local parent = self:GetParent();
TOM_RUS's avatar
TOM_RUS committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
	local clubId = parent:GetSelectedClubId();
	UIDropDownMenu_SetSelectedValue(self, clubId);
	
	local clubInfo = C_Club.GetClubInfo(clubId);
	UIDropDownMenu_SetText(self, clubInfo and clubInfo.name or "");
	
	self:UpdateUnreadNotification();
end

function CommunitiesListDropDownMenuMixin:UpdateUnreadNotification()
	local parent = self:GetParent();
	if parent.RegisterCallback then
		local clubId = parent:GetSelectedClubId();
		self.NotificationOverlay:SetShown(CommunitiesUtil.DoesOtherCommunityHaveUnreadMessages(clubId));
	else
		-- If our parent is not the communities frame we don't show unread notifications.
		self.NotificationOverlay:SetShown(false);
	end

TOM_RUS's avatar
TOM_RUS committed
981 982 983 984 985 986 987 988 989 990 991 992
end

function CommunitiesListScrollFrame_OnVerticalScroll(self)
	local communitiesList = self:GetParent();
	if communitiesList:GetSelectedEntryForDropDown() ~= nil then
		HideDropDownMenu(1);
	end
end

function CommunitiesListDropDownMenu_Initialize(self)
	local clubs = C_Club.GetSubscribedClubs();
	if clubs ~= nil then
TOM_RUS's avatar
TOM_RUS committed
993
		CommunitiesUtil.SortClubs(clubs);
TOM_RUS's avatar
TOM_RUS committed
994
		local info = UIDropDownMenu_CreateInfo();
TOM_RUS's avatar
TOM_RUS committed
995
		local parent = self:GetParent();
TOM_RUS's avatar
TOM_RUS committed
996 997
		for i, clubInfo in ipairs(clubs) do
			info.text = clubInfo.name;
TOM_RUS's avatar
TOM_RUS committed
998
			if CommunitiesUtil.DoesCommunityHaveUnreadMessages(clubInfo.clubId) then
TOM_RUS's avatar
TOM_RUS committed
999
				info.text = info.text.." "..CreateAtlasMarkup("communities-icon-notification", 11, 11);
TOM_RUS's avatar
TOM_RUS committed
1000 1001
			end
			
TOM_RUS's avatar
TOM_RUS committed
1002 1003
			info.value = clubInfo.clubId;
			info.func = function(button)
TOM_RUS's avatar
TOM_RUS committed
1004
				parent:SelectClub(button.value);
TOM_RUS's avatar
TOM_RUS committed
1005 1006 1007 1008
			end
			UIDropDownMenu_AddButton(info);
		end
		
TOM_RUS's avatar
TOM_RUS committed
1009 1010 1011 1012 1013 1014 1015
		local clubId = parent:GetSelectedClubId();
		if clubId then
			UIDropDownMenu_SetSelectedValue(self, clubId);
			
			local clubInfo = C_Club.GetClubInfo(clubId);
			UIDropDownMenu_SetText(self, clubInfo and clubInfo.name or "");
		end
TOM_RUS's avatar
TOM_RUS committed
1016 1017
	end
end