Skip to content

Commit

Permalink
Added Freemarker & jquery form
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvoelk committed Feb 4, 2012
1 parent 10eeb04 commit 3d1abec
Show file tree
Hide file tree
Showing 16 changed files with 1,206 additions and 56 deletions.
1 change: 1 addition & 0 deletions holidays-web.iml
Expand Up @@ -27,6 +27,7 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="AppEngine API" level="project" />
<orderEntry type="library" name="Restlet" level="project" />
<orderEntry type="library" name="Freemarker" level="project" />
<orderEntry type="library" name="Google Guava" level="project" />
<orderEntry type="library" name="JodaTime" level="project" />
<orderEntry type="library" name="jollyday-0.4.5" level="project" />
Expand Down
27 changes: 27 additions & 0 deletions java/src/org/voelk/holidays/web/HolidayApplication.java
@@ -0,0 +1,27 @@
package org.voelk.holidays.web;

import org.restlet.*;
import org.restlet.data.*;
import org.restlet.resource.*;
import org.restlet.routing.*;

import java.util.*;

public abstract class HolidayApplication extends Application {

public HolidayApplication() {
getConnectorService().getClientProtocols().add(Protocol.WAR);
getMetadataService().setDefaultCharacterSet(CharacterSet.UTF_8);
}

@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
for (Map.Entry<String, Class<? extends ServerResource>> entry : getRoutes().entrySet()) {
router.attach(entry.getKey(), entry.getValue());
}
return router;
}

protected abstract Map<String, Class<? extends ServerResource>> getRoutes();
}
34 changes: 34 additions & 0 deletions java/src/org/voelk/holidays/web/HolidayPagesApplication.java
@@ -0,0 +1,34 @@
package org.voelk.holidays.web;

import freemarker.template.*;
import org.restlet.*;
import org.restlet.ext.freemarker.*;
import org.restlet.resource.*;
import org.voelk.holidays.web.pages.*;

import java.util.*;

public class HolidayPagesApplication extends HolidayApplication {

private Configuration configuration;

@Override
public Restlet createInboundRoot() {
configuration = new Configuration();
configuration.setTemplateLoader(new ContextTemplateLoader(getContext(),
"war:///WEB-INF/templates/pages"));
configuration.setDefaultEncoding("UTF-8");
return super.createInboundRoot();
}

@Override
protected Map<String, Class<? extends ServerResource>> getRoutes() {
Map<String, Class<? extends ServerResource>> ret = new HashMap<String, Class<? extends ServerResource>>();
ret.put("/public/calculator", CalculatorPageResource.class);
return ret;
}

public Configuration getConfiguration() {
return configuration;
}
}
21 changes: 12 additions & 9 deletions java/src/org/voelk/holidays/web/HolidayWebservicesApplication.java
@@ -1,13 +1,16 @@
package org.voelk.holidays.web;

import org.restlet.*;
import org.restlet.routing.*;
import org.voelk.holidays.web.webservices.HolidayCalculatorWebserviceResource;

