Skip to content

Instantly share code, notes, and snippets.

@CoryChaplin
Created April 3, 2015 21:26
Show Gist options
  • Save CoryChaplin/90ef4c0249d465c19bb6 to your computer and use it in GitHub Desktop.
Save CoryChaplin/90ef4c0249d465c19bb6 to your computer and use it in GitHub Desktop.
<!--
Plugin by Cory Chaplin (elephantman@europnet.org) - http://www.europnet.org - 01/2015
-->
<style>
#kiwi .right_bar .queries > div {
display: none;
}
#kiwi .right_bar .queries > div.active {
display: block;
}
#kiwi .right_bar .queries {
margin: 10px;
}
#kiwi .right_bar .queries .query_avatar {
text-align: center;
width: 100%;
}
#kiwi .right_bar .queries .query_avatar > img {
border: 1px solid #999;
padding: 6px;
}
#kiwi .right_bar .queries .query_nick {
font-size: 1.5em;
font-weight: bold;
margin: 10px 0;
text-align: center;
}
#kiwi .right_bar .queries .common_channels {
clear: both;
margin: 10px 0;
}
#kiwi .right_bar .queries .common_channels .query_common_channels_title {
font-weight: bold;
font-size: 1.1em;
}
#kiwi .right_bar .queries .query_tools > button {
margin: 2px 0;
}
</style>
<script type="text/html" id="query_sidebar_container">
<div class="queries">
</div>
</script>
<script type="text/html" id="query_sidebar_content">
<div class="<%= query_id %>">
<div class="query_avatar"></div>
<div class="query_nick">
<span class="query_nick_nickname"></span>
</div>
<div class="query_infos">
<span class="query_gender"></span><span class="query_age"></span><span class="query_location"></span>
</div>
<div class="common_channels">
<span class="query_common_channels_title"><%= common_channels %></span><br />
</div>
<div class="query_tools">
<button class="query_whois"><i title="<%= whois %>" class="fa fa-search"></i>&nbsp;<%= whois %></button><br />
<button class="query_ignore"><i title="<%= ignore %>" class="fa fa-minus-circle"></i>&nbsp;<%= ignore %></button><br />
<i title="<%= hide_sidebar %>" class="fa fa-angle-double-right right-bar-toggle-inner"></i>
</div>
</div>
</script>
<script>
kiwi.plugins.on('loaded', function() {
// This plugin is only valid for rich_nicklist contexts
if(!kiwi.settings.get('rich_nicklist')) return;
var locale_strings,
text;
locale_strings = {
'en': {
"query_sidebar" : {
"" : {
"domain" : "query_sidebar",
"lang" : "en"
},
"whois" : [ null, "User info" ],
"ignore" : [ null, "Ignore user" ],
"unignore" : [ null, "Stop ignoring" ],
"common_channels" : [ null, "Channels in common" ],
"hide_sidebar" : [ null, "Hide" ],
"years_old" : [ null, "years old" ],
"gender_male" : [ null, "Male" ],
"gender_female" : [ null, "Female" ],
"gender_unknown" : [ null, "" ]
}
},
'fr': {
"query_sidebar" : {
"" : {
"domain" : "query_sidebar",
"lang" : "fr"
},
"whois" : [ null, "Infos" ],
"ignore" : [ null, "Bloquer" ],
"unignore" : [ null, "Ne plus bloquer" ],
"common_channels" : [ null, "Salons communs" ],
"hide_sidebar" : [ null, "Masquer" ],
"years_old" : [ null, "ans" ],
"gender_male" : [ null, "Homme" ],
"gender_female" : [ null, "Femme" ],
"gender_unknown" : [ null, "" ]
}
}
}
kiwi.events.on('panel:active', function(event, panel) {
// Hide all query rightbars
$('#kiwi .right-bar-content .queries').children().removeClass('active');
new queryInfo({'panel': panel['active']});
});
kiwi.events.on('panel:close', function(event, panel) {
var panel = panel['panel'];
if(panel.isQuery()) {
$('#kiwi .right_bar .queries .' + panel.cid).remove();
}
});
var querySidebar = Backbone.View.extend({
// function fillRightBar(member_info, query_nick, panel) {
render: function() {
this.initQueryRightPanel();
this.createQuerySidebar();
},
cleanup: function() {
$('#kiwi .right-bar-content .queries').children().removeClass('active');
$('#kiwi .queries').hide();
$('#kiwi .right_bar .right-bar-content .channel_tools').show();
$('#kiwi .memberlists').show();
},
initQueryRightPanel: function() {
// If this is the first query, create "queries" right_bar container
if($('#kiwi .right-bar-content .queries') == undefined || $('#kiwi .right-bar-content .queries').length == 0) {
var query_sidebar_container = $(_.template($('#query_sidebar_container').html().trim(), text));
$('#kiwi .right_bar .right-bar-content').append(query_sidebar_container);
} else {
$('#kiwi .right-bar-content .queries').show();
}
},
createQuerySidebar: function() {
// If this query doesn't exist yet, we'll create the right_bar for it
if($('#kiwi .right-bar-content .queries .' + this.model.panel.cid).length == 0) {
text['query_id'] = this.model.panel.cid;
// Push the html with the locale strings
var query_sidebar_content = $(_.template($('#query_sidebar_content').html().trim(), text));
var query_nick = this.model.panel.get('name'),
current_panel = this.model.panel.cid;
// Add the right_bar content
$('#kiwi .right-bar-content .queries').append(query_sidebar_content);
$('#kiwi .right_bar .queries .' + this.model.panel.cid + ' .query_nick .query_nick_nickname').text(query_nick);
// Make the right_bar for this query visible
$('#kiwi .right-bar-content .queries .' + this.model.panel.cid).addClass('active');
// Fix the resize_handle position
$('.memberlists_resize_handle').css('left', $('#kiwi').width() - parseInt($('.right_bar').width()) -10);
// Update the ignore status if query_nick is already ignored
if(getIgnoreStatus(query_nick) > -1) {
$('#kiwi .right_bar .queries .' + this.model.panel.cid + ' .query_ignore').html('<i title="' + text.unignore + '" class="fa fa-minus-circle"></i>&nbsp;' + text.unignore);
}
this.prepareGenderInfo();
this.displayNick();
this.displayCommonChannels();
this.displayAvatar();
this.displayASL();
this.bindQueryButtons();
} else { // The query exists but is inactive
// Hide all right_bars
$('#kiwi .memberlists').children().removeClass('active');
$('#kiwi .memberlists').hide();
$('#kiwi .right-bar-content .queries .' + this.model.panel.cid).addClass('active');
}
this.tweakRightbarVisibility();
},
tweakRightbarVisibility: function() {
// Tweak the right_bar visibility
setTimeout(function () {
if ($('#kiwi .right_bar').hasClass('disabled')) {
$('#kiwi .right_bar').removeClass('disabled');
$('#kiwi .memberlists').children().removeClass('active');
$('#kiwi .memberlists').hide();
$('#kiwi .right_bar .right-bar-content .channel_tools').hide();
// Panels to the side of the sidebar
$('#kiwi .panels').css('right', $('#kiwi .right_bar').outerWidth(true));
// The resize handle sits overlapping the panels and memberlist
$('#kiwi .resize_handle').css('left', $('#kiwi .right_bar').position().left - ($('#kiwi .resize_handle').outerWidth(true) / 2));
}
}, 20);
},
prepareGenderInfo: function() {
this.avatar = '/assets/plugins/avatar-undefined.jpg',
this.gender_text = text.gender_undefined,
this.color = '#000000';
// Set the gender avatar
if(this.model.member_info.query_gender == 'M') {
this.avatar = "/assets/plugins/user-Male.png";
this.color = '#0066ff';
this.gender_text = text.gender_male;
} else if(this.model.member_info.query_gender == 'F') {
this.avatar = "/assets/plugins/user-Female.png";
this.color = '#FF00FF';
this.gender_text = text.gender_female;
}
},
displayNick: function() {
$('#kiwi .right_bar .queries .' + this.model.panel.cid + ' .query_nick_nickname').css('color', this.color);
},
displayCommonChannels: function() {
var common_channels_html = [];
// Do the html for each channel
_.each(this.model.member_info.common_channels, function(channel) {
var clean_channel = channel.replace('#', '');
common_channels_html.push('<a class="chan ' + clean_channel + '" data-channel="' + channel + '">' + _.escape(channel) + '</a>');
});
$('#kiwi .right_bar .queries .' + this.model.panel.cid + ' .common_channels').append(common_channels_html.join(', '));
},
displayAvatar: function() {
$('#kiwi .queries .' + this.model.panel.cid + ' .query_avatar').append('<img src="' + this.avatar + '" width="160" height="160" />');
},
displayASL: function() {
if(this.model.member_info.query_gender != undefined) {
// Add the gender
$('#kiwi .right_bar .queries .' + this.model.panel.cid + ' .query_gender').text(this.gender_text);
// If there is an gender and an age, seperate with a comma
if(this.model.member_info.query_age != '' && this.model.member_info.gender_text != '') {
this.model.member_info.query_age = ', ' + this.model.member_info.query_age;
}
}
// Add the Age
if(this.model.member_info.query_age != undefined) {
if(this.model.member_info.query_age != '') {
this.model.member_info.query_age = this.model.member_info.query_age + ' ' + text.years_old;
}
$('#kiwi .right_bar .queries .' + this.model.panel.cid + ' .query_age').text(this.model.member_info.query_age);
// If there is an age and info, seperate with a comma
if(this.model.member_info.query_age != '' && this.model.member_info.query_info != '') {
this.model.member_info.query_info = ', ' + this.model.member_info.query_info;
}
}
// Add the Location
if(this.model.member_info.query_info != undefined) {
$('#kiwi .right_bar .queries .' + this.model.panel.cid + ' .query_location').text(this.model.member_info.query_info);
}
},
bindQueryButtons: function() {
var query_nick = this.model.panel.get('name'),
panel_cid = this.model.panel.cid;
// Click on WHOIS button
$('#kiwi .right_bar .queries .' + panel_cid + ' .query_whois').on('click', function () {
var net = kiwi.components.Network();
net.raw('WHOIS :' + query_nick);
});
// Click on Ignore button
$('#kiwi .right_bar .queries .' + panel_cid + ' .query_ignore').on('click', function () {
var ignore_state = toggleIgnore(query_nick);
// If nick is ignored, switch the button text
if(ignore_state) {
$('#kiwi .right_bar .queries .' + panel_cid + ' .query_ignore').html('<i title="' + text.unignore + '" class="fa fa-minus-circle"></i>&nbsp;' + text.unignore);
} else {
$('#kiwi .right_bar .queries .' + panel_cid + ' .query_ignore').html('<i title="' + text.ignore + '" class="fa fa-minus-circle"></i>&nbsp;' + text.ignore);
}
});
}
});
var queryInfo = Backbone.Model.extend({
initialize: function (options) {
this.panel = options.panel;
// Create a new JED instance
this.locale = new Jed({
"domain" : "query_sidebar",
"missing_key_callback" : function(key) {
console.error('[Jed] missing_key_callback:' + key);
},
"locale_data" : locale_strings[kiwi.i18n._textdomain] || locale_strings['en']
});
// Set the locale strings
text = {
whois: this.locale.translate('whois').fetch(),
ignore: this.locale.translate('ignore').fetch(),
unignore: this.locale.translate('unignore').fetch(),
common_channels: this.locale.translate('common_channels').fetch(),
hide_sidebar: this.locale.translate('hide_sidebar').fetch(),
years_old: this.locale.translate('years_old').fetch(),
gender_male: this.locale.translate('gender_male').fetch(),
gender_female: this.locale.translate('gender_female').fetch(),
gender_unknown: this.locale.translate('gender_unknown').fetch()
};
this.collectQueryInfo();
this.view = new querySidebar({model: this});
if(!this.panel.isQuery()) {
// Moving out of a query, put right bar back in place
this.view.cleanup();
} else {
this.view.render();
}
},
collectQueryInfo: function() {
var common_channels = [],
query_nick = this.panel.get('name'),
member_info = '';
// Run through all the open channels to find the ones in common with the current query user
_.each(kiwi.connections.panels(), function(panel) {
if(panel.get('members') != undefined) {
var members = panel.get('members').nick_cache;
// Check if the member is in the channel
for(member in members) {
var regexp = new RegExp(query_nick, 'i'),
panel_name = panel.get('name');
if(member.match(regexp)) {
common_channels.push(panel_name);
/*$('#kiwi .right_bar .queries .' + current_panel + ' .common_channels a.' + clean_panel_name).click(function() {
console.log('ok');
kiwi.connections.active_connection.createAndJoinChannels(panel_name);
kiwi.connections.active_connection.panels.getByName(panel_name).show();
});*/
// We only need to feed the ASL once
if(member_info == '') {
// Find ASL from the member we're opening a query with
var query_member = members[member];
// TODO : make the whole execution wait until the ASL is full
/*if(query_member.get('gender') == undefined) {
delay(2000);
}*/
member_info = {'query_gender': query_member.get('gender'),
'query_age': query_member.get('age'),
'query_info': query_member.get('info')};
}
}
}
}
return member_info;
});
this.member_info = member_info;
// Now we've been through all channels, push them into the final object
this.member_info['common_channels'] = common_channels;
}
});
function toggleIgnore(nick) {
var ignore_list = kiwi.connections.active_connection.get("ignore_list");
var i = ignore_list.indexOf(nick);;
if(i == -1) {
ignore_list.push(nick);
return true;
} else {
ignore_list.splice(i, 1);
return false;
}
}
function getIgnoreStatus(nick) {
var ignore_list = kiwi.connections.active_connection.get("ignore_list");
return ignore_list.indexOf(nick);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment