PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugs/ui/jquery/colorpicker/static/colorpicker/jquery.colorPicker.js

http://plugs.googlecode.com/
JavaScript | 307 lines | 162 code | 59 blank | 86 comment | 17 complexity | d8493bbe6eeea19752f1b89416fa455e MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause
  1. /**
  2. * Really Simple Color Picker in jQuery
  3. *
  4. * Licensed under the MIT (MIT-LICENSE.txt) licenses.
  5. *
  6. * Copyright (c) 2008 Lakshan Perera (www.laktek.com)
  7. * Daniel Lacy (daniellacy.com)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to
  11. * deal in the Software without restriction, including without limitation the
  12. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  13. * sell copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  25. * IN THE SOFTWARE.
  26. */
  27. (function ($) {
  28. /**
  29. * Create a couple private variables.
  30. **/
  31. var selectorOwner,
  32. activePalette,
  33. cItterate = 0,
  34. templates = {
  35. control : $('<div class="colorPicker-picker">&nbsp;</div>'),
  36. palette : $('<div id="colorPicker_palette" class="colorPicker-palette" />'),
  37. swatch : $('<div class="colorPicker-swatch">&nbsp;</div>'),
  38. hexLabel: $('<label for="colorPicker_hex">Hex</label>'),
  39. hexField: $('<input type="text" id="colorPicker_hex" />')
  40. },
  41. transparent = "transparent",
  42. lastColor;
  43. /**
  44. * Create our colorPicker function
  45. **/
  46. $.fn.colorPicker = function (options) {
  47. return this.each(function () {
  48. // Setup time. Clone new elements from our templates, set some IDs, make shortcuts, jazzercise.
  49. var element = $(this),
  50. opts = $.extend({}, $.fn.colorPicker.defaults, options),
  51. defaultColor = $.fn.colorPicker.toHex(
  52. (element.val().length > 0) ? element.val() : opts.pickerDefault
  53. ),
  54. newControl = templates.control.clone(),
  55. newPalette = templates.palette.clone().attr('id', 'colorPicker_palette-' + cItterate),
  56. newHexLabel = templates.hexLabel.clone(),
  57. newHexField = templates.hexField.clone(),
  58. paletteId = newPalette[0].id,
  59. swatch;
  60. /**
  61. * Build a color palette.
  62. **/
  63. $.each(opts.colors, function (i) {
  64. swatch = templates.swatch.clone();
  65. if (opts.colors[i] === transparent) {
  66. swatch.addClass(transparent).text('X');
  67. $.fn.colorPicker.bindPalette(newHexField, swatch, transparent);
  68. } else {
  69. swatch.css("background-color", "#" + this);
  70. $.fn.colorPicker.bindPalette(newHexField, swatch);
  71. }
  72. swatch.appendTo(newPalette);
  73. });
  74. newHexLabel.attr('for', 'colorPicker_hex-' + cItterate);
  75. newHexField.attr({
  76. 'id' : 'colorPicker_hex-' + cItterate,
  77. 'value' : defaultColor
  78. });
  79. newHexField.bind("keydown", function (event) {
  80. if (event.keyCode === 13) {
  81. $.fn.colorPicker.changeColor($.fn.colorPicker.toHex($(this).val()));
  82. }
  83. if (event.keyCode === 27) {
  84. $.fn.colorPicker.hidePalette(paletteId);
  85. }
  86. });
  87. $('<div class="colorPicker_hexWrap" />').append(newHexLabel).appendTo(newPalette);
  88. newPalette.find('.colorPicker_hexWrap').append(newHexField);
  89. $("body").append(newPalette);
  90. newPalette.hide();
  91. /**
  92. * Build replacement interface for original color input.
  93. **/
  94. newControl.css("background-color", defaultColor);
  95. newControl.bind("click", function () {
  96. $.fn.colorPicker.togglePalette($('#' + paletteId), $(this));
  97. });
  98. element.after(newControl);
  99. element.bind("change", function () {
  100. element.next(".colorPicker-picker").css(
  101. "background-color", $.fn.colorPicker.toHex($(this).val())
  102. );
  103. });
  104. // Hide the original input.
  105. element.val(defaultColor).hide();
  106. cItterate++;
  107. });
  108. };
  109. /**
  110. * Extend colorPicker with... all our functionality.
  111. **/
  112. $.extend(true, $.fn.colorPicker, {
  113. /**
  114. * Return a Hex color, convert an RGB value and return Hex, or return false.
  115. *
  116. * Inspired by http://code.google.com/p/jquery-color-utils
  117. **/
  118. toHex : function (color) {
  119. // If we have a standard or shorthand Hex color, return that value.
  120. if (color.match(/[0-9A-F]{6}|[0-9A-F]{3}$/i)) {
  121. return (color.charAt(0) === "#") ? color : ("#" + color);
  122. // Alternatively, check for RGB color, then convert and return it as Hex.
  123. } else if (color.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/)) {
  124. var c = ([parseInt(RegExp.$1, 10), parseInt(RegExp.$2, 10), parseInt(RegExp.$3, 10)]),
  125. pad = function (str) {
  126. if (str.length < 2) {
  127. for (var i = 0, len = 2 - str.length; i < len; i++) {
  128. str = '0' + str;
  129. }
  130. }
  131. return str;
  132. };
  133. if (c.length === 3) {
  134. var r = pad(c[0].toString(16)),
  135. g = pad(c[1].toString(16)),
  136. b = pad(c[2].toString(16));
  137. return '#' + r + g + b;
  138. }
  139. // Otherwise we wont do anything.
  140. } else {
  141. return false;
  142. }
  143. },
  144. /**
  145. * Check whether user clicked on the selector or owner.
  146. **/
  147. checkMouse : function (event, paletteId) {
  148. var selector = activePalette,
  149. selectorParent = $(event.target).parents("#" + selector.attr('id')).length;
  150. if (event.target === $(selector)[0] || event.target === selectorOwner || selectorParent > 0) {
  151. return;
  152. }
  153. $.fn.colorPicker.hidePalette();
  154. },
  155. /**
  156. * Hide the color palette modal.
  157. **/
  158. hidePalette : function (paletteId) {
  159. $(document).unbind("mousedown", $.fn.colorPicker.checkMouse);
  160. $('.colorPicker-palette').hide();
  161. },
  162. /**
  163. * Show the color palette modal.
  164. **/
  165. showPalette : function (palette) {
  166. var hexColor = selectorOwner.prev("input").val();
  167. palette.css({
  168. top: selectorOwner.offset().top + (selectorOwner.outerHeight()),
  169. left: selectorOwner.offset().left
  170. });
  171. $("#color_value").val(hexColor);
  172. palette.show();
  173. $(document).bind("mousedown", $.fn.colorPicker.checkMouse);
  174. },
  175. /**
  176. * Toggle visibility of the colorPicker palette.
  177. **/
  178. togglePalette : function (palette, origin) {
  179. // selectorOwner is the clicked .colorPicker-picker.
  180. if (origin) {
  181. selectorOwner = origin;
  182. }
  183. activePalette = palette;
  184. if (activePalette.is(':visible')) {
  185. $.fn.colorPicker.hidePalette();
  186. } else {
  187. $.fn.colorPicker.showPalette(palette);
  188. }
  189. },
  190. /**
  191. * Update the input with a newly selected color.
  192. **/
  193. changeColor : function (value) {
  194. selectorOwner.css("background-color", value);
  195. selectorOwner.prev("input").val(value).change();
  196. $.fn.colorPicker.hidePalette();
  197. },
  198. /**
  199. * Bind events to the color palette swatches.
  200. */
  201. bindPalette : function (paletteInput, element, color) {
  202. color = color ? color : $.fn.colorPicker.toHex(element.css("background-color"));
  203. element.bind({
  204. click : function (ev) {
  205. lastColor = color;
  206. $.fn.colorPicker.changeColor(color);
  207. },
  208. mouseover : function (ev) {
  209. lastColor = paletteInput.val();
  210. $(this).css("border-color", "#598FEF");
  211. paletteInput.val(color);
  212. },
  213. mouseout : function (ev) {
  214. $(this).css("border-color", "#000");
  215. paletteInput.val(selectorOwner.css("background-color"));
  216. paletteInput.val(lastColor);
  217. }
  218. });
  219. }
  220. });
  221. /**
  222. * Default colorPicker options.
  223. *
  224. * These are publibly available for global modification using a setting such as:
  225. *
  226. * $.fn.colorPicker.defaults.colors = ['151337', '111111']
  227. *
  228. * They can also be applied on a per-bound element basis like so:
  229. *
  230. * $('#element1').colorPicker({pickerDefault: 'efefef', transparency: true});
  231. * $('#element2').colorPicker({pickerDefault: '333333', colors: ['333333', '111111']});
  232. *
  233. **/
  234. $.fn.colorPicker.defaults = {
  235. // colorPicker default selected color.
  236. pickerDefault : "FFFFFF",
  237. // Default color set.
  238. colors : [
  239. '000000', '993300', '333300', '000080', '333399', '333333', '800000', 'FF6600',
  240. '808000', '008000', '008080', '0000FF', '666699', '808080', 'FF0000', 'FF9900',
  241. '99CC00', '339966', '33CCCC', '3366FF', '800080', '999999', 'FF00FF', 'FFCC00',
  242. 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0', 'FF99CC', 'FFCC99',
  243. 'FFFF99', 'CCFFFF', '99CCFF', 'FFFFFF'
  244. ],
  245. // If we want to simply add more colors to the default set, use addColors.
  246. addColors : []
  247. };
  248. })(jQuery);