Commit 545596a8 authored by Oliver Feldt's avatar Oliver Feldt
Browse files

Switched to handlebar templates; Minor bug fixes; Minor usability improvements

parent f4b77eb2
Handlebars.registerHelper('checkout_state', function(checkout) {
return checkout.checked_out_at === undefined ? "info" : "success"
});
Handlebars.registerHelper('checked_in_state', function(checkout) {
return checkout.checked_in_at !== null ? checkout.checked_in_at : "Still Out"
});
$.delete = function(url, data, callback, type){
if ( $.isFunction(data) ){
type = type || callback,
......@@ -67,10 +76,20 @@ $("#search_form").on("submit", function(e) {
}
$("#radio_form input[name='radio_id']").val(radio.name);
$("#radios tr[data-id='"+ radio.id +"']").data("last_scanned_at", new Date());
if (radio.angel === undefined) {
$("#radio_form button[name='return']").addClass("disabled");
} else {
$("#radio_form button[name='return']").removeClass("disabled");
}
var switch_button_text = radio.angel === undefined ? "Set Angel" : "Switch Angel";
$("#radio_form button[name='switch']").html(switch_button_text);
$("#radio_header").html(radio.name);
$("#radio").modal("show");
}
}
);
$("#radio_id").val("");
e.preventDefault();
});
......@@ -92,6 +111,7 @@ $("#radio button[name='switch']").on("click", function(e) {
radio_row.children(".previous_angel").html(current_angel);
}
radio_row.children(".current_angel").html(angel);
radio_row.data("angel", angel);
radio_row.data("last_scanned_at", new Date());
radio_row.data("checked_out_at", new Date());
set_time_interval();
......@@ -133,10 +153,23 @@ $("#new_radio").on("submit", function(e) {
$.post(
"/radios",
$("#new_radio_form").serialize(),
function (data) {
var radio = data;
function (response) {
var data = {
radio: response.radio,
previous_checkout: { },
current_checkout: {
angel: response.angel,
last_scanned_at: new Date(),
checked_out_at: new Date()
}
}
if (radio.id !== null) {
$("#new_radio").modal("hide");
var source = $("#radio-template").html();
var template = Handlebars.compile(source);
$("#radios").append(template(data));
set_time_interval();
}
},
"json"
......@@ -165,14 +198,17 @@ $("#radios").on("click", function(e) {
$.getJSON(
"/checkouts",
{ radio_id: radio.name },
function(data) {
var checkouts = data;
function(response) {
var checkouts = response;
var source = $("#checkout-template").html();
var template = Handlebars.compile(source);
$("#checkouts tbody").empty();
for (var index = 0; index < checkouts.length; index++) {
var angel = checkouts[index].angel;
var checked_in_at = checkouts[index].checked_in_at;
if (checked_in_at === null) { checked_in_at = "Still Out" }
$("#checkouts tbody").append("<tr><td>" + angel + "</td><td>" +checked_in_at + "</td></tr>")
var context = {
angel: checkouts[index].angel,
checkout: checkouts[index]
};
$("#checkouts tbody").append(template(context));
}
}
)
......
This diff is collapsed.
......@@ -10,6 +10,26 @@ html lang="en"
title CCC Radio Operation Center - Radio Checkout
== css :application, media: 'screen'
script id="radio-template" type="text/x-handlebars-template"
tr data-id="{{radio.id}}" data-name="{{radio.name}}" data-last_scanned_at="{{radio.last_scanned_at}}" data-angel="{{current_checkout.angel}}" data-checked_out_at="{{current_checkout.checked_out_at}}" class="{{checkout_state current_checkout}}"
td
|{{radio.name}}
td.previous_angel
|{{previous_checkout.angel}}
td.current_angel
|{{current_checkout.angel}}
td.last_scanned_at
|{{radio.last_scanned_at}}
td.checked_out_at
|{{current_checkout.checked_out_at}}
script id="checkout-template" type="text/x-handlebars-template"
tr
td
|{{angel}}
td
|{{checked_in_state checkout}}
body
#radio.modal.fade tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
.modal-dialog
......@@ -126,9 +146,10 @@ html lang="en"
td= radio.name
td.previous_angel= previous_checkout.angel if previous_checkout
td.current_angel= current_checkout.angel if current_checkout
td.last_scanned_at= radio.last_scanned_at
td.checked_out_at= current_checkout.checked_out_at if current_checkout
td.last_scanned_at
td.checked_out_at
script src="javascripts/jquery.min.js"
script src="javascripts/moment.min.js"
script src="javascripts/handlebars-v2.0.0.js"
script src="javascripts/bootstrap.js"
== js :application
......@@ -32,104 +32,104 @@ module CCC
css_compression :sass
end
helpers do
def radio_id_check
unless params[:radio_id] =~ /^ROC\d{1,8}$/
halt 400, json(error: "Unknown ID format")
end
end
end
not_found { :fourofour }
get "/" do
@radios = Radio.all
slim :index
end
get "/radios" do
if params[:radio_id] =~ /^ROC\d{1,6}$/
now = DateTime.now
@radio = Radio.first_or_new(name: params[:radio_id])
unless @radio.new?
@radio.last_scanned_at = now
@radio.save
end
radio_id_check
json radio: @radio, checkouts: @radio.checkouts.all
else
halt 400, json(error: "Unknown ID format")
now = DateTime.now
@radio = Radio.first_or_new(name: params[:radio_id])
unless @radio.new?
@radio.last_scanned_at = now
@radio.save
end
json radio: @radio, checkouts: @radio.checkouts.all
end
get "/checkouts" do
if params[:radio_id] =~ /^ROC\d{1,6}$/
@radio = Radio.first_or_new(name: params[:radio_id])
json @radio.checkouts.all(limit: 10, order: [ :checked_out_at.desc])
else
halt 400, json(error: "Unknown ID format")
end
radio_id_check
@radio = Radio.first_or_new(name: params[:radio_id])
json @radio.checkouts.all(limit: 10, order: [ :checked_out_at.desc])
end
post "/checkouts" do
if params[:radio_id] =~ /^ROC\d{1,6}$/
now = DateTime.now
@radio = Radio.first(name: params[:radio_id])
radio_id_check
if current_checkout = @radio.checkouts.last
current_checkout.checked_in_at = now
current_checkout.save
end
now = DateTime.now
@radio = Radio.first(name: params[:radio_id])
unless params[:angel].empty?
@radio.checkouts.create(
angel: params[:angel],
checked_out_at: now
)
end
@radio.last_scanned_at = now
@radio.save
if current_checkout = @radio.checkouts.last
current_checkout.checked_in_at = now
current_checkout.save
end
json(radio: @radio, angel: params[:angel])
else
halt 400, json(error: "Unknown ID format")
unless params[:angel].empty?
@radio.checkouts.create(
angel: params[:angel],
checked_out_at: now
)
end
@radio.last_scanned_at = now
@radio.save
json(radio: @radio, angel: params[:angel])
end
delete "/checkouts" do
if params[:radio_id] =~ /^ROC\d{1,6}$/
now = DateTime.now
@radio = Radio.first(name: params[:radio_id])
radio_id_check
if current_checkout = @radio.checkouts.last
current_checkout.checked_in_at = now
current_checkout.save
end
@radio.last_scanned_at = now
@radio.save
now = DateTime.now
@radio = Radio.first(name: params[:radio_id])
json @radio
else
halt 400, json(error: "Unknown ID format")
if current_checkout = @radio.checkouts.last
current_checkout.checked_in_at = now
current_checkout.save
end
@radio.last_scanned_at = now
@radio.save
json @radio
end
post "/radios" do
if params[:radio_id] =~ /^ROC\d{1,6}$/
now = DateTime.now
@radio = Radio.first_or_create(
name: params[:radio_id],
last_scanned_at: now
)
radio_id_check
if current_checkout = @radio.checkouts.last
current_checkout.checked_in_at = now
current_checkout.save
end
now = DateTime.now
@radio = Radio.first_or_create(
name: params[:radio_id],
last_scanned_at: now
)
unless params[:angel].empty?
@radio.checkouts.create(
angel: params[:angel],
checked_out_at: now
)
end
if current_checkout = @radio.checkouts.last
current_checkout.checked_in_at = now
current_checkout.save
end
json @radio
else
halt 400, json(error: "Unknown ID format")
unless params[:angel].empty?
@radio.checkouts.create(
angel: params[:angel],
checked_out_at: now
)
end
json(radio: @radio, angel: params[:angel])
end
end
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment