html5forms-rails 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/CHANGELOG.md +5 -0
  2. data/Gemfile +11 -0
  3. data/README.md +208 -0
  4. data/Rakefile +49 -0
  5. data/VERSION +1 -0
  6. data/demos/html5-form-demo.html +79 -0
  7. data/html5forms-rails.gemspec +142 -0
  8. data/lib/html5forms.rb +6 -0
  9. data/vendor/assets/images/colorpicker/blank.gif +0 -0
  10. data/vendor/assets/images/colorpicker/colorpicker_background.png +0 -0
  11. data/vendor/assets/images/colorpicker/colorpicker_hex.png +0 -0
  12. data/vendor/assets/images/colorpicker/colorpicker_hsb_b.png +0 -0
  13. data/vendor/assets/images/colorpicker/colorpicker_hsb_h.png +0 -0
  14. data/vendor/assets/images/colorpicker/colorpicker_hsb_s.png +0 -0
  15. data/vendor/assets/images/colorpicker/colorpicker_indic.gif +0 -0
  16. data/vendor/assets/images/colorpicker/colorpicker_overlay.png +0 -0
  17. data/vendor/assets/images/colorpicker/colorpicker_rgb_b.png +0 -0
  18. data/vendor/assets/images/colorpicker/colorpicker_rgb_g.png +0 -0
  19. data/vendor/assets/images/colorpicker/colorpicker_rgb_r.png +0 -0
  20. data/vendor/assets/images/colorpicker/colorpicker_select.gif +0 -0
  21. data/vendor/assets/images/colorpicker/colorpicker_submit.png +0 -0
  22. data/vendor/assets/images/colorpicker/custom_background.png +0 -0
  23. data/vendor/assets/images/colorpicker/custom_hex.png +0 -0
  24. data/vendor/assets/images/colorpicker/custom_hsb_b.png +0 -0
  25. data/vendor/assets/images/colorpicker/custom_hsb_h.png +0 -0
  26. data/vendor/assets/images/colorpicker/custom_hsb_s.png +0 -0
  27. data/vendor/assets/images/colorpicker/custom_indic.gif +0 -0
  28. data/vendor/assets/images/colorpicker/custom_rgb_b.png +0 -0
  29. data/vendor/assets/images/colorpicker/custom_rgb_g.png +0 -0
  30. data/vendor/assets/images/colorpicker/custom_rgb_r.png +0 -0
  31. data/vendor/assets/images/colorpicker/custom_submit.png +0 -0
  32. data/vendor/assets/images/colorpicker/select.png +0 -0
  33. data/vendor/assets/images/colorpicker/select2.png +0 -0
  34. data/vendor/assets/images/colorpicker/slider.png +0 -0
  35. data/vendor/assets/images/h5f/form_validation.png +0 -0
  36. data/vendor/assets/images/html5form-shim/asterisk.png +0 -0
  37. data/vendor/assets/images/html5form-shim/down.png +0 -0
  38. data/vendor/assets/images/html5form-shim/fail.png +0 -0
  39. data/vendor/assets/images/html5form-shim/ok.png +0 -0
  40. data/vendor/assets/images/html5forms/jscolor/arrow.gif +0 -0
  41. data/vendor/assets/images/html5forms/jscolor/cross.gif +0 -0
  42. data/vendor/assets/images/html5forms/jscolor/hs.png +0 -0
  43. data/vendor/assets/images/html5forms/jscolor/hv.png +0 -0
  44. data/vendor/assets/images/html5forms/slider/slider-1.png +0 -0
  45. data/vendor/assets/images/html5forms/slider/slider-disabled-1.png +0 -0
  46. data/vendor/assets/images/html5forms/slider/slider-disabled.png +0 -0
  47. data/vendor/assets/images/html5forms/slider/slider.png +0 -0
  48. data/vendor/assets/javascripts/colorpicker.js +484 -0
  49. data/vendor/assets/javascripts/colorpicker.min.js +9 -0
  50. data/vendor/assets/javascripts/h5f.js +328 -0
  51. data/vendor/assets/javascripts/h5f.min.js +4 -0
  52. data/vendor/assets/javascripts/html5forms/EventHelpers.min.js +15 -0
  53. data/vendor/assets/javascripts/html5forms/autocomplete.min.js +1 -0
  54. data/vendor/assets/javascripts/html5forms/cssQuery-p.min.js +6 -0
  55. data/vendor/assets/javascripts/html5forms/dev/EventHelpers.js +486 -0
  56. data/vendor/assets/javascripts/html5forms/dev/autocomplete.js +387 -0
  57. data/vendor/assets/javascripts/html5forms/dev/cssQuery-p.js +6 -0
  58. data/vendor/assets/javascripts/html5forms/dev/html5.js +121 -0
  59. data/vendor/assets/javascripts/html5forms/dev/html5Forms.js +892 -0
  60. data/vendor/assets/javascripts/html5forms/dev/html5Widgets.js +1417 -0
  61. data/vendor/assets/javascripts/html5forms/dev/jscolor.js +840 -0
  62. data/vendor/assets/javascripts/html5forms/dev/slider.js +797 -0
  63. data/vendor/assets/javascripts/html5forms/dev/timer.js +137 -0
  64. data/vendor/assets/javascripts/html5forms/dev/visibleIf.js +1100 -0
  65. data/vendor/assets/javascripts/html5forms/html5.min.js +2 -0
  66. data/vendor/assets/javascripts/html5forms/html5Forms.min.js +1 -0
  67. data/vendor/assets/javascripts/html5forms/html5Widgets.min.js +20 -0
  68. data/vendor/assets/javascripts/html5forms/jscolor.min.js +10 -0
  69. data/vendor/assets/javascripts/html5forms/slider.min.js +25 -0
  70. data/vendor/assets/javascripts/html5forms/timer.min.js +1 -0
  71. data/vendor/assets/javascripts/html5forms/visibleIf.min.js +19 -0
  72. data/vendor/assets/javascripts/html5forms.fallback.js +115 -0
  73. data/vendor/assets/javascripts/html5forms.fallback.min.js +11 -0
  74. data/vendor/assets/javascripts/jquery.html5form-shim.js +402 -0
  75. data/vendor/assets/javascripts/jquery.html5form.min.js +4 -0
  76. data/vendor/assets/javascripts/jquery.placehold.min.js +7 -0
  77. data/vendor/assets/javascripts/ui.spinner.js +649 -0
  78. data/vendor/assets/javascripts/ui.spinner.min.js +7 -0
  79. data/vendor/assets/javascripts/webforms2/webforms2-msie.js +1 -0
  80. data/vendor/assets/javascripts/webforms2/webforms2-p.js +14 -0
  81. data/vendor/assets/javascripts/webforms2/webforms2.js +14 -0
  82. data/vendor/assets/javascripts/webforms2/webforms2_src.js +3195 -0
  83. data/vendor/assets/stylesheets/colorpicker.css +161 -0
  84. data/vendor/assets/stylesheets/h5f.css +86 -0
  85. data/vendor/assets/stylesheets/html5form-shim.css +109 -0
  86. data/vendor/assets/stylesheets/html5forms/number.css +35 -0
  87. data/vendor/assets/stylesheets/html5forms/slider.css +169 -0
  88. data/vendor/assets/stylesheets/html5forms/slider_ie.css +41 -0
  89. data/vendor/assets/stylesheets/html5forms/visibleIf.css +23 -0
  90. data/vendor/assets/stylesheets/html5forms.layout.css +116 -0
  91. data/vendor/assets/stylesheets/ui.spinner.css +3 -0
  92. data/vendor/assets/stylesheets/webforms2.css +42 -0
  93. metadata +221 -0
