Skip to content

Commit

Permalink
Add support for disabled form fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfiller committed Sep 22, 2011
1 parent 91734e9 commit f1e1c55
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 69 deletions.
9 changes: 9 additions & 0 deletions chosen/chosen.css
Expand Up @@ -303,6 +303,15 @@
}
/* @end */

/* @group Disabled Support */
.chzn-disabled {
cursor: default;
opacity:0.5 !important;
}
.chzn-disabled .chzn-single {
cursor: default;
}

/* @group Right to Left */
.chzn-rtl { direction:rtl;text-align: right; }
.chzn-rtl .chzn-single { padding-left: 0; padding-right: 8px; }
Expand Down
54 changes: 36 additions & 18 deletions chosen/chosen.jquery.js
Expand Up @@ -44,6 +44,9 @@
this.click_test_action = __bind(function(evt) {
return this.test_active_click(evt);
}, this);
this.activate_action = __bind(function(evt) {
return this.activate_field(evt);
}, this);
this.active_field = false;
this.mouse_on_container = false;
this.results_showing = false;
Expand Down Expand Up @@ -134,30 +137,44 @@
return this.search_field.focus(__bind(function(evt) {
return this.input_focus(evt);
}, this));
}
};
Chosen.prototype.search_field_disabled = function() {
this.is_disabled = this.form_field_jq.attr('disabled');
if (this.is_disabled) {
this.container.addClass('chzn-disabled');
this.search_field.attr('disabled', true);
if (!this.is_multiple) {
return this.selected_item.unbind("focus", this.activate_action);
}
} else {
return this.selected_item.focus(__bind(function(evt) {
return this.activate_field(evt);
}, this));
this.container.removeClass('chzn-disabled');
this.search_field.attr('disabled', false);
if (!this.is_multiple) {
return this.selected_item.bind("focus", this.activate_action);
}
}
};
Chosen.prototype.container_mousedown = function(evt) {
if (evt && evt.type === "mousedown") {
evt.stopPropagation();
}
if (!this.pending_destroy_click) {
if (!this.active_field) {
if (this.is_multiple) {
this.search_field.val("");
if (!this.is_disabled) {
if (evt && evt.type === "mousedown") {
evt.stopPropagation();
}
if (!this.pending_destroy_click) {
if (!this.active_field) {
if (this.is_multiple) {
this.search_field.val("");
}
$(document).click(this.click_test_action);
this.results_show();
} else if (!this.is_multiple && evt && ($(evt.target) === this.selected_item || $(evt.target).parents("a.chzn-single").length)) {
evt.preventDefault();
this.results_toggle();
}
$(document).click(this.click_test_action);
this.results_show();
} else if (!this.is_multiple && evt && ($(evt.target) === this.selected_item || $(evt.target).parents("a.chzn-single").length)) {
evt.preventDefault();
this.results_toggle();
return this.activate_field();
} else {
return this.pending_destroy_click = false;
}
return this.activate_field();
} else {
return this.pending_destroy_click = false;
}
};
Chosen.prototype.mouse_enter = function() {
Expand Down Expand Up @@ -243,6 +260,7 @@
}
}
}
this.search_field_disabled();
this.show_search_field_default();
this.search_field_scale();
this.search_results.html(content);
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

52 changes: 35 additions & 17 deletions chosen/chosen.proto.js
Expand Up @@ -30,6 +30,9 @@
this.click_test_action = __bind(function(evt) {
return this.test_active_click(evt);
}, this);
this.activate_action = __bind(function(evt) {
return this.activate_field(evt);
}, this);
this.active_field = false;
this.mouse_on_container = false;
this.results_showing = false;
Expand Down Expand Up @@ -125,29 +128,43 @@
return this.search_field.observe("focus", __bind(function(evt) {
return this.input_focus(evt);
}, this));
}
};
Chosen.prototype.search_field_disabled = function() {
this.is_disabled = this.form_field.disabled;
if (this.is_disabled) {
this.container.addClassName('chzn-disabled');
this.search_field.disabled = true;
if (!this.is_multiple) {
return this.selected_item.stopObserving("focus", this.activate_action);
}
} else {
return this.selected_item.observe("focus", __bind(function(evt) {
return this.activate_field(evt);
}, this));
this.container.removeClassName('chzn-disabled');
this.search_field.disabled = false;
if (!this.is_multiple) {
return this.selected_item.observe("focus", this.activate_action);
}
}
};
Chosen.prototype.container_mousedown = function(evt) {
if (evt && evt.type === "mousedown") {
evt.stop();
}
if (!this.pending_destroy_click) {
if (!this.active_field) {
if (this.is_multiple) {
this.search_field.clear();
if (!this.is_disabled) {
if (evt && evt.type === "mousedown") {
evt.stop();
}
if (!this.pending_destroy_click) {
if (!this.active_field) {
if (this.is_multiple) {
this.search_field.clear();
}
document.observe("click", this.click_test_action);
this.results_show();
} else if (!this.is_multiple && evt && (evt.target === this.selected_item || evt.target.up("a.chzn-single"))) {
this.results_toggle();
}
document.observe("click", this.click_test_action);
this.results_show();
} else if (!this.is_multiple && evt && (evt.target === this.selected_item || evt.target.up("a.chzn-single"))) {
this.results_toggle();
return this.activate_field();
} else {
return this.pending_destroy_click = false;
}
return this.activate_field();
} else {
return this.pending_destroy_click = false;
}
};
Chosen.prototype.mouse_enter = function() {
Expand Down Expand Up @@ -229,6 +246,7 @@
}
}
}
this.search_field_disabled();
this.show_search_field_default();
this.search_field_scale();
this.search_results.update(content);
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.proto.min.js

Large diffs are not rendered by default.

45 changes: 28 additions & 17 deletions coffee/chosen.jquery.coffee
Expand Up @@ -31,6 +31,7 @@ class Chosen
set_default_values: ->

@click_test_action = (evt) => this.test_active_click(evt)
@activate_action = (evt) => this.activate_field(evt)
@active_field = false
@mouse_on_container = false
@results_showing = false
Expand Down Expand Up @@ -105,24 +106,34 @@ class Chosen
if @is_multiple
@search_choices.click (evt) => this.choices_click(evt)
@search_field.focus (evt) => this.input_focus(evt)

search_field_disabled: ->
@is_disabled = @form_field_jq.attr 'disabled'
if(@is_disabled)
@container.addClass 'chzn-disabled'
@search_field.attr 'disabled', true
@selected_item.unbind "focus", @activate_action if !@is_multiple
else
@selected_item.focus (evt) => this.activate_field(evt)
@container.removeClass 'chzn-disabled'
@search_field.attr 'disabled', false
@selected_item.bind "focus", @activate_action if !@is_multiple

container_mousedown: (evt) ->
if evt and evt.type is "mousedown"
evt.stopPropagation()
if not @pending_destroy_click
if not @active_field
@search_field.val "" if @is_multiple
$(document).click @click_test_action
this.results_show()
else if not @is_multiple and evt and ($(evt.target) is @selected_item || $(evt.target).parents("a.chzn-single").length)
evt.preventDefault()
this.results_toggle()

this.activate_field()
else
@pending_destroy_click = false
if !@is_disabled
if evt and evt.type is "mousedown"
evt.stopPropagation()
if not @pending_destroy_click
if not @active_field
@search_field.val "" if @is_multiple
$(document).click @click_test_action
this.results_show()
else if not @is_multiple and evt and ($(evt.target) is @selected_item || $(evt.target).parents("a.chzn-single").length)
evt.preventDefault()
this.results_toggle()

this.activate_field()
else
@pending_destroy_click = false

mouse_enter: -> @mouse_on_container = true
mouse_leave: -> @mouse_on_container = false
Expand Down Expand Up @@ -195,6 +206,7 @@ class Chosen
else if data.selected and not @is_multiple
@selected_item.find("span").text data.text

this.search_field_disabled()
this.show_search_field_default()
this.search_field_scale()

Expand Down Expand Up @@ -551,8 +563,7 @@ class Chosen
when 40
this.keydown_arrow()
break



search_field_scale: ->
if @is_multiple
h = 0
Expand Down
41 changes: 26 additions & 15 deletions coffee/chosen.proto.coffee
Expand Up @@ -23,6 +23,7 @@ class Chosen
set_default_values: ->

@click_test_action = (evt) => this.test_active_click(evt)
@activate_action = (evt) => this.activate_field(evt)
@active_field = false
@mouse_on_container = false
@results_showing = false
Expand Down Expand Up @@ -100,24 +101,33 @@ class Chosen
if @is_multiple
@search_choices.observe "click", (evt) => this.choices_click(evt)
@search_field.observe "focus", (evt) => this.input_focus(evt)
else
@selected_item.observe "focus", (evt) => this.activate_field(evt)

search_field_disabled: ->
@is_disabled = @form_field.disabled
if(@is_disabled)
@container.addClassName 'chzn-disabled'
@search_field.disabled = true
@selected_item.stopObserving "focus", @activate_action if !@is_multiple
else
@container.removeClassName 'chzn-disabled'
@search_field.disabled = false
@selected_item.observe "focus", @activate_action if !@is_multiple

container_mousedown: (evt) ->
if evt and evt.type is "mousedown"
evt.stop()
if not @pending_destroy_click
if not @active_field
@search_field.clear() if @is_multiple
document.observe "click", @click_test_action
this.results_show()
else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chzn-single"))
this.results_toggle()

this.activate_field()
else
@pending_destroy_click = false
if !@is_disabled
if evt and evt.type is "mousedown"
evt.stop()
if not @pending_destroy_click
if not @active_field
@search_field.clear() if @is_multiple
document.observe "click", @click_test_action
this.results_show()
else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chzn-single"))
this.results_toggle()

this.activate_field()
else
@pending_destroy_click = false

mouse_enter: -> @mouse_on_container = true
mouse_leave: -> @mouse_on_container = false
Expand Down Expand Up @@ -190,6 +200,7 @@ class Chosen
else if data.selected and not @is_multiple
@selected_item.down("span").update( data.html )

this.search_field_disabled()
this.show_search_field_default()
this.search_field_scale()

Expand Down

0 comments on commit f1e1c55

Please sign in to comment.