/*
 * DEXTUploadX5 - SVG manipulation library
 * http://www.dextsolution.com
 *
 * Copyright ⓒ DEXTSolution Inc.
 */
var SVGHelper = function () {}; SVGHelper.prototype.SVG_NS = "http://www.w3.org/2000/svg"; SVGHelper.prototype.XLINK_NS = "http://www.w3.org/1999/xlink"; SVGHelper.prototype.graphicElementNames = ["circle", "ellipse", "image", "line", "path", "polygon", "polyline", "rect", "text", "use"]; SVGHelper.prototype.isXLink = function (val) { if (val && val.length > 6 && val.substring(0, 6) == "xlink:") return true; else return false; }; SVGHelper.prototype.mkTextNode = function (txt) { return document.createTextNode(txt); };
SVGHelper.prototype.getText = function (element) { if (element && element.textContent) return element.textContent; else return ""; }; SVGHelper.prototype.replaceText = function (element, text) { if (element && (element.nodeName == "text" || element.nodeName == "tspan" || element.nodeName == "title")) { element.textContent = text; } }; SVGHelper.prototype.create = function (name, id, pack, child) { var element = document.createElementNS(this.SVG_NS, name); if (id) element.setAttribute("id", id); if (pack) this.attrs(element, pack, false); if (child) element.appendChild(child);
return element; }; SVGHelper.prototype.get = function (id) { return document.getElementById(getIdSharp(id, false)); }; SVGHelper.prototype.query = function (selector) { return document.querySelector(selector); }; SVGHelper.prototype.queryAll = function (selector) { return document.querySelectorAll(selector); }; SVGHelper.prototype.attr = function (element, name, val, appendable) { if (!element) { console.log("DX5: svg-helper attr call: element => " + element); console.log("DX5: svg-helper attr call: name => " + name); console.log("DX5: svg-helper attr call: val => " + val);
console.log("DX5: svg-helper attr call: appendable => " + appendable); throw new Error("DX5: element is " + element); } if (typeof (val) == "undefined") { if (this.isXLink(name)) return element.getAttributeNS(this.XLINK_NS, name.substring(6)); else return element.getAttributeNS(null, name); } else { if (appendable) { var oval = this.attr(element, name); if (oval && oval.length > 0) { val = oval + " " + val; } } if (this.isXLink(name))   element.setAttributeNS(this.XLINK_NS, name, val); else element.setAttributeNS(null, name, val); return this;
} }; SVGHelper.prototype.removeAttr = function (element, name, val) { var oval = this.attr(element, name); if (oval && oval.length > 0) { var tokens = oval.split(" "); for (var i = 0; i < tokens.length;) { if (tokens[i] == val) { tokens.splice(i, 1); } else { i++; } } oval = trim(tokens.join(" ")); } if (oval) { if (this.isXLink(name)) element.setAttributeNS(this.XLINK_NS, name.substring(6), oval); else element.setAttributeNS(null, name, oval); } return this; }; SVGHelper.prototype.attrs = function (element, pack, appendable) { if (!pack) return this;
for (var name in pack) { var val = pack[name]; if (appendable) { var oval = this.attr(element, name); if (oval && oval.length > 0) { val = oval + " " + val; } } if (this.isXLink(name)) { element.setAttributeNS(this.XLINK_NS, name.substring(6), val); } else { element.setAttributeNS(null, name, val); } } return this; }; SVGHelper.prototype.isGraphicElement = function (target) { if (!target || !target.nodeName) return false; return (graphicElementNames.indexOf(target.nodeName.toLowerCase()) != -1); };
SVGHelper.prototype.createNS = function (ns, name, id, pack, child) { var element = document.createElementNS(ns, name); if (id) element.setAttribute("id", id); if (pack) this.attrs(element, pack, false); if (child) element.appendChild(child); return element; }; SVGHelper.prototype.createUnit = function (name, id, pack, child) { var element = this.create(name, id, pack, child); return new SVGUnit(element); }; SVGHelper.prototype.createNSUnit = function (ns, name, id, pack, child) { var element = this.createNS(ns, name, id, pack, child); return new SVGUnit(element);
}; SVGHelper.prototype.getAsUnit = function (id) { var target = document.getElementById(getIdSharp(id, false)); if (target) return new SVGUnit(target); else return null; }; var SVGUnit = function (target) { this.element = (target instanceof SVGUnit) ? target.element : target; }; SVGUnit.prototype.SVG_NS = "http://www.w3.org/2000/svg"; SVGUnit.prototype.XLINK_NS = "http://www.w3.org/1999/xlink"; SVGUnit.prototype.isXLink = function (val) { return (val && val.length > 6 && val.substring(0, 6) == "xlink:"); };
SVGUnit.prototype.getId = function (id) { return this.element.getAttribute("id"); }; SVGUnit.prototype.setId = function (id) { this.element.setAttribute("id", id); return this; }; SVGUnit.prototype.has = function (name) { return typeof this.element[name] != "undefined"; }; SVGUnit.prototype.att = function (name, val, appendable) { if (typeof (val) == "undefined") { if (this.isXLink(name)) return this.element.getAttributeNS(this.XLINK_NS, name.substring(6)); else return this.element.getAttributeNS(null, name); } else {
if (appendable) { var oval = this.att(name); if (oval && oval.length > 0) { val = oval + " " + val; } } if (this.isXLink(name)) this.element.setAttributeNS(this.XLINK_NS, name, val); else this.element.setAttributeNS(null, name, val);       if (name === "layout-dx5") { if (this.element.calculateExpression) { var pairs = this.splitToStylePairs(val); for (var n in pairs) { this.element.calculateExpression[n] = null; } } } return this; } }; SVGUnit.prototype.attp = function (pack, appendable) { if (!pack) return this; for (var name in pack) {
var val = pack[name]; if (appendable) { var oval = this.att(name); if (oval && oval.length > 0) { val = oval + " " + val; } } if (this.isXLink(name)) { this.element.setAttributeNS(this.XLINK_NS, name.substring(6), val); } else { this.element.setAttributeNS(null, name, val); }       if (name === "layout-dx5") { if (this.element.calculateExpression) { var pairs = this.splitToStylePairs(val); for (var n in pairs) { this.element.calculateExpression[n] = null; } } } } return this; }; SVGUnit.prototype.atto = function (name, obj) { if (typeof (obj) == "undefined") {
return this.element[name]; } else { this.element[name] = obj; return this; } }; SVGUnit.prototype.css = function (name, val) { if (typeof (val) == "undefined") { return this.element.style[name]; } else { this.element.style[name] = val; return this; } }; SVGUnit.prototype.detach = function () { if (this.element.parentNode) { this.element.parentNode.removeChild(this.element); } return this; }; SVGUnit.prototype.append = function (child) { if (child instanceof SVGUnit) { child.detach(); this.element.appendChild(child.element); } else {
if (child.parentNode) child.parentNode.removeChild(child); this.element.appendChild(child); } return this; }; SVGUnit.prototype.appendBefore = function (nChild, beforeChild) { var pnode = this.element, nnode = null, rnode = null; if (nChild instanceof SVGUnit) { nChild.detach(); nnode = nChild.element; } else { if (nChild.parentNode) nChild.parentNode.removeChild(nChild); nnode = nChild; } rnode = (beforeChild instanceof SVGUnit) ? beforeChild.element : beforeChild; pnode.insertBefore(nnode, rnode); return this;
}; SVGUnit.prototype.getNextSibling = function () { var n = this.element.nextSibling;  return n ? new SVGUnit(n) : null; }; SVGUnit.prototype.into = function (container) { if (container instanceof SVGUnit) { container.append(this); } else if (container.appendChild) { this.detach(); container.appendChild(this.element); } return this; }; SVGUnit.prototype.bind = function (name, handler, capture) { this.element.addEventListener(name, handler, capture); return this; }; SVGUnit.prototype.query = function (selectors) { var target = this.element.querySelector(selectors);
return target ? new SVGUnit(target) : null; }; SVGUnit.prototype.replaceText = function (text) { if (this.element && (this.element.nodeName == "text" || this.element.nodeName == "tspan" || this.element.nodeName == "title")) { this.element.textContent = text; } return this; }; SVGUnit.prototype.getXinViewbox = function (def) { var t = this.element;   return (t.viewBox && typeof t.viewBox.baseVal != "undefined" && t.viewBox.baseVal != null) ? t.viewBox.baseVal.x : def; }; SVGUnit.prototype.getYinViewbox = function (def) { var t = this.element;
return (t.viewBox && typeof t.viewBox.baseVal != "undefined" && t.viewBox.baseVal != null) ? t.viewBox.baseVal.y : def; }; SVGUnit.prototype.getSVGWLen = function (def) { var t = this.element; return (t.width && typeof t.width.baseVal != "undefined") ? t.width.baseVal.value : def; }; SVGUnit.prototype.getSVGHLen = function (def) { var t = this.element; return (t.height && typeof t.height.baseVal != "undefined") ? t.height.baseVal.value : def; }; SVGUnit.prototype.getBound = function () { var t = this.element; return {
left: t.x.baseVal.value, top: t.y.baseVal.value, right: t.x.baseVal.value + t.width.baseVal.value, bottom: t.y.baseVal.value + t.height.baseVal.value }; }; SVGUnit.prototype.getAllStyles = function () { return window.getComputedStyle(this.element); }; SVGUnit.prototype.getLocalName = function () { return this.element.localName; }; SVGUnit.prototype.getNodeType = function () { return this.element.nodeType; }; SVGUnit.prototype.getComputedSize = function () { var style = this.getAllStyles(); var size = { w: 0, h: 0 };  
if (!style) { console.log("DX5: " + this.element.localName + " element not created"); } else { size.w = parseFloat(style.width) - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight); size.h = parseFloat(style.height) - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom); } return size; }; SVGUnit.prototype.computeExpressionUseCacheFunction = function (name, expr, p, g) { var target = this.element; if (target.calculateExpression && target.calculateExpression[name]) { var uce = target.getAttributeNS(null, "disableCacheExpression"); if (!uce || uce != "disabled") {
  return target.calculateExpression[name](p, g); } } var sv = expr.toString(); var tokens = sv.split(/([\+\-]+)/g); if (tokens.length > 0) { var strExpression = ""; var op = "+", num = 0, c = "", n = 0; for (var i = 0; i < tokens.length; i++) { c = tokens[i]; if (c == "+") { op = "+"; strExpression += "+"; continue; } else if (c == "-") { op = "-"; strExpression += "-"; continue; } else if (c.charAt(c.length - 1) == "%") { n = (p * parseFloat(c) / 100); strExpression += "(p*" + parseFloat(c) + "/100)"; } else if (c.charAt(0) == "{" && c.charAt(c.length - 1) == "}") {
n = g[c.substring(1, c.length - 1)]; strExpression += "g['" + c.substring(1, c.length - 1) + "']"; } else { n = parseFloat(c); strExpression += parseFloat(c).toString(); } switch (op) { case "+": num += n; break; case "-": num -= n; break; } if (!target.calculateExpression) target.calculateExpression = {}; target.calculateExpression[name] = new Function("p", "g", " return " + strExpression + ";"); } return num; } else return expr; }; SVGUnit.prototype.getChildrenCount = function () { return this.element.childNodes.length; };
SVGUnit.prototype.getChildUnit = function (index) { var len = this.element.childNodes.length; if (index >= 0 && index < len) { return new SVGUnit(this.element.childNodes[index]); } else return null; }; SVGUnit.prototype.splitToStylePairs = function (source) { var style = {}; if (!source) return style; var tokens = source.split(";"); for (var i = 0; i < tokens.length; i++) { var kv = tokens[i].split(":"); if (kv.length == 2) style[trim(kv[0])] = trim(kv[1]); } return style; }; SVGUnit.prototype.updateLayout = function (w, h, dic) { if (!dic) dic = {};
var target = this.element; if (target.localName != "g") { var layout = this.att("layout-dx5"), v = 0; if (layout) {   layout = this.splitToStylePairs(layout); for (var n in layout) { v = this.computeExpressionUseCacheFunction(n, layout[n], (["x", "x1", "x2", "cx", "dx", "width"].indexOf(n) >= 0 ? w : h), dic); if ((n == "width" || n == "height") && v < 0) v = 0; if (n == "width") w = v; else if (n == "height") h = v; this.att(n, v); } } w = this.getSVGWLen(w); h = this.getSVGHLen(h); } if (target.id == "UIC-FILE-AREA") {  
var from = this.getYinViewbox(0); if (from < 0) from = 0; this.att("viewBox", "0 " + from + " " + w + " " + h); } for (var i = 0, len = target.childNodes.length, child = null; i < len; i++) { child = target.childNodes[i]; if (child.nodeType === 1) { new SVGUnit(child).updateLayout(w, h, dic); } } return this; }; SVGUnit.prototype.show = function (type) { if (!isDefined(type) || type == 1) this.css("display", "block"); else if (type == 2) this.css("visibility", "visible"); return this; }; SVGUnit.prototype.hide = function (type) { if (!isDefined(type) || type == 1) this.css("display", "none");
else if (type == 2) this.css("visibility", "hidden"); return this; }; 