summaryrefslogtreecommitdiffstats
path: root/pyload/web/app/scripts/views/linkgrabber
diff options
context:
space:
mode:
Diffstat (limited to 'pyload/web/app/scripts/views/linkgrabber')
-rw-r--r--pyload/web/app/scripts/views/linkgrabber/collectorView.js36
-rw-r--r--pyload/web/app/scripts/views/linkgrabber/modalView.js121
-rw-r--r--pyload/web/app/scripts/views/linkgrabber/packageView.js86
3 files changed, 243 insertions, 0 deletions
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..08b426aff
--- /dev/null
+++ b/pyload/web/app/scripts/views/linkgrabber/collectorView.js
@@ -0,0 +1,36 @@
+define(['jquery', 'underscore', 'backbone', 'app', './packageView'],
+ function($, _, Backbone, App, packageView) {
+ 'use strict';
+ return Backbone.Marionette.CollectionView.extend({
+ itemView: packageView,
+
+ initialize: function() {
+ this.listenTo(App.vent, 'linkcheck:updated', _.bind(this.onData, this));
+ },
+
+ onData: function(rid, result) {
+ this.updateData({data: result});
+ },
+
+ 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);
+ }
+
+ // Remove links from other packages and delete empty ones
+ self.collection.each(function(pack2) {
+ console.log(pack2, links);
+ if (pack2 !== pack)
+ if (pack2.removeLinks(links))
+ self.collection.remove(pack2);
+ });
+
+ 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..8e24f259b
--- /dev/null
+++ b/pyload/web/app/scripts/views/linkgrabber/modalView.js
@@ -0,0 +1,121 @@
+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({
+
+ className: 'modal linkgrabber',
+ events: {
+ 'keyup #inputLinks': 'addOnKeyUp',
+ 'click .btn-container': 'selectContainer',
+ 'change #inputContainer': 'checkContainer',
+ 'keyup #inputURL': 'checkURL',
+ 'click .btn-remove-all': 'clearAll'
+ },
+
+ template: template,
+
+ // Holds the view that display the packages
+ collectorView: null,
+
+ inputSize: 0,
+
+ initialize: function() {
+ // Inherit parent events
+ this.events = _.extend({}, modalView.prototype.events, this.events);
+ this.listenTo(App.vent, 'package:added', _.bind(this.onAdded, this));
+ },
+
+ addOnKeyUp: function(e) {
+ // Enter adds the links
+ if (e.keyCode === 13)
+ this.checkLinks();
+
+ var inputSize = this.$('#inputLinks').val().length;
+
+ // TODO: checkbox to disable this
+ // add links when several characters was pasted into box
+ if (inputSize > this.inputSize + 4)
+ this.checkLinks();
+ else
+ this.inputSize = inputSize;
+ },
+
+ checkLinks: function() {
+ 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('');
+ this.inputSize = 0;
+ },
+
+ selectContainer: function(e) {
+ this.$('#inputContainer').trigger('click');
+ },
+
+ checkContainer: function(e) {
+ this.$('form').attr('action', App.apiUrl('api/checkContainer'));
+ this.$('form').trigger('submit');
+ },
+
+ checkURL: function(e) {
+ // check is triggered on enter
+ if (e.keyCode !== 13)
+ return;
+
+ var self = this;
+ $.ajax(App.apiRequest('checkHTML', {
+ html: '',
+ url: $(e.target).val()
+ }, {
+ success: function(data) {
+ self.collectorView.updateData(data);
+ }
+ }));
+
+ $(e.target).val('');
+ },
+
+ // deletes every package
+ clearAll: function(e) {
+ this.collectorView.collection.reset();
+
+ },
+
+ // Hide when there are no more packages
+ onAdded: function() {
+ if (this.collectorView !== null) {
+ if (this.collectorView.collection.length === 0)
+ this.hide();
+ }
+ },
+
+ 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..356d39b4b
--- /dev/null
+++ b/pyload/web/app/scripts/views/linkgrabber/packageView.js
@@ -0,0 +1,86 @@
+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: {
+ 'name': '.name',
+ 'table': 'table'
+ },
+
+ events: {
+ 'click .btn-expand': 'expand',
+ 'click .name': 'renamePackage',
+ 'keyup .name input': 'saveName',
+ '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(e) {
+ e.stopPropagation();
+ this.model.add();
+ return false;
+ },
+
+ renamePackage: function(e) {
+ e.stopPropagation();
+
+ this.ui.name.addClass('edit');
+ this.ui.name.find('input').focus();
+
+ var self = this;
+ $(document).one('click', function() {
+ self.ui.name.removeClass('edit');
+ self.ui.name.focus();
+ });
+
+ return false;
+ },
+
+ saveName: function(e) {
+ if (e.keyCode === 13) {
+ this.model.setName(this.ui.name.find('input').val());
+ }
+ },
+
+ 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(e) {
+ e.stopPropagation();
+ this.expanded ^= true;
+ this.ui.table.toggle();
+ return false;
+ }
+
+ });
+ }); \ No newline at end of file