From d89bcef8165619463c5e15e1981573c6273bbe80 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 12 Feb 2015 15:16:15 +1030 Subject: [PATCH] Helper method for adding menu separators --- .../app/components/application/user-dropdown.js | 14 +++++--------- .../core/ember/app/mixins/has-item-lists.js | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/framework/core/ember/app/components/application/user-dropdown.js b/framework/core/ember/app/components/application/user-dropdown.js index 32164d8d1..1f080e7a3 100644 --- a/framework/core/ember/app/components/application/user-dropdown.js +++ b/framework/core/ember/app/components/application/user-dropdown.js @@ -2,7 +2,6 @@ import Ember from 'ember'; import HasItemLists from 'flarum/mixins/has-item-lists'; import DropdownButton from 'flarum/components/ui/dropdown-button'; -import SeparatorItem from 'flarum/components/ui/separator-item'; export default DropdownButton.extend(HasItemLists, { layoutName: 'components/application/user-dropdown', @@ -13,15 +12,12 @@ export default DropdownButton.extend(HasItemLists, { label: Ember.computed.alias('user.username'), populateItems: function(items) { + var self = this; this.addActionItem(items, 'profile', 'Profile', 'user'); this.addActionItem(items, 'settings', 'Settings', 'cog'); - items.pushObject(SeparatorItem.create()); - this.addActionItem(items, 'logout', 'Log Out', 'sign-out', null, null, this); - }, - - actions: { - logout: function() { - this.logout(); - } + this.addSeparatorItem(items); + this.addActionItem(items, 'logout', 'Log Out', 'sign-out', null, function() { + self.sendAction('logout'); + }); } }) diff --git a/framework/core/ember/app/mixins/has-item-lists.js b/framework/core/ember/app/mixins/has-item-lists.js index 2054abf47..fef154eab 100644 --- a/framework/core/ember/app/mixins/has-item-lists.js +++ b/framework/core/ember/app/mixins/has-item-lists.js @@ -2,6 +2,7 @@ import Ember from 'ember'; import TaggedArray from 'flarum/utils/tagged-array'; import ActionButton from 'flarum/components/ui/action-button'; +import SeparatorItem from 'flarum/components/ui/separator-item'; export default Ember.Mixin.create({ itemLists: [], @@ -23,16 +24,15 @@ export default Ember.Mixin.create({ return items; }, - addActionItem: function(items, tag, label, icon, conditionProperty, actionName, actionTarget) { + addActionItem: function(items, tag, label, icon, conditionProperty, action) { if (conditionProperty && !this.get(conditionProperty)) { return; } var self = this; - actionTarget = actionTarget || self.get('controller'); var item = ActionButton.extend({ label: label, icon: icon, - action: function() { - actionTarget.send(actionName || tag); + action: action || function() { + self.get('controller').send(tag); } }); @@ -41,5 +41,13 @@ export default Ember.Mixin.create({ items.pushObjectWithTag(itemInstance, tag); return itemInstance; + }, + + addSeparatorItem: function(items) { + var length = items.get('length'); + var last = items.objectAt(length - 1); + if (last && !(last instanceof SeparatorItem)) { + items.pushObject(SeparatorItem.create()); + } } });