Move path helpers to the bundle

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2019-05-13 16:37:38 +02:00
parent 74ad4cce83
commit e3ae7dc115
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8
8 changed files with 211 additions and 221 deletions

36
core/js/dist/login.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

100
core/js/dist/main.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -81,39 +81,6 @@ Object.assign(window.OC, {
return OC._capabilities;
},
/**
* get the absolute path to an image file
* if no extension is given for the image, it will automatically decide
* between .png and .svg based on what the browser supports
* @param {string} app the app id to which the image belongs
* @param {string} file the name of the image file
* @return {string}
*/
imagePath:function(app,file){
if(file.indexOf('.')==-1){//if no extension is given, use svg
file+='.svg';
}
return OC.filePath(app,'img',file);
},
/**
* URI-Encodes a file path but keep the path slashes.
*
* @param path path
* @return encoded path
*/
encodePath: function(path) {
if (!path) {
return path;
}
var parts = path.split('/');
var result = [];
for (var i = 0; i < parts.length; i++) {
result.push(encodeURIComponent(parts[i]));
}
return result.join('/');
},
/**
* Loads translations for the given app asynchronously.
*
@ -125,102 +92,6 @@ Object.assign(window.OC, {
return OC.L10N.load(app, callback);
},
/**
* Returns the base name of the given path.
* For example for "/abc/somefile.txt" it will return "somefile.txt"
*
* @param {String} path
* @return {String} base name
*/
basename: function(path) {
return path.replace(/\\/g,'/').replace( /.*\//, '' );
},
/**
* Returns the dir name of the given path.
* For example for "/abc/somefile.txt" it will return "/abc"
*
* @param {String} path
* @return {String} dir name
*/
dirname: function(path) {
return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
},
/**
* Returns whether the given paths are the same, without
* leading, trailing or doubled slashes and also removing
* the dot sections.
*
* @param {String} path1 first path
* @param {String} path2 second path
* @return {bool} true if the paths are the same
*
* @since 9.0
*/
isSamePath: function(path1, path2) {
var filterDot = function(p) {
return p !== '.';
};
var pathSections1 = _.filter((path1 || '').split('/'), filterDot);
var pathSections2 = _.filter((path2 || '').split('/'), filterDot);
path1 = OC.joinPaths.apply(OC, pathSections1);
path2 = OC.joinPaths.apply(OC, pathSections2);
return path1 === path2;
},
/**
* Join path sections
*
* @param {...String} path sections
*
* @return {String} joined path, any leading or trailing slash
* will be kept
*
* @since 8.2
*/
joinPaths: function() {
if (arguments.length < 1) {
return '';
}
var path = '';
// convert to array
var args = Array.prototype.slice.call(arguments);
// discard empty arguments
args = _.filter(args, function(arg) {
return arg.length > 0;
});
if (args.length < 1) {
return '';
}
var lastArg = args[args.length - 1];
var leadingSlash = args[0].charAt(0) === '/';
var trailingSlash = lastArg.charAt(lastArg.length - 1) === '/';
var sections = [];
var i;
for (i = 0; i < args.length; i++) {
sections = sections.concat(args[i].split('/'));
}
var first = !leadingSlash;
for (i = 0; i < sections.length; i++) {
if (sections[i] !== '') {
if (first) {
first = false;
} else {
path += '/';
}
path += sections[i];
}
}
if (trailingSlash) {
// add it back
path += '/';
}
return path;
},
/**
* Parses a URL query string into a JS map
* @param {string} queryString query string in the format param1=1234&param2=abcde&param3=xyz

View File

@ -24,6 +24,13 @@ import Apps from './apps'
import {AppConfig, appConfig} from './appconfig'
import appswebroots from './appswebroots'
import Backbone from './backbone'
import {
basename,
dirname,
encodePath,
isSamePath,
joinPaths,
} from './path'
import Config from './config'
import {
coreApps,
@ -55,9 +62,10 @@ import {
import {isUserAdmin} from './admin'
import L10N from './l10n'
import {
filePath,
generateUrl,
getRootPath,
filePath,
imagePath,
linkTo,
linkToOCS,
linkToRemote,
@ -125,6 +133,15 @@ export default {
showMenu,
unregisterMenu,
/*
* Path helpers
*/
basename,
encodePath,
dirname,
isSamePath,
joinPaths,
msg,
Notification,
PasswordConfirmation,
@ -132,11 +149,12 @@ export default {
search,
Util,
debug,
filePath,
generateUrl,
get: get(window),
set: set(window),
getRootPath,
filePath,
imagePath,
redirect,
reload,
requestToken: getRequestToken(),

123
core/src/OC/path.js Normal file
View File

@ -0,0 +1,123 @@
/*
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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/>.
*/
/**
* URI-Encodes a file path but keep the path slashes.
*
* @param {String} path
* @return {String} encoded path
*/
export const encodePath = path => {
if (!path) {
return path
}
const parts = path.split('/')
const result = []
for (let i = 0; i < parts.length; i++) {
result.push(encodeURIComponent(parts[i]))
}
return result.join('/')
}
/**
* Returns the base name of the given path.
* For example for "/abc/somefile.txt" it will return "somefile.txt"
*
* @param {String} path
* @return {String} base name
*/
export const basename = path => path.replace(/\\/g, '/').replace(/.*\//, '')
/**
* Returns the dir name of the given path.
* For example for "/abc/somefile.txt" it will return "/abc"
*
* @param {String} path
* @return {String} dir name
*/
export const dirname = path => path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '')
/**
* Returns whether the given paths are the same, without
* leading, trailing or doubled slashes and also removing
* the dot sections.
*
* @param {String} path1 first path
* @param {String} path2 second path
* @return {bool} true if the paths are the same
*
* @since 9.0
*/
export const isSamePath = (path1, path2) => {
const pathSections1 = (path1 || '').split('/').filter(p => p !== '.')
const pathSections2 = (path2 || '').split('/').filter(p => p !== '.')
path1 = joinPaths.apply(undefined, pathSections1)
path2 = joinPaths.apply(undefined, pathSections2)
return path1 === path2
}
/**
* Join path sections
*
* @param {...String} path sections
*
* @return {String} joined path, any leading or trailing slash
* will be kept
*
* @since 8.2
*/
export const joinPaths = (...args) => {
if (arguments.length < 1) {
return ''
}
// discard empty arguments
const nonEmptyArgs = args.filter(arg => arg.length > 0)
if (nonEmptyArgs.length < 1) {
return ''
}
const lastArg = nonEmptyArgs[nonEmptyArgs.length - 1]
const leadingSlash = nonEmptyArgs[0].charAt(0) === '/'
const trailingSlash = lastArg.charAt(lastArg.length - 1) === '/';
const sections = nonEmptyArgs.reduce((acc, section) => acc.concat(section.split('/')), [])
let first = !leadingSlash
const path = sections.reduce((acc, section) => {
if (section === '') {
return acc
}
if (first) {
first = false
return acc + section
}
return acc + '/' + section
}, '')
if (trailingSlash) {
// add it back
return path + '/'
}
return path
}

View File

@ -98,6 +98,24 @@ export const generateUrl = (url, params, options) => {
return getRootPath() + '/index.php' + _build(url, params);
}
/**
* get the absolute path to an image file
* if no extension is given for the image, it will automatically decide
* between .png and .svg based on what the browser supports
*
* @param {string} app the app id to which the image belongs
* @param {string} file the name of the image file
* @return {string}
*/
export const imagePath = (app, file) => {
if (file.indexOf('.') === -1) {
//if no extension is given, use svg
return filePath(app, 'img', file + '.svg')
}
return filePath(app, 'img', file)
}
/**
* Get the absolute url for a file in an app
* @param {string} app the id of the app