Skip to content

Commit

Permalink
🐛 Fix uglified animationScript for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Apr 8, 2015
1 parent a8415c6 commit 0265a98
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v0.1.10 / Apr 8 2015
v0.1.11 / Apr 8 2015
=========================
* [`unknown`][6] :bug: Hotfix [#3][6A]

Expand Down
32 changes: 17 additions & 15 deletions lib/jaggy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ Jaggy.createSVG= (url,args...)->

Jaggy.getCache= (url)->
localStorage.getItem 'jaggy:'+url
Jaggy.setCache= (url,element)->
cache= element.outerHTML
Jaggy.setCache= (url,element,options={})->
cacheElement= element.cloneNode true
if options.cacheScript is 'base64'
script= cacheElement.querySelector 'script'
script.innerHTML= btoa script.innerHTML if script

cache= cacheElement.outerHTML
if not cache?
div= document.createElement 'div'
div.appendChild element
div.appendChild cacheElement
cache= div.innerHTML

try
Expand All @@ -109,6 +114,11 @@ Jaggy.angularModule= (window)->
useCache: 'localStorage'
useEmptyImage: yes
}
angularModule.config (jaggyConfig)->
key= 'jaggy:version'
localStorage.clear() if localStorage.getItem(key) isnt '0.1.11'
localStorage.setItem key,'0.1.11'

angularModule.constant 'jaggyEmptyImage','<svg viewBox="0 0 1 1" shape-rendering="crispEdges" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M0,0h1v1h-1Z" fill="rgba(0,0,0,0.50)"></path></svg>'
angularModule.directive 'jaggy',(jaggyConfig,jaggyEmptyImage)->
(scope,element,attrs)->
Expand All @@ -120,6 +130,7 @@ Jaggy.angularModule= (window)->
[key,value]= param.split ':'
options[key]= value
options.cache= !! jaggyConfig.useCache
options.cacheScript?= 'base64'

# fix <img ng-src="url" jaggy>
url= attrs.src
Expand All @@ -135,9 +146,8 @@ Jaggy.angularModule= (window)->
if svg.match and svg.match '<script>'
svgContainer= document.createElement 'div'
svgContainer.innerHTML= svg
script= svgContainer.querySelector('script')?.innerHTML
script= script.replace /&gt;/g,'>'
script= script.replace /&lt;/g,'<'
script= svgContainer.querySelector('script')?.innerHTML or ''
script= atob script if options.cacheScript is 'base64'
eval script

Jaggy.convertToSVG= (pixels,args...)->
Expand All @@ -158,18 +168,10 @@ Jaggy.convertToSVG= (pixels,args...)->
svg= svg.outerHTML.replace ' viewbox=',' viewBox='# fix to lowerCamel
svg= svg.replace(/&gt;/g,'>')# enable querySelector

Jaggy.setCache options.cacheUrl,svg if options.cacheUrl?
Jaggy.setCache options.cacheUrl,svg,options if options.cacheUrl?

callback null,svg

Jaggy.enableAnimation= (svg)->
throw new Error('Can enable appended element only') if svg.parentNode is null

# fix disabled script
script= svg.querySelector 'script'
script.parentNode.replaceChild script.cloneNode(),script if script
svg

# Use Buffer for node.js
Jaggy.readImageData= (file,callback)->
return callback 'file is not object' if typeof file isnt 'object'
Expand Down
53 changes: 33 additions & 20 deletions public/jaggy.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,22 @@ Jaggy.getCache = function(url) {
return localStorage.getItem('jaggy:' + url);
};

Jaggy.setCache = function(url, element) {
var cache, div, error;
cache = element.outerHTML;
Jaggy.setCache = function(url, element, options) {
var cache, cacheElement, div, error, script;
if (options == null) {
options = {};
}
cacheElement = element.cloneNode(true);
if (options.cacheScript === 'base64') {
script = cacheElement.querySelector('script');
if (script) {
script.innerHTML = btoa(script.innerHTML);
}
}
cache = cacheElement.outerHTML;
if (cache == null) {
div = document.createElement('div');
div.appendChild(element);
div.appendChild(cacheElement);
cache = div.innerHTML;
}
try {
Expand All @@ -476,6 +486,14 @@ Jaggy.angularModule = function(window) {
useCache: 'localStorage',
useEmptyImage: true
});
angularModule.config(["jaggyConfig", function(jaggyConfig) {
var key;
key = 'jaggy:version';
if (localStorage.getItem(key) !== '0.1.11') {
localStorage.clear();
}
return localStorage.setItem(key, '0.1.11');
}]);
angularModule.constant('jaggyEmptyImage', '<svg viewBox="0 0 1 1" shape-rendering="crispEdges" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M0,0h1v1h-1Z" fill="rgba(0,0,0,0.50)"></path></svg>');
return angularModule.directive('jaggy', ["jaggyConfig", "jaggyEmptyImage", function(jaggyConfig, jaggyEmptyImage) {
return function(scope, element, attrs) {
Expand All @@ -491,6 +509,9 @@ Jaggy.angularModule = function(window) {
}
}
options.cache = !!jaggyConfig.useCache;
if (options.cacheScript == null) {
options.cacheScript = 'base64';
}
url = attrs.src;
if (url == null) {
url = attrs.ngSrc;
Expand All @@ -507,9 +528,10 @@ Jaggy.angularModule = function(window) {
if (svg.match && svg.match('<script>')) {
svgContainer = document.createElement('div');
svgContainer.innerHTML = svg;
script = (ref2 = svgContainer.querySelector('script')) != null ? ref2.innerHTML : void 0;
script = script.replace(/&gt;/g, '>');
script = script.replace(/&lt;/g, '<');
script = ((ref2 = svgContainer.querySelector('script')) != null ? ref2.innerHTML : void 0) || '';
if (options.cacheScript === 'base64') {
script = atob(script);
}
return eval(script);
}
});
Expand Down Expand Up @@ -546,23 +568,11 @@ Jaggy.convertToSVG = function() {
svg = svg.replace(/&gt;/g, '>');
}
if (options.cacheUrl != null) {
Jaggy.setCache(options.cacheUrl, svg);
Jaggy.setCache(options.cacheUrl, svg, options);
}
return callback(null, svg);
};

Jaggy.enableAnimation = function(svg) {
var script;
if (svg.parentNode === null) {
throw new Error('Can enable appended element only');
}
script = svg.querySelector('script');
if (script) {
script.parentNode.replaceChild(script.cloneNode(), script);
}
return svg;
};

Jaggy.readImageData = function(file, callback) {
var buffer, getPixels, mime, mimeType;
if (typeof file !== 'object') {
Expand Down Expand Up @@ -28028,6 +28038,8 @@ module.exports={
"watch": "abigail ./**/*.coffee:build --ignore --execute",
"convert": "jaggy public -o hogekosan -g 2",

"update-bower": "git tag v$(jqn bower version) && git push --tags",

"test": "jasminetea test --verbose --cover --report",
"posttest": "rm public/*.svg",

Expand All @@ -28049,6 +28061,7 @@ module.exports={
"coffeeify": "^1.0.0",
"gulp": "^3.8.11",
"jasminetea": "^0.1.27",
"jqn": "0.0.3",
"ng-annotate": "^0.15.4",
"onefile": "^0.2.8",
"uglify-js": "^2.4.19"
Expand Down
12 changes: 7 additions & 5 deletions public/jaggy.browser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/jaggy.browser.min.js.map

Large diffs are not rendered by default.

0 comments on commit 0265a98

Please sign in to comment.