From 85c0725f83b27978fcb78cdd18d3521b088c5f11 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Sun, 25 Apr 2010 20:14:33 +0100 Subject: [PATCH] add a method to get the current states in the filter --- deluge/plugins/label/label/data/label.js | 65 ++++++++++++++++++++-- deluge/ui/web/js/deluge-all/FilterPanel.js | 13 ++++- deluge/ui/web/js/deluge-all/Sidebar.js | 4 +- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/deluge/plugins/label/label/data/label.js b/deluge/plugins/label/label/data/label.js index 49a0259a1..cca4b47a2 100644 --- a/deluge/plugins/label/label/data/label.js +++ b/deluge/plugins/label/label/data/label.js @@ -78,7 +78,9 @@ Deluge.ux.AddLabelWindow = Ext.extend(Ext.Window, { deluge.client.label.add(label, { success: function() { deluge.ui.update(); - } + this.fireEvent('labeladded', label); + }, + scope: this }); this.hide(); }, @@ -181,7 +183,39 @@ Deluge.plugins.LabelPlugin = Ext.extend(Deluge.Plugin, { onEnable: function() { deluge.sidebar.on('filtercreate', this.onFilterCreate, this); + deluge.sidebar.on('afterfiltercreate', this.onAfterFilterCreate, this); Deluge.FilterPanel.templates.label = '
{filter}no label ({count})
'; + + deluge.menus.torrent.add({ + xtype: 'menuseparator' + }); + + this.torrentMenu = new Ext.menu.Menu({ + items: [{ + text: _('No Label'), + label: '', + handler: this.onTorrentMenuClick, + scope: this + }] + }); + deluge.menus.torrent.add({ + text: _('Label'), + menu: this.torrentMenu + }) + }, + + onAfterFilterCreate: function(sidebar, filter) { + if (filter.filter != 'label') return; + + Ext.each(filter.getStates(), function(state) { + if (!state) return; + this.torrentMenu.addMenuItem({ + text: state, + label: state, + handler: this.onTorrentMenuClick, + scope: this + }); + }, this); }, onFilterCreate: function(sidebar, filter) { @@ -193,10 +227,22 @@ Deluge.plugins.LabelPlugin = Ext.extend(Deluge.Plugin, { }, onLabelAddClick: function() { - if (!this.addWindow) this.addWindow = new Deluge.ux.AddLabelWindow(); + if (!this.addWindow) { + this.addWindow = new Deluge.ux.AddLabelWindow(); + this.addWindow.on('labeladded', this.onLabelAdded, this); + } this.addWindow.show(); }, + onLabelAdded: function(label) { + this.torrentMenu.addMenuItem({ + text: label, + label: label, + handler: this.onTorrentMenuClick, + scope: this + }); + }, + onLabelContextMenu: function(dv, i, node, e) { e.preventDefault(); if (!this.labelMenu) this.createMenu(); @@ -224,11 +270,22 @@ Deluge.plugins.LabelPlugin = Ext.extend(Deluge.Plugin, { }, onLabelRemoveClick: function() { - deluge.client.label.remove(this.filter.getFilter(), { + var state = this.filter.getFilter(); + deluge.client.label.remove(state, { success: function() { deluge.ui.update(); - } + this.torrentMenu.items.each(function(item) { + if (item.text != state) return; + this.torrentMenu.remove(item); + var i = item; + }, this); + }, + scope: this }); + }, + + onTorrentMenuClick: function(item, e) { + alert(item.label); } }); Deluge.registerPlugin('Label', Deluge.plugins.LabelPlugin); diff --git a/deluge/ui/web/js/deluge-all/FilterPanel.js b/deluge/ui/web/js/deluge-all/FilterPanel.js index 09c2153b3..70e479ac3 100644 --- a/deluge/ui/web/js/deluge-all/FilterPanel.js +++ b/deluge/ui/web/js/deluge-all/FilterPanel.js @@ -97,6 +97,17 @@ Deluge.FilterPanel = Ext.extend(Ext.Panel, { return filter.id; }, + /** + * Return the current states in the filter + */ + getStates: function() { + var states = []; + this.list.getStore().each(function(r) { + states.push(r.get('filter')); + }); + return states; + }, + /** * Return the Store for the ListView of the FilterPanel * @returns {Ext.data.Store} the ListView store @@ -130,7 +141,7 @@ Deluge.FilterPanel = Ext.extend(Ext.Panel, { count: s[1] }); record.id = s[0]; - store.insert(i, [record]); + store.insert(i, record); } record.beginEdit(); record.set('filter', s[0]); diff --git a/deluge/ui/web/js/deluge-all/Sidebar.js b/deluge/ui/web/js/deluge-all/Sidebar.js index ac86fa0b5..8751491e8 100644 --- a/deluge/ui/web/js/deluge-all/Sidebar.js +++ b/deluge/ui/web/js/deluge-all/Sidebar.js @@ -77,12 +77,14 @@ Deluge.Sidebar = Ext.extend(Ext.Panel, { panel.on('selectionchange', function(view, nodes) { deluge.ui.update(); }); - panel.updateStates(states); this.add(panel); this.doLayout(); this.panels[filter] = panel; this.fireEvent('filtercreate', this, panel); + + panel.updateStates(states); + this.fireEvent('afterfiltercreate', this, panel); }, getFilters: function() {