2015-02-08 02:01:09 -05:00
var FFZ = window . FrankerFaceZ ,
constants = require ( '../constants' ) ;
2015-01-20 01:53:18 -05:00
// --------------------
// Initializer
// --------------------
FFZ . prototype . setup _menu = function ( ) {
this . log ( "Installing mouse-up event to auto-close menus." ) ;
var f = this ;
jQuery ( document ) . mouseup ( function ( e ) {
var popup = f . _popup , parent ;
if ( ! popup ) return ;
popup = jQuery ( popup ) ;
parent = popup . parent ( ) ;
if ( ! parent . is ( e . target ) && parent . has ( e . target ) . length === 0 ) {
popup . remove ( ) ;
delete f . _popup ;
2015-02-08 02:01:09 -05:00
f . _popup _kill && f . _popup _kill ( ) ;
delete f . _popup _kill ;
2015-01-20 01:53:18 -05:00
}
} ) ;
}
2015-02-08 02:01:09 -05:00
FFZ . menu _pages = { } ;
2015-01-20 01:53:18 -05:00
// --------------------
// Create Menu
// --------------------
FFZ . prototype . build _ui _popup = function ( view ) {
var popup = this . _popup ;
if ( popup ) {
popup . parentElement . removeChild ( popup ) ;
delete this . _popup ;
2015-02-08 02:01:09 -05:00
this . _popup _kill && this . _popup _kill ( ) ;
delete this . _popup _kill ;
2015-01-20 01:53:18 -05:00
return ;
}
// Start building the DOM.
var container = document . createElement ( 'div' ) ,
2015-02-08 02:01:09 -05:00
inner = document . createElement ( 'div' ) ,
menu = document . createElement ( 'ul' ) ,
dark = ( this . has _bttv ? BetterTTV . settings . get ( 'darkenedMode' ) : false ) ;
2015-01-20 01:53:18 -05:00
container . className = 'emoticon-selector chat-menu ffz-ui-popup' ;
inner . className = 'emoticon-selector-box dropmenu' ;
container . appendChild ( inner ) ;
2015-02-08 02:01:09 -05:00
container . classList . toggle ( 'dark' , dark ) ;
// Render Menu
menu . className = 'menu clearfix' ;
inner . appendChild ( menu ) ;
var el = document . createElement ( 'li' ) ;
el . className = 'title' ;
el . innerHTML = "<span>FrankerFaceZ</span>" ;
menu . appendChild ( el ) ;
el . addEventListener ( "click" , this . _add _emote . bind ( this , view , "To use custom emoticons in tons of channels, get FrankerFaceZ from http://www.frankerfacez.com" ) ) ;
var sub _container = document . createElement ( 'div' ) ;
sub _container . className = 'ffz-ui-menu-page' ;
inner . appendChild ( sub _container ) ;
for ( var key in FFZ . menu _pages ) {
var page = FFZ . menu _pages [ key ] ;
if ( ! page || ( page . hasOwnProperty ( "visible" ) && ( ! page . visible || ( typeof page . visible == "function" && ! page . visible . bind ( this ) ( ) ) ) ) )
continue ;
var el = document . createElement ( 'li' ) ,
link = document . createElement ( 'a' ) ;
el . className = 'item' ;
el . id = "ffz-menu-page-" + key ;
link . title = page . name ;
link . innerHTML = page . icon ;
link . addEventListener ( "click" , this . _ui _change _page . bind ( this , view , menu , sub _container , key ) ) ;
el . appendChild ( link ) ;
menu . appendChild ( el ) ;
2015-01-27 17:05:51 -05:00
}
2015-01-20 01:53:18 -05:00
2015-02-08 02:01:09 -05:00
// Render Current Page
this . _ui _change _page ( view , menu , sub _container , this . _last _page || "channel" ) ;
2015-01-20 01:53:18 -05:00
// Add the menu to the DOM.
this . _popup = container ;
2015-02-08 02:01:09 -05:00
sub _container . style . maxHeight = Math . max ( 300 , view . $ ( ) . height ( ) - 212 ) + "px" ;
2015-01-20 01:53:18 -05:00
view . $ ( '.chat-interface' ) . append ( container ) ;
}
2015-02-08 02:01:09 -05:00
FFZ . prototype . _ui _change _page = function ( view , menu , container , page ) {
this . _last _page = page ;
container . innerHTML = "" ;
var els = menu . querySelectorAll ( 'li.active' ) ;
for ( var i = 0 ; i < els . length ; i ++ )
els [ i ] . classList . remove ( 'active' ) ;
var el = menu . querySelector ( '#ffz-menu-page-' + page ) ;
if ( el )
el . classList . add ( 'active' ) ;
else
this . log ( "No matching page: " + page ) ;
FFZ . menu _pages [ page ] . render . bind ( this ) ( view , container ) ;
}
// --------------------
// Settings Page
// --------------------
FFZ . menu _pages . settings = {
render : function ( view , container ) {
var menu = document . createElement ( 'div' ) ;
menu . className = 'chat-menu-content' ;
var settings = [ ] ;
for ( var key in FFZ . settings _info )
settings . push ( [ key , FFZ . settings _info [ key ] ] ) ;
settings . sort ( function ( a , b ) {
var ai = a [ 1 ] ,
bi = b [ 1 ] ,
an = ai . name . toLowerCase ( ) ,
bn = bi . name . toLowerCase ( ) ;
if ( an < bn ) return - 1 ;
else if ( an > bn ) return 1 ;
return 0 ;
} ) ;
for ( var i = 0 ; i < settings . length ; i ++ ) {
var key = settings [ i ] [ 0 ] ,
info = settings [ i ] [ 1 ] ,
el = document . createElement ( 'p' ) ,
val = this . settings . get ( key ) ;
if ( info . visible !== undefined && info . visible !== null ) {
var visible = info . visible ;
if ( typeof info . visible == "function" )
visible = info . visible . bind ( this ) ( ) ;
if ( ! visible )
continue ;
}
el . className = 'clearfix' ;
if ( info . type == "boolean" ) {
var swit = document . createElement ( 'a' ) ,
label = document . createElement ( 'span' ) ;
swit . className = 'switch' ;
swit . classList . toggle ( 'active' , val ) ;
swit . innerHTML = "<span></span>" ;
label . className = 'switch-label' ;
label . innerHTML = info . name ;
el . appendChild ( swit ) ;
el . appendChild ( label ) ;
swit . addEventListener ( "click" , this . _ui _toggle _setting . bind ( this , swit , key ) ) ;
} else {
el . classList . add ( "option" ) ;
var link = document . createElement ( 'a' ) ;
link . innerHTML = info . name ;
link . href = "#" ;
el . appendChild ( link ) ;
link . addEventListener ( "click" , info . method . bind ( this ) ) ;
}
if ( info . help ) {
var help = document . createElement ( 'span' ) ;
help . className = 'help' ;
help . innerHTML = info . help ;
el . appendChild ( help ) ;
}
menu . appendChild ( el ) ;
}
container . appendChild ( menu ) ;
} ,
name : "Settings" ,
icon : constants . GEAR
} ;
FFZ . prototype . _ui _toggle _setting = function ( swit , key ) {
var val = ! this . settings . get ( key ) ;
this . settings . set ( key , val ) ;
swit . classList . toggle ( 'active' , val ) ;
}
// --------------------
// Favorites Page
// --------------------
/ * F F Z . m e n u _ p a g e s . f a v o r i t e s = {
render : function ( view , container ) {
} ,
name : "Favorites" ,
icon : constants . HEART
} ; * /
// --------------------
// Channel Page
// --------------------
FFZ . menu _pages . channel = {
render : function ( view , inner ) {
// Get the current room.
var room _id = view . get ( 'controller.currentRoom.id' ) ,
room = this . rooms [ room _id ] ;
this . log ( "Menu for Room: " + room _id , room ) ;
this . track ( 'trackEvent' , 'Menu' , 'Open' , room _id ) ;
// Add the header and ad button.
/ * v a r b t n = d o c u m e n t . c r e a t e E l e m e n t ( ' a ' ) ;
btn . className = 'button glyph-only ffz-button' ;
btn . title = 'Advertise for FrankerFaceZ in chat!' ;
btn . href = '#' ;
btn . innerHTML = '<svg class="svg-followers" height="16px" version="1.1" viewBox="0 0 16 16" width="16px" x="0px" y="0px"><path clip-rule="evenodd" d="M8,13.5L1.5,7V4l2-2h3L8,3.5L9.5,2h3l2,2v3L8,13.5z" fill-rule="evenodd"></path></svg>' ;
var hdr = document . createElement ( 'div' ) ;
hdr . className = 'list-header first' ;
hdr . appendChild ( btn ) ;
hdr . appendChild ( document . createTextNode ( 'FrankerFaceZ' ) ) ;
inner . appendChild ( hdr ) ; * /
var c = this . _emotes _for _sets ( inner , view , room && room . menu _sets || [ ] ) ;
/ * i f ( ! t h i s . _ w s _ e x i s t s ) {
btn . className = "button ffz-button primary" ;
btn . innerHTML = "Server Error" ;
btn . title = "FFZ Server Error" ;
btn . addEventListener ( 'click' , alert . bind ( window , "The FrankerFaceZ client was unable to create a WebSocket to communicate with the FrankerFaceZ server.\n\nThis is most likely due to your browser's configuration either disabling WebSockets entirely or limiting the number of simultaneous connections. Please ensure that WebSockets have not been disabled." ) ) ;
} else {
if ( c === 0 )
btn . addEventListener ( 'click' , this . _add _emote . bind ( this , view , "To use custom emoticons in tons of channels, get FrankerFaceZ from http://www.frankerfacez.com" ) ) ;
else
btn . addEventListener ( 'click' , this . _add _emote . bind ( this , view , "To view this channel's emoticons, get FrankerFaceZ from http://www.frankerfacez.com" ) ) ;
} * /
// Feature Friday!
this . _feature _friday _ui ( room _id , inner , view ) ;
} ,
name : "Channel" ,
icon : constants . ZREKNARF
} ;
2015-01-20 01:53:18 -05:00
// --------------------
// Emotes for Sets
// --------------------
FFZ . prototype . _emotes _for _sets = function ( parent , view , sets , header , btn ) {
if ( header != null ) {
var el _header = document . createElement ( 'div' ) ;
el _header . className = 'list-header' ;
el _header . appendChild ( document . createTextNode ( header ) ) ;
if ( btn )
el _header . appendChild ( btn ) ;
parent . appendChild ( el _header ) ;
}
var grid = document . createElement ( 'div' ) , c = 0 ;
grid . className = 'emoticon-grid' ;
for ( var i = 0 ; i < sets . length ; i ++ ) {
var set = this . emote _sets [ sets [ i ] ] ;
if ( ! set || ! set . emotes )
continue ;
for ( var eid in set . emotes ) {
var emote = set . emotes [ eid ] ;
if ( ! set . emotes . hasOwnProperty ( eid ) || emote . hidden )
continue ;
c ++ ;
var s = document . createElement ( 'span' ) ;
s . className = 'emoticon tooltip' ;
s . style . backgroundImage = 'url("' + emote . url + '")' ;
s . style . width = emote . width + "px" ;
s . style . height = emote . height + "px" ;
s . title = emote . name ;
s . addEventListener ( 'click' , this . _add _emote . bind ( this , view , emote . name ) ) ;
grid . appendChild ( s ) ;
}
}
if ( ! c ) {
grid . innerHTML = "This channel has no emoticons." ;
grid . className = "chat-menu-content ffz-no-emotes center" ;
}
parent . appendChild ( grid ) ;
}
FFZ . prototype . _add _emote = function ( view , emote ) {
var room = view . get ( 'controller.currentRoom' ) ,
current _text = room . get ( 'messageToSend' ) || '' ;
if ( current _text && current _text . substr ( - 1 ) !== " " )
current _text += ' ' ;
room . set ( 'messageToSend' , current _text + ( emote . name || emote ) ) ;
2015-01-12 17:58:07 -05:00
}