old.html (refs #13) + refactoring

This commit is contained in:
boyska 2014-03-01 20:43:45 +01:00
parent 2353bf7eb4
commit fbf94eceba
7 changed files with 180 additions and 110 deletions

View file

@ -13,6 +13,7 @@
<script src="/static/js/jquery-1.9.1.min.js"></script>
<script src="/static/js/jquery-ui.min.js"></script>
<script src="/static/js/jquery.ui.datepicker-it.min.js"></script>
<script src="/static/js/rec.js"></script>
<script src="/static/js/new.js"></script>
</head>

View file

@ -12,6 +12,9 @@
<script src="/static/js/jquery-1.9.1.min.js"></script>
<script src="/static/js/jquery-ui.min.js"></script>
<script src="/static/js/jquery.ui.datepicker-it.min.js"></script>
<script src="/static/js/jquery.form.min.js"></script>
<script src="/static/js/rec.js"></script>
<script src="/static/js/old.js"></script>
</head>
<body>
@ -20,7 +23,7 @@
<div id="rec-normal" class="pure-g-r">
<div class="pure-u-1-8"></div>
<div class="pure-u-3-4">
<form class="pure-form">
<form id="form" class="pure-form">
<label for="name" required placeholder="Corrispondenza dalla Luna">Nome</label>
<input type="text" id="name" name="name" size="40" required placeholder="Corrispondenza dalla Luna">

View file

@ -81,15 +81,16 @@ class RecAPI(Bottle):
def create(self):
req = dict(request.POST.allitems())
ret = {}
logger.debug("Server:: Create request %s " % req)
logger.debug("Create request %s " % req)
starttime = datetime.now()
name = ""
endtime = datetime.now()
now = datetime.now()
start = date_read(req['starttime']) if 'starttime' in req else now
name = req['name'] if 'name' in req else ""
end = date_read(req['endtime']) if 'endtime' in req else now
rec = Rec(name=name,
starttime=starttime,
endtime=endtime)
starttime=start,
endtime=end)
ret = self.db.add(rec)
return self.rec_msg("Nuova registrazione creata! (id:%d)" % ret.id,
@ -151,9 +152,9 @@ class RecAPI(Bottle):
(rec.endtime - rec.starttime).total_seconds()
}
rec.filename = get_config()['AUDIO_OUTPUT_FORMAT'] % {
'time': rec.starttime.strftime('%y%m%d_%H%M'),
'name': filter(lambda c: c.isalpha(), rec.name)
}
'time': rec.starttime.strftime('%y%m%d_%H%M'),
'name': filter(lambda c: c.isalpha(), rec.name)
}
self.db.update(rec.id, rec.serialize())
job_id = get_process_queue().submit(
create_mp3,
@ -262,8 +263,8 @@ class RecServer:
self._app.route('/new.html',
callback=partial(static_file, 'new.html',
root='pages/'))
self._app.route('/tempo.html',
callback=partial(static_file, 'tempo.html',
self._app.route('/old.html',
callback=partial(static_file, 'old.html',
root='pages/'))

7
server/static/js/jquery.form.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,40 +1,6 @@
/*global $*/
/*global $, config, RecAPI*/
//TODO: move to a separate file(?)
var config = {
polling_interval: 500,
datetimeformat: function(d) {
if(Math.abs(new Date() - d) > (3*60*60*1000)) {
return d.toLocaleString();
}
return d.toLocaleTimeString();
}
};
var API = {
create: function() {
return $.ajax('/api/create', {
method: 'POST',
dataType: 'json'
});
},
stop: function(rec) {
return $.post('/api/update/' + rec.id, {
starttime: rec.starttime
});
},
update: function(id, data) {
return $.post('/api/update/' + id, data);
},
generate: function(rec) {
return $.post('/api/generate', {
id: rec.id
});
},
get_ongoing: function() {
return $.getJSON('/api/get/ongoing');
}
};
$.widget("ror.countclock", {
options: {
errormsg: null,
@ -88,7 +54,6 @@ $.widget("ror.ongoingrec", {
$('<td/>').append($('<a/>')
.addClass('pure-button pure-button-large'))
);
this._update();
view.on("change", "input", function(evt) {
@ -148,7 +113,7 @@ $.widget("ror.ongoingrec", {
}
this.element.find('a').removeClass(
'pure-button-disabled rec-encoding rec-download rec-failed rec-stop')
'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(
@ -189,21 +154,23 @@ function poll_job(job_id, callback) {
function add_new_rec() {
//progress()
return API.create()
return RecAPI.create()
.done(function(res) {
/*global show_ongoing*/
//passa alla seconda schermata
$('#rec-inizia').remove();
$('#rec-normal').show();
show_ongoing([res.rec]);
})
.fail(function() {
/*global alert*/
alert("C'e' stato qualche problema nella comunicazione col server");
});
}
function gen_rec(rec, widget) {
"use strict";
var gen_xhr = API.generate(rec);
var gen_xhr = RecAPI.generate(rec);
gen_xhr.done(function(res_gen) {
widget.option("state", 1);
poll_job(res_gen.job_id, function(data) {
@ -227,7 +194,7 @@ function gen_rec(rec, widget) {
function stop_rec(rec, widget) {
"use strict";
var stop_xhr = API.stop(rec);
var stop_xhr = RecAPI.stop(rec);
stop_xhr.done(function(res_update) {
widget.option("rec", res_update.rec);
return gen_rec(rec, widget);
@ -237,7 +204,7 @@ function stop_rec(rec, widget) {
widget.option("errormsg", error);
widget.option("state", 9);
});
return stop_xhr; //API.stop
return stop_xhr; //RecAPI.stop
}
function show_ongoing(ongoing_recs) {
@ -250,7 +217,7 @@ function show_ongoing(ongoing_recs) {
gen_rec(data.rec, data.widget);
}).on("ongoingrecchange", function(evt, data) {
//TODO: aggiorna nome sul server
API.update(data.rec.id, data.rec);
RecAPI.update(data.rec.id, data.rec);
});
$('#ongoing-recs-table tbody').prepend(viewrec);
return viewrec;
@ -261,7 +228,7 @@ $(function() {
"use strict";
/*global getKeys*/
//TODO: get-ongoing
API.get_ongoing()
RecAPI.get_ongoing()
.done(function(recs) {
$('.add-new-rec').click(add_new_rec);
console.log(recs);

View file

@ -1,59 +1,101 @@
/* BEGIN Validation */
function form_check() {
"use strict";
var errs = [];
function err(msg, element) {
errs.unshift({ msg: msg, el: element});
}
if($('#name').val() == '') {
errs.unshift("Nome mancante", $('#name'));
}
if(parseInt($('#to-hour').val(), 10) - parseInt($('#from-hour').val(), 10) > 5) {
errs.unshift("Too long");
}
if(parseInt($('#to-hour').val(), 10) - parseInt($('#from-hour').val(), 10) < 0) {
//TODO: better date handling
errs.unshift("Inverted from/to ?");
}
return errs;
}
/*global $*/
function update_form_check(errors) {
"use strict";
/* This function reads results and changes "things" consequently */
if(errors.length > 0) {
console.log(errors);
$('#download').addClass("pure-button-disabled");
} else { /* everything fine */
$('#download').removeClass("pure-button-disabled");
}
}
/* END validation */
var form = {
MAX_MINS: 5*60, // 5 hours
get_values: function() {
var name = $('#name').val();
var start = $('#from-date').datepicker('getDate');
if(start !== null) {
start.setHours($('#from-hour').val());
start.setMinutes($('#from-min').val());
}
var end = $('#to-date').datepicker('getDate');
if(end !== null) {
end.setHours($('#to-hour').val());
end.setMinutes($('#to-min').val());
}
return { name: name, start: start, end: end };
},
check: function() {
"use strict";
var errs = [];
function err(msg, element) {
errs.unshift({ msg: msg, el: element});
}
var v = form.get_values();
if(v.val === '') {
err("Nome mancante", $('#name'));
}
if(v.start === null) {
err("Start unspecified");
}
if(v.end === null) {
err("End unspecified");
}
if(v.end <= v.start) {
err("Inverted from/to ?");
}
if( (v.end - v.start) / (1000*60) > form.MAX_MINS) {
err("Too long");
}
return errs;
}
};
$(function() {
"use strict";
$( "#from-date" ).datepicker({
defaultDate: "+0d",
changeMonth: true,
numberOfMonths: 1,
onClose: function( selectedDate ) {
if($('#to-date').val() == '') {
$('#to-date').datepicker("setDate", selectedDate);
}
$("#to-date").datepicker("option", "minDate", selectedDate);
}
});
$( "#to-date" ).datepicker({
defaultDate: "+0d",
changeMonth: true,
numberOfMonths: 1,
onClose: function( selectedDate ) {
$("#from-date").datepicker("option", "maxDate", selectedDate);
}
});
$('#to-date, #from-date').datepicker($.datepicker.regional['it']);
"use strict";
$( "#from-date" ).datepicker({
defaultDate: "+0d",
changeMonth: true,
numberOfMonths: 1,
maxDate: new Date(),
onClose: function( selectedDate ) {
if($('#to-date').val() === '') {
$('#to-date').datepicker("setDate", selectedDate);
}
$("#to-date").datepicker("option", "minDate", selectedDate);
}
});
$( "#to-date" ).datepicker({
defaultDate: "+0d",
changeMonth: true,
numberOfMonths: 1,
maxDate: new Date(),
onClose: function( selectedDate ) {
$("#from-date").datepicker("option", "maxDate", selectedDate);
}
});
$('#to-date, #from-date').datepicker($.datepicker.regional.it);
$('form input').change(function() {
update_form_check(form_check());
})
$('#form').ajaxForm({
beforeSubmit: function() {
console.log("check", form.check());
if(form.check().length > 0) {
console.log("Form not valid, aborting");
return false;
}
return true;
},
success: function() {
/*global RecAPI*/
var v = form.get_values();
RecAPI.fullcreate(v.name, v.start, v.end)
.done(function(res) {
console.log("ok, created");
RecAPI.generate(res.rec)
.done(function(res) {
console.log("ok, generated", res);
})
.fail(function() {
console.error("Oh shit, generate failed", res.rec);
});
})
.fail(function() {
console.error("Oh shit, fullcreate failed");
});
return false;
}
});
});
/* vim: set ts=2 sw=2 noet fdm=indent: */

49
server/static/js/rec.js Normal file
View file

@ -0,0 +1,49 @@
/*global $*/
var config = {
polling_interval: 500,
date_write: function(d) {
return Math.floor(d.getTime() / 1000);
},
datetimeformat: function(d) {
if(Math.abs(new Date() - d) > (3*60*60*1000)) {
return d.toLocaleString();
}
return d.toLocaleTimeString();
}
};
var RecAPI = {
create: function() {
return $.ajax('/api/create', {
method: 'POST',
dataType: 'json'
});
},
stop: function(rec) {
return $.post('/api/update/' + rec.id, {
starttime: rec.starttime
});
},
update: function(id, data) {
return $.post('/api/update/' + id, data);
},
fullcreate: function(name, start, end) {
return $.ajax(
'/api/create', {
method: 'POST',
dataType: 'json',
data: { name: name,
starttime: config.date_write(start),
endtime: config.date_write(end)
}
});
},
generate: function(rec) {
return $.post('/api/generate', {
id: rec.id
});
},
get_ongoing: function() {
return $.getJSON('/api/get/ongoing');
}
};