Refactor: Move personal user settings page to Vue

Signed-off-by: codewithvk <vivek.javiya@collabora.com>
This commit is contained in:
codewithvk 2025-01-16 10:32:44 +05:30
parent 705cdf624d
commit 3572c8bcbe
4 changed files with 390 additions and 212 deletions

View file

@ -0,0 +1,98 @@
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div class="doc-signing-field">
<NcTextArea :value="localValue"
:label="label"
:disabled="disabled"
:placeholder="placeholder"
:helper-text="helperText"
@update:value="handleUpdate" />
<div class="doc-signing-actions">
<NcButton type="secondary" @click="onClickSave">
{{ t('richdocuments', 'Save') }}
</NcButton>
<NcButton type="secondary" :title="t('richdocuments', 'Remove')" @click="onClickRemove">
<DeleteIcon :size="20" />
</NcButton>
</div>
</div>
</template>
<script>
import NcTextArea from '@nextcloud/vue/dist/Components/NcTextArea.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import DeleteIcon from 'vue-material-design-icons/Delete.vue'
export default {
name: 'DocSigningField',
components: {
NcTextArea,
NcButton,
DeleteIcon,
},
props: {
label: {
type: String,
required: true,
},
value: {
type: String,
default: '',
},
disabled: {
type: Boolean,
default: false,
},
placeholder: {
type: String,
default: '',
},
helperText: {
type: String,
default: '',
},
},
data() {
return {
localValue: this.value,
}
},
watch: {
value(newVal) {
this.localValue = newVal
},
},
methods: {
t(scope, text) {
return window.t ? window.t(scope, text) : text
},
handleUpdate(val) {
this.localValue = val
this.$emit('update:value', val)
},
onClickSave() {
this.$emit('save', this.localValue)
},
onClickRemove() {
this.$emit('remove')
},
},
}
</script>
<style scoped>
.doc-signing-field {
display: flex;
flex-direction: column;
}
.doc-signing-actions {
display: flex;
align-items: center;
gap: 1rem;
}
</style>

View file

