chisaco / jsvectoreditor

Automatically exported from code.google.com/p/jsvectoreditor
1 stars 0 forks source link

Minor improvement to dumpshape #12

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
in order to have 0 opacity saved to JSON you have to do 
{{{
            var tmp = shape.attr(attr[i]);
            if (tmp != undefined)
}}}
By the way, I suggest putting the setJSON and getJSON methods into the 
VectorEditor (I have already done that in my code):
{{{
VectorEditor.prototype.getJSON = function()
{
    var attr = "cx,cy,fill,fill-opacity,font,font-family,font-
size,font-
weight,gradient,height,opacity,path,r,rotation,rx,ry,src,stroke,stroke-
dasharray,stroke-opacity,stroke-width,width,x,y,text".split(",");
    function dumpShape (shape)
    {
        var info = {
            type: shape.type,
            id: shape.id,
            subtype: shape.subtype
        }
        for (var i = 0; i < attr.length; i++)
        {
            var tmp = shape.attr(attr[i]);
            if (tmp != undefined)
            {
                if (attr[i] == "path"){
                    tmp = tmp.toString();
                }
                info[attr[i]] = tmp;
            }
        }
        return info;
    }
    return encode(jQuery.map(this.shapes || [], dumpShape));
}

VectorEditor.prototype.setJSON = function(info)
{
    var instance = this;
    function loadShape(shape, noattachlistener)
    {
        if(!shape || !shape.type || !shape.id)
            return;     
        var newshape = null, draw = instance.draw;
        if(!(newshape=editor.getShapeById(shape.id)))
        {
            if (shape.type == "rect")
            {
                newshape = draw.rect(0, 0,0, 0)
            }
            else if (shape.type == "path")
            {
                newshape = draw.path("")
            }
            else if(shape.type == "image")
            {
                newshape = draw.image(shape.src, 0, 0, 0, 
0)
            }
            else if(shape.type == "ellipse")
            {
                newshape = draw.ellipse(0, 0, 0, 0)
            }
            else if(shape.type == "text")
            {
                newshape = draw.text(0, 0, shape.text)
            }
        }
        newshape.attr(shape);
        newshape.id = shape.id;
        newshape.subtype = shape.subtype;

        if (!noattachlistener) 
        {
            instance.addShape(newshape, true)
        }
    }

    try
    {
        var json = eval("("+ info +")");
        jQuery(json).each(function(index, item){
            loadShape(item);
        });
        return true;
    }
    catch (err)
    {
        // FIXME: log in other way
        alert(err.message);
        return false;
    }
}

}}}

Original issue reported on code.google.com by emiro...@gmail.com on 6 Jan 2010 at 10:30