diff --git a/README.adoc b/README.adoc index 1a90cb5b..6dd82f63 100644 --- a/README.adoc +++ b/README.adoc @@ -80,6 +80,19 @@ 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 URL that will customize automatically form inputs with this kind of hashbang parameters: + +[source,bash] +---- +https://start.spring.io/#!language=groovy&name=Groovy%20Sample +---- + +`type`, `groupId`, `artifactId`, `name`, `description`, `packageName`, +`packaging`, `javaVersion` and `language` parameters are supported. + [[meta-data]] == Service meta-data diff --git a/initializr/src/main/resources/static/js/start.js b/initializr/src/main/resources/static/js/start.js index 5c018cd7..fdb853aa 100644 --- a/initializr/src/main/resources/static/js/start.js +++ b/initializr/src/main/resources/static/js/start.js @@ -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(); + } + } + }); diff --git a/initializr/src/main/resources/templates/home.html b/initializr/src/main/resources/templates/home.html index 7510e0e3..9aa7e768 100644 --- a/initializr/src/main/resources/templates/home.html +++ b/initializr/src/main/resources/templates/home.html @@ -46,34 +46,34 @@
Artifact coordinates
-