Source: media/thumbnail/WebVTT.js

import { NetworkFile } from "../utils/networkTools.js";
export { WebVTT }

/**
 * Class that handles webvtt files. 
 * @author ckraemme <ckraemme@awi.de>
 */
class WebVTT {
    /**
     * Build a webVTT object, ready to be filled with webVTT related data.
     * @param {string} webVTTPath - Path to the webVTT file.
     */
    constructor(webVTTPath) {
        this.webVTTPath = webVTTPath;
        this.manifestSource = [];
    }

    /**
     * Load the webVTT file and include the manifest into the object.
     * @param {callback} - Function to be executed subsequently.
     * @returns {Promise} Promise that is resolved when manifest file was read in successfully.
     */
    readFile() {
        return new Promise((resolve, reject) => {
            NetworkFile.getHttpFile(this.webVTTPath, 'text/vtt', data => {

                // \n\r\n when webvtt file has been written with windows, \n\n with linux 
                this.manifestSource = data.split(/\n\r\n|\n\n/).map(function (item) {
                    var parts = item.split('\n')
                    return {
                        time_start: parts[0].split(' --> ')[0],
                        time_end: parts[0].split(' --> ')[1],
                        data: parts[1],
                    };
                })
                if (typeof this.manifestSource !== 'undefined')
                    return resolve();
                else
                    return reject();
            });
        })
    }
}