Skip to content

Commit

Permalink
Hotfix/js (#5)
Browse files Browse the repository at this point in the history
* Bind events for triggers on section widgets

* Add flag for styles

* Compile assets

* Update readme. Update version number
  • Loading branch information
mtthwmnc authored Aug 6, 2019
1 parent a631a35 commit 29ad0c3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ This bundle contains two widgets for a complete ApostropheCMS accordion widget.
The schema for `pk-accordion-section-widgets` contains a title `string` field as well as an area of widgets that you can configure yourself.

## How To Use
Declare the bundle in app.js
Declare the bundle in app.js. If you want to init the module with the basic css, set `options.styles` of `pk-accordion` to `true`

```js

bundles: [ 'pk-accordion' ],
modules: {
'pk-accordion': {},
'pk-accordion': {
styles: true
},
'pk-accordion-widgets': {},
'pk-accordion-section-widgets': {}
}
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module.exports = {
modules: ['pk-accordion-section-widgets', 'pk-accordion-widgets'],
directory: 'lib/modules'
},
afterConstruct: function (self) {
afterConstruct: (self) => {
if (self.options.styles) {
self.pushAsset('stylesheet', 'always', {
when: 'always'
});
}
self.pushAsset('script', 'always', {
when: 'always'
});
self.pushAsset('stylesheet', 'always', {
when: 'always'
});
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% set section = data.widget %}
{% set namespace = data.options.namespace %}

<div class="pk-accordion__section {% if namespace %} {{namespace}}__section {% endif %}">
<div class="pk-accordion__section {% if namespace %} {{namespace}}__section {% endif %}" data-component="pk-accordion-section">
<div class="pk-accordion__section-header {% if namespace %} {{namespace}}__section-header {% endif %}" data-role="pk-accordion-section-header">
<div class="pk-accordion__section-title {% if namespace %} {{namespace}}__section-title {% endif %}">
{%- block sectionTitle -%}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pk-accordion",
"version": "1.0.0",
"version": "1.0.1",
"description": "A bundle of accordion widgets for ApostropheCMS",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion public/js/always.js

Large diffs are not rendered by default.

22 changes: 9 additions & 13 deletions src/js/accordion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pkutils from 'pk-utilities';
const component = '[data-component=pk-accordion]';
const component = '[data-component=pk-accordion-section]';
const header = '[data-role=pk-accordion-section-header]';
const trigger = '[data-role=pk-accordion-section-trigger]';

Expand All @@ -8,21 +8,17 @@ export default {
};

function init () {
apos.define('pk-accordion-widgets', {
apos.define('pk-accordion-section-widgets', {
extend: 'apostrophe-widgets',
construct: function (self, options) {
self.play = function ($widget, data, options) {
const accordion = $widget[0].querySelector(component);
const triggers = accordion.querySelectorAll(trigger);

triggers.forEach(trigger => {
const currentTrigger = trigger;
const currentViewport = pkutils.helpers.closest(currentTrigger, header).nextElementSibling;
trigger.addEventListener('click', (event) => {
toggleSection({
trigger: currentTrigger,
viewport: currentViewport
});
const section = $widget[0].querySelector(component);
const currentTrigger = section.querySelector(trigger);
const currentViewport = pkutils.helpers.closest(currentTrigger, header).nextElementSibling;
currentTrigger.addEventListener('click', (event) => {
toggleSection({
trigger: currentTrigger,
viewport: currentViewport
});
});

Expand Down

0 comments on commit 29ad0c3

Please sign in to comment.