summaryrefslogtreecommitdiff
path: root/www/js/jingles.js
blob: 1d451b2aac37a04ad30190426b91b77ae84c6e02 (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
/*
 *  rhwebimport
 *
 *  Copyright (C) 2014-2015 Christian Pointner <equinox@helsinki.at>
 *  Copyright (C) 2015 Peter Grassberger <petertheone@gmail.at>
 *
 *  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/>.
 */

var JingleGroupList = function() {
  this.groups = [];

  $(this).on('change', this.render);

  var self = this;
  $(this).on('change', function() {
    console.log(self);
  });

  this.fetchGroups();
};

JingleGroupList.prototype.fetchGroups = function() {
  console.log('fetchGroups JingleGroupList');

  var self = this;
  data = { LOGIN_NAME: auth_username, PASSWORD: auth_token };
  $.post("/rh-bin/listdropboxes.cgi", data, function(groupsXml, status, req) {
    var dbs = $(groupsXml).find("dropboxList").children();
    dbs.each(function(index, groupXml) {
      type = $(groupXml).find('type').text();
      if (type == 'jingle') {
        var jingleGroup = new JingleGroup(
          $(groupXml).find('jingle-title').text(),
          $(groupXml).find('group').text(),
          $(groupXml).find('group-description').text(),
          $(groupXml).find('group-low-cart').text(),
          $(groupXml).find('group-high-cart').text(),
          $(groupXml).find('normalization-level').text(),
          $(groupXml).find('autotrim-level').text()
        );
        $(jingleGroup).on('change', function() {
          $(self).trigger('change');
        });
        self.groups.push(jingleGroup);
      }
    });
  }, "xml");
};

JingleGroupList.prototype.render = function() {
  console.log('render JingleGroupList');

  $.each(this.groups, function(index, group) {
    group.render();
  });
};

var JingleGroup = function(title, groupName, description, lowcart, highcart, normlevel, trimlevel) {
  this.title = title;
  this.groupName = groupName;
  this.description = description;
  this.lowcart = lowcart;
  this.highcart = highcart;
  this.normlevel = normlevel;
  this.trimlevel = trimlevel;

  this.mainCart = null;
  this.deactivateCart = null;

  this.fetchCarts();
};

JingleGroup.prototype.render = function() {
  console.log('render JingleGroup');

  var cuts = $.merge(this.mainCart.cuts, this.deactivateCart.cuts);
  cuts = cuts.sort(function(a, b) {
    return a.title.toLowerCase() < b.title.toLowerCase();
  });

  if (this.groupName === 'jingAllgem') {
    $('#jingles-jingAllgem table tbody').find('tr').remove();
    $.each(cuts, function(index, cut) {
      $('#jingles-jingAllgem table > tbody').append(cut.$el());
    });
  } else if (this.groupName === 'jingAllgem') {
    $('#jingles-jingAnlass table tbody').find('tr').remove();
    $.each(cuts, function(index, cut) {
      $('#jingles-jingAnlass table > tbody').append(cut.$el());
    });
  }
};

JingleGroup.prototype.fetchCarts = function() {
  console.log('fetchGroups JingleGroup');
  var self = this;
  data = { COMMAND: 7, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: this.lowcart, INCLUDE_CUTS: 1 };
  gcd = $.post("/rd-bin/rdxport.cgi", data, function(cartXml) {
    self.mainCart = self.createCartFromXml(cartXml, true);
    $(self).trigger('change');
  }, "xml");
  data = { COMMAND: 7, LOGIN_NAME: auth_username, PASSWORD: auth_token, CART_NUMBER: this.highcart, INCLUDE_CUTS: 1 };
  gcd = $.post("/rd-bin/rdxport.cgi", data, function(cartXml) {
    self.deactivateCart = self.createCartFromXml(cartXml, false);
  }, "xml");
};

JingleGroup.prototype.createCartFromXml = function(cartXml, active) {
  var cart = new JingleCart(
    $(cartXml).find('title').text(),
    $(cartXml).find('groupName').text()
  );

  var cuts = $(cartXml).find("cutList").children();
  cuts.each(function(index, cutXml) {
    cart.addCut(new JingleCut(
      $(cutXml).find('cutName').text(),
      $(cutXml).find('description').text(),
      active
    ));
  });

  return cart;
};


var JingleCart = function(title, groupName) {
  this.title = title;
  this.groupName = groupName;

  this.cuts = [];
};

JingleCart.prototype.addCut = function(cut) {
  this.cuts.push(cut);
};

var JingleCut = function(name, description, active) {
  this.name = name;
  this.description = description;
  this.active = active;
};

JingleCut.prototype.move = function() {
  console.log('move');
  // move to other group, if active stay in mainCart, if deactivated stay in deactiveCart


  //$(this).trigger('change');
};

JingleCut.prototype.toggleActivate = function() {
  console.log('toggleActivate');
  // move to other cart, from mainCart to deactiveCart or the other way around

  //$(this).trigger('change');
};

JingleCut.prototype.delete = function() {
  console.log('delete');
  // remove cut, command 11

  //$(this).trigger('change');
};

JingleCut.prototype.$el = function() {
  var moveButton = $('<button class="btn btn-info btn-mini"><i class="icon-arrow-right icon-white"></i> Verschieben</button>');
  var activateButton;
  if (this.active) {
    activateButton = $('<button class="btn btn-warning btn-mini"><i class="icon-minus icon-white"></i> Deactivieren</button>');
  } else {
    activateButton = $('<button class="btn btn-warning btn-mini"><i class="icon-plus icon-white"></i> Aktivieren</button>');
  }
  var deleteButton = $('<button class="btn btn-danger btn-mini"><i class="icon-trash icon-white"></i> Löschen</button>');

  moveButton.on('click', this.move);
  activateButton.on('click', this.toggleActivate);
  deleteButton.on('click', this.delete);

  return $('<tr>').append($('<td>').text(this.name))
    .append($('<td>').text(this.description))
    .append(
      $('<td>').css('text-align', 'center')
        .append(moveButton)
        .append(activateButton)
        .append(deleteButton)
    );
};

var jinglesGroupList = null;

function jingles_init() {
  jinglesGroupList = new JingleGroupList();
}

function jingles_cleanup() {
  jinglesGroupList = null;
}