@@ -0,0 +1,840 @@
1
+ /**
2
+ * jscolor, JavaScript Color Picker
3
+ *
4
+ * @version 1.3.1
5
+ * @license GNU Lesser General Public License, http://www.gnu.org/copyleft/lesser.html
6
+ * @author Jan Odvarko, http://odvarko.cz
7
+ * @created 2008-06-15
8
+ * @updated 2010-01-23
9
+ * @link http://jscolor.com
10
+ */
11
+
12
+
13
+ var jscolor = {
14
+
15
+
16
+ dir : '', // location of jscolor directory (leave empty to autodetect)
17
+ bindClass : 'color', // class name
18
+ binding : true, // automatic binding via <input class="...">
19
+ preloading : true, // use image preloading?
20
+
21
+
22
+ install : function() {
23
+ jscolor.addEvent(window, 'load', jscolor.init);
24
+ },
25
+
26
+
27
+ init : function() {
28
+ if(jscolor.binding) {
29
+ jscolor.bind();
30
+ }
31
+ if(jscolor.preloading) {
32
+ jscolor.preload();
33
+ }
34
+ },
35
+
36
+
37
+ getDir : function() {
38
+ if(!jscolor.dir) {
39
+ var detected = jscolor.detectDir();
40
+ jscolor.dir = detected!==false ? detected : 'jscolor/';
41
+ }
42
+ return jscolor.dir;
43
+ },
44
+
45
+
46
+ detectDir : function() {
47
+ var base = location.href;
48
+
49
+ var e = document.getElementsByTagName('base');
50
+ for(var i=0; i<e.length; i+=1) {
51
+ if(e[i].href) { base = e[i].href; }
52
+ }
53
+
54
+ var e = document.getElementsByTagName('script');
55
+ for(var i=0; i<e.length; i+=1) {
56
+ if(e[i].src && /(^|\/)jscolor\.js([?#].*)?$/i.test(e[i].src)) {
57
+ var src = new jscolor.URI(e[i].src);
58
+ var srcAbs = src.toAbsolute(base);
59
+ srcAbs.path = srcAbs.path.replace(/[^\/]+$/, ''); // remove filename
60
+ srcAbs.query = null;
61
+ srcAbs.fragment = null;
62
+ return srcAbs.toString();
63
+ }
64
+ }
65
+ return false;
66
+ },
67
+
68
+
69
+ bind : function() {
70
+ var matchClass = new RegExp('(^|\\s)('+jscolor.bindClass+')\\s*(\\{[^}]*\\})?', 'i');
71
+ var e = document.getElementsByTagName('input');
72
+ for(var i=0; i<e.length; i+=1) {
73
+ var m;
74
+ if(!e[i].color && e[i].className && (m = e[i].className.match(matchClass))) {
75
+ var prop = {};
76
+ if(m[3]) {
77
+ try {
78
+ eval('prop='+m[3]);
79
+ } catch(eInvalidProp) {}
80
+ }
81
+ e[i].color = new jscolor.color(e[i], prop);
82
+ }
83
+ }
84
+ },
85
+
86
+
87
+ preload : function() {
88
+ for(var fn in jscolor.imgRequire) {
89
+ if(jscolor.imgRequire.hasOwnProperty(fn)) {
90
+ jscolor.loadImage(fn);
91
+ }
92
+ }
93
+ },
94
+
95
+
96
+ images : {
97
+ pad : [ 181, 101 ],
98
+ sld : [ 16, 101 ],
99
+ cross : [ 15, 15 ],
100
+ arrow : [ 7, 11 ]
101
+ },
102
+
103
+
104
+ imgRequire : {},
105
+ imgLoaded : {},
106
+
107
+
108
+ requireImage : function(filename) {
109
+ jscolor.imgRequire[filename] = true;
110
+ },
111
+
112
+
113
+ loadImage : function(filename) {
114
+ if(!jscolor.imgLoaded[filename]) {
115
+ jscolor.imgLoaded[filename] = new Image();
116
+ jscolor.imgLoaded[filename].src = jscolor.getDir()+filename;
117
+ }
118
+ },
119
+
120
+
121
+ fetchElement : function(mixed) {
122
+ return typeof mixed === 'string' ? document.getElementById(mixed) : mixed;
123
+ },
124
+
125
+
126
+ addEvent : function(el, evnt, func) {
127
+ if(el.addEventListener) {
128
+ el.addEventListener(evnt, func, false);
129
+ } else if(el.attachEvent) {
130
+ el.attachEvent('on'+evnt, func);
131
+ }
132
+ },
133
+
134
+
135
+ fireEvent : function(el, evnt) {
136
+ if(!el) {
137
+ return;
138
+ }
139
+ if(document.createEventObject) {
140
+ var ev = document.createEventObject();
141
+ el.fireEvent('on'+evnt, ev);
142
+ } else if(document.createEvent) {
143
+ var ev = document.createEvent('HTMLEvents');
144
+ ev.initEvent(evnt, true, true);
145
+ el.dispatchEvent(ev);
146
+ } else if(el['on'+evnt]) { // alternatively use the traditional event model (IE5)
147
+ el['on'+evnt]();
148
+ }
149
+ },
150
+
151
+
152
+ getElementPos : function(e) {
153
+ var e1=e, e2=e;
154
+ var x=0, y=0;
155
+ if(e1.offsetParent) {
156
+ do {
157
+ x += e1.offsetLeft;
158
+ y += e1.offsetTop;
159
+ } while(e1 = e1.offsetParent);
160
+ }
161
+ while((e2 = e2.parentNode) && e2.nodeName.toUpperCase() !== 'BODY') {
162
+ x -= e2.scrollLeft;
163
+ y -= e2.scrollTop;
164
+ }
165
+ return [x, y];
166
+ },
167
+
168
+
169
+ getElementSize : function(e) {
170
+ return [e.offsetWidth, e.offsetHeight];
171
+ },
172
+
173
+
174
+ getMousePos : function(e) {
175
+ if(!e) { e = window.event; }
176
+ if(typeof e.pageX === 'number') {
177
+ return [e.pageX, e.pageY];
178
+ } else if(typeof e.clientX === 'number') {
179
+ return [
180
+ e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
181
+ e.clientY + document.body.scrollTop + document.documentElement.scrollTop
182
+ ];
183
+ }
184
+ },
185
+
186
+
187
+ getViewPos : function() {
188
+ if(typeof window.pageYOffset === 'number') {
189
+ return [window.pageXOffset, window.pageYOffset];
190
+ } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
191
+ return [document.body.scrollLeft, document.body.scrollTop];
192
+ } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
193
+ return [document.documentElement.scrollLeft, document.documentElement.scrollTop];
194
+ } else {
195
+ return [0, 0];
196
+ }
197
+ },
198
+
199
+
200
+ getViewSize : function() {
201
+ if(typeof window.innerWidth === 'number') {
202
+ return [window.innerWidth, window.innerHeight];
203
+ } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
204
+ return [document.body.clientWidth, document.body.clientHeight];
205
+ } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
206
+ return [document.documentElement.clientWidth, document.documentElement.clientHeight];
207
+ } else {
208
+ return [0, 0];
209
+ }
210
+ },
211
+
212
+
213
+ URI : function(uri) { // See RFC3986
214
+
215
+ this.scheme = null;
216
+ this.authority = null;
217
+ this.path = '';
218
+ this.query = null;
219
+ this.fragment = null;
220
+
221
+ this.parse = function(uri) {
222
+ var m = uri.match(/^(([A-Za-z][0-9A-Za-z+.-]*)(:))?((\/\/)([^\/?#]*))?([^?#]*)((\?)([^#]*))?((#)(.*))?/);
223
+ this.scheme = m[3] ? m[2] : null;
224
+ this.authority = m[5] ? m[6] : null;
225
+ this.path = m[7];
226
+ this.query = m[9] ? m[10] : null;
227
+ this.fragment = m[12] ? m[13] : null;
228
+ return this;
229
+ };
230
+
231
+ this.toString = function() {
232
+ var result = '';
233
+ if(this.scheme !== null) { result = result + this.scheme + ':'; }
234
+ if(this.authority !== null) { result = result + '//' + this.authority; }
235
+ if(this.path !== null) { result = result + this.path; }
236
+ if(this.query !== null) { result = result + '?' + this.query; }
237
+ if(this.fragment !== null) { result = result + '#' + this.fragment; }
238
+ return result;
239
+ };
240
+
241
+ this.toAbsolute = function(base) {
242
+ var base = new jscolor.URI(base);
243
+ var r = this;
244
+ var t = new jscolor.URI;
245
+
246
+ if(base.scheme === null) { return false; }
247
+
248
+ if(r.scheme !== null && r.scheme.toLowerCase() === base.scheme.toLowerCase()) {
249
+ r.scheme = null;
250
+ }
251
+
252
+ if(r.scheme !== null) {
253
+ t.scheme = r.scheme;
254
+ t.authority = r.authority;
255
+ t.path = removeDotSegments(r.path);
256
+ t.query = r.query;
257
+ } else {
258
+ if(r.authority !== null) {
259
+ t.authority = r.authority;
260
+ t.path = removeDotSegments(r.path);
261
+ t.query = r.query;
262
+ } else {
263
+ if(r.path === '') { // TODO: == or === ?
264
+ t.path = base.path;
265
+ if(r.query !== null) {
266
+ t.query = r.query;
267
+ } else {
268
+ t.query = base.query;
269
+ }
270
+ } else {
271
+ if(r.path.substr(0,1) === '/') {
272
+ t.path = removeDotSegments(r.path);
273
+ } else {
274
+ if(base.authority !== null && base.path === '') { // TODO: == or === ?
275
+ t.path = '/'+r.path;
276
+ } else {
277
+ t.path = base.path.replace(/[^\/]+$/,'')+r.path;
278
+ }
279
+ t.path = removeDotSegments(t.path);
280
+ }
281
+ t.query = r.query;
282
+ }
283
+ t.authority = base.authority;
284
+ }
285
+ t.scheme = base.scheme;
286
+ }
287
+ t.fragment = r.fragment;
288
+
289
+ return t;
290
+ };
291
+
292
+ function removeDotSegments(path) {
293
+ var out = '';
294
+ while(path) {
295
+ if(path.substr(0,3)==='../' || path.substr(0,2)==='./') {
296
+ path = path.replace(/^\.+/,'').substr(1);
297
+ } else if(path.substr(0,3)==='/./' || path==='/.') {
298
+ path = '/'+path.substr(3);
299
+ } else if(path.substr(0,4)==='/../' || path==='/..') {
300
+ path = '/'+path.substr(4);
301
+ out = out.replace(/\/?[^\/]*$/, '');
302
+ } else if(path==='.' || path==='..') {
303
+ path = '';
304
+ } else {
305
+ var rm = path.match(/^\/?[^\/]*/)[0];
306
+ path = path.substr(rm.length);
307
+ out = out + rm;
308
+ }
309
+ }
310
+ return out;
311
+ }
312
+
313
+ if(uri) {
314
+ this.parse(uri);
315
+ }
316
+
317
+ },
318
+
319
+
320
+ /*
321
+ * Usage example:
322
+ * var myColor = new jscolor.color(myInputElement)
323
+ */
324
+
325
+ color : function(target, prop) {
326
+
327
+
328
+ this.required = true; // refuse empty values?
329
+ this.adjust = true; // adjust value to uniform notation?
330
+ this.hash = false; // prefix color with # symbol?
331
+ this.caps = true; // uppercase?
332
+ this.valueElement = target; // value holder
333
+ this.styleElement = target; // where to reflect current color
334
+ this.hsv = [0, 0, 1]; // read-only 0-6, 0-1, 0-1
335
+ this.rgb = [1, 1, 1]; // read-only 0-1, 0-1, 0-1
336
+
337
+ this.pickerOnfocus = true; // display picker on focus?
338
+ this.pickerMode = 'HSV'; // HSV | HVS
339
+ this.pickerPosition = 'bottom'; // left | right | top | bottom
340
+ this.pickerFace = 10; // px
341
+ this.pickerFaceColor = 'ThreeDFace'; // CSS color
342
+ this.pickerBorder = 1; // px
343
+ this.pickerBorderColor = 'ThreeDHighlight ThreeDShadow ThreeDShadow ThreeDHighlight'; // CSS color
344
+ this.pickerInset = 1; // px
345
+ this.pickerInsetColor = 'ThreeDShadow ThreeDHighlight ThreeDHighlight ThreeDShadow'; // CSS color
346
+ this.pickerZIndex = 10000;
347
+
348
+
349
+ for(var p in prop) {
350
+ if(prop.hasOwnProperty(p)) {
351
+ this[p] = prop[p];
352
+ }
353
+ }
354
+
355
+
356
+ this.hidePicker = function() {
357
+ if(isPickerOwner()) {
358
+ removePicker();
359
+ }
360
+ };
361
+
362
+
363
+ this.showPicker = function() {
364
+ if(!isPickerOwner()) {
365
+ var tp = jscolor.getElementPos(target); // target pos
366
+ var ts = jscolor.getElementSize(target); // target size
367
+ var vp = jscolor.getViewPos(); // view pos
368
+ var vs = jscolor.getViewSize(); // view size
369
+ var ps = [ // picker size
370
+ 2*this.pickerBorder + 4*this.pickerInset + 2*this.pickerFace + jscolor.images.pad[0] + 2*jscolor.images.arrow[0] + jscolor.images.sld[0],
371
+ 2*this.pickerBorder + 2*this.pickerInset + 2*this.pickerFace + jscolor.images.pad[1]
372
+ ];
373
+ var a, b, c;
374
+ switch(this.pickerPosition.toLowerCase()) {
375
+ case 'left': a=1; b=0; c=-1; break;
376
+ case 'right':a=1; b=0; c=1; break;
377
+ case 'top': a=0; b=1; c=-1; break;
378
+ default: a=0; b=1; c=1; break;
379
+ }
380
+ var l = (ts[b]+ps[b])/2;
381
+ var pp = [ // picker pos
382
+ -vp[a]+tp[a]+ps[a] > vs[a] ?
383
+ (-vp[a]+tp[a]+ts[a]/2 > vs[a]/2 && tp[a]+ts[a]-ps[a] >= 0 ? tp[a]+ts[a]-ps[a] : tp[a]) :
384
+ tp[a],
385
+ -vp[b]+tp[b]+ts[b]+ps[b]-l+l*c > vs[b] ?
386
+ (-vp[b]+tp[b]+ts[b]/2 > vs[b]/2 && tp[b]+ts[b]-l-l*c >= 0 ? tp[b]+ts[b]-l-l*c : tp[b]+ts[b]-l+l*c) :
387
+ (tp[b]+ts[b]-l+l*c >= 0 ? tp[b]+ts[b]-l+l*c : tp[b]+ts[b]-l-l*c)
388
+ ];
389
+ drawPicker(pp[a], pp[b]);
390
+ }
391
+ };
392
+
393
+
394
+ this.importColor = function() {
395
+ if(!valueElement) {
396
+ this.exportColor();
397
+ } else {
398
+ if(!this.adjust) {
399
+ if(!this.fromString(valueElement.value, leaveValue)) {
400
+ styleElement.style.backgroundColor = styleElement.jscStyle.backgroundColor;
401
+ styleElement.style.color = styleElement.jscStyle.color;
402
+ this.exportColor(leaveValue | leaveStyle);
403
+ }
404
+ } else if(!this.required && /^\s*$/.test(valueElement.value)) {
405
+ valueElement.value = '';
406
+ styleElement.style.backgroundColor = styleElement.jscStyle.backgroundColor;
407
+ styleElement.style.color = styleElement.jscStyle.color;
408
+ this.exportColor(leaveValue | leaveStyle);
409
+
410
+ } else if(this.fromString(valueElement.value)) {
411
+ // OK
412
+ } else {
413
+ this.exportColor();
414
+ }
415
+ }
416
+ };
417
+
418
+
419
+ this.exportColor = function(flags) {
420
+ if(!(flags & leaveValue) && valueElement) {
421
+ var value = this.toString();
422
+ if(this.caps) { value = value.toUpperCase(); }
423
+ if(this.hash) { value = '#'+value; }
424
+ valueElement.value = value;
425
+ }
426
+ if(!(flags & leaveStyle) && styleElement) {
427
+ styleElement.style.backgroundColor =
428
+ '#'+this.toString();
429
+ styleElement.style.color =
430
+ 0.213 * this.rgb[0] +
431
+ 0.715 * this.rgb[1] +
432
+ 0.072 * this.rgb[2]
433
+ < 0.5 ? '#FFF' : '#000';
434
+ }
435
+ if(!(flags & leavePad) && isPickerOwner()) {
436
+ redrawPad();
437
+ }
438
+ if(!(flags & leaveSld) && isPickerOwner()) {
439
+ redrawSld();
440
+ }
441
+ };
442
+
443
+
444
+ this.fromHSV = function(h, s, v, flags) { // null = don't change
445
+ h<0 && (h=0) || h>6 && (h=6);
446
+ s<0 && (s=0) || s>1 && (s=1);
447
+ v<0 && (v=0) || v>1 && (v=1);
448
+ this.rgb = HSV_RGB(
449
+ h===null ? this.hsv[0] : (this.hsv[0]=h),
450
+ s===null ? this.hsv[1] : (this.hsv[1]=s),
451
+ v===null ? this.hsv[2] : (this.hsv[2]=v)
452
+ );
453
+ this.exportColor(flags);
454
+ };
455
+
456
+
457
+ this.fromRGB = function(r, g, b, flags) { // null = don't change
458
+ r<0 && (r=0) || r>1 && (r=1);
459
+ g<0 && (g=0) || g>1 && (g=1);
460
+ b<0 && (b=0) || b>1 && (b=1);
461
+ var hsv = RGB_HSV(
462
+ r===null ? this.rgb[0] : (this.rgb[0]=r),
463
+ g===null ? this.rgb[1] : (this.rgb[1]=g),
464
+ b===null ? this.rgb[2] : (this.rgb[2]=b)
465
+ );
466
+ if(hsv[0] !== null) {
467
+ this.hsv[0] = hsv[0];
468
+ }
469
+ if(hsv[2] !== 0) {
470
+ this.hsv[1] = hsv[1];
471
+ }
472
+ this.hsv[2] = hsv[2];
473
+ this.exportColor(flags);
474
+ };
475
+
476
+
477
+ this.fromString = function(hex, flags) {
478
+ var m = hex.match(/^\W*([0-9A-F]{3}([0-9A-F]{3})?)\W*$/i);
479
+ if(!m) {
480
+ return false;
481
+ } else {
482
+ if(m[1].length === 6) { // 6-char notation
483
+ this.fromRGB(
484
+ parseInt(m[1].substr(0,2),16) / 255,
485
+ parseInt(m[1].substr(2,2),16) / 255,
486
+ parseInt(m[1].substr(4,2),16) / 255,
487
+ flags
488
+ );
489
+ } else { // 3-char notation
490
+ this.fromRGB(
491
+ parseInt(m[1].charAt(0)+m[1].charAt(0),16) / 255,
492
+ parseInt(m[1].charAt(1)+m[1].charAt(1),16) / 255,
493
+ parseInt(m[1].charAt(2)+m[1].charAt(2),16) / 255,
494
+ flags
495
+ );
496
+ }
497
+ return true;
498
+ }
499
+ };
500
+
501
+
502
+ this.toString = function() {
503
+ return (
504
+ (0x100 | Math.round(255*this.rgb[0])).toString(16).substr(1) +
505
+ (0x100 | Math.round(255*this.rgb[1])).toString(16).substr(1) +
506
+ (0x100 | Math.round(255*this.rgb[2])).toString(16).substr(1)
507
+ );
508
+ };
509
+
510
+
511
+ function RGB_HSV(r, g, b) {
512
+ var n = Math.min(Math.min(r,g),b);
513
+ var v = Math.max(Math.max(r,g),b);
514
+ var m = v - n;
515
+ if(m === 0) { return [ null, 0, v ]; }
516
+ var h = r===n ? 3+(b-g)/m : (g===n ? 5+(r-b)/m : 1+(g-r)/m);
517
+ return [ h===6?0:h, m/v, v ];
518
+ }
519
+
520
+
521
+ function HSV_RGB(h, s, v) {
522
+ if(h === null) { return [ v, v, v ]; }
523
+ var i = Math.floor(h);
524
+ var f = i%2 ? h-i : 1-(h-i);
525
+ var m = v * (1 - s);
526
+ var n = v * (1 - s*f);
527
+ switch(i) {
528
+ case 6:
529
+ case 0: return [v,n,m];
530
+ case 1: return [n,v,m];
531
+ case 2: return [m,v,n];
532
+ case 3: return [m,n,v];
533
+ case 4: return [n,m,v];
534
+ case 5: return [v,m,n];
535
+ }
536
+ }
537
+
538
+
539
+ function removePicker() {
540
+ delete jscolor.picker.owner;
541
+ document.getElementsByTagName('body')[0].removeChild(jscolor.picker.boxB);
542
+ }
543
+
544
+
545
+ function drawPicker(x, y) {
546
+ if(!jscolor.picker) {
547
+ jscolor.picker = {
548
+ box : document.createElement('div'),
549
+ boxB : document.createElement('div'),
550
+ pad : document.createElement('div'),
551
+ padB : document.createElement('div'),
552
+ padM : document.createElement('div'),
553
+ sld : document.createElement('div'),
554
+ sldB : document.createElement('div'),
555
+ sldM : document.createElement('div')
556
+ };
557
+ for(var i=0,segSize=4; i<jscolor.images.sld[1]; i+=segSize) {
558
+ var seg = document.createElement('div');
559
+ seg.style.height = segSize+'px';
560
+ seg.style.fontSize = '1px';
561
+ seg.style.lineHeight = '0';
562
+ jscolor.picker.sld.appendChild(seg);
563
+ }
564
+ jscolor.picker.sldB.appendChild(jscolor.picker.sld);
565
+ jscolor.picker.box.appendChild(jscolor.picker.sldB);
566
+ jscolor.picker.box.appendChild(jscolor.picker.sldM);
567
+ jscolor.picker.padB.appendChild(jscolor.picker.pad);
568
+ jscolor.picker.box.appendChild(jscolor.picker.padB);
569
+ jscolor.picker.box.appendChild(jscolor.picker.padM);
570
+ jscolor.picker.boxB.appendChild(jscolor.picker.box);
571
+ }
572
+
573
+ var p = jscolor.picker;
574
+
575
+ // recompute controls positions
576
+ posPad = [
577
+ x+THIS.pickerBorder+THIS.pickerFace+THIS.pickerInset,
578
+ y+THIS.pickerBorder+THIS.pickerFace+THIS.pickerInset ];
579
+ posSld = [
580
+ null,
581
+ y+THIS.pickerBorder+THIS.pickerFace+THIS.pickerInset ];
582
+
583
+ // controls interaction
584
+ p.box.onmouseup =
585
+ p.box.onmouseout = function() { target.focus(); };
586
+ p.box.onmousedown = function() { abortBlur=true; };
587
+ p.box.onmousemove = function(e) { holdPad && setPad(e); holdSld && setSld(e); };
588
+ p.padM.onmouseup =
589
+ p.padM.onmouseout = function() { if(holdPad) { holdPad=false; jscolor.fireEvent(valueElement,'change'); } };
590
+ p.padM.onmousedown = function(e) { holdPad=true; setPad(e); };
591
+ p.sldM.onmouseup =
592
+ p.sldM.onmouseout = function() { if(holdSld) { holdSld=false; jscolor.fireEvent(valueElement,'change'); } };
593
+ p.sldM.onmousedown = function(e) { holdSld=true; setSld(e); };
594
+
595
+ // picker
596
+ p.box.style.width = 4*THIS.pickerInset + 2*THIS.pickerFace + jscolor.images.pad[0] + 2*jscolor.images.arrow[0] + jscolor.images.sld[0] + 'px';
597
+ p.box.style.height = 2*THIS.pickerInset + 2*THIS.pickerFace + jscolor.images.pad[1] + 'px';
598
+
599
+ // picker border
600
+ p.boxB.style.position = 'absolute';
601
+ p.boxB.style.clear = 'both';
602
+ p.boxB.style.left = x+'px';
603
+ p.boxB.style.top = y+'px';
604
+ p.boxB.style.zIndex = THIS.pickerZIndex;
605
+ p.boxB.style.border = THIS.pickerBorder+'px solid';
606
+ p.boxB.style.borderColor = THIS.pickerBorderColor;
607
+ p.boxB.style.background = THIS.pickerFaceColor;
608
+
609
+ // pad image
610
+ p.pad.style.width = jscolor.images.pad[0]+'px';
611
+ p.pad.style.height = jscolor.images.pad[1]+'px';
612
+
613
+ // pad border
614
+ p.padB.style.position = 'absolute';
615
+ p.padB.style.left = THIS.pickerFace+'px';
616
+ p.padB.style.top = THIS.pickerFace+'px';
617
+ p.padB.style.border = THIS.pickerInset+'px solid';
618
+ p.padB.style.borderColor = THIS.pickerInsetColor;
619
+
620
+ // pad mouse area
621
+ p.padM.style.position = 'absolute';
622
+ p.padM.style.left = '0';
623
+ p.padM.style.top = '0';
624
+ p.padM.style.width = THIS.pickerFace + 2*THIS.pickerInset + jscolor.images.pad[0] + jscolor.images.arrow[0] + 'px';
625
+ p.padM.style.height = p.box.style.height;
626
+ p.padM.style.cursor = 'crosshair';
627
+
628
+ // slider image
629
+ p.sld.style.overflow = 'hidden';
630
+ p.sld.style.width = jscolor.images.sld[0]+'px';
631
+ p.sld.style.height = jscolor.images.sld[1]+'px';
632
+
633
+ // slider border
634
+ p.sldB.style.position = 'absolute';
635
+ p.sldB.style.right = THIS.pickerFace+'px';
636
+ p.sldB.style.top = THIS.pickerFace+'px';
637
+ p.sldB.style.border = THIS.pickerInset+'px solid';
638
+ p.sldB.style.borderColor = THIS.pickerInsetColor;
639
+
640
+ // slider mouse area
641
+ p.sldM.style.position = 'absolute';
642
+ p.sldM.style.right = '0';
643
+ p.sldM.style.top = '0';
644
+ p.sldM.style.width = jscolor.images.sld[0] + jscolor.images.arrow[0] + THIS.pickerFace + 2*THIS.pickerInset + 'px';
645
+ p.sldM.style.height = p.box.style.height;
646
+ try {
647
+ p.sldM.style.cursor = 'pointer';
648
+ } catch(eOldIE) {
649
+ p.sldM.style.cursor = 'hand';
650
+ }
651
+
652
+ // load images in optimal order
653
+ switch(modeID) {
654
+ case 0: var padImg = 'hs.png'; break;
655
+ case 1: var padImg = 'hv.png'; break;
656
+ }
657
+ p.padM.style.background = "url('"+jscolor.getDir()+"cross.gif') no-repeat";
658
+ p.sldM.style.background = "url('"+jscolor.getDir()+"arrow.gif') no-repeat";
659
+ p.pad.style.background = "url('"+jscolor.getDir()+padImg+"') 0 0 no-repeat";
660
+
661
+ // place pointers
662
+ redrawPad();
663
+ redrawSld();
664
+
665
+ jscolor.picker.owner = THIS;
666
+ document.getElementsByTagName('body')[0].appendChild(p.boxB);
667
+ }
668
+
669
+
670
+ function redrawPad() {
671
+ // redraw the pad pointer
672
+ switch(modeID) {
673
+ case 0: var yComponent = 1; break;
674
+ case 1: var yComponent = 2; break;
675
+ }
676
+ var x = Math.round((THIS.hsv[0]/6) * (jscolor.images.pad[0]-1));
677
+ var y = Math.round((1-THIS.hsv[yComponent]) * (jscolor.images.pad[1]-1));
678
+ jscolor.picker.padM.style.backgroundPosition =
679
+ (THIS.pickerFace+THIS.pickerInset+x - Math.floor(jscolor.images.cross[0]/2)) + 'px ' +
680
+ (THIS.pickerFace+THIS.pickerInset+y - Math.floor(jscolor.images.cross[1]/2)) + 'px';
681
+
682
+ // redraw the slider image
683
+ var seg = jscolor.picker.sld.childNodes;
684
+
685
+ switch(modeID) {
686
+ case 0:
687
+ var rgb = HSV_RGB(THIS.hsv[0], THIS.hsv[1], 1);
688
+ for(var i=0; i<seg.length; i+=1) {
689
+ seg[i].style.backgroundColor = 'rgb('+
690
+ (rgb[0]*(1-i/seg.length)*100)+'%,'+
691
+ (rgb[1]*(1-i/seg.length)*100)+'%,'+
692
+ (rgb[2]*(1-i/seg.length)*100)+'%)';
693
+ }
694
+ break;
695
+ case 1:
696
+ var rgb, s, c = [ THIS.hsv[2], 0, 0 ];
697
+ var i = Math.floor(THIS.hsv[0]);
698
+ var f = i%2 ? THIS.hsv[0]-i : 1-(THIS.hsv[0]-i);
699
+ switch(i) {
700
+ case 6:
701
+ case 0: rgb=[0,1,2]; break;
702
+ case 1: rgb=[1,0,2]; break;
703
+ case 2: rgb=[2,0,1]; break;
704
+ case 3: rgb=[2,1,0]; break;
705
+ case 4: rgb=[1,2,0]; break;
706
+ case 5: rgb=[0,2,1]; break;
707
+ }
708
+ for(var i=0; i<seg.length; i+=1) {
709
+ s = 1 - 1/(seg.length-1)*i;
710
+ c[1] = c[0] * (1 - s*f);
711
+ c[2] = c[0] * (1 - s);
712
+ seg[i].style.backgroundColor = 'rgb('+
713
+ (c[rgb[0]]*100)+'%,'+
714
+ (c[rgb[1]]*100)+'%,'+
715
+ (c[rgb[2]]*100)+'%)';
716
+ }
717
+ break;
718
+ }
719
+ }
720
+
721
+
722
+ function redrawSld() {
723
+ // redraw the slider pointer
724
+ switch(modeID) {
725
+ case 0: var yComponent = 2; break;
726
+ case 1: var yComponent = 1; break;
727
+ }
728
+ var y = Math.round((1-THIS.hsv[yComponent]) * (jscolor.images.sld[1]-1));
729
+ jscolor.picker.sldM.style.backgroundPosition =
730
+ '0 ' + (THIS.pickerFace+THIS.pickerInset+y - Math.floor(jscolor.images.arrow[1]/2)) + 'px';
731
+ }
732
+
733
+
734
+ function isPickerOwner() {
735
+ return jscolor.picker && jscolor.picker.owner === THIS;
736
+ }
737
+
738
+
739
+ function blurTarget() {
740
+ if(valueElement === target) {
741
+ THIS.importColor();
742
+ }
743
+ if(THIS.pickerOnfocus) {
744
+ THIS.hidePicker();
745
+ }
746
+ }
747
+
748
+
749
+ function blurValue() {
750
+ if(valueElement !== target) {
751
+ THIS.importColor();
752
+ }
753
+ }
754
+
755
+
756
+ function setPad(e) {
757
+ var posM = jscolor.getMousePos(e);
758
+ var x = posM[0]-posPad[0];
759
+ var y = posM[1]-posPad[1];
760
+ switch(modeID) {
761
+ case 0: THIS.fromHSV(x*(6/(jscolor.images.pad[0]-1)), 1 - y/(jscolor.images.pad[1]-1), null, leaveSld); break;
762
+ case 1: THIS.fromHSV(x*(6/(jscolor.images.pad[0]-1)), null, 1 - y/(jscolor.images.pad[1]-1), leaveSld); break;
763
+ }
764
+ }
765
+
766
+
767
+ function setSld(e) {
768
+ var posM = jscolor.getMousePos(e);
769
+ var y = posM[1]-posPad[1];
770
+ switch(modeID) {
771
+ case 0: THIS.fromHSV(null, null, 1 - y/(jscolor.images.sld[1]-1), leavePad); break;
772
+ case 1: THIS.fromHSV(null, 1 - y/(jscolor.images.sld[1]-1), null, leavePad); break;
773
+ }
774
+ }
775
+
776
+
777
+ var THIS = this;
778
+ var modeID = this.pickerMode.toLowerCase()==='hvs' ? 1 : 0;
779
+ var abortBlur = false;
780
+ var
781
+ valueElement = jscolor.fetchElement(this.valueElement),
782
+ styleElement = jscolor.fetchElement(this.styleElement);
783
+ var
784
+ holdPad = false,
785
+ holdSld = false;
786
+ var
787
+ posPad,
788
+ posSld;
789
+ var
790
+ leaveValue = 1<<0,
791
+ leaveStyle = 1<<1,
792
+ leavePad = 1<<2,
793
+ leaveSld = 1<<3;
794
+
795
+ // target
796
+ jscolor.addEvent(target, 'focus', function() {
797
+ if(THIS.pickerOnfocus) { THIS.showPicker(); }
798
+ });
799
+ jscolor.addEvent(target, 'blur', function() {
800
+ if(!abortBlur) {
801
+ window.setTimeout(function(){ abortBlur || blurTarget(); abortBlur=false; }, 0);
802
+ } else {
803
+ abortBlur = false;
804
+ }
805
+ });
806
+
807
+ // valueElement
808
+ if(valueElement) {
809
+ var updateField = function() {
810
+ THIS.fromString(valueElement.value, leaveValue);
811
+ };
812
+ jscolor.addEvent(valueElement, 'keyup', updateField);
813
+ jscolor.addEvent(valueElement, 'input', updateField);
814
+ jscolor.addEvent(valueElement, 'blur', blurValue);
815
+ valueElement.setAttribute('autocomplete', 'off');
816
+ }
817
+
818
+ // styleElement
819
+ if(styleElement) {
820
+ styleElement.jscStyle = {
821
+ backgroundColor : styleElement.style.backgroundColor,
822
+ color : styleElement.style.color
823
+ };
824
+ }
825
+
826
+ // require images
827
+ switch(modeID) {
828
+ case 0: jscolor.requireImage('hs.png'); break;
829
+ case 1: jscolor.requireImage('hv.png'); break;
830
+ }
831
+ jscolor.requireImage('cross.gif');
832
+ jscolor.requireImage('arrow.gif');
833
+
834
+ this.importColor();
835
+ }
836
+
837
+ };
838
+
839
+
840
+ jscolor.install();