peity_vanilla_rails 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de5631424fd3980ed3c9c8a7e02a4c7e10cc77650ebdea1f8d17cb99d526ca96
4
- data.tar.gz: 85a1ae8f2d6e05fad50d5362c0ce3c55d8e0b8d04354d028cf247562a76194a6
3
+ metadata.gz: 6ed4200aaf0c4efeb3e7a866b3664102c18d25e4efaf371b0e209906d8fd1342
4
+ data.tar.gz: 59081cd66aba5f066daa5dc3dee710ffd1b230484adda56f3f70fbf189652191
5
5
  SHA512:
6
- metadata.gz: a90b637c07453922d6ad43cff0def5c8daa35fcdd4c0e094acd053f6e85226fab91a5f606efc434fe9144ed676993a2ebe95582ad8420aca182cf6e987674054
7
- data.tar.gz: 0b2a6938fd2ab60492f943965a8a45e128f31098d262ec5315a9e3252daa257b40be98728dee23402192ac5bd485069d839cfa080102126af39d711aa870c4ec
6
+ metadata.gz: 378508634d48a3f6a0dc1ceadfe33b2357196aa63d538fa7d69ec3696e10abc0296df79534f7b2ecc406c5e2b541daa21db78225fbd416fe6974de5779aa5f5a
7
+ data.tar.gz: 11f38fa1d733923d800868923c3a569f1a22d5aac34164ea317602931023aa95fd6808c81f8afc9c724a6b2f94c15d92a1b57be66279273148b784e04f1e1a0f
@@ -1,5 +1,5 @@
1
1
  /*!
2
- Peity Vanila Rails 0.2.1
2
+ Peity Vanila Rails 0.2.2
3
3
  Copyright © 2022 RailsJazz
4
4
  https://railsjazz.com
5
5
  */
@@ -434,6 +434,8 @@ function domReady() {
434
434
  });
435
435
  }
436
436
 
437
+ const queryPeityElements = (node) => node.matches("[peity]") ? [node] : Array.from(node.querySelectorAll("[peity]"));
438
+
437
439
  const getPeityType = (node) => node.getAttribute("peity");
438
440
 
439
441
  const elementFromNode = (node) => {
@@ -442,78 +444,106 @@ const elementFromNode = (node) => {
442
444
  }
443
445
  };
444
446
 
