preview.es6.js 2.2 KB

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