-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating to use Mutation Observer to watch for elements that are load…
…ed dynamically to the page.
- Loading branch information
1 parent
36f8402
commit 3643dcf
Showing
3 changed files
with
53 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"name": "inline-svg", | ||
"main": "inline-svg.js", | ||
"version": "0.1.3", | ||
"homepage": "https://github.com/zaneray/inline-svg", | ||
"authors": [ | ||
"Jeremiah Martin <[email protected]>" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,64 @@ | ||
/* | ||
$.inlineSVG(); | ||
Currently just using this to inject them into the style guide. Pretty experimental at this point. JJM | ||
*/ | ||
//temp one that inlines img srcs | ||
(function ($) { | ||
$.fn.inlineSVG = function (args) { | ||
var options = { | ||
|
||
}; | ||
|
||
$.extend( options, args ); | ||
this.not('.loaded').each(function(){ | ||
var $this = $(this); | ||
if($this) { | ||
var svgUrl = $this.attr('src'), | ||
$parent = $this.parent(), | ||
classNames = $this.attr('class'); | ||
var ajax = new XMLHttpRequest(); | ||
ajax.open("GET", svgUrl, true); | ||
ajax.send(); | ||
ajax.onload = function(e) { | ||
|
||
//if the status is not 404 do the replacement otherwise do nothing. | ||
|
||
//function to inline SVGs as part of the jquery plugin or part of the MutationObserver event. | ||
var makeSVGInline = function($el){ | ||
var svgUrl = $el.attr('src'), | ||
$parent = $el.parent(), | ||
classNames = $el.attr('class'); | ||
|
||
//this works better than $.ajax(); | ||
var ajax = new XMLHttpRequest(); | ||
ajax.open("GET", svgUrl, true); | ||
ajax.send(); | ||
|
||
ajax.onload = function(e) { | ||
//if the status is not 404 do the replacement otherwise do nothing. | ||
if(ajax.status !== 404) { | ||
|
||
var $svg = $(ajax.responseText); | ||
|
||
$svg.attr('class', classNames + ' loaded'); | ||
$this.replaceWith($svg); | ||
$el.replaceWith($svg); | ||
|
||
} | ||
else { | ||
$this.addClass('not-loaded'); | ||
$el.addClass('not-loaded'); | ||
} | ||
|
||
} | ||
} | ||
}; | ||
|
||
//loop on inline SVGs loaded with the plugin | ||
this.each(function(){ | ||
//if($this) { | ||
makeSVGInline($(this)); | ||
//} | ||
}); | ||
|
||
//observe the DOM for mutations, if anything changes on the page scan it for new img.inline-svg | ||
var observer = new MutationObserver(function(mutations) { | ||
mutations.forEach(function(mutation) { | ||
|
||
if (mutation.type === 'childList' && mutation.addedNodes.length) { | ||
var $inlineSVGs = $("img.inline-svg").not('.loaded'); | ||
|
||
$inlineSVGs.each(function(){ | ||
makeSVGInline($(this)); | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
observer.observe(document.body, { | ||
attributes: true, | ||
childList: true, | ||
subtree: true | ||
}); | ||
}; | ||
}( jQuery )); | ||
|