summaryrefslogtreecommitdiffstats
path: root/apps/comments/src/utils
diff options
context:
space:
mode:
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2020-10-04 15:33:17 +0200
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2020-10-20 13:58:06 +0200
commite7f5516b4d04c16ed2c12dcc9c9c5f34d9f1f73b (patch)
treea4c187c3dacfa5e9bf28fb97941678a515f3e571 /apps/comments/src/utils
parent3d2024faf94720125855db980d8bbebb658902d1 (diff)
downloadnextcloud-server-e7f5516b4d04c16ed2c12dcc9c9c5f34d9f1f73b.tar.gz
nextcloud-server-e7f5516b4d04c16ed2c12dcc9c9c5f34d9f1f73b.zip
Init vue comments tab
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/comments/src/utils')
-rw-r--r--apps/comments/src/utils/cancelableRequest.js62
-rw-r--r--apps/comments/src/utils/davUtils.js29
-rw-r--r--apps/comments/src/utils/fileUtils.js122
-rw-r--r--apps/comments/src/utils/numberUtil.js30
4 files changed, 243 insertions, 0 deletions
diff --git a/apps/comments/src/utils/cancelableRequest.js b/apps/comments/src/utils/cancelableRequest.js
new file mode 100644
index 00000000000..425e94a787a
--- /dev/null
+++ b/apps/comments/src/utils/cancelableRequest.js
@@ -0,0 +1,62 @@
+/**
+ * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @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 axios from '@nextcloud/axios'
+
+/**
+ * Create a cancel token
+ * @returns {CancelTokenSource}
+ */
+const createCancelToken = () => axios.CancelToken.source()
+
+/**
+ * Creates a cancelable axios 'request object'.
+ *
+ * @param {function} request the axios promise request
+ * @returns {Object}
+ */
+const cancelableRequest = function(request) {
+ /**
+ * Generate an axios cancel token
+ */
+ const cancelToken = createCancelToken()
+
+ /**
+ * Execute the request
+ *
+ * @param {string} url the url to send the request to
+ * @param {Object} [options] optional config for the request
+ */
+ const fetch = async function(url, options) {
+ return request(
+ url,
+ Object.assign({ cancelToken: cancelToken.token }, options)
+ )
+ }
+
+ return {
+ request: fetch,
+ cancel: cancelToken.cancel,
+ }
+}
+
+export default cancelableRequest
diff --git a/apps/comments/src/utils/davUtils.js b/apps/comments/src/utils/davUtils.js
new file mode 100644
index 00000000000..b10b62e4f34
--- /dev/null
+++ b/apps/comments/src/utils/davUtils.js
@@ -0,0 +1,29 @@
+/**
+ * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @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 { generateRemoteUrl } from '@nextcloud/router'
+
+const getRootPath = function() {
+ return generateRemoteUrl('dav/comments')
+}
+
+export { getRootPath }
diff --git a/apps/comments/src/utils/fileUtils.js b/apps/comments/src/utils/fileUtils.js
new file mode 100644
index 00000000000..298732c8af0
--- /dev/null
+++ b/apps/comments/src/utils/fileUtils.js
@@ -0,0 +1,122 @@
+/**
+ * @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @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 camelcase from 'camelcase'
+import { isNumber } from './numberUtil'
+
+/**
+ * Get an url encoded path
+ *
+ * @param {String} path the full path
+ * @returns {string} url encoded file path
+ */
+const encodeFilePath = function(path) {
+ const pathSections = (path.startsWith('/') ? path : `/${path}`).split('/')
+ let relativePath = ''
+ pathSections.forEach((section) => {
+ if (section !== '') {
+ relativePath += '/' + encodeURIComponent(section)
+ }
+ })
+ return relativePath
+}
+
+/**
+ * Extract dir and name from file path
+ *
+ * @param {String} path the full path
+ * @returns {String[]} [dirPath, fileName]
+ */
+const extractFilePaths = function(path) {
+ const pathSections = path.split('/')
+ const fileName = pathSections[pathSections.length - 1]
+ const dirPath = pathSections.slice(0, pathSections.length - 1).join('/')
+ return [dirPath, fileName]
+}
+
+/**
+ * Sorting comparison function
+ *
+ * @param {Object} fileInfo1 file 1 fileinfo
+ * @param {Object} fileInfo2 file 2 fileinfo
+ * @param {string} key key to sort with
+ * @param {boolean} [asc=true] sort ascending?
+ * @returns {number}
+ */
+const sortCompare = function(fileInfo1, fileInfo2, key, asc = true) {
+
+ if (fileInfo1.isFavorite && !fileInfo2.isFavorite) {
+ return -1
+ } else if (!fileInfo1.isFavorite && fileInfo2.isFavorite) {
+ return 1
+ }
+
+ // if this is a number, let's sort by integer
+ if (isNumber(fileInfo1[key]) && isNumber(fileInfo2[key])) {
+ return Number(fileInfo1[key]) - Number(fileInfo2[key])
+ }
+
+ // else we sort by string, so let's sort directories first
+ if (fileInfo1.type === 'directory' && fileInfo2.type !== 'directory') {
+ return -1
+ } else if (fileInfo1.type !== 'directory' && fileInfo2.type === 'directory') {
+ return 1
+ }
+
+ // finally sort by name
+ return asc
+ ? fileInfo1[key].localeCompare(fileInfo2[key], OC.getLanguage())
+ : -fileInfo1[key].localeCompare(fileInfo2[key], OC.getLanguage())
+}
+
+/**
+ * Generate a fileinfo object based on the full dav properties
+ * It will flatten everything and put all keys to camelCase
+ *
+ * @param {Object} obj the object
+ * @returns {Object}
+ */
+const genFileInfo = function(obj) {
+ const fileInfo = {}
+
+ Object.keys(obj).forEach(key => {
+ const data = obj[key]
+
+ // flatten object if any
+ if (!!data && typeof data === 'object' && !Array.isArray(data)) {
+ Object.assign(fileInfo, genFileInfo(data))
+ } else {
+ // format key and add it to the fileInfo
+ if (data === 'false') {
+ fileInfo[camelcase(key)] = false
+ } else if (data === 'true') {
+ fileInfo[camelcase(key)] = true
+ } else {
+ fileInfo[camelcase(key)] = isNumber(data)
+ ? Number(data)
+ : data
+ }
+ }
+ })
+ return fileInfo
+}
+
+export { encodeFilePath, extractFilePaths, sortCompare, genFileInfo }
diff --git a/apps/comments/src/utils/numberUtil.js b/apps/comments/src/utils/numberUtil.js
new file mode 100644
index 00000000000..018c34c49e8
--- /dev/null
+++ b/apps/comments/src/utils/numberUtil.js
@@ -0,0 +1,30 @@
+/**
+ * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @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/>.
+ *
+ */
+
+const isNumber = function(num) {
+ if (!num) {
+ return false
+ }
+ return Number(num).toString() === num.toString()
+}
+
+export { isNumber }