| 1 |
/* |
| 2 |
Field Color (color) |
| 3 |
*/ |
| 4 |
|
| 5 |
/*global jQuery, document, redux_change, redux*/ |
| 6 |
|
| 7 |
(function ($) { |
| 8 |
'use strict'; |
| 9 |
|
| 10 |
redux.field_objects = redux.field_objects || {}; |
| 11 |
redux.field_objects.rgba = redux.field_objects.rgba || {}; |
| 12 |
|
| 13 |
$(document).ready( |
| 14 |
function () { |
| 15 |
|
| 16 |
} |
| 17 |
); |
| 18 |
|
| 19 |
redux.field_objects.rgba.init = function (selector) { |
| 20 |
|
| 21 |
if (!selector) { |
| 22 |
selector = $(document).find(".redux-group-tab:visible").find('.redux-container-rgba:visible'); |
| 23 |
} |
| 24 |
|
| 25 |
$(selector).each( |
| 26 |
function () { |
| 27 |
|
| 28 |
var el = $(this); |
| 29 |
var parent = el; |
| 30 |
|
| 31 |
if (!el.hasClass('redux-field-container')) { |
| 32 |
parent = el.parents('.redux-field-container:first'); |
| 33 |
} |
| 34 |
if (parent.is(":hidden")) { // Skip hidden fields |
| 35 |
return; |
| 36 |
} |
| 37 |
if (parent.hasClass('redux-field-init')) { |
| 38 |
parent.removeClass('redux-field-init'); |
| 39 |
} else { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
el.find('.redux-rgba-init').wpColorPicker( |
| 44 |
{ |
| 45 |
change: function (e, ui) { |
| 46 |
$(this).val(ui.color.toString()); |
| 47 |
redux_change($(this)); |
| 48 |
el.find('#' + e.target.getAttribute('data-id') + '-transparency').removeAttr('checked'); |
| 49 |
}, |
| 50 |
clear: function (e, ui) { |
| 51 |
$(this).val(''); |
| 52 |
redux_change($(this).parent().find('.redux-rgba-init')); |
| 53 |
} |
| 54 |
} |
| 55 |
); |
| 56 |
|
| 57 |
el.find('.redux-color').on( |
| 58 |
'focus', function () { |
| 59 |
$(this).data('oldcolor', $(this).val()); |
| 60 |
} |
| 61 |
); |
| 62 |
|
| 63 |
el.find('.redux-color').on( |
| 64 |
'keyup', function () { |
| 65 |
var value = $(this).val(); |
| 66 |
var color = colorValidate(this); |
| 67 |
var id = '#' + $(this).attr('id'); |
| 68 |
|
| 69 |
if (value === "transparent") { |
| 70 |
$(this).parent().parent().find('.wp-color-result').css( |
| 71 |
'background-color', 'transparent' |
| 72 |
); |
| 73 |
|
| 74 |
el.find(id + '-transparency').attr('checked', 'checked'); |
| 75 |
} else { |
| 76 |
el.find(id + '-transparency').removeAttr('checked'); |
| 77 |
|
| 78 |
if (color && color !== $(this).val()) { |
| 79 |
$(this).val(color); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
// Replace and validate field on blur |
| 86 |
el.find('.redux-rgba').on( |
| 87 |
'blur', function () { |
| 88 |
var value = $(this).val(); |
| 89 |
var id = '#' + $(this).attr('id'); |
| 90 |
|
| 91 |
if (value === "transparent") { |
| 92 |
$(this).parent().parent().find('.wp-color-result').css( |
| 93 |
'background-color', 'transparent' |
| 94 |
); |
| 95 |
|
| 96 |
el.find(id + '-transparency').attr('checked', 'checked'); |
| 97 |
} else { |
| 98 |
if (colorValidate(this) === value) { |
| 99 |
if (value.indexOf("#") !== 0) { |
| 100 |
$(this).val($(this).data('oldcolor')); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
el.find(id + '-transparency').removeAttr('checked'); |
| 105 |
} |
| 106 |
} |
| 107 |
); |
| 108 |
|
| 109 |
// Store the old valid color on keydown |
| 110 |
el.find('.redux-color').on( |
| 111 |
'keydown', function () { |
| 112 |
$(this).data('oldkeypress', $(this).val()); |
| 113 |
} |
| 114 |
); |
| 115 |
|
| 116 |
// When transparency checkbox is clicked |
| 117 |
el.find('.color-transparency').on( |
| 118 |
'click', function () { |
| 119 |
if ($(this).is(":checked")) { |
| 120 |
el.find('.redux-saved-color').val($('#' + $(this).data('id')).val()); |
| 121 |
el.find('#' + $(this).data('id')).val('transparent'); |
| 122 |
el.find('#' + $(this).data('id')).parent().parent().find('.wp-color-result').css( |
| 123 |
'background-color', 'transparent' |
| 124 |
); |
| 125 |
} else { |
| 126 |
if (el.find('#' + $(this).data('id')).val() === 'transparent') { |
| 127 |
var prevColor = $('.redux-saved-color').val(); |
| 128 |
|
| 129 |
if (prevColor === '') { |
| 130 |
prevColor = $('#' + $(this).data('id')).data('default-color'); |
| 131 |
} |
| 132 |
|
| 133 |
el.find('#' + $(this).data('id')).parent().parent().find('.wp-color-result').css( |
| 134 |
'background-color', prevColor |
| 135 |
); |
| 136 |
|
| 137 |
el.find('#' + $(this).data('id')).val(prevColor); |
| 138 |
} |
| 139 |
} |
| 140 |
redux_change($(this)); |
| 141 |
} |
| 142 |
); |
| 143 |
} |
| 144 |
); |
| 145 |
}; |
| 146 |
})(jQuery); |