|
| 1 | +//= require ../vendor/pickr.min.js |
| 2 | + |
| 3 | +(function() { |
| 4 | + function _onTextChange() { |
| 5 | + var $input = $(this); |
| 6 | + var variable = $input.attr('name'); |
| 7 | + var value = $input.val(); |
| 8 | + |
| 9 | + document.documentElement.style.setProperty(variable, value); |
| 10 | + window.pickrs[variable].setColor(value, true); |
| 11 | + } |
| 12 | + |
| 13 | + function _onPickrChange(color, instance) { |
| 14 | + var value = color ? '#' + color.toHEXA().join('') : ''; |
| 15 | + var $variable = $(instance.getRoot().root).parents('.theming-editor__variable'); |
| 16 | + $input = $variable.find('.theming-editor__input'); |
| 17 | + var variable = $input.attr('name'); |
| 18 | + |
| 19 | + // Don't update input if the user is typing in it |
| 20 | + if (!$input.is(':focus')) { |
| 21 | + $input.val(value); |
| 22 | + } |
| 23 | + // Update the picker button color (doesn't by default) |
| 24 | + instance.applyColor(true); |
| 25 | + // Update CSS property |
| 26 | + document.documentElement.style.setProperty(variable, value); |
| 27 | + } |
| 28 | + |
| 29 | + function onSave() { |
| 30 | + var $editor = $(this).parents('.theming-editor__wrapper'); |
| 31 | + var css = ':root {\n'; |
| 32 | + $editor.find('.theming-editor__input').each(function() { |
| 33 | + var variable = $(this).attr('name'); |
| 34 | + var value = $(this).val(); |
| 35 | + css += ' ' + variable + ': ' + value + ';\n'; |
| 36 | + }); |
| 37 | + css += '}'; |
| 38 | + saveConfig(css); |
| 39 | + } |
| 40 | + |
| 41 | + function saveConfig(css) { |
| 42 | + var data = { |
| 43 | + hackathon_config: { |
| 44 | + custom_css: css, |
| 45 | + }, |
| 46 | + }; |
| 47 | + fetch('/manage/configs/custom_css', { |
| 48 | + method: 'PATCH', |
| 49 | + redirect: 'manual', |
| 50 | + headers: { |
| 51 | + 'Content-Type': 'application/json', |
| 52 | + 'X-CSRF-Token': Rails.csrfToken(), |
| 53 | + }, |
| 54 | + body: JSON.stringify(data), |
| 55 | + }) |
| 56 | + .then(function(response) { |
| 57 | + if (!response.ok && response.type != 'opaqueredirect') { |
| 58 | + alert('There was an error attempting to save. Please try again or refresh the page.'); |
| 59 | + return; |
| 60 | + } |
| 61 | + window.location.replace('/manage/configs/exit_theming_editor'); |
| 62 | + }) |
| 63 | + .catch(function() { |
| 64 | + alert('There was an error attempting to save. Please try again or refresh the page.'); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + var onTextChange = throttle(_onTextChange, 100); |
| 69 | + var onPickrChange = throttle(_onPickrChange, 100); |
| 70 | + |
| 71 | + function initHtml() { |
| 72 | + var variables = Object.values(getComputedStyle(document.documentElement)) |
| 73 | + .filter(function(x) { |
| 74 | + return x.startsWith('--primary'); |
| 75 | + }) |
| 76 | + .sort(); |
| 77 | + var $editor = $('#theming-editor'); |
| 78 | + var $variables = $editor.find('.theming-editor__variables'); |
| 79 | + var $variable_template = $editor.find('.theming-editor__variable').remove(); |
| 80 | + variables.forEach(function(variable) { |
| 81 | + var $new_variable = $variable_template.clone(); |
| 82 | + var $code = $new_variable.find('.theming-editor__variable-name-code'); |
| 83 | + var $input = $new_variable.find('.theming-editor__input'); |
| 84 | + var value = getComputedStyle(document.documentElement) |
| 85 | + .getPropertyValue(variable) |
| 86 | + .trim(); |
| 87 | + |
| 88 | + $code.text(variable); |
| 89 | + $input.val(value); |
| 90 | + $input.attr('name', variable); |
| 91 | + $input.on('input', onTextChange); |
| 92 | + $variables.append($new_variable); |
| 93 | + }); |
| 94 | + $editor.find('.theming-editor__button-save').on('click', onSave); |
| 95 | + } |
| 96 | + |
| 97 | + function initColorPickers() { |
| 98 | + window.pickrs = {}; |
| 99 | + $('.theming-editor__color-picker').each(function() { |
| 100 | + var $input = $(this) |
| 101 | + .parents('.theming-editor__variable') |
| 102 | + .find('.theming-editor__input'); |
| 103 | + var pickr = Pickr.create({ |
| 104 | + el: this, |
| 105 | + default: $input.val(), |
| 106 | + components: { |
| 107 | + opacity: false, |
| 108 | + hue: true, |
| 109 | + interaction: false, |
| 110 | + }, |
| 111 | + }); |
| 112 | + pickr.on('change', onPickrChange); |
| 113 | + var variable = $input.attr('name'); |
| 114 | + window.pickrs[variable] = pickr; |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + function init() { |
| 119 | + initHtml(); |
| 120 | + initColorPickers(); |
| 121 | + } |
| 122 | + |
| 123 | + document.addEventListener('turbolinks:load', init); |
| 124 | +})(); |
0 commit comments