Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(templates): checkbox field type #47290

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
30 changes: 20 additions & 10 deletions apps/files/src/components/TemplateFiller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
<form>
<h3>{{ t('files', 'Fill template fields') }}</h3>

<!-- We will support more than just text fields in the future -->
<div v-for="field in fields" :key="field.index">
<TemplateTextField v-if="field.type == 'rich-text'"
:field="field"
@input="trackInput" />
<component :is="getFieldComponent(field.type)" :field="field" @input="trackInput" />
</div>
</form>
</div>
Expand All @@ -29,11 +26,12 @@
</NcModal>
</template>

<script lang="ts">
<script>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why reverting to JS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't using TypeScript in this file, but if it makes it more consistent with the rest of the files I can leave it as TS.

import { defineComponent } from 'vue'
import { NcModal, NcButton, NcLoadingIcon } from '@nextcloud/vue'
import { translate as t } from '@nextcloud/l10n'
import TemplateTextField from './TemplateFiller/TemplateTextField.vue'
import TemplateRichTextField from './TemplateFiller/TemplateRichTextField.vue'
import TemplateCheckboxField from './TemplateFiller/TemplateCheckboxField.vue'

export default defineComponent({
name: 'TemplateFiller',
Expand All @@ -42,7 +40,8 @@ export default defineComponent({
NcModal,
NcButton,
NcLoadingIcon,
TemplateTextField,
TemplateRichTextField,
TemplateCheckboxField,
},

props: {
Expand All @@ -65,10 +64,21 @@ export default defineComponent({

methods: {
t,
trackInput([value, index]) {
this.localFields[index] = {
content: value,
trackInput({ index, property, value }) {
if (!this.localFields[index]) {
this.localFields[index] = {}
}

this.localFields[index][property] = value
},
getFieldComponent(fieldType) {
const fieldComponentType = fieldType.split('-')
.map((str) => {
return str.charAt(0).toUpperCase() + str.slice(1)
})
.join('')

return `Template${fieldComponentType}Field`
},
async submit() {
this.loading = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
<div class="template-field__checkbox">
<NcCheckboxRadioSwitch :id="fieldId"
:checked.sync="value"
type="switch"
@update:checked="input">
{{ fieldLabel }}
</NcCheckboxRadioSwitch>
</div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { NcCheckboxRadioSwitch } from '@nextcloud/vue'

export default defineComponent({
name: 'TemplateCheckboxField',

components: {
NcCheckboxRadioSwitch,
},

props: {
field: {
type: Object,
default: () => {},
},
},

data() {
return {
value: this.field.checked ?? false,
}
},

computed: {
fieldLabel() {
const label = this.field.name ?? this.field.alias ?? 'Unknown field'

return label.charAt(0).toUpperCase() + label.slice(1)
},
fieldId() {
return 'checkbox-field' + this.field.index
},
},

methods: {
input() {
this.$emit('input', {
index: this.field.index,
property: 'checked',
value: this.value,
})
},
},
})
</script>

<style lang="scss" scoped>
.template-field__checkbox {
margin: 20px 0;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
:label="fieldLabel"
:label-outside="true"
:placeholder="field.content"
@input="$emit('input', [value, field.index])" />
@input="input" />
</div>
</template>

Expand All @@ -24,7 +24,7 @@ import { defineComponent } from 'vue'
import { NcTextField } from '@nextcloud/vue'

export default defineComponent({
name: 'TemplateTextField',
name: 'TemplateRichTextField',

components: {
NcTextField,
Expand Down Expand Up @@ -53,6 +53,16 @@ export default defineComponent({
return 'text-field' + this.field.index
},
},

methods: {
input() {
this.$emit('input', {
index: this.field.index,
property: 'content',
value: this.value,
})
},
},
})
</script>

Expand Down
2 changes: 2 additions & 0 deletions dist/3235-3235.js

Large diffs are not rendered by default.

File renamed without changes.
1 change: 1 addition & 0 deletions dist/3235-3235.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/3235-3235.js.map.license
2 changes: 0 additions & 2 deletions dist/6113-6113.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/6113-6113.js.map

This file was deleted.

1 change: 0 additions & 1 deletion dist/6113-6113.js.map.license

This file was deleted.

4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@
'OCP\\Files\\Storage\\IWriteStreamStorage' => $baseDir . '/lib/public/Files/Storage/IWriteStreamStorage.php',
'OCP\\Files\\Template\\BeforeGetTemplatesEvent' => $baseDir . '/lib/public/Files/Template/BeforeGetTemplatesEvent.php',
'OCP\\Files\\Template\\Field' => $baseDir . '/lib/public/Files/Template/Field.php',
'OCP\\Files\\Template\\FieldFactory' => $baseDir . '/lib/public/Files/Template/FieldFactory.php',
'OCP\\Files\\Template\\FieldType' => $baseDir . '/lib/public/Files/Template/FieldType.php',
'OCP\\Files\\Template\\Fields\\CheckBoxField' => $baseDir . '/lib/public/Files/Template/Fields/CheckBoxField.php',
'OCP\\Files\\Template\\Fields\\RichTextField' => $baseDir . '/lib/public/Files/Template/Fields/RichTextField.php',
'OCP\\Files\\Template\\FileCreatedFromTemplateEvent' => $baseDir . '/lib/public/Files/Template/FileCreatedFromTemplateEvent.php',
'OCP\\Files\\Template\\ICustomTemplateProvider' => $baseDir . '/lib/public/Files/Template/ICustomTemplateProvider.php',
'OCP\\Files\\Template\\ITemplateManager' => $baseDir . '/lib/public/Files/Template/ITemplateManager.php',
Expand Down
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Files\\Storage\\IWriteStreamStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IWriteStreamStorage.php',
'OCP\\Files\\Template\\BeforeGetTemplatesEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Template/BeforeGetTemplatesEvent.php',
'OCP\\Files\\Template\\Field' => __DIR__ . '/../../..' . '/lib/public/Files/Template/Field.php',
'OCP\\Files\\Template\\FieldFactory' => __DIR__ . '/../../..' . '/lib/public/Files/Template/FieldFactory.php',
'OCP\\Files\\Template\\FieldType' => __DIR__ . '/../../..' . '/lib/public/Files/Template/FieldType.php',
'OCP\\Files\\Template\\Fields\\CheckBoxField' => __DIR__ . '/../../..' . '/lib/public/Files/Template/Fields/CheckBoxField.php',
'OCP\\Files\\Template\\Fields\\RichTextField' => __DIR__ . '/../../..' . '/lib/public/Files/Template/Fields/RichTextField.php',
'OCP\\Files\\Template\\FileCreatedFromTemplateEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Template/FileCreatedFromTemplateEvent.php',
'OCP\\Files\\Template\\ICustomTemplateProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Template/ICustomTemplateProvider.php',
'OCP\\Files\\Template\\ITemplateManager' => __DIR__ . '/../../..' . '/lib/public/Files/Template/ITemplateManager.php',
Expand Down
30 changes: 14 additions & 16 deletions lib/public/Files/Template/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,35 @@
/**
* @since 30.0.0
*/
class Field implements \JsonSerializable {
private string $index;
private string $content;
private FieldType $type;
private ?string $alias;
private ?int $id;
private ?string $tag;
abstract class Field implements \JsonSerializable {
public ?string $alias = null;
public ?string $tag = null;
public ?int $id = null;

/**
* @since 30.0.0
*/
public function __construct(string $index, string $content, FieldType $type, ?string $alias = null, ?int $id = null, ?string $tag = null) {
$this->index = $index;
$this->alias = $alias;
$this->type = $type;
$this->id = $id;
$this->tag = $tag;
$this->content = $content;
public function __construct(
elzody marked this conversation as resolved.
Show resolved Hide resolved
private string $index,
private FieldType $type
) {
}

/**
* @since 30.0.0
*/
abstract public function setValue(mixed $value): void;

/**
* @since 30.0.0
*/
public function jsonSerialize(): array {
return [
'index' => $this->index,
'content' => $this->content,
'type' => $this->type->value,
'alias' => $this->alias,
'id' => $this->id,
'tag' => $this->tag,
'id' => $this->id,
];
}
}
32 changes: 32 additions & 0 deletions lib/public/Files/Template/FieldFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\Files\Template;

use OCP\Files\Template\Fields\CheckBoxField;
use OCP\Files\Template\Fields\RichTextField;

/**
* @since 30.0.0
*/
class FieldFactory {
/**
* @since 30.0.0
*/
public static function createField(
string $index,
FieldType $type
): Field {
return match ($type) {
FieldType::RichText => new RichTextField($index, $type),
FieldType::CheckBox => new CheckBoxField($index, $type),
default => throw new InvalidFieldTypeException(),
};
}
}
47 changes: 47 additions & 0 deletions lib/public/Files/Template/Fields/CheckBoxField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\Files\Template\Fields;

use OCP\Files\Template\Field;
use OCP\Files\Template\FieldType;

/**
* @since 30.0.0
*/
class CheckBoxField extends Field {
private bool $checked = false;

/**
* @since 30.0.0
*/
public function __construct(string $index, FieldType $type) {
parent::__construct($index, $type);
}

/**
* @since 30.0.0
*/
public function setValue(mixed $value): void {
if (!is_bool($value)) {
throw new \Exception('Invalid value for checkbox field type');
}

$this->checked = $value;
}

/**
* @since 30.0.0
*/
public function jsonSerialize(): array {
$jsonProperties = parent::jsonSerialize();

return array_merge($jsonProperties, ['checked' => $this->checked]);
}
}
47 changes: 47 additions & 0 deletions lib/public/Files/Template/Fields/RichTextField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\Files\Template\Fields;

use OCP\Files\Template\Field;
use OCP\Files\Template\FieldType;

/**
* @since 30.0.0
*/
class RichTextField extends Field {
private string $content = '';

/**
* @since 30.0.0
*/
public function __construct(string $index, FieldType $type) {
parent::__construct($index, $type);
}

/**
* @since 30.0.0
*/
public function setValue(mixed $value): void {
if (!is_string($value)) {
throw new \Exception('Invalid value for rich-text field type');
}

$this->content = $value;
}

/**
* @since 30.0.0
*/
public function jsonSerialize(): array {
$jsonProperties = parent::jsonSerialize();

return array_merge($jsonProperties, ['content' => $this->content]);
}
}
Loading