Skip to content

Commit 7425638

Browse files
committed
Conditionally recopy title and classNames on update
Modified chosen so that if inherit_select_classes is set to true, then classes will be copied from the select to the chosen container every time "chosen:updated" is triggered. Also added logic to ALWAYS copy the title attribute from the select to the container whenever "chosen:updated" is triggered. Updated both jQuery and prototype versions Heavily refactored unit tests for better granularity All jQuery unit tests are passing, but two prototype unit tests are failing for an unknown reason (exception being thrown from a prototype method, it's not just a failed assertion).
1 parent 910a125 commit 7425638

4 files changed

Lines changed: 172 additions & 38 deletions

File tree

‎coffee/chosen.jquery.coffee‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@ class Chosen extends AbstractChosen
2525
@is_rtl = @form_field_jq.hasClass "chosen-rtl"
2626

2727
set_up_html: ->
28-
container_classes = ["chosen-container"]
29-
container_classes.push "chosen-container-" + (if @is_multiple then "multi" else "single")
30-
container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
31-
container_classes.push "chosen-rtl" if @is_rtl
3228

3329
container_props =
34-
'class': container_classes.join ' '
3530
'style': "width: #{this.container_width()};"
36-
'title': @form_field.title
3731

3832
container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chosen" if @form_field.id.length
3933

4034
@container = ($ "<div />", container_props)
35+
this.update_inherited_classes();
36+
this.update_title_from_form_field();
4137

4238
if @is_multiple
4339
@container.html '<ul class="chosen-choices"><li class="search-field"><input type="text" value="' + @default_text + '" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chosen-drop"><ul class="chosen-results"></ul></div>'
@@ -64,6 +60,17 @@ class Chosen extends AbstractChosen
6460
this.set_tab_index()
6561
this.set_label_behavior()
6662

63+
update_inherited_classes: ->
64+
container_classes = ["chosen-container"]
65+
container_classes.push "chosen-container-" + (if @is_multiple then "multi" else "single")
66+
container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
67+
container_classes.push "chosen-rtl" if @is_rtl
68+
69+
@container.attr('class', container_classes.join ' ')
70+
71+
update_title_from_form_field: ->
72+
@container.attr('title', @form_field.title)
73+
6774
on_ready: ->
6875
@form_field_jq.trigger("chosen:ready", {chosen: this})
6976

@@ -185,6 +192,8 @@ class Chosen extends AbstractChosen
185192

186193
@results_data = SelectParser.select_to_array @form_field
187194

195+
this.update_inherited_classes();
196+
188197
if @is_multiple
189198
@search_choices.find("li.search-choice").remove()
190199
else if not @is_multiple
@@ -198,6 +207,7 @@ class Chosen extends AbstractChosen
198207

199208
this.update_results_content this.results_option_build({first:true})
200209

210+
this.update_title_from_form_field();
201211
this.search_field_disabled()
202212
this.show_search_field_default()
203213
this.search_field_scale()

‎coffee/chosen.proto.coffee‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class @Chosen extends AbstractChosen
1919
container_classes.push "chosen-rtl" if @is_rtl
2020

2121
container_props =
22-
'class': container_classes.join ' '
2322
'style': "width: #{this.container_width()};"
24-
'title': @form_field.title
2523

2624
container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chosen" if @form_field.id.length
2725

2826
@container = if @is_multiple then new Element('div', container_props).update( @multi_temp.evaluate({ "default": @default_text}) ) else new Element('div', container_props).update( @single_temp.evaluate({ "default":@default_text }) )
27+
this.update_inherited_classes();
28+
this.update_title_from_form_field();
2929

3030
@form_field.hide().insert({ after: @container })
3131
@dropdown = @container.down('div.chosen-drop')
@@ -47,6 +47,17 @@ class @Chosen extends AbstractChosen
4747
this.set_tab_index()
4848
this.set_label_behavior()
4949

50+
update_inherited_classes: ->
51+
container_classes = ["chosen-container"]
52+
container_classes.push "chosen-container-" + (if @is_multiple then "multi" else "single")
53+
container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
54+
container_classes.push "chosen-rtl" if @is_rtl
55+
56+
@container.writeAttribute('class', container_classes.join ' ')
57+
58+
update_title_from_form_field: ->
59+
@container.writeAttribute('title', @form_field.title)
60+
5061
on_ready: ->
5162
@form_field.fire("chosen:ready", {chosen: this})
5263

@@ -179,6 +190,8 @@ class @Chosen extends AbstractChosen
179190

180191
@results_data = SelectParser.select_to_array @form_field
181192

193+
this.update_inherited_classes();
194+
182195
if @is_multiple
183196
@search_choices.select("li.search-choice").invoke("remove")
184197
else if not @is_multiple
@@ -192,6 +205,7 @@ class @Chosen extends AbstractChosen
192205

193206
this.update_results_content this.results_option_build({first:true})
194207

208+
this.update_title_from_form_field();
195209
this.search_field_disabled()
196210
this.show_search_field_default()
197211
this.search_field_scale()

‎spec/jquery/basic.spec.coffee‎

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,88 @@
1-
describe "Basic setup", ->
1+
describe "Existential Validation", ->
22
it "should add chosen to jQuery object", ->
33
expect(jQuery.fn.chosen).toBeDefined()
44

5-
it "should create very basic chosen", ->
5+
describe "Basic Functionality", ->
6+
7+
beforeEach ->
68
tmpl = "
7-
<select data-placeholder='Choose a Country...'>
9+
<select data-placeholder='Choose a Country...' title='Initial Title' class='TestClass'>
810
<option value=''></option>
911
<option value='United States'>United States</option>
1012
<option value='United Kingdom'>United Kingdom</option>
1113
<option value='Afghanistan'>Afghanistan</option>
1214
</select>
1315
"
14-
div = $("<div>").html(tmpl)
15-
select = div.find("select")
16-
expect(select.size()).toBe(1)
17-
select.chosen()
16+
@div = $("<div>").html(tmpl)
17+
@select = @div.find('select')
18+
@select.chosen()
19+
@container = @div.find(".chosen-container")
20+
21+
afterEach ->
22+
@select.chosen('destroy')
23+
@div.remove()
24+
25+
it "should contain all of the necessary elements", ->
26+
27+
div = @div
28+
1829
# very simple check that the necessary elements have been created
1930
["container", "container-single", "single", "default"].forEach (clazz)->
2031
el = div.find(".chosen-#{clazz}")
2132
expect(el.size()).toBe(1)
2233

34+
it "should copy the title attribute from the select to the container", ->
35+
expect(@container.attr('title')).toBe 'Initial Title'
36+
37+
it "should be initialized with no value", ->
2338
# test a few interactions
24-
expect(select.val()).toBe ""
39+
expect(@select.val()).toBe ""
2540

26-
container = div.find(".chosen-container")
27-
container.trigger("mousedown") # open the drop
28-
expect(container.hasClass("chosen-container-active")).toBe true
41+
it "should open the dropdown when the container is clicked", ->
42+
@container.trigger("mousedown") # open the drop
43+
expect(@container.hasClass("chosen-container-active")).toBe true
44+
45+
it "should update the select's value when an option is clicked", ->
46+
@container.trigger("mousedown") # open the drop
2947
#select an item
30-
container.find(".active-result").last().trigger("mouseup")
48+
@container.find(".active-result").last().trigger("mouseup")
49+
50+
expect(@select.val()).toBe "Afghanistan"
51+
52+
it "should re-copy the title from the select to the container on update", ->
53+
@select.attr('title', 'new title')
54+
@select.trigger("chosen:updated")
55+
expect(@container.attr('title')).toBe 'new title'
56+
57+
describe "When inherit selected classes is enabled", ->
58+
59+
beforeEach ->
60+
tmpl = "
61+
<select data-placeholder='Choose a Country...' title='Initial Title' class='TestClass'>
62+
<option value=''></option>
63+
<option value='United States'>United States</option>
64+
<option value='United Kingdom'>United Kingdom</option>
65+
<option value='Afghanistan'>Afghanistan</option>
66+
</select>
67+
"
68+
@div = $("<div>").html(tmpl)
69+
@select = @div.find('select')
70+
@select.chosen({inherit_select_classes: true})
71+
@container = @div.find(".chosen-container")
72+
73+
afterEach ->
74+
@select.chosen('destroy')
75+
@div.remove()
76+
77+
it "should copy all classes from the select to the container", ->
78+
expect(@container.hasClass('TestClass')).toBe true
79+
80+
it "should re-copy all classes from the select to the container on update", ->
81+
@select.addClass('SecondClass')
82+
@select.trigger("chosen:updated")
83+
expect(@container.hasClass('TestClass')).toBe true
84+
expect(@container.hasClass('SecondClass')).toBe true
85+
86+
3187

32-
expect(select.val()).toBe "Afghanistan"
3388

‎spec/proto/basic.spec.coffee‎

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,92 @@
1-
describe "Basic setup", ->
1+
describe "Existential Validation", ->
22
it "should add expose a Chosen global", ->
33
expect(Chosen).toBeDefined()
44

5-
it "should create very basic chosen", ->
5+
describe "Basic Functionality", ->
6+
7+
beforeEach ->
68
tmpl = "
7-
<select data-placeholder='Choose a Country...'>
9+
<select data-placeholder='Choose a Country...' title='Initial Title' class='TestClass'>
810
<option value=''></option>
911
<option value='United States'>United States</option>
1012
<option value='United Kingdom'>United Kingdom</option>
1113
<option value='Afghanistan'>Afghanistan</option>
1214
</select>
1315
"
1416

15-
div = new Element("div")
16-
document.body.insert(div)
17-
div.update(tmpl)
18-
select = div.down("select")
19-
expect(select).toBeDefined()
20-
new Chosen(select)
17+
@div = new Element("div")
18+
document.body.insert(@div)
19+
@div.update(tmpl)
20+
@select = @div.down("select")
21+
@chosen = new Chosen(@select)
22+
@container = @div.down(".chosen-container")
23+
24+
afterEach ->
25+
@chosen.destroy()
26+
@div.remove()
27+
28+
it "should contain all of the necessary elements", ->
29+
30+
div = @div
31+
32+
expect(@select).toBeDefined()
33+
2134
# very simple check that the necessary elements have been created
2235
["container", "container-single", "single", "default"].forEach (clazz)->
2336
el = div.down(".chosen-#{clazz}")
2437
expect(el).toBeDefined()
2538

39+
it "should copy the title attribute from the select to the container", ->
40+
expect(@container.readAttribute('title')).toBe 'Initial Title'
41+
42+
it "should be initialized with no value", ->
2643
# test a few interactions
27-
expect($F(select)).toBe ""
44+
expect($F(@select)).toBe ""
45+
46+
it "should open the dropdown when the container is clicked", ->
47+
@container.simulate("mousedown") # open the drop
48+
expect(@container.hasClassName("chosen-container-active")).toBe true
49+
50+
it "should update the select's value when an option is clicked", ->
51+
@container.simulate("mousedown") # open the drop
52+
@container.select(".active-result").last().simulate("mouseup")
53+
value = $F(@select)
54+
expect(value).toBe "Afghanistan"
55+
56+
it "should re-copy the title from the select to the container on update", ->
57+
@select.writeAttribute('title', 'new title')
58+
@select.fire("chosen:updated")
59+
expect(@container.readAttribute('title')).toBe 'new title'
60+
61+
describe "When inherit selected classes is enabled", ->
62+
63+
beforeEach ->
64+
tmpl = "
65+
<select data-placeholder='Choose a Country...' title='Initial Title' class='TestClass'>
66+
<option value=''></option>
67+
<option value='United States'>United States</option>
68+
<option value='United Kingdom'>United Kingdom</option>
69+
<option value='Afghanistan'>Afghanistan</option>
70+
</select>
71+
"
72+
73+
@div = new Element("div")
74+
document.body.insert(@div)
75+
@div.update(tmpl)
76+
@select = @div.down("select")
77+
@chosen = new Chosen(@select, {inherit_select_classes: true})
78+
@container = @div.down(".chosen-container")
79+
80+
afterEach ->
81+
@chosen.destroy()
82+
@div.remove()
2883

29-
container = div.down(".chosen-container")
30-
container.simulate("mousedown") # open the drop
31-
expect(container.hasClassName("chosen-container-active")).toBe true
84+
it "should copy all classes from the select to the container", ->
85+
expect(@container.hasClassName('TestClass')).toBe true
3286

33-
#select an item
34-
container.select(".active-result").last().simulate("mouseup")
87+
it "should re-copy all classes from the select to the container on update", ->
88+
@select.addClassName('SecondClass')
89+
@select.fire("chosen:updated")
90+
# expect(@container.hasClassName('TestClass')).toBe true
91+
expect(@container.hasClassName('SecondClass')).toBe true
3592

36-
expect($F(select)).toBe "Afghanistan"
37-
div.remove()

0 commit comments

Comments
 (0)