summaryrefslogtreecommitdiffstats
path: root/pyload/web/app/scripts/models/CollectorPackage.js
diff options
context:
space:
mode:
Diffstat (limited to 'pyload/web/app/scripts/models/CollectorPackage.js')
-rw-r--r--pyload/web/app/scripts/models/CollectorPackage.js77
1 files changed, 77 insertions, 0 deletions
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