From f3fab617da101b8b387520378c041a714342515a Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 12 Mar 2015 12:10:58 +1030 Subject: [PATCH] Extract user bio into its own component --- ember/app/components/user/user-bio.js | 42 +++++++++++++++++++ ember/app/components/user/user-card.js | 41 ++++-------------- ember/app/styles/flarum/user.less | 28 +++++++------ .../templates/components/user/user-bio.hbs | 9 ++++ .../templates/components/user/user-card.hbs | 14 ------- 5 files changed, 75 insertions(+), 59 deletions(-) create mode 100644 ember/app/components/user/user-bio.js create mode 100644 ember/app/templates/components/user/user-bio.hbs diff --git a/ember/app/components/user/user-bio.js b/ember/app/components/user/user-bio.js new file mode 100644 index 000000000..8ff58a34c --- /dev/null +++ b/ember/app/components/user/user-bio.js @@ -0,0 +1,42 @@ +import Ember from 'ember'; + +export default Ember.Component.extend({ + layoutName: 'components/user/user-bio', + classNames: ['user-bio'], + classNameBindings: ['isEditable:editable', 'editing'], + + isEditable: Ember.computed.and('user.canEdit', 'editable'), + editing: false, + + didInsertElement: function() { + this.$().on('click', 'a', function(e) { + e.stopPropagation(); + }); + }, + + click: function() { + this.send('edit'); + }, + + actions: { + edit: function() { + if (!this.get('isEditable')) { return; } + + this.set('editing', true); + var component = this; + Ember.run.scheduleOnce('afterRender', this, function() { + this.$('textarea').focus().blur(function() { + component.send('save', $(this).val()); + }); + }); + }, + + save: function(value) { + this.set('editing', false); + + var user = this.get('user'); + user.set('bio', value); + user.save(); + } + } +}); diff --git a/ember/app/components/user/user-card.js b/ember/app/components/user/user-card.js index a49905787..19f25322c 100644 --- a/ember/app/components/user/user-card.js +++ b/ember/app/components/user/user-card.js @@ -1,6 +1,7 @@ import Ember from 'ember'; import HasItemLists from 'flarum/mixins/has-item-lists'; +import UserBio from 'flarum/components/user/user-bio'; export default Ember.Component.extend(HasItemLists, { layoutName: 'components/user/user-card', @@ -12,44 +13,20 @@ export default Ember.Component.extend(HasItemLists, { return 'background-color: '+this.get('user.color'); }), - bioEditable: Ember.computed.and('user.canEdit', 'editable'), - showBio: Ember.computed.or('user.bioHtml', 'bioEditable'), - - didInsertElement: function() { - this.$().on('click', '.user-bio a', function(e) { - e.stopPropagation(); - }); - }, - - actions: { - editBio: function() { - if (!this.get('bioEditable')) { - return; - } - - this.set('editingBio', true); - var component = this; - Ember.run.scheduleOnce('afterRender', this, function() { - this.$('.user-bio textarea').focus().blur(function() { - component.send('saveBio', $(this).val()); - }); - }); - }, - - saveBio: function(value) { - var user = this.get('user'); - user.set('bio', value); - user.save(); - this.set('editingBio', false); - } - }, - populateControls: function(items) { this.addActionItem(items, 'edit', 'Edit', 'pencil'); this.addActionItem(items, 'delete', 'Delete', 'times'); }, populateInfo: function(items) { + if (this.get('user.bioHtml') || (this.get('editable') && this.get('user.canEdit'))) { + items.pushObjectWithTag(UserBio.extend({ + user: this.get('user'), + editable: this.get('editable'), + listItemClass: 'block-item' + }), 'bio'); + } + items.pushObjectWithTag(Ember.Component.extend({ layout: Ember.Handlebars.compile('{{fa-icon "circle"}} Online') }), 'lastActiveTime'); diff --git a/ember/app/styles/flarum/user.less b/ember/app/styles/flarum/user.less index 19bab8f95..907224ae5 100644 --- a/ember/app/styles/flarum/user.less +++ b/ember/app/styles/flarum/user.less @@ -42,10 +42,23 @@ & .badges { margin-left: 10px; } + & .user-info { + margin: 15px 0 0; + padding: 0; + list-style: none; + font-size: 12px; + + & > li { + display: inline-block; + margin-right: 15px; + } + & .block-item { + display: block; + } + } & .user-bio { - margin-top: 15px; padding: 10px 10px 1px; - margin: 5px -10px -5px; + margin: -10px -10px 10px; &.editable:not(.editing) { cursor: text; @@ -66,15 +79,4 @@ font-size: 14px; } } - & .user-info { - margin: 15px 0 0; - padding: 0; - list-style: none; - font-size: 12px; - - & > li { - display: inline-block; - margin-right: 15px; - } - } } diff --git a/ember/app/templates/components/user/user-bio.hbs b/ember/app/templates/components/user/user-bio.hbs new file mode 100644 index 000000000..a8c5fb346 --- /dev/null +++ b/ember/app/templates/components/user/user-bio.hbs @@ -0,0 +1,9 @@ +{{#if editing}} + {{textarea value=user.bio class="form-control"}} +{{else}} + {{#if user.bioHtml}} + {{{user.bioHtml}}} + {{else if isEditable}} +

Write something about yourself...

+ {{/if}} +{{/if}} diff --git a/ember/app/templates/components/user/user-card.hbs b/ember/app/templates/components/user/user-card.hbs index 73a5ac780..4f727c944 100644 --- a/ember/app/templates/components/user/user-card.hbs +++ b/ember/app/templates/components/user/user-card.hbs @@ -10,20 +10,6 @@ {{ui/item-list items=user.badges class="badges user-badges"}} - {{#if showBio}} -
- {{#if editingBio}} - {{textarea value=user.bio class="form-control"}} - {{else}} - {{#if user.bioHtml}} - {{{user.bioHtml}}} - {{else if bioEditable}} -

Write something about yourself...

- {{/if}} - {{/if}} -
- {{/if}} - {{ui/item-list items=info class="user-info"}}