From ab0078aded665dfc6d1633a3deca481f0548499e Mon Sep 17 00:00:00 2001 From: tshenry Date: Thu, 18 Jun 2020 15:54:01 -0700 Subject: [PATCH] Initial commit --- LICENSE | 21 ++++++ README.md | 5 ++ about.json | 7 ++ .../components/category-preview.js | 37 ++++++++++ .../category-preview-connector.hbs | 1 + .../templates/components/category-preview.hbs | 67 +++++++++++++++++++ mobile/mobile.scss | 11 +++ settings.yml | 43 ++++++++++++ 8 files changed, 192 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 about.json create mode 100644 javascripts/discourse-category-previews/components/category-preview.js create mode 100644 javascripts/discourse-category-previews/templates/connectors/category-list-above-each-category/category-preview-connector.hbs create mode 100644 javascripts/discourse/templates/components/category-preview.hbs create mode 100644 mobile/mobile.scss create mode 100644 settings.yml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..195ed07 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 tshenry + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9af3852 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Category Previews + +This Discourse theme component will allow you to expose restricted categories without allowing access. The "previews" can link anywhere on or off-site. + +More information here: diff --git a/about.json b/about.json new file mode 100644 index 0000000..86343d0 --- /dev/null +++ b/about.json @@ -0,0 +1,7 @@ +{ + "name": "Category Previews", + "about_url": null, + "license_url": null, + "component": true, + "minimum_discourse_version": "2.5.0.beta7" +} diff --git a/javascripts/discourse-category-previews/components/category-preview.js b/javascripts/discourse-category-previews/components/category-preview.js new file mode 100644 index 0000000..f806a7a --- /dev/null +++ b/javascripts/discourse-category-previews/components/category-preview.js @@ -0,0 +1,37 @@ +import Component from "@ember/component"; +import { equal } from "@ember/object/computed"; +import discourseComputed from "discourse-common/utils/decorators"; + +const rawCategoryPreviews = settings.category_previews.split("|"); + +export default Component.extend({ + noCategoryStyle: equal("siteSettings.category_style", "none"), + + @discourseComputed() + preview() { + const previewData = []; + const loggedInUser = this.currentUser; + const loggedInUserGroup = loggedInUser ? loggedInUser.groups.map(g => g.name) : []; + const isStaff = loggedInUser ? loggedInUser.staff : false; + const categorySlug = this.args.category.slug; + + rawCategoryPreviews.forEach(rawPreview => { + const previewPart = rawPreview.split("~"); + const permittedGroup = previewPart[4] ? previewPart[4].split(",") : []; + const hasCategoryAccess = loggedInUserGroup.some(g => permittedGroup.indexOf(g) > -1); + const shouldRender = !loggedInUser || isStaff || !hasCategoryAccess; + + if (shouldRender && categorySlug === previewPart[0]) { + previewData.push({ + title: previewPart[1], + description: previewPart[2], + href: previewPart[3], + className: `above-${categorySlug}`, + color: settings.border_color + }); + } + }); + + return previewData; + } +}); diff --git a/javascripts/discourse-category-previews/templates/connectors/category-list-above-each-category/category-preview-connector.hbs b/javascripts/discourse-category-previews/templates/connectors/category-list-above-each-category/category-preview-connector.hbs new file mode 100644 index 0000000..96543ec --- /dev/null +++ b/javascripts/discourse-category-previews/templates/connectors/category-list-above-each-category/category-preview-connector.hbs @@ -0,0 +1 @@ +{{category-preview tagName="" args=(hash category=category)}} diff --git a/javascripts/discourse/templates/components/category-preview.hbs b/javascripts/discourse/templates/components/category-preview.hbs new file mode 100644 index 0000000..57b80aa --- /dev/null +++ b/javascripts/discourse/templates/components/category-preview.hbs @@ -0,0 +1,67 @@ +{{#if preview}} + {{#each preview as |p|}} + {{#if site.mobileView}} +
+ + + + + + + + + +
+

+ {{#if p.href}} + +
+ {{#if (theme-setting 'locked_icon')}} + {{d-icon (theme-setting 'locked_icon')}} + {{/if}} + {{p.title}} +
+
+ {{else}} +
+ {{#if (theme-setting 'locked_icon')}} + {{d-icon (theme-setting 'locked_icon')}} + {{/if}} + {{p.title}} +
+ {{/if}} +

+
+
{{html-safe p.description}}
+
+
+ {{else}} + + +

+ {{#if p.href}} + +
+ {{#if (theme-setting 'locked_icon')}} + {{d-icon (theme-setting 'locked_icon')}} + {{/if}} + {{p.title}} +
+
+ {{else}} +
+ {{#if (theme-setting 'locked_icon')}} + {{d-icon (theme-setting 'locked_icon')}} + {{/if}} + {{p.title}} +
+ {{/if}} +

+ {{#if p.description}} +
{{p.description}}
+ {{/if}} + + + {{/if}} + {{/each}} +{{/if}} diff --git a/mobile/mobile.scss b/mobile/mobile.scss new file mode 100644 index 0000000..5b059d1 --- /dev/null +++ b/mobile/mobile.scss @@ -0,0 +1,11 @@ +.category-list.with-topics { + .category-list-item[class*="above-"] { + tr.category-description { + display: table-row; + border-bottom: none; + } + div.category-description { + display: block; + } + } +} diff --git a/settings.yml b/settings.yml new file mode 100644 index 0000000..b078de1 --- /dev/null +++ b/settings.yml @@ -0,0 +1,43 @@ +--- +category_previews: + type: list + list_type: simple + default: '' + description: + en: 'Each entry should be ~ delimited using the following format: +

+ category-slug~Preview Name~Preview Description~https://example.com~group +

+ + + There should always be 4 "~" in the entry, but you can choose to exclude values after each "~" to omit one of the above sections +

+ You can use the same category slug multiple times. When this is done, the previews will display in the order they appear in the setting.' +locked_icon: + type: string + default: 'lock' + description: + en: 'The name of a Font Awesome icon to display before the Preview Name. Leave blank if no icon is need.' +border_color: + type: string + default: '0088CC' + description: + en: 'The hex color associated with all category previews when the category style site setting is not set to "none."'