Skip to content

Commit 7fb6ca1

Browse files
adambaratzscottgonzalez
authored andcommitted
Slider: add handles in single DOM operation. Fixed #7259 - optimize handle creation.
1 parent 98fcb47 commit 7fb6ca1

File tree

1 file changed

+16
-34
lines changed

1 file changed

+16
-34
lines changed

ui/jquery.ui.slider.js

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ $.widget( "ui.slider", $.ui.mouse, {
3636

3737
_create: function() {
3838
var self = this,
39-
o = this.options;
39+
o = this.options,
40+
existingHandles = this.element.find( ".ui-slider-handle" ).addClass( "ui-state-default ui-corner-all" ),
41+
handle = "<a class='ui-slider-handle ui-state-default ui-corner-all' href='#'></a>",
42+
handleCount = ( o.values && o.values.length ) || 1,
43+
handles = [];
4044

4145
this._keySliding = false;
4246
this._mouseSliding = false;
@@ -50,57 +54,35 @@ $.widget( "ui.slider", $.ui.mouse, {
5054
" ui-slider-" + this.orientation +
5155
" ui-widget" +
5256
" ui-widget-content" +
53-
" ui-corner-all" );
54-
55-
if ( o.disabled ) {
56-
this.element.addClass( "ui-slider-disabled ui-disabled" );
57-
}
57+
" ui-corner-all" +
58+
( o.disabled ? " ui-slider-disabled ui-disabled" : "" ) );
5859

5960
this.range = $([]);
6061

6162
if ( o.range ) {
6263
if ( o.range === true ) {
63-
this.range = $( "<div></div>" );
6464
if ( !o.values ) {
6565
o.values = [ this._valueMin(), this._valueMin() ];
6666
}
6767
if ( o.values.length && o.values.length !== 2 ) {
6868
o.values = [ o.values[0], o.values[0] ];
6969
}
70-
} else {
71-
this.range = $( "<div></div>" );
72-
}
73-
74-
this.range
75-
.appendTo( this.element )
76-
.addClass( "ui-slider-range" );
77-
78-
if ( o.range === "min" || o.range === "max" ) {
79-
this.range.addClass( "ui-slider-range-" + o.range );
8070
}
8171

82-
// note: this isn't the most fittingly semantic framework class for this element,
83-
// but worked best visually with a variety of themes
84-
this.range.addClass( "ui-widget-header" );
85-
}
86-
87-
if ( $( ".ui-slider-handle", this.element ).length === 0 ) {
88-
$( "<a href='#'></a>" )
72+
this.range = $( "<div></div>" )
8973
.appendTo( this.element )
90-
.addClass( "ui-slider-handle" );
74+
.addClass( "ui-slider-range" +
75+
// note: this isn't the most fittingly semantic framework class for this element,
76+
// but worked best visually with a variety of themes
77+
" ui-widget-header" +
78+
( ( o.range === "min" || o.range === "max" ) ? " ui-slider-range-" + o.range : "" ) );
9179
}
9280

93-
if ( o.values && o.values.length ) {
94-
while ( $(".ui-slider-handle", this.element).length < o.values.length ) {
95-
$( "<a href='#'></a>" )
96-
.appendTo( this.element )
97-
.addClass( "ui-slider-handle" );
98-
}
81+
for ( var i = existingHandles.length; i < handleCount; i += 1 ) {
82+
handles.push( handle );
9983
}
10084

101-
this.handles = $( ".ui-slider-handle", this.element )
102-
.addClass( "ui-state-default" +
103-
" ui-corner-all" );
85+
this.handles = existingHandles.add( $( handles.join( "" ) ).appendTo( self.element ) );
10486

10587
this.handle = this.handles.eq( 0 );
10688

0 commit comments

Comments
 (0)