From b25f8b6d92923c25890bfa1d933cd0fe30c32d9f Mon Sep 17 00:00:00 2001 From: Alexis Deveria Date: Mon, 8 Feb 2010 16:39:14 +0000 Subject: [PATCH] Added fix to jQuery.attr() function to make it work with SVG elements, started work on marker color changing git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1355 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/svg-editor.js | 1 - editor/svgcanvas.js | 111 +++++++++++++++++++++++++++++++-------- extensions/ext-arrows.js | 41 +++++++++++++++ 3 files changed, 129 insertions(+), 24 deletions(-) diff --git a/editor/svg-editor.js b/editor/svg-editor.js index 967a36c3..e7c293cd 100644 --- a/editor/svg-editor.js +++ b/editor/svg-editor.js @@ -42,7 +42,6 @@ function svg_edit_setup() { default_img_url = "images/logo.png", workarea = $("#workarea"); - // Store and retrieve preferences $.pref = function(key, val) { if(val) curPrefs[key] = val; diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index d2135dfe..f8d5fa64 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -20,6 +20,68 @@ if(window.opera) { window.console.dir = function(str) {}; } +(function() { + + // This fixes $(...).attr() to work as expected with SVG elements. + // Does not currently use *AttributeNS() since we rarely need that. + + // See http://api.jquery.com/attr/ for basic documentation of .attr() + + // Additional functionality: + // - When getting attributes, a string that's a number is return as type number. + // - If an array is supplied as first parameter, multiple values are returned + // as an object with values for each given attributes + + var proxied = jQuery.fn.attr, svgns = "http://www.w3.org/2000/svg"; + jQuery.fn.attr = function(key, value) { + var len = this.length; + if(!len) return this; + for(var i=0; i 0) { - if (!preventUndo) + if (!preventUndo) { this.changeSelectedAttribute("stroke", val, elems); - else + call("changed", elems); + } else this.changeSelectedAttributeNoUndo("stroke", val, elems); } }; @@ -6053,9 +6117,10 @@ function BatchCommand(text) { } } if (elems.length > 0) { - if (!preventUndo) + if (!preventUndo) { this.changeSelectedAttribute("fill", val, elems); - else + call("changed", elems); + } else this.changeSelectedAttributeNoUndo("fill", val, elems); } }; diff --git a/extensions/ext-arrows.js b/extensions/ext-arrows.js index 8d3c6f48..f020d671 100644 --- a/extensions/ext-arrows.js +++ b/extensions/ext-arrows.js @@ -133,6 +133,32 @@ $(function() { S.call("changed", selElems); } + function colorChanged(elem) { + var color = elem.getAttribute('stroke'); + + var markers = ['start','mid','end']; + + $.each(markers, function(i, type) { + var href = elem.getAttribute('marker-'+type); + if(href) { + var marker_id = href.match(/\(\#(.*)\)/)[1]; + var marker = S.getElem(marker_id); + var shape = marker.firstChild; + var cur_color = shape.getAttribute('fill'); + console.log(cur_color, color); + + + // If color matches, ignore + // If color doesn't match, look for marker with shape that does match color + // - Found? Use its URL! + // - Not found? Create new one (based on this one) but with new fill + // (don't remove old marker: removeUnused* can take care of that) + } + + }); + + } + return { name: "Arrows", context_tools: [{ @@ -181,6 +207,21 @@ $(function() { showPanel(false); } } + }, + elementChanged: function(opts) { + var elem = opts.elems[0]; + if(elem && ( + elem.getAttribute("marker-start") || + elem.getAttribute("marker-mid") || + elem.getAttribute("marker-end") + )) { + // var start = elem.getAttribute("marker-start"); + // var mid = elem.getAttribute("marker-mid"); + // var end = elem.getAttribute("marker-end"); + // Has marker, so see if it should match color + colorChanged(elem); + } + } }; });