/* * rhwebimport * * Copyright (C) 2014-2015 Christian Pointner * Copyright (C) 2015 Peter Grassberger * * This file is part of rhwebimport. * * rhwebimport is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * rhwebimport is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with rhwebimport. If not, see . */ "use strict"; var JingleGroupList = function() { this.groups = []; this.fetchGroups(); }; JingleGroupList.prototype.fetchGroups = function() { console.log('fetchGroups JingleGroupList'); var self = this; 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) { var type = $(groupXml).find('type').text(); if (type == 'jingle') { var jingleGroup = new JingleGroup( $(groupXml).find('jingle-title').text(), $(groupXml).find('group').text(), $(groupXml).find('group-description').text(), $(groupXml).find('group-low-cart').text(), $(groupXml).find('group-high-cart').text(), $(groupXml).find('normalization-level').text(), $(groupXml).find('autotrim-level').text() ); $(jingleGroup).on('change', function() { $(self).trigger('change'); }); self.groups.push(jingleGroup); } }); }, "xml"); }; var JingleGroup = function(title, groupName, description, lowcart, highcart, normlevel, trimlevel) { this.title = title; this.groupName = groupName; this.description = description; this.lowcart = lowcart; this.highcart = highcart; this.normlevel = normlevel; this.trimlevel = trimlevel; this.mainCart = null; this.deactivateCart = null; this.$el = null; var self = this; $(this).on('change', function() { console.log('JingleGroup change!'); console.log(self.mainCart); console.log(self.deactivateCart); self.render(); }); this.fetchCarts(); }; JingleGroup.prototype.render = function() { console.log('render JingleGroup'); // todo: copy some kind of template and render.. if (!this.$el) { this.$el = $('.jingleGroupTemplate').clone().removeClass('jingleGroupTemplate'); this.$el.removeClass('hidden').appendTo('#app-jingles'); } $('h2', this.$el).html(this.title); $('table tbody tr', this.$el).remove(); var cuts = []; if (this.mainCart && this.deactivateCart) { cuts = $.merge(this.mainCart.cuts, this.deactivateCart.cuts); } cuts = cuts.sort(function(a, b) { if (a.title && b.title) { return a.title.toLowerCase() < b.title.toLowerCase(); } return a.title < b.title; }); var self = this; $.each(cuts, function(index, cut) { $('table > tbody', self.$el).append(cut.render()); }); }; JingleGroup.prototype.fetchCarts = function() { console.log('fetchGroups JingleGroup'); var self = this; 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) { var cut = new JingleCut( cart, cart.number, $(cutXml).find('cutName').text(), $(cutXml).find('description').text(), active ); $(cut).on('change', function(event, silent) { console.log('JingleCut change!'); if (!silent) { $(self).trigger('change'); } }); cart.addCut(cut); }); return cart; }; var JingleCart = function(number, title, groupName) { this.number = number; this.title = title; this.groupName = groupName; this.cuts = []; }; JingleCart.prototype.addCut = function(cut) { this.cuts.push(cut); }; JingleCart.prototype.removeCut = function(cut) { var index = this.cuts.indexOf(cut); if (index > -1) { this.cuts.splice(index, 1); } }; var JingleCut = function(cart, cartNumber, name, description, active) { this.cart = cart; this.cartNumber = cartNumber; this.number = name.substr(-3); this.name = name; this.description = description; this.active = active; this.$el = null; }; 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 self = this; 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, function() { console.log('done'); // use command 18 copy audio from one cart to another // then delete old cart self.delete(); // auto update //$(this).trigger('change'); }, "xml"); } }, "xml"); }; JingleCut.prototype.toggleActivate = function() { console.log('toggleActivate'); // move to other cart, from mainCart to deactiveCart or the other way around //$(this).trigger('change'); }; JingleCut.prototype.delete = function() { console.log('delete'); // remove cut, command 11 console.log(this.cartNumber); console.log(this.number); var self = this; 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, function() { self.cart.removeCut(this); self.$el.remove(); self.$el = null; $(self).trigger('change', true); $(self).off(); }); }; JingleCut.prototype.render = function() { if (this.$el) { return this.$el; } var moveButton = $(''); var activateButton; if (this.active) { activateButton = $(''); } else { activateButton = $(''); } var deleteButton = $(''); var self = this; moveButton.on('click', function() { self.move(); }); activateButton.on('click', function() { self.toggleActivate(); }); deleteButton.on('click', function() { self.delete(); }); this.$el = $('').append($('').text(this.name)) .append($('').text(this.description)) .append( $('').css('text-align', 'center') .append(moveButton) .append(activateButton) .append(deleteButton) ); return this.$el; }; var jinglesGroupList = null; function jingles_init() { jinglesGroupList = new JingleGroupList(); } function jingles_cleanup() { jinglesGroupList = null; }