Skip to content

Instantly share code, notes, and snippets.

@AStoker
Forked from fabioluz/app.html
Last active October 31, 2016 17:43
Show Gist options
  • Save AStoker/3a20dc24ca5c32c4b29fe6990dd30156 to your computer and use it in GitHub Desktop.
Save AStoker/3a20dc24ca5c32c4b29fe6990dd30156 to your computer and use it in GitHub Desktop.
<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>
export class App {
style = "worked";
change(){
console.log('changing');
if(this.style=="worked"){
this.style="cool";
} else {
this.style="worked";
}
}
}
<!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>
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);
}
});
}
}
}
Display the source blob
Display the rendered blob
Raw
<svg version="1.1" id="Layer_3" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 48 44.8" style="enable-background:new 0 0 48 44.8;" xml:space="preserve">
<path d="M43.4,7C33.1,26.9,6,38.7,0.1,41c0,0-2.6-20,21.4-28.3c0,0,19-5.7,26.3-12.7c0,0,3.6,24.6-18.5,40.2 c0,0-11.5,8-27.1,3C47.5,22.2,43.4,7,43.4,7z"></path>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment