Modules (188)

FileIndex

Description

FileIndex is an internal module used by FileSystem to maintain an index of all files and directories.

This module is only used by FileSystem, and should not be called directly.

Dependencies

Classes

Constructor

FileIndex

    function FileIndex() {
        this._index = {};
    }

Properties

Private

_index

Master index

Type
Object.<string, File, Directory>
    FileIndex.prototype._index = null;

Methods

addEntry

Add an entry.

entry FileSystemEntry
The entry to add.
    FileIndex.prototype.addEntry = function (entry) {
        this._index[entry.fullPath] = entry;
    };

clear

Clear the file index cache.

    FileIndex.prototype.clear = function () {
        this._index = {};
    };

entryRenamed

Notify the index that an entry has been renamed. This updates all affected entries in the index.

oldPath string
newPath string
isDirectory boolean
    FileIndex.prototype.entryRenamed = function (oldPath, newPath, isDirectory) {
        var path,
            renameMap = {},
            oldParentPath = FileUtils.getParentPath(oldPath),
            newParentPath = FileUtils.getParentPath(newPath);

        // Find all entries affected by the rename and put into a separate map.
        for (path in this._index) {
            if (this._index.hasOwnProperty(path)) {
                // See if we have a match. For directories, see if the path
                // starts with the old name. This is safe since paths always end
                // with '/'. For files, see if there is an exact match between
                // the path and the old name.
                if (isDirectory ? path.indexOf(oldPath) === 0 : path === oldPath) {
                    renameMap[path] = newPath + path.substr(oldPath.length);
                }
            }
        }

        // Do the rename.
        for (path in renameMap) {
            if (renameMap.hasOwnProperty(path)) {
                var item = this._index[path];

                // Sanity check to make sure the item and path still match
                console.assert(item.fullPath === path);

                delete this._index[path];
                this._index[renameMap[path]] = item;
                item._setPath(renameMap[path]);
            }
        }


        // If file path is changed, i.e the file is moved
        // Remove the moved entry from old Directory and add it to new Directory
        if (oldParentPath !== newParentPath) {
            var oldDirectory = this._index[oldParentPath],
                newDirectory = this._index[newParentPath],
                renamedEntry;

            if (oldDirectory && oldDirectory._contents) {
                oldDirectory._contents = oldDirectory._contents.filter(function(entry) {
                    if (entry.fullPath === newPath) {
                        renamedEntry = entry;
                        return false;
                    }
                    return true;
                });
            }

            if (newDirectory && newDirectory._contents && renamedEntry) {
                renamedEntry._setPath(newPath);
                newDirectory._contents.push(renamedEntry);
            }
        }
    };

getEntry

Returns the cached entry for the specified path, or undefined if the path has not been cached.

path string
The path of the entry to return.
Returns: File,Directory
The entry for the path, or undefined if it hasn't been cached yet.
    FileIndex.prototype.getEntry = function (path) {
        return this._index[path];
    };

    // Export public API
    module.exports = FileIndex;
});

removeEntry

Remove an entry.

entry FileSystemEntry
The entry to remove.
    FileIndex.prototype.removeEntry = function (entry) {
        var path = entry.fullPath,
            property;

        function replaceMember(property) {
            var member = entry[property];
            if (typeof member === "function") {
                entry[property] = function () {
                    console.warn("FileSystemEntry used after being removed from index: ", path);
                    return member.apply(entry, arguments);
                };
            }
        }

        delete this._index[path];

        for (property in entry) {
            if (entry.hasOwnProperty(property)) {
                replaceMember(property);
            }
        }
    };

visitAll

Visits every entry in the entire index; no stopping condition.

Called non-nullable function(FileSystemEntry, string):void
with an entry and its fullPath
    FileIndex.prototype.visitAll = function (visitor) {
        var path;
        for (path in this._index) {
            if (this._index.hasOwnProperty(path)) {
                visitor(this._index[path], path);
            }
        }
    };