summaryrefslogtreecommitdiffstats
path: root/pyload/web/app/scripts/models
diff options
context:
space:
mode:
authorGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2013-06-09 18:10:22 +0200
committerGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2013-06-09 18:10:23 +0200
commit16af85004c84d0d6c626b4f8424ce9647669a0c1 (patch)
tree025d479862d376dbc17e934f4ed20031c8cd97d1 /pyload/web/app/scripts/models
parentadapted to jshint config (diff)
downloadpyload-16af85004c84d0d6c626b4f8424ce9647669a0c1.tar.xz
moved everything from module to pyload
Diffstat (limited to 'pyload/web/app/scripts/models')
-rw-r--r--pyload/web/app/scripts/models/Account.js51
-rw-r--r--pyload/web/app/scripts/models/ConfigHolder.js68
-rw-r--r--pyload/web/app/scripts/models/ConfigItem.js40
-rw-r--r--pyload/web/app/scripts/models/File.js92
-rw-r--r--pyload/web/app/scripts/models/InteractionTask.js41
-rw-r--r--pyload/web/app/scripts/models/Package.js119
-rw-r--r--pyload/web/app/scripts/models/Progress.js50
-rw-r--r--pyload/web/app/scripts/models/ServerStatus.js47
-rw-r--r--pyload/web/app/scripts/models/TreeCollection.js50
-rw-r--r--pyload/web/app/scripts/models/UserSession.js20
10 files changed, 578 insertions, 0 deletions
diff --git a/pyload/web/app/scripts/models/Account.js b/pyload/web/app/scripts/models/Account.js
new file mode 100644
index 000000000..a2e24b056
--- /dev/null
+++ b/pyload/web/app/scripts/models/Account.js
@@ -0,0 +1,51 @@
+define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], function($, Backbone, _, App, Api) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ // TODO
+ // generated, not submitted
+ idAttribute: 'user',
+
+ defaults: {
+ plugin: null,
+ loginname: null,
+ owner: -1,
+ valid: false,
+ validuntil: -1,
+ trafficleft: -1,
+ maxtraffic: -1,
+ premium: false,
+ activated: false,
+ shared: false,
+ options: null
+ },
+
+ // Model Constructor
+ initialize: function() {
+ },
+
+ // Any time a model attribute is set, this method is called
+ validate: function(attrs) {
+
+ },
+
+ save: function(options) {
+ options = App.apiRequest('updateAccountInfo', {account: this.toJSON()}, options);
+ return $.ajax(options);
+ },
+
+ destroy: function(options) {
+ options = App.apiRequest('removeAccount', {account: this.toJSON()}, options);
+ var self = this;
+ options.success = function() {
+ self.trigger('destroy', self, self.collection, options);
+ };
+
+ // TODO request is not dispatched
+// return Backbone.Model.prototype.destroy.call(this, options);
+ return $.ajax(options);
+ }
+ });
+
+}); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/ConfigHolder.js b/pyload/web/app/scripts/models/ConfigHolder.js
new file mode 100644
index 000000000..40efbc7c0
--- /dev/null
+++ b/pyload/web/app/scripts/models/ConfigHolder.js
@@ -0,0 +1,68 @@
+define(['jquery', 'backbone', 'underscore', 'app', './ConfigItem'],
+ function($, Backbone, _, App, ConfigItem) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ defaults: {
+ name: '',
+ label: '',
+ description: '',
+ long_description: null,
+ // simple list but no collection
+ items: null,
+ info: null
+ },
+
+ // Model Constructor
+ initialize: function() {
+
+ },
+
+ // Loads it from server by name
+ fetch: function(options) {
+ options = App.apiRequest('loadConfig/"' + this.get('name') + '"', null, options);
+ return Backbone.Model.prototype.fetch.call(this, options);
+ },
+
+ save: function(options) {
+ var config = this.toJSON();
+ var items = [];
+ // Convert changed items to json
+ _.each(config.items, function(item) {
+ if (item.isChanged()) {
+ items.push(item.prepareSave());
+ }
+ });
+ config.items = items;
+ // TODO: only set new values on success
+
+ options = App.apiRequest('saveConfig', {config: config}, options);
+
+ return $.ajax(options);
+ },
+
+ parse: function(resp) {
+ // Create item models
+ resp.items = _.map(resp.items, function(item) {
+ return new ConfigItem(item);
+ });
+
+ return Backbone.Model.prototype.parse.call(this, resp);
+ },
+
+ isLoaded: function() {
+ return this.has('items') || this.has('long_description');
+ },
+
+ // check if any of the items has changes
+ hasChanges: function() {
+ var items = this.get('items');
+ if (!items) return false;
+ return _.reduce(items, function(a, b) {
+ return a || b.isChanged();
+ }, false);
+ }
+
+ });
+ }); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/ConfigItem.js b/pyload/web/app/scripts/models/ConfigItem.js
new file mode 100644
index 000000000..2d325c2a2
--- /dev/null
+++ b/pyload/web/app/scripts/models/ConfigItem.js
@@ -0,0 +1,40 @@
+define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'],
+ function($, Backbone, _, App, Api) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ defaults: {
+ name: '',
+ label: '',
+ description: '',
+ input: null,
+ default_value: null,
+ value: null,
+ // additional attributes
+ inputView: null
+ },
+
+ // Model Constructor
+ initialize: function() {
+
+ },
+
+ isChanged: function() {
+ return this.get('inputView') && this.get('inputView').getVal() !== this.get('value');
+ },
+
+ // set new value and return json
+ prepareSave: function() {
+ // set the new value
+ if (this.get('inputView'))
+ this.set('value', this.get('inputView').getVal());
+
+ var data = this.toJSON();
+ delete data.inputView;
+ delete data.description;
+
+ return data;
+ }
+ });
+ }); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/File.js b/pyload/web/app/scripts/models/File.js
new file mode 100644
index 000000000..3beb7f270
--- /dev/null
+++ b/pyload/web/app/scripts/models/File.js
@@ -0,0 +1,92 @@
+define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'], function($, Backbone, _, App, Api) {
+ 'use strict';
+
+ var Finished = [Api.DownloadStatus.Finished, Api.DownloadStatus.Skipped];
+ var Failed = [Api.DownloadStatus.Failed, Api.DownloadStatus.Aborted, Api.DownloadStatus.TempOffline, Api.DownloadStatus.Offline];
+ // Unfinished - Other
+
+ return Backbone.Model.extend({
+
+ idAttribute: 'fid',
+
+ defaults: {
+ fid: -1,
+ name: null,
+ package: -1,
+ owner: -1,
+ size: -1,
+ status: -1,
+ media: -1,
+ added: -1,
+ fileorder: -1,
+ download: null,
+
+ // UI attributes
+ selected: false,
+ visible: true,
+ progress: 0,
+ eta: 0
+ },
+
+ // Model Constructor
+ initialize: function() {
+
+ },
+
+ fetch: function(options) {
+ options = App.apiRequest(
+ 'getFileInfo',
+ {fid: this.get('fid')},
+ options);
+
+ return Backbone.Model.prototype.fetch.call(this, options);
+ },
+
+ destroy: function(options) {
+ // also not working when using data
+ options = App.apiRequest(
+ 'deleteFiles/[' + this.get('fid') + ']',
+ null, options);
+ options.method = 'post';
+
+ return Backbone.Model.prototype.destroy.call(this, options);
+ },
+
+ // Does not send a request to the server
+ destroyLocal: function(options) {
+ this.trigger('destroy', this, this.collection, options);
+ },
+
+ restart: function(options) {
+ options = App.apiRequest(
+ 'restartFile',
+ {fid: this.get('fid')},
+ options);
+
+ return $.ajax(options);
+ },
+
+ // Any time a model attribute is set, this method is called
+ validate: function(attrs) {
+
+ },
+
+ isDownload: function() {
+ return this.has('download');
+ },
+
+ isFinished: function() {
+ return _.indexOf(Finished, this.get('download').status) > -1;
+ },
+
+ isUnfinished: function() {
+ return _.indexOf(Finished, this.get('download').status) === -1 && _.indexOf(Failed, this.get('download').status) === -1;
+ },
+
+ isFailed: function() {
+ return _.indexOf(Failed, this.get('download').status) > -1;
+ }
+
+ });
+
+}); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/InteractionTask.js b/pyload/web/app/scripts/models/InteractionTask.js
new file mode 100644
index 000000000..54c739d4b
--- /dev/null
+++ b/pyload/web/app/scripts/models/InteractionTask.js
@@ -0,0 +1,41 @@
+define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'],
+ function($, Backbone, _, App, Api) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ idAttribute: 'iid',
+
+ defaults: {
+ iid: -1,
+ type: null,
+ input: null,
+ default_value: null,
+ title: '',
+ description: '',
+ plugin: '',
+ // additional attributes
+ result: ''
+ },
+
+ // Model Constructor
+ initialize: function() {
+
+ },
+
+ save: function(options) {
+ options = App.apiRequest('setInteractionResult/' + this.get('iid'),
+ {result: this.get('result')}, options);
+
+ return $.ajax(options);
+ },
+
+ isNotification: function() {
+ return this.get('type') === Api.Interaction.Notification;
+ },
+
+ isCaptcha: function() {
+ return this.get('type') === Api.Interaction.Captcha;
+ }
+ });
+ }); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/Package.js b/pyload/web/app/scripts/models/Package.js
new file mode 100644
index 000000000..a34ec1c69
--- /dev/null
+++ b/pyload/web/app/scripts/models/Package.js
@@ -0,0 +1,119 @@
+define(['jquery', 'backbone', 'underscore', 'app', 'collections/FileList', 'require'],
+ function($, Backbone, _, App, FileList, require) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ idAttribute: 'pid',
+
+ defaults: {
+ pid: -1,
+ name: null,
+ folder: '',
+ root: -1,
+ owner: -1,
+ site: '',
+ comment: '',
+ password: '',
+ added: -1,
+ tags: null,
+ status: -1,
+ shared: false,
+ packageorder: -1,
+ stats: null,
+ fids: null,
+ pids: null,
+ files: null, // Collection
+ packs: null, // Collection
+
+ selected: false // For Checkbox
+ },
+
+ // Model Constructor
+ initialize: function() {
+ },
+
+ toJSON: function(options) {
+ var obj = Backbone.Model.prototype.toJSON.call(this, options);
+ obj.percent = Math.round(obj.stats.linksdone * 100 / obj.stats.linkstotal);
+
+ return obj;
+ },
+
+ // Changes url + method and delegates call to super class
+ fetch: function(options) {
+ options = App.apiRequest(
+ 'getFileTree/' + this.get('pid'),
+ {full: false},
+ options);
+
+ return Backbone.Model.prototype.fetch.call(this, options);
+ },
+
+ // Create a pseudo package und use search to populate data
+ search: function(qry, options) {
+ options = App.apiRequest(
+ 'findFiles',
+ {pattern: qry},
+ options);
+
+ return Backbone.Model.prototype.fetch.call(this, options);
+ },
+
+ save: function(options) {
+ // TODO
+ },
+
+ destroy: function(options) {
+ // TODO: Not working when using data?, array seems to break it
+ options = App.apiRequest(
+ 'deletePackages/[' + this.get('pid') + ']',
+ null, options);
+ options.method = 'post';
+
+ console.log(options);
+
+ return Backbone.Model.prototype.destroy.call(this, options);
+ },
+
+ restart: function(options) {
+ options = App.apiRequest(
+ 'restartPackage',
+ {pid: this.get('pid')},
+ options);
+
+ var self = this;
+ options.success = function() {
+ self.fetch();
+ };
+ return $.ajax(options);
+ },
+
+ parse: function(resp) {
+ // Package is loaded from tree collection
+ if (_.has(resp, 'root')) {
+ if (!this.has('files'))
+ resp.root.files = new FileList(_.values(resp.files));
+ else
+ this.get('files').set(_.values(resp.files));
+
+ // circular dependencies needs to be avoided
+ var PackageList = require('collections/PackageList');
+
+ if (!this.has('packs'))
+ resp.root.packs = new PackageList(_.values(resp.packages));
+ else
+ this.get('packs').set(_.values(resp.packages));
+
+ return resp.root;
+ }
+ return Backbone.model.prototype.parse.call(this, resp);
+ },
+
+ // Any time a model attribute is set, this method is called
+ validate: function(attrs) {
+
+ }
+
+ });
+ }); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/Progress.js b/pyload/web/app/scripts/models/Progress.js
new file mode 100644
index 000000000..b0bbb684d
--- /dev/null
+++ b/pyload/web/app/scripts/models/Progress.js
@@ -0,0 +1,50 @@
+define(['jquery', 'backbone', 'underscore', 'utils/apitypes'], function($, Backbone, _, Api) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ // generated, not submitted
+ idAttribute: 'pid',
+
+ defaults: {
+ pid: -1,
+ plugin: null,
+ name: null,
+ statusmsg: -1,
+ eta: -1,
+ done: -1,
+ total: -1,
+ download: null
+ },
+
+ getPercent: function() {
+ if (this.get('total') > 0)
+ return Math.round(this.get('done') * 100 / this.get('total'));
+ return 0;
+ },
+
+ // Model Constructor
+ initialize: function() {
+
+ },
+
+ // Any time a model attribute is set, this method is called
+ validate: function(attrs) {
+
+ },
+
+ toJSON: function(options) {
+ var obj = Backbone.Model.prototype.toJSON.call(this, options);
+ obj.percent = this.getPercent();
+ obj.downloading = this.isDownload() && this.get('download').status === Api.DownloadStatus.Downloading;
+
+ return obj;
+ },
+
+ isDownload : function() {
+ return this.has('download');
+ }
+
+ });
+
+}); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/ServerStatus.js b/pyload/web/app/scripts/models/ServerStatus.js
new file mode 100644
index 000000000..59739b41e
--- /dev/null
+++ b/pyload/web/app/scripts/models/ServerStatus.js
@@ -0,0 +1,47 @@
+define(['jquery', 'backbone', 'underscore'],
+ function($, Backbone, _) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ defaults: {
+ speed: 0,
+ linkstotal: 0,
+ linksqueue: 0,
+ sizetotal: 0,
+ sizequeue: 0,
+ notifications: -1,
+ paused: false,
+ download: false,
+ reconnect: false
+ },
+
+ // Model Constructor
+ initialize: function() {
+
+ },
+
+ fetch: function(options) {
+ options || (options = {});
+ options.url = 'api/getServerStatus';
+
+ return Backbone.Model.prototype.fetch.call(this, options);
+ },
+
+ toJSON: function(options) {
+ var obj = Backbone.Model.prototype.toJSON.call(this, options);
+
+ obj.linksdone = obj.linkstotal - obj.linksqueue;
+ obj.sizedone = obj.sizetotal - obj.sizequeue;
+ if (obj.speed && obj.speed > 0)
+ obj.eta = Math.round(obj.sizequeue / obj.speed);
+ else if (obj.sizequeue > 0)
+ obj.eta = Infinity;
+ else
+ obj.eta = 0;
+
+ return obj;
+ }
+
+ });
+ }); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/TreeCollection.js b/pyload/web/app/scripts/models/TreeCollection.js
new file mode 100644
index 000000000..2f761e6cc
--- /dev/null
+++ b/pyload/web/app/scripts/models/TreeCollection.js
@@ -0,0 +1,50 @@
+define(['jquery', 'backbone', 'underscore', 'app', 'models/Package', 'collections/FileList', 'collections/PackageList'],
+ function($, Backbone, _, App, Package, FileList, PackageList) {
+ 'use strict';
+
+ // TreeCollection
+ // A Model and not a collection, aggregates other collections
+ return Backbone.Model.extend({
+
+ defaults: {
+ root: null,
+ packages: null,
+ files: null
+ },
+
+ initialize: function() {
+
+ },
+
+ fetch: function(options) {
+ options || (options = {});
+ var pid = options.pid || -1;
+
+ options = App.apiRequest(
+ 'getFileTree/' + pid,
+ {full: false},
+ options);
+
+ console.log('Fetching package tree ' + pid);
+ return Backbone.Model.prototype.fetch.call(this, options);
+ },
+
+ // Parse the response and updates the collections
+ parse: function(resp) {
+ var ret = {};
+ if (!this.has('packages'))
+ ret.packages = new PackageList(_.values(resp.packages));
+ else
+ this.get('packages').set(_.values(resp.packages));
+
+ if (!this.has('files'))
+ ret.files = new FileList(_.values(resp.files));
+ else
+ this.get('files').set(_.values(resp.files));
+
+ ret.root = new Package(resp.root);
+ return ret;
+ }
+
+ });
+ }); \ No newline at end of file
diff --git a/pyload/web/app/scripts/models/UserSession.js b/pyload/web/app/scripts/models/UserSession.js
new file mode 100644
index 000000000..a7e9aa848
--- /dev/null
+++ b/pyload/web/app/scripts/models/UserSession.js
@@ -0,0 +1,20 @@
+define(['jquery', 'backbone', 'underscore', 'utils/apitypes', 'cookie'],
+ function($, Backbone, _, Api) {
+ 'use strict';
+
+ return Backbone.Model.extend({
+
+ idAttribute: 'username',
+
+ defaults: {
+ username: null,
+ permissions: null,
+ session: null
+ },
+
+ // Model Constructor
+ initialize: function() {
+ this.set('session', $.cookie('beaker.session.id'));
+ }
+ });
+ }); \ No newline at end of file