All files / components/uploader/js FileQueue.js

100% Statements 48/48
80% Branches 4/5
100% Functions 19/19
100% Lines 48/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                              12x   12x     12x   12x                                                                                                                                                                           12x 20x 20x 20x     12x 10x 10x 10x     12x 60x     12x 64x           12x 214x     12x 12x 20x       12x 432x 100542x       12x 1168x 1168x     12x 1168x 1168x 2084x 2228x         12x 254x 254x     12x 26x 26x     12x 86x                       12x 28x 28x     12x 60x 60x 60x 60x          
/*
Copyright 2008-2009 University of Toronto
Copyright 2008-2009 University of California, Berkeley
Copyright 2010-2011 OCAD University
Copyright 2013 Raising the Floor - US
Copyright 2015 Raising the Floor - International
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
 
var fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    fluid.registerNamespace("fluid.uploader");
 
    fluid.defaults("fluid.uploader.fileQueue", {
        gradeNames: ["fluid.component"],
        members: {
            files: [],
            isUploading: false
        },
        invokers: {
            /********************
             * Queue Operations *
             ********************/
            start: {
                funcName: "fluid.uploader.fileQueue.start",
                args: "{that}"
            },
            startFile: {
                funcName: "fluid.uploader.fileQueue.startFile",
                args: "{that}.currentBatch"
            },
            finishFile: {
                funcName: "fluid.uploader.fileQueue.finishFile",
                args: "{that}.currentBatch"
            },
            shouldUploadNextFile: {
                funcName: "fluid.uploader.fileQueue.shouldUploadNextFile",
                args: "{that}"
            },
            /*****************************
             * File manipulation methods *
             *****************************/
            addFile: {
                funcName: "fluid.uploader.fileQueue.addFile",
                args: ["{that}.files", "{arguments}.0"]
            },
            removeFile: {
                funcName: "fluid.uploader.fileQueue.removeFile",
                args: ["{that}.files", "{arguments}.0"]
            },
            /**********************
             * Queue Info Methods *
             **********************/
            totalBytes: {
                funcName: "fluid.uploader.fileQueue.sizeOfFiles",
                args: "{that}.files"
            },
            getReadyFiles: {
                funcName: "fluid.uploader.fileQueue.filesByStatus",
                args: ["{that}.files", ["QUEUED", "CANCELLED"]]
            },
            getErroredFiles: {
                funcName: "fluid.uploader.fileQueue.filesByStatus",
                args: ["{that}.files", "ERROR"]
            },
            getUploadedFiles: {
                funcName: "fluid.uploader.fileQueue.filesByStatus",
                args: ["{that}.files", "COMPLETE"]
            },
            sizeOfReadyFiles: {
                funcName: "fluid.uploader.fileQueue.sizeOfFilesByStatus",
                args: ["{that}.files", ["QUEUED", "CANCELLED"]]
            },
            sizeOfUploadedFiles: {
                funcName: "fluid.uploader.fileQueue.sizeOfFilesByStatus",
                args: ["{that}.files", "COMPLETE"]
            },
            /*****************
             * Batch Methods *
             *****************/
            setupCurrentBatch:  {
                funcName: "fluid.uploader.fileQueue.setupCurrentBatch",
                args: "{that}"
            },
            clearCurrentBatch:  {
                funcName: "fluid.uploader.fileQueue.clearCurrentBatch",
                args: "{that}"
            },
            updateCurrentBatch:  {
                funcName: "fluid.uploader.fileQueue.updateCurrentBatch",
                args: [{expander: {func: "{that}.getReadyFiles"}}, "{that}.currentBatch"]
            },
            updateBatchStatus:  {
                funcName: "fluid.uploader.fileQueue.updateBatchStatus",
                args: ["{arguments}.0", "{that}.currentBatch"]
            }
        }
    });
 
    fluid.uploader.fileQueue.start = function (that) {
        that.setupCurrentBatch();
        that.isUploading = true;
        that.shouldStop = false;
    };
 
    fluid.uploader.fileQueue.startFile = function (currentBatch) {
        currentBatch.fileIdx++;
        currentBatch.bytesUploadedForFile = 0;
        currentBatch.previousBytesUploadedForFile = 0;
    };
 
    fluid.uploader.fileQueue.finishFile = function (currentBatch) {
        currentBatch.numFilesCompleted++;
    };
 
    fluid.uploader.fileQueue.shouldUploadNextFile = function (that) {
        return !that.shouldStop &&
            that.isUploading &&
            (that.currentBatch.numFilesCompleted + that.currentBatch.numFilesErrored) <
            that.currentBatch.files.length;
    };
 
    fluid.uploader.fileQueue.addFile = function (files, file) {
        files.push(file);
    };
 
    fluid.uploader.fileQueue.removeFile = function (files, file) {
        fluid.remove_if(files, function (thisFile) {
            return file === thisFile;
        });
    };
 
    fluid.uploader.fileQueue.sizeOfFiles = function (files) {
        return fluid.accumulate(files, function (file, totalBytes) {
            return totalBytes + file.size;
        }, 0);
    };
 
    fluid.uploader.fileQueue.filterFiles = function (files, filterFn) {
        var filteredFiles = []; // filterFn returns TRUE for the files we want
        return fluid.remove_if(fluid.makeArray(files), filterFn, filteredFiles);
    };
 
    fluid.uploader.fileQueue.filesByStatus = function (files, statuses) {
        statuses = fluid.makeArray(statuses);
        return fluid.uploader.fileQueue.filterFiles(files, function (file) {
            return fluid.find_if(statuses, function (status) {
                return file.filestatus === fluid.uploader.fileStatusConstants[status];
            });
        });
    };
 
    fluid.uploader.fileQueue.sizeOfFilesByStatus = function (files, statuses) {
        files = fluid.uploader.fileQueue.filesByStatus(files, statuses);
        return fluid.uploader.fileQueue.sizeOfFiles(files);
    };
 
    fluid.uploader.fileQueue.setupCurrentBatch = function (that) {
        that.clearCurrentBatch();
        that.updateCurrentBatch();
    };
 
    fluid.uploader.fileQueue.clearCurrentBatch = function (that) {
        that.currentBatch = {
            fileIdx: 0,
            files: [],
            totalBytes: 0,
            numFilesCompleted: 0,
            numFilesErrored: 0,
            bytesUploadedForFile: 0,
            previousBytesUploadedForFile: 0,
            totalBytesUploaded: 0
        };
    };
 
    fluid.uploader.fileQueue.updateCurrentBatch = function (readyFiles, currentBatch) {
        currentBatch.files = readyFiles;
        currentBatch.totalBytes = fluid.uploader.fileQueue.sizeOfFiles(readyFiles);
    };
 
    fluid.uploader.fileQueue.updateBatchStatus = function (currentBytes, currentBatch) {
        var byteIncrement = currentBytes - currentBatch.previousBytesUploadedForFile;
        currentBatch.totalBytesUploaded += byteIncrement;
        currentBatch.bytesUploadedForFile += byteIncrement;
        currentBatch.previousBytesUploadedForFile = currentBytes;
    };
 
 
})(jQuery, fluid_3_0_0);