Plugin hook for nick list click
[KiwiIRC.git] / client / src / views / memberlist.js
1 _kiwi.view.MemberList = Backbone.View.extend({
2 tagName: "div",
3 events: {
4 "click .nick": "nickClick",
5 "click .channel_info": "channelInfoClick"
6 },
7
8 initialize: function (options) {
9 this.model.bind('all', this.render, this);
10 this.$el.appendTo('#kiwi .memberlists');
11
12 // Holds meta data. User counts, etc
13 this.$meta = $('<div class="meta"></div>').appendTo(this.$el);
14
15 // The list for holding the nicks
16 this.$list = $('<ul></ul>').appendTo(this.$el);
17 },
18 render: function () {
19 var that = this;
20
21 this.$list.empty();
22 this.model.forEach(function (member) {
23 member.view.$el.data('member', member);
24 that.$list.append(member.view.$el);
25 });
26
27 // User count
28 if(this.model.channel.isActive()) {
29 this.renderMeta();
30 }
31
32 return this;
33 },
34
35 renderMeta: function() {
36 var members_count = this.model.length + ' ' + translateText('client_applets_chanlist_users');
37 this.$meta.text(members_count);
38 },
39
40 nickClick: function (event) {
41 var $target = $(event.currentTarget).parent('li'),
42 member = $target.data('member');
43
44 _kiwi.global.events.emit('nicklist:select', {target: $target, member: member})
45 .then(_.bind(this.openUserMenuForItem, this, $target));
46 },
47
48
49 // Open a user menu for the given userlist item (<li>)
50 openUserMenuForItem: function($target) {
51 var member = $target.data('member'),
52 userbox,
53 are_we_an_op = !!this.model.getByNick(_kiwi.app.connections.active_connection.get('nick')).get('is_op');
54
55 userbox = new _kiwi.view.UserBox();
56 userbox.setTargets(member, this.model.channel);
57 userbox.displayOpItems(are_we_an_op);
58
59 var menu = new _kiwi.view.MenuBox(member.get('nick') || 'User');
60 menu.addItem('userbox', userbox.$el);
61 menu.showFooter(false);
62
63 _kiwi.global.events.emit('usermenu:created', {menu: menu, userbox: userbox})
64 .then(_.bind(function() {
65 menu.show();
66
67 var target_offset = $target.offset(),
68 t = target_offset.top,
69 m_bottom = t + menu.$el.outerHeight(), // Where the bottom of menu will be
70 memberlist_bottom = this.$el.parent().offset().top + this.$el.parent().outerHeight(),
71 l = target_offset.left,
72 m_right = l + menu.$el.outerWidth(), // Where the left of menu will be
73 memberlist_right = this.$el.parent().offset().left + this.$el.parent().outerWidth();
74
75 // If the bottom of the userbox is going to be too low.. raise it
76 if (m_bottom > memberlist_bottom){
77 t = memberlist_bottom - menu.$el.outerHeight();
78 }
79
80 // If the top of the userbox is going to be too high.. lower it
81 if (t < 0){
82 t = 0;
83 }
84
85 // If the right of the userbox is going off screen.. bring it in
86 if (m_right > memberlist_right){
87 l = memberlist_right - menu.$el.outerWidth();
88 }
89
90 // Set the new positon
91 menu.$el.offset({
92 left: l,
93 top: t
94 });
95
96 }, this))
97 .catch(_.bind(function() {
98 userbox = null;
99
100 menu.dispose();
101 menu = null;
102 }, this));
103 },
104
105
106 channelInfoClick: function(event) {
107 new _kiwi.model.ChannelInfo({channel: this.model.channel});
108 },
109
110
111 show: function () {
112 $('#kiwi .memberlists').children().removeClass('active');
113 $(this.el).addClass('active');
114
115 this.renderMeta();
116 }
117 });