From 19356fb14eaa7d5b20a45fcc5cdce94d52a48ec6 Mon Sep 17 00:00:00 2001
From: Peter Grassberger <petertheone@gmail.com>
Date: Fri, 21 Aug 2015 23:33:54 +0200
Subject: jingle: implement basic delete cut


diff --git a/www/js/jingles.js b/www/js/jingles.js
index 1d451b2..7e8f51a 100644
--- a/www/js/jingles.js
+++ b/www/js/jingles.js
@@ -20,16 +20,11 @@
  *  along with rhwebimport. If not, see <http://www.gnu.org/licenses/>.
  */
 
+"use strict";
+
 var JingleGroupList = function() {
   this.groups = [];
 
-  $(this).on('change', this.render);
-
-  var self = this;
-  $(this).on('change', function() {
-    console.log(self);
-  });
-
   this.fetchGroups();
 };
 
@@ -37,11 +32,11 @@ JingleGroupList.prototype.fetchGroups = function() {
   console.log('fetchGroups JingleGroupList');
 
   var self = this;
-  data = { LOGIN_NAME: auth_username, PASSWORD: auth_token };
-  $.post("/rh-bin/listdropboxes.cgi", data, function(groupsXml, status, req) {
+  var command = { LOGIN_NAME: auth_username, PASSWORD: auth_token };
+  $.post("/rh-bin/listdropboxes.cgi", command, function(groupsXml, status, req) {
     var dbs = $(groupsXml).find("dropboxList").children();
     dbs.each(function(index, groupXml) {
-      type = $(groupXml).find('type').text();
+      var type = $(groupXml).find('type').text();
       if (type == 'jingle') {
         var jingleGroup = new JingleGroup(
           $(groupXml).find('jingle-title').text(),
@@ -61,14 +56,6 @@ JingleGroupList.prototype.fetchGroups = function() {
   }, "xml");
 };
 
-JingleGroupList.prototype.render = function() {
-  console.log('render JingleGroupList');
-
-  $.each(this.groups, function(index, group) {
-    group.render();
-  });
-};
-
 var JingleGroup = function(title, groupName, description, lowcart, highcart, normlevel, trimlevel) {
   this.title = title;
   this.groupName = groupName;
@@ -81,24 +68,37 @@ var JingleGroup = function(title, groupName, description, lowcart, highcart, nor
   this.mainCart = null;
   this.deactivateCart = null;
 
+  var self = this;
+  $(this).on('change', function() {
+    self.render();
+  });
+
   this.fetchCarts();
 };
 
 JingleGroup.prototype.render = function() {
   console.log('render JingleGroup');
 
-  var cuts = $.merge(this.mainCart.cuts, this.deactivateCart.cuts);
+  var cuts = [];
+  if (this.mainCart && this.deactivateCart) {
+    cuts = $.merge(this.mainCart.cuts, this.deactivateCart.cuts);
+  }
   cuts = cuts.sort(function(a, b) {
-    return a.title.toLowerCase() < b.title.toLowerCase();
+    if (a.title && b.title) {
+      return a.title.toLowerCase() < b.title.toLowerCase();
+    }
+    return a.title < b.title;
   });
 
   if (this.groupName === 'jingAllgem') {
-    $('#jingles-jingAllgem table tbody').find('tr').remove();
+    $('#jingles-jingAllgem h2').html(this.title);
+    $('#jingles-jingAllgem table tbody tr').remove();
     $.each(cuts, function(index, cut) {
       $('#jingles-jingAllgem table > tbody').append(cut.$el());
     });
-  } else if (this.groupName === 'jingAllgem') {
-    $('#jingles-jingAnlass table tbody').find('tr').remove();
+  } else if (this.groupName === 'jingAnlass') {
+    $('#jingles-jingAnlass h2').html(this.title);
+    $('#jingles-jingAnlass table tbody tr').remove();
     $.each(cuts, function(index, cut) {
       $('#jingles-jingAnlass table > tbody').append(cut.$el());
     });
@@ -108,37 +108,48 @@ JingleGroup.prototype.render = function() {
 JingleGroup.prototype.fetchCarts = function() {
   console.log('fetchGroups JingleGroup');
   var self = this;
-  data = { COMMAND: 7, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: this.lowcart, INCLUDE_CUTS: 1 };
-  gcd = $.post("/rd-bin/rdxport.cgi", data, function(cartXml) {
-    self.mainCart = self.createCartFromXml(cartXml, true);
-    $(self).trigger('change');
-  }, "xml");
-  data = { COMMAND: 7, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: this.highcart, INCLUDE_CUTS: 1 };
-  gcd = $.post("/rd-bin/rdxport.cgi", data, function(cartXml) {
-    self.deactivateCart = self.createCartFromXml(cartXml, false);
-  }, "xml");
+  var command1 = { COMMAND: 7, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: this.lowcart, INCLUDE_CUTS: 1 };
+  var command2 = { COMMAND: 7, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: this.highcart, INCLUDE_CUTS: 1 };
+  $.when(
+    $.post("/rd-bin/rdxport.cgi", command1, function(cartXml) {
+      self.mainCart = self.createCartFromXml(cartXml, true);
+    }, "xml"),
+    $.post("/rd-bin/rdxport.cgi", command2, function(cartXml) {
+      self.deactivateCart = self.createCartFromXml(cartXml, false);
+    }, "xml")
+  ).then(function() {
+      $(self).trigger('change');
+  });
 };
 
 JingleGroup.prototype.createCartFromXml = function(cartXml, active) {
   var cart = new JingleCart(
+    $(cartXml).find('number').text(),
     $(cartXml).find('title').text(),
     $(cartXml).find('groupName').text()
   );
 
+  var self = this;
   var cuts = $(cartXml).find("cutList").children();
   cuts.each(function(index, cutXml) {
-    cart.addCut(new JingleCut(
+    var cut = new JingleCut(
+      cart.number,
       $(cutXml).find('cutName').text(),
       $(cutXml).find('description').text(),
       active
-    ));
+    );
+    $(cut).on('change', function() {
+      $(self).trigger('change');
+    });
+    cart.addCut(cut);
   });
 
   return cart;
 };
 
 
-var JingleCart = function(title, groupName) {
+var JingleCart = function(number, title, groupName) {
+  this.number = number;
   this.title = title;
   this.groupName = groupName;
 
@@ -149,7 +160,9 @@ JingleCart.prototype.addCut = function(cut) {
   this.cuts.push(cut);
 };
 
-var JingleCut = function(name, description, active) {
+var JingleCut = function(cartNumber, name, description, active) {
+  this.cartNumber = cartNumber;
+  this.number = name.substr(-3);
   this.name = name;
   this.description = description;
   this.active = active;
@@ -159,6 +172,30 @@ JingleCut.prototype.move = function() {
   console.log('move');
   // move to other group, if active stay in mainCart, if deactivated stay in deactiveCart
 
+  // check if there is an empty cut at destination
+  // todo: remove hardcoded cartNumbers
+  var destinationCart = ((parseInt(this.cartNumber) - 2000 + 2) % 4) + 2000;
+  console.log(parseInt(this.cartNumber));
+  console.log(destinationCart);
+
+  var command = { COMMAND: 9, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: destinationCart };
+  $.post("/rd-bin/rdxport.cgi", command, function(cutsXml, status, req) {
+    var cuts = $(cutsXml).find("cutList").children();
+    console.log(cuts.length);
+
+    // create one if not.
+
+    if (cuts.length < 1) {
+      var command = { COMMAND: 10, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: destinationCart };
+      $.post("/rd-bin/rdxport.cgi", command, null, "xml").done(function() {
+        console.log('done');
+      });
+    }
+  }, "xml");
+
+
+  // use command 18 copy audio from one cart to another
+
 
   //$(this).trigger('change');
 };
@@ -170,10 +207,18 @@ JingleCut.prototype.toggleActivate = function() {
   //$(this).trigger('change');
 };
 
-JingleCut.prototype.delete = function() {
+JingleCut.prototype.delete = function(self) {
   console.log('delete');
   // remove cut, command 11
 
+  console.log(this.cartNumber);
+  console.log(this.number);
+
+  var command = { COMMAND: 11, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: this.cartNumber, CUT_NUMBER: this.number};
+  $.post("/rd-bin/rdxport.cgi", command, null, "xml").done(function() {
+    console.log('done');
+  });
+
   //$(this).trigger('change');
 };
 
@@ -187,9 +232,16 @@ JingleCut.prototype.$el = function() {
   }
   var deleteButton = $('<button class="btn btn-danger btn-mini"><i class="icon-trash icon-white"></i> Löschen</button>');
 
-  moveButton.on('click', this.move);
-  activateButton.on('click', this.toggleActivate);
-  deleteButton.on('click', this.delete);
+  var self = this;
+  moveButton.on('click', function() {
+    self.move();
+  });
+  activateButton.on('click', function() {
+    self.toggleActivate();
+  });
+  deleteButton.on('click', function() {
+    self.delete();
+  });
 
   return $('<tr>').append($('<td>').text(this.name))
     .append($('<td>').text(this.description))
-- 
cgit v0.10.2