Skip to content

Commit

Permalink
New built
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyschudel committed May 9, 2013
1 parent 85b7e1b commit 5b92c5a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
84 changes: 51 additions & 33 deletions dist/jsvg.js
Expand Up @@ -17,25 +17,27 @@ if (!('$sa' in window)) {
_this = this,

options = {
cache: true
cache : true,
appendToBody : true
},
assets = {};

this.VERSION = '0.5.7';

this.init = function() {
$.extend(true, options, options_);
};

this.load = function(opt_) {
var
opt = $.extend({
url: '',
name: null,
cache: options.cache
cache : options.cache,
appendToBody : options.appendToBody
}, opt_),
name = opt.name;

if (!name) {
name = opt['name'] = opt.url.replace(/\.svgz?$/, '');
name = opt['name'] = opt.url.match(/([^\/]*)\.svgz?$/)[1];
}

var asset = assets[name] = new $sa.SVGAsset(opt);
Expand All @@ -45,12 +47,10 @@ if (!('$sa' in window)) {

this.getAsset = function(name) {
if (name instanceof $sa.SVGAsset) {

return name;
}

if (!(name in assets)) {

throw new Error("Could not find asset: "+name);
}

Expand All @@ -61,6 +61,10 @@ if (!('$sa' in window)) {
return this.getAsset(asset).get(id, cls);
};

this.getAssets = function() {
return assets;
};

this.getOption = function(key) {
return options[key];
};
Expand All @@ -71,26 +75,35 @@ if (!('$sa' in window)) {
})(window.$sa, window.jQuery);
(function($sa, $) {

var
RE_TRIM = /^[\s]*|[\s]*$/g,

_trim = function(str) {
return str.replace(RE_TRIM, '');
};

$sa.SVGAsset = function(options_) {
var
_this = this,

options = {
url: '',
index: '',
cache: true,
className: null
url : '',
index : '',
cache : true,
className : null,
appendToBody : true
},
core = {
data: null,
ids: []
data : null,
ids : []
},
cache = {
$data: null,
$svg: null,
sprites: {}
$svg : null,
sprites : {}
};

this.VERSION = '0.5.7';

this.init = function() {
$.extend(true, options, options_);

Expand All @@ -104,24 +117,27 @@ if (!('$sa' in window)) {

var
xhr = this.xhr.load = $.ajax({
url: options.url,
cache: options.cache,
dataType: 'text',
context: {
asset: _this
url : options.url,
cache : options.cache,
dataType : 'text',
context : {
asset : _this
}
})
.done(function(data) {
core.data = $.parseXML(data);
cache.$data = $(core.data);
core.ids = [];
.done(function(data_) {
var data = core.data = $.parseXML(data_),
$svg = cache.$svg = $(data).find('> svg'),
ids = core.ids = [];

cache.$data.find('[id]').each(function(index, item) {
core.ids.push(item.id);
$svg.find('[id]').each(function(index, item) {
ids.push(item.id);
});

if (options.appendToBody) {
$('body').append($svg);
}
})
.fail(function(data) {

throw new Error("Could not load svg file: "+options.url);
});

Expand All @@ -132,21 +148,19 @@ if (!('$sa' in window)) {
if (id && id[0] === '#') { id = id.slice(1); }

var
className = [id, options.className, cls].join(' '),
className = _trim([id, options.className, cls].join(' ')),
$sprite = cache.sprites[id],
$svg, $item, $body;

// sprite is cached
if ($sprite) {

return $sprite.clone();
}

// find element
$item = cache.$data.find('#'+id);
$item = cache.$svg.find('#'+id);
if (!$item[0]) {

throw new Error("Could not find element #"+id);
throw new Error("Could not find svg element #"+id);
}

// is svg element
Expand Down Expand Up @@ -177,6 +191,10 @@ if (!('$sa' in window)) {
return $sprite;
};

this.getIds = function() {
return core.ids;
};

this.getOption = function(key) {
return options[key];
};
Expand Down
2 changes: 1 addition & 1 deletion dist/jsvg.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b92c5a

Please sign in to comment.