Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/richdocuments.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2020-05-13 16:42:50 +0300
committerJulius Härtl <jus@bitgrid.net>2020-10-13 15:35:54 +0300
commitc38a459ce83a95ed3c2f93d4a7df930bbadab809 (patch)
tree7f2dbfadc9c65a4302f469ebe1a8fda070e37462 /src
parenta9fd8985e8b033425871e9186fb86a5d66aed7aa (diff)
Move preload and new file menu to separate files
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'src')
-rw-r--r--src/files.js227
-rw-r--r--src/services/preload.js44
-rw-r--r--src/view/NewFileMenu.js192
3 files changed, 254 insertions, 209 deletions
diff --git a/src/files.js b/src/files.js
index a9fadf91..82ec170a 100644
--- a/src/files.js
+++ b/src/files.js
@@ -1,35 +1,18 @@
import { getDocumentUrlFromTemplate, getDocumentUrlForPublicFile, getDocumentUrlForFile, getSearchParam } from './helpers/url'
import PostMessageService from './services/postMessage'
import Config from './services/config'
+import Preload from './services/preload'
import Types from './helpers/types'
import FilesAppIntegration from './view/FilesAppIntegration'
import '../css/viewer.scss'
import { splitPath } from './helpers'
+import NewFileMenu from './view/NewFileMenu'
const FRAME_DOCUMENT = 'FRAME_DOCUMENT'
const PostMessages = new PostMessageService({
FRAME_DOCUMENT: () => document.getElementById('richdocumentsframe').contentWindow
})
-const preloadCreate = getSearchParam('richdocuments_create')
-const preloadOpen = getSearchParam('richdocuments_open')
-const Preload = {}
-
-if (preloadCreate) {
- Preload.create = {
- type: getSearchParam('richdocuments_create'),
- filename: getSearchParam('richdocuments_filename')
- }
-}
-
-if (preloadOpen) {
- Preload.open = {
- filename: preloadOpen,
- id: getSearchParam('richdocuments_fileId'),
- dir: getSearchParam('dir')
- }
-}
-
const isDownloadHidden = document.getElementById('hideDownload') && document.getElementById('hideDownload').value === 'true'
const isPublic = document.getElementById('isPublic') && document.getElementById('isPublic').value === '1'
@@ -216,195 +199,6 @@ const odfViewer = {
FilesAppIntegration.close()
},
-
- registerFilesMenu: function() {
-
- const registerFilesMenu = (OCA) => {
- OCA.FilesLOMenu = {
- attach: function(newFileMenu) {
- var self = this
- const document = Types.getFileType('document')
- const spreadsheet = Types.getFileType('spreadsheet')
- const presentation = Types.getFileType('presentation')
-
- newFileMenu.addMenuEntry({
- id: 'add-' + document.extension,
- displayName: t('richdocuments', 'New Document'),
- templateName: t('richdocuments', 'New Document') + '.' + document.extension,
- iconClass: 'icon-filetype-document',
- fileType: 'x-office-document',
- actionHandler: function(filename) {
- if (OC.getCapabilities().richdocuments.templates) {
- self._openTemplatePicker('document', document.mime, filename)
- } else {
- self._createDocument(document.mime, filename)
- }
- }
- })
-
- newFileMenu.addMenuEntry({
- id: 'add-' + spreadsheet.extension,
- displayName: t('richdocuments', 'New Spreadsheet'),
- templateName: t('richdocuments', 'New Spreadsheet') + '.' + spreadsheet.extension,
- iconClass: 'icon-filetype-spreadsheet',
- fileType: 'x-office-spreadsheet',
- actionHandler: function(filename) {
- if (OC.getCapabilities().richdocuments.templates) {
- self._openTemplatePicker('spreadsheet', spreadsheet.mime, filename)
- } else {
- self._createDocument(spreadsheet.mime, filename)
- }
- }
- })
-
- newFileMenu.addMenuEntry({
- id: 'add-' + presentation.extension,
- displayName: t('richdocuments', 'New Presentation'),
- templateName: t('richdocuments', 'New Presentation') + '.' + presentation.extension,
- iconClass: 'icon-filetype-presentation',
- fileType: 'x-office-presentation',
- actionHandler: function(filename) {
- if (OC.getCapabilities().richdocuments.templates) {
- self._openTemplatePicker('presentation', presentation.mime, filename)
- } else {
- self._createDocument(presentation.mime, filename)
- }
- }
- })
- },
-
- _createDocument: function(mimetype, filename) {
- OCA.Files.Files.isFileNameValid(filename)
- filename = FileList.getUniqueName(filename)
-
- $.post(
- OC.generateUrl('apps/richdocuments/ajax/documents/create'),
- { mimetype: mimetype, filename: filename, dir: document.getElementById('dir').value },
- function(response) {
- if (response && response.status === 'success') {
- FileList.add(response.data, { animate: true, scrollTo: true })
- } else {
- OC.dialogs.alert(response.data.message, t('core', 'Could not create file'))
- }
- }
- )
- },
-
- _createDocumentFromTemplate: function(templateId, mimetype, filename) {
- OCA.Files.Files.isFileNameValid(filename)
- filename = FileList.getUniqueName(filename)
- $.post(
- OC.generateUrl('apps/richdocuments/ajax/documents/create'),
- { mimetype: mimetype, filename: filename, dir: document.getElementById('dir').value },
- function(response) {
- if (response && response.status === 'success') {
- FileList.add(response.data, { animate: false, scrollTo: false })
- const path = document.getElementById('dir').value + '/' + filename
- OCA.RichDocuments.openWithTemplate({
- fileId: -1,
- path,
- templateId: templateId
- })
- } else {
- OC.dialogs.alert(response.data.message, t('core', 'Could not create file'))
- }
- }
- )
- },
-
- _openTemplatePicker: function(type, mimetype, filename) {
- var self = this
- $.ajax({
- url: OC.linkToOCS('apps/richdocuments/api/v1/templates', 2) + type,
- dataType: 'json'
- }).then(function(response) {
- if (response.ocs.data.length === 1) {
- const { id } = response.ocs.data[0]
- self._createDocumentFromTemplate(id, mimetype, filename)
- return
- }
- self._buildTemplatePicker(response.ocs.data)
- .then(function() {
- var buttonlist = [{
- text: t('core', 'Cancel'),
- classes: 'cancel',
- click: function() {
- $(this).ocdialog('close')
- }
- }, {
- text: t('richdocuments', 'Create'),
- classes: 'primary',
- click: function() {
- var templateId = this.dataset.templateId
- self._createDocumentFromTemplate(templateId, mimetype, filename)
- $(this).ocdialog('close')
- }
- }]
-
- $('#template-picker').ocdialog({
- closeOnEscape: true,
- modal: true,
- buttons: buttonlist
- })
- })
- })
- },
-
- _buildTemplatePicker: function(data) {
- var self = this
- return $.get(OC.filePath('richdocuments', 'templates', 'templatePicker.html'), function(tmpl) {
- var $tmpl = $(tmpl)
- // init template picker
- var $dlg = $tmpl.octemplate({
- dialog_name: 'template-picker',
- dialog_title: t('richdocuments', 'Select template')
- })
-
- // create templates list
- var templates = _.values(data)
- templates.forEach(function(template) {
- self._appendTemplateFromData($dlg[0], template)
- })
-
- $('body').append($dlg)
- })
- },
-
- _appendTemplateFromData: function(dlg, data) {
- var template = dlg.querySelector('.template-model').cloneNode(true)
- template.className = ''
- template.querySelector('img').src = OC.generateUrl('apps/richdocuments/template/preview/' + data.id)
- template.querySelector('h2').textContent = data.name
- template.onclick = function() {
- dlg.dataset.templateId = data.id
- }
- if (!dlg.dataset.templateId) {
- dlg.dataset.templateId = data.id
- }
-
- dlg.querySelector('.template-container').appendChild(template)
- }
- }
- }
- registerFilesMenu(OCA)
-
- OC.Plugins.register('OCA.Files.NewFileMenu', OCA.FilesLOMenu)
-
- // Open the template picker if there was a create parameter detected on load
- if (Preload.create && Preload.create.type && Preload.create.filename) {
- const fileType = Types.getFileType(Preload.create.type, Config.get('ooxml'))
- OCA.FilesLOMenu._openTemplatePicker(Preload.create.type, fileType.mime, Preload.create.filename + '.' + fileType.extension)
- }
-
- if (Preload.open) {
- FileList.$fileList.one('updated', function() {
- odfViewer.onEdit(Preload.open.filename, {
- fileId: Preload.open.id,
- dir: document.getElementById('dir').value
- })
- })
- }
- }
}
const settings = OC.getCapabilities()['richdocuments']['config'] || {}
@@ -448,7 +242,22 @@ $(document).ready(function() {
}
odfViewer.registerFileActions()
- odfViewer.registerFilesMenu()
+ OC.Plugins.register('OCA.Files.NewFileMenu', NewFileMenu)
+ }
+
+ // Open the template picker if there was a create parameter detected on load
+ if (Preload.create && Preload.create.type && Preload.create.filename) {
+ const fileType = Types.getFileType(Preload.create.type, Config.get('ooxml'))
+ NewFileMenu._openTemplatePicker(Preload.create.type, fileType.mime, Preload.create.filename + '.' + fileType.extension)
+ }
+
+ if (Preload.open) {
+ FileList.$fileList.one('updated', function() {
+ odfViewer.onEdit(Preload.open.filename, {
+ fileId: Preload.open.id,
+ dir: document.getElementById('dir').value
+ })
+ })
}
// Open documents if a public page is opened for a supported mimetype
diff --git a/src/services/preload.js b/src/services/preload.js
new file mode 100644
index 00000000..4b8c02bc
--- /dev/null
+++ b/src/services/preload.js
@@ -0,0 +1,44 @@
+/*
+ * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
+ *
+ * @author Julius Härtl <jus@bitgrid.net>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+import { getSearchParam } from '../helpers/url'
+
+const preloadCreate = getSearchParam('richdocuments_create')
+const preloadOpen = getSearchParam('richdocuments_open')
+const Preload = {}
+
+if (preloadCreate) {
+ Preload.create = {
+ type: getSearchParam('richdocuments_create'),
+ filename: getSearchParam('richdocuments_filename')
+ }
+}
+
+if (preloadOpen) {
+ Preload.open = {
+ filename: preloadOpen,
+ id: getSearchParam('richdocuments_fileId'),
+ dir: getSearchParam('dir')
+ }
+}
+
+export default Preload
diff --git a/src/view/NewFileMenu.js b/src/view/NewFileMenu.js
new file mode 100644
index 00000000..5a6cb3a5
--- /dev/null
+++ b/src/view/NewFileMenu.js
@@ -0,0 +1,192 @@
+/*
+ * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
+ *
+ * @author Julius Härtl <jus@bitgrid.net>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+import Types from '../helpers/types'
+
+/** @type OC.Plugin */
+const NewFileMenu = {
+ attach: function(newFileMenu) {
+ var self = this
+ const document = Types.getFileType('document')
+ const spreadsheet = Types.getFileType('spreadsheet')
+ const presentation = Types.getFileType('presentation')
+
+ newFileMenu.addMenuEntry({
+ id: 'add-' + document.extension,
+ displayName: t('richdocuments', 'New Document'),
+ templateName: t('richdocuments', 'New Document') + '.' + document.extension,
+ iconClass: 'icon-filetype-document',
+ fileType: 'x-office-document',
+ actionHandler: function(filename) {
+ if (OC.getCapabilities().richdocuments.templates) {
+ self._openTemplatePicker('document', document.mime, filename)
+ } else {
+ self._createDocument(document.mime, filename)
+ }
+ }
+ })
+
+ newFileMenu.addMenuEntry({
+ id: 'add-' + spreadsheet.extension,
+ displayName: t('richdocuments', 'New Spreadsheet'),
+ templateName: t('richdocuments', 'New Spreadsheet') + '.' + spreadsheet.extension,
+ iconClass: 'icon-filetype-spreadsheet',
+ fileType: 'x-office-spreadsheet',
+ actionHandler: function(filename) {
+ if (OC.getCapabilities().richdocuments.templates) {
+ self._openTemplatePicker('spreadsheet', spreadsheet.mime, filename)
+ } else {
+ self._createDocument(spreadsheet.mime, filename)
+ }
+ }
+ })
+
+ newFileMenu.addMenuEntry({
+ id: 'add-' + presentation.extension,
+ displayName: t('richdocuments', 'New Presentation'),
+ templateName: t('richdocuments', 'New Presentation') + '.' + presentation.extension,
+ iconClass: 'icon-filetype-presentation',
+ fileType: 'x-office-presentation',
+ actionHandler: function(filename) {
+ if (OC.getCapabilities().richdocuments.templates) {
+ self._openTemplatePicker('presentation', presentation.mime, filename)
+ } else {
+ self._createDocument(presentation.mime, filename)
+ }
+ }
+ })
+ },
+
+ _createDocument: function(mimetype, filename) {
+ OCA.Files.Files.isFileNameValid(filename)
+ filename = FileList.getUniqueName(filename)
+
+ $.post(
+ OC.generateUrl('apps/richdocuments/ajax/documents/create'),
+ { mimetype: mimetype, filename: filename, dir: document.getElementById('dir').value },
+ function(response) {
+ if (response && response.status === 'success') {
+ FileList.add(response.data, { animate: true, scrollTo: true })
+ } else {
+ OC.dialogs.alert(response.data.message, t('core', 'Could not create file'))
+ }
+ }
+ )
+ },
+
+ _createDocumentFromTemplate: function(templateId, mimetype, filename) {
+ OCA.Files.Files.isFileNameValid(filename)
+ filename = FileList.getUniqueName(filename)
+ $.post(
+ OC.generateUrl('apps/richdocuments/ajax/documents/create'),
+ { mimetype: mimetype, filename: filename, dir: document.getElementById('dir').value },
+ function(response) {
+ if (response && response.status === 'success') {
+ FileList.add(response.data, { animate: false, scrollTo: false })
+ const path = document.getElementById('dir').value + '/' + filename
+ OCA.RichDocuments.openWithTemplate({
+ fileId: -1,
+ path,
+ templateId: templateId
+ })
+ } else {
+ OC.dialogs.alert(response.data.message, t('core', 'Could not create file'))
+ }
+ }
+ )
+ },
+
+ _openTemplatePicker: function(type, mimetype, filename) {
+ var self = this
+ $.ajax({
+ url: OC.linkToOCS('apps/richdocuments/api/v1/templates', 2) + type,
+ dataType: 'json'
+ }).then(function(response) {
+ if (response.ocs.data.length === 1) {
+ const { id } = response.ocs.data[0]
+ self._createDocumentFromTemplate(id, mimetype, filename)
+ return
+ }
+ self._buildTemplatePicker(response.ocs.data)
+ .then(function() {
+ var buttonlist = [{
+ text: t('core', 'Cancel'),
+ classes: 'cancel',
+ click: function() {
+ $(this).ocdialog('close')
+ }
+ }, {
+ text: t('richdocuments', 'Create'),
+ classes: 'primary',
+ click: function() {
+ var templateId = this.dataset.templateId
+ self._createDocumentFromTemplate(templateId, mimetype, filename)
+ $(this).ocdialog('close')
+ }
+ }]
+
+ $('#template-picker').ocdialog({
+ closeOnEscape: true,
+ modal: true,
+ buttons: buttonlist
+ })
+ })
+ })
+ },
+
+ _buildTemplatePicker: function(data) {
+ var self = this
+ return $.get(OC.filePath('richdocuments', 'templates', 'templatePicker.html'), function(tmpl) {
+ var $tmpl = $(tmpl)
+ // init template picker
+ var $dlg = $tmpl.octemplate({
+ dialog_name: 'template-picker',
+ dialog_title: t('richdocuments', 'Select template')
+ })
+
+ // create templates list
+ var templates = _.values(data)
+ templates.forEach(function(template) {
+ self._appendTemplateFromData($dlg[0], template)
+ })
+
+ $('body').append($dlg)
+ })
+ },
+
+ _appendTemplateFromData: function(dlg, data) {
+ var template = dlg.querySelector('.template-model').cloneNode(true)
+ template.className = ''
+ template.querySelector('img').src = OC.generateUrl('apps/richdocuments/template/preview/' + data.id)
+ template.querySelector('h2').textContent = data.name
+ template.onclick = function() {
+ dlg.dataset.templateId = data.id
+ }
+ if (!dlg.dataset.templateId) {
+ dlg.dataset.templateId = data.id
+ }
+
+ dlg.querySelector('.template-container').appendChild(template)
+ }
+}
+
+export default NewFileMenu