summaryrefslogtreecommitdiff
path: root/www/js/shows.js
blob: c325e01b18135d4c1e6af3e6d72e6f1847b721eb (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 *  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 shows_currentid = null;
var shows_list = [];
var shows_current;
var shows_group_carts = {};
var shows_log_carts = [];
var shows_clock;

function shows_deleteCart(cart) {
  rivendell.removeCart(cart, function() {
    shows_updateGroupCartInfo(cart);
  });
}

function shows_importAddCut(cart, dz, file) {
  rivendell.addCut(cart, function() {
    dz.processQueue();
  }).fail(function(xhr, status, err) {
    shows_importFileUploadError(cart, dz, file, err, xhr);
  });
}

function shows_importAddCart(cart, dz, file) {
  rivendell.addCart(shows_current.group.name, 'audio', cart, function() {
    shows_importAddCut(cart, dz, file);
  }).fail(function(xhr, status, err) {
    shows_importFileUploadError(cart, dz, file, err, xhr);
  });
}

function shows_importCartCancel(cart, dz) {
  shows_deleteCart(cart);
  dz.off("error");
  dz.disable();
}

function shows_importFileUploadError(cart, dz, file, msg, xhr) {
  var error_row = shows_newImportErrorEntry(cart, msg);
  error_row.find('button').unbind('click').click(function() { shows_deleteCart(cart); });
  $('#show-cart-' + cart).replaceWith(error_row);
  error_row.find('.file-name').text(file.name);
  dz.disable();
}

function shows_importFileUploadSuccess(cart, dz, file) {
  shows_updateGroupCartInfo(cart);
  dz.disable();
}

function shows_importUpdateProgress(file, stats) {
  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";
    stats.find('.file-bytes').text(bytes_str);
    stats.find('.progress .bar').css("width", file.upload.progress + "%");
  } else {
    stats.find('.file-bytes').text('importiere...');
    stats.find('.progress .bar').css('width', '100%');
    stats.find('.progress').addClass('progress-striped').addClass('active');
  }
}

function shows_importCartConfirm(cart, dz) {
  $('#uploadModal').modal('hide');
  var cart_row = $('#show-cart-' + cart);
  cart_row.find('.btn').attr('disabled','disabled');
  var importing_row = shows_newImportingEntry(cart);
  importing_row.find('button').unbind('click').click(function() { shows_importCartCancel(cart, dz); });
  cart_row.replaceWith(importing_row);

  dz.off("error");
  var files = dz.getAcceptedFiles();
  importing_row.find('.file-name').text(files[0].name);
  shows_importUpdateProgress(files[0], importing_row);
  dz.on("uploadprogress", function(file) { shows_importUpdateProgress(file, importing_row); });
  dz.on("success", function(file) { shows_importFileUploadSuccess(cart, dz, file); });
  dz.on("error", function(file, msg, xhr) { shows_importFileUploadError(cart, this, file, msg, xhr); });
  shows_importAddCart(cart, dz, files[0]);
}

function shows_createImportForm(cart) {
  return $('<form>').attr("action", "/rd-bin/rdxport.cgi").attr("method", "post").attr("enctype", "multipart/form-data")
             .append($('<input>').attr("type", "hidden").attr("name", "COMMAND").attr("value", 2))
             .append($('<input>').attr("type", "hidden").attr("name", "LOGIN_NAME").attr("value", auth_username))
             .append($('<input>').attr("type", "hidden").attr("name", "PASSWORD").attr("value", auth_token))
             .append($('<input>').attr("type", "hidden").attr("name", "CART_NUMBER").attr("value", cart))
             .append($('<input>').attr("type", "hidden").attr("name", "CUT_NUMBER").attr("value", 1))
             .append($('<input>').attr("type", "hidden").attr("name", "CHANNELS").attr("value", 2))
             .append($('<input>').attr("type", "hidden").attr("name", "NORMALIZATION_LEVEL").attr("value", shows_current.normlevel))
             .append($('<input>').attr("type", "hidden").attr("name", "AUTOTRIM_LEVEL").attr("value", shows_current.trimlevel))
             .append($('<input>').attr("type", "hidden").attr("name", "USE_METADATA").attr("value", 1));
}

function shows_importFileAdded(dz, file, cart) {
  $(dz.getAcceptedFiles()).each(function(idx, elem) { dz.removeFile(elem); });
  $('#uploadModal div.modal-body').css("background-image", "url('/img/audio_file.png')")
  $('#uploadModal div.modal-header h3').text(file.name)
  $('#uploadModal-confirm').removeAttr('disabled').unbind('click').click(function() { shows_importCartConfirm(cart, dz); });
}

