PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/public/plugins/colorpicker/bootstrap-colorpicker.js

https://gitlab.com/betanurlaila/UI_onlineshop
JavaScript | 1079 lines | 1011 code | 33 blank | 35 comment | 159 complexity | 503e036aa0d83420d63b63aa80a4c906 MD5 | raw file
  1. /*!
  2. * Bootstrap Colorpicker
  3. * http://mjolnic.github.io/bootstrap-colorpicker/
  4. *
  5. * Originally written by (c) 2012 Stefan Petre
  6. * Licensed under the Apache License v2.0
  7. * http://www.apache.org/licenses/LICENSE-2.0.txt
  8. *
  9. * @todo Update DOCS
  10. */
  11. (function(factory) {
  12. "use strict";
  13. if (typeof exports === 'object') {
  14. module.exports = factory(window.jQuery);
  15. } else if (typeof define === 'function' && define.amd) {
  16. define(['jquery'], factory);
  17. } else if (window.jQuery && !window.jQuery.fn.colorpicker) {
  18. factory(window.jQuery);
  19. }
  20. }
  21. (function($) {
  22. 'use strict';
  23. // Color object
  24. var Color = function(val, customColors) {
  25. this.value = {
  26. h: 0,
  27. s: 0,
  28. b: 0,
  29. a: 1
  30. };
  31. this.origFormat = null; // original string format
  32. if (customColors) {
  33. $.extend(this.colors, customColors);
  34. }
  35. if (val) {
  36. if (val.toLowerCase !== undefined) {
  37. // cast to string
  38. val = val + '';
  39. this.setColor(val);
  40. } else if (val.h !== undefined) {
  41. this.value = val;
  42. }
  43. }
  44. };
  45. Color.prototype = {
  46. constructor: Color,
  47. // 140 predefined colors from the HTML Colors spec
  48. colors: {
  49. "aliceblue": "#f0f8ff",
  50. "antiquewhite": "#faebd7",
  51. "aqua": "#00ffff",
  52. "aquamarine": "#7fffd4",
  53. "azure": "#f0ffff",
  54. "beige": "#f5f5dc",
  55. "bisque": "#ffe4c4",
  56. "black": "#000000",
  57. "blanchedalmond": "#ffebcd",
  58. "blue": "#0000ff",
  59. "blueviolet": "#8a2be2",
  60. "brown": "#a52a2a",
  61. "burlywood": "#deb887",
  62. "cadetblue": "#5f9ea0",
  63. "chartreuse": "#7fff00",
  64. "chocolate": "#d2691e",
  65. "coral": "#ff7f50",
  66. "cornflowerblue": "#6495ed",
  67. "cornsilk": "#fff8dc",
  68. "crimson": "#dc143c",
  69. "cyan": "#00ffff",
  70. "darkblue": "#00008b",
  71. "darkcyan": "#008b8b",
  72. "darkgoldenrod": "#b8860b",
  73. "darkgray": "#a9a9a9",
  74. "darkgreen": "#006400",
  75. "darkkhaki": "#bdb76b",
  76. "darkmagenta": "#8b008b",
  77. "darkolivegreen": "#556b2f",
  78. "darkorange": "#ff8c00",
  79. "darkorchid": "#9932cc",
  80. "darkred": "#8b0000",
  81. "darksalmon": "#e9967a",
  82. "darkseagreen": "#8fbc8f",
  83. "darkslateblue": "#483d8b",
  84. "darkslategray": "#2f4f4f",
  85. "darkturquoise": "#00ced1",
  86. "darkviolet": "#9400d3",
  87. "deeppink": "#ff1493",
  88. "deepskyblue": "#00bfff",
  89. "dimgray": "#696969",
  90. "dodgerblue": "#1e90ff",
  91. "firebrick": "#b22222",
  92. "floralwhite": "#fffaf0",
  93. "forestgreen": "#228b22",
  94. "fuchsia": "#ff00ff",
  95. "gainsboro": "#dcdcdc",
  96. "ghostwhite": "#f8f8ff",
  97. "gold": "#ffd700",
  98. "goldenrod": "#daa520",
  99. "gray": "#808080",
  100. "green": "#008000",
  101. "greenyellow": "#adff2f",
  102. "honeydew": "#f0fff0",
  103. "hotpink": "#ff69b4",
  104. "indianred": "#cd5c5c",
  105. "indigo": "#4b0082",
  106. "ivory": "#fffff0",
  107. "khaki": "#f0e68c",
  108. "lavender": "#e6e6fa",
  109. "lavenderblush": "#fff0f5",
  110. "lawngreen": "#7cfc00",
  111. "lemonchiffon": "#fffacd",
  112. "lightblue": "#add8e6",
  113. "lightcoral": "#f08080",
  114. "lightcyan": "#e0ffff",
  115. "lightgoldenrodyellow": "#fafad2",
  116. "lightgrey": "#d3d3d3",
  117. "lightgreen": "#90ee90",
  118. "lightpink": "#ffb6c1",
  119. "lightsalmon": "#ffa07a",
  120. "lightseagreen": "#20b2aa",
  121. "lightskyblue": "#87cefa",
  122. "lightslategray": "#778899",
  123. "lightsteelblue": "#b0c4de",
  124. "lightyellow": "#ffffe0",
  125. "lime": "#00ff00",
  126. "limegreen": "#32cd32",
  127. "linen": "#faf0e6",
  128. "magenta": "#ff00ff",
  129. "maroon": "#800000",
  130. "mediumaquamarine": "#66cdaa",
  131. "mediumblue": "#0000cd",
  132. "mediumorchid": "#ba55d3",
  133. "mediumpurple": "#9370d8",
  134. "mediumseagreen": "#3cb371",
  135. "mediumslateblue": "#7b68ee",
  136. "mediumspringgreen": "#00fa9a",
  137. "mediumturquoise": "#48d1cc",
  138. "mediumvioletred": "#c71585",
  139. "midnightblue": "#191970",
  140. "mintcream": "#f5fffa",
  141. "mistyrose": "#ffe4e1",
  142. "moccasin": "#ffe4b5",
  143. "navajowhite": "#ffdead",
  144. "navy": "#000080",
  145. "oldlace": "#fdf5e6",
  146. "olive": "#808000",
  147. "olivedrab": "#6b8e23",
  148. "orange": "#ffa500",
  149. "orangered": "#ff4500",
  150. "orchid": "#da70d6",
  151. "palegoldenrod": "#eee8aa",
  152. "palegreen": "#98fb98",
  153. "paleturquoise": "#afeeee",
  154. "palevioletred": "#d87093",
  155. "papayawhip": "#ffefd5",
  156. "peachpuff": "#ffdab9",
  157. "peru": "#cd853f",
  158. "pink": "#ffc0cb",
  159. "plum": "#dda0dd",
  160. "powderblue": "#b0e0e6",
  161. "purple": "#800080",
  162. "red": "#ff0000",
  163. "rosybrown": "#bc8f8f",
  164. "royalblue": "#4169e1",
  165. "saddlebrown": "#8b4513",
  166. "salmon": "#fa8072",
  167. "sandybrown": "#f4a460",
  168. "seagreen": "#2e8b57",
  169. "seashell": "#fff5ee",
  170. "sienna": "#a0522d",
  171. "silver": "#c0c0c0",
  172. "skyblue": "#87ceeb",
  173. "slateblue": "#6a5acd",
  174. "slategray": "#708090",
  175. "snow": "#fffafa",
  176. "springgreen": "#00ff7f",
  177. "steelblue": "#4682b4",
  178. "tan": "#d2b48c",
  179. "teal": "#008080",
  180. "thistle": "#d8bfd8",
  181. "tomato": "#ff6347",
  182. "turquoise": "#40e0d0",
  183. "violet": "#ee82ee",
  184. "wheat": "#f5deb3",
  185. "white": "#ffffff",
  186. "whitesmoke": "#f5f5f5",
  187. "yellow": "#ffff00",
  188. "yellowgreen": "#9acd32",
  189. "transparent": "transparent"
  190. },
  191. _sanitizeNumber: function(val) {
  192. if (typeof val === 'number') {
  193. return val;
  194. }
  195. if (isNaN(val) || (val === null) || (val === '') || (val === undefined)) {
  196. return 1;
  197. }
  198. if (val.toLowerCase !== undefined) {
  199. return parseFloat(val);
  200. }
  201. return 1;
  202. },
  203. isTransparent: function(strVal) {
  204. if (!strVal) {
  205. return false;
  206. }
  207. strVal = strVal.toLowerCase().trim();
  208. return (strVal === 'transparent') || (strVal.match(/#?00000000/)) || (strVal.match(/(rgba|hsla)\(0,0,0,0?\.?0\)/));
  209. },
  210. rgbaIsTransparent: function(rgba) {
  211. return ((rgba.r === 0) && (rgba.g === 0) && (rgba.b === 0) && (rgba.a === 0));
  212. },
  213. //parse a string to HSB
  214. setColor: function(strVal) {
  215. strVal = strVal.toLowerCase().trim();
  216. if (strVal) {
  217. if (this.isTransparent(strVal)) {
  218. this.value = {
  219. h: 0,
  220. s: 0,
  221. b: 0,
  222. a: 0
  223. };
  224. } else {
  225. this.value = this.stringToHSB(strVal) || {
  226. h: 0,
  227. s: 0,
  228. b: 0,
  229. a: 1
  230. }; // if parser fails, defaults to black
  231. }
  232. }
  233. },
  234. stringToHSB: function(strVal) {
  235. strVal = strVal.toLowerCase();
  236. var alias;
  237. if (typeof this.colors[strVal] !== 'undefined') {
  238. strVal = this.colors[strVal];
  239. alias = 'alias';
  240. }
  241. var that = this,
  242. result = false;
  243. $.each(this.stringParsers, function(i, parser) {
  244. var match = parser.re.exec(strVal),
  245. values = match && parser.parse.apply(that, [match]),
  246. format = alias || parser.format || 'rgba';
  247. if (values) {
  248. if (format.match(/hsla?/)) {
  249. result = that.RGBtoHSB.apply(that, that.HSLtoRGB.apply(that, values));
  250. } else {
  251. result = that.RGBtoHSB.apply(that, values);
  252. }
  253. that.origFormat = format;
  254. return false;
  255. }
  256. return true;
  257. });
  258. return result;
  259. },
  260. setHue: function(h) {
  261. this.value.h = 1 - h;
  262. },
  263. setSaturation: function(s) {
  264. this.value.s = s;
  265. },
  266. setBrightness: function(b) {
  267. this.value.b = 1 - b;
  268. },
  269. setAlpha: function(a) {
  270. this.value.a = parseInt((1 - a) * 100, 10) / 100;
  271. },
  272. toRGB: function(h, s, b, a) {
  273. if (!h) {
  274. h = this.value.h;
  275. s = this.value.s;
  276. b = this.value.b;
  277. }
  278. h *= 360;
  279. var R, G, B, X, C;
  280. h = (h % 360) / 60;
  281. C = b * s;
  282. X = C * (1 - Math.abs(h % 2 - 1));
  283. R = G = B = b - C;
  284. h = ~~h;
  285. R += [C, X, 0, 0, X, C][h];
  286. G += [X, C, C, X, 0, 0][h];
  287. B += [0, 0, X, C, C, X][h];
  288. return {
  289. r: Math.round(R * 255),
  290. g: Math.round(G * 255),
  291. b: Math.round(B * 255),
  292. a: a || this.value.a
  293. };
  294. },
  295. toHex: function(h, s, b, a) {
  296. var rgb = this.toRGB(h, s, b, a);
  297. if (this.rgbaIsTransparent(rgb)) {
  298. return 'transparent';
  299. }
  300. return '#' + ((1 << 24) | (parseInt(rgb.r) << 16) | (parseInt(rgb.g) << 8) | parseInt(rgb.b)).toString(16).substr(1);
  301. },
  302. toHSL: function(h, s, b, a) {
  303. h = h || this.value.h;
  304. s = s || this.value.s;
  305. b = b || this.value.b;
  306. a = a || this.value.a;
  307. var H = h,
  308. L = (2 - s) * b,
  309. S = s * b;
  310. if (L > 0 && L <= 1) {
  311. S /= L;
  312. } else {
  313. S /= 2 - L;
  314. }
  315. L /= 2;
  316. if (S > 1) {
  317. S = 1;
  318. }
  319. return {
  320. h: isNaN(H) ? 0 : H,
  321. s: isNaN(S) ? 0 : S,
  322. l: isNaN(L) ? 0 : L,
  323. a: isNaN(a) ? 0 : a
  324. };
  325. },
  326. toAlias: function(r, g, b, a) {
  327. var rgb = this.toHex(r, g, b, a);
  328. for (var alias in this.colors) {
  329. if (this.colors[alias] === rgb) {
  330. return alias;
  331. }
  332. }
  333. return false;
  334. },
  335. RGBtoHSB: function(r, g, b, a) {
  336. r /= 255;
  337. g /= 255;
  338. b /= 255;
  339. var H, S, V, C;
  340. V = Math.max(r, g, b);
  341. C = V - Math.min(r, g, b);
  342. H = (C === 0 ? null :
  343. V === r ? (g - b) / C :
  344. V === g ? (b - r) / C + 2 :
  345. (r - g) / C + 4
  346. );
  347. H = ((H + 360) % 6) * 60 / 360;
  348. S = C === 0 ? 0 : C / V;
  349. return {
  350. h: this._sanitizeNumber(H),
  351. s: S,
  352. b: V,
  353. a: this._sanitizeNumber(a)
  354. };
  355. },
  356. HueToRGB: function(p, q, h) {
  357. if (h < 0) {
  358. h += 1;
  359. } else if (h > 1) {
  360. h -= 1;
  361. }
  362. if ((h * 6) < 1) {
  363. return p + (q - p) * h * 6;
  364. } else if ((h * 2) < 1) {
  365. return q;
  366. } else if ((h * 3) < 2) {
  367. return p + (q - p) * ((2 / 3) - h) * 6;
  368. } else {
  369. return p;
  370. }
  371. },
  372. HSLtoRGB: function(h, s, l, a) {
  373. if (s < 0) {
  374. s = 0;
  375. }
  376. var q;
  377. if (l <= 0.5) {
  378. q = l * (1 + s);
  379. } else {
  380. q = l + s - (l * s);
  381. }
  382. var p = 2 * l - q;
  383. var tr = h + (1 / 3);
  384. var tg = h;
  385. var tb = h - (1 / 3);
  386. var r = Math.round(this.HueToRGB(p, q, tr) * 255);
  387. var g = Math.round(this.HueToRGB(p, q, tg) * 255);
  388. var b = Math.round(this.HueToRGB(p, q, tb) * 255);
  389. return [r, g, b, this._sanitizeNumber(a)];
  390. },
  391. toString: function(format) {
  392. format = format || 'rgba';
  393. var c = false;
  394. switch (format) {
  395. case 'rgb':
  396. {
  397. c = this.toRGB();
  398. if (this.rgbaIsTransparent(c)) {
  399. return 'transparent';
  400. }
  401. return 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')';
  402. }
  403. break;
  404. case 'rgba':
  405. {
  406. c = this.toRGB();
  407. return 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',' + c.a + ')';
  408. }
  409. break;
  410. case 'hsl':
  411. {
  412. c = this.toHSL();
  413. return 'hsl(' + Math.round(c.h * 360) + ',' + Math.round(c.s * 100) + '%,' + Math.round(c.l * 100) + '%)';
  414. }
  415. break;
  416. case 'hsla':
  417. {
  418. c = this.toHSL();
  419. return 'hsla(' + Math.round(c.h * 360) + ',' + Math.round(c.s * 100) + '%,' + Math.round(c.l * 100) + '%,' + c.a + ')';
  420. }
  421. break;
  422. case 'hex':
  423. {
  424. return this.toHex();
  425. }
  426. break;
  427. case 'alias':
  428. return this.toAlias() || this.toHex();
  429. default:
  430. {
  431. return c;
  432. }
  433. break;
  434. }
  435. },
  436. // a set of RE's that can match strings and generate color tuples.
  437. // from John Resig color plugin
  438. // https://github.com/jquery/jquery-color/
  439. stringParsers: [{
  440. re: /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*?\)/,
  441. format: 'rgb',
  442. parse: function(execResult) {
  443. return [
  444. execResult[1],
  445. execResult[2],
  446. execResult[3],
  447. 1
  448. ];
  449. }
  450. }, {
  451. re: /rgb\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*?\)/,
  452. format: 'rgb',
  453. parse: function(execResult) {
  454. return [
  455. 2.55 * execResult[1],
  456. 2.55 * execResult[2],
  457. 2.55 * execResult[3],
  458. 1
  459. ];
  460. }
  461. }, {
  462. re: /rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
  463. format: 'rgba',
  464. parse: function(execResult) {
  465. return [
  466. execResult[1],
  467. execResult[2],
  468. execResult[3],
  469. execResult[4]
  470. ];
  471. }
  472. }, {
  473. re: /rgba\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
  474. format: 'rgba',
  475. parse: function(execResult) {
  476. return [
  477. 2.55 * execResult[1],
  478. 2.55 * execResult[2],
  479. 2.55 * execResult[3],
  480. execResult[4]
  481. ];
  482. }
  483. }, {
  484. re: /hsl\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*?\)/,
  485. format: 'hsl',
  486. parse: function(execResult) {
  487. return [
  488. execResult[1] / 360,
  489. execResult[2] / 100,
  490. execResult[3] / 100,
  491. execResult[4]
  492. ];
  493. }
  494. }, {
  495. re: /hsla\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
  496. format: 'hsla',
  497. parse: function(execResult) {
  498. return [
  499. execResult[1] / 360,
  500. execResult[2] / 100,
  501. execResult[3] / 100,
  502. execResult[4]
  503. ];
  504. }
  505. }, {
  506. re: /#?([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
  507. format: 'hex',
  508. parse: function(execResult) {
  509. return [
  510. parseInt(execResult[1], 16),
  511. parseInt(execResult[2], 16),
  512. parseInt(execResult[3], 16),
  513. 1
  514. ];
  515. }
  516. }, {
  517. re: /#?([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/,
  518. format: 'hex',
  519. parse: function(execResult) {
  520. return [
  521. parseInt(execResult[1] + execResult[1], 16),
  522. parseInt(execResult[2] + execResult[2], 16),
  523. parseInt(execResult[3] + execResult[3], 16),
  524. 1
  525. ];
  526. }
  527. }],
  528. colorNameToHex: function(name) {
  529. if (typeof this.colors[name.toLowerCase()] !== 'undefined') {
  530. return this.colors[name.toLowerCase()];
  531. }
  532. return false;
  533. }
  534. };
  535. var defaults = {
  536. horizontal: false, // horizontal mode layout ?
  537. inline: false, //forces to show the colorpicker as an inline element
  538. color: false, //forces a color
  539. format: false, //forces a format
  540. input: 'input', // children input selector
  541. container: false, // container selector
  542. component: '.add-on, .input-group-addon', // children component selector
  543. sliders: {
  544. saturation: {
  545. maxLeft: 100,
  546. maxTop: 100,
  547. callLeft: 'setSaturation',
  548. callTop: 'setBrightness'
  549. },
  550. hue: {
  551. maxLeft: 0,
  552. maxTop: 100,
  553. callLeft: false,
  554. callTop: 'setHue'
  555. },
  556. alpha: {
  557. maxLeft: 0,
  558. maxTop: 100,
  559. callLeft: false,
  560. callTop: 'setAlpha'
  561. }
  562. },
  563. slidersHorz: {
  564. saturation: {
  565. maxLeft: 100,
  566. maxTop: 100,
  567. callLeft: 'setSaturation',
  568. callTop: 'setBrightness'
  569. },
  570. hue: {
  571. maxLeft: 100,
  572. maxTop: 0,
  573. callLeft: 'setHue',
  574. callTop: false
  575. },
  576. alpha: {
  577. maxLeft: 100,
  578. maxTop: 0,
  579. callLeft: 'setAlpha',
  580. callTop: false
  581. }
  582. },
  583. template: '<div class="colorpicker dropdown-menu">' +
  584. '<div class="colorpicker-saturation"><i><b></b></i></div>' +
  585. '<div class="colorpicker-hue"><i></i></div>' +
  586. '<div class="colorpicker-alpha"><i></i></div>' +
  587. '<div class="colorpicker-color"><div /></div>' +
  588. '<div class="colorpicker-selectors"></div>' +
  589. '</div>',
  590. align: 'right',
  591. customClass: null,
  592. colorSelectors: null
  593. };
  594. var Colorpicker = function(element, options) {
  595. this.element = $(element).addClass('colorpicker-element');
  596. this.options = $.extend(true, {}, defaults, this.element.data(), options);
  597. this.component = this.options.component;
  598. this.component = (this.component !== false) ? this.element.find(this.component) : false;
  599. if (this.component && (this.component.length === 0)) {
  600. this.component = false;
  601. }
  602. this.container = (this.options.container === true) ? this.element : this.options.container;
  603. this.container = (this.container !== false) ? $(this.container) : false;
  604. // Is the element an input? Should we search inside for any input?
  605. this.input = this.element.is('input') ? this.element : (this.options.input ?
  606. this.element.find(this.options.input) : false);
  607. if (this.input && (this.input.length === 0)) {
  608. this.input = false;
  609. }
  610. // Set HSB color
  611. this.color = new Color(this.options.color !== false ? this.options.color : this.getValue(), this.options.colorSelectors);
  612. this.format = this.options.format !== false ? this.options.format : this.color.origFormat;
  613. // Setup picker
  614. this.picker = $(this.options.template);
  615. if (this.options.customClass) {
  616. this.picker.addClass(this.options.customClass);
  617. }
  618. if (this.options.inline) {
  619. this.picker.addClass('colorpicker-inline colorpicker-visible');
  620. } else {
  621. this.picker.addClass('colorpicker-hidden');
  622. }
  623. if (this.options.horizontal) {
  624. this.picker.addClass('colorpicker-horizontal');
  625. }
  626. if (this.format === 'rgba' || this.format === 'hsla' || this.options.format === false) {
  627. this.picker.addClass('colorpicker-with-alpha');
  628. }
  629. if (this.options.align === 'right') {
  630. this.picker.addClass('colorpicker-right');
  631. }
  632. if (this.options.colorSelectors) {
  633. var colorpicker = this;
  634. $.each(this.options.colorSelectors, function(name, color) {
  635. var $btn = $('<i />').css('background-color', color).data('class', name);
  636. $btn.click(function() {
  637. colorpicker.setValue($(this).css('background-color'));
  638. });
  639. colorpicker.picker.find('.colorpicker-selectors').append($btn);
  640. });
  641. this.picker.find('.colorpicker-selectors').show();
  642. }
  643. this.picker.on('mousedown.colorpicker touchstart.colorpicker', $.proxy(this.mousedown, this));
  644. this.picker.appendTo(this.container ? this.container : $('body'));
  645. // Bind events
  646. if (this.input !== false) {
  647. this.input.on({
  648. 'keyup.colorpicker': $.proxy(this.keyup, this)
  649. });
  650. this.input.on({
  651. 'change.colorpicker': $.proxy(this.change, this)
  652. });
  653. if (this.component === false) {
  654. this.element.on({
  655. 'focus.colorpicker': $.proxy(this.show, this)
  656. });
  657. }
  658. if (this.options.inline === false) {
  659. this.element.on({
  660. 'focusout.colorpicker': $.proxy(this.hide, this)
  661. });
  662. }
  663. }
  664. if (this.component !== false) {
  665. this.component.on({
  666. 'click.colorpicker': $.proxy(this.show, this)
  667. });
  668. }
  669. if ((this.input === false) && (this.component === false)) {
  670. this.element.on({
  671. 'click.colorpicker': $.proxy(this.show, this)
  672. });
  673. }
  674. // for HTML5 input[type='color']
  675. if ((this.input !== false) && (this.component !== false) && (this.input.attr('type') === 'color')) {
  676. this.input.on({
  677. 'click.colorpicker': $.proxy(this.show, this),
  678. 'focus.colorpicker': $.proxy(this.show, this)
  679. });
  680. }
  681. this.update();
  682. $($.proxy(function() {
  683. this.element.trigger('create');
  684. }, this));
  685. };
  686. Colorpicker.Color = Color;
  687. Colorpicker.prototype = {
  688. constructor: Colorpicker,
  689. destroy: function() {
  690. this.picker.remove();
  691. this.element.removeData('colorpicker').off('.colorpicker');
  692. if (this.input !== false) {
  693. this.input.off('.colorpicker');
  694. }
  695. if (this.component !== false) {
  696. this.component.off('.colorpicker');
  697. }
  698. this.element.removeClass('colorpicker-element');
  699. this.element.trigger({
  700. type: 'destroy'
  701. });
  702. },
  703. reposition: function() {
  704. if (this.options.inline !== false || this.options.container) {
  705. return false;
  706. }
  707. var type = this.container && this.container[0] !== document.body ? 'position' : 'offset';
  708. var element = this.component || this.element;
  709. var offset = element[type]();
  710. if (this.options.align === 'right') {
  711. offset.left -= this.picker.outerWidth() - element.outerWidth();
  712. }
  713. this.picker.css({
  714. top: offset.top + element.outerHeight(),
  715. left: offset.left
  716. });
  717. },
  718. show: function(e) {
  719. if (this.isDisabled()) {
  720. return false;
  721. }
  722. this.picker.addClass('colorpicker-visible').removeClass('colorpicker-hidden');
  723. this.reposition();
  724. $(window).on('resize.colorpicker', $.proxy(this.reposition, this));
  725. if (e && (!this.hasInput() || this.input.attr('type') === 'color')) {
  726. if (e.stopPropagation && e.preventDefault) {
  727. e.stopPropagation();
  728. e.preventDefault();
  729. }
  730. }
  731. if (this.options.inline === false) {
  732. $(window.document).on({
  733. 'mousedown.colorpicker': $.proxy(this.hide, this)
  734. });
  735. }
  736. this.element.trigger({
  737. type: 'showPicker',
  738. color: this.color
  739. });
  740. },
  741. hide: function() {
  742. this.picker.addClass('colorpicker-hidden').removeClass('colorpicker-visible');
  743. $(window).off('resize.colorpicker', this.reposition);
  744. $(document).off({
  745. 'mousedown.colorpicker': this.hide
  746. });
  747. this.update();
  748. this.element.trigger({
  749. type: 'hidePicker',
  750. color: this.color
  751. });
  752. },
  753. updateData: function(val) {
  754. val = val || this.color.toString(this.format);
  755. this.element.data('color', val);
  756. return val;
  757. },
  758. updateInput: function(val) {
  759. val = val || this.color.toString(this.format);
  760. if (this.input !== false) {
  761. if (this.options.colorSelectors) {
  762. var color = new Color(val, this.options.colorSelectors);
  763. var alias = color.toAlias();
  764. if (typeof this.options.colorSelectors[alias] !== 'undefined') {
  765. val = alias;
  766. }
  767. }
  768. this.input.prop('value', val);
  769. }
  770. return val;
  771. },
  772. updatePicker: function(val) {
  773. if (val !== undefined) {
  774. this.color = new Color(val, this.options.colorSelectors);
  775. }
  776. var sl = (this.options.horizontal === false) ? this.options.sliders : this.options.slidersHorz;
  777. var icns = this.picker.find('i');
  778. if (icns.length === 0) {
  779. return;
  780. }
  781. if (this.options.horizontal === false) {
  782. sl = this.options.sliders;
  783. icns.eq(1).css('top', sl.hue.maxTop * (1 - this.color.value.h)).end()
  784. .eq(2).css('top', sl.alpha.maxTop * (1 - this.color.value.a));
  785. } else {
  786. sl = this.options.slidersHorz;
  787. icns.eq(1).css('left', sl.hue.maxLeft * (1 - this.color.value.h)).end()
  788. .eq(2).css('left', sl.alpha.maxLeft * (1 - this.color.value.a));
  789. }
  790. icns.eq(0).css({
  791. 'top': sl.saturation.maxTop - this.color.value.b * sl.saturation.maxTop,
  792. 'left': this.color.value.s * sl.saturation.maxLeft
  793. });
  794. this.picker.find('.colorpicker-saturation').css('backgroundColor', this.color.toHex(this.color.value.h, 1, 1, 1));
  795. this.picker.find('.colorpicker-alpha').css('backgroundColor', this.color.toHex());
  796. this.picker.find('.colorpicker-color, .colorpicker-color div').css('backgroundColor', this.color.toString(this.format));
  797. return val;
  798. },
  799. updateComponent: function(val) {
  800. val = val || this.color.toString(this.format);
  801. if (this.component !== false) {
  802. var icn = this.component.find('i').eq(0);
  803. if (icn.length > 0) {
  804. icn.css({
  805. 'backgroundColor': val
  806. });
  807. } else {
  808. this.component.css({
  809. 'backgroundColor': val
  810. });
  811. }
  812. }
  813. return val;
  814. },
  815. update: function(force) {
  816. var val;
  817. if ((this.getValue(false) !== false) || (force === true)) {
  818. // Update input/data only if the current value is not empty
  819. val = this.updateComponent();
  820. this.updateInput(val);
  821. this.updateData(val);
  822. this.updatePicker(); // only update picker if value is not empty
  823. }
  824. return val;
  825. },
  826. setValue: function(val) { // set color manually
  827. this.color = new Color(val, this.options.colorSelectors);
  828. this.update(true);
  829. this.element.trigger({
  830. type: 'changeColor',
  831. color: this.color,
  832. value: val
  833. });
  834. },
  835. getValue: function(defaultValue) {
  836. defaultValue = (defaultValue === undefined) ? '#000000' : defaultValue;
  837. var val;
  838. if (this.hasInput()) {
  839. val = this.input.val();
  840. } else {
  841. val = this.element.data('color');
  842. }
  843. if ((val === undefined) || (val === '') || (val === null)) {
  844. // if not defined or empty, return default
  845. val = defaultValue;
  846. }
  847. return val;
  848. },
  849. hasInput: function() {
  850. return (this.input !== false);
  851. },
  852. isDisabled: function() {
  853. if (this.hasInput()) {
  854. return (this.input.prop('disabled') === true);
  855. }
  856. return false;
  857. },
  858. disable: function() {
  859. if (this.hasInput()) {
  860. this.input.prop('disabled', true);
  861. this.element.trigger({
  862. type: 'disable',
  863. color: this.color,
  864. value: this.getValue()
  865. });
  866. return true;
  867. }
  868. return false;
  869. },
  870. enable: function() {
  871. if (this.hasInput()) {
  872. this.input.prop('disabled', false);
  873. this.element.trigger({
  874. type: 'enable',
  875. color: this.color,
  876. value: this.getValue()
  877. });
  878. return true;
  879. }
  880. return false;
  881. },
  882. currentSlider: null,
  883. mousePointer: {
  884. left: 0,
  885. top: 0
  886. },
  887. mousedown: function(e) {
  888. if (!e.pageX && !e.pageY && e.originalEvent) {
  889. e.pageX = e.originalEvent.touches[0].pageX;
  890. e.pageY = e.originalEvent.touches[0].pageY;
  891. }
  892. e.stopPropagation();
  893. e.preventDefault();
  894. var target = $(e.target);
  895. //detect the slider and set the limits and callbacks
  896. var zone = target.closest('div');
  897. var sl = this.options.horizontal ? this.options.slidersHorz : this.options.sliders;
  898. if (!zone.is('.colorpicker')) {
  899. if (zone.is('.colorpicker-saturation')) {
  900. this.currentSlider = $.extend({}, sl.saturation);
  901. } else if (zone.is('.colorpicker-hue')) {
  902. this.currentSlider = $.extend({}, sl.hue);
  903. } else if (zone.is('.colorpicker-alpha')) {
  904. this.currentSlider = $.extend({}, sl.alpha);
  905. } else {
  906. return false;
  907. }
  908. var offset = zone.offset();
  909. //reference to guide's style
  910. this.currentSlider.guide = zone.find('i')[0].style;
  911. this.currentSlider.left = e.pageX - offset.left;
  912. this.currentSlider.top = e.pageY - offset.top;
  913. this.mousePointer = {
  914. left: e.pageX,
  915. top: e.pageY
  916. };
  917. //trigger mousemove to move the guide to the current position
  918. $(document).on({
  919. 'mousemove.colorpicker': $.proxy(this.mousemove, this),
  920. 'touchmove.colorpicker': $.proxy(this.mousemove, this),
  921. 'mouseup.colorpicker': $.proxy(this.mouseup, this),
  922. 'touchend.colorpicker': $.proxy(this.mouseup, this)
  923. }).trigger('mousemove');
  924. }
  925. return false;
  926. },
  927. mousemove: function(e) {
  928. if (!e.pageX && !e.pageY && e.originalEvent) {
  929. e.pageX = e.originalEvent.touches[0].pageX;
  930. e.pageY = e.originalEvent.touches[0].pageY;
  931. }
  932. e.stopPropagation();
  933. e.preventDefault();
  934. var left = Math.max(
  935. 0,
  936. Math.min(
  937. this.currentSlider.maxLeft,
  938. this.currentSlider.left + ((e.pageX || this.mousePointer.left) - this.mousePointer.left)
  939. )
  940. );
  941. var top = Math.max(
  942. 0,
  943. Math.min(
  944. this.currentSlider.maxTop,
  945. this.currentSlider.top + ((e.pageY || this.mousePointer.top) - this.mousePointer.top)
  946. )
  947. );
  948. this.currentSlider.guide.left = left + 'px';
  949. this.currentSlider.guide.top = top + 'px';
  950. if (this.currentSlider.callLeft) {
  951. this.color[this.currentSlider.callLeft].call(this.color, left / this.currentSlider.maxLeft);
  952. }
  953. if (this.currentSlider.callTop) {
  954. this.color[this.currentSlider.callTop].call(this.color, top / this.currentSlider.maxTop);
  955. }
  956. // Change format dynamically
  957. // Only occurs if user choose the dynamic format by
  958. // setting option format to false
  959. if (this.currentSlider.callTop === 'setAlpha' && this.options.format === false) {
  960. // Converting from hex / rgb to rgba
  961. if (this.color.value.a !== 1) {
  962. this.format = 'rgba';
  963. this.color.origFormat = 'rgba';
  964. }
  965. // Converting from rgba to hex
  966. else {
  967. this.format = 'hex';
  968. this.color.origFormat = 'hex';
  969. }
  970. }
  971. this.update(true);
  972. this.element.trigger({
  973. type: 'changeColor',
  974. color: this.color
  975. });
  976. return false;
  977. },
  978. mouseup: function(e) {
  979. e.stopPropagation();
  980. e.preventDefault();
  981. $(document).off({
  982. 'mousemove.colorpicker': this.mousemove,
  983. 'touchmove.colorpicker': this.mousemove,
  984. 'mouseup.colorpicker': this.mouseup,
  985. 'touchend.colorpicker': this.mouseup
  986. });
  987. return false;
  988. },
  989. change: function(e) {
  990. this.keyup(e);
  991. },
  992. keyup: function(e) {
  993. if ((e.keyCode === 38)) {
  994. if (this.color.value.a < 1) {
  995. this.color.value.a = Math.round((this.color.value.a + 0.01) * 100) / 100;
  996. }
  997. this.update(true);
  998. } else if ((e.keyCode === 40)) {
  999. if (this.color.value.a > 0) {
  1000. this.color.value.a = Math.round((this.color.value.a - 0.01) * 100) / 100;
  1001. }
  1002. this.update(true);
  1003. } else {
  1004. this.color = new Color(this.input.val(), this.options.colorSelectors);
  1005. // Change format dynamically
  1006. // Only occurs if user choose the dynamic format by
  1007. // setting option format to false
  1008. if (this.color.origFormat && this.options.format === false) {
  1009. this.format = this.color.origFormat;
  1010. }
  1011. if (this.getValue(false) !== false) {
  1012. this.updateData();
  1013. this.updateComponent();
  1014. this.updatePicker();
  1015. }
  1016. }
  1017. this.element.trigger({
  1018. type: 'changeColor',
  1019. color: this.color,
  1020. value: this.input.val()
  1021. });
  1022. }
  1023. };
  1024. $.colorpicker = Colorpicker;
  1025. $.fn.colorpicker = function(option) {
  1026. var pickerArgs = arguments,
  1027. rv;
  1028. var $returnValue = this.each(function() {
  1029. var $this = $(this),
  1030. inst = $this.data('colorpicker'),
  1031. options = ((typeof option === 'object') ? option : {});
  1032. if ((!inst) && (typeof option !== 'string')) {
  1033. $this.data('colorpicker', new Colorpicker(this, options));
  1034. } else {
  1035. if (typeof option === 'string') {
  1036. rv = inst[option].apply(inst, Array.prototype.slice.call(pickerArgs, 1));
  1037. }
  1038. }
  1039. });
  1040. if (option === 'getValue') {
  1041. return rv;
  1042. }
  1043. return $returnValue;
  1044. };
  1045. $.fn.colorpicker.constructor = Colorpicker;
  1046. }));