1 | /*global $, jQuery */ |
---|
2 | 'use strict'; |
---|
3 | |
---|
4 | $(function() { |
---|
5 | $('.colorpicker').colorPicker(); |
---|
6 | }); |
---|
7 | |
---|
8 | jQuery.fn.colorPicker = function() { |
---|
9 | $(this).each(function() { |
---|
10 | let colbox = $('#jquery-colorpicker')[0]; |
---|
11 | if (colbox == undefined) { |
---|
12 | colbox = document.createElement('div'); |
---|
13 | colbox.id = 'jquery-colorpicker'; |
---|
14 | colbox.linkedto = null; |
---|
15 | |
---|
16 | $(colbox).addClass('color-color-picker'); |
---|
17 | $(colbox).css({ |
---|
18 | display: 'none', |
---|
19 | position: 'absolute' |
---|
20 | }); |
---|
21 | $('body').append(colbox); |
---|
22 | } |
---|
23 | const f = $.farbtastic(colbox); |
---|
24 | f.linkTo(this); |
---|
25 | |
---|
26 | const handler = $(document.createElement('img')); |
---|
27 | handler.attr('src', 'images/picker.png'); |
---|
28 | handler.attr('alt', ''); |
---|
29 | |
---|
30 | const span = $(document.createElement('span')); |
---|
31 | |
---|
32 | if ($(this).css('position') == 'absolute') { |
---|
33 | span.css('position', 'absolute'); |
---|
34 | span.css('top', $(this).css('top')); |
---|
35 | span.css('left', $(this).css('left')); |
---|
36 | $(this).css('position', 'static'); |
---|
37 | } else { |
---|
38 | span.css('position', 'relative'); |
---|
39 | } |
---|
40 | span.css('display', 'inline-block'); |
---|
41 | span.css('padding', '0 5px 0 0'); |
---|
42 | $(this).wrap(span); |
---|
43 | $(this).after(handler); |
---|
44 | |
---|
45 | handler.css({ |
---|
46 | position: 'absolute', |
---|
47 | top: 3, |
---|
48 | }); |
---|
49 | |
---|
50 | handler.css({ |
---|
51 | cursor: 'default' |
---|
52 | }); |
---|
53 | |
---|
54 | const This = this; |
---|
55 | handler.click(function() { |
---|
56 | if ($(colbox).css('display') == 'none' || this != colbox.linkedto) { |
---|
57 | f.linkTo(This); |
---|
58 | This.focus(); |
---|
59 | const offset = $(This).offset(); |
---|
60 | $(colbox).css({ |
---|
61 | zIndex: 1000, |
---|
62 | top: offset.top + $(this).height() + 5, |
---|
63 | left: offset.left |
---|
64 | }); |
---|
65 | if (document.all) { |
---|
66 | $('select').hide(); |
---|
67 | } |
---|
68 | $(colbox).show(); |
---|
69 | colbox.linkedto = this; |
---|
70 | |
---|
71 | } else { |
---|
72 | $(colbox).hide(); |
---|
73 | if (document.all) { |
---|
74 | $('select').show(); |
---|
75 | } |
---|
76 | colbox.linkedto = null; |
---|
77 | } |
---|
78 | }); |
---|
79 | $(this).blur(function() { |
---|
80 | $(colbox).hide(); |
---|
81 | if (document.all) { |
---|
82 | $('select').show(); |
---|
83 | } |
---|
84 | }); |
---|
85 | }); |
---|
86 | return this; |
---|
87 | }; |
---|