public class HolidayWebservicesApplication extends Application {
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/calculate/neededDays", HolidayCalculatorWebserviceResource.class);
return router;
import org.restlet.resource.*;
import org.voelk.holidays.web.webservices.*;

import java.util.*;

public class HolidayWebservicesApplication extends HolidayApplication {

@Override
protected Map<String, Class<? extends ServerResource>> getRoutes() {
Map<String, Class<? extends ServerResource>> ret = new HashMap<String, Class<? extends ServerResource>>();
ret.put("/public/calculate/neededDays", HolidayCalculatorWebserviceResource.class);
return ret;
}
}
11 changes: 11 additions & 0 deletions java/src/org/voelk/holidays/web/I18nBean.java
@@ -0,0 +1,11 @@
package org.voelk.holidays.web;

import java.util.*;

public class I18nBean {
private static ResourceBundle labelsBundle = ResourceBundle.getBundle("i18n.labelsBundle");

public static String getText(String key) {
return labelsBundle.getString(key);
}
}
19 changes: 19 additions & 0 deletions java/src/org/voelk/holidays/web/pages/CalculatorPageResource.java
@@ -0,0 +1,19 @@
package org.voelk.holidays.web.pages;

import com.google.common.collect.*;

import java.util.*;

public class CalculatorPageResource extends PageResource {


@Override
protected String getTemplatePath() {
return "calculator.ftl";
}

@Override
protected Map<String, Object> getPageData() {
return Maps.newHashMap();
}
}
33 changes: 33 additions & 0 deletions java/src/org/voelk/holidays/web/pages/PageResource.java
@@ -0,0 +1,33 @@
package org.voelk.holidays.web.pages;

import org.restlet.data.*;
import org.restlet.ext.freemarker.*;
import org.restlet.representation.*;
import org.restlet.resource.*;
import org.voelk.holidays.web.*;

import java.util.*;

public abstract class PageResource extends ServerResource {
@Override
public HolidayPagesApplication getApplication() {
return (HolidayPagesApplication) super.getApplication();
}

@Get
public Representation toHtml() {
return toRepresentation();
}

protected MediaType getMediaType() {
return MediaType.TEXT_HTML;
}

protected abstract String getTemplatePath();

protected abstract Map<String, Object> getPageData();

protected Representation toRepresentation() {
return new TemplateRepresentation(getTemplatePath(), getApplication().getConfiguration(), getPageData(), getMediaType());
}
}
5 changes: 5 additions & 0 deletions web/WEB-INF/appengine-web.xml
Expand Up @@ -5,4 +5,9 @@
<system-properties>
<property name="de.jollyday.config" value="WEB-INF/jollyday.properties"/>
</system-properties>
<static-files>
<include path="/css/**.css"/>
<include path="/js/**.js"/>
<include path="/favicon.ico"/>
</static-files>
</appengine-web-app>
1 change: 1 addition & 0 deletions web/WEB-INF/classes/i18n/labelsBundle.properties
@@ -0,0 +1 @@
application.title=Urlaubsplaner
Binary file added web/WEB-INF/lib/freemarker-gae-2.3.18.jar
Binary file not shown.
Binary file added web/WEB-INF/lib/org.restlet.ext.freemarker.jar
Binary file not shown.
@@ -1,3 +1,4 @@
<#macro page>
<!DOCTYPE html>
<html>
<head>
Expand All @@ -10,8 +11,8 @@
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/i18n/jquery.ui.datepicker-de.js"
type="text/javascript"></script>
<script src="js/jquery.form.js" type="text/javascript"></script>
<script src="http://www.planbox.com/feedback/feedback.js" type="text/javascript"></script>
<script type="text/javascript" src="js/calculator.js"></script>
<link type="text/css" rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/sunny/jquery-ui.css"
type="text/css" media="all"/>
Expand All @@ -24,17 +25,8 @@
FeedbackOptions.dialogTitle = 'Tell us what you think';
</script>
<div id="content">
<h1>Urlaubsplanung</h1>

<div id="calendar">
<label for="from">Von:</label>
<input type="text" name="from" id="from"/>
<label for="to">Bis:</label>
<input type="text" name="to" id="to"/>
</div>
<div>
benötigte Tage: <span id="daysNeeded"></span>
</div>
<#nested/>
</div>
</body>
</html>
</html>
</#macro>
17 changes: 17 additions & 0 deletions web/WEB-INF/templates/pages/calculator.ftl
@@ -0,0 +1,17 @@
<#import "basePage.ftl" as master>
<@master.page>
<script type="text/javascript" src="js/calculator.js"></script>
<form id="calculationForm" action="/rest/public/calculate/neededDays" method="get">
<h1>Urlaubsplanung</h1>

<div id="calendar">
<label for="localFrom">Von:</label>
<input type="text" name="localFrom" id="localFrom"/><input type="hidden" name="from" id="from"/>
<label for="localTo">Bis:</label>
<input type="text" name="localTo" id="localTo"/><input type="hidden" name="to" id="to"/>
</div>
<div>
benötigte Tage: <span id="daysNeeded"></span>
</div>
</form>
</@master.page>
30 changes: 21 additions & 9 deletions web/WEB-INF/web.xml
Expand Up @@ -14,18 +14,30 @@
</servlet>
<servlet-mapping>
<servlet-name>HolidayWebservices</servlet-name>
<url-pattern>/public/rest/*</url-pattern>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HolidayPages</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>org.voelk.holidays.web.HolidayPagesApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HolidayPages</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>public/calculator.html</welcome-file>
<welcome-file>pages/public/calculator</welcome-file>
</welcome-file-list>
<!--<security-constraint>-->
<!--<web-resource-collection>-->
<!--<web-resource-name>all</web-resource-name>-->
<!--<url-pattern>/*</url-pattern>-->
<!--</web-resource-collection>-->
<!--<auth-constraint>-->
<!--<role-name>*</role-name>-->
<!--</auth-constraint>-->
<!--<web-resource-collection>-->
<!--<web-resource-name>all</web-resource-name>-->
<!--<url-pattern>/*</url-pattern>-->
<!--</web-resource-collection>-->
<!--<auth-constraint>-->
<!--<role-name>*</role-name>-->
<!--</auth-constraint>-->
<!--</security-constraint>-->
</web-app>
47 changes: 22 additions & 25 deletions web/js/calculator.js
@@ -1,37 +1,34 @@
$(function () {
var dates = $("#from, #to").datepicker({
/*global $, jQuery */
function initDatePickers() {
"use strict";
var dates = $("#localFrom, #localTo").datepicker({
numberOfMonths:2,
altFormat:"yy-mm-dd",
onSelect:function (selectedDate) {
var option = this.id == "from" ? "minDate" : "maxDate",
var option = this.id === "localFrom" ? "minDate" : "maxDate",
instance = $(this).data("datepicker"),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings);
dates.not(this).datepicker("option", option, date);
$("#daysNeeded").text("Berechne...");
$.ajax({
url:function () {
var fromDate = dates.filter("#from").datepicker("getDate"),
toDate = dates.filter("#to").datepicker("getDate"),
from = "",
to = "";
if (fromDate) {
from = $.datepicker.formatDate('yy-mm-dd', fromDate);
}
if (toDate) {
to = $.datepicker.formatDate('yy-mm-dd', toDate);
}
return "/public/rest/calculate/neededDays?from=" + from + "&to=" + to;
}(),
context:document.body,
dataType:"text",
success:function (data) {
$("#daysNeeded").text(data);
}
});
$('#calculationForm').submit();
}
})
;
});
$("#localFrom").datepicker("option", "altField", '#from');
$("#localTo").datepicker("option", "altField", '#to');
}
function bindSubmitToAjaxRequest() {
"use strict";
var options = {
target:'#daysNeeded'
};
$('#calculationForm').ajaxForm(options);
}

$(function () {
"use strict";
bindSubmitToAjaxRequest();
initDatePickers();
});

0 comments on commit 3d1abec

Please sign in to comment.