<commit_before>/*!
 * Copyright 2014 Digital Services, University of Cambridge Licensed
 * under the Educational Community License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 *     http://opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

define(['jquery', 'oae.core', 'jquery.fileupload', 'jquery.iframe-transport'], function($, oae) {

    return function(uid) {
        var $rootel = $('#' + uid);

        var $upload = $('#oa-submitpublication-upload', $rootel);
        var $form = $('#oa-submitpublication-form', $rootel);
        var $progress = $('#oa-submitpublication-upload-progress-container', $rootel);
        var $success = $('#oa-submitpublication-success-container', $rootel);
        var $welcome = $('#oa-submitpublication-welcome-container', $rootel);
        var $uploadError = $('#oa-submitpublication-upload-error-container', $rootel);

        // This variable will hold the file which the user selects to upload
        var selectedFile;
        // The configured default for the visibility of files
        var visibility = oae.api.config.getValue('oae-content', 'visibility', 'files');

        /////////////////////////
        //   Welcome message   //
        /////////////////////////

        /**
         * Render and shows the welcome message
         */
        var initWelcome = function() {
            oae.api.util.template().render($('#oa-submitpublication-welcome-name-template', $rootel), {}, $('#oa-submitpublication-welcome-name-container', $welcome));
            $welcome.show();
        };

        /**
         * Hide all panels excluding the elements provided to the function
         *
         * @param {...*} [panel] The panels to show.
         */
        var showPanels = function() {
            $form.hide();
            $progress.hide();
            $success.hide();
            $upload.hide();
            $welcome.hide();
            // Run through the arguments array and calls jQuery.fn.show on each item
            _.invoke(arguments, $.fn.show);
            // Reset the page scroll position
            $(window).scrollTop(0);
        };

        /////////////////////////
        //     File upload     //
        /////////////////////////

        /**
         * Render and shows an error in the upload form.
         *
         * @param  {String}    message    The error message to be displayed
         */
        var showFileUploadError = function(message) {
            var messages = {
                'standard': 'There was an error uploading your file, please try again later.',
                'emptyFile': 'The selected file seems to be empty, please make sure you select the correct file.',
                'multipleFiles': 'I’m sorry, you can only upload one file at a time. Please try again'
            };
            // Render the error message, display the default message if none is provided
            message = messages[message] || messages.standard;
            oae.api.util.template().render($('#oa-submitpublication-upload-error-template', $rootel), {
                'errorMessage': message
            }, $uploadError);
            $uploadError.show();
        };

        /**
         * Reset the upload form to be empty.
         */
        var resetFileUpload = function() {
            $('form', $upload)[0].reset();
        };

        /**
         * Initialise the fileupload functionlity of the widget
         */
        var initFileupload = function() {
            $upload.fileupload({
                'add': function(event, data) {
                    $uploadError.hide();
                    var addedFile = data.files[0];

                    if (addedFile.size === 0) {
                        showFileUploadError('emptyFile');
                        resetFileUpload();
                    } else {
                        // Cache the selected file
                        selectedFile = data.files[0];
                        renderFileData();
                        // Show the form
                        showPanels($form);
                        $(document).trigger('oa.submitpublication.start');
                    }
                },
                'drop': function(event, data) {
                    // Show an error when the user attempts to upload multiple files.
                    if (data.files.length > 1) {
                        showFileUploadError('multipleFiles');
                        return false;
                    }
                },
                'dropZone': $upload,
                'fileInput': $('.oa-submitpublication-input-upload', $rootel),
                'forceIframeTransport': !$.support.xhrFileUpload && !$.support.xhrFormDataFileUpload,
                'replaceFileInput': false,
                'progress': function(data) {
                    var percentage = data.loaded / data.total * 100;
                    var totalSize = $.fn.fileSize(data.total);
                    var doneSize = $.fn.fileSize(data.loaded);
                    // Update the upload progress bar
                    var $progressBar = $('.oa-progress-bar', $rootel).width(percentage + '%').attr('aria-valuenow', percentage);
                    $('span', $progressBar).text(percentage + '% Complete');
                    // Update the progress text (e.g. '1.2 MB of 5 MB')
                    $('.oa-progress-size', $rootel).text(doneSize + ' of ' + totalSize);
                },
                'url': '/api/publications/create'
            });

            $upload.on('dragover', function() {
                $('#oa-submitpublication-upload-wrap', $upload).addClass('oa-dragover');
            });

            $upload.on('dragleave', function() {
                $('#oa-submitpublication-upload-wrap', $upload).removeClass('oa-dragover');
            });
        };

        /**
         * Insert and initialise the publicationform widget
         */
        var initForm = function() {
            // Fetch and insert the publicationform widget
            oae.api.widget.insertWidget('publicationform', null, $('#oa-submitpublication-form-container', $rootel));
            $(document).on('oa.publicationform.submit', function(event, data) {
                showPanels($progress);
                oae.api.publication.createPublication(data.title, data.author, data.department, data.journal, data.acceptanceDate, data.funders, data.email, data['use-cambridge-addendum'] === 'on', data.comment, $upload, selectedFile, function(error, data) {
                    // Reset the upload form
                    resetFileUpload();
                    if (error) {
                        showPanels($welcome, $upload);
                        // Show there was an error
                        showFileUploadError();
                        return;
                    }
                    // Reset the form
                    $('form', $form)[0].reset();
                    // Render and display the success message if there was no error
                    oae.api.util.template().render($('#oa-submitpublication-success-template', $rootel), {
                        'addendumUrl': oae.api.config.getValue('oae-tickets', 'general', 'addendumUrl'),
                        'publication': data.publication
                    }, $success);
                    showPanels($upload, $success);
                    $(document).trigger('oa.submitpublication.end');
                });
            });
        };

        /**
         * Update all html containing information about the selected file
         */
        var renderFileData = function() {
            var data = {
                'fileName': selectedFile.name,
                'fileSize': selectedFile.size ? $.fn.fileSize(selectedFile.size) : ''
            };
            oae.api.util.template().render($('#oa-submitpublication-upload-selected-template', $rootel), data, $('#oa-submitpublication-upload-selected-container', $rootel));
            oae.api.util.template().render($('#oa-submitpublication-upload-progress-template', $rootel), data, $progress);
        };

        initFileupload();
        initForm();
        initWelcome();
    };
});<commit_msg>Flip the expand/collapse arrow in the upload version area<commit_after>/*!
 * Copyright 2014 Digital Services, University of Cambridge Licensed
 * under the Educational Community License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 *     http://opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

define(['jquery', 'oae.core', 'jquery.fileupload', 'jquery.iframe-transport'], function($, oae) {

    return function(uid) {
        var $rootel = $('#' + uid);

        var $upload = $('#oa-submitpublication-upload', $rootel);
        var $form = $('#oa-submitpublication-form', $rootel);
        var $progress = $('#oa-submitpublication-upload-progress-container', $rootel);
        var $success = $('#oa-submitpublication-success-container', $rootel);
        var $welcome = $('#oa-submitpublication-welcome-container', $rootel);
        var $uploadError = $('#oa-submitpublication-upload-error-container', $rootel);
        var $versionCollapseTarget = $('#oa-submitpublication-welcome-help', $rootel);
        var $versionCollapseIcon = $('button[data-target="#oa-submitpublication-welcome-help"] [class*=icon-]', $rootel);

        // This variable will hold the file which the user selects to upload
        var selectedFile;
        // The configured default for the visibility of files
        var visibility = oae.api.config.getValue('oae-content', 'visibility', 'files');

        /////////////////////////
        //   Welcome message   //
        /////////////////////////

        /**
         * Render and shows the welcome message
         */
        var initWelcome = function() {
            oae.api.util.template().render($('#oa-submitpublication-welcome-name-template', $rootel), {}, $('#oa-submitpublication-welcome-name-container', $welcome));
            $welcome.show();
        };

        /**
         * Hide all panels excluding the elements provided to the function
         *
         * @param {...*} [panel] The panels to show.
         */
        var showPanels = function() {
            $form.hide();
            $progress.hide();
            $success.hide();
            $upload.hide();
            $welcome.hide();
            // Run through the arguments array and calls jQuery.fn.show on each item
            _.invoke(arguments, $.fn.show);
            // Reset the page scroll position
            $(window).scrollTop(0);
        };

        /////////////////////////
        //     File upload     //
        /////////////////////////

        /**
         * Render and shows an error in the upload form.
         *
         * @param  {String}    message    The error message to be displayed
         */
        var showFileUploadError = function(message) {
            var messages = {
                'standard': 'There was an error uploading your file, please try again later.',
                'emptyFile': 'The selected file seems to be empty, please make sure you select the correct file.',
                'multipleFiles': 'I’m sorry, you can only upload one file at a time. Please try again'
            };
            // Render the error message, display the default message if none is provided
            message = messages[message] || messages.standard;
            oae.api.util.template().render($('#oa-submitpublication-upload-error-template', $rootel), {
                'errorMessage': message
            }, $uploadError);
            $uploadError.show();
        };

        /**
         * Reset the upload form to be empty.
         */
        var resetFileUpload = function() {
            $('form', $upload)[0].reset();
        };

        /**
         * Initialise the fileupload functionlity of the widget
         */
        var initFileupload = function() {
            $upload.fileupload({
                'add': function(event, data) {
                    $uploadError.hide();
                    var addedFile = data.files[0];

                    if (addedFile.size === 0) {
                        showFileUploadError('emptyFile');
                        resetFileUpload();
                    } else {
                        // Cache the selected file
                        selectedFile = data.files[0];
                        renderFileData();
                        // Show the form
                        showPanels($form);
                        $(document).trigger('oa.submitpublication.start');
                    }
                },
                'drop': function(event, data) {
                    // Show an error when the user attempts to upload multiple files.
                    if (data.files.length > 1) {
                        showFileUploadError('multipleFiles');
                        return false;
                    }
                },
                'dropZone': $upload,
                'fileInput': $('.oa-submitpublication-input-upload', $rootel),
                'forceIframeTransport': !$.support.xhrFileUpload && !$.support.xhrFormDataFileUpload,
                'replaceFileInput': false,
                'progress': function(data) {
                    var percentage = data.loaded / data.total * 100;
                    var totalSize = $.fn.fileSize(data.total);
                    var doneSize = $.fn.fileSize(data.loaded);
                    // Update the upload progress bar
                    var $progressBar = $('.oa-progress-bar', $rootel).width(percentage + '%').attr('aria-valuenow', percentage);
                    $('span', $progressBar).text(percentage + '% Complete');
                    // Update the progress text (e.g. '1.2 MB of 5 MB')
                    $('.oa-progress-size', $rootel).text(doneSize + ' of ' + totalSize);
                },
                'url': '/api/publications/create'
            });

            $upload.on('dragover', function() {
                $('#oa-submitpublication-upload-wrap', $upload).addClass('oa-dragover');
            });

            $upload.on('dragleave', function() {
                $('#oa-submitpublication-upload-wrap', $upload).removeClass('oa-dragover');
            });
        };

        /**
         * Insert and initialise the publicationform widget
         */
        var initForm = function() {
            // Fetch and insert the publicationform widget
            oae.api.widget.insertWidget('publicationform', null, $('#oa-submitpublication-form-container', $rootel));
            $(document).on('oa.publicationform.submit', function(event, data) {
                showPanels($progress);
                oae.api.publication.createPublication(data.title, data.author, data.department, data.journal, data.acceptanceDate, data.funders, data.email, data['use-cambridge-addendum'] === 'on', data.comment, $upload, selectedFile, function(error, data) {
                    // Reset the upload form
                    resetFileUpload();
                    if (error) {
                        showPanels($welcome, $upload);
                        // Show there was an error
                        showFileUploadError();
                        return;
                    }
                    // Reset the form
                    $('form', $form)[0].reset();
                    // Render and display the success message if there was no error
                    oae.api.util.template().render($('#oa-submitpublication-success-template', $rootel), {
                        'addendumUrl': oae.api.config.getValue('oae-tickets', 'general', 'addendumUrl'),
                        'publication': data.publication
                    }, $success);
                    showPanels($upload, $success);
                    $(document).trigger('oa.submitpublication.end');
                });
            });
        };

        /**
         * Update all html containing information about the selected file
         */
        var renderFileData = function() {
            var data = {
                'fileName': selectedFile.name,
                'fileSize': selectedFile.size ? $.fn.fileSize(selectedFile.size) : ''
            };
            oae.api.util.template().render($('#oa-submitpublication-upload-selected-template', $rootel), data, $('#oa-submitpublication-upload-selected-container', $rootel));
            oae.api.util.template().render($('#oa-submitpublication-upload-progress-template', $rootel), data, $progress);
        };

        // constants for flipExpandCollapseChevron()
        var COLLAPSE_OPEN_CLASS = 'icon-chevron-down';
        var COLLAPSE_CLOSE_CLASS = 'icon-chevron-up';
        var COLLAPSE_CLASSES = COLLAPSE_OPEN_CLASS + ' ' + COLLAPSE_CLOSE_CLASS;

        /**
         * Flip an up/down arrow icon when a Bootstrap collapsable area expands/collapses.
         *
         * @param  {jQuery}  $collapseTarget  The element to watch for expand/collapse on
         * @param  {jQuery}  $iconElems       The icon element(s) to flip the arrow icon of when the target is collapsed/expanded
         */
        var flipExpandCollapseChevron = function($collapseTarget, $iconElems) {
            $collapseTarget.on('show.bs.collapse hide.bs.collapse', function(e) {
                $iconElems.removeClass(COLLAPSE_CLASSES);
                var iconClass = COLLAPSE_OPEN_CLASS;
                if (e.type === 'show') {
                    iconClass = COLLAPSE_CLOSE_CLASS;
                }
                $iconElems.addClass(iconClass);
            });
        };

        initFileupload();
        initForm();
        initWelcome();

        // Keep the expand/collapse arrow pointing in the right direction
        flipExpandCollapseChevron($versionCollapseTarget, $versionCollapseIcon);
    };
});
{'fraction_non_alphanumeric': 0.1321150228433217, 'fraction_numerical': 0.0022574576726686374, 'mean_word_length': 1.6702066590126292, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '257664', 'n_tokens_mistral': 4666, 'n_tokens_neox': 4451, 'n_words': 1172}