From db074d0f87558ad22d8b50e5eea6f87df9f3559e Mon Sep 17 00:00:00 2001
From: PeterTheOne <petertheone@gmail.com>
Date: Fri, 29 Jan 2016 01:46:50 +0100
Subject: musicpools: modify importer to create carts


diff --git a/www/js/importer.js b/www/js/importer.js
index 8aa6c09..d2c1e3b 100644
--- a/www/js/importer.js
+++ b/www/js/importer.js
@@ -28,15 +28,19 @@ Rivendell.Importer = function() {
   this.$el = $('#uploadModal');
 };
 
-Rivendell.Importer.prototype.resetModal = function(group) {
+Rivendell.Importer.prototype.resetModal = function() {
   $('div.modal-header h3', this.$el).text("Datei auswählen...");
   $('.modal-body', this.$el).css("background-image", "url('/img/dz-backdrop.png')");
   $('#uploadModal-confirm', this.$el).attr('disabled','disabled').off('click');
 };
 
-Rivendell.Importer.prototype.openModal = function(group) {
+Rivendell.Importer.prototype.openModal = function(group, createCart) {
   this.resetModal();
 
+  if (createCart === undefined) {
+    createCart = false;
+  }
+
   var $form = $('<form>');
   var self = this;
   $form.dropzone({
@@ -51,7 +55,7 @@ Rivendell.Importer.prototype.openModal = function(group) {
     autoProcessQueue: false,
     init: function() {
       this.on('addedfile', function(file) {
-        self.importFileAdded(this, file, group);
+        self.importFileAdded(this, file, group, createCart);
       });
       this.on('error', function(file, msg) {
         self.importFileSelectError(this, file, msg);
@@ -63,7 +67,7 @@ Rivendell.Importer.prototype.openModal = function(group) {
   this.$el.modal({keyboard: true});
 };
 
-Rivendell.Importer.prototype.importFileAdded = function(dropzone, file, group) {
+Rivendell.Importer.prototype.importFileAdded = function(dropzone, file, group, createCart) {
   // wtf? remove accepted files?
   /*$(dropzone.getAcceptedFiles()).each(function(index, elem) {
     dropzone.removeFile(elem);
@@ -74,7 +78,7 @@ Rivendell.Importer.prototype.importFileAdded = function(dropzone, file, group) {
 
   var self = this;
   $('#uploadModal-confirm', this.$el).off('click').on('click', function() {
-    self.importCartConfirm(dropzone, group);
+    self.importCartConfirm(dropzone, group, createCart);
   }).removeAttr('disabled');
 };
 
@@ -83,7 +87,7 @@ Rivendell.Importer.prototype.importFileSelectError = function(dropzone, file, ms
   dropzone.removeFile(file);
 };
 
-Rivendell.Importer.prototype.importCartConfirm = function(dropzone, group) {
+Rivendell.Importer.prototype.importCartConfirm = function(dropzone, group, createCart) {
   this.$el.modal('hide');
 
   //var progressBar = group.addUpload();
@@ -112,7 +116,16 @@ Rivendell.Importer.prototype.importCartConfirm = function(dropzone, group) {
   /*$(files).each(function(index, file) {
    self.importAddCut(dropzone, group, file);
    });*/
-  self.importAddCut(dropzone, group, files[0]);
+
+  if (createCart) {
+    rivendell.addCart(group.groupName, 'audio', null, function(cartXML) {
+      var cart = new Rivendell.Cart(cartXML, group);
+      self.importAddCut(dropzone, group, files[0], createCart, cart);
+    });
+  } else {
+    var cart = group.mainCart;
+    self.importAddCut(dropzone, group, files[0], createCart, cart);
+  }
 };
 
 Rivendell.Importer.prototype.uploadProgress = function(file) {
@@ -158,12 +171,11 @@ Rivendell.Importer.prototype.importFileUploadError = function(dropzone, file, ms
   dropzone.disable();
 };
 
-Rivendell.Importer.prototype.importAddCut = function(dropzone, group, file) {
-  var cart = group.mainCart;
+Rivendell.Importer.prototype.importAddCut = function(dropzone, group, file, createCart, cart) {
   rivendell.addAndEditCut(cart.number, {DESCRIPTION: file.name}, function(cutXml) {
     group.fetchCarts();
     dropzone.on('sending', function(file, xhr, formData) {
-      var cutNumber = $(cutXml).find('cutNumber').text();
+      var cutNumber = $('cutNumber', cutXml).text();
       var cutNumberLeading = cutNumber;
       switch (cutNumber.toString().length) {
         case 0: cutNumberLeading = '000' + cutNumber; break;
@@ -186,6 +198,9 @@ Rivendell.Importer.prototype.importAddCut = function(dropzone, group, file) {
     });
     dropzone.processQueue();
   }).fail(function(xhr, status, err) {
-    self.importFileUploadError(cart, dropzone, file, err, xhr);
+    self.importFileUploadError(dropzone, file, err, xhr);
+    if (createCart) {
+      rivendell.removeCart(cart.number);
+    }
   });
 };
diff --git a/www/js/jingles.js b/www/js/jingles.js
index 451f643..d80dd95 100644
--- a/www/js/jingles.js
+++ b/www/js/jingles.js
@@ -104,7 +104,7 @@ JingleGroupView.prototype.render = function() {
   $('table tbody tr', this.$el).remove();
 
   $('.uploadButton', this.$el).on('click', function() {
-    importer.openModal(self.model);
+    importer.openModal(self.model, false);
   });
 };
 
diff --git a/www/js/musicpools.js b/www/js/musicpools.js
index 2ccd27f..00cffe3 100644
--- a/www/js/musicpools.js
+++ b/www/js/musicpools.js
@@ -139,9 +139,9 @@ Rivendell.MusicpoolView.prototype.render = function() {
     $('table tbody tr', self.$el).remove();
 
     // todo
-    /*$('.uploadButton', self.$el).on('click', function() {
-      importer.openModal(self.model);
-    });*/
+    $('.uploadButton', self.$el).on('click', function() {
+      importer.openModal(self.model, true);
+    });
 
   });
   this.model.fetchCarts();
diff --git a/www/js/rivendell.js b/www/js/rivendell.js
index 89cf471..4a40ce0 100644
--- a/www/js/rivendell.js
+++ b/www/js/rivendell.js
@@ -110,9 +110,11 @@ Rivendell.Rivendell.prototype.addCart = function(groupName, type, cartNumber, su
     LOGIN_NAME: this.username,
     PASSWORD: this.token,
     GROUP_NAME: groupName,
-    TYPE: type,
-    CART_NUMBER: cartNumber
+    TYPE: type
   };
+  if (cartNumber !== null) {
+    command.CART_NUMBER = cartNumber;
+  }
   return $.post(this.rdxportEndpoint, command, success, 'xml');
 };
 
-- 
cgit v0.10.2