Skip to content

Commit 850e5aa

Browse files
committed
Change template sample request to handle custom named groups.
1 parent 3ae43c6 commit 850e5aa

File tree

4 files changed

+104
-61
lines changed

4 files changed

+104
-61
lines changed

template/css/style.css

+8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ article h3 {
8989
margin: 0 0 10px 0;
9090
}
9191

92+
article h4 {
93+
font-family: "Source Sans Pro", sans-serif;
94+
font-weight: 600;
95+
font-size: 14px;
96+
line-height: 16px;
97+
margin: 0 0 8px 0;
98+
}
99+
92100
table {
93101
border-collapse: collapse;
94102
width: 100%;

template/handlebars_helper.js

+29-19
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ define([
2727
Handlebars.registerHelper("underscoreToSpace", function(text){
2828
return text.replace(/(_+)/g, " ");
2929
});
30-
30+
3131
/**
32-
*
32+
*
3333
*/
3434
Handlebars.registerHelper("assign", function(name) {
3535
if(arguments.length > 0)
@@ -43,50 +43,60 @@ define([
4343
});
4444

4545
/**
46-
*
46+
*
4747
*/
4848
Handlebars.registerHelper("nl2br", function(text) {
4949
return _handlebarsNewlineToBreak(text);
5050
});
5151

5252
/**
53-
*
53+
*
5454
*/
5555
Handlebars.registerHelper("if_eq", function(context, options) {
56-
if(context === options.hash.compare) return options.fn(this);
56+
var compare = context;
57+
// Get length if context is an object
58+
if (context instanceof Object && ! (options.hash.compare instanceof Object) )
59+
compare = Object.keys(context).length;
60+
61+
if(compare === options.hash.compare) return options.fn(this);
5762
return options.inverse(this);
5863
});
5964

6065
/**
61-
*
66+
*
6267
*/
6368
Handlebars.registerHelper("if_gt", function(context, options) {
64-
if(context > options.hash.compare) return options.fn(this);
69+
var compare = context;
70+
// Get length if context is an object
71+
if (context instanceof Object && ! (options.hash.compare instanceof Object) )
72+
compare = Object.keys(context).length;
73+
74+
if(compare > options.hash.compare) return options.fn(this);
6575
return options.inverse(this);
6676
});
67-
77+
6878
/**
69-
*
79+
*
7080
*/
7181
Handlebars.registerHelper("subTemplate", function(name, sourceContext) {
7282
var template = Handlebars.compile($("#template-" + name).html());
7383
var templateContext = $.extend({}, this, sourceContext.hash);
7484
return new Handlebars.SafeString( template(templateContext) );
7585
});
76-
86+
7787
/**
78-
*
88+
*
7989
*/
8090
Handlebars.registerHelper("toLowerCase", function(value) {
8191
return (value && typeof value === "string") ? value.toLowerCase() : '';
8292
});
8393

8494
/**
85-
*
95+
*
8696
*/
8797
Handlebars.registerHelper("splitFill", function(value, splitChar, fillChar) {
8898
var splits = value.split(splitChar);
89-
99+
90100
return new Array(splits.length).join(fillChar) + splits[splits.length - 1];
91101
});
92102

@@ -102,7 +112,7 @@ define([
102112
} // _handlebarsNewlineToBreak
103113

104114
/**
105-
*
115+
*
106116
*/
107117
Handlebars.registerHelper("each_compare_keys", function(source, compare, options) {
108118
var newSource = [];
@@ -130,23 +140,23 @@ define([
130140
}
131141
return _handlebarsEachCompared("key", newSource, newCompare, options);
132142
});
133-
143+
134144
/**
135-
*
145+
*
136146
*/
137147
Handlebars.registerHelper("each_compare_field", function(source, compare, options) {
138148
return _handlebarsEachCompared("field", source, compare, options);
139149
});
140150

141151
/**
142-
*
152+
*
143153
*/
144154
Handlebars.registerHelper("each_compare_title", function(source, compare, options) {
145155
return _handlebarsEachCompared("title", source, compare, options);
146156
});
147157

148158
/**
149-
*
159+
*
150160
*/
151161
Handlebars.registerHelper("showDiff", function(source, compare, options) {
152162
var ds = "";
@@ -165,7 +175,7 @@ define([
165175
});
166176

167177
/**
168-
*
178+
*
169179
*/
170180
function _handlebarsEachCompared(fieldname, source, compare, options)
171181
{

template/index.html

+39-21
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ <h1>{{underscoreToSpace article.group}}{{#if article.title}} - {{article.title}}
162162
{{subTemplate "article-param-block" params=article.info _hasType=_hasTypeInInfoFields section="info"}}
163163
{{subTemplate "article-param-block" params=article.error _col1="Name" _hasType=_hasTypeInErrorFields section="error"}}
164164

165-
{{subTemplate "article-sample-request" article=article}}
165+
{{subTemplate "article-sample-request" article=article id=id}}
166166

167167
</article>
168168
</script>
@@ -234,31 +234,49 @@ <h2>{{__ "Send a Sample Request"}}</h2>
234234
</div>
235235
</div>
236236

237+
{{#if article.header}}
238+
{{#if article.header.fields}}
237239
<h3>{{__ "Headers"}}</h3>
238-
{{#each article.header.fields.Header}}
239-
<div class="control-group">
240-
<label class="control-label" for="sample-request-field-{{field}}">{{field}}</label>
241-
<div class="controls">
242-
<div class="input-append">>
243-
<input type="text" placeholder="{{field}}" class="sample-request-header" data-name="{{field}}">
244-
<span class="add-on">{{type}}</span>
240+
{{#each article.header.fields}}
241+
<h4><input type="radio" data-sample-request-header-group-id="sample-request-header-{{@index}}" name="{{../id}}-sample-request-header" value="{{@index}}" class="sample-request-header sample-request-switch"{{#if_eq @index compare=0}} checked{{/if_eq}}> {{@key}}</h4>
242+
<div class="{{../id}}-sample-request-header-fields{{#if_gt @index compare=0}} hide{{/if_gt}}">
243+
{{#each this}}
244+
<div class="control-group">
245+
<label class="control-label" for="sample-request-header-field-{{field}}">{{field}}</label>
246+
<div class="controls">
247+
<div class="input-append">>
248+
<input type="text" placeholder="{{field}}" class="sample-request-header" data-sample-request-header-name="{{field}}" data-sample-request-header-group="sample-request-header-{{@../index}}">
249+
<span class="add-on">{{type}}</span>
250+
</div>
251+
</div>
245252
</div>
253+
{{/each}}
246254
</div>
247-
</div>
248-
{{/each}}
255+
{{/each}}
256+
{{/if}}
257+
{{/if}}
249258

250-
<h3>{{__ "Data"}}</h3>
251-
{{#each article.parameter.fields.Parameter}}
252-
<div class="control-group">
253-
<label class="control-label" for="sample-request-field-{{field}}">{{field}}</label>
254-
<div class="controls">
255-
<div class="input-append">>
256-
<input type="text" placeholder="{{field}}" class="sample-request-param" data-name="{{field}}">
257-
<span class="add-on">{{type}}</span>
259+
{{#if article.parameter}}
260+
{{#if article.parameter.fields}}
261+
<h3>{{__ "Parameters"}}</h3>
262+
{{#each article.parameter.fields}}
263+
<h4><input type="radio" data-sample-request-param-group-id="sample-request-param-{{@index}}" name="{{../id}}-sample-request-param" value="{{@index}}" class="sample-request-param sample-request-switch"{{#if_eq @index compare=0}} checked{{/if_eq}}> {{@key}}</h4>
264+
<div class="{{../id}}-sample-request-param-fields{{#if_gt @index compare=0}} hide{{/if_gt}}">
265+
{{#each this}}
266+
<div class="control-group">
267+
<label class="control-label" for="sample-request-param-field-{{field}}">{{field}}</label>
268+
<div class="controls">
269+
<div class="input-append">>
270+
<input type="text" placeholder="{{field}}" class="sample-request-param" data-sample-request-param-name="{{field}}" data-sample-request-param-group="sample-request-param-{{@../index}}">
271+
<span class="add-on">{{type}}</span>
272+
</div>
273+
</div>
258274
</div>
275+
{{/each}}
259276
</div>
260-
</div>
261-
{{/each}}
277+
{{/each}}
278+
{{/if}}
279+
{{/if}}
262280

263281
<div class="control-group">
264282
<div class="controls">
@@ -369,7 +387,7 @@ <h1>{{underscoreToSpace article.group}} - {{{showDiff article.title compare.titl
369387
{{subTemplate "article-compare-param-block" source=article.info compare=compare.info _hasType=_hasTypeInInfoFields section="info"}}
370388
{{subTemplate "article-compare-param-block" source=article.error compare=compare.error _col1="Name" _hasType=_hasTypeInErrorFields section="error"}}
371389

372-
{{subTemplate "article-sample-request" article=article}}
390+
{{subTemplate "article-sample-request" article=article id=id}}
373391

374392
</article>
375393
</script>

0 commit comments

Comments
 (0)