From 8fe2f5448948ea3f462dba4ffbf34a20adf92588 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 25 Jun 2015 15:31:15 +0930 Subject: [PATCH] Split discussion controls into three groups: - user (reply, subscription) - moderation (rename, sticky, tags) - destructive (delete) Will keep extension-added items organised nicely --- .../src/initializers/discussion-controls.js | 32 ++++++++++++++++--- js/lib/utils/item-list.js | 13 +++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/js/forum/src/initializers/discussion-controls.js b/js/forum/src/initializers/discussion-controls.js index a5d590cf3..f83f05a3a 100644 --- a/js/forum/src/initializers/discussion-controls.js +++ b/js/forum/src/initializers/discussion-controls.js @@ -70,7 +70,7 @@ export default function(app) { } } - Discussion.prototype.controls = function(context) { + Discussion.prototype.userControls = function(context) { var items = new ItemList(); if (context instanceof DiscussionPage) { @@ -78,20 +78,42 @@ export default function(app) { ? ActionButton.component({ icon: 'reply', label: app.session.user() ? 'Reply' : 'Log In to Reply', onclick: this.replyAction.bind(this, true, false) }) : ActionButton.component({ icon: 'reply', label: 'Can\'t Reply', className: 'disabled', title: 'You don\'t have permission to reply to this discussion.' }) ); - - items.add('separator', Separator.component()); } + return items; + }; + + Discussion.prototype.moderationControls = function(context) { + var items = new ItemList(); + if (this.canRename()) { items.add('rename', ActionButton.component({ icon: 'pencil', label: 'Rename', onclick: this.renameAction.bind(this) })); } - if (this.canDelete()) { - items.add('separator2', Separator.component()); + return items; + }; + Discussion.prototype.destructiveControls = function(context) { + var items = new ItemList(); + + if (this.canDelete()) { items.add('delete', ActionButton.component({ icon: 'times', label: 'Delete', onclick: this.deleteAction.bind(this) })); } + return items; + }; + + Discussion.prototype.controls = function(context) { + var items = new ItemList(); + + ['user', 'moderation', 'destructive'].forEach(section => { + var controls = this[section+'Controls'](context).toArray(); + if (controls.length) { + items.add(section, controls); + items.add(section+'Separator', Separator.component()); + } + }); + return items; } }; diff --git a/js/lib/utils/item-list.js b/js/lib/utils/item-list.js index 238aa857f..5ee67bf87 100644 --- a/js/lib/utils/item-list.js +++ b/js/lib/utils/item-list.js @@ -59,7 +59,18 @@ export default class ItemList { } }); - return array.map((item) => item.content); + array = array.map(item => item.content); + + //recursively flatten array + for (var i = 0, len = array.length; i < len; i++) { + if (array[i] instanceof Array) { + array = array.concat.apply([], array); + i-- //check current index again and flatten until there are no more nested arrays at that index + len = array.length; + } + } + + return array; } }