summaryrefslogtreecommitdiff
path: root/www/js/musicpools.js
blob: ce8e81a02938b6c1dfa9f23446588a22cd1b54b6 (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
/*
 *  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 rivendell = null;
var importer = null;
var musicpoolsView = null;

function musicpools_init() {
  rivendell = new Rivendell.Rivendell(auth_username, auth_token, '/rd-bin/rdxport.cgi');
  rivendell.setListDropboxesEndpoint('/rh-bin/listdropboxes.cgi');

  importer = new Rivendell.Importer();

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

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

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

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

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

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().render();
  });

  this.getCurrentPoolView().render();
};

// this and jinglegroup are basicly the same thing
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.$el = null;
};

Rivendell.MusicpoolView.prototype.render = function() {
  var self = this;
  this.model.fetchCarts(function() {
    self.$el = $('#hiddenTemplates .musicpoolTemplate').clone().removeClass('musicpoolTemplate');
    $('#app-musicpools .musicpoolContainer').html(self.$el);

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

    // todo
    /*$('.uploadButton', self.$el).on('click', function() {
      importer.showUploadModal(self.model);
    });*/

  });
};

Rivendell.Musicpool.prototype.fetchCarts = function(success) {
  var self = this;
  rivendell.listCarts(this.groupName, 1, function(cartsXml, status, req) {
    self.carts = [];

    var dbs = $('cartList', cartsXml).children();
    dbs.each(function(index, cartXml) {
      var cart = new Rivendell.Cart(cartXml, self);

      var cuts = $('cutList', cartXml).children();
      cart.cuts.push(new Rivendell.Cut(cuts[0], cart));

      self.carts.push(cart);
    });

    success();
  });
};

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

  this.$el = null;
};