Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed May 27, 2024
1 parent d4bf8ea commit 2781ba4
Show file tree
Hide file tree
Showing 31 changed files with 2,218 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v0.1.0
## 05/22/2024

1. [](#new)
* ChangeLog started...
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MIT License
The MIT License (MIT)

Copyright (c) 2024 Trilby Media

Expand Down
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Polls Plugin

**This README.md file should be modified to describe the features, installation, configuration, and general usage of the plugin.**

The **Polls** Plugin is an extension for [Grav CMS](https://github.com/getgrav/grav). Simple polls for Grav CMS

## Installation

Installing the Polls plugin can be done in one of three ways: The GPM (Grav Package Manager) installation method lets you quickly install the plugin with a simple terminal command, the manual method lets you do so via a zip file, and the admin method lets you do so via the Admin Plugin.

### GPM Installation (Preferred)

To install the plugin via the [GPM](https://learn.getgrav.org/cli-console/grav-cli-gpm), through your system's terminal (also called the command line), navigate to the root of your Grav-installation, and enter:

bin/gpm install polls

This will install the Polls plugin into your `/user/plugins`-directory within Grav. Its files can be found under `/your/site/grav/user/plugins/polls`.

### Manual Installation

To install the plugin manually, download the zip-version of this repository and unzip it under `/your/site/grav/user/plugins`. Then rename the folder to `polls`. You can find these files on [GitHub](https://github.com/trilbymedia/grav-plugin-polls) or via [GetGrav.org](https://getgrav.org/downloads/plugins).

You should now have all the plugin files under

/your/site/grav/user/plugins/polls

> NOTE: This plugin is a modular component for Grav which may require other plugins to operate, please see its [blueprints.yaml-file on GitHub](https://github.com/trilbymedia/grav-plugin-polls/blob/main/blueprints.yaml).
### Admin Plugin

If you use the Admin Plugin, you can install the plugin directly by browsing the `Plugins`-menu and clicking on the `Add` button.

## Configuration

Before configuring this plugin, you should copy the `user/plugins/polls/polls.yaml` to `user/config/plugins/polls.yaml` and only edit that copy.

Here is the default configuration and an explanation of available options:

```yaml
enabled: true
```
Note that if you use the Admin Plugin, a file with your configuration named polls.yaml will be saved in the `user/config/plugins/`-folder once the configuration is saved in the Admin.

## Usage

**Describe how to use the plugin.**

## Credits

**Did you incorporate third-party code? Want to thank somebody?**

## To Do

- [ ] Future plans, if any

83 changes: 83 additions & 0 deletions admin/blueprints/polls.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
title: Polls
form:
validation: loose
fields:
configuration_title:
type: section
title: PLUGIN_POLLS.MANAGE_POLLS
underline: true
fields:
polls:
type: list
label: Polls
style: vertical
fields:
.question:
type: text
label: Question
validate:
required: true
.id:
type: text
label: ID
validate:
required: true
.answers:
type: array
value_only: true
label: Answers
.enabled:
type: toggle
label: Enabled
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
.max_answers:
type: number
label: Max Answers
size: x-small
default: 1
validate:
type: int
min: 1
.min_answers:
type: number
label: Min Answers
size: x-small
default: 1
validate:
type: int
min: 1
.show_hints:
type: toggle
label: Show Hints
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
.show_created:
type: toggle
label: Show Created
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
.created_at:
type: datetime
label: Created
size: medium
readonly: true
default: 'now'



7 changes: 7 additions & 0 deletions admin/pages/polls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Polls Manager

access:
admin.super: true
admin.polls: true
---
21 changes: 21 additions & 0 deletions admin/templates/polls.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'partials/base.html.twig' %}

{#{% set slug = uri.basename %}#}
{#{% set base_route = location %}#}

{% block stylesheets %}
{{ parent() }}
{% do assets.addJs('plugin://polls/assets/admin/polls.js') %}
{% endblock %}

{% block titlebar %}
<div class="button-bar">
<a class="button" href="{{ base_url }}"><i class="fa fa-reply"></i> {{ "PLUGIN_ADMIN.BACK"|tu }}</a>
<button class="button" type="submit" name="task" value="savePolls" form="blueprints"><i class="fa fa-check"></i> {{ "PLUGIN_ADMIN.SAVE"|tu }}</button>
</div>
<h1><i class="fa fa-fw fa-key"></i> {{ "PLUGIN_POLLS.TITLE"|tu }}</h1>
{% endblock %}

{% block content %}
{% include 'partials/blueprints.html.twig' with { data: grav.polls.getData, blueprints: grav.polls.getBlueprints.init() } %}
{% endblock %}
61 changes: 61 additions & 0 deletions assets/admin/polls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Function to convert a string to a slug
function stringToSlug(str) {
return str.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '');
}

// Function to add event listeners to a single item
function addListenersToItems(item) {
const questionInput = item.querySelector('input[type="text"][name*="[question]"]');
const idInput = item.querySelector('input[type="text"][name*="[id]"]');
if (!questionInput || !idInput) return;
let isIdEdited = idInput.value.trim() !== '';

questionInput.addEventListener('input', function() {
if (!isIdEdited) {
idInput.value = stringToSlug(questionInput.value);
}
});

idInput.addEventListener('input', function() {
if (idInput.value.trim() === '') {
isIdEdited = false;
} else {
isIdEdited = true;
}
});

const maxAnswersInput = item.querySelector('input[type="number"][name*="[max_answers]"]');
const minAnswersInput = item.querySelector('input[type="number"][name*="[min_answers]"]');
let isMinAnswersEdited = minAnswersInput.value.trim() !== maxAnswersInput.value.trim();

maxAnswersInput.addEventListener('input', function() {
if (!isMinAnswersEdited) {
minAnswersInput.value = maxAnswersInput.value;
}
});

minAnswersInput.addEventListener('input', function() {
isMinAnswersEdited = true;
});
}

document.addEventListener('DOMContentLoaded', function () {
const list = document.querySelector('ul[data-collection-holder]');
list.querySelectorAll('li[data-collection-item]').forEach(addListenersToItems);

// Observer to watch for new li elements
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
console.log('mutation');
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE && node.matches('li[data-collection-item]')) {
addListenersToItems(node);
}
});
});
});

// Configuration of the observer:
const config = { childList: true };
observer.observe(list, config);
});
Loading

0 comments on commit 2781ba4

Please sign in to comment.