Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/title and desc #23

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions addon/components/async-svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Ember from 'ember';
import Aria from '../mixins/aria';
import Common from '../mixins/common';
const PREFIX = '';

const async = Ember.Component.extend(Aria, Common, {
_svg: Ember.observer('src', function() {
const src = this.get('src').replace(/\.svg$/, '');
Ember.$.ajax({
method: 'GET',
url: `${PREFIX}/${src}.svg`
}).done(data => {
this.setSVG(data);
});
return true;
})
});

async.reopenClass({
positionalParams: ['src']
});
async[Ember.NAME_KEY] = 'async-svg';
export default async;
22 changes: 22 additions & 0 deletions addon/components/inline-svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Ember from 'ember';
import Aria from '../mixins/aria';
import Common from '../mixins/common';
import SVGs from 'svgs';

const inline = Ember.Component.extend(Aria, Common, {
_svg: Ember.computed('src', function() {
const src = this.get('src') || '';
const path = src.replace(/\.svg$/, '').replace(/\//g, '.');
const svg = Ember.get(SVGs, path);

Ember.assert(`No SVG found for ${path}`, svg);

this.setSVG(svg);
})
});

inline.reopenClass({
positionalParams: ['src']
});
inline[Ember.NAME_KEY] = 'inline-svg';
export default inline;
10 changes: 10 additions & 0 deletions addon/mixins/aria.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Ember from 'ember';

export default Ember.Mixin.create({
labelledBy: Ember.computed('title', 'desc', function() {
const title = this.get('title') ? 'title' : '';
const desc = this.get('desc') ? ' desc' : '';
return `${title}${desc}`;
}),
role: 'img'
});
60 changes: 60 additions & 0 deletions addon/mixins/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Ember from 'ember';
import layout from '../templates/components/svg';
const htmlSafe = Ember.String.htmlSafe;

export default Ember.Mixin.create({
layout: layout,
tagName: '',

/**
* Initialize SVG on load
*/
init() {
this._super(...arguments);
Ember.run.schedule('afterRender', () => {
this.notifyPropertyChange('src');
});
},
preserveAspectRatio: null,
setSVG(data) {
const {width, height, x, y} = this.getProperties('width', 'height', 'x', 'y');
this.set('svg', htmlSafe(Ember.$(data).find('svg').html()));

/**
* viewBox
*
* Not required but often included;
*/
const viewBox = Ember.$(data).find('svg').attr('viewBox');
if (Ember.typeOf(this.get('viewBox')) === 'undefined' && viewBox) { this.set('viewBox', viewBox); }

/**
* viewport (width, height)
*
* The visible portion of the SVG. Firefox may object if not defined but isn't
* strictly required and you run the risk of cutoff.
*
* By default if not set on component or in the SVG then a width of 100% will be
* used; this is typically the desired behaviour.
*/
const viewWidth = Ember.$(data).find('svg').attr('width');
const viewHeight = Ember.$(data).find('svg').attr('height');
if (!width) { this.set('width', viewWidth || '100%'); }
if (!height && !width && viewHeight) { this.set('height', viewHeight); }

/**
* Aspect Ratio (preserveAspectRatio)
*
* SVG has a boolean flag to ensure that viewBox and viewport are the same aspect
* ratio. If its stated in the file we'll proxy it through but default its not set.
*/
const preserveAspectRatio = Ember.$(data).find('svg').attr('preserveAspectRatio');
if (preserveAspectRatio) { this.set('preserveAspectRatio', preserveAspectRatio); }

const defaultX = Ember.$(data).find('svg').attr('x');
const defaultY = Ember.$(data).find('svg').attr('y');
if (!x && defaultX) { this.set('x', Ember.$(data).find('svg').attr('x')); }
if (!y && defaultY) { this.set('y', Ember.$(data).find('svg').attr('y')); }
}

});
20 changes: 20 additions & 0 deletions addon/templates/components/svg.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<svg
class={{class}}
aria-labelledby="{{labelledBy}}"
role="{{role}}"
viewBox={{viewBox}}
preserveAspectRatio={{preserveAspectRatio}}
width={{width}}
height={{height}}
x={{x}}
y={{y}}
>
{{#if title}}
<title class="title">{{title}}</title>
{{/if}}
{{#if desc}}
<desc class="desc">{{desc}}</desc>
{{/if}}
{{svg}}
{{yield}}
</svg>
14 changes: 0 additions & 14 deletions addon/utils/general.js

This file was deleted.

1 change: 1 addition & 0 deletions app/components/async-svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-inline-svg/components/async-svg';
1 change: 1 addition & 0 deletions app/components/inline-svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-inline-svg/components/inline-svg';
File renamed without changes.
47 changes: 37 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

var fs = require('fs');
var path = require('path');
var merge = require('merge');
var mergeTrees = require('broccoli-merge-trees');
var flatiron = require('broccoli-flatiron');
Expand All @@ -10,14 +11,14 @@ var SVGOptmizer = require('./svg-optimizer');

module.exports = {
name: 'ember-inline-svg',

included: function(app) {
if (app.app) {
app = app.app;
}
this.app = app;
},

options: function() {
return merge(true, {}, {
paths: ['public'],
Expand All @@ -41,22 +42,48 @@ module.exports = {
return new SVGOptmizer(tree, {svgoConfig: config});
},

treeForApp: function(tree) {
var svgs = mergeTrees(this.svgPaths().filter(function(path) {
return fs.existsSync(path);
buildSvgTree: function() {
let svgs = mergeTrees(this.svgPaths().filter(function(path) {
return fs.statSync(path);
}));

svgs = new Funnel(svgs, {
include: [new RegExp(/\.svg$/)]
});

svgs = this.optimizeSVGs(svgs);
return this.optimizeSVGs(svgs);
},

svgs = flatiron(svgs, {
treeForPublic: function(tree) {
const trees = [];
const svgs = this.buildSvgTree();

if(tree) {
trees.push(tree);
}
trees.push(flatiron(svgs, {
outputFile: 'svgs.js',
trimExtensions: true
});
}));

return mergeTrees(trees);
},

// treeForVendor: function(tree) {
// const trees = [];
// const svgs = this.buildSvgTree();
// console.log('treeForVendor, cwd: ', path.dirname());
//
// if(tree) {
// trees.push(tree);
// }
// // trees.push(svgs);
// trees.push(flatiron(svgs, {
// outputFile: 'svgs.js',
// trimExtensions: true
// }));
//
// return mergeTrees(trees);
// },

return mergeTrees([tree, svgs]);
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@
"ember-cli-app-version": "^1.0.0",
"ember-cli-content-security-policy": "0.4.0",
"ember-cli-dependency-checker": "^1.1.0",
"ember-cli-htmlbars": "^1.0.1",
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-ic-ajax": "0.2.4",
"ember-cli-inject-live-reload": "^1.3.1",
"ember-cli-qunit": "^1.0.4",
"ember-cli-release": "0.2.8",
"ember-cli-sri": "^1.2.0",
"ember-cli-uglify": "^1.2.0",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-disable-proxy-controllers": "^1.0.1",
"ember-export-application-global": "^1.0.4",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-try": "0.0.8"
},
"keywords": [
"ember-addon",
"svg"
],
"dependencies": {
"ember-cli-htmlbars": "^1.0.1",
"broccoli-caching-writer": "^0.5.5",
"broccoli-flatiron": "0.0.0",
"broccoli-funnel": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Dummy</title>
<title>Inline SVG</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

Expand Down
15 changes: 14 additions & 1 deletion tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<h2 id='title'>ember-inline-svg</h2>

{{inline-svg 'kiwi'}}
<h3>Compile Time: </h3>
{{inline-svg 'kiwi'
title='kiwi'
desc='demonstration of compile-time inlining'
width='400px'
}}

<h3>Run Time:</h3>

{{async-svg 'kiwi'
title='kiwi'
desc='demonstration of run-time inlining'
width='400px'
}}

{{outlet}}
25 changes: 25 additions & 0 deletions tests/integration/components/async-svg-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('async-svg', 'Integration | Component | async svg', {
integration: true
});

test('it renders', function(assert) {

// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });" + EOL + EOL +

this.render(hbs`{{async-svg}}`);

assert.equal(this.$().text().trim(), '');

// Template block usage:" + EOL +
this.render(hbs`
{{#async-svg}}
template block text
{{/async-svg}}
`);

assert.equal(this.$().text().trim(), 'template block text');
});
25 changes: 25 additions & 0 deletions tests/integration/components/inline-svg-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('inline-svg', 'Integration | Component | inline svg', {
integration: true
});

test('it renders', function(assert) {

// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });" + EOL + EOL +

this.render(hbs`{{inline-svg}}`);

assert.equal(this.$().text().trim(), '');

// Template block usage:" + EOL +
this.render(hbs`
{{#inline-svg}}
template block text
{{/inline-svg}}
`);

assert.equal(this.$().text().trim(), 'template block text');
});
12 changes: 12 additions & 0 deletions tests/unit/mixins/aria-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from 'ember';
import AriaMixin from '../../../mixins/aria';
import { module, test } from 'qunit';

module('Unit | Mixin | aria');

// Replace this with your real tests.
test('it works', function(assert) {
let AriaObject = Ember.Object.extend(AriaMixin);
let subject = AriaObject.create();
assert.ok(subject);
});
12 changes: 12 additions & 0 deletions tests/unit/mixins/common-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from 'ember';
import CommonMixin from '../../../mixins/common';
import { module, test } from 'qunit';

module('Unit | Mixin | common');

// Replace this with your real tests.
test('it works', function(assert) {
let CommonObject = Ember.Object.extend(CommonMixin);
let subject = CommonObject.create();
assert.ok(subject);
});