$(document).ready(function() { // ------------------------------ // Settings // ------------------------------ var apiUrl = 'https://api.coinmarketcap.com/v1/ticker/?limit=0'; var dataJson = 'manifest.json'; var formats = ['svg', '128', '32', '32@2x']; var variants = ['color', 'black', 'icon', 'white']; var iconDefault = 'black'; var iconHover = 'color'; // ------------------------------ // Init search // ------------------------------ $('form .search').on('input', function() { search($(this)); }); $('form').submit(function( event ) { event.preventDefault(); search($('form .search')); }); // ------------------------------ // Get icons // ------------------------------ // Get icons from CoinMarketCap $.getJSON(apiUrl, function(coins){ // Get icons in manifest $.getJSON(dataJson, function(data) { var icons = ''; var count = 0; data.icons.forEach(function(icon) { // Get symbol var coin = coins.find(function(coin){ return coin.symbol.toLowerCase() === icon; }) // Get name var name = coin ? coin.name : icon; var nameAttr = name.split(' ').join('-').toLowerCase(); // Avoid duplicates if (icons.indexOf('data-icon="' + icon + '"') === -1) { // Construct icon icons += '
'; icons += ''; icons += ''; count++; } }); // Display $('.row.icons').html(icons); // Hover $('.icon').hover(function() { changeFolder($(this), iconDefault, iconHover); }); // Mouseleave $('.icon').mouseleave(function() { changeFolder($(this), iconHover, iconDefault ); }); // Count icons $('.count-cryptos').text(count); }); }); // ------------------------------ // Functions // ------------------------------ // Search and Replace in Image src // ------------------------------ function changeFolder(target, search, replace) { var target = $(target).find('img'); var srcInit = $(target).attr('src'); var srcAfter = srcInit.replace(search, replace); $(target).attr('src', srcAfter); } // Display icon info in Modal // ------------------------------ $('#infoIcon').on('show.bs.modal', function (event) { // Modal settings var button = $(event.relatedTarget); var modal = $(this); var icon = button.data('icon'); // Table settings var infos = ''; var i = 0; var j = 0; // Construct table infos += '' + icon + ' | ';
while (variants[j]) {
var formatCss = formats[i].replace('@', '-');
infos += '' + variants[j] + ' | '; j++; } j = 0; infos += '
---|---|
' + formats[i] + ' | '; // File extension if (formats[i] == 'svg') { var extension = '.svg'; } else if (formats[i] == '32@2x') { var extension = '@2x.png'; } else { var extension = '.png'; } // Construct icons cells while (variants[j]) { infos += ''; infos += ''; infos += ' | '; j++; } j = 0; i++; infos += '