default.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI Widget - Default functionality</title>
  6. <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
  7. <script src="../../jquery-1.10.2.js"></script>
  8. <script src="../../ui/jquery.ui.core.js"></script>
  9. <script src="../../ui/jquery.ui.position.js"></script>
  10. <script src="../../ui/jquery.ui.widget.js"></script>
  11. <script src="../../ui/jquery.ui.button.js"></script>
  12. <link rel="stylesheet" href="../demos.css">
  13. <style>
  14. .custom-colorize {
  15. font-size: 20px;
  16. position: relative;
  17. width: 75px;
  18. height: 75px;
  19. }
  20. .custom-colorize-changer {
  21. font-size: 10px;
  22. position: absolute;
  23. right: 0;
  24. bottom: 0;
  25. }
  26. </style>
  27. <script>
  28. $(function() {
  29. // the widget definition, where "custom" is the namespace,
  30. // "colorize" the widget name
  31. $.widget( "custom.colorize", {
  32. // default options
  33. options: {
  34. red: 255,
  35. green: 0,
  36. blue: 0,
  37. // callbacks
  38. change: null,
  39. random: null
  40. },
  41. // the constructor
  42. _create: function() {
  43. this.element
  44. // add a class for theming
  45. .addClass( "custom-colorize" )
  46. // prevent double click to select text
  47. .disableSelection();
  48. this.changer = $( "<button>", {
  49. text: "change",
  50. "class": "custom-colorize-changer"
  51. })
  52. .appendTo( this.element )
  53. .button();
  54. // bind click events on the changer button to the random method
  55. this._on( this.changer, {
  56. // _on won't call random when widget is disabled
  57. click: "random"
  58. });
  59. this._refresh();
  60. },
  61. // called when created, and later when changing options
  62. _refresh: function() {
  63. this.element.css( "background-color", "rgb(" +
  64. this.options.red +"," +
  65. this.options.green + "," +
  66. this.options.blue + ")"
  67. );
  68. // trigger a callback/event
  69. this._trigger( "change" );
  70. },
  71. // a public method to change the color to a random value
  72. // can be called directly via .colorize( "random" )
  73. random: function( event ) {
  74. var colors = {
  75. red: Math.floor( Math.random() * 256 ),
  76. green: Math.floor( Math.random() * 256 ),
  77. blue: Math.floor( Math.random() * 256 )
  78. };
  79. // trigger an event, check if it's canceled
  80. if ( this._trigger( "random", event, colors ) !== false ) {
  81. this.option( colors );
  82. }
  83. },
  84. // events bound via _on are removed automatically
  85. // revert other modifications here
  86. _destroy: function() {
  87. // remove generated elements
  88. this.changer.remove();
  89. this.element
  90. .removeClass( "custom-colorize" )
  91. .enableSelection()
  92. .css( "background-color", "transparent" );
  93. },
  94. // _setOptions is called with a hash of all options that are changing
  95. // always refresh when changing options
  96. _setOptions: function() {
  97. // _super and _superApply handle keeping the right this-context
  98. this._superApply( arguments );
  99. this._refresh();
  100. },
  101. // _setOption is called for each individual option that is changing
  102. _setOption: function( key, value ) {
  103. // prevent invalid color values
  104. if ( /red|green|blue/.test(key) && (value < 0 || value > 255) ) {
  105. return;
  106. }
  107. this._super( key, value );
  108. }
  109. });
  110. // initialize with default options
  111. $( "#my-widget1" ).colorize();
  112. // initialize with two customized options
  113. $( "#my-widget2" ).colorize({
  114. red: 60,
  115. blue: 60
  116. });
  117. // initialize with custom green value
  118. // and a random callback to allow only colors with enough green
  119. $( "#my-widget3" ).colorize( {
  120. green: 128,
  121. random: function( event, ui ) {
  122. return ui.green > 128;
  123. }
  124. });
  125. // click to toggle enabled/disabled
  126. $( "#disable" ).click(function() {
  127. // use the custom selector created for each widget to find all instances
  128. // all instances are toggled together, so we can check the state from the first
  129. if ( $( ":custom-colorize" ).colorize( "option", "disabled" ) ) {
  130. $( ":custom-colorize" ).colorize( "enable" );
  131. } else {
  132. $( ":custom-colorize" ).colorize( "disable" );
  133. }
  134. });
  135. // click to set options after initialization
  136. $( "#black" ).click( function() {
  137. $( ":custom-colorize" ).colorize( "option", {
  138. red: 0,
  139. green: 0,
  140. blue: 0
  141. });
  142. });
  143. });
  144. </script>
  145. </head>
  146. <body>
  147. <div>
  148. <div id="my-widget1">color me</div>
  149. <div id="my-widget2">color me</div>
  150. <div id="my-widget3">color me</div>
  151. <button id="disable">Toggle disabled option</button>
  152. <button id="black">Go black</button>
  153. </div>
  154. <div class="demo-description">
  155. <p>This demo shows a simple custom widget built using the widget factory (jquery.ui.widget.js).</p>
  156. <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>
  157. </div>
  158. </body>
  159. </html>