/* * rhwebimport * * Copyright (C) 2014-2016 Christian Pointner * Copyright (C) 2015-2016 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 Rivendell = Rivendell || {}; var rivendell = null; var importer = null; var groupList = null; function jingles_init() { rivendell = new Rivendell.Rivendell(auth_username, auth_token, '/rd-bin/rdxport.cgi'); rivendell.setListDropboxesEndpoint('/rh-bin/listdropboxes.cgi'); importer = new Rivendell.Importer(); groupList = new Rivendell.GroupList(rivendell); // todo: move this elsewhere? $('#app-jingles .groups').html(''); groupList.fetch('jingle'); } function jingles_cleanup() { if (groupList) { groupList.destroy(); groupList = null; } importer = null; rivendell = null; } var JingleGroup = function(groupName, description, lowcart, highcart, normlevel, trimlevel, title) { if (arguments.length = 1) { Rivendell.Group.call(this, groupName); this.title = $('jingle-title', this.xml).text(); } else { Rivendell.Group.call(this, groupName, description, lowcart, highcart, normlevel, trimlevel); this.title = title; } this.mainCart = null; this.deactivateCart = null; this.$el = null; this.render(); this.fetchCarts(); }; JingleGroup.prototype = Object.create(Rivendell.Group.prototype); JingleGroup.prototype.constructor = JingleGroup; JingleGroup.prototype.fetchCarts = function() { var self = this; $.when( rivendell.listCart(this.lowcart, 1, function(cartXml) { self.mainCart = self.createCartFromXml(cartXml, true); }), rivendell.listCart(this.highcart, 1, function(cartXml) { self.deactivateCart = self.createCartFromXml(cartXml, false); }) ).then(function() { self.renderCarts(); }); }; JingleGroup.prototype.createCartFromXml = function(cartXml, active) { var cart = new JingleCart( $(cartXml).find('number').text(), $(cartXml).find('title').text(), $(cartXml).find('groupName').text(), this ); 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 ); cart.addCut(cut); }); return cart; }; JingleGroup.prototype.render = function() { var self = this; this.$el = $('.jingleGroupTemplate').clone().removeClass('jingleGroupTemplate'); this.$el.appendTo('#app-jingles .groups'); $('h2', this.$el).html(this.title); $('table tbody tr', this.$el).remove(); $('.uploadButton', this.$el).on('click', function() { importer.showUploadModal(self); }); this.renderCarts(); }; JingleGroup.prototype.renderCarts = function() { $('table > tbody', this.$el).html(''); var self = this; if (this.mainCart) { $.each(this.mainCart.cuts, function(index, cut) { $('table > tbody', self.$el).append(cut.render()); }); } if (this.deactivateCart) { $.each(this.deactivateCart.cuts, function(index, cut) { $('table > tbody', self.$el).append(cut.render()); }); } }; JingleGroup.prototype.destroy = function() { $('table > tbody', this.$el).html(''); }; /*JingleGroup.prototype.addUpload = function() { var $tfoot = $('table > tfoot', this.$el); var $progressBar = $('.progressBarTemplate').clone().removeClass('progressBarTemplate'); $progressBar.appendTo($tfoot); return $progressBar; };*/ var JingleCart = function(number, title, groupName, group) { this.number = number; this.title = title; this.groupName = groupName; this.group = group; this.cuts = []; }; JingleCart.prototype.addCut = function(cut) { this.cuts.push(cut); }; JingleCart.prototype.removeCut = function(cut) { var self = this; $.each(this.cuts, function(index, currentCut){ if(currentCut === cut) { self.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() { var self = this; var destinationCart = this.cartNumber; // todo: make this work for multiple groups if (groupList.groups.length === 2) { $(groupList.groups).each(function(index, group) { if (self.active) { if (self.cartNumber !== group.mainCart.number) { destinationCart = group.mainCart; } } else { if (self.cartNumber !== group.deactivateCart.number) { destinationCart = group.deactivateCart; } } }); } else { return; } // todo: fix rivendell.moveCut(this.cartNumber, this.number, destinationCart.number, function() { self.cart.group.fetchCarts(); destinationCart.group.fetchCarts(); }); }; JingleCut.prototype.toggleActive = function() { var destinationCart = this.cartNumber; if (this.active) { destinationCart++; } else { destinationCart--; } var self = this; rivendell.moveCut(this.cartNumber, this.number, destinationCart, function() { self.cart.group.fetchCarts(); }); }; JingleCut.prototype.removeSelf = function() { var self = this; rivendell.removeCut(this.cartNumber, this.number, function() { self.cart.removeCut(this); self.$el.remove(); }); }; JingleCut.prototype.render = function() { 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.toggleActive(); }); deleteButton.on('click', function() { self.removeSelf(); }); this.$el = $('') .attr('id', 'jingle-' + this.cartNumber + '-' + this.number) .append($('').text(this.name)) .append($('').text(this.description)) .append( $('') .append(moveButton) .append(activateButton) .append(deleteButton) ); return this.$el; };