function shows_importFileSelectError(dz, file, msg) {
  $('#uploadModal div.modal-header h3').text("Datei auswählen...")
  $('#uploadModal div.modal-body').css("background-image", "url('/img/dz-backdrop.png')");
  $('#uploadModal-confirm').attr('disabled','disabled').unbind('click');
  dz.removeFile(file);
}

function shows_importCart(cart) {
  var form = shows_createImportForm(cart);
  $('#uploadModal div.modal-header h3').text("Datei auswählen...")
  $('#uploadModal div.modal-body').empty().append(form).css("background-image", "url('/img/dz-backdrop.png')");
  $('#uploadModal-confirm').attr('disabled','disabled').unbind('click');
  var dz = form.dropzone({ url: "/rd-bin/rdxport.cgi", parallelUploads: 1, maxFilesize: 2048, paramName: 'FILENAME',
                           uploadMultiple: false, clickable: true, createImageThumbnails: false,
                           acceptedFiles: ".flac,.wav,.ogg,.mp3", autoProcessQueue: false, init: function() {
            this.on("addedfile", function(file) { shows_importFileAdded(this, file, cart); });
            this.on("error", function(file, msg) { shows_importFileSelectError(this, file, msg); });
    }});

  $('#uploadModal').modal({ keyboard: true });
}

// <td>
//   <button class="btn btn-info btn-mini disabled"><i class="icon-arrow-up icon-white"></i></button>
//   <button class="btn btn-info btn-mini disabled"><i class="icon-arrow-down icon-white"></i></button>
//   <button class="btn btn-danger btn-mini"><i class="icon-trash icon-white"></i> Löschen</button>
// </td>
// <td>
//   <button class="btn btn-info btn-mini"><i class="icon-arrow-up icon-white"></i></button>
//   <button class="btn btn-info btn-mini disabled"><i class="icon-arrow-down icon-white"></i></button>
//   <button class="btn btn-success btn-mini"><i class="icon-upload icon-white"></i> Importieren</button>
// </td>

function shows_newCartEntry(elem) {
  var cart_exists = shows_group_carts[elem] ? 1 : 0;
  var cart = {};
  cart.title = cart_exists ? shows_group_carts[elem].title : '-';
  cart.length = cart_exists ? shows_group_carts[elem].length : '-';
  cart.imported = cart_exists ? format_datetime(shows_group_carts[elem].imported) : '-';
  cart.playcnt = cart_exists ? shows_group_carts[elem].playcnt : '-';
  cart.lastplayed = cart_exists ? format_datetime(shows_group_carts[elem].lastplayed) : '-';

  var buttons = cart_exists ?
        '<button class="btn btn-danger btn-mini" onclick="shows_deleteCart(' + elem + ');">' +
           '<i class="icon-trash icon-white"></i> Löschen</button>' :
        '<button class="btn btn-success btn-mini" onclick="shows_importCart(' + elem + ');">' +
           '<i class="icon-upload icon-white"></i> Importieren</button>';

  return $('<tr>').append($('<td>').text(elem))
                  .append($('<td>').text(cart.title))
                  .append($('<td>').text(msToTimeString(cart.length)))
                  .append($('<td>').text(cart.imported))
                  .append($('<td>').text(cart.playcnt))
                  .append($('<td>').text(cart.lastplayed))
                  .append($('<td>').css('text-align', 'center').append(buttons))
                  .attr("id", "show-cart-" + elem);
}

function shows_newImportingEntry(cart) {
  var progress_bar = '<div class="progress"><div class="bar" style="width: 0%;"></div></div>';
  var cancel_button = '<button class="btn btn-danger btn-mini">' +
                      '<i class="icon-remove icon-white"></i> Abbrechen</button>';

  return $('<tr>').append($('<td>').text(cart))
                  .append($('<td>').addClass('file-name').text('-'))
                  .append($('<td>').addClass('file-bytes').text('??.? von ??.? MB'))
                  .append($('<td colspan=3>').append(progress_bar))
                  .append($('<td>').css('text-align', 'center').append(cancel_button))
                  .attr("id", "show-cart-" + cart);
}

function shows_newImportErrorEntry(cart, msg) {
  var dismiss_button = '<button class="btn btn-info btn-mini">' +
                       '<i class="icon-remove icon-white"></i> Ok</button>';

  var xmlDoc = $.parseXML(msg);
  msg = $(xmlDoc);
  var resp_code = msg.find('ResponseCode').text();
  var err_string = msg.find('ErrorString').text();
  var reason = $('<span>').addClass('badge').addClass('badge-error').text(resp_code).after($('<b>').html('&nbsp;' + err_string))

  return $('<tr>').append($('<td>').text(cart))
                  .append($('<td>').addClass('file-name').text('-'))
                  .append($('<td>').append($('<b>').text('Import Fehler')))
                  .append($('<td colspan=3>').append(reason))
                  .append($('<td>').css('text-align', 'center').append(dismiss_button))
                  .attr("id", "show-cart-" + cart);
}

