-
Notifications
You must be signed in to change notification settings - Fork 9
/
angular-colorpicker-dr.js
258 lines (247 loc) · 10.5 KB
/
angular-colorpicker-dr.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
/*global angular*/
/*jslint plusplus: true*/
/*!
* Angular Color Picker
*
* File: angular-colorpicker-dr.js
* Version: 0.1.0
*
* Author: Daniel Reznick
* Info: https://github.com/vedmack/angular-colorpicker
* Contact: vedmack@gmail.com
* Twitter: @danielreznick
* Q&A http://stackoverflow.com
*
* Copyright 2015 Daniel Reznick, all rights reserved.
* Copyright 2015 Released under the MIT License
*
* This source file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
*/
(function () {
'use strict';
var directive = function ($compile, $document, $window) {
return {
restrict: 'A',
require: 'ngModel',
scope: {
changeColor: "&",
tinyTrigger: "@",
colorMe: "@",
ngModel : '='
},
link: function (scope, element, attrs, ngModel) {
var content,
templateHidden = '<div class="color-picker-wrapper body hide">',
templateInline = '<div class="color-picker-wrapper">',
template =
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker navy" color="#001F3F">' +
'</cp-color>' +
'<cp-color class="color-picker blue" color="#0074D9">' +
'</cp-color>' +
'<cp-color class="color-picker aqua" color="#7FDBFF">' +
'</cp-color>' +
'<cp-color class="color-picker teal" color="#39CCCC">' +
'</cp-color>' +
'</div>' +
'</div>' +
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker olive" color="#3D9970">' +
'</cp-color>' +
'<cp-color class="color-picker green" color="#2ECC40">' +
'</cp-color>' +
'<cp-color class="color-picker lime" color="#01FF70">' +
'</cp-color>' +
'<cp-color class="color-picker yellow" color="#FFDC00">' +
'</cp-color>' +
'</div>' +
'</div>' +
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker orange" color="#FF851B">' +
'</cp-color>' +
'<cp-color class="color-picker red" color="#FF4136">' +
'</cp-color>' +
'<cp-color class="color-picker maroon" color="#85144B">' +
'</cp-color>' +
'<cp-color class="color-picker fuchia" color="#F012BE">' +
'</cp-color>' +
'</div>' +
'</div>' +
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker purple" color="#B10DC9">' +
'</cp-color>' +
'<cp-color class="color-picker black" color="#111111">' +
'</cp-color>' +
'<cp-color class="color-picker gray" color="#AAAAAA">' +
'</cp-color>' +
'<cp-color class="color-picker white" color="#FFFFFF">' +
'</cp-color>' +
'</div>' +
'</div>' +
'</div>',
templateTinyTrigger =
'<div class="color-picker-wrapper picker-icon">' +
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker navy">' +
'</cp-color>' +
'<cp-color class="color-picker blue">' +
'</cp-color>' +
'<cp-color class="color-picker aqua">' +
'</cp-color>' +
'<cp-color class="color-picker teal">' +
'</cp-color>' +
'</div>' +
'</div>' +
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker olive">' +
'</cp-color>' +
'<cp-color class="color-picker green">' +
'</cp-color>' +
'<cp-color class="color-picker lime">' +
'</cp-color>' +
'<cp-color class="color-picker yellow">' +
'</cp-color>' +
'</div>' +
'</div>' +
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker orange">' +
'</cp-color>' +
'<cp-color class="color-picker red">' +
'</cp-color>' +
'<cp-color class="color-picker maroon">' +
'</cp-color>' +
'<cp-color class="color-picker fuchia">' +
'</cp-color>' +
'</div>' +
'</div>' +
'<div class="color-picker-row-wrapper">' +
'<div class="color-picker-row">' +
'<cp-color class="color-picker purple">' +
'</cp-color>' +
'<cp-color class="color-picker black">' +
'</cp-color>' +
'<cp-color class="color-picker gray">' +
'</cp-color>' +
'<cp-color class="color-picker white">' +
'</cp-color>' +
'</div>' +
'</div>' +
'</div>',
elementTinyTrigger,
elementColorPicker,
closestFunc,
prevFunc;
prevFunc = function (elem) {
if (elem.previousElementSibling) {
return elem.previousElementSibling;
}
while (elem = elem.previousSibling) {
if (elem.nodeType === 1) {
return elem;
}
}
};
closestFunc = function (elem, cls) {
var found = false;
while (elem.parent() !== undefined && found === false) {
if (elem.parent().hasClass(cls)) {
found = true;
}
elem = elem.parent();
}
return elem;
};
if (scope.tinyTrigger !== undefined && scope.tinyTrigger === 'true') {
templateTinyTrigger = templateTinyTrigger.replace('picker-icon', 'picker-icon trigger');
elementTinyTrigger = $compile(templateTinyTrigger)(scope);
element.replaceWith(elementTinyTrigger);
template = templateHidden + template;
elementColorPicker = angular.element(template);
elementColorPicker.find('cp-color').on('click', function (ev) {
ngModel.$setViewValue(angular.element(ev.target).attr('color'));
});
angular.element(document.body).append(elementColorPicker);
elementTinyTrigger.bind("click", function (ev) {
var wrapper = closestFunc(angular.element(ev.target), 'color-picker-wrapper'),
top = wrapper[0].getBoundingClientRect().top,
height = wrapper[0].getBoundingClientRect().height;
top = top + $window.pageYOffset;
elementColorPicker.removeClass('hide');
elementColorPicker[0].style.top = top + height + 'px';
elementColorPicker[0].style.left = wrapper[0].getBoundingClientRect().left + 'px';
ev.stopPropagation();
});
} else {
if (element[0].tagName === 'INPUT') {
template = templateHidden + template;
elementColorPicker = angular.element(template);
elementColorPicker.find('cp-color').on('click', function (ev) {
var color = angular.element(ev.target).attr('color');
ngModel.$setViewValue(color);
if (scope.colorMe !== undefined && scope.colorMe === 'true') {
element[0].style.backgroundColor = color;
} else {
element.val(color);
}
});
angular.element(document.body).append(elementColorPicker);
element.bind("click", function (ev) {
//show color picker beneath the input
var top = ev.target.getBoundingClientRect().top,
height = ev.target.getBoundingClientRect().height;
top = top + $window.pageYOffset;
elementColorPicker.removeClass('hide');
elementColorPicker[0].style.top = top + height + 'px';
elementColorPicker[0].style.left = ev.target.getBoundingClientRect().left + 'px';
ev.stopPropagation();
});
elementTinyTrigger = $compile(templateTinyTrigger)(scope);
element.after(elementTinyTrigger);
elementTinyTrigger.bind("click", function (ev) {
//show color picker beneath the input
var $wrapper = closestFunc(angular.element(ev.target), 'color-picker-wrapper'),
wrapperPrev = prevFunc($wrapper[0]),
top = wrapperPrev.getBoundingClientRect().top,
height = wrapperPrev.getBoundingClientRect().height;
top = top + $window.pageYOffset;
elementColorPicker.removeClass('hide');
elementColorPicker[0].style.top = top + height + 'px';
elementColorPicker[0].style.left = wrapperPrev.getBoundingClientRect().left + 'px';
ev.stopPropagation();
});
$compile(content)(scope);
} else {
//replace element with the color picker
template = templateInline + template;
elementColorPicker = $compile(template)(scope);
element.replaceWith(elementColorPicker);
elementColorPicker.find('cp-color').on('click', function (ev) {
ngModel.$setViewValue(angular.element(ev.target).attr('color'));
ev.stopPropagation();
});
}
}
//when clicking somewhere on the screen / body -> hide the color picker
$document.bind("click", function (ev) {
var i,
docChildren = $document.find('body').children();
for (i = 0; i < docChildren.length; i++) {
if (docChildren[i].getAttribute("class") && docChildren[i].getAttribute("class").indexOf('color-picker-wrapper body') !== -1) {
angular.element(docChildren[i]).addClass('hide');
}
}
});
}
};
};
angular.module('colorpicker-dr', []).directive('colorPicker',['$compile', '$document', '$window', directive]);
}());