Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4106: Showing color preview in Edit Profile #4212

Merged
merged 4 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
colorPreview javascript added to packs
  • Loading branch information
saurabhdaware committed Oct 2, 2019
commit ff601cf2dab6ebb59b1b0e03a2151d8dd03605f0
2 changes: 1 addition & 1 deletion app/assets/stylesheets/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
text-align:center;
padding:20px;
#color-select-preview-logo{
background-color:#0099ff;
background-color:#111111;
fill:#fff;
padding:25px 20px;
border-radius:7px;
Expand Down
59 changes: 55 additions & 4 deletions app/javascript/packs/colorPreview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
// const bgColorField = document.getElementById('bg-color-colorfield');
// const textColorField = document.getElementById('text-color-colorfield');
const colorField = {
// input type="color"
bgColor: document.getElementById('bg-color-colorfield'),
textColor: document.getElementById('text-color-colorfield'),
};

// const bgTextField = document.getElementById('bg-color-textfield');
// const colorTextField = document.getElementById('text-color-textfield');
const textField = {
// input type="text"
bgColor: document.getElementById('bg-color-textfield'),
textColor: document.getElementById('text-color-textfield'),
};

const preview = document.getElementById('color-select-preview-logo');

// Assigns input[type='text'] values to input[type='color']
function updateColorFields() {
colorField.bgColor.value = textField.bgColor.value;
colorField.textColor.value = textField.textColor.value;
}

// Updating text fields when color fields are changed
function updateTextFields() {
textField.bgColor.value = colorField.bgColor.value;
textField.textColor.value = colorField.textColor.value;
}

// Updates Preview Colors
function updatePreview() {
preview.style.backgroundColor = textField.bgColor.value;
preview.style.fill = textField.textColor.value;
}

// Event Watchers
// When color fields change -> updateTextField values and refresh preview
function watchColorFields() {
updateTextFields();
updatePreview();
}

// When text fields change -> updateColorField values and refresh preview
function watchTextFields(e) {
if (e.target.value.match(/[0-9a-f]{6}/gi)) {
updateColorFields();
updatePreview();
}
}

// Event Listeners
colorField.bgColor.addEventListener('change', watchColorFields);
colorField.textColor.addEventListener('change', watchColorFields);
textField.bgColor.addEventListener('keyup', watchTextFields);
textField.textColor.addEventListener('keyup', watchTextFields);

// on init
updateColorFields();
updatePreview();
2 changes: 1 addition & 1 deletion app/views/users/_profile.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<% @user.bg_color_hex = user_colors(@user)[:text] if @user.text_color_hex.blank? %>
<%= f.label :text_color_hex, "text color" %><br>
<div class="select-field-wrapper">
<%= f.text_field :text_color_hex, placeholder: "#ffffff", class: "color-select-textbox", id: "text-color-colorfield" %>
<%= f.text_field :text_color_hex, placeholder: "#ffffff", class: "color-select-textbox", id: "text-color-textfield" %>
<input id="text-color-colorfield" type="color">
</div>
</div>
Expand Down