@ -0,0 +1,271 @@
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcSettingsSection :name="t('richdocuments', 'Nextcloud Office')"
:description="t('richdocuments', 'Personal Settings for Nextcloud Office')"
:limit-width="true">
<!-- Template folder selection -->
<div class="template-folder-settings">
<div class="template-input-wrapper">
<NcTextField v-model="templateFolder"
:label="t('richdocuments', 'Select a template directory')"
:disabled="true" />
</div>
<NcButton id="templateSelectButton"
type="secondary"
@click="onTemplateSelectButtonClick">
<span class="icon-folder"
:title="t('richdocuments', 'Select a personal template folder')"
data-toggle="tooltip" />
</NcButton>
<NcButton id="templateResetButton"
type="secondary"
:title="t('richdocuments', 'Remove personal template folder')"
@click="resetTemplate">
<DeleteIcon :size="20" />
</NcButton>
</div>
<p>
<em>
{{ t('richdocuments', 'Templates inside of this directory will be added to the template selector of Nextcloud Office.') }}
</em>
</p>
<!-- Zotero -->
<div class="zotero-section">
<p><strong>{{ t('richdocuments', 'Zotero') }}</strong></p>
<template v-if="hasZoteroSupport">
<div class="input-wrapper">
<div class="zotero-inline">
<div class="zotero-input-wrapper">
<NcTextField id="zoteroAPIKeyField"
:value="zoteroAPIKey"
:label="t('richdocuments', 'Enter Zotero API Key')"
@update:value="setZoteroAPIKey" />
</div>
<NcButton id="zoteroAPIKeySave"
type="secondary"
@click="saveZoteroAPIKey">
{{ t('richdocuments', 'Save') }}
</NcButton>
<NcButton id="zoteroAPIKeyRemove"
type="secondary"
:title="t('richdocuments', 'Remove Zotero API Key')"
@click="resetZoteroAPI">
<DeleteIcon :size="20" />
</NcButton>
</div>
<p>
<em>
{{ t('richdocuments', 'To use Zotero specify your API key here. You can create your API key in your') }}
<a href="https://www.zotero.org/settings/keys" target="_blank">
{{ t('richdocuments', 'Zotero account API settings.') }}
</a>
</em>
</p>
</div>
</template>
<p v-else>
<em>
{{ t('richdocuments', 'This instance does not support Zotero, because the feature is missing or disabled. Please contact the administration.') }}
</em>
</p>
</div>
<!-- Document signing -->
<div class="docsign-section">
<p class="doc_sign_head">
<strong>{{ t('richdocuments', 'Document signing') }}</strong>
</p>
<template v-if="hasDocumentSigningSupport">
<div class="input-wrapper">
<!-- Document Signing Cert -->
<DocSigningField v-model="documentSigningCert"
:label="t('richdocuments', 'Enter document signing cert (in PEM format)')"
@save="val => setDocumentSigningCert(val)"
@remove="() => setDocumentSigningCert('')" />
<!-- Document Signing Key -->
<DocSigningField v-model="documentSigningKey"
:label="t('richdocuments', 'Enter document signing key')"
@save="val => setDocumentSigningKey(val)"
@remove="() => setDocumentSigningKey('')" />
<!-- Document Signing CA -->
<DocSigningField v-model="documentSigningCa"
:label="t('richdocuments', 'Enter document signing CA chain')"
@save="val => setDocumentSigningCa(val)"
@remove="() => setDocumentSigningCa('')" />
<p>
<em>
{{ t('richdocuments', 'To use document signing, specify your signing certificate, key and CA chain here.') }}
</em>
</p>
</div>
</template>
<p v-else>
<em>
{{ t('richdocuments', 'This instance does not support document signing, because the feature is missing or disabled. Please contact the administrator.') }}
</em>
</p>
</div>
</NcSettingsSection>
</template>
<script>
import { generateFilePath } from '@nextcloud/router'
import { showError, showSuccess } from '@nextcloud/dialogs'
import NcSettingsSection from '@nextcloud/vue/dist/Components/NcSettingsSection.js'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import DocSigningField from './DocSigningField.vue'
import DeleteIcon from 'vue-material-design-icons/Delete.vue'
import axios from '@nextcloud/axios'
export default {
name: 'PersonalSettings',
components: {
NcSettingsSection,
NcTextField,
NcButton,
DocSigningField,
DeleteIcon,
},
props: {
initial: {
type: Object,
required: true,
},
},
data() {
return {
templateFolder: this.initial.templateFolder || '',
hasZoteroSupport: this.initial.hasZoteroSupport || false,
zoteroAPIKey: this.initial.zoteroAPIKey || '',
hasDocumentSigningSupport: this.initial.hasDocumentSigningSupport || false,
documentSigningCert: this.initial.documentSigningCert || '',
documentSigningKey: this.initial.documentSigningKey || '',
documentSigningCa: this.initial.documentSigningCa || '',
}
},
methods: {
setZoteroAPIKey(newVal) {
this.zoteroAPIKey = newVal
},
async onTemplateSelectButtonClick() {
OC.dialogs.filepicker(
this.t('richdocuments', 'Select a personal template folder'),
async (datapath) => {
const success = await this.updateSetting({ templateFolder: datapath })
if (success) {
this.templateFolder = datapath
}
},
false,
'httpd/unix-directory',
true,
OC.dialogs.FILEPICKER_TYPE_CHOOSE,
)
},
async resetTemplate() {
const success = await this.updateSetting({ templateFolder: '' })
if (success) {
this.templateFolder = ''
}
},
async saveZoteroAPIKey() {
await this.updateSetting({ zoteroAPIKeyInput: this.zoteroAPIKey })
},
async resetZoteroAPI() {
const success = await this.updateSetting({ zoteroAPIKeyInput: '' })
if (success) {
this.zoteroAPIKey = ''
}
},
async setDocumentSigningCert(val) {
const success = await this.updateSetting({ documentSigningCertInput: val })
if (success) {
this.documentSigningCert = val
}
},
async setDocumentSigningKey(val) {
const success = await this.updateSetting({ documentSigningKeyInput: val })
if (success) {
this.documentSigningKey = val
}
},
async setDocumentSigningCa(val) {
const success = await this.updateSetting({ documentSigningCaInput: val })
if (success) {
this.documentSigningCa = val
}
},
async updateSetting(settings) {
try {
const response = await axios.post(
generateFilePath('richdocuments', 'ajax', 'personal.php'),
settings,
)
if (response.data.status === 'success') {
showSuccess(this.t('richdocuments', 'Settings saved successfully.'))
return true
} else {
showError(response.data.data.message || this.t('richdocuments', 'Failed to save settings.'))
return false
}
} catch (error) {
console.error('Error updating settings:', error)
showError(this.t('richdocuments', 'Unexpected error occurred.'))
return false
}
},
},
}
</script>
<style scoped>
.template-folder-settings {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: 2rem;
}
.template-input-wrapper {
width: 300px;
flex-shrink: 0;
}
.input-wrapper {
display: flex;
flex-direction: column;
gap: 1rem;
}
.zotero-section p {
margin-top: 5px !important;
}
.zotero-inline {
display: flex;
align-items: center;
gap: 1rem;
}
.doc_sign_head {
padding-top: 10px;
padding-bottom: 5px;
}
.msg {
display: inline-block;
margin-bottom: 1rem;
color: var(--color-warning);
}
.icon-folder::before {
content: "\1F4C1";
}
</style>

