move the state updating into the FilterPanel to allow for a custom show_zero value to

be specified
This commit is contained in:
Damien Churchill 2010-04-25 19:07:26 +01:00
parent c8ada0ba07
commit 80f151be94
2 changed files with 48 additions and 54 deletions

View File

@ -39,6 +39,8 @@ Deluge.FilterPanel = Ext.extend(Ext.Panel, {
border: false,
show_zero: null,
initComponent: function() {
Deluge.FilterPanel.superclass.initComponent.call(this);
this.filterType = this.initialConfig.filter;
@ -101,7 +103,50 @@ Deluge.FilterPanel = Ext.extend(Ext.Panel, {
*/
getStore: function() {
return this.list.getStore();
},
/**
* Update the states in the FilterPanel
*/
updateStates: function(states) {
var show_zero = (this.show_zero == null) ? deluge.config.sidebar_show_zero : this.show_zero;
if (!show_zero) {
var newStates = [];
Ext.each(states, function(state) {
if (state[1] > 0 || state[0] == _('All')) {
newStates.push(state);
}
});
states = newStates;
}
var store = this.getStore();
var filters = {};
Ext.each(states, function(s, i) {
var record = store.getById(s[0]);
if (!record) {
record = new store.recordType({
filter: s[0],
count: s[1]
});
record.id = s[0];
store.insert(i, [record]);
}
record.beginEdit();
record.set('filter', s[0]);
record.set('count', s[1]);
record.endEdit();
filters[s[0]] = true;
}, this);
store.each(function(record) {
if (filters[record.id]) return;
store.remove(record);
}, this);
store.commitChanges();
},
});
Deluge.FilterPanel.templates = {

View File

@ -77,12 +77,7 @@ Deluge.Sidebar = Ext.extend(Ext.Panel, {
panel.on('selectionchange', function(view, nodes) {
deluge.ui.update();
});
if (deluge.config.sidebar_show_zero == false) {
states = this.removeZero(states);
}
panel.getStore().loadData(states);
panel.updateStates(states);
this.add(panel);
this.doLayout();
@ -116,24 +111,11 @@ Deluge.Sidebar = Ext.extend(Ext.Panel, {
deluge.ui.update();
},
/**
* Remove the states with zero torrents in them.
*/
removeZero: function(states) {
var newStates = [];
Ext.each(states, function(state) {
if (state[1] > 0 || state[0] == _('All')) {
newStates.push(state);
}
});
return newStates;
},
update: function(filters) {
for (var filter in filters) {
var states = filters[filter];
if (Ext.getKeys(this.panels).indexOf(filter) > -1) {
this.updateFilter(filter, states);
this.panels[filter].updateStates(states);
} else {
this.createFilter(filter, states);
}
@ -148,38 +130,5 @@ Deluge.Sidebar = Ext.extend(Ext.Panel, {
delete this.panels[filter];
}
}, this);
},
updateFilter: function(filter, states) {
if (deluge.config.sidebar_show_zero == false) {
states = this.removeZero(states);
}
var store = this.panels[filter].getStore();
var filters = [];
Ext.each(states, function(s, i) {
var record = store.getById(s[0]);
if (!record) {
record = new store.recordType({
filter: s[0],
count: s[1]
});
record.id = s[0];
store.insert(i, [record]);
}
record.beginEdit();
record.set('filter', s[0]);
record.set('count', s[1]);
record.endEdit();
filters[s[0]] = true;
}, this);
store.each(function(record) {
if (filters[record.id]) return;
store.remove(record);
}, this);
store.commitChanges();
}
});