REFACTOR: DRY up New Topic button code

This commit is contained in:
Robin Ward 2017-08-01 14:50:17 -04:00
parent a870137845
commit 513b7bf706
12 changed files with 100 additions and 51 deletions

View File

@ -0,0 +1 @@
export default Ember.Component.extend({ tagName: '' });

View File

@ -14,11 +14,5 @@ export default Ember.Controller.extend({
@computed
loginRequired() {
return Discourse.SiteSettings.login_required && !Discourse.User.current();
},
actions: {
appRouteAction(name) {
return this.send(name);
}
}
});

View File

@ -5,6 +5,7 @@ import Category from 'discourse/models/category';
import { escapeExpression } from 'discourse/lib/utilities';
import { setTransient } from 'discourse/lib/page-tracker';
import { iconHTML } from 'discourse-common/lib/icon-library';
import Composer from 'discourse/models/composer';
const SortOrders = [
{name: I18n.t('search.relevance'), id: 0},
@ -18,6 +19,7 @@ const PAGE_LIMIT = 10;
export default Ember.Controller.extend({
application: Ember.inject.controller(),
composer: Ember.inject.controller(),
bulkSelectEnabled: null,
loading: false,
@ -226,6 +228,21 @@ export default Ember.Controller.extend({
actions: {
createTopic(searchTerm) {
let topicCategory;
if (searchTerm.indexOf("category:") !== -1) {
const match = searchTerm.match(/category:(\S*)/);
if (match && match[1]) {
topicCategory = match[1];
}
}
this.get('composer').open({
action: Composer.CREATE_TOPIC,
draftKey: Composer.CREATE_TOPIC,
topicCategory
});
},
selectAll() {
this.get('selected').addObjects(this.get('model.posts').map(r => r.topic));
// Doing this the proper way is a HUGE pain,

View File

@ -0,0 +1,59 @@
const {
A: emberArray,
Helper,
assert,
computed,
get,
getOwner,
run,
runInDebug
} = Ember;
function getCurrentHandlerInfos(router) {
let routerLib = router._routerMicrolib || router.router;
return routerLib.currentHandlerInfos;
}
function getRoutes(router) {
return emberArray(getCurrentHandlerInfos(router))
.mapBy('handler')
.reverse();
}
function getRouteWithAction(router, actionName) {
let action;
let handler = emberArray(getRoutes(router)).find((route) => {
let actions = route.actions || route._actions;
action = actions[actionName];
return typeof(action) === 'function';
});
return { action, handler };
}
export default Helper.extend({
router: computed(function() {
return getOwner(this).lookup('router:main');
}).readOnly(),
compute([actionName, ...params]) {
let router = get(this, 'router');
assert('[ember-route-action-helper] Unable to lookup router', router);
runInDebug(() => {
let { handler } = getRouteWithAction(router, actionName);
assert(`[ember-route-action-helper] Unable to find action ${actionName}`, handler);
});
let routeAction = function(...invocationArgs) {
let { action, handler } = getRouteWithAction(router, actionName);
let args = params.concat(invocationArgs);
return run.join(handler, action, ...args);
};
return routeAction;
}
});

View File

@ -1,9 +1,7 @@
import { ajax } from 'discourse/lib/ajax';
import { translateResults, getSearchKey, isValidSearchTerm } from "discourse/lib/search";
import Composer from 'discourse/models/composer';
import PreloadStore from 'preload-store';
import { getTransient, setTransient } from 'discourse/lib/page-tracker';
import { getOwner } from 'discourse-common/lib/get-owner';
export default Discourse.Route.extend({
queryParams: { q: {}, expanded: false, context_id: {}, context: {}, skip_context: {} },
@ -47,17 +45,6 @@ export default Discourse.Route.extend({
didTransition() {
this.controllerFor("full-page-search")._showFooter();
return true;
},
createTopic(searchTerm) {
let category;
if (searchTerm.indexOf("category:")) {
const match = searchTerm.match(/category:(\S*)/);
if (match && match[1]) {
category = match[1];
}
}
getOwner(this).lookup('controller:composer').open({action: Composer.CREATE_TOPIC, draftKey: Composer.CREATE_TOPIC, topicCategory: category});
}
}

View File

@ -1,11 +1,11 @@
{{plugin-outlet name="above-site-header"}}
{{site-header canSignUp=canSignUp
showCreateAccount=(action "appRouteAction" "showCreateAccount")
showLogin=(action "appRouteAction" "showLogin")
showKeyboard=(action "appRouteAction" "showKeyboardShortcutsHelp")
toggleMobileView=(action "appRouteAction" "toggleMobileView")
toggleAnonymous=(action "appRouteAction" "toggleAnonymous")
logout=(action "appRouteAction" "logout")}}
showCreateAccount=(route-action "showCreateAccount")
showLogin=(route-action "showLogin")
showKeyboard=(route-action "showKeyboardShortcutsHelp")
toggleMobileView=(route-action "toggleMobileView")
toggleAnonymous=(route-action "toggleAnonymous")
logout=(route-action "logout")}}
{{plugin-outlet name="below-site-header"}}
<div id="main-outlet" class="wrap">

View File

@ -0,0 +1,9 @@
{{#if canCreateTopic}}
{{d-button
id="create-topic"
class="btn btn-default"
action=action
icon="plus"
disabled=disabled
label="topic.create"}}
{{/if}}

View File

@ -14,9 +14,7 @@
{{/if}}
</div>
{{#if canCreateTopic}}
<span class="new-topic-btn">{{d-button id="create-topic" class="btn-default" action="createTopic" actionParam=searchTerm icon="plus" label="topic.create"}}</span>
{{/if}}
{{create-topic-button canCreateTopic=canCreateTopic action=(action "createTopic" searchTerm)}}
{{#if canBulkSelect}}
{{d-button icon="list" class="bulk-select" title="topics.bulk.toggle" action="toggleBulkSelect"}}

View File

@ -6,12 +6,5 @@
{{#if canCreateCategory}}
{{categories-admin-dropdown}}
{{/if}}
{{#if canCreateTopic}}
{{d-button
id="create-topic"
action="createTopic"
icon="plus"
label="topic.create"
}}
{{/if}}
{{create-topic-button canCreateTopic=canCreateTopic action=(route-action "createTopic")}}
{{/d-section}}

View File

@ -20,14 +20,10 @@
{{category-notifications-button category=category}}
{{/if}}
{{#if canCreateTopic}}
{{d-button id="create-topic"
class="btn-default"
action="createTopic"
icon="plus"
label="topic.create"
disabled=cannotCreateTopicOnCategory}}
{{/if}}
{{create-topic-button
canCreateTopic=canCreateTopic
disabled=cannotCreateTopicOnCategory
action=(route-action "createTopic")}}
{{#if canEditCategory}}
{{d-button class="btn-default edit-category" action="editCategory" actionParam=category icon="wrench" label="category.edit_long"}}

View File

@ -3,8 +3,5 @@
{{navigation-bar navItems=navItems filterMode=filterMode}}
{{#if canCreateTopic}}
<button id="create-topic" class='btn btn-default' {{action "createTopic"}}>
{{d-icon "plus"}} <span>{{i18n 'topic.create'}}</span></button>
{{/if}}
{{create-topic-button canCreateTopic=canCreateTopic action=(route-action "createTopic")}}
{{/d-section}}

View File

@ -15,9 +15,7 @@
{{d-button action="deleteTag" icon="trash-o" class="admin-tag btn-danger"}}
{{d-button action="renameTag" actionParam=tag icon="pencil" class="admin-tag"}}
{{else}}
{{#if canCreateTopic}}
<button id="create-topic" class='btn btn-default' {{action "createTopic"}}>{{d-icon "plus"}}{{i18n 'topic.create'}}</button>
{{/if}}
{{create-topic-button canCreateTopic=canCreateTopic action=(route-action "createTopic")}}
{{/if}}
{{#if showTagFilter}}