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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment