CoreFiles by mySites.guru
actions.js199 lines, 6,306 bytes, from Joomla_4.0.0-alpha2-Alpha-Full_Package.zip
Raw History Compare with 3.10.0-alpha9
md5
45f580e00c176f7eb76ada49fb6859d6
sha1
2141c33013f722bc060aa4ed09561be715de9ad9
sha256
4e59cb35c0387c02334bc13b7640b56800f5a5bc32e8b08ba50ed506ad70ead0
import {api} from "../app/Api";
import * as types from "./mutation-types";
import {notifications} from "../app/Notifications";

// Actions are similar to mutations, the difference being that:
// - Instead of mutating the state, actions commit mutations.
// - Actions can contain arbitrary asynchronous operations.

// TODO move to utils
function updateUrlPath(path) {
    if (path == null) {
        path = '';
    }
    let url = window.location.href;
    var pattern = new RegExp('\\b(path=).*?(&|$)');

    if (url.search(pattern) >= 0) {
        history.pushState(null, '', url.replace(pattern, '$1' + path + '$2'));
    } else {
        history.pushState(null, '', url + (url.indexOf('?') > 0 ? '&' : '?') + 'path=' + path);
    }
}

/**
 * Get contents of a directory from the api
 * @param commit
 * @param payload
 */
export const getContents = (context, payload) => {

    // Update the url
    updateUrlPath(payload);
    context.commit(types.SET_IS_LOADING, true);

    api.getContents(payload, 0)
        .then(contents => {
            context.commit(types.LOAD_CONTENTS_SUCCESS, contents);
            context.commit(types.UNSELECT_ALL_BROWSER_ITEMS);
            context.commit(types.SELECT_DIRECTORY, payload);
            context.commit(types.SET_IS_LOADING, false);
        })
        .catch(error => {
            // TODO error handling
            context.commit(types.SET_IS_LOADING, false);
            console.log("error", error);
        });
}

/**
 * Get the full contents of a directory
 * @param context
 * @param payload
 */
export const getFullContents = (context, payload) => {
    context.commit(types.SET_IS_LOADING, true);
    api.getContents(payload.path, 1)
        .then(contents => {
            context.commit(types.LOAD_FULL_CONTENTS_SUCCESS, contents.files[0]);
            context.commit(types.SET_IS_LOADING, false);
        })
        .catch(error => {
            // TODO error handling
            context.commit(types.SET_IS_LOADING, false);
            console.log("error", error);
        });
}

/**
 * Toggle the selection state of an item
 * @param commit
 * @param payload
 */
export const toggleBrowserItemSelect = (context, payload) => {
    const item = payload;
    const isSelected = context.state.selectedItems.some(selected => selected.path === item.path);
    if (!isSelected) {
        context.commit(types.SELECT_BROWSER_ITEM, item);
    } else {
        context.commit(types.UNSELECT_BROWSER_ITEM, item);
    }
}

/**
 * Create a new folder
 * @param commit
 * @param payload object with the new folder name and its parent directory
 */
export const createDirectory = (context, payload) => {
    context.commit(types.SET_IS_LOADING, true);
    api.createDirectory(payload.name, payload.parent)
        .then(folder => {
            context.commit(types.CREATE_DIRECTORY_SUCCESS, folder);
            context.commit(types.HIDE_CREATE_FOLDER_MODAL);
            context.commit(types.SET_IS_LOADING, false);
        })
        .catch(error => {
            // TODO error handling
            context.commit(types.SET_IS_LOADING, false);
            console.log("error", error);
        })
}

/**
 * Create a new folder
 * @param commit
 * @param payload object with the new folder name and its parent directory
 */
export const uploadFile = (context, payload) => {
    context.commit(types.SET_IS_LOADING, true);
    api.upload(payload.name, payload.parent, payload.content, payload.override || false)
        .then(file => {
            context.commit(types.UPLOAD_SUCCESS, file);
            context.commit(types.SET_IS_LOADING, false);
        })
        .catch(error => {
            context.commit(types.SET_IS_LOADING, false);

            // Handle file exists
            if (error.status === 409) {
                if (notifications.ask('"' + payload.name + '" does already exist. Do you want to override it?', {})) {
                    payload.override = true;
                    uploadFile(context, payload);
                }
            }
        })
}

/**
 * Delete a single item
 * @param context
 * @param payload object: the item to delete
 */
export const deleteItem = (context, payload) => {
    context.commit(types.SET_IS_LOADING, true);
    const item = payload;
    api.delete(item.path)
        .then(() => {
            context.commit(types.DELETE_SUCCESS, item);
            context.commit(types.UNSELECT_ALL_BROWSER_ITEMS);
            context.commit(types.SET_IS_LOADING, false);
        })
        .catch(error => {
            // TODO error handling
            context.commit(types.SET_IS_LOADING, false);
            console.log("error", error);
        })
}

/**
 * Rename an item
 * @param context
 * @param payload object: the old and the new path
 */
export const renameItem = (context, payload) => {
    context.commit(types.SET_IS_LOADING, true);
    api.rename(payload.path, payload.newPath)
        .then((item) => {
            context.commit(types.RENAME_SUCCESS, {
                item: item,
                oldPath: payload.path,
            });
            context.commit(types.HIDE_RENAME_MODAL);
            context.commit(types.SET_IS_LOADING, false);
        })
        .catch(error => {
            // TODO error handling
            context.commit(types.SET_IS_LOADING, false);
            console.log("error", error);
        })
}

/**
 * Delete the selected items
 * @param context
 * @param payload object
 */
export const deleteSelectedItems = (context, payload) => {
    context.commit(types.SET_IS_LOADING, true);
    // Get the selected items from the store
    const selectedItems = context.state.selectedItems;
    if (selectedItems.length > 0) {
        selectedItems.forEach(item => {
            api.delete(item.path)
                .then(() => {
                    context.commit(types.DELETE_SUCCESS, item);
                    context.commit(types.UNSELECT_ALL_BROWSER_ITEMS);
                    context.commit(types.SET_IS_LOADING, false);
                })
                .catch(error => {
                    // TODO error handling
                    context.commit(types.SET_IS_LOADING, false);
                    console.log("error", error);
                })
        })
    } else {
        // TODO notify the user that he has to select at least one item
    }
}

No other release has this exact content. See every version of this file.