Skip to content

Commit

Permalink
[#635] add onfocus handler and greyed-out nil-value support to name-one
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanlarsen committed Feb 21, 2010
1 parent 60e3a2a commit 53b07f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
22 changes: 17 additions & 5 deletions hobo/rails_generators/hobo_rapid/templates/hobo-rapid.js
Expand Up @@ -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]
Expand All @@ -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();
}
}

})


Expand Down
Expand Up @@ -97,4 +97,6 @@ select.dev-user-changer:hover { opacity: 1; }
optgroup.disabled-option {
color: #ccc;
height: 1em;
}
}

input.nil-value { color:grey; }
18 changes: 15 additions & 3 deletions hobo/taglibs/rapid_forms.dryml
Expand Up @@ -717,14 +717,26 @@ We're using an object as the complete-target rather than a class. This allows

There's another example of `<name-one>` 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.

-->
<def tag="name-one" attrs="complete-target, completer"><%
<def tag="name-one" attrs="complete-target, completer, min-chars, nil-value"><%
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)
-%>
<input type="text" name="#{param_name_for_this}"
class="autocompleter #{type_and_field.dasherize} #{css_data :complete_on, typed_id(complete_target), completer}"
value="&name :no_wrapper => true, :if_present => true"
class="autocompleter #{type_and_field.dasherize} #{css_data :complete_on, typed_id(complete_target), completer} #{css_data :min_chars, min_chars} #{'nil-value' if value==''}"
value="#{value=='' ? nil_value : value}"
merge-attrs/>
<div class="completions-popup" style="display:none"></div>
</def>
Expand Down

0 comments on commit 53b07f2

Please sign in to comment.