From 53b07f2e289f229a8e7fc75bfa09a605c282d4e8 Mon Sep 17 00:00:00 2001 From: Bryan Larsen Date: Sun, 21 Feb 2010 11:42:55 -0500 Subject: [PATCH] [#635] add onfocus handler and greyed-out nil-value support to name-one --- .../hobo_rapid/templates/hobo-rapid.js | 22 ++++++++++++++----- .../clean/public/stylesheets/rapid-ui.css | 4 +++- hobo/taglibs/rapid_forms.dryml | 18 ++++++++++++--- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/hobo/rails_generators/hobo_rapid/templates/hobo-rapid.js b/hobo/rails_generators/hobo_rapid/templates/hobo-rapid.js index 2418cd1fe..7c7ee916a 100755 --- a/hobo/rails_generators/hobo_rapid/templates/hobo-rapid.js +++ b/hobo/rails_generators/hobo_rapid/templates/hobo-rapid.js @@ -867,9 +867,10 @@ NameManyInput = Object.extend(SelectManyInput, { } }) - + AutocompleteBehavior = Behavior.create({ initialize : function() { + this.minChars = parseInt(Hobo.getClassData(this.element, "min-chars")); var match = this.element.className.match(/complete-on::([\S]+)/) var target = match[1].split('::') var typedId = target[0] @@ -878,11 +879,22 @@ AutocompleteBehavior = Behavior.create({ var spec = Hobo.parseModelSpec(typedId) var url = urlBase + "/" + Hobo.pluralise(spec.name) + "/complete_" + completer var parameters = spec.id ? "id=" + spec.id : "" - new Ajax.Autocompleter(this.element, - this.element.next('.completions-popup'), - url, - {paramName:'query', method:'get', parameters: parameters}); + this.autocompleter = new Ajax.Autocompleter(this.element, + this.element.next('.completions-popup'), + url, + {paramName:'query', method:'get', parameters: parameters, minChars: this.minChars}); + }, + + onfocus: function() { + if(this.element.hasClassName("nil-value")) { + this.element.value = ''; + this.element.removeClassName("nil-value"); + } + if(this.minChars==0) { + this.autocompleter.activate(); + } } + }) diff --git a/hobo/rails_generators/hobo_rapid/templates/themes/clean/public/stylesheets/rapid-ui.css b/hobo/rails_generators/hobo_rapid/templates/themes/clean/public/stylesheets/rapid-ui.css index d4801bf63..7fceec8db 100755 --- a/hobo/rails_generators/hobo_rapid/templates/themes/clean/public/stylesheets/rapid-ui.css +++ b/hobo/rails_generators/hobo_rapid/templates/themes/clean/public/stylesheets/rapid-ui.css @@ -97,4 +97,6 @@ select.dev-user-changer:hover { opacity: 1; } optgroup.disabled-option { color: #ccc; height: 1em; -} \ No newline at end of file +} + +input.nil-value { color:grey; } \ No newline at end of file diff --git a/hobo/taglibs/rapid_forms.dryml b/hobo/taglibs/rapid_forms.dryml index 1a7eee817..a96665108 100644 --- a/hobo/taglibs/rapid_forms.dryml +++ b/hobo/taglibs/rapid_forms.dryml @@ -717,14 +717,26 @@ We're using an object as the complete-target rather than a class. This allows There's another example of `` use in the [recipes](http://cookbook.hobocentral.net/recipes/36-using-a-name-one-one-a). +### Attributes: + +- `complete-target`, `completer`: see above +- `min-chars`: The minimum number of characters that must be entered in the input field before an Ajax request is made. +- `nil-value`: If there is no current value, this text will appear greyed out inside the control, and will disappear on focus. + +### Note: + +If you wish to set `min-chars` to 0, you will require this [patch to controls.js](http://github.com/bryanlarsen/scriptaculous/commit/3915b7b). 'controls.js' was added to your project via the rails generator, not via Hobo. + --> -<% +<% complete_target ||= this_field_reflection.klass completer ||= (complete_target.is_a?(Class) ? complete_target : complete_target.class).name_attribute + min_chars ||= 1 + value = name(:no_wrapper => true, :if_present => true) -%>