summaryrefslogtreecommitdiff
path: root/www/js/musicpools.js
blob: 301ce070fbfc6a66b655acdb3baf4b744a4100ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 *  rhwebimport
 *
 *  Copyright (C) 2014-2016 Christian Pointner <equinox@helsinki.at>
 *  Copyright (C) 2015-2016 Peter Grassberger <petertheone@gmail.com>
 *
 *  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 <http://www.gnu.org/licenses/>.
 */

'use strict';

var Rivendell = Rivendell || {};

var musicpoolsView = null;

function musicpools_init() {
  var musicpools = new Rivendell.GroupList(rivendell);
  musicpoolsView = new Rivendell.MusicpoolsView(musicpools);
}

function musicpools_cleanup() {
  musicpoolsView = null;
  rivendell = null;
}

Rivendell.MusicpoolsView = function(model) {
  this.model = model;

  this.musicpoolViews = [];
  this.currentPoolId = sessionStorage.getItem('currentPoolId');

  var self = this;
  $(this.model).on('update', function() {
    $(self.model.groups).each(function(index, musicpool) {
      var musicpoolView = new Rivendell.MusicpoolView(musicpool);
      self.musicpoolViews.push(musicpoolView);
    });
    self.updateSelector();
  });
  this.model.fetch('musicpool');
};

Rivendell.MusicpoolsView.prototype.setCurrentPoolId = function(currentPoolId) {
  this.currentPoolId = currentPoolId;
  sessionStorage.setItem('currentPoolId', this.currentPoolId);
};

Rivendell.MusicpoolsView.prototype.getCurrentPoolView = function() {
  if (this.model.groups.length === 0) {
    return null;
  }
  if (this.musicpoolViews.length === 0) {
    return null;
  }
  if (this.currentPoolId === null) {
    this.setCurrentPoolId(this.model.groups[0].clock);
  }
  var self = this;
  var musicpoolViewFound = null;
  $(this.musicpoolViews).each(function(index, musicpoolView) {
    if (musicpoolView.model.clock === self.currentPoolId) {
      musicpoolViewFound = musicpoolView;
      return true;
    }
  });
  return musicpoolViewFound;
};

Rivendell.MusicpoolsView.prototype.updateSelector = function() {
  var self = this;
  var $musicpoolSelector = $('#musicpool-selector');

  $musicpoolSelector.off();
  $('option', $musicpoolSelector).remove();

  $(this.model.groups).each(function(index, musicpool) {
    var name = musicpool.title + ' (' + musicpool.clock + ')';
    $musicpoolSelector.append($('<option>').attr('value', musicpool.clock).text(name));
  });

  if (this.currentPoolId === null) {
    this.setCurrentPoolId(this.model.groups[0].clock);
  }
  $('option[value="' + this.currentPoolId + '"]', $musicpoolSelector).attr('selected', 'selected');

  $musicpoolSelector.on('change', function() {
    self.setCurrentPoolId($('option:selected', $musicpoolSelector).attr('value'));
    self.getCurrentPoolView().model.fetchCarts();
  });

  this.getCurrentPoolView().model.fetchCarts();
};

Rivendell.Musicpool = function(groupName, description, lowcart, highcart, normlevel, trimlevel, title, clock) {
  if (arguments.length === 1) {
    Rivendell.Group.call(this, groupName);
    this.title = $('musicpool-title', this.xml).text();
    this.clock = $('musicpool-clock', this.xml).text();
  } else {
    Rivendell.Group.call(this, groupName, description, lowcart, highcart, normlevel, trimlevel);
    this.title = title;
    this.clock = clock;
  }
};
Rivendell.Musicpool.prototype = Object.create(Rivendell.Group.prototype);
Rivendell.Musicpool.prototype.constructor = Rivendell.Musicpool;

Rivendell.MusicpoolView = function(model) {
  this.model = model;
  this.cartViews = [];

  this.$el = null;

  var self = this;
  $(this.model).on('update', function() {
    self.render();
  });
};

Rivendell.MusicpoolView.prototype.render = function() {
  var self = this;

  this.$el = $('#hiddenTemplates .musicpoolTemplate').clone().removeClass('musicpoolTemplate');
  $('#app-musicpools .musicpoolContainer').html(this.$el);

  $('h2', this.$el).html(this.model.title);
  $('table tbody tr', this.$el).remove();

  this.cartViews = [];
  $(this.model.carts).each(function(index, cart) {
    var cartView = new Rivendell.MusicpoolCartView(cart);
    self.cartViews.push(cartView);

    $('table > tbody', self.$el).append(cartView.$el);
  });

  $('.uploadButton', this.$el).on('click', function() {
    importer.openModal(self.model, self, true, true);
  });
};

Rivendell.MusicpoolView.prototype.uploadProgress = function(file) {
  if (!file.cartNumber || !file.cutNumber) {
    return;
  }
  var $cart = $('#musicpool-' + file.cartNumber).first();
  if (!$cart.hasClass('uploading')) {
    var $progressBar = $('.progressBarTemplate.musicpools').clone().removeClass('progressBarTemplate');
    $progressBar.find('.file-name').text(file.name);
    $progressBar.find('.cart-number').text(file.cartNumber);
    $cart.html($progressBar.html());
    $cart.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";
    $cart.find('.file-bytes').text(bytes_str);
    $cart.find('.progress .progress-bar').css("width", file.upload.progress + "%");
  } else {
    $cart.find('.file-bytes').text('importiere...');
    $cart.find('.progress .progress-bar').css('width', '100%');
    $cart.find('.progress .progress-bar').addClass('progress-bar-striped').addClass('active');
  }
};

Rivendell.MusicpoolCartView = function(model) {
  this.model = model;

  this.$el = null;

  this.render();
};

Rivendell.MusicpoolCartView.prototype.render = function() {
  var number = this.model.number;
  var title = this.model.title;

  var length = '-';
  var imported = '-';
  var playcnt = '-';
  var lastplayed = '-';

  if (this.model.cuts.length > 0) {
    var cut = this.model.cuts[0];

    title = this.model.cuts[0].description;

    length = msToTimeString(Number(cut.length));
    if (!isNaN(new Date(cut.originDatetime))) {
      imported = format_datetime(new Date(cut.originDatetime));
    }
    playcnt = cut.playCounter;
    if (!isNaN(new Date(cut.lastPlayDatetime))) {
      lastplayed = format_datetime();
    }
  }

  var deleteButton = $('<button class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-trash"></span>&nbsp;&nbsp;Löschen</button>');

  var self = this;
  deleteButton.on('click', function() {
    self.delete();
  });


  this.$el = $('<tr>')
    .attr('id', 'musicpool-' + this.model.number)
    .append($('<td>').text(number))
    .append($('<td>').text(title))
    .append($('<td>').text(length))
    .append($('<td>').text(imported))
    .append($('<td>').text(playcnt))
    .append($('<td>').text(lastplayed))
    .append($('<td>').append(deleteButton));
};

Rivendell.MusicpoolCartView.prototype.delete = function() {
  var self = this;
  rivendell.removeCart(this.model.number, function() {
    self.model.group.removeCart(self.model);
    self.$el.remove();
  });
};