source: deluge/ui/web/js/Deluge.Add.js@ 8582915

2.0.x develop extjs4-port
Last change on this file since 8582915 was 8582915, checked in by Damien Churchill <damoc@gmail.com>, 16 years ago

add torrents to be added to the grid before their info has been
retrieved with a url or filename in place of the torrent name

  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2Script: Deluge.Add.js
3 Contains the Add Torrent window.
4
5Copyright:
6 (C) Damien Churchill 2009 <damoxc@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, write to:
19 The Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor
21 Boston, MA 02110-1301, USA.
22*/
23
24Ext.namespace('Ext.deluge.add');
25Ext.deluge.add.OptionsPanel = Ext.extend(Ext.TabPanel, {
26
27 constructor: function(config) {
28 config = Ext.apply({
29 region: 'south',
30 margins: '5 5 5 5',
31 activeTab: 0,
32 height: 220
33 }, config);
34 Ext.deluge.add.OptionsPanel.superclass.constructor.call(this, config);
35 },
36
37 initComponent: function() {
38 Ext.deluge.add.OptionsPanel.superclass.initComponent.call(this);
39 this.files = this.add(new Ext.tree.ColumnTree({
40 layout: 'fit',
41 title: _('Files'),
42 rootVisible: false,
43 autoScroll: true,
44 height: 170,
45 border: false,
46 animate: false,
47
48 columns: [{
49 header: _('Filename'),
50 width: 275,
51 dataIndex: 'filename'
52 },{
53 header: _('Size'),
54 width: 80,
55 dataIndex: 'size'
56 }],
57
58 root: new Ext.tree.AsyncTreeNode({
59 text: 'Files'
60 })
61 }));
62 new Ext.tree.TreeSorter(this.files, {
63 folderSort: true
64 });
65
66 this.form = this.add({
67 xtype: 'form',
68 labelWidth: 1,
69 frame: false,
70 title: _('Options'),
71 bodyStyle: 'padding: 5px;',
72 border: false,
73
74
75 items: [{
76 xtype: 'fieldset',
77 title: _('Download Location'),
78 border: false,
79 defaultType: 'textfield',
80 labelWidth: 1,
81 items: [{
82 fieldLabel: '',
83 labelSeperator: '',
84 name: 'download_location',
85 width: 330
86 }]
87 }]
88 });
89 },
90
91 clear: function() {
92 this.clearFiles();
93 },
94
95 clearFiles: function() {
96 var root = this.files.getRootNode();
97 if (!root.hasChildNodes()) return;
98 root.cascade(function(node) {
99 if (!node.parentNode || !node.getOwnerTree()) return;
100 node.remove();
101 });
102 },
103
104 getDefaults: function() {
105 var keys = [
106 'add_paused',
107 'compact_allocation',
108 'download_location',
109 'max_connections_per_torrent',
110 'max_download_speed_per_torrent',
111 'max_upload_slots_per_torrent',
112 'max_upload_speed_per_torrent',
113 'prioritize_first_last_pieces'
114 ]
115 Deluge.Client.core.get_config_values(keys, {
116 success: function(config) {
117 this.defaults = config;
118 for (var key in config) {
119 var field = this.form.findField(key);
120 if (!field) return;
121 field.setValue(config[key]);
122 }
123 var field = this.form.findField('compact_allocation');
124 if (config['compact_allocation']) {
125 field.items.get('compact_allocation_true').setValue(true);
126 field.items.get('compact_allocation_false').setValue(false);
127 } else {
128 field.items.get('compact_allocation_false').setValue(true);
129 field.items.get('compact_allocation_true').setValue(false);
130 }
131 },
132 scope: this
133 });
134 }
135});
136
137Ext.deluge.add.Window = Ext.extend(Ext.Window, {
138 initComponent: function() {
139 Ext.deluge.add.Window.superclass.initComponent.call(this);
140 this.addEvents(
141 'beforeadd',
142 'add'
143 );
144 },
145
146 createTorrentId: function() {
147 return new Date().getTime();
148 }
149});
150
151Ext.deluge.add.AddWindow = Ext.extend(Ext.deluge.add.Window, {
152
153 torrents: {},
154
155 constructor: function(config) {
156 config = Ext.apply({
157 title: _('Add Torrents'),
158 layout: 'border',
159 width: 470,
160 height: 450,
161 bodyStyle: 'padding: 10px 5px;',
162 buttonAlign: 'right',
163 closeAction: 'hide',
164 closable: true,
165 plain: true,
166 iconCls: 'x-deluge-add-window-icon'
167 }, config);
168 Ext.deluge.add.AddWindow.superclass.constructor.call(this, config);
169 },
170
171 initComponent: function() {
172 Ext.deluge.add.AddWindow.superclass.initComponent.call(this);
173
174 this.addButton(_('Cancel'), this.onCancel, this);
175 this.addButton(_('Add'), this.onAdd, this);
176
177 function torrentRenderer(value, p, r) {
178 if (r.data['infohash']) {
179 return String.format('<div class="x-add-torrent-name">{0}</div>', value);
180 } else {
181 return String.format('<div class="x-add-torrent-name-loading">{0}</div>', value);
182 }
183 }
184
185 this.grid = this.add({
186 xtype: 'grid',
187 region: 'center',
188 store: new Ext.data.SimpleStore({
189 fields: [
190 {name: 'info_hash', mapping: 1},
191 {name: 'text', mapping: 2}
192 ],
193 id: 0
194 }),
195 columns: [{
196 id: 'torrent',
197 width: 150,
198 sortable: true,
199 renderer: torrentRenderer,
200 dataIndex: 'text'
201 }],
202 stripeRows: true,
203 selModel: new Ext.grid.RowSelectionModel({
204 singleSelect: true,
205 listeners: {
206 'rowselect': {
207 fn: this.onSelect,
208 scope: this
209 }
210 }
211 }),
212 hideHeaders: true,
213 autoExpandColumn: 'torrent',
214 deferredRender: false,
215 autoScroll: true,
216 margins: '5 5 5 5',
217 bbar: new Ext.Toolbar({
218 items: [{
219 id: 'file',
220 cls: 'x-btn-text-icon',
221 iconCls: 'x-deluge-add-file',
222 text: _('File'),
223 handler: this.onFile,
224 scope: this
225 }, {
226 id: 'url',
227 cls: 'x-btn-text-icon',
228 text: _('Url'),
229 icon: '/icons/add_url.png',
230 handler: this.onUrl,
231 scope: this
232 }, {
233 id: 'infohash',
234 cls: 'x-btn-text-icon',
235 text: _('Infohash'),
236 icon: '/icons/add_magnet.png',
237 disabled: true
238 }, '->', {
239 id: 'remove',
240 cls: 'x-btn-text-icon',
241 text: _('Remove'),
242 icon: '/icons/remove.png',
243 handler: this.onRemove,
244 scope: this
245 }]
246 })
247 });
248
249 this.options = this.add(new Ext.deluge.add.OptionsPanel());
250 this.on('show', this.onShow, this);
251 },
252
253 clear: function() {
254 this.torrents = {};
255 this.grid.getStore().removeAll();
256 this.options.clear();
257 },
258
259 onAdd: function() {
260 torrents = [];
261 for (var id in this.torrents) {
262 var info = this.torrents[id];
263 torrents.push({
264 path: info['filename'],
265 options: {}
266 });
267 }
268 Deluge.Client.web.add_torrents(torrents, {
269 success: function(result) {
270 }
271 })
272 this.clear();
273 this.hide();
274 },
275
276 onCancel: function() {
277 this.clear();
278 this.hide();
279 },
280
281 onFile: function() {
282 this.file.show();
283 },
284
285 onRemove: function() {
286 var selection = this.grid.getSelectionModel();
287 if (!selection.hasSelection()) return;
288 var torrent = selection.getSelected();
289
290 delete this.torrents[torrent.id];
291 this.grid.getStore().remove(torrent);
292 this.options.clear();
293 },
294
295 onSelect: function(selModel, rowIndex, record) {
296 var torrentInfo = this.torrents[record.get('info_hash')];
297
298 function walk(files, parent) {
299 for (var file in files) {
300 var item = files[file];
301
302 if (Ext.type(item) == 'object') {
303 var child = new Ext.tree.TreeNode({
304 text: file
305 });
306 walk(item, child);
307 parent.appendChild(child);
308 } else {
309 parent.appendChild(new Ext.tree.TreeNode({
310 filename: file,
311 text: file, // this needs to be here for sorting reasons
312 size: fsize(item[0]),
313 leaf: true,
314 checked: item[1],
315 iconCls: 'x-deluge-file',
316 uiProvider: Ext.tree.ColumnNodeUI
317 }));
318 }
319 }
320 }
321
322 this.options.clearFiles();
323 var root = this.options.files.getRootNode();
324 walk(torrentInfo['files_tree'], root);
325 root.firstChild.expand();
326 },
327
328 onShow: function() {
329 if (!this.url) {
330 this.url = new Ext.deluge.add.UrlWindow();
331 this.url.on('beforeadd', this.onTorrentBeforeAdd, this);
332 this.url.on('add', this.onTorrentAdd, this);
333 }
334
335 if (!this.file) {
336 this.file = new Ext.deluge.add.FileWindow();
337 this.file.on('beforeadd', this.onTorrentBeforeAdd, this);
338 this.file.on('add', this.onTorrentAdd, this);
339 }
340 },
341
342 onTorrentBeforeAdd: function(torrentId, text) {
343 var store = this.grid.getStore();
344 store.loadData([[torrentId, null, text]], true);
345 },
346
347 onTorrentAdd: function(torrentId, info) {
348 if (!info) {
349 Ext.MessageBox.show({
350 title: _('Error'),
351 msg: _('Not a valid torrent'),
352 buttons: Ext.MessageBox.OK,
353 modal: false,
354 icon: Ext.MessageBox.ERROR,
355 iconCls: 'x-deluge-icon-error'
356 });
357 return;
358 }
359
360 var r = this.grid.getStore().getById(torrentId);
361 r.set('info_hash', info['info_hash']);
362 r.set('text', info['name']);
363 this.grid.getStore().commitChanges();
364 this.torrents[info['info_hash']] = info;
365 },
366
367 onUrl: function(button, event) {
368 this.url.show();
369 }
370});
371Deluge.Add = new Ext.deluge.add.AddWindow();
Note: See TracBrowser for help on using the repository browser.