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 refactor #41

Merged
merged 3 commits into from
Dec 26, 2017
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ Ember Initials gives you a built-in components which you can use in yours templa
```hbs
{{ember-initials name="John Doe"}}
```
or

```hbs
{{ember-initials/initials name="John Doe"}}
```

##### Image avatars:
```hbs
Expand Down
9 changes: 0 additions & 9 deletions addon/config.js

This file was deleted.

10 changes: 10 additions & 0 deletions addon/initializers/ember-initials-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Store from 'ember-initials/utils/store';

export function initialize(application) {
application.register('store:ember-initials', Store);
}

export default {
initialize,
name: 'ember-initials-store',
};
26 changes: 9 additions & 17 deletions addon/mixins/adorable.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { computed } from '@ember/object';
import { reads, or } from '@ember/object/computed';
import Mixin from '@ember/object/mixin';
import Avatar from 'ember-initials/mixins/avatar';
import { computed } from '@ember/object';

export default Mixin.create({
tagName: 'img',
attributeBindings: ['width', 'height', 'src', 'title', 'alt'],

export default Mixin.create(Avatar, {
email: '',
image: '',

title: 'User Avatar',
alt: 'User Avatar',

size: 30,
height: reads('size'),
width: reads('size'),

src: or('image', 'adorable'),
src: computed('image', 'email', 'size', function() {
return this.get('image') || this._adorableSrc(this.get('email'), this.get('size'));
}),

adorable: computed('email', 'size', function() {
return `https://api.adorable.io/avatars/${this.get('size')}/${this.get('email')}`;
})
_adorableSrc(email, size) {
return `https://api.adorable.io/avatars/${size}/${email}`;
}
});
31 changes: 31 additions & 0 deletions addon/mixins/avatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Mixin from '@ember/object/mixin';
import { reads } from '@ember/object/computed';
import { getOwner } from '@ember/application';
import { computed } from '@ember/object';

export default Mixin.create({
tagName: 'img',
attributeBindings: ['width', 'height', 'src', 'alt', 'title', 'onError'],

src: '',
size: 30,
alt: 'User Avatar',
title: 'User Avatar',

height: reads('size'),
width: reads('size'),

config: computed(function() {
return getOwner(this).resolveRegistration('config:environment').emberInitials;
}),

fastboot: computed(function() {
return getOwner(this).lookup('service:fastboot');
}),

avatarsStore: computed(function() {
return getOwner(this).lookup('store:ember-initials');
}),

onError() {}
});
34 changes: 10 additions & 24 deletions addon/mixins/gravatar.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
import { assign } from '@ember/polyfills';
import { getOwner } from '@ember/application';
import { reads } from '@ember/object/computed';
import { computed } from '@ember/object';
import Mixin from '@ember/object/mixin';
import Config from 'ember-initials/config';
import Avatar from 'ember-initials/mixins/avatar';
import { computed } from '@ember/object';
import md5 from 'md5';