445
- const processNodeAdded = (node) => {
446
- if (node.matches("[peity]")) {
447
- peity(node, getPeityType(node));
447
+ class Watcher {
448
+ constructor() {
449
+ this.elements = new Set();
448
450
  }
449
- };
450
451
 
451
- const processNodeRemoved = (node) => {
452
- if (node._peity) {
453
- node._peity.destroy();
454
- }
455
- };
452
+ async start() {
453
+ await domReady();
456
454
 
457
- const processAttributeChanged = (mutation) => {
458
- const node = mutation.target;
459
- switch (mutation.attributeName) {
460
- case "peity":
461
- const type = getPeityType(node);
462
- if (type) {
463
- peity(node, type);
464
- } else {
465
- node._peity.unmount();
455
+ this.element = document.documentElement;
456
+
457
+ const observer = new MutationObserver(this.callback.bind(this));
458
+ observer.observe(this.element, {
459
+ childList: true,
460
+ attributes: true,
461
+ subtree: true,
462
+ });
463
+
464
+ this.processNodeAdded(this.element);
465
+ };
466
+
467
+ processNodeAdded(node) {
468
+ for (const element of queryPeityElements(node)) {
469
+ if (!this.elements.has(element)) {
470
+ this.elements.add(element);
471
+ peity(element, getPeityType(element));
466
472
  }
467
- break;
468
- case "data-peity":
469
- if (node._peity) {
470
- peity(node, node._peity.type);
473
+ }
474
+ };
475
+
476
+ processNodeRemoved(node) {
477
+ const matches = new Set(queryPeityElements(node));
478
+
479
+ for (const element of this.elements) {
480
+ if(matches.has(element)) {
481
+ this.elements.delete(element);
482
+ if (element._peity) {
483
+ element._peity.destroy();
484
+ }
471
485
  }
472
- break;
473
- }
474
- };
486
+ }
487
+ };
488
+
489
+ processAttributeChanged(mutation) {
490
+ const node = mutation.target;
491
+ if(!this.elementIsActive(node)) return;
475
492
 
476
- const callback = (mutationList, observer) => {
477
- mutationList.forEach(function (mutation) {
478
- switch (mutation.type) {
479
- case "childList":
480
- for (const node of Array.from(mutation.addedNodes)) {
481
- const element = elementFromNode(node);
482
- if (element) {
483
- processNodeAdded(element);
493
+ switch (mutation.attributeName) {
494
+ case "peity":
495
+ const type = getPeityType(node);
496
+ if (type) {
497
+ if (!this.elements.has(node)) {
498
+ this.elements.add(node);
484
499
  }
485
- }
486
- for (const node of Array.from(mutation.removedNodes)) {
487
- const element = elementFromNode(node);
488
- if (element) {
489
- processNodeRemoved(element);
500
+ peity(node, type);
501
+ } else {
502
+ if (this.elements.has(node)) {
503
+ this.elements.delete(node);
504
+ if (node._peity) {
505
+ node._peity.destroy();
506
+ }
490
507
  }
491
508
  }
492
509
  break;
493
- case "attributes":
494
- processAttributeChanged(mutation);
510
+ case "data-peity":
511
+ if (node._peity) {
512
+ peity(node, node._peity.type);
513
+ }
495
514
  break;
496
515
  }
497
- });
498
- };
499
-
500
- const observerOptions = {
501
- childList: true,
502
- attributes: true,
503
- subtree: true,
504
- };
505
-
506
- const start = async () => {
507
- await domReady();
516
+ }
508
517
 
509
- const observer = new MutationObserver(callback);
510
- observer.observe(document.documentElement, observerOptions);
518
+ callback(mutationList, observer) {
519
+ mutationList.forEach((mutation) => {
520
+ switch (mutation.type) {
521
+ case "childList":
522
+ for (const node of Array.from(mutation.removedNodes)) {
523
+ const element = elementFromNode(node);
524
+ if (element) {
525
+ this.processNodeRemoved(element);
526
+ }
527
+ }
528
+ for (const node of Array.from(mutation.addedNodes)) {
529
+ const element = elementFromNode(node);
530
+ if (element && this.elementIsActive(element)) {
531
+ this.processNodeAdded(element);
532
+ }
533
+ }
534
+ break;
535
+ case "attributes":
536
+ this.processAttributeChanged(mutation);
537
+ break;
538
+ }
539
+ });
540
+ };
511
541
 
512
- for (const node of Array.from(document.documentElement.querySelectorAll("[peity]"))) {
513
- processNodeAdded(node);
542
+ elementIsActive(element) {
543
+ return this.element.contains(element);
514
544
  }
515
- };
545
+ }
516
546
 
517
- start();
547
+ new Watcher().start();
518
548
 
519
549
  export { peity as default };
@@ -1,5 +1,5 @@
1
1
  /*!
2
- Peity Vanila Rails 0.2.1
2
+ Peity Vanila Rails 0.2.2
3
3
  Copyright © 2022 RailsJazz
4
4
  https://railsjazz.com
5
5
  */
@@ -8,4 +8,4 @@ var peity=function(){"use strict";
8
8
  Peity Vanila JS 0.0.8
9
9
  Copyright © 2022 RailsJazz
10
10
  https://railsjazz.com
11
- */const t=t=>null!==t&&"function"==typeof t&&!!t.apply,e=(t,e)=>{const i=document.createElementNS("http://www.w3.org/2000/svg",t);for(var n in e)i.setAttribute(n,e[n]);return i},i="createElementNS"in document&&e("svg",{}).createSVGRect();class n{static defaults={};static graphers={};constructor(t,e,i={}){this.element=t,this.type=e,this.options=Object.assign({},n.defaults[this.type],JSON.parse(t.dataset.peity||"{}"),i),this.element._peity&&this.element._peity.destroy(),this.element._peity=this}draw(){const e=this.options;n.graphers[this.type](this),t(e.after)&&e.after.call(this,e)}fill(){var e=this.options.fill;return t(e)?e:function(t,i){return e[i%e.length]}}prepare(t,i){return this.svg||(this.element.style.display="none",this.element.after(this.svg=e("svg",{class:"peity"}))),this.svg.innerHTML="",this.svg.setAttribute("width",t),this.svg.setAttribute("height",i),this.svg}get values(){return this.element.innerText.split(this.options.delimiter).map((t=>parseFloat(t)))}mount(){i&&(this.element.addEventListener("DOMSubtreeModified",this.draw.bind(this)),this.draw(),this.mounted=!0)}unmount(){this.element.removeEventListener("DOMSubtreeModified",this.draw),this.svg.remove(),this.mounted=!1}destroy(){this.unmount(),delete this.element._peity}static register(t,e,i){n.defaults[t]=e,n.graphers[t]=i}}const s=t=>{if(!t.options.delimiter){const e=t.element.innerText.match(/[^0-9\.]/);t.options.delimiter=e?e[0]:","}let i=t.values.map((t=>t>0?t:0));if("/"==t.options.delimiter){let t=i[0],e=i[1];i=[t,Math.max(0,e-t)]}let n=0,s=i.length,o=0;for(;n<s;n++)o+=i[n];o||(s=2,o=1,i=[0,1]);let a=2*t.options.radius;const r=t.prepare(t.options.width||a,t.options.height||a),l=r.clientWidth,h=r.clientHeight,p=l/2,c=h/2,d=Math.min(p,c);let u=t.options.innerRadius;"donut"!=t.type||u||(u=.5*d);const m=t.fill(),f=(t,e)=>{const i=t/o*Math.PI*2-Math.PI/2;return[e*Math.cos(i)+p,e*Math.sin(i)+c]};let g=0;for(n=0;n<s;n++){const s=i[n],a=s/o;let l;if(0!=a){if(1==a)if(u){const t=p-.01,i=c-d,n=c-u;l=e("path",{d:["M",p,i,"A",d,d,0,1,1,t,i,"L",t,n,"A",u,u,0,1,0,p,n].join(" "),"data-value":s})}else l=e("circle",{cx:p,cy:c,"data-value":s,r:d});else{const t=g+s;let i=["M"].concat(f(g,d),"A",d,d,0,a>.5?1:0,1,f(t,d),"L");u?i=i.concat(f(t,u),"A",u,u,0,a>.5?1:0,0,f(g,u)):i.push(p,c),g+=s,l=e("path",{d:i.join(" "),"data-value":s})}l.setAttribute("fill",m.call(t,s,n,i)),r.append(l)}}},o={fill:["#ff9900","#fff4dd","#ffc66e"],radius:8};n.register("pie",o,s),n.register("donut",o,s),n.register("bar",{delimiter:",",fill:["#4D89F9"],height:16,min:0,padding:.1,width:32},(t=>{const i=t.values,n=Math.max.apply(Math,null==t.options.max?i:i.concat(t.options.max)),s=Math.min.apply(Math,null==t.options.min?i:i.concat(t.options.min)),o=t.prepare(t.options.width,t.options.height),a=o.clientWidth,r=o.clientHeight,l=n-s,h=t.options.padding,p=t.fill(),c=t=>t*a/i.length,d=t=>r-(l?(t-s)/l*r:1);for(var u=0;u<i.length;u++){let a,r=c(u+h),m=c(u+1-h)-r,f=i[u],g=d(f),y=g,v=g;l?f<0?y=d(Math.min(n,0)):v=d(Math.max(s,0)):a=1,a=v-y,0==a&&(a=1,n>0&&l&&y--),o.append(e("rect",{"data-value":f,fill:p.call(t,f,u,i),x:r,y:y,width:m,height:a}))}})),n.register("line",{delimiter:",",fill:"#c6d9fd",height:16,min:0,stroke:"#4d89f9",strokeWidth:1,width:32},(t=>{const i=t.values;1==i.length&&i.push(i[0]);const n=Math.max.apply(Math,null==t.options.max?i:i.concat(t.options.max)),s=Math.min.apply(Math,null==t.options.min?i:i.concat(t.options.min)),o=t.prepare(t.options.width,t.options.height),a=t.options.strokeWidth,r=o.clientWidth,l=o.clientHeight-a,h=n-s,p=t=>{let e=l;return h&&(e-=(t-s)/h*l),e+a/2};let c=p(Math.max(s,0)),d=[0,c];for(var u=0;u<i.length;u++)d.push(u*(r/(i.length-1)),p(i[u]));d.push(r,c),t.options.fill&&o.append(e("polygon",{fill:t.options.fill,points:d.join(" ")})),a&&o.append(e("polyline",{fill:"none",points:d.slice(2,d.length-2).join(" "),stroke:t.options.stroke,"stroke-width":a,"stroke-linecap":"square"}))}));const a=function(t,e,i){const s=new n(t,e,i);return s.mount(),s};a.defaults=n.defaults,a.graphers=n.graphers;const r=t=>t.getAttribute("peity"),l=t=>{if(t.nodeType==Node.ELEMENT_NODE)return t},h=t=>{t.matches("[peity]")&&a(t,r(t))},p=t=>{t._peity&&t._peity.destroy()},c=(t,e)=>{t.forEach((function(t){switch(t.type){case"childList":for(const e of Array.from(t.addedNodes)){const t=l(e);t&&h(t)}for(const e of Array.from(t.removedNodes)){const t=l(e);t&&p(t)}break;case"attributes":(t=>{const e=t.target;switch(t.attributeName){case"peity":const t=r(e);t?a(e,t):e._peity.unmount();break;case"data-peity":e._peity&&a(e,e._peity.type)}})(t)}}))},d={childList:!0,attributes:!0,subtree:!0};return(async()=>{await new Promise((t=>{"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()}));new MutationObserver(c).observe(document.documentElement,d);for(const t of Array.from(document.documentElement.querySelectorAll("[peity]")))h(t)})(),a}();
11
+ */const t=t=>null!==t&&"function"==typeof t&&!!t.apply,e=(t,e)=>{const i=document.createElementNS("http://www.w3.org/2000/svg",t);for(var s in e)i.setAttribute(s,e[s]);return i},i="createElementNS"in document&&e("svg",{}).createSVGRect();class s{static defaults={};static graphers={};constructor(t,e,i={}){this.element=t,this.type=e,this.options=Object.assign({},s.defaults[this.type],JSON.parse(t.dataset.peity||"{}"),i),this.element._peity&&this.element._peity.destroy(),this.element._peity=this}draw(){const e=this.options;s.graphers[this.type](this),t(e.after)&&e.after.call(this,e)}fill(){var e=this.options.fill;return t(e)?e:function(t,i){return e[i%e.length]}}prepare(t,i){return this.svg||(this.element.style.display="none",this.element.after(this.svg=e("svg",{class:"peity"}))),this.svg.innerHTML="",this.svg.setAttribute("width",t),this.svg.setAttribute("height",i),this.svg}get values(){return this.element.innerText.split(this.options.delimiter).map((t=>parseFloat(t)))}mount(){i&&(this.element.addEventListener("DOMSubtreeModified",this.draw.bind(this)),this.draw(),this.mounted=!0)}unmount(){this.element.removeEventListener("DOMSubtreeModified",this.draw),this.svg.remove(),this.mounted=!1}destroy(){this.unmount(),delete this.element._peity}static register(t,e,i){s.defaults[t]=e,s.graphers[t]=i}}const n=t=>{if(!t.options.delimiter){const e=t.element.innerText.match(/[^0-9\.]/);t.options.delimiter=e?e[0]:","}let i=t.values.map((t=>t>0?t:0));if("/"==t.options.delimiter){let t=i[0],e=i[1];i=[t,Math.max(0,e-t)]}let s=0,n=i.length,o=0;for(;s<n;s++)o+=i[s];o||(n=2,o=1,i=[0,1]);let a=2*t.options.radius;const r=t.prepare(t.options.width||a,t.options.height||a),l=r.clientWidth,h=r.clientHeight,d=l/2,p=h/2,c=Math.min(d,p);let m=t.options.innerRadius;"donut"!=t.type||m||(m=.5*c);const u=t.fill(),f=(t,e)=>{const i=t/o*Math.PI*2-Math.PI/2;return[e*Math.cos(i)+d,e*Math.sin(i)+p]};let g=0;for(s=0;s<n;s++){const n=i[s],a=n/o;let l;if(0!=a){if(1==a)if(m){const t=d-.01,i=p-c,s=p-m;l=e("path",{d:["M",d,i,"A",c,c,0,1,1,t,i,"L",t,s,"A",m,m,0,1,0,d,s].join(" "),"data-value":n})}else l=e("circle",{cx:d,cy:p,"data-value":n,r:c});else{const t=g+n;let i=["M"].concat(f(g,c),"A",c,c,0,a>.5?1:0,1,f(t,c),"L");m?i=i.concat(f(t,m),"A",m,m,0,a>.5?1:0,0,f(g,m)):i.push(d,p),g+=n,l=e("path",{d:i.join(" "),"data-value":n})}l.setAttribute("fill",u.call(t,n,s,i)),r.append(l)}}},o={fill:["#ff9900","#fff4dd","#ffc66e"],radius:8};s.register("pie",o,n),s.register("donut",o,n),s.register("bar",{delimiter:",",fill:["#4D89F9"],height:16,min:0,padding:.1,width:32},(t=>{const i=t.values,s=Math.max.apply(Math,null==t.options.max?i:i.concat(t.options.max)),n=Math.min.apply(Math,null==t.options.min?i:i.concat(t.options.min)),o=t.prepare(t.options.width,t.options.height),a=o.clientWidth,r=o.clientHeight,l=s-n,h=t.options.padding,d=t.fill(),p=t=>t*a/i.length,c=t=>r-(l?(t-n)/l*r:1);for(var m=0;m<i.length;m++){let a,r=p(m+h),u=p(m+1-h)-r,f=i[m],g=c(f),y=g,v=g;l?f<0?y=c(Math.min(s,0)):v=c(Math.max(n,0)):a=1,a=v-y,0==a&&(a=1,s>0&&l&&y--),o.append(e("rect",{"data-value":f,fill:d.call(t,f,m,i),x:r,y:y,width:u,height:a}))}})),s.register("line",{delimiter:",",fill:"#c6d9fd",height:16,min:0,stroke:"#4d89f9",strokeWidth:1,width:32},(t=>{const i=t.values;1==i.length&&i.push(i[0]);const s=Math.max.apply(Math,null==t.options.max?i:i.concat(t.options.max)),n=Math.min.apply(Math,null==t.options.min?i:i.concat(t.options.min)),o=t.prepare(t.options.width,t.options.height),a=t.options.strokeWidth,r=o.clientWidth,l=o.clientHeight-a,h=s-n,d=t=>{let e=l;return h&&(e-=(t-n)/h*l),e+a/2};let p=d(Math.max(n,0)),c=[0,p];for(var m=0;m<i.length;m++)c.push(m*(r/(i.length-1)),d(i[m]));c.push(r,p),t.options.fill&&o.append(e("polygon",{fill:t.options.fill,points:c.join(" ")})),a&&o.append(e("polyline",{fill:"none",points:c.slice(2,c.length-2).join(" "),stroke:t.options.stroke,"stroke-width":a,"stroke-linecap":"square"}))}));const a=function(t,e,i){const n=new s(t,e,i);return n.mount(),n};a.defaults=s.defaults,a.graphers=s.graphers;const r=t=>t.matches("[peity]")?[t]:Array.from(t.querySelectorAll("[peity]")),l=t=>t.getAttribute("peity"),h=t=>{if(t.nodeType==Node.ELEMENT_NODE)return t};return(new class{constructor(){this.elements=new Set}async start(){await new Promise((t=>{"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()})),this.element=document.documentElement;new MutationObserver(this.callback.bind(this)).observe(this.element,{childList:!0,attributes:!0,subtree:!0}),this.processNodeAdded(this.element)}processNodeAdded(t){for(const e of r(t))this.elements.has(e)||(this.elements.add(e),a(e,l(e)))}processNodeRemoved(t){const e=new Set(r(t));for(const t of this.elements)e.has(t)&&(this.elements.delete(t),t._peity&&t._peity.destroy())}processAttributeChanged(t){const e=t.target;if(this.elementIsActive(e))switch(t.attributeName){case"peity":const t=l(e);t?(this.elements.has(e)||this.elements.add(e),a(e,t)):this.elements.has(e)&&(this.elements.delete(e),e._peity&&e._peity.destroy());break;case"data-peity":e._peity&&a(e,e._peity.type)}}callback(t,e){t.forEach((t=>{switch(t.type){case"childList":for(const e of Array.from(t.removedNodes)){const t=h(e);t&&this.processNodeRemoved(t)}for(const e of Array.from(t.addedNodes)){const t=h(e);t&&this.elementIsActive(t)&&this.processNodeAdded(t)}break;case"attributes":this.processAttributeChanged(t)}}))}elementIsActive(t){return this.element.contains(t)}}).start(),a}();
@@ -1,3 +1,3 @@
1
1
  module PeityVanillaRails
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peity_vanilla_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-05-24 00:00:00.000000000 Z
12
+ date: 2022-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails