Add option to enter channel key on server selection screen
[KiwiIRC.git] / client / assets / dev / view.js
index 41bde15becac6b14c6b27a5779efe58125a25783..e6e5c8d15ab7e991b31e560f5d19dad041e939b7 100644 (file)
@@ -20,11 +20,13 @@ _kiwi.view.MemberList = Backbone.View.extend({
                 .data('member', member);\r
         });\r
     },\r
-    nickClick: function (x) {\r
-        var $target = $(x.currentTarget).parent('li'),\r
+    nickClick: function (event) {\r
+        var $target = $(event.currentTarget).parent('li'),\r
             member = $target.data('member'),\r
             userbox;\r
         \r
+        event.stopPropagation();\r
+\r
         // If the userbox already exists here, hide it\r
         if ($target.find('.userbox').length > 0) {\r
             $('.userbox', this.$el).remove();\r
@@ -33,11 +35,33 @@ _kiwi.view.MemberList = Backbone.View.extend({
 \r
         userbox = new _kiwi.view.UserBox();\r
         userbox.member = member;\r
+        userbox.channel = this.model.channel;\r
+\r
+        if (!this.model.getByNick(_kiwi.gateway.get('nick')).get('is_op')) {\r
+            userbox.$el.children('.if_op').remove();\r
+        }\r
+\r
+        var menu = new _kiwi.view.MenuBox(member.get('nick') || 'User');\r
+        menu.addItem('userbox', userbox.$el);\r
+        menu.show();\r
 \r
-        // Remove any existing userboxes\r
-        $('.userbox', this.$el).remove();\r
+        // Position the userbox + menubox\r
+        (function() {\r
+            var t = event.pageY,\r
+                m_bottom = t + menu.$el.outerHeight(),  // Where the bottom of menu will be\r
+                memberlist_bottom = this.$el.parent().offset().top + this.$el.parent().outerHeight();\r
 \r
-        $target.append(userbox.$el);\r
+            // If the bottom of the userbox is going to be too low.. raise it\r
+            if (m_bottom > memberlist_bottom){\r
+                t = memberlist_bottom - menu.$el.outerHeight();\r
+            }\r
+\r
+            // Set the new positon\r
+            menu.$el.offset({\r
+                left: _kiwi.app.view.$el.width() - menu.$el.outerWidth() - 20,\r
+                top: t\r
+            });\r
+        }).call(this);\r
     },\r
     show: function () {\r
         $('#memberlists').children().removeClass('active');\r
@@ -51,7 +75,13 @@ _kiwi.view.UserBox = Backbone.View.extend({
     events: {\r
         'click .query': 'queryClick',\r
         'click .info': 'infoClick',\r
-        'click .slap': 'slapClick'\r
+        'click .slap': 'slapClick',\r
+        'click .op': 'opClick',\r
+        'click .deop': 'deopClick',\r
+        'click .voice': 'voiceClick',\r
+        'click .devoice': 'devoiceClick',\r
+        'click .kick': 'kickClick',\r
+        'click .ban': 'banClick'\r
     },\r
 \r
     initialize: function () {\r
@@ -70,6 +100,32 @@ _kiwi.view.UserBox = Backbone.View.extend({
 \r
     slapClick: function (event) {\r
         _kiwi.app.controlbox.processInput('/slap ' + this.member.get('nick'));\r
+    },\r
+\r
+    opClick: function (event) {\r
+        _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' +o ' + this.member.get('nick'));\r
+    },\r
+\r
+    deopClick: function (event) {\r
+        _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' -o ' + this.member.get('nick'));\r
+    },\r
+\r
+    voiceClick: function (event) {\r
+        _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' +v ' + this.member.get('nick'));\r
+    },\r
+\r
+    devoiceClick: function (event) {\r
+        _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' -v ' + this.member.get('nick'));\r
+    },\r
+\r
+    kickClick: function (event) {\r
+        // TODO: Enable the use of a custom kick message\r
+        _kiwi.app.controlbox.processInput('/kick ' + this.member.get('nick') + ' Bye!');\r
+    },\r
+\r
+    banClick: function (event) {\r
+        // TODO: Set ban on host, not just on nick\r
+        _kiwi.app.controlbox.processInput('/mode ' + this.channel.get('name') + ' +b ' + this.member.get('nick') + '!*');\r
     }\r
 });\r
 \r
@@ -113,7 +169,8 @@ _kiwi.view.ServerSelect = function () {
         events: {\r
             'submit form': 'submitForm',\r
             'click .show_more': 'showMore',\r
-            'change .have_pass input': 'showPass'\r
+            'change .have_pass input': 'showPass',\r
+            'change .have_key input': 'showKey'\r
         },\r
 \r
         initialize: function () {\r
@@ -199,6 +256,14 @@ _kiwi.view.ServerSelect = function () {
             }\r
         },\r
 \r
+        showKey: function (event) {\r
+            if (this.$el.find('tr.have_key input').is(':checked')) {\r
+                this.$el.find('tr.key').show().find('input').focus();\r
+            } else {\r
+                this.$el.find('tr.key').hide().find('input').val('');\r
+            }\r
+        },\r
+\r
         showMore: function (event) {\r
             $('.more', this.$el).slideDown('fast');\r
             $('input.server', this.$el).select();\r
@@ -221,9 +286,17 @@ _kiwi.view.ServerSelect = function () {
             $('input.server', this.$el).val(server);\r
             $('input.port', this.$el).val(port);\r
             $('input.ssl', this.$el).prop('checked', ssl);\r
+            $('input#server_select_show_pass', this.$el).prop('checked', !(!password));\r
             $('input.password', this.$el).val(password);\r
+            if (!(!password)) {\r
+                $('tr.pass', this.$el).show();\r
+            }\r
             $('input.channel', this.$el).val(channel);\r
+            $('input#server_select_show_channel_key', this.$el).prop('checked', !(!channel_key));\r
             $('input.channel_key', this.$el).val(channel_key);\r
+            if (!(!channel_key)) {\r
+                $('tr.key', this.$el).show();\r
+            }\r
         },\r
 \r
         hide: function () {\r
@@ -406,6 +479,7 @@ _kiwi.view.Panel = Backbone.View.extend({
 \r
         } else if (is_highlight) {\r
             _kiwi.app.view.alertWindow('* People are talking!');\r
+            _kiwi.app.view.playSound('highlight');\r
             this.alert('highlight');\r
 \r
         } else {\r
@@ -416,6 +490,11 @@ _kiwi.view.Panel = Backbone.View.extend({
             this.alert('activity');\r
         }\r
 \r
+        if (this.model.isQuery() && !this.model.isActive()) {\r
+            _kiwi.app.view.alertWindow('* People are talking!');\r
+            _kiwi.app.view.playSound('highlight');\r
+        }\r
+\r
         // Update the activity counters\r
         (function () {\r
             // Only inrement the counters if we're not the active panel\r
@@ -671,6 +750,7 @@ _kiwi.view.Tabs = Backbone.View.extend({
 \r
         if (panel.isServer()) {\r
             panel.tab.addClass('server');\r
+            panel.tab.addClass('icon-nonexistant');\r
         }\r
 \r
         panel.tab.data('panel_id', panel.cid)\r
@@ -696,7 +776,7 @@ _kiwi.view.Tabs = Backbone.View.extend({
 \r
         // Only show the part image on non-server tabs\r
         if (!panel.isServer()) {\r
-            panel.tab.append('<span class="part"></span>');\r
+            panel.tab.append('<span class="part icon-nonexistant"></span>');\r
         }\r
     },\r
 \r
@@ -893,12 +973,20 @@ _kiwi.view.ControlBox = Backbone.View.extend({
             }\r
             \r
             (function () {\r
-                var tokens = inp_val.substring(0, inp[0].selectionStart).split(' '),\r
-                    val,\r
-                    p1,\r
-                    newnick,\r
-                    range,\r
-                    nick = tokens[tokens.length - 1];\r
+                var tokens,              // Words before the cursor position\r
+                    val,                 // New value being built up\r
+                    p1,                  // Position in the value just before the nick \r
+                    newnick,             // New nick to be displayed (cycles through)\r
+                    range,               // TextRange for setting new text cursor position\r
+                    nick,                // Current nick in the value\r
+                    trailing = ': ';     // Text to be inserted after a tabbed nick\r
+\r
+                tokens = inp_val.substring(0, inp[0].selectionStart).split(' ');\r
+                if (tokens[tokens.length-1] == ':')\r
+                    tokens.pop();\r
+\r
+                nick  = tokens[tokens.length - 1];\r
+\r
                 if (this.tabcomplete.prefix === '') {\r
                     this.tabcomplete.prefix = nick;\r
                 }\r
@@ -908,21 +996,31 @@ _kiwi.view.ControlBox = Backbone.View.extend({
                 });\r
 \r
                 if (this.tabcomplete.data.length > 0) {\r
+                    // Get the current value before cursor position\r
                     p1 = inp[0].selectionStart - (nick.length);\r
                     val = inp_val.substr(0, p1);\r
+\r
+                    // Include the current selected nick\r
                     newnick = this.tabcomplete.data.shift();\r
                     this.tabcomplete.data.push(newnick);\r
                     val += newnick;\r
+\r
+                    if (inp_val.substr(inp[0].selectionStart, 2) !== trailing)\r
+                        val += trailing;\r
+\r
+                    // Now include the rest of the current value\r
                     val += inp_val.substr(inp[0].selectionStart);\r
+\r
                     inp.val(val);\r
 \r
+                    // Move the cursor position to the end of the nick\r
                     if (inp[0].setSelectionRange) {\r
-                        inp[0].setSelectionRange(p1 + newnick.length, p1 + newnick.length);\r
+                        inp[0].setSelectionRange(p1 + newnick.length + trailing.length, p1 + newnick.length + trailing.length);\r
                     } else if (inp[0].createTextRange) { // not sure if this bit is actually needed....\r
                         range = inp[0].createTextRange();\r
                         range.collapse(true);\r
-                        range.moveEnd('character', p1 + newnick.length);\r
-                        range.moveStart('character', p1 + newnick.length);\r
+                        range.moveEnd('character', p1 + newnick.length + trailing.length);\r
+                        range.moveStart('character', p1 + newnick.length + trailing.length);\r
                         range.select();\r
                     }\r
                 }\r
@@ -971,6 +1069,13 @@ _kiwi.view.ControlBox = Backbone.View.extend({
         if (!this._callbacks['command:' + command]) {\r
             this.trigger('unknown_command', {command: command, params: params});\r
         }\r
+    },\r
+\r
+\r
+    addPluginIcon: function ($icon) {\r
+        var $tool = $('<div class="tool"></div>').append($icon);\r
+        this.$el.find('.input_tools').append($tool);\r
+        _kiwi.app.view.doLayout();\r
     }\r
 });\r
 \r
@@ -992,7 +1097,7 @@ _kiwi.view.StatusMessage = Backbone.View.extend({
         opt.timeout = opt.timeout || 5000;\r
 \r
         this.$el.text(text).attr('class', opt.type);\r
-        this.$el.slideDown(_kiwi.app.view.doLayout);\r
+        this.$el.slideDown($.proxy(_kiwi.app.view.doLayout, this));\r
 \r
         if (opt.timeout) this.doTimeout(opt.timeout);\r
     },\r
@@ -1010,7 +1115,7 @@ _kiwi.view.StatusMessage = Backbone.View.extend({
     },\r
 \r
     hide: function () {\r
-        this.$el.slideUp(_kiwi.app.view.doLayout);\r
+        this.$el.slideUp($.proxy(_kiwi.app.view.doLayout, this));\r
     },\r
 \r
     doTimeout: function (length) {\r
@@ -1072,9 +1177,11 @@ _kiwi.view.AppToolbar = Backbone.View.extend({
 \r
 _kiwi.view.Application = Backbone.View.extend({\r
     initialize: function () {\r
-        $(window).resize(this.doLayout);\r
-        $('#toolbar').resize(this.doLayout);\r
-        $('#controlbox').resize(this.doLayout);\r
+        var that = this;\r
+\r
+        $(window).resize(function() { that.doLayout.apply(that); });\r
+        $('#toolbar').resize(function() { that.doLayout.apply(that); });\r
+        $('#controlbox').resize(function() { that.doLayout.apply(that); });\r
 \r
         // Change the theme when the config is changed\r
         _kiwi.global.settings.on('change:theme', this.updateTheme, this);\r
@@ -1083,6 +1190,9 @@ _kiwi.view.Application = Backbone.View.extend({
         _kiwi.global.settings.on('change:channel_list_style', this.setTabLayout, this);\r
         this.setTabLayout(_kiwi.global.settings.get('channel_list_style'));\r
 \r
+        _kiwi.global.settings.on('change:show_timestamps', this.displayTimestamps, this);\r
+        this.displayTimestamps(_kiwi.global.settings.get('show_timestamps'));\r
+\r
         this.doLayout();\r
 \r
         $(document).keydown(this.setKeyFocus);\r
@@ -1093,6 +1203,8 @@ _kiwi.view.Application = Backbone.View.extend({
                 return 'This will close all KiwiIRC conversations. Are you sure you want to close this window?';\r
             }\r
         };\r
+\r
+        this.initSound();\r
     },\r
 \r
 \r
@@ -1132,6 +1244,20 @@ _kiwi.view.Application = Backbone.View.extend({
     },\r
 \r
 \r
+    displayTimestamps: function (show_timestamps) {\r
+        // If called by the settings callback, get the correct new_value\r
+        if (show_timestamps === _kiwi.global.settings) {\r
+            show_timestamps = arguments[1];\r
+        }\r
+\r
+        if (show_timestamps) {\r
+            this.$el.addClass('timestamps');\r
+        } else {\r
+            this.$el.removeClass('timestamps');\r
+        }\r
+    },\r
+\r
+\r
     // Globally shift focus to the command input box on a keypress\r
     setKeyFocus: function (ev) {\r
         // If we're copying text, don't shift focus\r
@@ -1149,12 +1275,12 @@ _kiwi.view.Application = Backbone.View.extend({
 \r
 \r
     doLayout: function () {\r
-        var el_kiwi = $('#kiwi');\r
-        var el_panels = $('#panels');\r
-        var el_memberlists = $('#memberlists');\r
-        var el_toolbar = $('#toolbar');\r
-        var el_controlbox = $('#controlbox');\r
-        var el_resize_handle = $('#memberlists_resize_handle');\r
+        var el_kiwi = this.$el;\r
+        var el_panels = $('#kiwi #panels');\r
+        var el_memberlists = $('#kiwi #memberlists');\r
+        var el_toolbar = $('#kiwi #toolbar');\r
+        var el_controlbox = $('#kiwi #controlbox');\r
+        var el_resize_handle = $('#kiwi #memberlists_resize_handle');\r
 \r
         var css_heights = {\r
             top: el_toolbar.outerHeight(true),\r
@@ -1178,7 +1304,7 @@ _kiwi.view.Application = Backbone.View.extend({
 \r
         // If we have channel tabs on the side, adjust the height\r
         if (el_kiwi.hasClass('chanlist_treeview')) {\r
-            $('#kiwi #tabs').css(css_heights);\r
+            $('#tabs', el_kiwi).css(css_heights);\r
         }\r
 \r
         // Determine if we have a narrow window (mobile/tablet/or even small desktop window)\r
@@ -1200,6 +1326,9 @@ _kiwi.view.Application = Backbone.View.extend({
             // And move the handle just out of sight to the right\r
             el_resize_handle.css('left', el_panels.outerWidth(true));\r
         }\r
+\r
+        var input_wrap_width = parseInt($('#kiwi #controlbox .input_tools').outerWidth());\r
+        el_controlbox.find('.input_wrap').css('right', input_wrap_width + 7);\r
     },\r
 \r
 \r
@@ -1278,8 +1407,8 @@ _kiwi.view.Application = Backbone.View.extend({
         var that = this;\r
 \r
         if (!instant) {\r
-            $('#toolbar').slideUp({queue: false, duration: 400, step: this.doLayout});\r
-            $('#controlbox').slideUp({queue: false, duration: 400, step: this.doLayout});\r
+            $('#toolbar').slideUp({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});\r
+            $('#controlbox').slideUp({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});\r
         } else {\r
             $('#toolbar').slideUp(0);\r
             $('#controlbox').slideUp(0);\r
@@ -1291,13 +1420,47 @@ _kiwi.view.Application = Backbone.View.extend({
         var that = this;\r
 \r
         if (!instant) {\r
-            $('#toolbar').slideDown({queue: false, duration: 400, step: this.doLayout});\r
-            $('#controlbox').slideDown({queue: false, duration: 400, step: this.doLayout});\r
+            $('#toolbar').slideDown({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});\r
+            $('#controlbox').slideDown({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});\r
         } else {\r
             $('#toolbar').slideDown(0);\r
             $('#controlbox').slideDown(0);\r
             this.doLayout();\r
         }\r
+    },\r
+\r
+\r
+    initSound: function () {\r
+        var that = this,\r
+            base_path = this.model.get('base_path');\r
+\r
+        $script(base_path + '/assets/libs/soundmanager2/soundmanager2-nodebug-jsmin.js', function() {\r
+            if (typeof soundManager === 'undefined')\r
+                return;\r
+\r
+            soundManager.setup({\r
+                url: base_path + '/assets/libs/soundmanager2/',\r
+                flashVersion: 9, // optional: shiny features (default = 8)// optional: ignore Flash where possible, use 100% HTML5 mode\r
+                preferFlash: true,\r
+\r
+                onready: function() {\r
+                    that.sound_object = soundManager.createSound({\r
+                        id: 'highlight',\r
+                        url: base_path + '/assets/sound/highlight.mp3'\r
+                    });\r
+                }\r
+            });\r
+        });\r
+    },\r
+\r
+\r
+    playSound: function (sound_id) {\r
+        if (!this.sound_object) return;\r
+\r
+        if (_kiwi.global.settings.get('mute_sounds'))\r
+            return;\r
+        \r
+        soundManager.play(sound_id);\r
     }\r
 });\r
 \r
@@ -1428,4 +1591,96 @@ _kiwi.view.MediaMessage = Backbone.View.extend({
 \r
         return html;\r
     }\r
-});
\ No newline at end of file
+});\r
+\r
+\r
+\r
+_kiwi.view.MenuBox = Backbone.View.extend({\r
+    events: {\r
+        'click .ui_menu_foot .close': 'dispose'\r
+    },\r
+\r
+    initialize: function(title) {\r
+        var that = this;\r
+\r
+        this.$el = $('<div class="ui_menu"></div>');\r
+\r
+        this._title = title || '';\r
+        this._items = {};\r
+        this._display_footer = true;\r
+\r
+        this._close_proxy = function(event) {\r
+            that.onDocumentClick(event);\r
+        };\r
+        $(document).on('click', this._close_proxy);\r
+    },\r
+\r
+\r
+    render: function() {\r
+        var that = this;\r
+\r
+        this.$el.find('*').remove();\r
+\r
+        if (this._title) {\r
+            $('<div class="ui_menu_title"></div>')\r
+                .text(this._title)\r
+                .appendTo(this.$el);\r
+        }\r
+\r
+\r
+        _.each(this._items, function(item) {\r
+            var $item = $('<div class="ui_menu_content hover"></div>')\r
+                .append(item);\r
+\r
+            that.$el.append($item);\r
+        });\r
+\r
+        if (this._display_footer)\r
+            this.$el.append('<div class="ui_menu_foot"><a class="close" onclick="">Close <i class="icon-remove"></i></a></div>');\r
+    },\r
+\r
+\r
+    onDocumentClick: function(event) {\r
+        var $target = $(event.target);\r
+\r
+        // If this is not itself AND we don't contain this element, dispose $el\r
+        if ($target[0] != this.$el[0] && this.$el.has($target).length === 0)\r
+            this.dispose();\r
+    },\r
+\r
+\r
+    dispose: function() {\r
+        _.each(this._items, function(item) {\r
+            item.dispose && item.dispose();\r
+            item.remove && item.remove();\r
+        });\r
+\r
+        this._items = null;\r
+        this.remove();\r
+\r
+        $(document).off('click', this._close_proxy);\r
+    },\r
+\r
+\r
+    addItem: function(item_name, $item) {\r
+        $item = $($item);\r
+        if ($item.is('a')) $item.addClass('icon-chevron-right');\r
+        this._items[item_name] = $item;\r
+    },\r
+\r
+\r
+    removeItem: function(item_name) {\r
+        delete this._items[item_name];\r
+    },\r
+\r
+\r
+    showFooter: function(show) {\r
+        this._show_footer = show;\r
+    },\r
+\r
+\r
+    show: function() {\r
+        this.render();\r
+        this.$el.appendTo(_kiwi.app.view.$el);\r
+    }\r
+});\r