diff --git a/tailbone/forms/core.py b/tailbone/forms/core.py
index 4ce15b14..0f7211c4 100644
--- a/tailbone/forms/core.py
+++ b/tailbone/forms/core.py
@@ -330,7 +330,7 @@ class Form(object):
def __init__(self, fields=None, schema=None, request=None, mobile=False, readonly=False, readonly_fields=[],
model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers=None,
- hidden={}, widgets={}, defaults={}, validators={}, required={}, helptext={},
+ hidden={}, widgets={}, defaults={}, validators={}, required={}, helptext={}, focus_spec=None,
action_url=None, cancel_url=None):
self.fields = None
@@ -362,6 +362,7 @@ class Form(object):
self.validators = validators or {}
self.required = required or {}
self.helptext = helptext or {}
+ self.focus_spec = focus_spec
self.action_url = action_url
self.cancel_url = cancel_url
@@ -717,6 +718,8 @@ class Form(object):
# TODO: deprecate / remove the latter option here
if self.auto_disable_save or self.auto_disable:
context['form_kwargs']['class_'] = 'autodisable'
+ if self.focus_spec:
+ context['form_kwargs']['data-focus'] = self.focus_spec
context['request'] = self.request
context['readonly_fields'] = self.readonly_fields
context['render_field_readonly'] = self.render_field_readonly
diff --git a/tailbone/static/js/tailbone.mobile.js b/tailbone/static/js/tailbone.mobile.js
index c168543a..6ebbac06 100644
--- a/tailbone/static/js/tailbone.mobile.js
+++ b/tailbone/static/js/tailbone.mobile.js
@@ -50,7 +50,8 @@ $(document).on('autocompleteitemselected', function(event, uuid) {
/**
* Automatically set focus to certain fields, on various pages
- * TODO: this should accept selector params instead of hard-coding..?
+ * TODO: should be letting the form declare a "focus spec" instead, to avoid
+ * hard-coding these field names below!
*/
function setfocus() {
var el = null;
@@ -73,6 +74,21 @@ $(document).on('pageshow', function() {
setfocus();
+ // if current page has form, which has declared a "focus spec", then try to
+ // set focus accordingly
+ var form = $('.ui-page-active form');
+ if (form) {
+ var spec = form.data('focus');
+ if (spec) {
+ var input = $(spec);
+ if (input) {
+ if (input.is(':visible')) {
+ input.focus();
+ }
+ }
+ }
+ }
+
});
diff --git a/tailbone/templates/mobile/master/edit_row.mako b/tailbone/templates/mobile/master/edit_row.mako
index 3c81eb55..93eb12e3 100644
--- a/tailbone/templates/mobile/master/edit_row.mako
+++ b/tailbone/templates/mobile/master/edit_row.mako
@@ -5,15 +5,7 @@
<%def name="page_title()">${h.link_to(index_title, index_url)} » ${h.link_to(parent_title, parent_url)} » ${h.link_to(instance_title, instance_url)} » Edit%def>
-## TODO: this should not be necessary, correct?
-## <%def name="buttons()">
-##
-## ${h.submit('create', form.update_label)}
-## ${h.link_to("Cancel", form.cancel_url, class_='ui-btn ui-corner-all')}
-## %def>
-