Skip to content

Commit

Permalink
Remove jQuery dependency (#51)
Browse files Browse the repository at this point in the history
* Remove jQuery dependency

* Upgrades

* Refactor
  • Loading branch information
Exelord authored Feb 4, 2018
1 parent c802182 commit 8c5ec3b
Show file tree
Hide file tree
Showing 8 changed files with 2,854 additions and 1,981 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports = {
],
excludedFiles: [
'app/**',
'addon/**'
'addon/**',
'tests/dummy/app/**'
],
parserOptions: {
sourceType: 'script',
Expand Down
4 changes: 2 additions & 2 deletions addon/mixins/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default Mixin.create(Avatar, {

onError: computed('image', function() {
if (this.get('image')) {
return this._checkImage.bind(this);
return this._assignInitialsSrc.bind(this);
}
}),

Expand Down Expand Up @@ -103,7 +103,7 @@ export default Mixin.create(Avatar, {
};
},

_checkImage(e) {
_assignInitialsSrc(e) {
e.srcElement.src = this.createInitials();
},

Expand Down
30 changes: 17 additions & 13 deletions addon/utils/generators/svg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Ember$ from 'jquery';
import Base from './base';

export default class extends Base {
Expand All @@ -8,38 +7,43 @@ export default class extends Base {

generate(properties) {
let textElement = this._generateTextElement(properties.initials, properties.initialsColor, properties.textStyles);
let svgElement = this._generateSvgElement(properties.width, properties.height, properties.backgroundStyles);
let svgElement = this._generateSvgElement(textElement, properties.width, properties.height, properties.backgroundStyles);
let blob = new Blob([svgElement], { type: "image/svg+xml" });

svgElement.append(textElement);
let finalElement = Ember$('<div>').append(svgElement);
let blob = new Blob([finalElement.html()], { type: "image/svg+xml" });
return URL.createObjectURL(blob);
}

// Private

_generateTextElement(text, color, styles = {}) {
return this._generateElement('<text></text>', styles, {
return this._generateElement('text', text, styles, {
y: '50%',
x: '50%',
dy: '0.35em',
'text-anchor': 'middle',
'pointer-events': 'auto',
fill: color,
}).html(text);
}

_generateElement(name, styles = {}, attrs = {}) {
return Ember$(name).attr(attrs).css(styles);
});
}

_generateSvgElement(width, height, styles = {}) {
return this._generateElement('<svg></svg>', styles, {
_generateSvgElement(text, width, height, styles = {}) {
return this._generateElement('svg', text, styles, {
width,
height,
xmlns: 'http://www.w3.org/2000/svg',
'pointer-events': 'none',
'viewBox': '0 0 100 100'
});
}

_generateElement(name, content = '', styles = {}, attrs = {}) {
let attrsString = this._transformObject(attrs, (key) => `${key}="${attrs[key]}"`)
let stylesString = this._transformObject(styles, (key) => `${key}: ${styles[key]};`)

return `<${name} ${attrsString} style="${stylesString}">${content}</${name}>`
}

_transformObject(object, transform = function() {}) {
return Object.keys(object).map((key) => transform(key)).join(' ');
}
}
5 changes: 0 additions & 5 deletions addon/utils/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export default EmberObject.extend({
this.set('cache', {});
},

removeAll() {
Object.keys(this.get(`cache`)).forEach((key) => this.get('generator').revoke(key))
this.set('cache', {});
},

initialsFor(properties) {
let key = hash(properties);
return this.get('cache')[key] || this._create(key, properties);
Expand Down
Loading

0 comments on commit 8c5ec3b

Please sign in to comment.