/*\
title: $:/plugins/ihm/tidgraph/utils.js
type: application/javascript
module-type: library

Internal utility functions for tidgraph plugin.

\*/
(function(){function u(a){var c=a.getBoundingClientRect(),b=document.body,e=document.documentElement,g=c.top-(a.scrollTop||window.pageYOffset||e.scrollTop||b.scrollTop)-(e.clientTop||b.clientTop||0);a=c.left-(a.scrollLeft||window.pageXOffset||e.scrollLeft||b.scrollLeft)-(e.clientLeft||b.clientLeft||0);return{top:g,left:a,width:c.width,height:c.height,right:a+c.width,bottom:g+c.height}}function q(a,c,b){b=b||function(a,b,c){if(a)return!0};a=$tw.utils.parseStringArray(a);for(var e=a.length,g=c.length,
f=0;f<g;f++)for(var h=0;h<e;h++){var d=$tw.wiki.getTiddler(c[f]);if(d&&(d=d.getFieldString(a[h]),b(d,a[h],c[f])))return d}return""}function x(a){var c=!1;return(a=q("_tgr_node_class _tgr_node_class_add",[a.id,a.template],function(a,e,g){if(a)return c="_tgr_node_class_add"===e?!0:!1,!0}))&&"tgr-default"!==a?c?"ihm-tgr-node tgr-node "+a:a:"ihm-tgr-node tgr-node"}function w(a,c,b){var e;a=u(a);if("string"===typeof b){if(e=document.querySelector(b),null==e)return null}else b instanceof HTMLElement&&(e=
b);var g=u(e);b=g.bottom-a.top;e=g.left-a.left;var f=g.right-a.left;a=g.top-a.top;g="";switch(c.toUpperCase()){case "L":g=[Math.round(e),Math.round(b/2+a/2)];break;case "R":g=[Math.round(f),Math.round(b/2+a/2)];break;case "T":g=[Math.round(f/2+e/2),Math.round(a)];break;case "B":g=[Math.round(f/2+e/2),Math.round(b)]}return g}function y(a,c,b,e,g){var f;a:{var h=u(c),d=u(b);f=h.left+h.width/2;var h=h.top+h.height/2,n=d.left+d.width/2,d=d.top+d.height/2;switch(e){case "E":f=4>n-f?["R","R"]:["R","L"];
break a;case "S":f=4>d-h?["B","B"]:["B","T"];break a}f=void 0}d=w(a,f[0],c);a=w(a,f[1],b);var m,l,h=10,n="";g&&(n=' class="tgr-edge-weak"');if(null==c||null==b)return error("can't connect null element");if(null==d)return error("port not found for "+c.tagName+" - "+c.innerHTML);if(null==a)return error("port not found for "+b.tagName+" - "+b.innerHTML);c=Math.abs(a[1]-d[1]);b=Math.abs(a[0]-d[0]);switch(e){case "E":return a[1]>d[1]&&(m=c/2),a[1]<d[1]&&(m=-c/2),5>c&&(m=0),"L"==f[1]&&(l=-10),"R"==f[1]&&
(l=10,h=20),'<path d="M'+d[0]+","+d[1]+" Q"+(d[0]+h)+","+d[1]+"  "+(d[0]+h)+","+(d[1]+m)+" Q"+(d[0]+h)+","+a[1]+"  "+(a[0]+l)+","+a[1]+'"'+n+' marker-end="url(#tgr-arrow)"/>';case "S":return a[0]>d[0]&&(l=b/2),a[0]<d[0]&&(l=-b/2),5>b&&(l=0),"T"==f[1]&&(m=-10,h=10),"B"==f[1]&&(m=10,h=20),'<path d="M'+d[0]+","+d[1]+" Q"+d[0]+","+(d[1]+h)+"  "+(d[0]+l)+","+(d[1]+h)+" Q"+a[0]+","+(d[1]+h)+"  "+a[0]+","+(a[1]+m)+'"'+n+' marker-end="url(#tgr-arrow)"/>'}}function v(a,c){var b;switch(c.mode){case "tagging":b=
"[["+a+"]tagging[]]+"+c.filter;break;case "linking":b="[["+a+"]links[]!is[missing]]+"+c.filter;break;default:b="[["+a+"]"+c.mode+"]+"+c.filter}return $tw.wiki.filterTiddlers(b)}function z(a,c,b){switch(b.mode.toLowerCase()){case "tagging":return(b=$tw.wiki.getTiddler(a))?b.hasTag(c):!1;default:return b=v(c,b),-1!==b.indexOf(a)}}function A(a,c){function b(b,l,k){h=l;d=b;n=encodeURIComponent(h);m=encodeURIComponent(d);g=document.getElementById(c.id+"-"+n);f=document.getElementById(c.id+"-"+m);g&&f&&
e.push(y(a,g,f,c.layout,k))}var e=[],g,f,h,d,n,m;p(c.root,function(a,c,d){(c=a.parent)&&b(a.id,c.id)},{},{skipvisited:!0});for(var l=c.outliers.length,k=0;k<l;k++)b(c.outliers[k][0],c.outliers[k][1],!0);return e.join(" ")}function p(a,c,b,e){e=e||{};var g=e.done||[],f=e.getCh||function(a){return a.collapse?[]:a.children},h=e.lvl||0,d=void 0===e.skipvisited?!0:e.skipvisited;e.leave=e.leave||!1;if(d&&-1!==g.indexOf(a))return b;g.push(a);f=f(a);d=f.length;b=b||{};e.lvl=h+1;e.done=g;if(!1===c(a,b,h))return e.leave=
!0,b;for(a=0;a<d;a++)if(b=p(f[a],c,b,e),e.leave)return b;e.lvl--;return b}function B(a,c,b,e){e=e||{};var g=e.getCh||function(a){return a.collapse?[]:a.children},f=e.getId||function(a){return a.id},h=void 0===e.skipvisited?!0:e.skipvisited,d=e.maxdepth||Number.MAX_VALUE;b=b||{};var n=[],m=[],l=[],k=0;n.push(a);l[f(a)]=void 0;do{a=n.length;for(var q=0;q<a;q++){var r=n.shift(),p;p=h?-1===m.indexOf(r)?!1:!0:!1;if(!p&&!1===c(r,l[f(r)],b,k))return b;m.push(r);p=g(r);n=n.concat(p);p&&p.forEach(function(a){var b=
l[f(a)];b?f(b)!==f(r)&&e.outlier&&e.outlier(a,r):l[f(a)]=r})}k++}while(0!==n.length&&k<=d);return b}function C(a,c){return p(a,function(a,c){c.cnt++;return!0},{cnt:0},{skipvisited:c}).cnt-1}function D(a,c,b){function e(a,b){if(-1!==$tw.utils.parseStringArray(b).indexOf(c.toString()))return!0}b=$tw.utils.parseStringArray(b);var g=q("_tgr_node_template",[a]),f=[];$tw.utils.each(b,function(b){var c=$tw.wiki.getTiddler(b),c=c?c.getFieldString("_tgr_node_filter"):"",d=$tw.wiki.filterTiddlers(c);c&&-1===
d.indexOf(a)&&f.push(b)});0<f.length&&$tw.utils.removeArrayEntries(b,f);g||q("_tgr_node_filter",b,function(b,c,d){c=$tw.wiki.filterTiddlers(b);if(b&&-1!==c.indexOf(a))if(b=$tw.wiki.getTiddler(d).getFieldString("_tgr_node_level")){if(e(d,b))return g=d,!0}else return g=d,!0});g||q("_tgr_node_level",b,function(a,b,c){if(e(c,a))return g=c,!0});if(!g)for(var h=b.length,d=0;d<h;d++){var n=$tw.wiki.getTiddler(b[d]);if(n&&!n.hasField("_tgr_node_level")&&!n.hasField("_tgr_node_filter")){g=b[d];break}}g||(g=
"tgr-default");return g}function t(a,c,b,e){if(!(this instanceof t))throw"Error: call new tnode(id="+c+")";this.parent=a;this.id=c;this.children=[];this.collapse=!1;this.widget=e;this.template=void 0;a=D(c,b,e.nodetemplate);"tgr-default"!==a&&(this.transcluder=b="$:/temp/tidgraph/"+e.tidtree.id+"/"+c,this.template=a,$tw.wiki.addTiddler(new $tw.Tiddler({title:b,text:"{{"+c+"||"+a+"}}"})),-1===e.templatesInUse.indexOf(a)&&e.templatesInUse.push(a))}exports.buildTable=function(a,c){function b(a,b){return $tw.utils.domMaker(a,
$tw.utils.extend(b,{document:c.document}))}function e(a){var d=encodeURIComponent(a.id),e;var f=a.id;e=$tw.wiki.tiddlerExists(f)?c.nodetitle?q(c.nodetitle,[f]):q("caption title",[f]):f;var g=$tw.wiki.tiddlerExists(a.id)?"tc-tiddlylink-resolves":"tc-tiddlylink-missing",g="tc-tiddlylink "+g,f=x(a);a.template?a=b("div",{"class":f,innerHTML:$tw.wiki.renderTiddler("text/html",a.transcluder)}):(a=b("a",{"class":g,text:e,attributes:{href:"#"+d}}),a=b("div",{"class":f,children:[a]}));return a}function g(a,
d){var e=1+C(a,!0),f=encodeURIComponent(a.id),g=q(c.tooltip,[a.id]),k;!1===c.nocollapse&&a.children&&0<a.children.length?(k=$tw.wiki.renderTiddler("text/html","$:/plugins/ihm/templates/"+(a.collapse?"expand":"collapse")).replace(/^<p>/,"<span>").replace(/<\/p>$/,"</span>"),k=b("span",{"class":"ihm-tgr-collapse "+("E"==a.widget.tidtree.layout?"ihm-tgr-collapse-east":"ihm-tgr-collapse-south")+" tc-tiddlylink",innerHTML:k}),$tw.utils.addEventListeners(k,[{name:"click",handlerObject:a,handlerMethod:"collapseClickEvent"}]),
k=[d,k]):k=[d];f=b("div",{"class":"ihm-tgr-node-container "+("E"==a.widget.tidtree.layout?"ihm-tgr-node-container-east":"ihm-tgr-node-container-south"),children:k,attributes:{id:c.id+"-"+f,title:g}});return"E"===c.layout?b("td",{attributes:{rowspan:e},children:[f]}):b("div",{attributes:{"class":"ihm-tgr-node-cell"},children:[f]})}var f;f="E"==c.layout?b("table",{"class":"ihm-tgr-table",attributes:{id:c.id+"-table"}}):b("div",{"class":"ihm-tgr-divtable",attributes:{id:c.id+"-table"}});(function(a){switch(c.layout){case "E":p(c.root,
function(d,f,m){m>=c.startat&&(f=e(d),d=g(d,f),d=b("tr",{children:[d]}),a.appendChild(d));return!0},{},{skipvisited:!0});break;case "S":p(c.root,function(d,f,m){if(m>=c.startat){var l=e(d),l=g(d,l),k=f.nodegroup[f.nodegroup.length-1];k?m>=f.lastdepth?k.appendChild(l):m<f.lastdepth&&(f.nodegroup.pop(),k=f.nodegroup[f.nodegroup.length-1],k.appendChild(l)):a.appendChild(l);!d.collapse&&0<d.children.length&&(d=b("div",{"class":"ihm-tgr-node-group"}),f.nodegroup.push(d),l.appendChild(d))}f.lastdepth=m;
return!0},{nodegroup:[],lastdepth:-1},{skipvisited:!0})}})(f);return f};exports.error=function(a){return'<span style="color:green; font-size:1.5em">\u26a0 Tidgraph: </span><span style="color:red">'+a+"</span>"};exports.buildSVG=function(a,c){var b=document.getElementById(c.id+"-table");if(b)return getComputedStyle(b),'<svg  xmlns="http://www.w3.org/2000/svg" height="'+a.offsetHeight+'px" width="'+a.offsetWidth+'px" style="overflow: visible"><g class="ihm-tgr-link tgr-link tgr-edge" style="overflow: visible"> <defs> <marker id="tgr-arrow" viewBox="0 0 10 10" refX="1" refY="5" markerUnits="strokeWidth" orient="auto" markerWidth="8" markerHeight="6"> <polyline class="ihm-tgr-arrow tgr-arrow" points="0,0 10,5 0,10 0,5" style="opacity:1;" /></marker></defs> '+
A(a,c)+"</g> </svg>"};exports.isDescendant=function(a,c,b){if(z(a,c,b))return!0;var e=!1;p(c,function(b,c,h){if(b===a)return e=!0,!1},{},{skipvisited:!0,getCh:function(a){return v(a,b)}});return e};exports.makeTidTree=function(a,c,b){b=b||{};var e=!1;c.outliers=[];var g=new t(void 0,a,0,b.widget);B(a,function(a,c,d,e){if(c){a:{for(var g=d.visited,l=g.length,k=0;k<l;k++)if(g[k].id===c){c=g[k];break a}c=void 0}a=c.addChild(a,e,b.widget);d.visited.push(a)}return!0},{visited:[g]},{getId:function(a){return a},
getCh:function(a){return v(a,c)},maxdepth:c.maxdepth,skipvisited:!0,outlier:function(a,b){e=!1;$tw.utils.each(c.outliers,function(c){c[0]===a&&c[1]===b&&(e=!0)});e||c.outliers.push([a,b])}});return g};t.prototype.addChild=function(a,c,b){a=new t(this,a,c,b);this.children.push(a);return a};t.prototype.toString=function(){return"tnode(id="+this.id+")"};t.prototype.collapseClickEvent=function(a){this.collapse=!this.collapse;this.widget.paint()}})();
