Commit 1592d6eb by Antti Jaakkola Committed by Tuomas Riihimäki

Place reservation using ajax and prime face remote command.

Conflicts:
	code/moya-web/WebContent/place/map.xhtml
	code/moya-web/src/main/java/fi/codecrew/moya/rest/placemap/v1/PlacemapRestViewV1.java
	code/moya-web/src/main/java/fi/codecrew/moya/web/cdiview/map/AjaxMapView.java
1 parent 06e56f7e
......@@ -552,7 +552,7 @@ public class PlaceBean implements PlaceBeanLocal {
}
@Override
@RolesAllowed(MapPermission.S_MANAGE_MAPS)
@RolesAllowed(MapPermission.S_VIEW)
public Place find(int placeId) {
return placeFacade.find(placeId);
}
......
......@@ -19,12 +19,10 @@
<ui:param name="thispage" value="page.place.placemap" />
<ui:define name="content">
<button
onclick="alert('pre');placeClicker([{name:'placeId', value:1}]);alert('post');">button</button>
<h:form>
<p:remoteCommand name="placeClicker" update=":fbdiag"
action="#{ajaxMapView.placeClicked()}" />
<!-- update=":fbdiag" -->
<p:remoteCommand name="placeClicker"
action="#{ajaxMapView.placeClicked()}" onsuccess="return px.update_place()" />
</h:form>
<p:dialog visible="#{!empty ajaxMapView.place}" id="fbdiag">
......@@ -47,18 +45,11 @@
moyaurl : "#{request.contextPath}",
map_id : #{ajaxMapView.map.id},
onclick : function(d) {
alert(d);
placeClicker([{name: 'placeId', value: d}])
return true;
return placeClicker([{name: 'placeId', value: d.id}]);
}
});
// document.getElementById("editbutton").addEventListener("click",
// function() {
// px.enable_edit();
// });
//px.enable_edit();
</script>
</ui:define>
</ui:composition>
</h:body>
</html>
\ No newline at end of file
</html>
......@@ -26,13 +26,19 @@ function placemap(opts)
throw "onclick is mandatory argument";
}
if (opts.editclick === undefined )
{
opts.editclick = function(x, y) {return false;};
}
// px is returned object
var px = {
element: d3.select(opts.element),
moyaurl: opts.moyaurl,
map_id: opts.map_id,
onclick: opts.onclick
onclick: opts.onclick,
clicked_place: undefined
};
......@@ -198,19 +204,25 @@ function placemap(opts)
data = element.data()[0];
if (px.onclick(data.id) != true) return;
px.clicked_place = data.id;
px.onclick(data);
px.update_place(data.id);
//if ( != true) return;
if (data.state == "F")
/*if (data.state == "F")
{
element.transition().duration(100).style("fill", px.place_colors["P"]);
data.state = "P";
element.transition().duration(100).style("fill", px.place_colors["T"]);
data.state = "T";
}
else if (data.state == "P")
else if (data.state == "T")
{
element.transition()
.duration(100).style("fill", px.place_colors["F"]);
data.state = "F";
}
}*/
}
function place_color(d, i)
......@@ -231,12 +243,22 @@ function placemap(opts)
.attr("y", function(d, i) { return d.y; })
.attr("width", function(d, i) { return d.w;})
.attr("height", function(d, i) { return d.h; })
.attr("id", function(d) {return d.id })
.attr("id", function(d) {return d.id; })
.style("fill", place_color)
.style('stroke', '#101010')
.style('stroke-width', '1');
places.append("title", function(d, i) { return d.name; });
var old_places = map_layer.selectAll("rect")
.data(data, function(d) { return d.id; })
.filter(function(d,i){ return d.state != "D"; })
.attr("x", function(d, i) { return d.x; })
.attr("y", function(d, i) { return d.y; })
.attr("width", function(d, i) { return d.w;})
.attr("height", function(d, i) { return d.h; })
.attr("id", function(d) {return d.id; })
.style("fill", place_color);
if (px.edit_enabled == false)
{
places
......@@ -268,12 +290,12 @@ function placemap(opts)
.attr("width", 10)
.attr("height", 10)
.attr("fill", "#111");
tip_add_places.show(pos.x, pos.y);
px.editclick(pos.x, pos.y);
}
else {
edit_layer.selectAll("circle").remove();
clicked = false;
tip_add_places.hide(d, i);
//tip_add_places.hide(d, i);
}
}
......@@ -316,10 +338,20 @@ function placemap(opts)
});
};
px.update_place = function (place_id)
{
if (place_id === undefined) place_id = px.clicked_place;
if (place_id !== undefined ) {
d3.json(px.moyaurl + "/rest/placemap/v1/place/" + place_id, function (data) {
draw_places(data.places);
});
}
};
px.init = function() {
d3.json(px.moyaurl + "/rest/placemap/v1/" + px.map_id + "/", function(data){
px.element.attr("id", data.map.id).attr("name", data.map.name);
px.update()
px.update();
});
};
......@@ -329,115 +361,7 @@ function placemap(opts)
px.element.call(tip_updown);
// Add tooltip
// TODO: Make this less ugly!
function create_tooltip()
{
var mytip = {};
mytip.node = d3.select(document.createElement('div'));
mytip.offset = [20, 10]
mytip.node.attr("class", 'place-edit-tooltip');
mytip.node.style({
position: 'absolute',
top: 0,
opacity: 0,
'pointer-events': 'none',
'box-sizing': 'border-box'
});
document.body.appendChild(mytip.node.node())
mytip.node.html('<span id="place_close_popup">X</span>'+
'<form><fieldset><ol>'+
'<li><label for="place_add_count">Count:</label><input name="count" id="place_add_count" value=1 class="numeric", min=1, max=999999><span id="place_add_count_error"></span></li>'+
'<li><label for="place_add_width">Width:</label><input name="width" id="place_add_width" value=10 class="numeric", min=1, max=999999> px <span id="place_add_width_error"></span></li>'+
'<li><label for="place_add_height">Height:</label><input name="height" id="place_add_height" value=10 class="numeric", min=1, max=999999> px <span id="place_add_height_error"></span></li>'+
'<li><label for="place_add_direction">Direction:</label>'+
'<select name="place_add_direction" id="place_add_direction"><option value="horizontal">Horizontal &rarr;</option><option value="vertical">Vertical &darr;</option></select></li>'+
'<li><input type="button" name="place_add_rows" id="place_add_rows" value="Add places"></li>'+
'</fieldset></form>');
function number_validator(f, ef) {
return function() {
var field = f;
var errorfield = ef;
var derrorfield = d3.select(ef);
var dfield = d3.select(f);
var min = parseInt(field.min);
var max = parseInt(field.max);
var num = -1;
try {
num = parseInt(field.value)
}
catch(err) {
console.log(err);
}
if (num < min)
{
dfield.style({"border": "2px solid #e22"});
derrorfield.html(" " + field.name + " must be &gt; " + min);
}
else if (num > max)
{
dfield.style({"border": "2px solid #e22"});
derrorfield.html(" " + field.name + " must be &lt; " + max);
}
else if (isNaN(num))
{
dfield.style({"border": "2px solid #e22"});
derrorfield.html(" " + field.name + " must be number");
}
else {
dfield.style({"border": "none"});
derrorfield.html("");
}
}
}
document.getElementById("place_add_count").addEventListener("change",
number_validator(document.getElementById("place_add_count"),
document.getElementById("place_add_count_error")
));
document.getElementById("place_add_width").addEventListener("change",
number_validator(document.getElementById("place_add_width"),
document.getElementById("place_add_width_error")
));
document.getElementById("place_add_height").addEventListener("change",
number_validator(document.getElementById("place_add_height"),
document.getElementById("place_add_height_error")
));
document.getElementById("place_close_popup").addEventListener("click", function(){
edit_layer.selectAll("circle").remove();
clicked = false;
mytip.hide();
});
mytip.show = function (x, y)
{
mytip.node.style ({
top: y + mytip.offset[1] + "px",
left: x + mytip.offset[0] + "px",
opacity: 1,
'pointer-events': 'all'
});
};
mytip.hide = function ()
{
mytip.node.style ({
opacity: 0,
'pointer-events': 'none'
});
};
return mytip;
}
var tip_add_places = create_tooltip();
function add_places(){
px.add_places = function (width, height, count, direction){
var width = parseInt(document.getElementById("place_add_width").value);
var height = parseInt(document.getElementById("place_add_height").value);
var count = parseInt(document.getElementById("place_add_count").value);
......@@ -445,12 +369,8 @@ function placemap(opts)
draw_places(gen_row(clickpos.x,clickpos.y, width, height, direction, count));
edit_layer.selectAll("circle").remove();
clicked = false;
tip_add_places.hide();
clear_guides();
}
document.getElementById("place_add_rows").addEventListener("click", add_places);
};
return px;
}
......
......@@ -6,6 +6,7 @@ import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
......@@ -25,6 +26,7 @@ import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.rest.pojo.placemap.PlacemapMapRootPojo;
import fi.codecrew.moya.rest.pojo.placemap.SimplePlacePojo;
import fi.codecrew.moya.rest.pojo.placemap.SimplePlacelistRoot;
import fi.codecrew.moya.web.cdiview.user.UserView;
@RequestScoped
@Path("/placemap/v1")
......@@ -48,7 +50,7 @@ public class PlacemapRestViewV1 {
@POST
@Path("/place/{id}/reserve")
public void reservePlace() {
logger.warn("Reserving not yet implemented");
logger .warn("Reserving not yet implemented");
}
@GET
......@@ -81,4 +83,31 @@ public class PlacemapRestViewV1 {
return SimplePlacelistRoot.wrap(map.getPlaces(), null);
}
@GET
@Path("/place/{place}")
public SimplePlacelistRoot getPlace(@PathParam("place") Integer placeId)
{
List <Place> thisplace = new ArrayList<Place>();
thisplace.add(placebean.find(placeId));
return SimplePlacelistRoot.wrap(thisplace, eventuser.getSelectedUser());
}
@POST
@Path("/place/{place}")
public SimplePlacelistRoot setPlace(@PathParam("place") Integer placeId)
{
Place p = placebean.find(placeId);
if (p.isReservedFor(eventuser.getSelectedUser()))
{
placebean.releasePlace(p);
}
else if (p.isBuyable() && !p.isTaken())
{
placebean.reservePlace(p, eventuser.getSelectedUser());
}
List <Place> thisplace = new ArrayList<Place>();
thisplace.add(p);
return SimplePlacelistRoot.wrap(thisplace, eventuser.getSelectedUser());
}
}
......@@ -13,6 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.PlaceBeanLocal;
import fi.codecrew.moya.enums.apps.MapPermission;
import fi.codecrew.moya.model.EventMap;
......@@ -36,7 +37,9 @@ public class AjaxMapView extends GenericCDIView {
@EJB
private transient EventBeanLocal eventbean;
@EJB
private transient PlaceBeanLocal placebean;
private PlaceBeanLocal placebean;
@EJB
private PermissionBeanLocal permbean;
@Inject
private transient FacesContext context;
......@@ -84,6 +87,14 @@ public class AjaxMapView extends GenericCDIView {
int placeId = Integer.parseInt(vmap.get("placeId"));
place = placebean.find(placeId);
logger.info("Found place {} with placeid {}", place, placeId);
if (place.isReservedFor(permbean.getCurrentUser()))
{
placebean.releasePlace(place);
}
else if (place.isBuyable() && !place.isTaken())
{
placebean.reservePlace(place, permbean.getCurrentUser());
}
}
public String getTestVal() {
......@@ -112,7 +123,7 @@ public class AjaxMapView extends GenericCDIView {
public FacesContext getContext() {
return context;
}
}
public void setContext(FacesContext context) {
this.context = context;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!