|
3 | 3 | (function(countlyVue) {
|
4 | 4 |
|
5 | 5 | var _mixins = countlyVue.mixins;
|
6 |
| - var HEX_COLOR_REGEX = new RegExp('^#([0-9a-f]{3}|[0-9a-f]{6})$', 'i'); |
| 6 | + var HEX_COLOR_REGEX = new RegExp('^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$', 'i'); |
| 7 | + |
| 8 | + Vue.component("cly-colorpicker", countlyVue.components.create({ |
| 9 | + template: CV.T('/javascripts/countly/vue/templates/UI/color-picker.html'), |
| 10 | + |
| 11 | + components: { |
| 12 | + picker: window.VueColor.Sketch |
| 13 | + }, |
| 14 | + |
| 15 | + props: { |
| 16 | + placement: { |
| 17 | + default: 'left', |
| 18 | + type: String |
| 19 | + }, |
| 20 | + |
| 21 | + resetValue: { |
| 22 | + default: '#FFFFFF', |
| 23 | + type: [String, Object] |
| 24 | + }, |
| 25 | + |
| 26 | + testId: { |
| 27 | + default: 'cly-colorpicker-test-id', |
| 28 | + type: String |
| 29 | + }, |
| 30 | + |
| 31 | + value: { |
| 32 | + default: '#FFFFFF', |
| 33 | + type: [String, Object], |
| 34 | + } |
| 35 | + }, |
| 36 | + |
| 37 | + emits: [ |
| 38 | + 'change', |
| 39 | + 'input' |
| 40 | + ], |
7 | 41 |
|
8 |
| - Vue.component("cly-colorpicker", countlyVue.components.BaseComponent.extend({ |
9 | 42 | mixins: [
|
10 | 43 | _mixins.i18n
|
11 | 44 | ],
|
12 |
| - props: { |
13 |
| - value: {type: [String, Object], default: "#FFFFFF"}, |
14 |
| - resetValue: { type: [String, Object], default: "#FFFFFF"}, |
15 |
| - placement: {type: String, default: "left"}, |
16 |
| - testId: {type: String, default: "cly-colorpicker-test-id"} |
17 |
| - }, |
| 45 | + |
18 | 46 | data: function() {
|
19 | 47 | return {
|
20 | 48 | isOpened: false,
|
21 | 49 |
|
22 | 50 | previousColor: null
|
23 | 51 | };
|
24 | 52 | },
|
| 53 | + |
25 | 54 | computed: {
|
26 |
| - previewStyle: function() { |
27 |
| - return { |
28 |
| - "background-color": this.value |
29 |
| - }; |
| 55 | + bodyClasses() { |
| 56 | + return ['cly-vue-color-picker__body--' + this.placement]; |
| 57 | + }, |
| 58 | + |
| 59 | + dropStyles() { |
| 60 | + return { color: this.localValue }; |
30 | 61 | },
|
31 |
| - localValue: { |
32 |
| - get: function() { |
33 |
| - var rawValue = this.value || this.resetValue; |
34 | 62 |
|
35 |
| - return rawValue.replace("#", ""); |
| 63 | + localValue: { |
| 64 | + get() { |
| 65 | + return (this.value || this.resetValue); |
36 | 66 | },
|
37 |
| - set: function(value) { |
38 |
| - var colorValue = "#" + value.replace("#", ""); |
| 67 | + set(value) { |
| 68 | + let finalValue = value; |
| 69 | + |
| 70 | + if (!finalValue.startsWith('#')) { |
| 71 | + finalValue = `#${finalValue}`; |
| 72 | + } |
39 | 73 |
|
40 |
| - if (colorValue.match(HEX_COLOR_REGEX)) { |
41 |
| - this.setColor({hex: colorValue}); |
| 74 | + if (finalValue.match(HEX_COLOR_REGEX)) { |
| 75 | + this.$emit('input', finalValue); |
42 | 76 | }
|
43 | 77 | }
|
44 |
| - }, |
45 |
| - alignment: function() { |
46 |
| - return "picker-body--" + this.placement; |
47 |
| - }, |
| 78 | + } |
48 | 79 | },
|
49 | 80 |
|
50 | 81 | watch: {
|
51 |
| - isOpened: { |
52 |
| - handler(value) { |
53 |
| - if (value) { |
54 |
| - this.previousColor = JSON.parse(JSON.stringify(this.value)); |
55 |
| - } |
| 82 | + isOpened(value) { |
| 83 | + if (value) { |
| 84 | + this.previousColor = JSON.parse(JSON.stringify(this.value)); |
56 | 85 | }
|
57 | 86 | }
|
58 | 87 | },
|
59 | 88 |
|
60 | 89 | methods: {
|
61 |
| - setColor: function(color) { |
62 |
| - var finalColor = color.hex8 || color.hex; |
63 |
| - this.previousColor = JSON.parse(JSON.stringify(this.localValue)); |
64 |
| - this.$emit("input", finalColor); |
65 |
| - }, |
66 |
| - reset: function() { |
67 |
| - this.setColor({hex: this.resetValue}); |
68 |
| - this.close(); |
69 |
| - }, |
70 |
| - open: function() { |
| 90 | + onInputContainerClick() { |
71 | 91 | this.isOpened = true;
|
72 | 92 | },
|
73 |
| - close: function() { |
74 |
| - this.isOpened = false; |
75 |
| - }, |
76 |
| - confirm: function(color) { |
77 |
| - this.$emit('change', color); |
| 93 | + |
| 94 | + close() { |
78 | 95 | this.isOpened = false;
|
79 | 96 | },
|
80 | 97 |
|
81 | 98 | onCancelClick() {
|
82 | 99 | this.localValue = this.previousColor;
|
83 | 100 | this.close();
|
| 101 | + }, |
| 102 | + |
| 103 | + onConfirmClick() { |
| 104 | + this.$emit('change', this.localValue); |
| 105 | + this.close(); |
| 106 | + }, |
| 107 | + |
| 108 | + onPickerInput(color) { |
| 109 | + this.localValue = color.hex8 || color.hex; |
| 110 | + }, |
| 111 | + |
| 112 | + onResetClick() { |
| 113 | + this.localValue = this.resetValue; |
| 114 | + this.close(); |
84 | 115 | }
|
85 |
| - }, |
86 |
| - components: { |
87 |
| - picker: window.VueColor.Sketch |
88 |
| - }, |
89 |
| - template: '<div class="cly-vue-colorpicker">\n' + |
90 |
| - '<div @click.stop="open" :data-test-id="testId" class="preview">\n' + |
91 |
| - '<div>\n' + |
92 |
| - '<div class="drop bu-mt-auto" :data-test-id="testId + \'-cly-color-picker-img-wrapper\'" :style="previewStyle"></div>\n' + |
93 |
| - '<img src="images/icons/blob.svg"/>\n' + |
94 |
| - '</div>\n' + |
95 |
| - '<input class="color-input" v-model="localValue" type="text"/>\n' + |
96 |
| - '<img height="12px" width="10px" class="bu-pt-2" v-if="!isOpened" src="images/icons/arrow_drop_down_.svg"/>\n' + |
97 |
| - '<img height="12px" width="10px" class="bu-pt-2" v-if="isOpened" src="images/icons/arrow_drop_up_.svg"/>\n' + |
98 |
| - '</div>\n' + |
99 |
| - '<div class="picker-body" v-if="isOpened" v-click-outside="close" :class="alignment">\n' + |
100 |
| - '<picker :preset-colors="[]" :value="value" @input="setColor"></picker>\n' + |
101 |
| - '<div class="button-controls">\n' + |
102 |
| - '<cly-button :data-test-id="testId + \'-reset-button\'" :label="i18n(\'common.reset\')" @click="reset" skin="light"></cly-button>\n' + |
103 |
| - '<cly-button :data-test-id="testId + \'-cancel-button\'" :label="i18n(\'common.cancel\')" @click="onCancelClick" skin="light"></cly-button>\n' + |
104 |
| - '<cly-button :data-test-id="testId + \'-confirm-button\'" :label="i18n(\'common.confirm\')" @click="confirm(setColor)" skin="green"></cly-button>\n' + |
105 |
| - '</div>\n' + |
106 |
| - '</div>\n' + |
107 |
| - '</div>' |
| 116 | + } |
108 | 117 | }));
|
109 | 118 |
|
110 | 119 | Vue.component("cly-dropzone", window.vue2Dropzone);
|
|
0 commit comments