summaryrefslogtreecommitdiff
path: root/www/js/musicpools.js
blob: 2292aa1d28a83db90aa43c0f54de0dc016e8835e (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 *  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 Rdxport = Rdxport || {};

var musicpoolsView = null;

function musicpools_init(subpage) {
  var musicpools = new Rdxport.GroupList();
  musicpoolsView = new Rdxport.MusicpoolsView(musicpools, subpage);
}

function musicpools_cleanup() {
  importer.closeAllUploads();
  musicpoolsView = null;
}

Rdxport.MusicpoolsView = function(model, subpage) {
  this.model = model;

  this.musicpoolViews = [];
  this.currentPoolId = null;

  this.setCurrentPoolId(subpage);

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

Rdxport.MusicpoolsView.prototype.setCurrentPoolId = function(currentPoolId) {
  if (!currentPoolId) {
    return;
  }
  if (this.currentPoolId !== currentPoolId) {
    if (this.currentPoolId) {
      history.pushState(null, null, '/musicpools/' + currentPoolId + '/');
    } else {
      history.replaceState(null, null, '/musicpools/' + currentPoolId + '/');
    }
  }
  this.currentPoolId = currentPoolId;
  sessionStorage.setItem('currentPoolId', this.currentPoolId);
};

Rdxport.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;
};

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

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

  $(this.model.groups).sort(function(a, b) {
    var atitle = a.title.toLowerCase();
    var btitle = b.title.toLowerCase();
    if(atitle == btitle) {
      if(a.clock > b.clock) return 1;
      if(a.clock < b.clock) return -1;
      return 0;
    }
    if(atitle > btitle) return 1;
    return -1;
  }).each(function(index, musicpool) {
    var name = '<strong>' + musicpool.title + '</strong> (' + musicpool.clock + ')';
    var link = $('<a>').attr('href', '#').html(name).click(function() {
      self.setCurrentPoolId(musicpool.clock);
      self.getCurrentPoolView().model.fetchCarts();
    });
    $musicpoolSelector.append($('<li>').append(link));
  });
  if($musicpoolSelector.children().length == 0) {
    $musicpoolSelector.append($('<li>').append($('<a>').text('Keinen Musikpool gefunden!')));
  }

  // todo: maybe integrate this into setCurrentShowId?
  if (!this.currentPoolId) {
    var currentPoolId = sessionStorage.getItem('currentPoolId');
    if (currentPoolId) {
      this.setCurrentPoolId(currentPoolId);
    } else {
      this.setCurrentPoolId(this.model.groups[0].id);
    }
  }

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

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

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

  this.$el = null;

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

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

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

  $('#musicpool-title').text(this.model.title);
  $('table tbody tr', this.$el).remove();

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

    $('table > tbody', self.$el).append(cartView.$el);
  });
  var uploads = importer.getUploadsByGroupName(this.model.name);
  $(uploads).each(function(index, upload) {
    var $el = Rdxport.MusicpoolCartView.renderUploading(upload, self.model.number);
    $('table > tbody', self.$el).append($el);
  });


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

Rdxport.MusicpoolView.prototype.uploadProgress = function(upload) {
  var $cart = $('tr[data-upload-id="' + upload.uploadId + '"]').first();
  if (!$cart.hasClass('uploading')) {
    var $progressBar = $('.progressBarTemplate.musicpools').clone().removeClass('progressBarTemplate');
    $cart.html($progressBar.html());

    $('button', $cart).on('click', function() {
      upload.cancel();
    });

    $cart.addClass('uploading');
  }
  if (upload.title) {
    $('.file-name', $cart).text(upload.title);
  }
  if (upload.cartNumber) {
    $('.cart-number', $cart).text(upload.cartNumber);
  }

  updateProgressBar($cart, upload);
};

Rdxport.MusicpoolView.prototype.uploadError = function(upload, status, errorString, acknowledge) {
  var reason = $('<span>').addClass('label').addClass('label-danger').text(status).after($('<b>').text(' ' + errorString));

  var dismiss_button = '<button class="btn btn-info btn-xs">' +
    '<span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;Ok</button>';

  var $errorRow = $('<tr>')
    .attr('data-upload-id', upload.uploadId)
    .append($('<td>').addClass('.cart-number').text('...'))
    .append($('<td>').addClass('file-name'))
    .append($('<td>').append($('<b>').text('Import Fehler')))
    .append($('<td colspan="5">').append(reason))
    .append($('<td>').css('text-align', 'center').append(dismiss_button));

  if (upload.cartNumber) {
    $('.cart-number', $errorRow).text(upload.cartNumber);
  }
  if (upload.title) {
    $('.file-name', $errorRow).text(upload.title);
  }

  $('button', $errorRow).on('click', function() {
    acknowledge();
  });

  var $cart = $('tr[data-upload-id="' + upload.uploadId + '"]').first();
  $cart.replaceWith($errorRow);
};

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

  this.$spinner = null;
  this.$el = null;

  this.render();
};

Rdxport.MusicpoolCartView.prototype.render = function() {
  var length = '-';
  var imported = '-';
  var playcnt = '-';
  var lastplayed = '-';

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

    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.$spinner = $('#hiddenTemplates .spinnerTemplate').clone().removeClass('spinnerTemplate');

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

Rdxport.MusicpoolCartView.renderUploading = function(upload) {
  var $progressBar = $('.progressBarTemplate.musicpools').clone().removeClass('progressBarTemplate');
  $('.file-name', $progressBar).text(upload.title);

  var $el = $('<tr>')
    .html($progressBar.html())
    .attr('id', 'musicpool-upload-' + upload.uploadId)
    .addClass('uploading')
    .attr('data-upload-id', upload.uploadId);

  $('button', $el).on('click', function() {
    upload.cancel();
  });

  updateProgressBar($el, upload);
  return $el;
};

Rdxport.MusicpoolCartView.prototype.delete = function() {
  this.$el.find('td:last').html(this.$spinner);

  var self = this;
  rdxport.removeCart(this.model.number, function() {
    self.model.group.removeCart(self.model);
    self.$el.remove();
  });
};