/
umbcolorpicker.directive.js
315 lines (251 loc) · 10.4 KB
/
umbcolorpicker.directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
@ngdoc directive
@name umbraco.directives.directive:umbColorPicker
@restrict E
@scope
@description
<strong>Added in Umbraco v. 8.10:</strong> Use this directive to render a color picker.
<h3>Markup example</h3>
<pre>
<div ng-controller="My.ColorController as vm">
<umb-color-picker
options="vm.options"
on-show="vm.show(color)"
on-hide="vm.hide(color)"
on-change="vm.change(color)">
</umb-color-picker>
</div>
</pre>
<h3>Controller example</h3>
<pre>
(function () {
"use strict";
function Controller() {
var vm = this;
vm.options = {
type: "color",
color: defaultColor,
showAlpha: false,
showPalette: true,
showPaletteOnly: false,
preferredFormat: "hex",
};
vm.show = show;
vm.hide = hide;
vm.change = change;
function show(color) {
color.toHexString().trimStart("#");
}
function hide(color) {
color.toHexString().trimStart("#");
}
function change(color) {
color.toHexString().trimStart("#");
}
}
angular.module("umbraco").controller("My.ColorController", Controller);
})();
</pre>
@param {string} ngModel (<code>binding</code>): Value for the color picker.
@param {object} options (<code>binding</code>): Config object for the color picker.
@param {function} onBeforeShow (<code>expression</code>): You can prevent the color picker from showing up if you return false in the beforeShow event. This event is ignored on a flat color picker.
@param {function} onChange (<code>expression</code>): Called as the original input changes. Only happens when the input is closed or the 'Choose' button is clicked.
@param {function} onShow (<code>expression</code>): Called after the color picker is opened. This is ignored on a flat color picker. Note, when any color picker on the page is shown it will hide any that are already open.
@param {function} onHide (<code>expression</code>): Called after the color picker is hidden. This happens when clicking outside of the picker while it is open. Note, when any color picker on the page is shown it will hide any that are already open. This event is ignored on a flat color picker.
@param {function} onMove (<code>expression</code>): Called as the user moves around within the color picker.
@param {function} onDragStart (<code>expression</code>): Called at the beginning of a drag event on either hue slider, alpha slider, or main color picker areas.
@param {function} onDragStop (<code>expression</code>): Called at the end of a drag event on either hue slider, alpha slider, or main color picker areas.
**/
(function () {
'use strict';
function ColorPickerController($scope, $element, $timeout, assetsService, localizationService, $attrs) {
const ctrl = this;
let colorPickerInstance = null;
let labels = {};
let options = null;
ctrl.readonly = false;
ctrl.value = null;
ctrl.$onInit = function () {
// load the separate css for the editor to avoid it blocking our js loading
assetsService.loadCss("lib/spectrum/spectrum.min.css", $scope);
// load the js file for the color picker
assetsService.load([
//"lib/spectrum/tinycolor.js",
"lib/spectrum/spectrum.js"
], $scope).then(function () {
// init color picker
grabElementAndRun();
});
}
ctrl.$onChanges = function (changes) {
if (colorPickerInstance && changes.ngModel) {
colorPickerInstance.spectrum("set", changes.ngModel.currentValue);
}
}
$attrs.$observe('readonly', (value) => {
ctrl.readonly = value !== undefined;
if (!colorPickerInstance) {
return;
}
if (ctrl.readonly) {
colorPickerInstance.spectrum('disable');
} else {
colorPickerInstance.spectrum('enable');
}
});
function grabElementAndRun() {
var labelKeys = [
"general_cancel",
"general_choose",
"general_clear"
];
localizationService.localizeMany(labelKeys).then(values => {
labels.cancel = values[0];
labels.choose = values[1];
labels.clear = values[2];
});
$timeout(function () {
const element = $element.find('.umb-color-picker > input')[0];
setColorPicker(element, labels);
}, 0, true);
}
function setColorPicker(element, labels) {
// Spectrum options: https://seballot.github.io/spectrum/#options
const defaultOptions = {
type: "color",
color: null,
showAlpha: false,
showInitial: false,
showInput: true,
cancelText: labels.cancel,
clearText: labels.clear,
chooseText: labels.choose,
preferredFormat: "hex",
clickoutFiresChange: true
};
// If has ngModel set the color
if (ctrl.ngModel) {
defaultOptions.color = ctrl.ngModel;
}
//const options = ctrl.options ? ctrl.options : defaultOptions;
options = Utilities.extend(defaultOptions, ctrl.options);
var elem = angular.element(element);
// Create new color pickr instance
const colorPicker = elem.spectrum(options);
colorPickerInstance = colorPicker;
if (colorPickerInstance) {
if (ctrl.readonly) {
colorPickerInstance.spectrum('disable');
}
const tinyColor = colorPickerInstance.spectrum("get");
ctrl.value = getColorString(tinyColor, options.preferredFormat);
colorPickerInstance.on('change', (e, tinyColor) => {
ctrl.value = getColorString(tinyColor, options.preferredFormat);
$scope.$applyAsync();
});
// destroy the color picker instance when the dom element is removed
elem.on('$destroy', function () {
colorPickerInstance.spectrum('destroy');
});
}
setUpCallbacks();
// Refresh the scope
$scope.$applyAsync();
}
// Spectrum events: https://seballot.github.io/spectrum/#events
function setUpCallbacks() {
if (colorPickerInstance) {
// bind hook for beforeShow
if (ctrl.onBeforeShow) {
colorPickerInstance.on('beforeShow.spectrum', (e, tinycolor) => {
$timeout(function () {
ctrl.onBeforeShow({ color: tinycolor });
});
});
}
// bind hook for show
if (ctrl.onShow) {
colorPickerInstance.on('show.spectrum', (e, tinycolor) => {
$timeout(function () {
ctrl.onShow({ color: tinycolor });
});
});
}
// bind hook for hide
if (ctrl.onHide) {
colorPickerInstance.on('hide.spectrum', (e, tinycolor) => {
$timeout(function () {
ctrl.onHide({ color: tinycolor });
});
});
}
// bind hook for change
if (ctrl.onChange) {
colorPickerInstance.on('change.spectrum', (e, tinycolor) => {
$timeout(function () {
ctrl.onChange({ color: tinycolor });
});
});
}
// bind hook for move
if (ctrl.onMove) {
colorPickerInstance.on('move.spectrum', (e, tinycolor) => {
$timeout(function () {
ctrl.onMove({ color: tinycolor });
});
});
}
// bind hook for drag start
if (ctrl.onDragStart) {
colorPickerInstance.on('dragstart.spectrum', (e, tinycolor) => {
$timeout(function () {
ctrl.onDragStart({ color: tinycolor });
});
});
}
// bind hook for drag stop
if (ctrl.onDragStop) {
colorPickerInstance.on('dragstop.spectrum', (e, tinycolor) => {
$timeout(function () {
ctrl.onDragStop({ color: tinycolor });
});
});
}
}
}
function getColorString (tinyColor, format) {
if (!tinyColor) {
return;
}
switch(format) {
case 'rgb':
return tinyColor.toRgbString();
case 'hsv':
return tinyColor.toHsvString();
case 'hsl':
return tinyColor.toHslString();
case 'name':
return tinyColor.toName();
default:
return tinyColor.toHexString();
}
}
}
angular
.module('umbraco.directives')
.component('umbColorPicker', {
template: '<div class="flex items-center"><div class="umb-color-picker"><input type="hidden" /></div><small ng-if="$ctrl.readonly">{{ $ctrl.value }}</small></div>',
controller: ColorPickerController,
bindings: {
ngModel: '<',
options: '<',
onBeforeShow: '&?',
onShow: '&?',
onHide: '&?',
onChange: '&?',
onMove: '&?',
onDragStart: '&?',
onDragStop: '&?'
}
});
})();