Commit 35238511 authored by Ammar Alakkad's avatar Ammar Alakkad

Add default colors for broadcast messages

parent 29a7c83e
......@@ -3,26 +3,31 @@ import _ from 'underscore';
import axios from '~/lib/utils/axios_utils';
import flash from '~/flash';
import { __ } from '~/locale';
import { textColorForBackground } from '~/lib/utils/color_utils';
export default () => {
$('input#broadcast_message_color').on('input', function onMessageColorInput() {
const $broadcastMessageColor = $('input#broadcast_message_color');
const $broadcastMessagePreview = $('div.broadcast-message-preview');
$broadcastMessageColor.on('input', function onMessageColorInput() {
const previewColor = $(this).val();
$('div.broadcast-message-preview').css('background-color', previewColor);
$broadcastMessagePreview.css('background-color', previewColor);
});
$('input#broadcast_message_font').on('input', function onMessageFontInput() {
const previewColor = $(this).val();
$('div.broadcast-message-preview').css('color', previewColor);
$broadcastMessagePreview.css('color', previewColor);
});
const previewPath = $('textarea#broadcast_message_message').data('previewPath');
const $broadcastMessage = $('textarea#broadcast_message_message');
const previewPath = $broadcastMessage.data('previewPath');
const $jsBroadcastMessagePreview = $('.js-broadcast-message-preview');
$('textarea#broadcast_message_message').on(
$broadcastMessage.on(
'input',
_.debounce(function onMessageInput() {
const message = $(this).val();
if (message === '') {
$('.js-broadcast-message-preview').text(__('Your message here'));
$jsBroadcastMessagePreview.text(__('Your message here'));
} else {
axios
.post(previewPath, {
......@@ -31,10 +36,40 @@ export default () => {
},
})
.then(({ data }) => {
$('.js-broadcast-message-preview').html(data.message);
$jsBroadcastMessagePreview.html(data.message);
})
.catch(() => flash(__('An error occurred while rendering preview broadcast message')));
}
}, 250),
);
const updateColorPreview = () => {
const selectedBackgroundColor = $broadcastMessageColor.val();
const contrastTextColor = textColorForBackground(selectedBackgroundColor);
// save contrastTextColor to hidden input field
$('input.text-font-color').val(contrastTextColor);
// Updates the preview color with the hex-color input
const selectedColorStyle = {
backgroundColor: selectedBackgroundColor,
color: contrastTextColor,
};
$('.label-color-preview').css(selectedColorStyle);
return $broadcastMessagePreview.css(selectedColorStyle);
};
const setSuggestedColor = e => {
const color = $(e.currentTarget).data('color');
$broadcastMessageColor
.val(color)
// Notify the form, that color has changed
.trigger('input');
updateColorPreview();
return e.preventDefault();
};
$(document).on('click', '.suggest-colors a', setSuggestedColor);
};
......@@ -17,19 +17,27 @@
required: true,
dir: 'auto',
data: { preview_path: preview_admin_broadcast_messages_path }
.form-group.row.js-toggle-colors-container
.col-sm-10.offset-sm-2
= link_to 'Customize colors', '#', class: 'js-toggle-colors-link'
.form-group.row.js-toggle-colors-container.toggle-colors.hide
.form-group.row
.col-sm-2.col-form-label
= f.label :color, "Background Color"
= f.label :color, _("Background color")
.col-sm-10
= f.color_field :color, class: "form-control"
.input-group
.input-group-prepend
.input-group-text.label-color-preview{ :style => 'background-color: ' + @broadcast_message.color + '; color: ' + @broadcast_message.font }
= ' '.html_safe
= f.text_field :color, class: "form-control"
.form-text.text-muted
= _('Choose any color.')
%br
= _("Or you can choose one of the suggested colors below")
= render_suggested_colors
.form-group.row.js-toggle-colors-container.toggle-colors.hide
.col-sm-2.col-form-label
= f.label :font, "Font Color"
.col-sm-10
= f.color_field :font, class: "form-control"
= f.color_field :font, class: "form-control text-font-color"
.form-group.row
.col-sm-2.col-form-label
= f.label :starts_at, _("Starts at (UTC)")
......
---
title: add color selector to broadcast messages form
merge_request: 30988
author:
type: other
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment