Skip to content

Commit 0e9f87f

Browse files
committed
Widget demo: Some improvements based on Scott's comments.
1 parent 8deb745 commit 0e9f87f

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

demos/widget/default.html

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
</style>
1919
<script>
2020
$(function() {
21-
// the widget definition, where 'custom' is the namespace,
22-
// 'colorize' the widget name
23-
$.widget( 'custom.colorize', {
21+
// the widget definition, where "custom" is the namespace,
22+
// "colorize" the widget name
23+
$.widget( "custom.colorize", {
2424
// default options
2525
options: {
2626
red: 255,
2727
green: 0,
28-
blue: 0
28+
blue: 0,
29+
30+
// callbacks
31+
random: null
2932
},
3033

3134
// the constructor
@@ -35,87 +38,96 @@
3538
.addClass("colorize")
3639
// prevent double click to select text
3740
.disableSelection();
41+
3842
// bind click events to random method
3943
this._bind({
40-
// _bind won't call random when widget is disabled
44+
// _bind won"t call random when widget is disabled
4145
click: "random"
4246
});
4347
this._refresh();
4448
},
4549

4650
// called when created, and later when changing options
4751
_refresh: function() {
48-
this.element.css( 'background-color', 'rgb(' +
49-
this.options.red +',' +
50-
this.options.green + ',' +
51-
this.options.blue + ')'
52+
this.element.css( "background-color", "rgb(" +
53+
this.options.red +"," +
54+
this.options.green + "," +
55+
this.options.blue + ")"
5256
);
5357

5458
// trigger a callback/event
55-
this._trigger( 'change' );
59+
this._trigger( "change" );
5660
},
5761

5862
// a public method to change the color to a random value
59-
// can be called directly via .colorize( 'random' )
63+
// can be called directly via .colorize( "random" )
6064
random: function( event ) {
6165
var colors = {
6266
red: Math.floor( Math.random() * 256 ),
6367
green: Math.floor( Math.random() * 256 ),
6468
blue: Math.floor( Math.random() * 256 )
6569
};
6670

67-
// trigger an event, check if it's canceled
68-
if ( this._trigger( 'random', event, colors ) !== false ) {
69-
$.extend( this.options, colors );
70-
this._refresh();
71+
// trigger an event, check if it"s canceled
72+
if ( this._trigger( "random", event, colors ) !== false ) {
73+
this.option( colors );
7174
}
7275
},
7376

7477
// events bound via _bind are removed automatically
7578
// revert other modifications here
7679
_destroy: function() {
7780
this.element
78-
.removeClass( 'colorize' )
81+
.removeClass( "colorize" )
7982
.enableSelection()
80-
.css( 'background-color', 'transparent' );
83+
.css( "background-color", "transparent" );
8184
},
8285

83-
// always refresh when changing an option
8486
_setOption: function( key, value ) {
87+
// prevent invalid color values
88+
if ( /red|green|blue/.test(key) && value < 0 || value > 255 ) {
89+
return;
90+
}
91+
this._super( "_setOptions", options );
92+
},
93+
94+
// always refresh when changing options
95+
_setOptions: function() {
8596
// _super handles keeping the right this-context
86-
this._super( "_setOption", key, value );
97+
this._superApply( "_setOptions", arguments );
8798
this._refresh();
8899
}
89100
});
90101

91102
// initialize with default options
92-
$( '#my-widget1' ).colorize();
103+
$( "#my-widget1" ).colorize();
93104

94105
// initialize with two customized options
95-
$( '#my-widget2' ).colorize({
106+
$( "#my-widget2" ).colorize({
96107
red: 60,
97108
blue: 60
98109
});
99110

100111
// initialize with custom green value
101112
// and a random callback to allow only colors with enough green
102-
$( '#my-widget3' ).colorize( {
113+
$( "#my-widget3" ).colorize( {
103114
green: 128,
104115
random: function( event, ui ) {
105116
return ui.green > 128;
106117
}
107118
});
108119

109120
// click to toggle enabled/disabled
110-
$( '#disable' ).toggle(function() {
111-
$( ':custom-colorize' ).colorize( 'disable' );
121+
$( "#disable" ).toggle(function() {
122+
// use the custom selector created for each widget to find all instances
123+
$( ":custom-colorize" ).colorize( "disable" );
112124
}, function() {
113-
$( ':custom-colorize' ).colorize( 'enable' );
125+
$( ":custom-colorize" ).colorize( "enable" );
114126
});
115127

116128
// click to set options after initalization
117-
$( '#black' ).click( function() {
118-
$( ':custom-colorize' ).colorize( 'option', {
129+
$( "#black" ).click( function() {
130+
$( ":custom-colorize" ).colorize( "option", {
119131
red: 0,
120132
green: 0,
121133
blue: 0
@@ -142,7 +154,7 @@
142154

143155
<div class="demo-description">
144156
<p>This demo shows a simple custom widget built using the widget factory (jquery.ui.widget.js).</p>
145-
<p>The three boxes are initialized in different ways. Clicking them changes their background color. View source to see how it works.</p>
157+
<p>The three boxes are initialized in different ways. Clicking them changes their background color. View source to see how it works, its heavily commented</p>
146158
<p><a href="http://wiki.jqueryui.com/w/page/12138135/Widget-factory">For more details on the widget factory, visit the jQuery UI planning wiki.</a></p>
147159
</div><!-- End demo-description -->
148160

0 commit comments

Comments
 (0)