-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwdtexthelper_min.js
10 lines (10 loc) · 4.53 KB
/
gwdtexthelper_min.js
1
2
3
4
5
6
7
8
9
10
-function(){"use strict";var g;var k=function(a,b){var c=parseInt(a.getAttribute("gwd-min-font-size-px"),10);isNaN(c)&&(c=b);return c},l=function(a,b){switch(a.getAttribute("gwd-fitting-truncate")){case "true":return!0;case "false":return!1;default:return b}};var n=/[^\s\xA0]/,q=function(a){return(a=p.exec(a||""))?parseFloat(a[1]):null},p=/^(\d+|\d*\.\d+)px$/i,r=/^(\d+|\d*\.\d+)em$/i;function v(){this.a=[];this.o=10;this.v=!1}
v.prototype.fit=function(a){var b=[];a.forEach(function(a){if(this.a.every(function(b){return b.element!=a})&&!a.hasAttribute("gwd-dont-fit")){w(a);var d=x(this,a);this.a.push(d);b.push(d)}},this);b.forEach(function(a){var b={originalFontSize:a.s,fittedFontSizePx:a.w,wasTruncated:null!=a.h};null!=a.h&&(b.originalInnerHTML=a.h);a=a.element;var e;b?(e=document.createEvent("CustomEvent"),e.initCustomEvent("gwdtextfitted",!0,!0,b)):(e=document.createEvent("Event"),e.initEvent("gwdtextfitted",!0,!0));
a.dispatchEvent(e)})};
var y=function(a,b){b.forEach(function(a){for(var b=0;b<this.a.length;b++){var e=this.a[b];if(e.element==a){e.element.style.fontSize=e.s;this.a.splice(b,1);break}}}.bind(a))},x=function(a,b){var c=window.getComputedStyle(b),d=q(c.fontSize)||24,e=k(b,a.o),h=l(b,a.v),c=b.style.fontSize,t=null,f=d;a:{for(f=d;f>=e;f--)if(b.style.fontSize=""+f+"px",!(b.scrollHeight>b.clientHeight||b.scrollWidth>b.clientWidth))break a;f=e;if(h){for(var t=b.innerHTML,e=b.textContent,h=e.length,d=1,u=0;10>u;u++){var m=Math.floor((h+
d)/2);b.innerHTML=e.slice(0,m)+"\u2026";b.scrollHeight>b.clientHeight||b.scrollWidth>b.clientWidth?h=m:d=m}b.innerHTML=e.slice(0,d)+"\u2026"}}return{element:b,s:c,w:f,h:t}},w=function(a){function b(a,c){a.hasAttribute("gwd-dont-fit")||Array.prototype.forEach.call(a.children,function(a){var d=window.getComputedStyle(a).fontSize,f=q(d);if(f)f!=c&&(d=f/c,a.style.fontSize=d+"em");else if(d=(f=r.exec(d||""))?parseFloat(f[1]):null)f=d*c;else return;b(a,f)})}var c=q(window.getComputedStyle(a).fontSize);
null!=c&&b(a,c)};var z=function(a){this.u=a;this.g=new WeakMap;this.j=new WeakMap};z.prototype.saveInitialText=function(a){for(var b=document.createNodeIterator(a,NodeFilter.SHOW_TEXT,null,!1),c=null,d;d=b.nextNode();)if(n.test(d.textContent)){c=d;break}if(c&&(b=c.parentNode,b!=a)){a.setAttribute("gwd-dont-fit","");for(c=b;c.parentNode!=a;)c=c.parentNode;this.g.set(a,{f:c,parent:b})}};
z.prototype.applyStylePreservation=function(a){if(this.g.get(a)){a.ownerDocument.compareDocumentPosition(a)&Node.DOCUMENT_POSITION_DISCONNECTED||A(this,a);var b=document.createElement("gwd-attached");a.appendChild(b);var c=function(){b.removeEventListener("attached",c);A(this,a);a.removeChild(b)}.bind(this);b.addEventListener("attached",c)}};
var A=function(a,b){var c=a.g.get(b),d=a.j.get(b)||{};if(c){c.f.style.display="none";b.insertBefore(c.f,b.firstChild);try{y(a.u,[b]);var e=getComputedStyle(c.parent);B.forEach(function(a){var c=e.getPropertyValue(a);!d.hasOwnProperty(a)&&c&&b.style.setProperty(a,c,"")});b.removeAttribute("gwd-dont-fit");a.u.fit([b])}finally{b.removeChild(c.f)}}};z.prototype.saveStyleOverride=function(a,b,c){var d=this.j.get(a)||{};d[b]=c;this.j.set(a,d)};var B="color font-family font-size font-style font-weight font-stretch text-align white-space".split(" ");var C=function(){};goog.inherits(C,HTMLElement);g=C.prototype;g.createdCallback=function(){this.c=new v;this.b=":not(*)";this.i=new z(this.c);this.C=this.B.bind(this);this.l=this.A.bind(this);document.addEventListener("pagetransitionstart",this.C);document.addEventListener("adinitialized",this.l);["fit-font-size-for-selector","gwd-fitting-truncate","gwd-min-font-size-px"].forEach(this.m,this)};
g.A=function(a){"gwd-doubleclick"==a.target.tagName.toLowerCase()&&(document.removeEventListener("adinitialized",this.l),this.fit(Array.prototype.slice.call(document.querySelectorAll(this.b))))};g.B=function(a){"gwd-pagedeck"==a.target.getAttribute("is").toLowerCase()&&this.fit(Array.prototype.slice.call(a.target.querySelectorAll(this.b)))};g.saveInitialText=function(a){this.i.saveInitialText(a)};g.applyStylePreservation=function(a){this.i.applyStylePreservation(a)};
g.saveStyleOverride=function(a,b,c){this.i.saveStyleOverride(a,b,c)};g.m=function(a){var b=this.getAttribute(a);switch(a){case "fit-font-size-for-selector":this.b=b||":not(*)";break;case "gwd-min-font-size-px":this.c.o=k(this,10);break;case "gwd-fitting-truncate":this.c.v=l(this,!1)}};g.fit=function(a){this.c.fit(a||Array.prototype.slice.call(document.querySelectorAll(this.b)))};g.attributeChangedCallback=function(a){this.m(a)};document.registerElement("gwd-text-helper",{prototype:C.prototype});}()