diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2013-08-18 17:01:17 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2013-08-18 17:01:17 +0200 |
commit | 9a6ea22616cf3cc67e292c908521b79764400faf (patch) | |
tree | 1924843f28d992490d867d0557da90dfb1da6404 /pyload/web/app/scripts | |
parent | fixed login (diff) | |
download | pyload-9a6ea22616cf3cc67e292c908521b79764400faf.tar.xz |
new linkgrabber
Diffstat (limited to 'pyload/web/app/scripts')
-rw-r--r-- | pyload/web/app/scripts/collections/LinkList.js | 14 | ||||
-rw-r--r-- | pyload/web/app/scripts/config.js | 6 | ||||
-rw-r--r-- | pyload/web/app/scripts/helpers/linkStatus.js | 18 | ||||
-rw-r--r-- | pyload/web/app/scripts/models/CollectorPackage.js | 77 | ||||
-rw-r--r-- | pyload/web/app/scripts/models/LinkStatus.js | 22 | ||||
-rw-r--r-- | pyload/web/app/scripts/views/abstract/modalView.js | 6 | ||||
-rw-r--r-- | pyload/web/app/scripts/views/headerView.js | 2 | ||||
-rw-r--r-- | pyload/web/app/scripts/views/linkGrabberModal.js | 49 | ||||
-rw-r--r-- | pyload/web/app/scripts/views/linkgrabber/collectorView.js | 21 | ||||
-rw-r--r-- | pyload/web/app/scripts/views/linkgrabber/modalView.js | 61 | ||||
-rw-r--r-- | pyload/web/app/scripts/views/linkgrabber/packageView.js | 58 |
11 files changed, 281 insertions, 53 deletions
diff --git a/pyload/web/app/scripts/collections/LinkList.js b/pyload/web/app/scripts/collections/LinkList.js new file mode 100644 index 000000000..170a2c039 --- /dev/null +++ b/pyload/web/app/scripts/collections/LinkList.js @@ -0,0 +1,14 @@ +define(['jquery', 'backbone', 'underscore', 'models/LinkStatus'], function($, Backbone, _, LinkStatus) { + 'use strict'; + + return Backbone.Collection.extend({ + + model: LinkStatus, + + comparator: function(link) { + return link.get('name'); + } + + }); + +});
\ No newline at end of file diff --git a/pyload/web/app/scripts/config.js b/pyload/web/app/scripts/config.js index ff4082ce4..51ea63285 100644 --- a/pyload/web/app/scripts/config.js +++ b/pyload/web/app/scripts/config.js @@ -59,8 +59,10 @@ require.config({ deps: ['underscore', 'jquery'], exports: 'Backbone' }, - - marionette: ['backbone'], + marionette: { + deps: ['backbone'], + exports: 'Backbone' + }, handlebars: { exports: 'Handlebars' }, diff --git a/pyload/web/app/scripts/helpers/linkStatus.js b/pyload/web/app/scripts/helpers/linkStatus.js new file mode 100644 index 000000000..2497785fb --- /dev/null +++ b/pyload/web/app/scripts/helpers/linkStatus.js @@ -0,0 +1,18 @@ +define('helpers/linkStatus', ['underscore', 'handlebars', 'utils/apitypes', 'utils/i18n'], + function(_, Handlebars, Api, i18n) { + 'use strict'; + function linkStatus(status) { + var s; + if (status === Api.DownloadStatus.Online) + s = '<span class="text-success">' + i18n.gettext('online') + '</span>'; + else if (status === Api.DownloadState.Offline) + s = '<span class="text-error">' + i18n.gettext('offline') + '</span>'; + else + s = '<span class="text-info">' + i18n.gettext('unknown') + '</span>'; + + return new Handlebars.SafeString(s); + } + + Handlebars.registerHelper('linkStatus', linkStatus); + return linkStatus; + }); diff --git a/pyload/web/app/scripts/models/CollectorPackage.js b/pyload/web/app/scripts/models/CollectorPackage.js new file mode 100644 index 000000000..293342440 --- /dev/null +++ b/pyload/web/app/scripts/models/CollectorPackage.js @@ -0,0 +1,77 @@ +define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes', 'collections/LinkList'], + function($, Backbone, _, App, Api, LinkList) { + 'use strict'; + return Backbone.Model.extend({ + + idAttribute: 'name', + defaults: { + name: 'Unnamed package', + new_name: null, + links: null + }, + + initialize: function() { + this.set('links', new LinkList()); + }, + + destroy: function() { + // Copied from backbones destroy method + var model = this; + model.trigger('destroy', model, model.collection); + }, + + // get the actual name + getName: function() { + var new_name = this.get('new_name'); + if (new_name) + return new_name; + + return this.get('name'); + + }, + // Add the package to pyload + add: function() { + var self = this; + var links = this.get('links').pluck('url'); + + $.ajax(App.apiRequest('addPackage', + {name: this.getName(), + links: links}, + {success: function() { + self.destroy(); + App.vent.trigger('package:added'); + }})); + + }, + + updateLinks: function(links) { + this.get('links').set(links, {remove: false}); + this.trigger('change'); + }, + + toJSON: function() { + var data = { + name: this.getName(), + links: this.get('links').toJSON() + }; + var links = this.get('links'); + data.length = links.length; + data.online = 0; + data.offline = 0; + data.unknown = 0; + + // Summary + links.each(function(link) { + if (link.get('status') === Api.DownloadStatus.Online) + data.online++; + else if (link.get('status') === Api.DownloadStatus.Offline) + data.offline++; + else + data.unknown++; + }); + + return data; + } + + }); + });
\ No newline at end of file diff --git a/pyload/web/app/scripts/models/LinkStatus.js b/pyload/web/app/scripts/models/LinkStatus.js new file mode 100644 index 000000000..2be1ce368 --- /dev/null +++ b/pyload/web/app/scripts/models/LinkStatus.js @@ -0,0 +1,22 @@ +define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], + function($, Backbone, _, App, Api) { + 'use strict'; + + return Backbone.Model.extend({ + + idAttribute: 'url', + + defaults: { + name: '', + plugin: '', + size: -1, + status: Api.DownloadStatus.Queued + }, + + destroy: function() { + var model = this; + model.trigger('destroy', model, model.collection); + } + + }); + });
\ No newline at end of file diff --git a/pyload/web/app/scripts/views/abstract/modalView.js b/pyload/web/app/scripts/views/abstract/modalView.js index b6d9f0eab..61016a9fb 100644 --- a/pyload/web/app/scripts/views/abstract/modalView.js +++ b/pyload/web/app/scripts/views/abstract/modalView.js @@ -117,10 +117,14 @@ define(['jquery', 'backbone', 'underscore', 'omniwindow'], function($, Backbone, }, destroy: function() { + this.onDestroy(); this.$el.remove(); this.dialog = null; this.remove(); - } + }, + onDestroy: function() { + + } }); });
\ No newline at end of file diff --git a/pyload/web/app/scripts/views/headerView.js b/pyload/web/app/scripts/views/headerView.js index 3fdfe32ba..60a47ad62 100644 --- a/pyload/web/app/scripts/views/headerView.js +++ b/pyload/web/app/scripts/views/headerView.js @@ -172,7 +172,7 @@ define(['jquery', 'underscore', 'backbone', 'app', 'models/ServerStatus', 'colle open_grabber: function() { var self = this; - _.requireOnce(['views/linkGrabberModal'], function(ModalView) { + _.requireOnce(['views/linkgrabber/modalView'], function(ModalView) { if (self.grabber === null) self.grabber = new ModalView(); diff --git a/pyload/web/app/scripts/views/linkGrabberModal.js b/pyload/web/app/scripts/views/linkGrabberModal.js deleted file mode 100644 index e6f59c134..000000000 --- a/pyload/web/app/scripts/views/linkGrabberModal.js +++ /dev/null @@ -1,49 +0,0 @@ -define(['jquery', 'underscore', 'app', 'views/abstract/modalView', 'hbs!tpl/dialogs/linkgrabber'], - function($, _, App, modalView, template) { - 'use strict'; - // Modal dialog for package adding - triggers package:added when package was added - return modalView.extend({ - - events: { - 'click .btn-success': 'addPackage', - 'keypress #inputPackageName': 'addOnEnter' - }, - - template: template, - - initialize: function() { - // Inherit parent events - this.events = _.extend({}, modalView.prototype.events, this.events); - }, - - addOnEnter: function(e) { - if (e.keyCode !== 13) return; - this.addPackage(e); - }, - - addPackage: function(e) { - var self = this; - var options = App.apiRequest('addPackage', - { - name: $('#inputPackageName').val(), - // TODO: better parsing / tokenization - links: $('#inputLinks').val().split('\n') - }, - { - success: function() { - App.vent.trigger('package:added'); - self.hide(); - } - }); - - $.ajax(options); - $('#inputPackageName').val(''); - $('#inputLinks').val(''); - }, - - onShow: function() { - this.$('#inputPackageName').focus(); - } - - }); - });
\ No newline at end of file diff --git a/pyload/web/app/scripts/views/linkgrabber/collectorView.js b/pyload/web/app/scripts/views/linkgrabber/collectorView.js new file mode 100644 index 000000000..d2b43f699 --- /dev/null +++ b/pyload/web/app/scripts/views/linkgrabber/collectorView.js @@ -0,0 +1,21 @@ +define(['jquery', 'underscore', 'backbone', 'app', './packageView'], + function($, _, Backbone, App, packageView) { + 'use strict'; + return Backbone.Marionette.CollectionView.extend({ + itemView: packageView, + updateData: function(result) { + var self = this; + _.each(result.data, function(links, name) { + var pack = self.collection.get(name); + if (!pack) { + pack = new self.collection.model({name: name}); + self.collection.add(pack); + } + + // TODO: remove links from all other packages than pack + pack.updateLinks(links); + }); + } + + }); + });
\ No newline at end of file diff --git a/pyload/web/app/scripts/views/linkgrabber/modalView.js b/pyload/web/app/scripts/views/linkgrabber/modalView.js new file mode 100644 index 000000000..6e4781ac2 --- /dev/null +++ b/pyload/web/app/scripts/views/linkgrabber/modalView.js @@ -0,0 +1,61 @@ +define(['jquery', 'underscore', 'backbone', 'app', 'models/CollectorPackage', 'views/abstract/modalView', './collectorView', 'hbs!tpl/linkgrabber/modal'], + function($, _, Backbone, App, CollectorPackage, modalView, CollectorView, template) { + 'use strict'; + // Modal dialog for package adding - triggers package:added when package was added + return modalView.extend({ + + events: { + 'keypress #inputLinks': 'addOnEnter' + }, + + template: template, + + // Holds the view that display the packages + collectorView: null, + + initialize: function() { + // Inherit parent events + this.events = _.extend({}, modalView.prototype.events, this.events); + }, + + addOnEnter: function(e) { + if (e.keyCode !== 13) return; + this.addPackage(e); + }, + + addPackage: function(e) { + var self = this; + // split, trim and remove empty links + var links = _.filter(_.map(this.$('#inputLinks').val().split('\n'), function(link) { + return $.trim(link); + }), function(link) { + return link.length > 0; + }); + + var options = App.apiRequest('checkLinks', + {links: links}, + { + success: function(data) { + self.collectorView.updateData(data); + } + }); + + $.ajax(options); + this.$('#inputLinks').val(''); + }, + + onRender: function() { + // anonymous collection + this.collectorView = new CollectorView({collection: new (Backbone.Collection.extend({ + model: CollectorPackage + }))()}); + this.collectorView.setElement(this.$('.prepared-packages')); + }, + + onDestroy: function() { + if (this.collectorView) + this.collectorView.close(); + } + + }); + });
\ No newline at end of file diff --git a/pyload/web/app/scripts/views/linkgrabber/packageView.js b/pyload/web/app/scripts/views/linkgrabber/packageView.js new file mode 100644 index 000000000..97d00f722 --- /dev/null +++ b/pyload/web/app/scripts/views/linkgrabber/packageView.js @@ -0,0 +1,58 @@ +define(['jquery', 'underscore', 'backbone', 'app', 'hbs!tpl/linkgrabber/package'], + function($, _, Backbone, App, template) { + 'use strict'; + return Backbone.Marionette.ItemView.extend({ + + tagName: 'div', + className: 'row-fluid package', + template: template, + + modelEvents: { + change: 'render' + }, + + ui: { + 'table': 'table' + }, + + events: { + 'click .btn-expand': 'expand', + 'click .btn-add': 'addPackage', + 'click .btn-delete': 'deletePackage', + 'click .btn-mini': 'deleteLink' + }, + + expanded: false, + + serializeData: function() { + var data = this.model.toJSON(); + data.expanded = this.expanded; + return data; + }, + + addPackage: function() { + this.model.add(); + }, + + deletePackage: function() { + this.model.destroy(); + }, + + deleteLink: function(e) { + var el = $(e.target); + var id = parseInt(el.data('index'), 10); + + var model = this.model.get('links').at(id); + if (model) + model.destroy(); + + this.render(); + }, + + expand: function() { + this.expanded ^= true; + this.ui.table.toggle(); + } + + }); + });
\ No newline at end of file |