View file

@ -3,171 +3,27 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import './init-shared.js'
import Vue from 'vue'
import PersonalSettings from './components/PersonalSettings.vue'
import '../css/admin.scss'
import { generateFilePath } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
(function() {
const PersonalSettings = function() {
this.templateInput = document.getElementById('templateInputField')
this.templateSelectButton = document.getElementById('templateSelectButton')
this.templateResetButton = document.getElementById('templateResetButton')
// CSP config for webpack dynamic chunk loading
// eslint-disable-next-line
__webpack_nonce__ = btoa(OC.requestToken)
this.zoteroAPIKeyInput = document.getElementById('zoteroAPIKeyField')
this.zoteroAPIKeySaveButton = document.getElementById('zoteroAPIKeySave')
this.zoteroAPIKeyRemoveButton = document.getElementById('zoteroAPIKeyRemove')
// Correct the root of the app for chunk loading
// OC.linkTo matches the apps folders
// eslint-disable-next-line
__webpack_public_path__ = OC.linkTo('richdocuments', 'js/')
this.documentSigningCertInput = document.getElementById('documentSigningCertField')
this.documentSigningCertSaveButton = document.getElementById('documentSigningCertSave')
this.documentSigningCertRemoveButton = document.getElementById('documentSigningCertRemove')
this.documentSigningKeyInput = document.getElementById('documentSigningKeyField')
this.documentSigningKeySaveButton = document.getElementById('documentSigningKeySave')
this.documentSigningKeyRemoveButton = document.getElementById('documentSigningKeyRemove')
this.documentSigningCaInput = document.getElementById('documentSigningCaField')
this.documentSigningCaSaveButton = document.getElementById('documentSigningCaSave')
this.documentSigningCaRemoveButton = document.getElementById('documentSigningCaRemove')
Vue.prototype.t = t
Vue.prototype.n = n
Vue.prototype.OC = OC
Vue.prototype.OCA = OCA
const self = this
this.templateSelectButton.addEventListener('click', function() {
OC.dialogs.filepicker(t('richdocuments', 'Select a personal template folder'), function(datapath, returntype) {
self.updateSetting(datapath)
}, false, 'httpd/unix-directory', true, OC.dialogs.FILEPICKER_TYPE_CHOOSE)
})
const element = document.getElementById('personal-vue')
this.templateResetButton.addEventListener('click', this.resetSettings.bind(this))
this.zoteroAPIKeySaveButton.addEventListener('click', function() {
self.updateZoteroAPIKey(self.zoteroAPIKeyInput.value)
})
this.zoteroAPIKeyRemoveButton.addEventListener('click', this.resetZoteroAPI.bind(this))
this.documentSigningCertSaveButton.addEventListener('click', function() {
self.updateDocumentSigningCert(self.documentSigningCertInput.value)
})
this.documentSigningCertRemoveButton.addEventListener('click', this.resetDocumentSigningCert.bind(this))
this.documentSigningKeySaveButton.addEventListener('click', function() {
self.updateDocumentSigningKey(self.documentSigningKeyInput.value)
})
this.documentSigningKeyRemoveButton.addEventListener('click', this.resetDocumentSigningKey.bind(this))
this.documentSigningCaSaveButton.addEventListener('click', function() {
self.updateDocumentSigningCa(self.documentSigningCaInput.value)
})
this.documentSigningCaRemoveButton.addEventListener('click', this.resetDocumentSigningCa.bind(this))
}
PersonalSettings.prototype.updateSetting = function(path) {
const self = this
this._updateSetting({ templateFolder: path }, function() {
self.templateInput.value = path
}, function() {
})
}
PersonalSettings.prototype.resetSettings = function() {
const self = this
this._updateSetting({ templateFolder: '' }, function() {
self.templateInput.value = ''
}, function() {
})
}
PersonalSettings.prototype.updateZoteroAPIKey = function(key) {
const self = this
this._updateSetting({ zoteroAPIKeyInput: key }, function() {
self.zoteroAPIKeyInput.value = key
}, function() {
showError(t('richdocuments', 'Failed to update the Zotero API key'))
})
}
PersonalSettings.prototype.resetZoteroAPI = function() {
const self = this
this._updateSetting({ zoteroAPIKeyInput: '' }, function() {
self.zoteroAPIKeyInput.value = ''
}, function() {
})
}
PersonalSettings.prototype.updateDocumentSigningCert = function(ca) {
const self = this
this._updateSetting({ documentSigningCertInput: ca }, function() {
self.documentSigningCertInput.value = ca
}, function() {
showError(t('richdocuments', 'Failed to update the document signing CA chain'))
})
}
PersonalSettings.prototype.resetDocumentSigningCert = function() {
const self = this
this._updateSetting({ documentSigningCertInput: '' }, function() {
self.documentSigningCertInput.value = ''
}, function() {
})
}
PersonalSettings.prototype.updateDocumentSigningKey = function(ca) {
const self = this
this._updateSetting({ documentSigningKeyInput: ca }, function() {
self.documentSigningKeyInput.value = ca
}, function() {
showError(t('richdocuments', 'Failed to update the document signing CA chain'))
})
}
PersonalSettings.prototype.resetDocumentSigningKey = function() {
const self = this
this._updateSetting({ documentSigningKeyInput: '' }, function() {
self.documentSigningKeyInput.value = ''
}, function() {
})
}
PersonalSettings.prototype.updateDocumentSigningCa = function(ca) {
const self = this
this._updateSetting({ documentSigningCaInput: ca }, function() {
self.documentSigningCaInput.value = ca
}, function() {
showError(t('richdocuments', 'Failed to update the document signing CA chain'))
})
}
PersonalSettings.prototype.resetDocumentSigningCa = function() {
const self = this
this._updateSetting({ documentSigningCaInput: '' }, function() {
self.documentSigningCaInput.value = ''
}, function() {
})
}
PersonalSettings.prototype._updateSetting = function(data, successCallback, errorCallback) {
OC.msg.startAction('#documents-admin-msg', t('richdocuments', 'Saving …'))
const request = new XMLHttpRequest()
request.open('POST', generateFilePath('richdocuments', 'ajax', 'personal.php'), true)
request.setRequestHeader('Content-Type', 'application/json')
request.setRequestHeader('requesttoken', OC.requestToken)
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
const response = JSON.parse(request.response)
OC.msg.finishedAction('#documents-admin-msg', response)
successCallback(response)
} else {
errorCallback(this.response)
}
}
request.onerror = function() {
errorCallback(this.response)
}
request.send(JSON.stringify(data))
}
return new PersonalSettings()
})()
/* eslint-disable-next-line no-new */
new Vue({
render: h => h(PersonalSettings, { props: { initial: JSON.parse(element.dataset.initial) } }),
}).$mount('#personal-vue')

