Skip to content

Commit 1e6d679

Browse files
committed
Delaying init until directive link function, to fix a bug in complex multi color picker pages
1 parent 8eddc4c commit 1e6d679

File tree

2 files changed

+74
-76
lines changed

2 files changed

+74
-76
lines changed

src/scripts/controller.js

Lines changed: 73 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,7 @@ export default class AngularColorPickerController {
66
this.$document = _$document;
77
this.$timeout = _$timeout;
88

9-
// browser variables
10-
this.chrome = Boolean(window.chrome);
11-
let _android_version = window.navigator.userAgent.match(/Android\s([0-9\.]*)/i);
12-
this.android_version = _android_version && _android_version.length > 1 ? parseFloat(_android_version[1]) : NaN;
13-
14-
// needed variables
15-
this.onChangeValue = null;
16-
this.updateModel = true;
17-
18-
//---------------------------
19-
// watchers
20-
//---------------------------
21-
22-
// ngModel
23-
24-
this.$scope.$watch('AngularColorPickerController.ngModel', this.watchNgModel.bind(this));
25-
26-
// options
27-
28-
this.$scope.$watch('AngularColorPickerController.options.swatchPos', this.watchSwatchPos.bind(this));
29-
30-
this.$scope.$watchGroup(
31-
[
32-
'AngularColorPickerController.options.format',
33-
'AngularColorPickerController.options.alpha',
34-
'AngularColorPickerController.options.case'
35-
],
36-
this.reInitAndUpdate.bind(this)
37-
);
38-
39-
this.$scope.$watchGroup(
40-
[
41-
'AngularColorPickerController.options.disabled',
42-
'AngularColorPickerController.options.swatchBootstrap',
43-
'AngularColorPickerController.options.swatchOnly',
44-
'AngularColorPickerController.options.swatch',
45-
'AngularColorPickerController.options.pos',
46-
'AngularColorPickerController.options.inline'
47-
],
48-
this.reInit.bind(this)
49-
);
50-
51-
// api
52-
53-
this.$scope.$watch('AngularColorPickerController.api', this.watchApi.bind(this));
54-
55-
// internal
56-
57-
this.$scope.$watch('AngularColorPickerController.swatchColor', this.updateSwatchBackground.bind(this));
58-
59-
this.$scope.$watch('AngularColorPickerController.hue', this.hueUpdate.bind(this));
60-
61-
this.$scope.$watch('AngularColorPickerController.saturation', this.saturationUpdate.bind(this));
62-
63-
this.$scope.$watch('AngularColorPickerController.lightness', this.lightnessUpdate.bind(this));
64-
65-
this.$scope.$watch('AngularColorPickerController.opacity', this.opacityUpdate.bind(this));
66-
67-
//---------------------------
68-
// destroy
69-
//---------------------------
70-
71-
this.$scope.$on('$destroy', () => {
72-
this.$document.off('mousedown', this.onMouseDown);
73-
this.$document.off('mouseup', this.onMouseUp);
74-
this.$document.off('mousemove', this.onMouseMove);
75-
76-
this.eventApiDispatch('onDestroy');
77-
});
78-
79-
//---------------------------
80-
// Init
81-
//---------------------------
82-
83-
this.init();
9+
this.$scope.init = this.init.bind(this);
8410
}
8511

8612
watchNgModel(newValue, oldValue) {
@@ -140,7 +66,7 @@ export default class AngularColorPickerController {
14066
}
14167
}
14268

143-
watchApi () {
69+
setupApi () {
14470
if (!this.api) {
14571
this.api = {};
14672
}
@@ -194,6 +120,76 @@ export default class AngularColorPickerController {
194120
}
195121

196122
init () {
123+
// browser variables
124+
this.chrome = Boolean(window.chrome);
125+
let _android_version = window.navigator.userAgent.match(/Android\s([0-9\.]*)/i);
126+
this.android_version = _android_version && _android_version.length > 1 ? parseFloat(_android_version[1]) : NaN;
127+
128+
// needed variables
129+
this.onChangeValue = null;
130+
this.updateModel = true;
131+
132+
//---------------------------
133+
// watchers
134+
//---------------------------
135+
136+
// ngModel
137+
138+
this.$scope.$watch('AngularColorPickerController.ngModel', this.watchNgModel.bind(this));
139+
140+
// options
141+
142+
this.$scope.$watch('AngularColorPickerController.options.swatchPos', this.watchSwatchPos.bind(this));
143+
144+
this.$scope.$watchGroup(
145+
[
146+
'AngularColorPickerController.options.format',
147+
'AngularColorPickerController.options.alpha',
148+
'AngularColorPickerController.options.case'
149+
],
150+
this.reInitAndUpdate.bind(this)
151+
);
152+
153+
this.$scope.$watchGroup(
154+
[
155+
'AngularColorPickerController.options.disabled',
156+
'AngularColorPickerController.options.swatchBootstrap',
157+
'AngularColorPickerController.options.swatchOnly',
158+
'AngularColorPickerController.options.swatch',
159+
'AngularColorPickerController.options.pos',
160+
'AngularColorPickerController.options.inline'
161+
],
162+
this.reInit.bind(this)
163+
);
164+
165+
// api
166+
167+
this.$scope.$watch('AngularColorPickerController.api', this.setupApi.bind(this));
168+
169+
// internal
170+
171+
this.$scope.$watch('AngularColorPickerController.swatchColor', this.updateSwatchBackground.bind(this));
172+
173+
this.$scope.$watch('AngularColorPickerController.hue', this.hueUpdate.bind(this));
174+
175+
this.$scope.$watch('AngularColorPickerController.saturation', this.saturationUpdate.bind(this));
176+
177+
this.$scope.$watch('AngularColorPickerController.lightness', this.lightnessUpdate.bind(this));
178+
179+
this.$scope.$watch('AngularColorPickerController.opacity', this.opacityUpdate.bind(this));
180+
181+
//---------------------------
182+
// destroy
183+
//---------------------------
184+
185+
this.$scope.$on('$destroy', () => {
186+
this.$document.off('mousedown', this.onMouseDown);
187+
this.$document.off('mouseup', this.onMouseUp);
188+
this.$document.off('mousemove', this.onMouseMove);
189+
190+
this.eventApiDispatch('onDestroy');
191+
});
192+
197193
// if no color provided
198194
if (this.ngModel === undefined) {
199195
this.setDefaults();
@@ -244,6 +240,7 @@ export default class AngularColorPickerController {
244240
onMouseUp (event) {
245241
// no current mouse events and not an element in the picker
246242
if (!this.colorMouse && !this.hueMouse && !this.opacityMouse && this.find(event.target).length === 0) {
243+
this.setupApi(); // TODO - there are some weird times when this is needed to call close. Need to figure out why.
247244
this.api.close(event);
248245
// mouse event on color grid
249246
} else if (this.colorMouse) {

src/scripts/directive.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function colorPickerDirective () {
1616
controllerAs: 'AngularColorPickerController',
1717
link: function ($scope, element, attrs, control) {
1818
$scope.control = control;
19+
$scope.init();
1920
}
2021
};
2122
}

0 commit comments

Comments
 (0)