export default Mixin.create({
tagName: 'img',
attributeBindings: ['width', 'height', 'src'],

export default Mixin.create(Avatar, {
email: null,

image: null,
relativeUrl: false,

defaultImage: computed(function() {
return this.get('config.gravatar.defaultImage');
}),

size: 30,
height: reads('size'),
width: reads('size'),

src: computed('email', 'size', 'image', 'defaultImage', function() {
return this.get('image') ? this.get('image') : this.generateGravatarUrl();
}),

config: computed(function() {
let appSettings = getOwner(this).resolveRegistration('config:environment').emberInitials || {};
return assign({}, Config, appSettings);
}),

generateGravatarUrl() {
let hash = md5(this.get('email'));
let size = this.get('size');
Expand All @@ -41,10 +26,11 @@ export default Mixin.create({
},

defaultImageUrl() {
if (this.get('relativeUrl') && this.get('defaultImage')) {
return `${window.location.origin}/${this.get('defaultImage')}`;
} else {
return this.get('defaultImage');
}
let defaultImage = this.get('defaultImage');
return this.get('relativeUrl') && defaultImage ? this._absoluteImageSrc(defaultImage) : defaultImage;
},

_absoluteImageSrc(relativePath) {
return `${window.location.origin}/${relativePath}`;
}
});
27 changes: 5 additions & 22 deletions addon/mixins/image.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { assign } from '@ember/polyfills';
import { getOwner } from '@ember/application';
import { computed } from '@ember/object';
import { reads, or } from '@ember/object/computed';
import Mixin from '@ember/object/mixin';
import Config from 'ember-initials/config';

export default Mixin.create({
tagName: 'img',
attributeBindings: ['width', 'height', 'src', 'title', 'alt'],

image: null,
defaultImage: reads('config.image.defaultImageUrl'),
import Avatar from 'ember-initials/mixins/avatar';
import { reads, or } from '@ember/object/computed';

title: 'User Avatar',
alt: 'User Avatar',
size: 30,
height: reads('size'),
width: reads('size'),
export default Mixin.create(Avatar, {
image: '',

src: or('image', 'defaultImage'),

config: computed(function() {
let appSettings = getOwner(this).resolveRegistration('config:environment').emberInitials || {};
return assign({}, Config, appSettings);
})
defaultImage: reads('config.image.defaultImageUrl'),
});
86 changes: 39 additions & 47 deletions addon/mixins/initials.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,34 @@
import Mixin from '@ember/object/mixin';
import Avatar from 'ember-initials/mixins/avatar';
import { assign } from '@ember/polyfills';
import { getOwner } from '@ember/application';
import { observer, computed } from '@ember/object';
import { reads } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import Mixin from '@ember/object/mixin';
import ColorIndex from '../utils/color-index';
import Initials from '../utils/initials';
import ColorIndex from 'ember-initials/utils/color-index';
import Initials from 'ember-initials/utils/initials';

export default Mixin.create({
tagName: 'img',
attributeBindings: ['width', 'height', 'src', 'onError'],

initialsStore: service('ember-initials-store'),
export default Mixin.create(Avatar, {
image: null,
colors: null,
textStyles: null,
backgroundStyles: null,

defaultName: '?',
defaultBackground: '#dd6a58',

image: null,
name: reads('defaultName'),
seedText: reads('name'),

size: 30,
height: reads('size'),
width: reads('size'),

backgroundStyles: {},
textStyles: {},

textColor: 'white',
fontSize: 50,
fontWeight: 200,
textColor: 'white',
fontFamily: 'Helvetica Neue Light, Arial, sans-serif',

colors: [
'#1abc9c', '#16a085', '#f1c40f',
'#f39c12', '#2ecc71', '#27ae60',
'#e67e22', '#d35400', '#3498db',
'#2980b9', '#e74c3c', '#c0392b',
'#9b59b6', '#8e44ad', '#bdc3c7',
'#34495e', '#2c3e50', '#95a5a6',
'#7f8c8d', '#ec87bf', '#d870ad',
'#f69785', '#9ba37e', '#b49255',
'#b49255', '#a94136', '#5461b4',
],
name: reads('defaultName'),
seedText: reads('name'),

src: computed('fastboot.isFastBoot', 'image', function() {
let image = this.get('image');

if (image) return image;
return this.get('fastboot.isFastBoot') ? '' : this.createInitials();
}),

initialsObserver: observer('name', 'seedText', 'fontSize', 'fontWeight', 'fontFamily', 'textColor', 'defaultName', function () {
this.notifyPropertyChange('src');
Expand All @@ -57,19 +43,25 @@ export default Mixin.create({
}
}),

src: computed('fastboot.isFastBoot', 'image', function() {
let image = this.get('image');

if (!this.get('fastboot.isFastBoot')) {
return image ? image : this.createInitials();
} else {
return image ? image : '';
}
}),

fastboot: computed(function() {
return getOwner(this).lookup('service:fastboot');
}),
init() {
this._super(...arguments);

this.setProperties({
backgroundStyles: {},
textStyles: {},
colors: [
'#1abc9c', '#16a085', '#f1c40f',
'#f39c12', '#2ecc71', '#27ae60',
'#e67e22', '#d35400', '#3498db',
'#2980b9', '#e74c3c', '#c0392b',
'#9b59b6', '#8e44ad', '#bdc3c7',
'#34495e', '#2c3e50', '#95a5a6',
'#7f8c8d', '#ec87bf', '#d870ad',
'#f69785', '#9ba37e', '#b49255',
'#b49255', '#a94136', '#5461b4',
]
})
},

onError: computed('image', function() {
if (this.get('image')) {
Expand All @@ -78,7 +70,7 @@ export default Mixin.create({
}),

createInitials() {
return this.get('initialsStore').initialsFor(this.initialsProperties());
return this.get('avatarsStore').initialsFor(this.initialsProperties());
},

initialsProperties() {
Expand Down
31 changes: 0 additions & 31 deletions addon/services/ember-initials-store.js

This file was deleted.

31 changes: 31 additions & 0 deletions addon/utils/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { computed } from '@ember/object';
import EmberObject from "@ember/object";
import hash from 'object-hash';
import SvgGenerator from '../utils/generators/svg';

export default EmberObject.extend({
cache: null,

generator: computed(function() {
return new SvgGenerator;
}),

init() {
this._super(...arguments);
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);
},

_create(key, properties) {
return this.get('cache')[key] = this.get('generator').generate(properties);
}
});
1 change: 1 addition & 0 deletions app/components/ember-initials/initials/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-initials/components/initials';
1 change: 1 addition & 0 deletions app/initializers/ember-initials-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-initials/initializers/ember-initials-store';
1 change: 0 additions & 1 deletion app/services/ember-initials-store.js

This file was deleted.

11 changes: 9 additions & 2 deletions config/environment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* eslint-env node */
'use strict';

module.exports = function (/* environment, appConfig */) {
return { };
module.exports = function (environment, appConfig) {
appConfig.emberInitials = {
image: {
defaultImageUrl: ''
},
gravatar: {
defaultImageUrl: ''
},
};
};
Loading