View file

@ -4,54 +4,7 @@
* SPDX-FileCopyrightText: 2013-2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
script('richdocuments', 'richdocuments-personal');
\OCP\Util::addScript('richdocuments', 'richdocuments-personal');
?>
<div class="section" id="richdocuments">
<h2>
<?php p($l->t('Nextcloud Office')) ?>
</h2>
<span id="documents-admin-msg" class="msg"></span>
<p><label for="templateInputField"><?php p($l->t('Select a template directory')); ?></label><br />
<input type="text" name="templateInputField" id="templateInputField" value="<?php p($_['templateFolder']); ?>" disabled />
<button id="templateSelectButton"><span class="icon-folder" title="<?php p($l->t('Select a personal template folder')); ?>" data-toggle="tooltip"></span></button>
<button id="templateResetButton"><span class="icon-delete" title="<?php p($l->t('Remove personal template folder')); ?>" data-toggle="tooltip"></span></button>
</p>
<p><em><?php p($l->t('Templates inside of this directory will be added to the template selector of Nextcloud Office.')); ?></em></p>
<p><strong><?php p($l->t('Zotero')) ?></strong></p>
<?php if ($_['hasZoteroSupport']) { ?>
<div class="input-wrapper">
<p><label for="zoteroAPIKeyField"><?php p($l->t('Enter Zotero API Key')); ?></label><br />
<input type="text" name="zoteroAPIKeyField" id="zoteroAPIKeyField" value="<?php p($_['zoteroAPIKey']); ?>"/>
<button id="zoteroAPIKeySave"><span title="<?php p($l->t('Save Zotero API key')); ?>" data-toggle="tooltip">Save</span></button>
<button id="zoteroAPIKeyRemove"><span class="icon-delete" title="<?php p($l->t('Remove Zotero API Key')); ?>" data-toggle="tooltip"></span></button>
</p>
<p><em><?php p($l->t('To use Zotero specify your API key here. You can create your API key in your')); ?> <a href="https://www.zotero.org/settings/keys" target="_blank"><?php p($l->t('Zotero account API settings.')); ?></a></em></p>
</div>
<?php } else { ?>
<p><em><?php p($l->t('This instance does not support Zotero, because the feature is missing or disabled. Please contact the administration.')); ?></em></p>
<?php } ?>
<p><strong><?php p($l->t('Document signing')) ?></strong></p>
<?php if ($_['hasDocumentSigningSupport']) { ?>
<div class="input-wrapper">
<p><label for="documentSigningCertField"><?php p($l->t('Enter document signing cert (in PEM format)')); ?></label><br />
<textarea type="text" name="documentSigningCertField" id="documentSigningCertField"><?php p($_['documentSigningCert']); ?></textarea><br />
<button id="documentSigningCertSave"><span title="<?php p($l->t('Save document signing cert')); ?>" data-toggle="tooltip">Save</span></button>
<button id="documentSigningCertRemove"><span class="icon-delete" title="<?php p($l->t('Remove document signing cert')); ?>" data-toggle="tooltip"></span></button>
</p>
<p><label for="documentSigningKeyField"><?php p($l->t('Enter document signing key')); ?></label><br />
<textarea type="text" name="documentSigningKeyField" id="documentSigningKeyField"><?php p($_['documentSigningKey']); ?></textarea><br />
<button id="documentSigningKeySave"><span title="<?php p($l->t('Save document signing key')); ?>" data-toggle="tooltip">Save</span></button>
<button id="documentSigningKeyRemove"><span class="icon-delete" title="<?php p($l->t('Remove document signing key')); ?>" data-toggle="tooltip"></span></button>
</p>
<p><label for="documentSigningCaField"><?php p($l->t('Enter document signing CA chain')); ?></label><br />
<textarea type="text" name="documentSigningCaField" id="documentSigningCaField"><?php p($_['documentSigningCa']); ?></textarea><br />
<button id="documentSigningCaSave"><span title="<?php p($l->t('Save document signing CA chain')); ?>" data-toggle="tooltip">Save</span></button>
<button id="documentSigningCaRemove"><span class="icon-delete" title="<?php p($l->t('Remove document signing CA chain')); ?>" data-toggle="tooltip"></span></button>
</p>
<p><em><?php p($l->t('To use document signing, specify your signing certificate, key and CA chain here.')); ?></em></p>
</div>
<?php } else { ?>
<p><em><?php p($l->t('This instance does not support document signing, because the feature is missing or disabled. Please contact the administrator.')); ?></em></p>
<?php } ?>
</div>
</div>
<div id="personal-vue" data-initial="<?php p(json_encode($_, true)); ?>"></div>