From 288831fdde9bab4c039f88924885f7726d19e335 Mon Sep 17 00:00:00 2001 From: simeydotme Date: Sat, 14 Jul 2018 20:43:18 +0800 Subject: [PATCH] Fix issue with DOM Ready - add a dom-ready wrapper for jQuery - add a dom-ready listener for vanilla - bump package --- .eslintrc.yml | 2 ++ dist/donutty-jquery.js | 26 +++++++++++++++++--------- dist/donutty-jquery.min.js | 4 ++-- dist/donutty.js | 16 ++++++++++------ dist/donutty.min.js | 4 ++-- package.json | 2 +- src/jquery.js | 24 ++++++++++++++++-------- src/vanilla.js | 14 +++++++++----- 8 files changed, 59 insertions(+), 33 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 4178628..81c9048 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -25,3 +25,5 @@ rules: semi: - error - always + no-console: + - off diff --git a/dist/donutty-jquery.js b/dist/donutty-jquery.js index 7212d70..58e740b 100644 --- a/dist/donutty-jquery.js +++ b/dist/donutty-jquery.js @@ -1,7 +1,7 @@ /** * donutty // Create SVG donut charts with Javascript * @author simeydotme - * @version 1.1.0 + * @version 1.1.2 * @license ISC * @link http://simey.me * @preserve @@ -255,20 +255,28 @@ if ( typeof window.$ !== "undefined" ) { - $.fn.donutty = function( options ) { + $( function() { - return $( this ).each( function( k, el ) { + $.fn.donutty = function( options ) { - var $el = $( el ), - instance = new Donutty( el, $.extend( {}, $el.data(), options ) ); + return $( this ).each( function( k, el ) { - $el.data( "donutty", instance ); + var $el = $( el ), + instance = new Donutty( el, $.extend( {}, $el.data(), options ) ); - }); + $el.data( "donutty", instance ); - }; + }); + + }; + + $( "[data-donutty]" ).donutty(); + + }); + + } else { - $( "[data-donutty]" ).donutty(); + console.warn( "Can't find jQuery to attach Donutty" ); } diff --git a/dist/donutty-jquery.min.js b/dist/donutty-jquery.min.js index 3f7448d..89194ce 100644 --- a/dist/donutty-jquery.min.js +++ b/dist/donutty-jquery.min.js @@ -1,9 +1,9 @@ /** * donutty // Create SVG donut charts with Javascript * @author simeydotme - * @version 1.1.0 + * @version 1.1.2 * @license ISC * @link http://simey.me * @preserve */ -!function(n,t){function o(t){return void 0!==t}function e(t){return parseFloat(t,10)}function r(t){return o(t)&&(!0===t||"true"===t)}var i=window.Donutty=function(t,i){var s=this;return t&&"string"==typeof t?s.$wrapper=n.querySelectorAll(t)[0]:t instanceof window.HTMLElement?s.$wrapper=t:(s.$wrapper=n.body,i=t),s.state={},s.options=i||{},s.options.min=o(s.options.min)?e(s.options.min):0,s.options.max=o(s.options.max)?e(s.options.max):100,s.options.value=o(s.options.value)?e(s.options.value):50,s.options.round=!o(s.options.round)||r(s.options.round),s.options.circle=!o(s.options.circle)||r(s.options.circle),s.options.padding=o(s.options.padding)?e(s.options.padding):4,s.options.radius=e(s.options.radius)||50,s.options.thickness=e(s.options.thickness)||10,s.options.bg=s.options.bg||"rgba(70, 130, 180, 0.15)",s.options.color=s.options.color||"mediumslateblue",s.options.transition=s.options.transition||"all 1.2s cubic-bezier(0.57, 0.13, 0.18, 0.98)",s.init(),s};i.prototype.init=function(){return this.state.min=this.options.min,this.state.max=this.options.max,this.state.value=this.options.value,this.state.bg=this.options.bg,this.state.color=this.options.color,this.createFragments(),this},i.prototype.createFragments=function(){var t="http://www.w3.org/2000/svg",i=2*this.options.radius+this.options.thickness+2*this.options.padding,s=this.getDashValues(),o=this.options.round?this.options.thickness/3:0,e=this.options.circle?90+o:-225;return this.$html=n.createDocumentFragment(),this.$svg=n.createElementNS(t,"svg"),this.$bg=n.createElementNS(t,"circle"),this.$donut=n.createElementNS(t,"circle"),this.$svg.setAttribute("xmlns",t),this.$svg.setAttribute("viewbox","0 0 "+i+" "+i),this.$svg.setAttribute("transform","rotate( "+e+" )"),this.$svg.style.width=i,this.$svg.style.height=i,this.$bg.setAttribute("cx","50%"),this.$bg.setAttribute("cy","50%"),this.$bg.setAttribute("r",this.options.radius),this.$bg.setAttribute("fill","transparent"),this.$bg.setAttribute("stroke",this.state.bg),this.$bg.setAttribute("stroke-width",this.options.thickness+this.options.padding),this.$bg.setAttribute("stroke-dasharray",s.full*s.multiplier),this.$donut.setAttribute("fill","transparent"),this.$donut.setAttribute("cx","50%"),this.$donut.setAttribute("cy","50%"),this.$donut.setAttribute("r",this.options.radius),this.$donut.setAttribute("stroke",this.state.color),this.$donut.setAttribute("stroke-width",this.options.thickness),this.$donut.setAttribute("stroke-dashoffset",s.full),this.$donut.setAttribute("stroke-dasharray",s.full),this.$donut.style.opacity=0,this.options.round&&(this.$bg.setAttribute("stroke-linecap","round"),this.$donut.setAttribute("stroke-linecap","round")),this.$svg.appendChild(this.$bg),this.$svg.appendChild(this.$donut),this.$html.appendChild(this.$svg),this.$wrapper.appendChild(this.$html),this.animate(s.fill,s.full),this},i.prototype.getDashValues=function(){var t,i,s,o;return o=this.options.circle?1:.75,s=(t=2*Math.PI*this.options.radius)-t*o/100*(i=(this.state.value-this.state.min)/(this.state.max-this.state.min)*100),this.options.round&&this.options.circle&&i<100&&s - * @version 1.1.0 + * @version 1.1.2 * @license ISC * @link http://simey.me * @preserve @@ -253,14 +253,18 @@ ( function( Donutty ) { - var $donuts = document.querySelectorAll( "[data-donutty]" ); + document.addEventListener("DOMContentLoaded", function() { - Array.prototype.forEach.call( $donuts , function( $el ) { + var $donuts = document.querySelectorAll( "[data-donutty]" ); - var options = JSON.parse( JSON.stringify( $el.dataset ) ), - instance = new Donutty( $el, options ); + Array.prototype.forEach.call( $donuts , function( $el ) { - $el.dataset.donutty = instance; + var options = JSON.parse( JSON.stringify( $el.dataset ) ), + instance = new Donutty( $el, options ); + + $el.dataset.donutty = instance; + + }); }); diff --git a/dist/donutty.min.js b/dist/donutty.min.js index a9774bf..bb17848 100644 --- a/dist/donutty.min.js +++ b/dist/donutty.min.js @@ -1,9 +1,9 @@ /** * donutty // Create SVG donut charts with Javascript * @author simeydotme - * @version 1.1.0 + * @version 1.1.2 * @license ISC * @link http://simey.me * @preserve */ -!function(n,t){function o(t){return void 0!==t}function e(t){return parseFloat(t,10)}function r(t){return o(t)&&(!0===t||"true"===t)}var s=window.Donutty=function(t,s){var i=this;return t&&"string"==typeof t?i.$wrapper=n.querySelectorAll(t)[0]:t instanceof window.HTMLElement?i.$wrapper=t:(i.$wrapper=n.body,s=t),i.state={},i.options=s||{},i.options.min=o(i.options.min)?e(i.options.min):0,i.options.max=o(i.options.max)?e(i.options.max):100,i.options.value=o(i.options.value)?e(i.options.value):50,i.options.round=!o(i.options.round)||r(i.options.round),i.options.circle=!o(i.options.circle)||r(i.options.circle),i.options.padding=o(i.options.padding)?e(i.options.padding):4,i.options.radius=e(i.options.radius)||50,i.options.thickness=e(i.options.thickness)||10,i.options.bg=i.options.bg||"rgba(70, 130, 180, 0.15)",i.options.color=i.options.color||"mediumslateblue",i.options.transition=i.options.transition||"all 1.2s cubic-bezier(0.57, 0.13, 0.18, 0.98)",i.init(),i};s.prototype.init=function(){return this.state.min=this.options.min,this.state.max=this.options.max,this.state.value=this.options.value,this.state.bg=this.options.bg,this.state.color=this.options.color,this.createFragments(),this},s.prototype.createFragments=function(){var t="http://www.w3.org/2000/svg",s=2*this.options.radius+this.options.thickness+2*this.options.padding,i=this.getDashValues(),o=this.options.round?this.options.thickness/3:0,e=this.options.circle?90+o:-225;return this.$html=n.createDocumentFragment(),this.$svg=n.createElementNS(t,"svg"),this.$bg=n.createElementNS(t,"circle"),this.$donut=n.createElementNS(t,"circle"),this.$svg.setAttribute("xmlns",t),this.$svg.setAttribute("viewbox","0 0 "+s+" "+s),this.$svg.setAttribute("transform","rotate( "+e+" )"),this.$svg.style.width=s,this.$svg.style.height=s,this.$bg.setAttribute("cx","50%"),this.$bg.setAttribute("cy","50%"),this.$bg.setAttribute("r",this.options.radius),this.$bg.setAttribute("fill","transparent"),this.$bg.setAttribute("stroke",this.state.bg),this.$bg.setAttribute("stroke-width",this.options.thickness+this.options.padding),this.$bg.setAttribute("stroke-dasharray",i.full*i.multiplier),this.$donut.setAttribute("fill","transparent"),this.$donut.setAttribute("cx","50%"),this.$donut.setAttribute("cy","50%"),this.$donut.setAttribute("r",this.options.radius),this.$donut.setAttribute("stroke",this.state.color),this.$donut.setAttribute("stroke-width",this.options.thickness),this.$donut.setAttribute("stroke-dashoffset",i.full),this.$donut.setAttribute("stroke-dasharray",i.full),this.$donut.style.opacity=0,this.options.round&&(this.$bg.setAttribute("stroke-linecap","round"),this.$donut.setAttribute("stroke-linecap","round")),this.$svg.appendChild(this.$bg),this.$svg.appendChild(this.$donut),this.$html.appendChild(this.$svg),this.$wrapper.appendChild(this.$html),this.animate(i.fill,i.full),this},s.prototype.getDashValues=function(){var t,s,i,o;return o=this.options.circle?1:.75,i=(t=2*Math.PI*this.options.radius)-t*o/100*(s=(this.state.value-this.state.min)/(this.state.max-this.state.min)*100),this.options.round&&this.options.circle&&s<100&&i