Trying streamlined svgicons again

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1719 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Alexis Deveria 2010-09-16 12:19:35 +00:00
parent ef86b8a08a
commit 3e159f2180
1 changed files with 12 additions and 62 deletions

View File

@ -128,39 +128,6 @@ $(function() {
(function($) { (function($) {
function makeSVG(el) {
// manually create a copy of the element
var new_el = document.createElementNS(el.namespaceURI, el.nodeName);
$.each(el.attributes, function(i, attr) {
var ns = attr.namespaceURI;
if(ns) {
new_el.setAttributeNS(ns, attr.nodeName, attr.nodeValue);
} else {
new_el.setAttribute(attr.nodeName, attr.nodeValue);
}
if(attr.nodeName == 'transform') {
console.log('val1:', attr.nodeValue);
console.log('val2:', new_el.getAttribute('transform'));
}
});
// now create copies of all children
$.each(el.childNodes, function(i, child) {
switch(child.nodeType) {
case 1: // element node
new_el.appendChild(makeSVG(child));
break;
case 3: // text node
new_el.textContent = child.nodeValue;
break;
default:
break;
}
});
return new_el;
}
var svg_icons = {}, fixIDs; var svg_icons = {}, fixIDs;
$.svgIcons = function(file, opts) { $.svgIcons = function(file, opts) {
@ -177,18 +144,22 @@ $(function() {
var data_el = $('<object data="' + file + '" type=image/svg+xml>').appendTo('body').hide(); var data_el = $('<object data="' + file + '" type=image/svg+xml>').appendTo('body').hide();
try { try {
svgdoc = data_el[0].contentDocument; svgdoc = data_el[0].contentDocument;
// TODO: IE still loads this, shouldn't even bother.
data_el.load(getIcons); data_el.load(getIcons);
getIcons(0, true); // Opera will not run "load" event if file is already cached getIcons(0, true); // Opera will not run "load" event if file is already cached
} catch(err1) { } catch(err1) {
useFallback(); useFallback();
} }
} else { } else {
var parser = new DOMParser();
$.ajax({ $.ajax({
url: file, url: file,
dataType: 'xml', dataType: 'string',
success: function(data) { success: function(data) {
svgdoc = data; if(!data) {
$(useFallback);
return;
}
svgdoc = parser.parseFromString(data, "text/xml");
$(function() { $(function() {
getIcons('ajax'); getIcons('ajax');
}); });
@ -201,17 +172,9 @@ $(function() {
}); });
} else { } else {
if(err.responseXML) { if(err.responseXML) {
// Is there a non-ActiveX solution in IE9? svgdoc = parser.parseFromString(err.responseXML, "text/xml");
svgdoc = err.responseXML;
if(!svgdoc.childNodes.length) { if(!svgdoc.childNodes.length) {
if(window.ActiveXObject) {
svgdoc = new ActiveXObject("Microsoft.XMLDOM");
svgdoc.loadXML(err.responseText);
} else {
$(useFallback);
}
} else {
$(useFallback); $(useFallback);
} }
$(function() { $(function() {
@ -346,9 +309,7 @@ $(function() {
holder = $('#' + id); holder = $('#' + id);
if(elem.getElementsByTagNameNS) { if(elem.getElementsByTagNameNS) {
var svg = elem.getElementsByTagNameNS(svgns, 'svg')[0]; var svg = elem.getElementsByTagNameNS(svgns, 'svg')[0];
} else { }
var svg = elem.getElementsByTagName('svg')[0];
}
var svgroot = document.createElementNS(svgns, "svg"); var svgroot = document.createElementNS(svgns, "svg");
svgroot.setAttributeNS(svgns, 'viewBox', [0,0,icon_w,icon_h].join(' ')); svgroot.setAttributeNS(svgns, 'viewBox', [0,0,icon_w,icon_h].join(' '));
@ -375,13 +336,7 @@ $(function() {
// With cloning, causes issue in Opera/Win/Non-EN // With cloning, causes issue in Opera/Win/Non-EN
if(!isOpera) svg = svg.cloneNode(true); if(!isOpera) svg = svg.cloneNode(true);
// TODO: Figure out why makeSVG is necessary for IE9 svgroot.appendChild(svg);
try {
svgroot.appendChild(svg);
} catch(e) {
// For IE9
svgroot.appendChild(makeSVG(svg));
}
if(toImage) { if(toImage) {
// Without cloning, Safari will crash // Without cloning, Safari will crash
@ -402,12 +357,7 @@ $(function() {
$.each(opts.placement, function(sel, id) { $.each(opts.placement, function(sel, id) {
if(!svg_icons[id]) return; if(!svg_icons[id]) return;
$(sel).each(function(i) { $(sel).each(function(i) {
// TODO: Figure out why makeSVG is necessary for IE9 var copy = svg_icons[id].clone();
try {
var copy = svg_icons[id].clone();
} catch(e) {
var copy = makeSVG(svg_icons[id][0]);
}
setIcon($(this), copy, id); setIcon($(this), copy, id);
}) })
}); });
@ -500,7 +450,7 @@ $(function() {
$.getSvgIcon = function(id, uniqueClone) { $.getSvgIcon = function(id, uniqueClone) {
var icon = svg_icons[id]; var icon = svg_icons[id];
if(uniqueClone) { if(uniqueClone && icon) {
icon = fixIDs(icon, 0, true).clone(true); icon = fixIDs(icon, 0, true).clone(true);
} }
return icon; return icon;