|
18 | 18 | </style>
|
19 | 19 | <script>
|
20 | 20 | $(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", { |
24 | 24 | // default options
|
25 | 25 | options: {
|
26 | 26 | red: 255,
|
27 | 27 | green: 0,
|
28 |
| - blue: 0 |
| 28 | + blue: 0, |
| 29 | + |
| 30 | + // callbacks |
| 31 | + random: null |
29 | 32 | },
|
30 | 33 |
|
31 | 34 | // the constructor
|
|
35 | 38 | .addClass("colorize")
|
36 | 39 | // prevent double click to select text
|
37 | 40 | .disableSelection();
|
| 41 | + |
38 | 42 | // bind click events to random method
|
39 | 43 | this._bind({
|
40 |
| - // _bind won't call random when widget is disabled |
| 44 | + // _bind won"t call random when widget is disabled |
41 | 45 | click: "random"
|
42 | 46 | });
|
43 | 47 | this._refresh();
|
44 | 48 | },
|
45 | 49 |
|
46 | 50 | // called when created, and later when changing options
|
47 | 51 | _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 + ")" |
52 | 56 | );
|
53 | 57 |
|
54 | 58 | // trigger a callback/event
|
55 |
| - this._trigger( 'change' ); |
| 59 | + this._trigger( "change" ); |
56 | 60 | },
|
57 | 61 |
|
58 | 62 | // 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" ) |
60 | 64 | random: function( event ) {
|
61 | 65 | var colors = {
|
62 | 66 | red: Math.floor( Math.random() * 256 ),
|
63 | 67 | green: Math.floor( Math.random() * 256 ),
|
64 | 68 | blue: Math.floor( Math.random() * 256 )
|
65 | 69 | };
|
66 | 70 |
|
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 ); |
71 | 74 | }
|
72 | 75 | },
|
73 | 76 |
|
74 | 77 | // events bound via _bind are removed automatically
|
75 | 78 | // revert other modifications here
|
76 | 79 | _destroy: function() {
|
77 | 80 | this.element
|
78 |
| - .removeClass( 'colorize' ) |
| 81 | + .removeClass( "colorize" ) |
79 | 82 | .enableSelection()
|
80 |
| - .css( 'background-color', 'transparent' ); |
| 83 | + .css( "background-color", "transparent" ); |
81 | 84 | },
|
82 | 85 |
|
83 |
| - // always refresh when changing an option |
84 | 86 | _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() { |
85 | 96 | // _super handles keeping the right this-context
|
86 |
| - this._super( "_setOption", key, value ); |
| 97 | + this._superApply( "_setOptions", arguments ); |
87 | 98 | this._refresh();
|
88 | 99 | }
|
89 | 100 | });
|
90 | 101 |
|
91 | 102 | // initialize with default options
|
92 |
| - $( '#my-widget1' ).colorize(); |
| 103 | + $( "#my-widget1" ).colorize(); |
93 | 104 |
|
94 | 105 | // initialize with two customized options
|
95 |
| - $( '#my-widget2' ).colorize({ |
| 106 | + $( "#my-widget2" ).colorize({ |
96 | 107 | red: 60,
|
97 | 108 | blue: 60
|
98 | 109 | });
|
99 | 110 |
|
100 | 111 | // initialize with custom green value
|
101 | 112 | // and a random callback to allow only colors with enough green
|
102 |
| - $( '#my-widget3' ).colorize( { |
| 113 | + $( "#my-widget3" ).colorize( { |
103 | 114 | green: 128,
|
104 | 115 | random: function( event, ui ) {
|
105 | 116 | return ui.green > 128;
|
106 | 117 | }
|
107 | 118 | });
|
108 | 119 |
|
109 | 120 | // 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" ); |
112 | 124 | }, function() {
|
113 |
| - $( ':custom-colorize' ).colorize( 'enable' ); |
| 125 | + $( ":custom-colorize" ).colorize( "enable" ); |
114 | 126 | });
|
115 | 127 |
|
116 | 128 | // click to set options after initalization
|
117 |
| - $( '#black' ).click( function() { |
118 |
| - $( ':custom-colorize' ).colorize( 'option', { |
| 129 | + $( "#black" ).click( function() { |
| 130 | + $( ":custom-colorize" ).colorize( "option", { |
119 | 131 | red: 0,
|
120 | 132 | green: 0,
|
121 | 133 | blue: 0
|
|
142 | 154 |
|
143 | 155 | <div class="demo-description">
|
144 | 156 | <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> |
146 | 158 | <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>
|
147 | 159 | </div><!-- End demo-description -->
|
148 | 160 |
|
|
0 commit comments