Merge pull request #173 from sdeleuze/prefill-form

* pr/173:
  Polish contribution
  Pre-fill form based on URL hashbang parameters
This commit is contained in:
Stephane Nicoll
2016-01-11 22:28:10 +01:00
6 changed files with 129 additions and 18 deletions

View File

@@ -80,6 +80,22 @@ a compressed tarball instead.
You could use this infrastructure to create your own client since the project is generated
via a plain HTTP call.
[[customize-form]]
== Customize form inputs
You can share or bookmark URLs that will automatically customize form inputs. For instance,
the following URL from the default instance uses `groovy` by default and set the name
to `Groovy Sample`:
[source,bash]
----
https://start.spring.io/#!language=groovy&name=Groovy%20Sample
----
The following hashbang parameters are supported: `type`, `groupId`, `artifactId`, `name`,
`description`, `packageName`, `packaging`, `javaVersion` and `language`. Review the section
above for a description of each of them.
[[meta-data]]
== Service meta-data

View File

@@ -61,6 +61,55 @@
return versionA[3].localeCompare(versionB[3]);
}
/**
* Parse hash bang parameters from a URL as key value object.
* For repeated parameters the last parameter is effective.
* If = syntax is not used the value is set to null.
* #!x&y=3 -> { x:null, y:3 }
* @param url URL to parse or null if window.location is used
* @return Object of key -> value mappings.
* @source https://gist.github.com/zaus/5201739
*/
hashbang = function (url, i, hash) {
url = url || window.location.href;
var pos = url.indexOf('#!');
if( pos < 0 ) return [];
var vars = [], hashes = url.slice(pos + 2).split('&');
for(i = hashes.length; i--;) {
hash = hashes[i].split('=');
vars.push({ name: hash[0], value: hash.length > 1 ? hash[1] : null});
}
return vars;
}
applyParams = function() {
var params = hashbang();
$.each(params, function( index, param ) {
var value = decodeURIComponent(param.value);
switch(param.name) {
case 'type':
case 'packaging':
case 'javaVersion':
case 'language':
$('.' + param.name.toLowerCase() + '-form-group').removeClass("hidden");
$('#' + param.name+ ' option[value="' + value + '"]').prop('selected', true);
break;
case 'groupId':
case 'artifactId':
case 'name':
case 'description':
case 'packageName':
$('.' + param.name.toLowerCase() + '-form-group').removeClass("hidden");
$('#' + param.name).val(value);
break;
}
});
}
}());
$(function () {
@@ -124,6 +173,7 @@ $(function () {
$(".full").addClass("hidden");
$(".tofullversion").removeClass("hidden");
$(".tosimpleversion").addClass("hidden");
applyParams();
$("body").scrollTop(0);
return false;
});
@@ -201,4 +251,15 @@ $(function () {
e.returnValue = false;
}
});
applyParams();
if ("onhashchange" in window) {
window.onhashchange = function() {
$(".full").addClass("hidden");
$(".tofullversion").removeClass("hidden");
$(".tosimpleversion").addClass("hidden");
applyParams();
}
}
});

View File

