| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
// Flash notification element |
| 5 |
var flash = null; |
| 6 |
function showFlash(hex) { |
| 7 |
if (!flash) { |
| 8 |
flash = document.createElement('div'); |
| 9 |
flash.className = 'bkbg-ct-copied-flash'; |
| 10 |
document.body.appendChild(flash); |
| 11 |
} |
| 12 |
flash.textContent = 'Copied ' + hex; |
| 13 |
flash.classList.add('show'); |
| 14 |
clearTimeout(flash._timer); |
| 15 |
flash._timer = setTimeout(function () { flash.classList.remove('show'); }, 1600); |
| 16 |
} |
| 17 |
|
| 18 |
function init() { |
| 19 |
document.querySelectorAll('.bkbg-ct-wrap').forEach(function (wrap) { |
| 20 |
if (wrap.dataset.ctReady) return; |
| 21 |
wrap.dataset.ctReady = '1'; |
| 22 |
|
| 23 |
wrap.querySelectorAll('.bkbg-ct-color-block').forEach(function (block) { |
| 24 |
var hex = block.dataset.hex; |
| 25 |
if (!hex) return; |
| 26 |
block.addEventListener('click', function () { |
| 27 |
if (navigator.clipboard) { |
| 28 |
navigator.clipboard.writeText(hex).then(function () { showFlash(hex); }); |
| 29 |
} else { |
| 30 |
var ta = document.createElement('textarea'); |
| 31 |
ta.value = hex; |
| 32 |
ta.style.position = 'fixed'; |
| 33 |
ta.style.opacity = '0'; |
| 34 |
document.body.appendChild(ta); |
| 35 |
ta.select(); |
| 36 |
document.execCommand('copy'); |
| 37 |
document.body.removeChild(ta); |
| 38 |
showFlash(hex); |
| 39 |
} |
| 40 |
}); |
| 41 |
}); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
if (document.readyState !== 'loading') { init(); } else { document.addEventListener('DOMContentLoaded', init); } |
| 46 |
})(); |
| 47 |
|