Client: view.js split up into multiple files
[KiwiIRC.git] / client / assets / src / views / memberlist.js
1 _kiwi.view.MemberList = Backbone.View.extend({
2 tagName: "ul",
3 events: {
4 "click .nick": "nickClick"
5 },
6 initialize: function (options) {
7 this.model.bind('all', this.render, this);
8 $(this.el).appendTo('#kiwi .memberlists');
9 },
10 render: function () {
11 var $this = this.$el;
12 $this.empty();
13 this.model.forEach(function (member) {
14 $this.append(member.view.el);
15 });
16 return this;
17 },
18 nickClick: function (event) {
19 var $target = $(event.currentTarget).parent('li'),
20 member = $target.data('member'),
21 userbox;
22
23 event.stopPropagation();
24
25 // If the userbox already exists here, hide it
26 if ($target.find('.userbox').length > 0) {
27 $('.userbox', this.$el).remove();
28 return;
29 }
30
31 userbox = new _kiwi.view.UserBox();
32 userbox.member = member;
33 userbox.channel = this.model.channel;
34
35 if (!this.model.getByNick(_kiwi.app.connections.active_connection.get('nick')).get('is_op')) {
36 userbox.$el.children('.if_op').remove();
37 }
38
39 var menu = new _kiwi.view.MenuBox(member.get('nick') || 'User');
40 menu.addItem('userbox', userbox.$el);
41 menu.show();
42
43 // Position the userbox + menubox
44 (function() {
45 var t = event.pageY,
46 m_bottom = t + menu.$el.outerHeight(), // Where the bottom of menu will be
47 memberlist_bottom = this.$el.parent().offset().top + this.$el.parent().outerHeight();
48
49 // If the bottom of the userbox is going to be too low.. raise it
50 if (m_bottom > memberlist_bottom){
51 t = memberlist_bottom - menu.$el.outerHeight();
52 }
53
54 // Set the new positon
55 menu.$el.offset({
56 left: _kiwi.app.view.$el.width() - menu.$el.outerWidth() - 20,
57 top: t
58 });
59 }).call(this);
60 },
61 show: function () {
62 $('#kiwi .memberlists').children().removeClass('active');
63 $(this.el).addClass('active');
64 }
65 });