-
-
Save AStoker/3a20dc24ca5c32c4b29fe6990dd30156 to your computer and use it in GitHub Desktop.
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
<template> | |
<require from="./inline-svg"></require> | |
<style> | |
.cool { | |
fill: red; | |
} | |
.worked { | |
fill: blue; | |
} | |
</style> | |
<div> | |
<button click.trigger="change()">test</button> | |
<inline-svg svg="leaf" class="${style}"></inline-svg> | |
<div></div> | |
</div> | |
</template> |
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
export class App { | |
style = "worked"; | |
change(){ | |
console.log('changing'); | |
if(this.style=="worked"){ | |
this.style="cool"; | |
} else { | |
this.style="worked"; | |
} | |
} | |
} | |
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
<!doctype html> | |
<html> | |
<head> | |
<title>Aurelia</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="https://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/bootstrap-datepicker3.min.css"> | |
</head> | |
<body aurelia-app> | |
<h1>Loading...</h1> | |
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script> | |
<script> | |
require(['aurelia-bootstrapper']); | |
</script> | |
</body> | |
</html> |
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
import {bindable, containerless, customElement, inject, inlineView, Loader} from 'aurelia-framework'; | |
@containerless() | |
@inlineView('<template></template>') | |
@customElement('inline-svg') | |
@inject(Element, Loader) | |
export class InlineSvg { | |
@bindable svg; | |
constructor(el, loader) { | |
this.el = el; | |
this.loader = loader; | |
this.classList; | |
this.targetInstruction; | |
this.view; | |
} | |
created(view){ | |
this.view = view; | |
this.targetInstruction = this.getTargetInstructionByType('class'); | |
} | |
getTargetInstructionByType(type){ | |
let targetInst; | |
for(let instKey in this.view.viewFactory.instructions){ | |
let inst = this.view.viewFactory.instructions[instKey]; | |
if (inst.expressions && inst.expressions.length > 0){ | |
let classExp = inst.expressions.some(exp => { | |
return exp.attribute == type | |
}); | |
if(classExp){ | |
targetInst = inst; | |
} | |
} | |
} | |
return targetInst; | |
} | |
bind(context){ | |
this.classList = this.el.class; //this.el is a comment | |
this.svgChanged(this.svg); | |
} | |
svgChanged(svg) { | |
if (svg) { | |
this.loader.loadText(`${svg}.svg`) | |
.then(text => { | |
let nextSibling = this.el.nextElementSibling; | |
let containerDiv = document.createElement("div"); | |
containerDiv.innerHTML = text; | |
let svgElem = containerDiv.children[0]; | |
let previousClassBindingIndex = this.view.bindings.findIndex(binding => { | |
return binding.target == nextSibling; | |
}); | |
if(previousClassBindingIndex > -1){ | |
this.view.bindings.splice(previousClassBindingIndex, 1); | |
} | |
if(this.targetInstruction){ | |
let svgBinding = this.targetInstruction.expressions[0].createBinding(svgElem); | |
this.view.addBinding(svgBinding); | |
} else { | |
svgElem.setAttribute('class', this.classList); | |
} | |
if(nextSibling && nextSibling.tagName.toLowerCase() == 'svg'){ | |
nextSibling.parentElement.replaceChild(svgElem, nextSibling); | |
} else { | |
this.el.parentElement.insertBefore(svgElem, this.el.nextSibling); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment