Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions nodebb-plugin-polls-category/library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const Categories = require.main.require('./src/categories');
const Topics = require.main.require('./src/topics');

const Plugin = {};

Plugin.init = async function (params) {
const { app, router, middleware } = params;

// --- 1️⃣ Ensure Polls category exists ---
const allCategories = await Categories.getCategories([]);
let pollsCat = allCategories.find(c => c.slug === 'polls');

if (!pollsCat) {
pollsCat = await Categories.create({
name: 'Polls',
description: 'Vote on community polls and see results',
descriptionParsed: '<p>Vote on community polls and see results</p>\n',
bgColor: '#f5a623',
color: '#ffffff',
icon: 'fa-poll',
order: 5
});
console.log('Polls category created!');
} else {
console.log('Polls category already exists, skipping creation.');
}

// --- 2️⃣ Routes ---
router.get('/polls', middleware.buildHeader, async (req, res) => {
const result = await Topics.getTopicsByCategory(pollsCat.cid, 0, 50);
const topicsList = result.topics || [];
res.render('polls-page', { title: 'Community Polls', topics: topicsList });
});
};

module.exports = Plugin;
12 changes: 12 additions & 0 deletions nodebb-plugin-polls-category/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "nodebb-plugin-polls-category",
"version": "1.0.0",
"main": "library.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
30 changes: 30 additions & 0 deletions nodebb-plugin-polls-category/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"id": "nodebb-plugin-polls-category",
"name": "Polls Category",
"description": "Automatically creates a Polls category on NodeBB startup and enables poll creation and display for topics in that category.",
"url": "https://github.com/yourusername/nodebb-plugin-polls-category",
"library": "library.js",
"hooks": [
{
"hook": "static:app.load",
"method": "init"
},
{
"hook": "filter:topic.build",
"method": "addPollForm"
},
{
"hook": "action:topic.save",
"method": "attachPoll"
}
],
"templates": "templates",
"compatibility": "^4.0.0",
"authors": [
{
"name": "Your Name",
"email": "you@example.com",
"url": "https://yourwebsite.com"
}
]
}
25 changes: 25 additions & 0 deletions nodebb-plugin-polls-category/templates/poll-form.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="poll-form">
<label for="poll-question">Poll Question:</label>
<input type="text" id="poll-question" name="poll[question]" placeholder="Enter your question" required>

<label>Options:</label>
<div id="poll-options">
<input type="text" name="poll[options][]" placeholder="Option 1" required>
<input type="text" name="poll[options][]" placeholder="Option 2" required>
<input type="text" name="poll[options][]" placeholder="Option 3">
</div>

<button type="button" id="add-option">Add another option</button>
</div>

<script>
document.getElementById('add-option').addEventListener('click', () => {
const optionsDiv = document.getElementById('poll-options');
const inputCount = optionsDiv.querySelectorAll('input').length + 1;
const newInput = document.createElement('input');
newInput.type = 'text';
newInput.name = 'poll[options][]';
newInput.placeholder = 'Option ' + inputCount;
optionsDiv.appendChild(newInput);
});
</script>
5 changes: 5 additions & 0 deletions nodebb-plugin-polls-category/templates/polls-category.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="polls-category-page">
<h1>{title}</h1>
<p>{message}</p>
<p>This page was created by the polls category plugin.</p>
</div>
10 changes: 10 additions & 0 deletions nodebb-plugin-polls-category/templates/polls-page.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>{title}</h1>

<ul>
<!--% for (var i = 0; i < topics.length; i++) { %-->
<li>
<a href="/topic/<!--%:= topics[i].tid %-->"><!--%:= topics[i].title %--></a>
<span>— <!--%:= topics[i].replies %--> replies</span>
</li>
<!--% } %-->
</ul>
66 changes: 66 additions & 0 deletions nodebb-plugin-polls-page/library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Categories = require.main.require('./src/categories');
const Topics = require.main.require('./src/topics');

const Plugin = {};

Plugin.init = async function (params) {
const { app, router, middleware } = params;

// --- 1️⃣ Create Polls category if it doesn't exist ---
const allCategories = await Categories.getCategories([]);
const exists = allCategories.some(c => c.name === 'Polls');
if (!exists) {
await Categories.create({
name: 'Polls',
description: 'Vote on community polls and see results',
descriptionParsed: '<p>Vote on community polls and see results</p>\n',
bgColor: '#f5a623',
color: '#ffffff',
icon: 'fa-poll',
order: 5
});
console.log('Polls category created!');
}

// --- 2️⃣ Route for static polls-category page ---
router.get('/polls-category', middleware.buildHeader, renderPollsCategory);
router.get('/api/polls-category', renderPollsCategory);

// --- 3️⃣ Route for dynamic polls page ---
router.get('/polls', middleware.buildHeader, renderPollsPage);
router.get('/api/polls', renderPollsPage);
};

// --- 4️⃣ Render function for /polls-category ---
async function renderPollsCategory(req, res) {
res.render('polls-category', {
title: 'Polls Category',
message: 'Welcome to the Polls Category page!',
});
}

// --- 5️⃣ Render function for /polls (shows real topics) ---
async function renderPollsPage(req, res) {
// Get Polls category
const allCategories = await Categories.getCategories([]);
const pollsCat = allCategories.find(c => c.name === 'Polls');

let topicsList = [];
if (pollsCat) {
const result = await Topics.getTopicsByCategory(pollsCat.cid, 0, 50);
topicsList = result.topics || [];
}

// Render template
res.render('polls-page', {
title: 'Community Polls',
message: 'Here are the latest polls in the community!',
topics: topicsList
});
}

module.exports = Plugin;
8 changes: 8 additions & 0 deletions nodebb-plugin-polls-page/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "nodebb-plugin-polls-page",
"version": "1.0.0",
"description": "Provides a standalone page to list all polls in the Polls category.",
"main": "library.js",
"author": "Your Name <you@example.com>",
"license": "MIT"
}
10 changes: 10 additions & 0 deletions nodebb-plugin-polls-page/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "nodebb-plugin-polls-test",
"name": "Polls Test",
"description": "Test plugin to render a simple page",
"library": "library.js",
"hooks": [
{ "hook": "static:app.load", "method": "init" }
],
"compatibility": "^4.0.0"
}
16 changes: 16 additions & 0 deletions nodebb-plugin-polls-page/template/polls-page.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>{title}</h1>
<p>{message}</p>

<ul>
{{each topics as topic}}
<li>
<a href="/topic/{{topic.tid}}">{{topic.title}}</a>
<span> — {{topic.replies}} replies</span>
</li>
{{/each}}
</ul>

<div style="width:100%;height:50px;background:lightgray;margin-top:20px;">
<!-- This gray box proves the template is rendering -->
Template is rendering ✅
</div>
5 changes: 5 additions & 0 deletions nodebb-plugin-polls-page/template/polls-test.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>{title}</h1>
<p>{message}</p>
<div style="width:100%; height:500px; background:blue; border:1px solid #ccc;">
This is a white box representing your polls page.
</div>