Skip to content

Commit bbb0370

Browse files
committed
initial commit
0 parents  commit bbb0370

File tree

8 files changed

+225
-0
lines changed

8 files changed

+225
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Kirby Conditional Blocks plugin
2+
3+
## Installation
4+
5+
### Download
6+
7+
Download and copy this repository to `/site/plugins/conditional-blocks`.
8+
9+
### Git submodule
10+
11+
```
12+
git submodule add https://github.com/rasteiner/conditionalblocks site/plugins/conditional-blocks
13+
```
14+
15+
### Composer
16+
17+
```
18+
composer require rasteiner/conditionalblocks
19+
```
20+
21+
## Blueprint Example
22+
fields:
23+
layout:
24+
type: layout
25+
layouts:
26+
- "1/1"
27+
- "1/2, 1/4, 1/4"
28+
- "1/6, 5/6"
29+
30+
requires:
31+
min:
32+
1/3:
33+
- image
34+
- video
35+
- code
36+
max:
37+
1/2: text
38+
39+
fieldsets:
40+
- text
41+
- image
42+
- video
43+
- code
44+
45+
## License
46+
47+
MIT

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "rasteiner/conditionalblocks",
3+
"description": "Disables blocks in layout fields when the column width does not match the block requirements",
4+
"type": "kirby-plugin",
5+
"license": "MIT",
6+
"homepage": "https://getkirby.com/plugins/your-id/plugin-name",
7+
"version": "0.0.0",
8+
"authors": [
9+
{
10+
"name": "Roman Steiner",
11+
"email": "[email protected]",
12+
"homepage": "https://getkirby.com/plugins/rasteiner"
13+
}
14+
],
15+
"require": {
16+
"getkirby/composer-installer": "^1.2"
17+
}
18+
}

index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Kirby\Cms\App;
4+
5+
load([
6+
'rasteiner\\conditionalBlocks\\LayoutField' => 'LayoutField.php',
7+
], __DIR__ . '/lib');
8+
9+
App::plugin('rasteiner/conditionalblocks', [
10+
'fields' => [
11+
'layout' => 'rasteiner\\conditionalBlocks\\LayoutField',
12+
]
13+
]);

lib/LayoutField.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace rasteiner\conditionalBlocks;
4+
5+
use Kirby\Form\Field\LayoutField as OriginalLayoutField;
6+
7+
class LayoutField extends OriginalLayoutField {
8+
protected $requires = null;
9+
10+
public function __construct(array $params)
11+
{
12+
$this->requires = $params['requires'] ?? null;
13+
parent::__construct($params);
14+
}
15+
16+
public function getRequires(): array {
17+
$min = $this->requires['min'] ?? [];
18+
$max = $this->requires['max'] ?? [];
19+
20+
$constraints = [];
21+
22+
foreach (['min', 'max'] as $varname) {
23+
$constraint = $$varname;
24+
25+
if(!is_array($constraint)) {
26+
$$varname = [];
27+
continue;
28+
}
29+
30+
foreach ($constraint as $columnWidth => $fields) {
31+
//check if columnWidth is a number smaller than 1 or matches the pattern "^\d/\d$"
32+
if(is_numeric($columnWidth)) {
33+
$columnWidth = floatval($columnWidth);
34+
} else if(preg_match('|^(\d)/(\d)$|', $columnWidth, $matches)) {
35+
$columnWidth = intval($matches[1]) / intval($matches[2]);
36+
} else {
37+
continue;
38+
}
39+
40+
if(is_string($fields)) {
41+
$fields = [$fields];
42+
}
43+
44+
foreach ($fields as $field) {
45+
if(!is_string($field)) {
46+
continue;
47+
}
48+
49+
$constraints[$field][$varname] = $columnWidth;
50+
}
51+
}
52+
}
53+
54+
return $constraints;
55+
}
56+
57+
public function props(): array
58+
{
59+
return array_merge(parent::props(), [
60+
'requires' => $this->getRequires(),
61+
]);
62+
}
63+
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"scripts": {
3+
"dev": "npx -y kirbyup src/index.js --watch",
4+
"build": "npx -y kirbyup src/index.js"
5+
}
6+
}

src/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
panel.plugin("rasteiner/conditionalblocks", {
2+
use: [
3+
function (Vue) {
4+
const LayoutField = Vue.component("k-layout-field").options;
5+
const BlockLayouts = LayoutField.components["k-block-layouts"];
6+
const Layout = BlockLayouts.components["k-layout"];
7+
const LayoutColumn = Layout.components["k-layout-column"];
8+
const BlockSelector = Vue.component("k-block-selector").options;
9+
10+
Vue.component("k-layout-field", {
11+
extends: LayoutField,
12+
13+
provide() {
14+
return {
15+
constraints: this.requires,
16+
};
17+
},
18+
props: {
19+
requires: {
20+
type: Object,
21+
required: false,
22+
},
23+
},
24+
});
25+
26+
LayoutColumn.provide = function () {
27+
return {
28+
width: this.width,
29+
};
30+
};
31+
32+
BlockSelector.inject = ["constraints", "width"];
33+
34+
const open = BlockSelector.methods.open;
35+
BlockSelector.methods.open = function () {
36+
open.call(this, ...arguments);
37+
38+
if (!this.constraints || !this.width) return;
39+
40+
const [num, denum] = this.width.split("/");
41+
const width = parseInt(num) / parseInt(denum);
42+
43+
const myDisabled = Object.entries(this.constraints)
44+
.filter(([_, value]) => {
45+
return (
46+
(value.min && width < value.min) ||
47+
(value.max && width > value.max)
48+
);
49+
})
50+
.map(([key, _]) => key);
51+
52+
this.disabled = [...this.disabled, ...myDisabled];
53+
};
54+
},
55+
],
56+
});

0 commit comments

Comments
 (0)