preview.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @file
  3. * Attaches preview-related behavior for the Color module.
  4. */
  5. (function ($, Drupal) {
  6. 'use strict';
  7. /**
  8. * Namespace for color-related functionality for Drupal.
  9. *
  10. * @namespace
  11. */
  12. Drupal.color = {
  13. /**
  14. * The callback for when the color preview has been attached.
  15. *
  16. * @param {Element} context
  17. * The context to initiate the color behaviour.
  18. * @param {object} settings
  19. * Settings for the color functionality.
  20. * @param {HTMLFormElement} form
  21. * The form to initiate the color behaviour on.
  22. * @param {object} farb
  23. * The farbtastic object.
  24. * @param {number} height
  25. * Height of gradient.
  26. * @param {number} width
  27. * Width of gradient.
  28. */
  29. callback: function (context, settings, form, farb, height, width) {
  30. var accum;
  31. var delta;
  32. // Solid background.
  33. form.find('.color-preview').css('backgroundColor', form.find('.color-palette input[name="palette[base]"]').val());
  34. // Text preview.
  35. form.find('#text').css('color', form.find('.color-palette input[name="palette[text]"]').val());
  36. form.find('#text a, #text h2').css('color', form.find('.color-palette input[name="palette[link]"]').val());
  37. function gradientLineColor(i, element) {
  38. for (var k in accum) {
  39. if (accum.hasOwnProperty(k)) {
  40. accum[k] += delta[k];
  41. }
  42. }
  43. element.style.backgroundColor = farb.pack(accum);
  44. }
  45. // Set up gradients if there are some.
  46. var color_start;
  47. var color_end;
  48. for (var i in settings.gradients) {
  49. if (settings.gradients.hasOwnProperty(i)) {
  50. color_start = farb.unpack(form.find('.color-palette input[name="palette[' + settings.gradients[i].colors[0] + ']"]').val());
  51. color_end = farb.unpack(form.find('.color-palette input[name="palette[' + settings.gradients[i].colors[1] + ']"]').val());
  52. if (color_start && color_end) {
  53. delta = [];
  54. for (var j in color_start) {
  55. if (color_start.hasOwnProperty(j)) {
  56. delta[j] = (color_end[j] - color_start[j]) / (settings.gradients[i].vertical ? height[i] : width[i]);
  57. }
  58. }
  59. accum = color_start;
  60. // Render gradient lines.
  61. form.find('#gradient-' + i + ' > div').each(gradientLineColor);
  62. }
  63. }
  64. }
  65. }
  66. };
  67. })(jQuery, Drupal);