dashboard-widget.js
126 lines
| 1 | (function() { |
| 2 | function generateDashboardWidget(element) { |
| 3 | element = jQuery(element); |
| 4 | var container = element.find('.inside'); |
| 5 | jQuery('.chart').addClass('hidden'); |
| 6 | // Adding a class to the widget element so that classes are only used in the stylesheet |
| 7 | jQuery('#tinypng_dashboard_widget').addClass('tiny_dashboard_widget'); |
| 8 | attachHandlers(container); |
| 9 | retrieveStats(container); |
| 10 | } |
| 11 | |
| 12 | function retrieveStats(container) { |
| 13 | jQuery.ajax({ |
| 14 | url: ajaxurl, |
| 15 | type: 'POST', |
| 16 | data: { |
| 17 | _nonce: tinyCompressDashboard.nonce, |
| 18 | action: 'tiny_get_optimization_statistics', |
| 19 | id: '#tinypng_dashboard_widget' |
| 20 | }, |
| 21 | success: function(data) { |
| 22 | if (data === 0) { |
| 23 | container.append('<p> An error occured. </p>'); |
| 24 | } else { |
| 25 | renderWidget(data); |
| 26 | } |
| 27 | }, |
| 28 | error: function() { |
| 29 | container.append('<p> An error occured. </p>'); |
| 30 | } |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | function attachHandlers(container) { |
| 35 | setContainerClass(container); |
| 36 | jQuery(window).resize(function(){ |
| 37 | setContainerClass(container); |
| 38 | }); |
| 39 | jQuery('#tinypng_dashboard_widget .hndle').click(function() { |
| 40 | jQuery(this).siblings('.inside').removeClass('mobile'); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | function setContainerClass(container) { |
| 45 | if (jQuery(container).width() < 400) { |
| 46 | jQuery(container).addClass('mobile'); |
| 47 | } else if (jQuery(container).width() < 490 && jQuery(container).width() >= 400) { |
| 48 | jQuery(container).addClass('tablet'); |
| 49 | jQuery(container).removeClass('mobile'); |
| 50 | } else if (jQuery(container).width() >= 490) { |
| 51 | jQuery(container).removeClass('tablet').removeClass('mobile'); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | function renderWidget(data) { |
| 56 | var stats = jQuery.parseJSON(data); |
| 57 | if (stats !== null) { |
| 58 | var savings = stats['display-percentage']; |
| 59 | var libraryOptimized = optimizedPercentage(stats); |
| 60 | renderContent(libraryOptimized, stats, savings); |
| 61 | renderChart(savings); |
| 62 | jQuery('#optimization-chart').show(); |
| 63 | } |
| 64 | jQuery('#widget-spinner').attr('class', 'hidden'); |
| 65 | } |
| 66 | |
| 67 | function renderPercentage(percentage) { |
| 68 | jQuery('#savings-percentage').find('span').html(percentage); |
| 69 | } |
| 70 | |
| 71 | function renderContent(percentage, stats, savingsPercentage) { |
| 72 | renderPercentage(savingsPercentage); |
| 73 | if ( 0 === stats['uploaded-images'] + stats['available-unoptimized-sizes'] ) { |
| 74 | jQuery('#tinypng_dashboard_widget').addClass('no-images-uploaded'); |
| 75 | } else if ( percentage === 0 ) { |
| 76 | jQuery('#tinypng_dashboard_widget').addClass('not-optimized'); |
| 77 | } else if ( percentage === 100 ) { |
| 78 | jQuery('#tinypng_dashboard_widget').addClass('full-optimized'); |
| 79 | } else { |
| 80 | jQuery('#uploaded-images').html( stats['uploaded-images'] ); |
| 81 | jQuery('#unoptimized-sizes').html( stats['available-unoptimized-sizes'] ); |
| 82 | jQuery('#tinypng_dashboard_widget').addClass('half-optimized'); |
| 83 | } |
| 84 | jQuery('#ie8-compressed').find('span').html(savingsPercentage); |
| 85 | } |
| 86 | |
| 87 | function chartOptions(percentage) { |
| 88 | var chart = {}; |
| 89 | chart.size = 160; |
| 90 | chart.radius = chart.size / 2 * 0.9; |
| 91 | chart['main-radius'] = chart.radius * 0.88; |
| 92 | chart['circle-size'] = 2 * Math.PI * chart['main-radius']; |
| 93 | chart['dash-array-size'] = percentage / 100 * chart['circle-size']; |
| 94 | return chart; |
| 95 | } |
| 96 | |
| 97 | function optimizedPercentage(stats) { |
| 98 | if ( 0 !== stats['unoptimized-library-size'] ) { |
| 99 | return Math.round((stats['optimized-image-sizes'] / (stats['optimized-image-sizes'] + stats['available-unoptimized-sizes']) * 100), 0); |
| 100 | } else { |
| 101 | return 0; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function renderChart(savingsPercentage) { |
| 106 | var chart = chartOptions(savingsPercentage); |
| 107 | jQuery('#optimization-chart svg circle.main').css('stroke-dasharray', chart['dash-array-size'] + ' ' + chart['circle-size']); |
| 108 | var style = |
| 109 | ' @keyframes shwoosh {' + |
| 110 | ' from { stroke-dasharray: 0 ' + chart['circle-size'] + '}' + |
| 111 | ' to { stroke-dasharray:' + chart['dash-array-size'] + ' ' + chart['circle-size'] + '}}'; |
| 112 | |
| 113 | // JQuery bug where you cannot append to style tag https://bugs.jquery.com/ticket/9832 |
| 114 | try { |
| 115 | jQuery('#tinypng_dashboard_widget .inside style').append(style); |
| 116 | } catch(err) { |
| 117 | |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Check if widget is loaded |
| 122 | if (jQuery('#tinypng_dashboard_widget').length) { |
| 123 | generateDashboardWidget('#tinypng_dashboard_widget'); |
| 124 | } |
| 125 | }).call(); |
| 126 |