var FFZ = window.FrankerFaceZ,
utils = require('../utils'),
constants = require('../constants');
// --------------------
// Settings
// --------------------
FFZ.basic_settings.delayed_chat = {
type: "select",
options: {
0: "No Delay",
300: "Minor (Bot Moderation; 0.3s)",
1200: "Normal (Human Moderation; 1.2s)",
5000: "Large (Spoiler Removal / Really Slow Mods; 5s)"
},
category: "Chat",
no_bttv: true,
name: "Delay and Filter Chat",
help: "Delay the appearance of chat messages to allow time for moderation and completely hide removed messages.",
get: function() {
if ( ! this.settings.remove_deleted || ! this.settings.remove_bot_ban_notices )
return 0;
return this.settings.chat_delay;
},
set: function(val) {
val = +val;
this.settings.set('remove_deleted', val !== 0);
this.settings.set('remove_bot_ban_notices', val !== 0);
this.settings.set('chat_delay', val);
}
};
FFZ.settings_info.minimal_chat = {
type: "select",
options: {
0: "Disabled",
1: "No Heading",
2: "Minimalistic Input",
3: "All"
},
value: 0,
category: "Chat Appearance",
name: "Minimalistic Chat",
help: "Hide all of the chat user interface, only showing messages and an input box.",
process_value: function(val) {
if ( val === false )
return 0;
else if ( val === true )
return 3;
else if ( typeof val === "string" )
return parseInt(val) || 0;
return val;
},
on_update: function(val) {
document.body.classList.toggle("ffz-minimal-chat-head", val === 1 || val === 3);
document.body.classList.toggle("ffz-minimal-chat-input", val > 1);
if ( this.settings.group_tabs && this._chatv && this._chatv._ffz_tabs ) {
var f = this;
setTimeout(function() {
f._chatv && f._chatv.$('.chat-room').css('top', f._chatv._ffz_tabs.offsetHeight + "px");
f._roomv && f._roomv.get('stuckToBottom') && f._roomv._scrollToBottom();
},0);
}
if ( (val === 1 || val === 3) && this._chatv && this._chatv.get('controller.showList') )
this._chatv.set('controller.showList', false);
// Remove the style if we have it.
if ( ! (val > 1) && this._chat_style ) {
if ( this._inputv ) {
if ( this._inputv._ffz_minimal_style )
this._inputv._ffz_minimal_style.innerHTML = '';
this._inputv._ffz_last_height = undefined;
}
utils.update_css(this._chat_style, "input_height", '');
this._roomv && this._roomv.get('stuckToBottom') && this._roomv._scrollToBottom();
} else if ( val > 1 && this._inputv )
this._inputv.ffzResizeInput();
}
};
FFZ.settings_info.chat_batching = {
type: "select",
options: {
0: "No Batching",
250: "Minor (0.25s)",
500: "Normal (0.5s)",
750: "Large (0.75s)",
1000: "Extreme (1s)"
},
value: 0,
category: "Chat Appearance",
no_bttv: true,
name: "Chat Message Batching",
help: "Display chat messages in batches to improve performance in extremely fast chats.",
process_value: function(val) {
if ( typeof val === "string" )
return parseInt(val) || 0;
return val;
},
on_update: function(val) {
if ( this._roomv )
this._roomv.ffzUpdateStatus();
}
};
FFZ.settings_info.chat_delay = {
type: "select",
options: {
0: "No Delay",
300: "Minor (Bot Moderation; 0.3s)",
1200: "Normal (Human Moderation; 1.2s)",
5000: "Large (Spoiler Removal / Really Slow Mods; 5s)"
},
value: 0,
category: "Chat Appearance",
no_bttv: true,
name: "Artificial Chat Delay",
help: "Delay the appearance of chat messages to allow for moderation before you see them.",
process_value: function(val) {
if ( typeof val === "string" )
return parseInt(val || "0");
return val;
},
on_update: function (val) {
if ( this._roomv )
this._roomv.ffzUpdateStatus();
}
};
FFZ.settings_info.remove_deleted = {
type: "boolean",
value: false,
no_bttv: true,
category: "Chat Filtering",
name: "Remove Deleted Messages",
help: "Remove deleted messages from chat entirely rather than leaving behind a clickable <deleted message>.",
on_update: function(val) {
if ( this.has_bttv || ! this.rooms || ! val )
return;
for(var room_id in this.rooms) {
var ffz_room = this.rooms[room_id],
room = ffz_room && ffz_room.room;
if ( ! room )
continue;
var msgs = room.get('messages'),
total = msgs.get('length'),
i = total,
alternate;
while(i--) {
var msg = msgs.get(i);
if ( msg.ffz_deleted || msg.deleted ) {
if ( alternate === undefined )
alternate = msg.ffz_alternate;
msgs.removeAt(i);
continue;
}
if ( alternate === undefined )
alternate = msg.ffz_alternate;
else {
alternate = ! alternate;
room.set('messages.' + i + '.ffz_alternate', alternate);
}
}
}
}
};
FFZ.settings_info.remove_bot_ban_notices = {
type: "boolean",
value: false,
no_bttv: true,
category: "Chat Filtering",
name: "Remove Bot Ban Notices",
help: "Remove messages from bots announcing who was banned for what reason and for how long.",
};
FFZ.settings_info.prevent_clear = {
type: "boolean",
value: false,
no_bttv: true,
category: "Chat Filtering",
name: "Show Deleted Messages",
help: "Fade deleted messages instead of replacing them, and prevent chat from being cleared.",
on_update: function(val) {
if ( this.has_bttv || ! this.rooms )
return;
for(var room_id in this.rooms) {
var ffz_room = this.rooms[room_id],
room = ffz_room && ffz_room.room;
if ( ! room )
continue;
room.get("messages").forEach(function(s, n) {
if ( val && ! s.ffz_deleted && s.deleted )
room.set("messages." + n + ".deleted", false);
else if ( s.ffz_deleted && ! val && ! s.deleted )
room.set("messages." + n + ".deleted", true);
});
}
}
};
FFZ.settings_info.chat_history = {
type: "boolean",
value: true,
visible: false,
category: "Chat Appearance",
name: "Chat History Alpha",
help: "Load previous chat messages when loading a chat room so you can see what people have been talking about. This currently only works in a handful of channels due to server capacity.",
};
FFZ.settings_info.group_tabs = {
type: "boolean",
value: false,
no_bttv: true,
category: "Chat Moderation",
name: "Chat Room Tabs Beta",
help: "Enhanced UI for switching the current chat room and noticing new messages.",
on_update: function(val) {
var enabled = !this.has_bttv && val;
if ( ! this._chatv || enabled === this._group_tabs_state )
return;
if ( enabled )
this._chatv.ffzEnableTabs();
else
this._chatv.ffzDisableTabs();
}
};
FFZ.settings_info.pinned_rooms = {
value: [],
visible: false,
};
FFZ.settings_info.visible_rooms = {
value: [],
visible: false,
};
// --------------------
// Initialization
// --------------------
FFZ.prototype.setup_chatview = function() {
document.body.classList.toggle("ffz-minimal-chat-head", this.settings.minimal_chat === 1 || this.settings.minimal_chat === 3);
document.body.classList.toggle("ffz-minimal-chat-input", this.settings.minimal_chat === 2 || this.settings.minimal_chat === 3);
this.log("Hooking the Ember Chat controller.");
var Chat = App.__container__.lookup('controller:chat'),
f = this;
if ( Chat ) {
Chat.reopen({
ffzUpdateChannels: function() {
if ( ! f._chatv )
return;
f._chatv.ffzRebuildMenu();
if ( f.settings.group_tabs )
f._chatv.ffzRebuildTabs();
}.observes("currentChannelRoom", "connectedPrivateGroupRooms"),
removeCurrentChannelRoom: function() {
if ( ! f.settings.group_tabs || f.has_bttv )
return this._super();
var room = this.get("currentChannelRoom"),
room_id = room && room.get('id'),
user = f.get_user();
if ( ! f.settings.pinned_rooms || f.settings.pinned_rooms.indexOf(room_id) === -1 ) {
if ( room === this.get("currentRoom") )
this.blurRoom();
// Don't destroy it if it's the user's room.
if ( room && user && user.login !== room_id )
room.destroy();
}
this.set("currentChannelRoom", void 0);
}
});
}
this.log("Hooking the Ember Chat view.");
var Chat = App.__container__.resolve('view:chat');
this._modify_cview(Chat);
// For some reason, this doesn't work unless we create an instance of the
// chat view and then destroy it immediately.
try {
Chat.create().destroy();
} catch(err) { }
// Modify all existing Chat views.
for(var key in Ember.View.views) {
if ( ! Ember.View.views.hasOwnProperty(key) )
continue;
var view = Ember.View.views[key];
if ( !(view instanceof Chat) )
continue;
this.log("Manually updating existing Chat view.", view);
try {
view.ffzInit();
} catch(err) {
this.error("setup: build_ui_link: " + err);
}
}
this.log("Hooking the Ember 'Right Column' controller. Seriously...");
var Column = App.__container__.lookup('controller:right-column');
if ( ! Column )
return;
Column.reopen({
ffzFixTabs: function() {
if ( f.settings.group_tabs && f._chatv && f._chatv._ffz_tabs ) {
setTimeout(function() {
f._chatv && f._chatv.$('.chat-room').css('top', f._chatv._ffz_tabs.offsetHeight + "px");
},0);
}
}.observes("firstTabSelected")
});
}
// --------------------
// Modify Chat View
// --------------------
FFZ.prototype._modify_cview = function(view) {
var f = this;
view.reopen({
didInsertElement: function() {
this._super();
try {
this.ffzInit();
} catch(err) {
f.error("ChatView didInsertElement: " + err);
}
},
willClearRender: function() {
try {
this.ffzTeardown();
} catch(err) {
f.error("ChatView willClearRender: " + err);
}
this._super();
},
ffzInit: function() {
f._chatv = this;
this.$('.textarea-contain').append(f.build_ui_link(this));
this.$('.chat-messages').find('.html-tooltip').tipsy({live: true, html: true, gravity: utils.tooltip_placement(2*constants.TOOLTIP_DISTANCE, 'n')});
if ( !f.has_bttv && f.settings.group_tabs )
this.ffzEnableTabs();
this.ffzRebuildMenu();
setTimeout(function() {
if ( f.settings.group_tabs && f._chatv && f._chatv._ffz_tabs )
f._chatv.$('.chat-room').css('top', f._chatv._ffz_tabs.offsetHeight + "px");
var controller = f._chatv && f._chatv.get('controller');
controller && controller.set('showList', false);
}, 1000);
},
ffzTeardown: function() {
if ( f._chatv === this )
f._chatv = null;
this.$('.textarea-contain .ffz-ui-toggle').remove();
if ( f.settings.group_tabs )
this.ffzDisableTabs();
},
ffzChangeRoom: Ember.observer('controller.currentRoom', function() {
f.update_ui_link();
// Close mod cards when changing to a new room.
if ( f._mod_card )
f._mod_card.send('close');
var room = this.get('controller.currentRoom'), rows;
room && room.resetUnreadCount();
if ( room && room._ffz_was_unread ) {
room._ffz_was_unread = false;
var el = this.get('element'),
unread_display = el && el.querySelector('#ffz-group-tabs .button .notifications'),
unread_count = unread_display ? parseInt(unread_display.textContent) : 0;
unread_count--;
if ( unread_display )
unread_display.textContent = unread_count || '';
}
if ( this._ffz_chan_table ) {
rows = jQuery(this._ffz_chan_table);
rows.children('.ffz-room-row').removeClass('active');
}
if ( this._ffz_group_table ) {
rows = jQuery(this._ffz_group_table);
rows.children('.ffz-room-row').removeClass('active');
}
if ( !f.has_bttv && f.settings.group_tabs && this._ffz_tabs ) {
var tabs = jQuery(this._ffz_tabs);
tabs.children('.ffz-chat-tab').removeClass('active');
if ( room && room._ffz_tab ) {
room._ffz_tab.classList.remove('tab-mentioned');
room._ffz_tab.classList.remove('hidden');
room._ffz_tab.classList.add('active');
var sp = room._ffz_tab.querySelector('span');
if ( sp )
sp.innerHTML = '';
}
if ( room && room._ffz_row ) {
room._ffz_row.classList.remove('row-mentioned');
room._ffz_row.classList.remove('hidden');
room._ffz_row.classList.add('active');
var sp = room._ffz_row.querySelector('span');
if ( sp )
sp.innerHTML = '';
}
// Invite Link
var can_invite = room && room.get('canInvite');
this._ffz_invite && this._ffz_invite.classList.toggle('hidden', !can_invite);
this.set('controller.showInviteUser', can_invite && this.get('controller.showInviteUser'))
// Now, adjust the chat-room.
this.$('.chat-room').css('top', this._ffz_tabs.offsetHeight + "px");
}
}),
// Better Menu
ffzRebuildMenu: function() {
/*var el = this.get('element'),
room_list = el && el.querySelector('.chat-rooms .tse-content');
if ( ! room_list )
return;
if ( ! room_list.classList.contains('ffz-room-list') ) {
room_list.classList.add('ffz-room-list');
// Find the Pending Invitations
var headers = room_list.querySelectorAll('.list-header'),
hdr = headers.length ? headers[headers.length-1] : undefined;
if ( hdr ) {
hdr.classList.add('ffz');
if ( hdr.nextSibling && hdr.nextSibling.classList )
hdr.nextSibling.classList.add('ffz');
}
}
// Channel Table
var t = this,
chan_table = this._ffz_chan_table || room_list.querySelector('#ffz-channel-table tbody');
if ( ! chan_table ) {
var tbl = document.createElement('table');
tbl.setAttribute('cellspacing', 0);
tbl.id = 'ffz-channel-table';
tbl.className = 'ffz';
tbl.innerHTML = 'Channels Pin