new 'addNavigationBarItem' plugin api

This commit is contained in:
Régis Hanol 2017-08-05 02:23:35 +02:00
parent e958500735
commit 4b53fe3cc7
2 changed files with 72 additions and 38 deletions

View File

@ -20,10 +20,11 @@ import { addPostClassesCallback } from 'discourse/widgets/post';
import { addPostTransformCallback } from 'discourse/widgets/post-stream'; import { addPostTransformCallback } from 'discourse/widgets/post-stream';
import { attachAdditionalPanel } from 'discourse/widgets/header'; import { attachAdditionalPanel } from 'discourse/widgets/header';
import { registerIconRenderer } from 'discourse-common/lib/icon-library'; import { registerIconRenderer } from 'discourse-common/lib/icon-library';
import { addNavItem } from 'discourse/models/nav-item';
// If you add any methods to the API ensure you bump up this number // If you add any methods to the API ensure you bump up this number
const PLUGIN_API_VERSION = '0.8.8'; const PLUGIN_API_VERSION = '0.8.9';
class PluginApi { class PluginApi {
constructor(version, container) { constructor(version, container) {
@ -524,6 +525,22 @@ class PluginApi {
addPostTransformCallback(callback) { addPostTransformCallback(callback) {
addPostTransformCallback(callback); addPostTransformCallback(callback);
} }
/**
*
* Adds a new item in the navigation bar.
*
* Example:
*
* addNavigationBarItem({
* name: "discourse",
* displayName: "Discourse"
* href: "https://www.discourse.org",
* })
*/
addNavigationBarItem(item) {
addNavItem(item);
}
} }
let _pluginv01; let _pluginv01;

View File

@ -1,83 +1,92 @@
import { toTitleCase } from 'discourse/lib/formatter'; import { toTitleCase } from 'discourse/lib/formatter';
import computed from 'ember-addons/ember-computed-decorators';
const NavItem = Discourse.Model.extend({ const NavItem = Discourse.Model.extend({
displayName: function() { @computed("categoryName", "name", "count")
var categoryName = this.get('categoryName'), displayName(categoryName, name, count) {
name = this.get('name'), count = count || 0;
count = this.get('count') || 0;
if (name === 'latest' && !Discourse.Site.currentProp('mobileView')) { if (name === 'latest' && !Discourse.Site.currentProp('mobileView')) {
count = 0; count = 0;
} }
var extra = { count: count }; let extra = { count: count };
var titleKey = count === 0 ? '.title' : '.title_with_count'; const titleKey = count === 0 ? '.title' : '.title_with_count';
if (categoryName) { if (categoryName) {
name = 'category'; name = 'category';
extra.categoryName = toTitleCase(categoryName); extra.categoryName = toTitleCase(categoryName);
} }
return I18n.t("filters." + name.replace("/", ".") + titleKey, extra);
}.property('categoryName', 'name', 'count'),
categoryName: function() { return I18n.t(`filters.${name.replace("/", ".") + titleKey}`, extra);
var split = this.get('name').split('/'); },
@computed("name")
categoryName(name) {
const split = name.split('/');
return split[0] === 'category' ? split[1] : null; return split[0] === 'category' ? split[1] : null;
}.property('name'), },
categorySlug: function() { @computed("name")
var split = this.get('name').split('/'); categorySlug(name) {
const split = name.split('/');
if (split[0] === 'category' && split[1]) { if (split[0] === 'category' && split[1]) {
var cat = Discourse.Site.current().categories.findBy('nameLower', split[1].toLowerCase()); const cat = Discourse.Site.current().categories.findBy('nameLower', split[1].toLowerCase());
return cat ? Discourse.Category.slugFor(cat) : null; return cat ? Discourse.Category.slugFor(cat) : null;
} }
return null; return null;
}.property('name'), },
@computed("filterMode")
href(filterMode) {
let customHref = null;
href: function() {
var customHref = null;
_.each(NavItem.customNavItemHrefs, function(cb) { _.each(NavItem.customNavItemHrefs, function(cb) {
customHref = cb.call(this, this); customHref = cb.call(this, this);
if (customHref) { return false; } if (customHref) { return false; }
}, this); }, this);
if (customHref) { return customHref; } if (customHref) { return customHref; }
return Discourse.getURL("/") + this.get('filterMode');
}.property('filterMode'),
// href from this item return Discourse.getURL("/") + filterMode;
filterMode: function() { },
var name = this.get('name');
if( name.split('/')[0] === 'category' ) { @computed("name", "category", "categorySlug", "noSubcategories")
return 'c/' + this.get('categorySlug'); filterMode(name, category, categorySlug, noSubcategories) {
if (name.split('/')[0] === 'category') {
return 'c/' + categorySlug;
} else { } else {
var mode = "", let mode = "";
category = this.get("category"); if (category) {
if(category){
mode += "c/"; mode += "c/";
mode += Discourse.Category.slugFor(this.get('category')); mode += Discourse.Category.slugFor(category);
if (this.get('noSubcategories')) { mode += '/none'; } if (noSubcategories) { mode += '/none'; }
mode += "/l/"; mode += "/l/";
} }
return mode + name.replace(' ', '-'); return mode + name.replace(' ', '-');
} }
}.property('name'), },
count: function() { @computed("topicTrackingState", "name", "category")
var state = this.get('topicTrackingState'); count(state, name, category) {
if (state) { if (state) {
return state.lookupCount(this.get('name'), this.get('category')); return state.lookupCount(name, category);
} }
}.property('topicTrackingState.messageCount') }
}); });
const ExtraNavItem = NavItem.extend({
@computed("href")
href: (href) => href
});
NavItem.reopenClass({ NavItem.reopenClass({
extraArgsCallbacks: [], extraArgsCallbacks: [],
customNavItemHrefs: [], customNavItemHrefs: [],
extraNavItems: [],
// create a nav item from the text, will return null if there is not valid nav item for this particular text // create a nav item from the text, will return null if there is not valid nav item for this particular text
fromText(text, opts) { fromText(text, opts) {
@ -113,16 +122,24 @@ NavItem.reopenClass({
items.push(args.filterMode); items.push(args.filterMode);
} }
return items.map(i => Discourse.NavItem.fromText(i, args)) items = items.map(i => Discourse.NavItem.fromText(i, args))
.filter(i => i !== null && !(category && i.get("name").indexOf("categor") === 0)); .filter(i => i !== null && !(category && i.get("name").indexOf("categor") === 0));
return items.concat(NavItem.extraNavItems);
} }
}); });
export default NavItem; export default NavItem;
export function extraNavItemProperties(cb) { export function extraNavItemProperties(cb) {
NavItem.extraArgsCallbacks.push(cb); NavItem.extraArgsCallbacks.push(cb);
} }
export function customNavItemHref(cb) { export function customNavItemHref(cb) {
NavItem.customNavItemHrefs.push(cb); NavItem.customNavItemHrefs.push(cb);
} }
export function addNavItem(item) {
NavItem.extraNavItems.push(ExtraNavItem.create(item));
}