2014-03-02 00:27:40 +01:00
|
|
|
/*global $, config, RecAPI, poll_job*/
|
2013-12-10 01:52:21 +01:00
|
|
|
|
2014-03-01 20:43:45 +01:00
|
|
|
//TODO: move to a separate file(?)
|
2013-12-02 20:58:43 +01:00
|
|
|
$.widget("ror.countclock", {
|
|
|
|
options: {
|
2013-12-27 02:17:12 +01:00
|
|
|
errormsg: null,
|
2013-12-02 20:58:43 +01:00
|
|
|
since: null,
|
2014-04-22 16:57:11 +02:00
|
|
|
editable:true,
|
2013-12-03 21:23:54 +01:00
|
|
|
to: null
|
2013-12-02 20:58:43 +01:00
|
|
|
},
|
|
|
|
_create: function() {
|
2014-04-22 16:57:11 +02:00
|
|
|
"use strict";
|
2013-12-02 20:58:43 +01:00
|
|
|
this._update();
|
|
|
|
//TODO: aggiungi conto secondi/minuti passati
|
2014-04-22 16:57:11 +02:00
|
|
|
|
|
|
|
var widg = this;
|
|
|
|
this.element.on('click', '.countclock-edit-time', function() {
|
|
|
|
if(widg.options.editable === true) {
|
|
|
|
widg._change_starttime(widg.options.since);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_change_starttime: function ( since ) {
|
|
|
|
"use strict";
|
|
|
|
var widget = this;
|
|
|
|
time_changer_dialog(since, function(newsince) {
|
|
|
|
widget._trigger("change", null, { since: newsince })
|
|
|
|
});
|
2013-12-02 20:58:43 +01:00
|
|
|
},
|
2014-04-22 16:57:11 +02:00
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
_setOption: function(key, value) {
|
|
|
|
this.options[key] = value;
|
|
|
|
this._update();
|
|
|
|
},
|
2014-04-22 16:57:11 +02:00
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
_update: function() {
|
2014-04-22 16:57:11 +02:00
|
|
|
"use strict";
|
|
|
|
var text = "";
|
2013-12-02 20:58:43 +01:00
|
|
|
if(this.options.since !== null) {
|
2013-12-03 21:23:54 +01:00
|
|
|
if(this.options.to === null) {
|
2014-04-22 16:57:11 +02:00
|
|
|
text = "Registrando da " + config.datetimeformat(this.options.since);
|
2013-12-03 21:23:54 +01:00
|
|
|
} else {
|
2014-04-22 16:57:11 +02:00
|
|
|
text = "Registrando da " +
|
2013-12-03 21:23:54 +01:00
|
|
|
config.datetimeformat(this.options.since) +
|
2014-04-22 16:57:11 +02:00
|
|
|
" a " +
|
|
|
|
config.datetimeformat(this.options.to);
|
|
|
|
this.options.editable = false;
|
2013-12-03 21:23:54 +01:00
|
|
|
}
|
2014-04-22 16:57:11 +02:00
|
|
|
}
|
|
|
|
this.element.text(text);
|
|
|
|
if(this.options.editable) {
|
|
|
|
var btn = $('<span/>');
|
|
|
|
btn.addClass('pure-button pure-button-compact countclock-edit-time');
|
|
|
|
btn.css('margin-left', '0.5em');
|
|
|
|
|
|
|
|
btn.append($('<i/>').addClass('fa fa-pencil'));
|
|
|
|
this.element.append(btn);
|
2013-12-02 20:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.widget("ror.ongoingrec", {
|
|
|
|
options: {
|
|
|
|
rec: null,
|
2014-03-02 00:27:40 +01:00
|
|
|
state: 0,
|
|
|
|
filename: null,
|
|
|
|
/*0 = ongoing, 1 = encoding, 2 = ready to download, 9 = errors*/
|
2013-12-02 20:58:43 +01:00
|
|
|
},
|
|
|
|
_create: function() {
|
|
|
|
"use strict";
|
|
|
|
//convert a Rec into a <tr>
|
|
|
|
var widget = this;
|
|
|
|
var rec = this.options.rec;
|
|
|
|
var view = this.element.data('rec', rec).addClass('ongoing-rec').append(
|
2014-05-03 21:58:04 +02:00
|
|
|
$('<td/>').addClass('pure-form').append(
|
|
|
|
$('<input/>').attr('type', 'text').attr('placeholder', 'Nome trasmissione')
|
2014-03-02 00:27:40 +01:00
|
|
|
)
|
|
|
|
).append( $('<td class="ongoingrec-time"/>').countclock()).append(
|
|
|
|
$('<td/>').append($('<a/>')
|
|
|
|
.addClass('pure-button pure-button-large'))
|
|
|
|
);
|
2013-12-02 20:58:43 +01:00
|
|
|
this._update();
|
|
|
|
|
|
|
|
view.on("change", "input", function(evt) {
|
|
|
|
console.log('change', evt);
|
|
|
|
var prevrec = widget.options.rec;
|
|
|
|
prevrec.name = $(evt.target).val();
|
|
|
|
$(evt.target).parents('tr.ongoing-rec').data('rec', prevrec);
|
|
|
|
widget._trigger("change", evt,
|
2014-03-02 00:27:40 +01:00
|
|
|
{rec: rec, widget: widget, changed: {name: rec.name}}
|
|
|
|
);
|
2013-12-02 20:58:43 +01:00
|
|
|
});
|
|
|
|
view.on("click", ".rec-stop", function(evt) {
|
|
|
|
widget._trigger("stop", evt, {rec: rec, widget: widget});
|
|
|
|
});
|
2013-12-27 02:17:12 +01:00
|
|
|
view.on("click", ".rec-failed", function(evt) {
|
|
|
|
$('<div/>').html($('<pre/>').text(widget.options.errormsg))
|
|
|
|
.dialog({modal: true, title: "Dettaglio errori",
|
2014-03-02 00:27:40 +01:00
|
|
|
buttons: {
|
|
|
|
Retry: function() {
|
|
|
|
console.log("retrying");
|
|
|
|
widget._setOption("state", 0);
|
|
|
|
widget._trigger("retry", evt, {rec: rec, widget: widget});
|
|
|
|
$(this).dialog("close");
|
|
|
|
}, Cancel: function() {
|
|
|
|
$(this).dialog("close");
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 02:17:12 +01:00
|
|
|
});
|
|
|
|
});
|
2013-12-02 20:58:43 +01:00
|
|
|
|
|
|
|
return view;
|
|
|
|
},
|
|
|
|
_setOption: function(key, value) {
|
|
|
|
this.options[key] = value;
|
2013-12-03 01:56:54 +01:00
|
|
|
if(key === 'state') {
|
2013-12-27 02:17:12 +01:00
|
|
|
if(value !== 9) {
|
|
|
|
this.options.errormsg = null;
|
|
|
|
}
|
2013-12-03 01:56:54 +01:00
|
|
|
if(value < 2) {
|
|
|
|
this.options.filename = null;
|
|
|
|
}
|
|
|
|
}
|
2013-12-02 20:58:43 +01:00
|
|
|
this._update();
|
|
|
|
},
|
2014-04-22 16:57:11 +02:00
|
|
|
|
|
|
|
_update: function() {
|
|
|
|
var rec = this.options.rec;
|
|
|
|
this.element.find('input').val(rec.name);
|
|
|
|
this.element.find(':ror-countclock').countclock("option", "since",
|
|
|
|
rec.starttime !== null ? config.date_read(rec.starttime) :
|
|
|
|
null).on("countclockchange",
|
|
|
|
function(evt, data) {
|
|
|
|
count_widg = this;
|
|
|
|
console.log(this);
|
|
|
|
console.log(rec.starttime, data.since.getTime() / 1000);
|
|
|
|
rec.starttime = data.since.getTime() / 1000;
|
|
|
|
RecAPI.update(rec.id, rec).done(
|
|
|
|
function() {
|
|
|
|
$(count_widg).countclock('option', 'since', data.since);
|
|
|
|
}).fail(console.error);
|
|
|
|
});
|
|
|
|
|
2013-12-05 01:08:20 +01:00
|
|
|
if(this.options.state > 0) {
|
2014-04-22 16:57:11 +02:00
|
|
|
this.element.find(':ror-countclock').countclock("option", "to",
|
|
|
|
rec.endtime !== null ? new Date(rec.endtime*1000) : null
|
2014-03-02 00:27:40 +01:00
|
|
|
);
|
2013-12-05 01:08:20 +01:00
|
|
|
} else {
|
|
|
|
this.element.find(':ror-countclock').countclock("option", "to", null);
|
|
|
|
}
|
2014-03-02 00:27:40 +01:00
|
|
|
|
2013-12-27 02:17:12 +01:00
|
|
|
this.element.find('a').removeClass(
|
2014-03-02 00:27:40 +01:00
|
|
|
'pure-button-disabled rec-encoding rec-download rec-failed rec-stop');
|
|
|
|
switch(this.options.state) {
|
|
|
|
case 0:
|
|
|
|
this.element.find('a').addClass("rec-stop").html(
|
|
|
|
$('<i/>').addClass('fa fa-stop')).append(' Stop');
|
2013-12-02 20:58:43 +01:00
|
|
|
break;
|
2014-03-02 00:27:40 +01:00
|
|
|
case 1:
|
|
|
|
this.element.find('a')
|
2013-12-02 20:58:43 +01:00
|
|
|
.addClass("pure-button-disabled rec-encoding").html(
|
2014-03-02 00:27:40 +01:00
|
|
|
$('<i/>').addClass('fa fa-clock-o')).append(' Aspetta');
|
2013-12-02 20:58:43 +01:00
|
|
|
break;
|
2014-03-02 00:27:40 +01:00
|
|
|
case 2:
|
|
|
|
this.element.find('a').addClass("rec-download")
|
2013-12-03 01:56:54 +01:00
|
|
|
.prop('href', this.options.filename)
|
|
|
|
.html(
|
2014-03-02 00:27:40 +01:00
|
|
|
$('<i/>').addClass('fa fa-download').css('color', 'green'))
|
|
|
|
.append(' Scarica');
|
2013-12-02 20:58:43 +01:00
|
|
|
break;
|
2014-03-02 00:27:40 +01:00
|
|
|
case 9:
|
|
|
|
this.element.find('a').addClass("rec-failed")
|
|
|
|
.html(
|
|
|
|
$('<i/>').addClass('fa fa-warning')).append(' Errori');
|
2013-12-27 02:17:12 +01:00
|
|
|
break;
|
2014-03-02 00:27:40 +01:00
|
|
|
}
|
2013-12-02 20:58:43 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function add_new_rec() {
|
|
|
|
//progress()
|
2014-03-01 20:43:45 +01:00
|
|
|
return RecAPI.create()
|
2013-12-02 20:58:43 +01:00
|
|
|
.done(function(res) {
|
2014-03-01 20:43:45 +01:00
|
|
|
/*global show_ongoing*/
|
2013-12-02 20:58:43 +01:00
|
|
|
//passa alla seconda schermata
|
|
|
|
$('#rec-inizia').remove();
|
|
|
|
$('#rec-normal').show();
|
|
|
|
show_ongoing([res.rec]);
|
|
|
|
})
|
|
|
|
.fail(function() {
|
2014-03-01 20:43:45 +01:00
|
|
|
/*global alert*/
|
2013-12-02 20:58:43 +01:00
|
|
|
alert("C'e' stato qualche problema nella comunicazione col server");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-27 02:17:12 +01:00
|
|
|
function gen_rec(rec, widget) {
|
|
|
|
"use strict";
|
2014-03-01 20:43:45 +01:00
|
|
|
var gen_xhr = RecAPI.generate(rec);
|
2013-12-27 02:17:12 +01:00
|
|
|
gen_xhr.done(function(res_gen) {
|
|
|
|
widget.option("state", 1);
|
|
|
|
poll_job(res_gen.job_id, function(data) {
|
|
|
|
if(data.job_status !== 'DONE') {
|
|
|
|
console.error("Job failed!", data);
|
|
|
|
widget.option("errormsg", "Generation failed");
|
|
|
|
widget.option("state", 9);
|
|
|
|
} else {
|
|
|
|
widget.option("filename", res_gen.result);
|
|
|
|
widget.option("state", 2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
gen_xhr.fail(function(res_gen) {
|
|
|
|
var error = JSON.parse(res_gen.responseText).message;
|
|
|
|
widget.option("errormsg", error);
|
|
|
|
widget.option("state", 9);
|
|
|
|
});
|
|
|
|
return gen_xhr;
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
function stop_rec(rec, widget) {
|
|
|
|
"use strict";
|
2014-03-01 20:43:45 +01:00
|
|
|
var stop_xhr = RecAPI.stop(rec);
|
2013-12-27 02:17:12 +01:00
|
|
|
stop_xhr.done(function(res_update) {
|
2013-12-03 21:23:54 +01:00
|
|
|
widget.option("rec", res_update.rec);
|
2013-12-27 02:17:12 +01:00
|
|
|
return gen_rec(rec, widget);
|
|
|
|
});
|
|
|
|
stop_xhr.fail(function(res_update) {
|
2014-03-02 00:27:40 +01:00
|
|
|
var error = JSON.parse(res_update.responseText).message;
|
|
|
|
widget.option("errormsg", error);
|
|
|
|
widget.option("state", 9);
|
2013-12-02 20:58:43 +01:00
|
|
|
});
|
2014-03-01 20:43:45 +01:00
|
|
|
return stop_xhr; //RecAPI.stop
|
2013-12-02 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function show_ongoing(ongoing_recs) {
|
|
|
|
return ongoing_recs.map(function(rec) {
|
|
|
|
var viewrec = $('<tr/>').ongoingrec({rec: rec});
|
|
|
|
viewrec.on("ongoingrecstop", function(evt, data) {
|
|
|
|
stop_rec(data.rec, data.widget);
|
2013-12-27 02:17:12 +01:00
|
|
|
}).on("ongoingrecretry", function(evt, data) {
|
|
|
|
//FIXME: bisognerebbe solo generare, senza stoppare
|
|
|
|
gen_rec(data.rec, data.widget);
|
2013-12-02 20:58:43 +01:00
|
|
|
}).on("ongoingrecchange", function(evt, data) {
|
|
|
|
//TODO: aggiorna nome sul server
|
2014-03-01 20:43:45 +01:00
|
|
|
RecAPI.update(data.rec.id, data.rec);
|
2013-12-02 20:58:43 +01:00
|
|
|
});
|
|
|
|
$('#ongoing-recs-table tbody').prepend(viewrec);
|
|
|
|
return viewrec;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
"use strict";
|
2013-12-10 02:35:53 +01:00
|
|
|
/*global getKeys*/
|
2013-12-02 20:58:43 +01:00
|
|
|
//TODO: get-ongoing
|
2014-03-01 20:43:45 +01:00
|
|
|
RecAPI.get_ongoing()
|
2013-12-10 02:35:53 +01:00
|
|
|
.done(function(recs) {
|
|
|
|
$('.add-new-rec').click(add_new_rec);
|
|
|
|
console.log(recs);
|
|
|
|
if(getKeys(recs).length !== 0) {
|
|
|
|
$('#rec-inizia').remove();
|
|
|
|
$('#rec-normal').show();
|
|
|
|
show_ongoing(getKeys(recs).map(function(id) { console.log(id); return recs[id]; }));
|
|
|
|
}
|
|
|
|
});
|
2013-12-02 20:58:43 +01:00
|
|
|
});
|
|
|
|
|
2013-12-10 02:35:53 +01:00
|
|
|
//POLYFILL for Object.keys
|
|
|
|
function getKeys(obj) {
|
|
|
|
var keys = [];
|
|
|
|
var key;
|
|
|
|
for(key in obj) {
|
|
|
|
if(obj.hasOwnProperty(key)) {
|
|
|
|
keys.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
/* vim: set ts=2 sw=2 noet fdm=indent: */
|