export.js 1.93 KB
Newer Older
1 2 3
define([
    'domReady', 'js/views/export', 'jquery', 'gettext'
], function(domReady, Export, $, gettext) {
4
    'use strict';
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
    return function(courselikeHomeUrl, library, statusUrl) {
        var $submitBtn = $('.action-export'),
            unloading = false,
            previousExport = Export.storedExport(courselikeHomeUrl);

        var onComplete = function() {
            $submitBtn.show();
        };

        var startExport = function(e) {
            e.preventDefault();
            $submitBtn.hide();
            Export.reset(library);
            Export.start(statusUrl).then(onComplete);
            $.ajax({
                type: 'POST',
                url: window.location.pathname,
                data: {},
                success: function(result, textStatus, xhr) {
                    if (xhr.status === 200) {
                        setTimeout(function() { Export.pollStatus(result); }, 1000);
                    } else {
                        // It could be that the user is simply refreshing the page
                        // so we need to be sure this is an actual error from the server
                        if (!unloading) {
                            $(window).off('beforeunload.import');

                            Export.reset(library);
                            onComplete();

                            Export.showError(gettext('Your export has failed.'));
36 37 38 39
                        }
                    }
                }
            });
40 41 42 43 44 45 46 47
        };

        $(window).on('beforeunload', function() { unloading = true; });

        // Display the status of last file upload on page load
        if (previousExport) {
            if (previousExport.completed !== true) {
                $submitBtn.hide();
48
            }
49
            Export.resume(library).then(onComplete);
50 51
        }

52 53 54 55
        domReady(function() {
            // export form setup
            $submitBtn.bind('click', startExport);
        });
56 57
    };
});