/* * 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 jingleGroupListView = null; function jingles_init() { var groupList = new Rivendell.GroupList(rivendell); jingleGroupListView = new Rivendell.JingleGroupListView(groupList); } function jingles_cleanup() { if (jingleGroupListView) { jingleGroupListView.destroy(); jingleGroupListView = null; } } Rivendell.JingleGroupListView = function(model) { this.model = model; this.jingleGroupViews = []; $('#app-jingles .groups').html(''); var self = this; $(this.model).on('update', function() { $(self.model.groups).each(function(index, group) { self.jingleGroupViews.push(new Rivendell.JingleGroupView(group)); }); }); this.model.fetch('jingle'); }; Rivendell.JingleGroupListView.prototype.destroy = function() { var self = this; $(this.jingleGroupViews).each(function(index, groupView) { groupView.destroy(); }); this.jingleGroupViews = []; }; Rivendell.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; } }; Rivendell.JingleGroup.prototype = Object.create(Rivendell.Group.prototype); Rivendell.JingleGroup.prototype.constructor = Rivendell.JingleGroup; Rivendell.JingleGroupView = function(model) { this.model = model; this.mainCartView = null; this.deactivateCartView = null; this.$el = null; this.render(); var self = this; $(this.model).on('update', function() { $('table > tbody', self.$el).html(''); self.model.mainCart = self.model.carts[0]; self.mainCartView = new Rivendell.JingleCartView(self.model.mainCart, self, true); self.model.deactivateCart = self.model.carts[1]; self.deactivateCartView = new Rivendell.JingleCartView(self.model.deactivateCart, self, false); }); this.model.fetchCarts(); }; Rivendell.JingleGroupView.prototype.render = function() { var self = this; this.$el = $('#hiddenTemplates .jingleGroupTemplate').clone().removeClass('jingleGroupTemplate'); this.$el.appendTo('#app-jingles .groups'); $('h2', this.$el).html(this.model.title); $('table tbody tr', this.$el).remove(); $('.uploadButton', this.$el).on('click', function() { importer.openModal(self.model, self, false, false); }); }; Rivendell.JingleGroupView.prototype.destroy = function() { $('table > tbody', this.$el).html(''); }; Rivendell.JingleGroupView.prototype.uploadProgress = function(file) { if (!file.cartNumber || !file.cutNumber) { return; } var $cut = $('#jingle-' + file.cartNumber + '-' + file.cutNumber).first(); if (!$cut.hasClass('uploading')) { var $progressBar = $('.progressBarTemplate.jingles').clone().removeClass('progressBarTemplate'); $progressBar.find('.file-name').text(file.name); $progressBar.find('.cart-number').text(file.cartNumber); $cut.html($progressBar.html()); $cut.addClass('uploading'); } if(file.upload.progress < 99) { var bytes_str = Number((file.upload.bytesSent/1024)/1024).toFixed(1) + " von " + Number((file.upload.total/1024)/1024).toFixed(1) + " MB"; $cut.find('.file-bytes').text(bytes_str); $cut.find('.progress .progress-bar').css("width", file.upload.progress + "%"); } else { $cut.find('.file-bytes').text('importiere...'); $cut.find('.progress .progress-bar').css('width', '100%'); $cut.find('.progress .progress-bar').addClass('progress-bar-striped').addClass('active'); } }; Rivendell.JingleCartView = function(model, groupView, active) { this.model = model; this.groupView = groupView; this.active = active; this.cutViews = []; var self = this; if (this.model) { $(this.model.cuts).each(function(index, cut) { cut.active = self.active; var cutView = new Rivendell.JingleCutView(cut); self.cutViews.push(cutView); $('table > tbody', self.groupView.$el).append(cutView.$el); }); } }; Rivendell.JingleCutView = function(model) { this.model = model; this.$el = null; this.render(); }; Rivendell.JingleCutView.prototype.render = function() { var moveButton = $(''); var activateButton; if (this.model.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.delete(); }); this.$el = $('') .attr('id', 'jingle-' + this.model.cartNumber + '-' + this.model.number) .append($('').text(this.model.name)) .append($('').text(this.model.description)) .append( $('') .append(moveButton) .append(' ') .append(activateButton) .append(' ') .append(deleteButton) ); }; Rivendell.JingleCutView.prototype.move = function() { var self = this; var destinationCart = this.model.cartNumber; // todo: make this work for multiple groups if (jingleGroupListView.model.groups.length === 2) { $(jingleGroupListView.model.groups).each(function(index, group) { if (self.model.active) { if (self.model.cartNumber !== group.mainCart.number) { destinationCart = group.mainCart; } } else { if (self.model.cartNumber !== group.deactivateCart.number) { destinationCart = group.deactivateCart; } } }); } else { return; } // todo: fix rivendell.moveCut(this.model.cartNumber, this.model.number, destinationCart.number, function() { self.model.cart.group.fetchCarts(); destinationCart.group.fetchCarts(); }); }; Rivendell.JingleCutView.prototype.toggleActive = function() { var destinationCart = this.model.cartNumber; if (this.model.active) { destinationCart++; } else { destinationCart--; } var self = this; rivendell.moveCut(this.model.cartNumber, this.model.number, destinationCart, function() { self.model.cart.group.fetchCarts(); }); }; Rivendell.JingleCutView.prototype.delete = function() { var self = this; rivendell.removeCut(this.model.cartNumber, this.model.number, function() { self.model.cart.removeCut(self.model); self.$el.remove(); }); };