Skip to content

Commit 859bbe9

Browse files
committed
Add programmatic token to automate editing browser.html
In Spring Data REST, we need a more suitable way to tweak browser.html and inject our custom form and JSON Editor. Having a token to search on is quite handy. Updated the docs, sharing how others can use this as well.
1 parent 3325375 commit 859bbe9

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

README.adoc

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,62 @@ HAL.customPostForm = CustomPostForm;
151151
</script>
152152
----
153153

154-
NOTE: To load a custom JavaScript module AND a custom HTML template, you will probably need to create a customized version of `browser.html`.
155-
156154
NOTE: The HAL Browser uses a global `HAL` object, so there is no need to deal with JavaScript packages.
157155

156+
=== Hooking in custom code
157+
158+
To load a custom JavaScript module AND a custom HTML template, you will probably need to create a customized version of `browser.html`. Manually editing the file might be good enough. Just look for the comment at the end of `browser.html`.
159+
160+
But if you need something a little more sophisticated, like hooking into your build system, you can do a token search like this:
161+
162+
[source,xml]
163+
----
164+
<plugin>
165+
<groupId>org.apache.maven.plugins</groupId>
166+
<artifactId>maven-antrun-plugin</artifactId>
167+
<executions>
168+
<execution>
169+
<phase>process-resources</phase>
170+
<goals>
171+
<goal>run</goal>
172+
</goals>
173+
<configuration>
174+
<target>
175+
<copy todir="${project.build.outputDirectory}/META-INF/spring-data-rest/hal-browser">
176+
<fileset dir="${project.build.directory}/hal-browser/META-INF/resources/webjars/hal-browser/${browser.version}" />
177+
</copy>
178+
<copy todir="${project.build.outputDirectory}/META-INF/spring-data-rest/hal-browser/vendor/js">
179+
<fileset dir="${project.build.directory}/json-editor/META-INF/resources/webjars/json-editor/${json-editor.version}" />
180+
</copy>
181+
<copy file="${project.build.outputDirectory}/META-INF/spring-data-rest/hal-browser/browser.html"
182+
tofile="${project.build.outputDirectory}/META-INF/spring-data-rest/hal-browser/index.html" />
183+
<replace file="${project.build.outputDirectory}/META-INF/spring-data-rest/hal-browser/index.html">
184+
<replacefilter>
185+
<replacetoken>&lt;!-- Need to plugin some custom code? Put it here. --&gt;</replacetoken>
186+
<replacevalue>
187+
188+
&lt;!-- Spring Data REST's plugin --&gt;
189+
190+
&lt;script src="vendor/js/jsoneditor.js" /&gt;
191+
&lt;script src="js/CustomPostForm.js" /&gt;
192+
193+
&lt;/body&gt;</replacevalue>
194+
</replacefilter>
195+
</replace>
196+
<delete file="${project.build.outputDirectory}/META-INF/spring-data-rest/hal-browser/browser.html" />
197+
</target>
198+
</configuration>
199+
</execution>
200+
</executions>
201+
</plugin>
202+
----
203+
204+
This is code from http://projects.spring.io/spring-data-rest/[Spring Data REST]
205+
206+
* The HAL Browser and the JSON Editor are both unpacked from WebJars
207+
* Key files are copied into the target artifacts.
208+
* The custom editor (driven by Spring Data REST's metadata) are then swapped in with a <<Customizing the POST form,custom form>>.
209+
158210
== Usage Instructions
159211

160212
All you should need to do is copy the files into your webroot.

browser.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ <h2>Embedded Resources</h2>
246246

247247
<script src="js/hal/views/documentation.js"></script>
248248

249+
<!-- Need to plugin some custom code? Put it here. -->
250+
249251
<script>
250252
var browser = new HAL.Browser({
251253
container: $('#browser'),

0 commit comments

Comments
 (0)