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,
|
2013-12-03 21:23:54 +01:00
|
|
|
to: null
|
2013-12-02 20:58:43 +01:00
|
|
|
},
|
|
|
|
_create: function() {
|
|
|
|
this._update();
|
|
|
|
//TODO: aggiungi conto secondi/minuti passati
|
|
|
|
},
|
|
|
|
_setOption: function(key, value) {
|
|
|
|
this.options[key] = value;
|
|
|
|
this._update();
|
|
|
|
},
|
|
|
|
_update: function() {
|
|
|
|
if(this.options.since !== null) {
|
2013-12-03 21:23:54 +01:00
|
|
|
if(this.options.to === null) {
|
|
|
|
this.element.text("Registrando da " +
|
|
|
|
config.datetimeformat(this.options.since)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.element.text("Registrando da " +
|
|
|
|
config.datetimeformat(this.options.since) +
|
|
|
|
" a " +
|
|
|
|
config.datetimeformat(this.options.to)
|
|
|
|
);
|
|
|
|
}
|
2013-12-02 20:58:43 +01:00
|
|
|
} else {
|
|
|
|
this.element.text('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.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(
|
|
|
|
$('<td/>').append(
|
|
|
|
$('<input/>').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();
|
|
|
|
},
|
|
|
|
_update: function() {
|
|
|
|
var rec = this.options.rec;
|
|
|
|
this.element.find('input').val(rec.name);
|
2013-12-03 21:23:54 +01:00
|
|
|
this.element.find(':ror-countclock').countclock("option", "since",
|
2014-04-23 12:41:12 +02:00
|
|
|
rec.starttime !== null ? config.date_read(rec.starttime) : null);
|
2013-12-05 01:08:20 +01:00
|
|
|
if(this.options.state > 0) {
|
|
|
|
this.element.find(':ror-countclock').countclock("option", "to",
|
2014-04-23 12:41:12 +02:00
|
|
|
rec.endtime !== null ? config.date_read(rec.endtime) : 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: */
|