Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3535,7 +3535,9 @@ def _render_options_form(self, profile_list):
profile_form_template = env.from_string(self.profile_form_template)
else:
profile_form_template = env.get_template("form.html")
return profile_form_template.render(profile_list=profile_list)
return profile_form_template.render(
profile_list=profile_list, user_options=self.user_options or {}
)

async def _render_options_form_dynamically(self, current_spawner):
"""
Expand Down
19 changes: 18 additions & 1 deletion kubespawner/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
name="profile"
id="profile-item-{{ profile.slug }}"
value="{{ profile.slug }}"
{% if profile.default %}checked{% endif %} />
{% if user_options.profile == profile.slug %}checked {% elif not user_options.profile and profile.default %}checked {% endif %} />
</div>
<div>
<h3>{{ profile.display_name }}</h3>
Expand Down Expand Up @@ -53,6 +53,7 @@ <h3>{{ profile.display_name }}</h3>
{%- endfor %}
</div>
<script>
const userOptions = {{ user_options|tojson }};
$('.js-profile-option-select, .js-profile-option-label').click(function() {
// we need this bit of JS to select the profile when a <select> inside is clicked.
$(this).parents('.js-profile-label')
Expand Down Expand Up @@ -89,6 +90,22 @@ <h3>{{ profile.display_name }}</h3>
// wrapping in a document.ready to make clear this is executed once
// on page load
$(document).ready(() => {
// Pre-select options from previously saved user_options
const savedProfile = userOptions && userOptions.profile;
if (savedProfile) {
$(`label[for="profile-item-${savedProfile}"] .js-profile-option-select`).each(function() {
// name format: profile-option-{slug}--{optionKey}
const optionKey = $(this).attr('name').split('--').pop();
const unlistedValue = userOptions[optionKey + '--unlisted-choice'];
const savedValue = userOptions[optionKey];
if (unlistedValue) {
$(this).val('unlisted-choice');
$(this).parents('.js-options-container').find('.js-other-input').val(unlistedValue);
} else if (savedValue) {
$(this).val(savedValue);
}
});
}
// trigger the `change` event on the select inputs,
// so that if "Other" is selected on page load, the corresponding
// free-text input shows
Expand Down