2013-12-02 20:58:43 +01:00
|
|
|
/*global $*/
|
|
|
|
//TODO: move to a separate file(?)
|
|
|
|
var config = {
|
2013-12-03 01:56:54 +01:00
|
|
|
polling_interval: 500,
|
2013-12-02 20:58:43 +01:00
|
|
|
datetimeformat: function(d) {
|
|
|
|
if(Math.abs(new Date() - d) > (3*60*60*1000)) {
|
|
|
|
return d.toLocaleString();
|
|
|
|
}
|
|
|
|
return d.toLocaleTimeString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$.widget("ror.countclock", {
|
|
|
|
options: {
|
|
|
|
since: null,
|
2013-12-03 21:23:54 +01:00
|
|
|
to: null
|
2013-12-02 20:58:43 +01:00
|
|
|
},
|
|
|
|
_create: function() {
|
|
|
|
console.log("create");
|
|
|
|
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,
|
|
|
|
state: 0,
|
2013-12-03 01:56:54 +01:00
|
|
|
filename: null,
|
2013-12-02 20:58:43 +01:00
|
|
|
/*0 = ongoing, 1 = encoding, 2 = ready to download*/
|
|
|
|
},
|
|
|
|
_create: function() {
|
|
|
|
"use strict";
|
|
|
|
//convert a Rec into a <tr>
|
|
|
|
console.log(this.element);
|
|
|
|
var widget = this;
|
|
|
|
var rec = this.options.rec;
|
|
|
|
console.log("rec", rec);
|
|
|
|
var view = this.element.data('rec', rec).addClass('ongoing-rec').append(
|
|
|
|
$('<td/>').append(
|
|
|
|
$('<input/>').attr('placeholder', 'Nome trasmissione')
|
|
|
|
)
|
|
|
|
).append( $('<td class="ongoingrec-time"/>').countclock()).append(
|
2013-12-03 01:56:54 +01:00
|
|
|
$('<td/>').append($('<a/>')
|
2013-12-02 20:58:43 +01:00
|
|
|
.addClass('pure-button pure-button-large'))
|
|
|
|
);
|
|
|
|
|
|
|
|
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,
|
|
|
|
{rec: rec, widget: widget, changed: {name: rec.name}}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
view.on("click", ".rec-stop", function(evt) {
|
|
|
|
widget._trigger("stop", evt, {rec: rec, widget: widget});
|
|
|
|
});
|
|
|
|
|
|
|
|
return view;
|
|
|
|
},
|
|
|
|
_setOption: function(key, value) {
|
|
|
|
this.options[key] = value;
|
2013-12-03 01:56:54 +01:00
|
|
|
if(key === 'state') {
|
|
|
|
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",
|
2013-12-02 20:58:43 +01:00
|
|
|
rec.starttime !== null ? new Date(rec.starttime*1000) : null
|
|
|
|
);
|
2013-12-03 21:23:54 +01:00
|
|
|
this.element.find(':ror-countclock').countclock("option", "to",
|
|
|
|
rec.endtime !== null ? new Date(rec.endtime*1000) : null
|
|
|
|
);
|
2013-12-02 20:58:43 +01:00
|
|
|
|
|
|
|
switch(this.options.state) {
|
|
|
|
case 0:
|
2013-12-03 01:56:54 +01:00
|
|
|
this.element.find('a').removeClass('pure-button-disabled rec-encoding rec-download')
|
2013-12-02 20:58:43 +01:00
|
|
|
.addClass("rec-stop").html(
|
|
|
|
$('<i/>').addClass('fa fa-stop')).append(' Stop');
|
|
|
|
break;
|
|
|
|
case 1:
|
2013-12-03 01:56:54 +01:00
|
|
|
this.element.find('a').removeClass('rec-stop rec-download')
|
2013-12-02 20:58:43 +01:00
|
|
|
.addClass("pure-button-disabled rec-encoding").html(
|
|
|
|
$('<i/>').addClass('fa fa-clock-o')).append(' Aspetta');
|
|
|
|
break;
|
|
|
|
case 2:
|
2013-12-03 01:56:54 +01:00
|
|
|
this.element.find('a').removeClass('pure-button-disabled rec-stop rec-encoding')
|
|
|
|
.addClass("rec-download")
|
|
|
|
.prop('href', this.options.filename)
|
|
|
|
.html(
|
|
|
|
$('<i/>').addClass('fa fa-download').css('color',
|
|
|
|
'green')).append(' Scarica');
|
2013-12-02 20:58:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-12-03 01:56:54 +01:00
|
|
|
function poll_job(job_id, callback) {
|
|
|
|
$.getJSON('/api/jobs/' + job_id)
|
|
|
|
.done(function(data) {
|
|
|
|
if(data.job_status !== 'WIP') {
|
|
|
|
console.log("polling completed for job[" + job_id + "]", data);
|
|
|
|
callback(data);
|
|
|
|
} else {
|
|
|
|
setTimeout(function() { poll_job(job_id, callback); },
|
|
|
|
config.polling_interval);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:58:43 +01:00
|
|
|
function add_new_rec() {
|
|
|
|
//progress()
|
|
|
|
return $.ajax('/api/create', {
|
|
|
|
method: 'POST',
|
|
|
|
dataType: 'json'
|
|
|
|
})
|
|
|
|
.done(function(res) {
|
|
|
|
//passa alla seconda schermata
|
|
|
|
$('#rec-inizia').remove();
|
|
|
|
$('#rec-normal').show();
|
|
|
|
show_ongoing([res.rec]);
|
|
|
|
})
|
|
|
|
.fail(function() {
|
|
|
|
alert("C'e' stato qualche problema nella comunicazione col server");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop_rec(rec, widget) {
|
|
|
|
"use strict";
|
|
|
|
var data = {
|
|
|
|
recid: rec.recid,
|
|
|
|
starttime: rec.starttime
|
|
|
|
};
|
|
|
|
var xhr = $.ajax('/api/update', {
|
|
|
|
method: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
data: data
|
|
|
|
});
|
2013-12-03 21:07:17 +01:00
|
|
|
xhr.done(function(res_update) {
|
|
|
|
if(res_update.status !== true) {
|
|
|
|
console.error(res_update.status);
|
2013-12-02 20:58:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-12-03 21:23:54 +01:00
|
|
|
widget.option("rec", res_update.rec);
|
2013-12-03 21:07:17 +01:00
|
|
|
$.ajax('/api/generate', {
|
|
|
|
method: 'POST',
|
|
|
|
data: { 'recid': rec.recid }
|
|
|
|
}).done(function(res_gen) {
|
|
|
|
//TODO: start polling on res.job_id
|
|
|
|
widget.option("state", 1);
|
|
|
|
poll_job(res_gen.job_id, function(data) {
|
|
|
|
if(data.job_status !== 'DONE') {
|
|
|
|
console.error("Job failed!", data);
|
|
|
|
} else {
|
|
|
|
widget.option("filename", res_gen.result);
|
|
|
|
widget.option("state", 2);
|
|
|
|
}
|
|
|
|
});
|
2013-12-03 01:56:54 +01:00
|
|
|
});
|
2013-12-03 21:07:17 +01:00
|
|
|
return xhr;
|
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);
|
|
|
|
}).on("ongoingrecchange", function(evt, data) {
|
|
|
|
//TODO: aggiorna nome sul server
|
|
|
|
$.ajax('/api/update', {
|
|
|
|
method: 'POST',
|
|
|
|
data: data.rec,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$('#ongoing-recs-table tbody').prepend(viewrec);
|
|
|
|
return viewrec;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
"use strict";
|
|
|
|
//TODO: get-ongoing
|
|
|
|
$('.add-new-rec').click(add_new_rec);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet fdm=indent: */
|