function shows_updateGroupCartInfo(cart) {
  rivendell.listCart(cart, 1, function(data) {
    shows_updateGroupCartList(data);
    shows_redrawCartEntry(cart);
  }).fail(function() {
    delete shows_group_carts[cart];
    shows_redrawCartEntry(cart);
  });
}

function shows_redrawCartEntry(cart) {
  $('#show-cart-' + cart).replaceWith(shows_newCartEntry(cart));
}

function shows_updateCartListing() {
  $('#show-carts tbody').find('tr').remove();
  shows_log_carts.forEach(function(elem) {
    $('#show-carts > tbody:last').append(shows_newCartEntry(elem));
  });
}

function shows_updateGroupCartList(data) {
  var cartlist = $(data).find("cartList");
  var carts = cartlist.children();
  carts.each(function() {
      var cut = $(this).find("cutList").get(0);
      var number = $(this).find('number').text();
      var cart = {
        title: $(this).find('title').text(),
        length: Number($(cut).find('length').text()),
        imported: new Date($(cut).find('originDatetime').text()),
        playcnt: $(cut).find('playCounter').text(),
        lastplayed: new Date($(cut).find('lastPlayDatetime').text())
      };
      cart.imported = isNaN(cart.imported) ? '-' : cart.imported;
      cart.lastplayed = isNaN(cart.lastplayed) ? '-' : cart.lastplayed;
      shows_group_carts[number] = cart;
    }
  );
}

function shows_updateLogCartList(data, status, req) {
  var loglist = $(data).find("logList");
  var loglines = loglist.children();
  loglines.each(function() {
      var number = $(this).find('cartNumber').text();
      if(number >= shows_current.group.lowcart && number <= shows_current.group.highcart) {
        shows_log_carts.push(number);
      }
    }
  );
}

function shows_showSelected() {
  shows_currentid = $('#show-selector option:selected').attr('value');
  sessionStorage.setItem("shows_currentid", shows_currentid);
  shows_current = $.grep(shows_list, function(elem) { return elem.id == shows_currentid; })[0];

  if(shows_current) {
    $('#show-title').text(shows_current.title);
    $('#show-dow').text(weekday[shows_current.dow]);
    $('#show-rhythm').text(shows_current.rhythm);
    $('#show-starttime').text(shows_current.starttime);
    $('#show-length').text(shows_current.length + ' Min.');

    shows_log_carts = [];
    var lcd = rivendell.listLog(shows_current.log, shows_updateLogCartList);

    shows_group_carts = {};
    var gcd = rivendell.listCarts(shows_current.group.name, 1, shows_updateGroupCartList);

    $.when(lcd, gcd).done(
      function(lcres, gcres) {
        if(lcres[1] == 'success' && gcres[1] == 'success') {
          shows_updateCartListing();
        }
      }
    );
  }
}

function shows_updateList(data, status, req) {
  shows_list = [];
  $('#show-selector').find('option').remove();
  var dblist = $(data).find("dropboxList");
  var dbs = dblist.children();
  dbs.each(function() {
    var show = {
      id: $(this).find('show-id').text(),
      title: $(this).find('show-title').text(),
      dow: $(this).find('show-dayofweek').text(),
      rhythm: $(this).find('show-rhythm').text(),
      starttime: $(this).find('show-starttime').text(),
      length: $(this).find('show-length').text(),
      log: $(this).find('show-log').text(),
      group: {
        name: $(this).find('group').text(),
        lowcart: $(this).find('group-low-cart').text(),
        highcart: $(this).find('group-high-cart').text()
      },
      normlevel: $(this).find('normalization-level').text(),
      trimlevel: $(this).find('autotrim-level').text()
    };

    var name = show.title + ' (' + show.rhythm + ', ' + weekday[show.dow] + ', ' + show.starttime + ', ' + show.length + ' Min.)';
    $('#show-selector').append($('<option>').attr('value',show.id).text(name));

    shows_list.push(show);
  });
  $('#show-selector').val(shows_currentid);
  shows_showSelected();
}

function shows_init() {
  shows_currentid = sessionStorage.getItem("shows_currentid");
  shows_list = [];
  rivendell.listDropboxes('show', shows_updateList);
  drawClock('Do, 1.1.1970', '00:00:00', 0);
  clock_add_callback(drawClock);
}

function shows_cleanup() {
  $('#show-carts tbody').find('tr').remove();
  sessionStorage.removeItem("shows_currentid");
  shows_currentid = null;
  shows_list = [];
  shows_group_carts = {};
  shows_log_carts = [];
}