@@ -46,34 +46,34 @@
<p>Artifact coordinates</p>
<div class="form-group">
<div class="form-group groupid-form-group">
<label for="groupId" class="control-label">${groupId.title}</label>
<input tabindex="3" id="groupId" class="form-control" type="text" value="${groupId.content}"
name="groupId">
</div>
<div class="form-group">
<div class="form-group artifactid-form-group">
<label for="artifactId" class="control-label">${artifactId.title}</label>
<input tabindex="4" id="artifactId" class="form-control" type="text"
value="${artifactId.content}"
name="artifactId">
</div>
<div class="form-group full hidden">
<div class="form-group full hidden name-form-group">
<label for="name" class="control-label">${name.title}</label>
<input tabindex="5" id="name" class="form-control" type="text" value="${name.content}"
name="name">
</div>
<div class="form-group full hidden">
<div class="form-group full hidden description-form-group">
<label for="description" class="control-label">${description.title}</label>
<input tabindex="6" id="description" class="form-control" type="text"
value="${description.content}" name="description">
</div>
<div class="form-group full hidden">
<div class="form-group full hidden packagename-form-group">
<label for="packageName" class="control-label">${packageName.title}</label>
<input tabindex="7" id="packageName" class="form-control" type="text"
value="${packageName.content}"
name="packageName">
</div>
<div class="form-group full hidden">
<div class="form-group full hidden packaging-form-group">
<label for="packaging" class="control-label">${packagings.title}</label>
<select tabindex="8" class="form-control" id="packaging" name="packaging">
<% packagings.content.each { %>
@@ -82,7 +82,7 @@
<% } %>
</select>
</div>
<div class="form-group full hidden">
<div class="form-group full hidden javaversion-form-group">
<label for="javaVersion" class="control-label">${javaVersions.title}</label>
<select tabindex="9" class="form-control" name="javaVersion" id="javaVersion">
<% javaVersions.content.each { %>
@@ -91,7 +91,7 @@
<% } %>
</select>
</div>
<div class="form-group full hidden">
<div class="form-group full hidden language-form-group">
<label for="language" class="control-label">${languages.title}</label>
<select tabindex="10" class="form-control" name="language" id="language">
<% languages.content.each { %>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ class ProjectAssert {
public static final DEFAULT_PACKAGE_NAME = 'com.example'
private static final DEFAULT_APPLICATION_NAME = generateDefaultApplicationName()
public static final DEFAULT_APPLICATION_NAME = 'DemoApplication'
final File dir
Boolean mavenProject
@@ -159,8 +159,4 @@ class ProjectAssert {
new File(dir, localPath)
}
private static generateDefaultApplicationName() {
'DemoApplication'
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.openqa.selenium.interactions.Actions
import org.springframework.test.context.ActiveProfiles
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
/**
@@ -310,8 +311,45 @@ class ProjectGenerationSmokeTests extends AbstractInitializrControllerIntegratio
}
}
@Test
void customizationShowsUpInDefaultView() {
toHome('/#!language=groovy&packageName=com.example.acme') {
assertEquals 'groovy', page.language.value()
assertEquals 'com.example.acme', page.packageName.value()
page.generateProject.click()
at HomePage
def projectAssert = zipProjectAssert(from('demo.zip'))
projectAssert.hasBaseDir('demo')
.isMavenProject()
.isGroovyProject('com.example.acme', ProjectAssert.DEFAULT_APPLICATION_NAME )
.hasStaticAndTemplatesResources(false)
.pomAssert().hasDependenciesCount(3)
.hasSpringBootStarterRootDependency().hasSpringBootStarterTest()
.hasDependency('org.codehaus.groovy', 'groovy')
}
}
@Test
void customizationsShowsUpWhenViewIsSwitched() {
toHome('/#!packaging=war&javaVersion=1.7') {
assertEquals 'war', page.packaging.value()
assertEquals '1.7', page.javaVersion.value()
page.advanced().click()
assertEquals 'war', page.packaging.value()
assertEquals '1.7', page.javaVersion.value()
page.simple().click()
assertEquals 'war', page.packaging.value()
assertEquals '1.7', page.javaVersion.value()
}
}
private Browser toHome(Closure script) {
browser.go("http://localhost:" + port + "/")
toHome('/', script)
}
private Browser toHome(String uri, Closure script) {
browser.go("http://localhost:$port$uri")
browser.at HomePage
script.delegate = browser
script()

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ class HomePage extends Page {
static at = { title == 'Spring Initializr' }
static content = {
advanced { $('.tofullversion a') }
simple { $('tosimpleversion a') }
simple { $('.tosimpleversion a') }
// Simple view
groupId { $('form').groupId() }