123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- define("dojox/gfx/utils", ["dojo/_base/kernel","dojo/_base/lang","./_base", "dojo/_base/html","dojo/_base/array", "dojo/_base/window", "dojo/_base/json",
- "dojo/_base/Deferred", "dojo/_base/sniff", "require","dojo/_base/config"],
- function(kernel, lang, g, html, arr, win, jsonLib, Deferred, has, require, config){
- var gu = g.utils = {};
-
- lang.mixin(gu, {
- forEach: function(
- /*dojox.gfx.Surface|dojox.gfx.Shape*/ object,
- /*Function|String|Array*/ f, /*Object?*/ o
- ){
-
-
-
-
-
-
-
- o = o || win.global;
- f.call(o, object);
- if(object instanceof g.Surface || object instanceof g.Group){
- arr.forEach(object.children, function(shape){
- gu.forEach(shape, f, o);
- });
- }
- },
- serialize: function(
- /* dojox.gfx.Surface|dojox.gfx.Shape */ object
- ){
-
-
- var t = {}, v, isSurface = object instanceof g.Surface;
- if(isSurface || object instanceof g.Group){
- t.children = arr.map(object.children, gu.serialize);
- if(isSurface){
- return t.children;
- }
- }else{
- t.shape = object.getShape();
- }
- if(object.getTransform){
- v = object.getTransform();
- if(v){ t.transform = v; }
- }
- if(object.getStroke){
- v = object.getStroke();
- if(v){ t.stroke = v; }
- }
- if(object.getFill){
- v = object.getFill();
- if(v){ t.fill = v; }
- }
- if(object.getFont){
- v = object.getFont();
- if(v){ t.font = v; }
- }
- return t;
- },
- toJson: function(
- /* dojox.gfx.Surface|dojox.gfx.Shape */ object,
- /* Boolean? */ prettyPrint
- ){
-
-
- return jsonLib.toJson(gu.serialize(object), prettyPrint);
- },
- deserialize: function(
- /* dojox.gfx.Surface|dojox.gfx.Shape */ parent,
- /* dojox.gfx.Shape|Array */ object
- ){
-
-
- if(object instanceof Array){
- return arr.map(object, lang.hitch(null, gu.deserialize, parent));
- }
- var shape = ("shape" in object) ? parent.createShape(object.shape) : parent.createGroup();
- if("transform" in object){
- shape.setTransform(object.transform);
- }
- if("stroke" in object){
- shape.setStroke(object.stroke);
- }
- if("fill" in object){
- shape.setFill(object.fill);
- }
- if("font" in object){
- shape.setFont(object.font);
- }
- if("children" in object){
- arr.forEach(object.children, lang.hitch(null, gu.deserialize, shape));
- }
- return shape;
- },
- fromJson: function(
- /* dojox.gfx.Surface|dojox.gfx.Shape */ parent,
- /* String */ json){
-
-
- return gu.deserialize(parent, jsonLib.fromJson(json));
- },
- toSvg: function(/*GFX object*/surface){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var deferred = new Deferred();
-
- if(g.renderer === "svg"){
-
- try{
- var svg = gu._cleanSvg(gu._innerXML(surface.rawNode));
- deferred.callback(svg);
- }catch(e){
- deferred.errback(e);
- }
- }else{
-
-
- if (!gu._initSvgSerializerDeferred) {
- gu._initSvgSerializer();
- }
- var jsonForm = gu.toJson(surface);
- var serializer = function(){
- try{
- var sDim = surface.getDimensions();
- var width = sDim.width;
- var height = sDim.height;
-
- var node = gu._gfxSvgProxy.document.createElement("div");
- gu._gfxSvgProxy.document.body.appendChild(node);
-
- win.withDoc(gu._gfxSvgProxy.document, function() {
- html.style(node, "width", width);
- html.style(node, "height", height);
- }, this);
-
- var ts = gu._gfxSvgProxy[dojox._scopeName].gfx.createSurface(node, width, height);
-
-
- var draw = function(surface) {
- try{
- gu._gfxSvgProxy[dojox._scopeName].gfx.utils.fromJson(surface, jsonForm);
-
- var svg = gu._cleanSvg(node.innerHTML);
- surface.clear();
- surface.destroy();
- gu._gfxSvgProxy.document.body.removeChild(node);
- deferred.callback(svg);
- }catch(e){
- deferred.errback(e);
- }
- };
- ts.whenLoaded(null,draw);
- }catch (ex) {
- deferred.errback(ex);
- }
- };
-
-
- if(gu._initSvgSerializerDeferred.fired > 0){
- serializer();
- }else{
- gu._initSvgSerializerDeferred.addCallback(serializer);
- }
- }
- return deferred;
- },
-
- _gfxSvgProxy: null,
-
- _initSvgSerializerDeferred: null,
- _svgSerializerInitialized: function() {
-
-
-
-
- gu._initSvgSerializerDeferred.callback(true);
- },
- _initSvgSerializer: function(){
-
-
-
-
-
- if(!gu._initSvgSerializerDeferred){
- gu._initSvgSerializerDeferred = new Deferred();
- var f = win.doc.createElement("iframe");
- html.style(f, {
- display: "none",
- position: "absolute",
- width: "1em",
- height: "1em",
- top: "-10000px"
- });
- var intv;
- if(has("ie")){
- f.onreadystatechange = function(){
- if(f.contentWindow.document.readyState == "complete"){
- f.onreadystatechange = function() {};
- intv = setInterval(function() {
- if(f.contentWindow[kernel.scopeMap["dojo"][1]._scopeName] &&
- f.contentWindow[kernel.scopeMap["dojox"][1]._scopeName].gfx &&
- f.contentWindow[kernel.scopeMap["dojox"][1]._scopeName].gfx.utils){
- clearInterval(intv);
- f.contentWindow.parent[kernel.scopeMap["dojox"][1]._scopeName].gfx.utils._gfxSvgProxy = f.contentWindow;
- f.contentWindow.parent[kernel.scopeMap["dojox"][1]._scopeName].gfx.utils._svgSerializerInitialized();
- }
- }, 50);
- }
- };
- }else{
- f.onload = function(){
- f.onload = function() {};
- intv = setInterval(function() {
- if(f.contentWindow[kernel.scopeMap["dojo"][1]._scopeName] &&
- f.contentWindow[kernel.scopeMap["dojox"][1]._scopeName].gfx &&
- f.contentWindow[kernel.scopeMap["dojox"][1]._scopeName].gfx.utils){
- clearInterval(intv);
- f.contentWindow.parent[kernel.scopeMap["dojox"][1]._scopeName].gfx.utils._gfxSvgProxy = f.contentWindow;
- f.contentWindow.parent[kernel.scopeMap["dojox"][1]._scopeName].gfx.utils._svgSerializerInitialized();
- }
- }, 50);
- };
- }
-
- var uri = (config["dojoxGfxSvgProxyFrameUrl"]||require.toUrl("dojox/gfx/resources/gfxSvgProxyFrame.html"));
- f.setAttribute("src", uri.toString());
- win.body().appendChild(f);
- }
- },
- _innerXML: function(/*Node*/node){
-
-
-
-
-
-
- if(node.innerXML){
- return node.innerXML;
- }else if(node.xml){
- return node.xml;
- }else if(typeof XMLSerializer != "undefined"){
- return (new XMLSerializer()).serializeToString(node);
- }
- return null;
- },
- _cleanSvg: function(svg) {
-
-
-
-
- if(svg){
-
- if(svg.indexOf("xmlns=\"http://www.w3.org/2000/svg\"") == -1){
- svg = svg.substring(4, svg.length);
- svg = "<svg xmlns=\"http://www.w3.org/2000/svg\"" + svg;
- }
-
- if(svg.indexOf("xmlns:xlink=\"http://www.w3.org/1999/xlink\"") == -1){
- svg = svg.substring(4, svg.length);
- svg = "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\"" + svg;
- }
-
-
- if(svg.indexOf("xlink:href") === -1){
- svg = svg.replace(/href\s*=/g, "xlink:href=");
- }
-
-
- svg = svg.replace(/\bdojoGfx\w*\s*=\s*(['"])\w*\1/g, "");
- svg = svg.replace(/\b__gfxObject__\s*=\s*(['"])\w*\1/g, "");
- svg = svg.replace(/[=]([^"']+?)(\s|>)/g,'="$1"$2');
-
-
-
- svg = svg.replace(/\bstroke-opacity\w*\s*=\s*(['"])undefined\1/g, "");
- }
- return svg;
- }
- });
- return gu;
- });
|