<commit_before>define( [ 'jquery', 'enketo-js/Widget', 'file-manager' ], function( $, Widget, fileManager ) {
    "use strict";

    var pluginName = 'filepicker';

    /**
     * FilePicker that works both offline and online. It abstracts the file storage/cache away
     * with the injected fileManager.
     *
     * @constructor
     * @param {Element} element [description]
     * @param {(boolean|{touch: boolean, maxlength:number})} options options
     * @param {*=} e     event
     */

    function Filepicker( element, options, e ) {
        if ( e ) {
            e.stopPropagation();
            e.preventDefault();
        }
        this.namespace = pluginName;
        Widget.call( this, element, options );
        this._init();
    }

    // copy the prototype functions from the Widget super class
    Filepicker.prototype = Object.create( Widget.prototype );

    // ensure the constructor is the new one
    Filepicker.prototype.constructor = Filepicker;

    /**
     * Initialize
     */
    Filepicker.prototype._init = function() {
        var $input = $( this.element ),
            existingFileName = $input.attr( 'data-loaded-file-name' ),
            that = this;

        this.mediaType = $input.attr( 'accept' );

        $input
            .attr( 'disabled', 'disabled' )
            .addClass( 'transparent' )
            .parent().addClass( 'with-media clearfix' );

        this.$widget = $(
                '<div class="widget file-picker">' +
                '<div class="fake-file-input"></div>' +
                '<div class="file-feedback"></div>' +
                '<div class="file-preview"></div>' +
                '</div>' )
            .insertAfter( $input );
        this.$feedback = this.$widget.find( '.file-feedback' );
        this.$preview = this.$widget.find( '.file-preview' );
        this.$fakeInput = this.$widget.find( '.fake-file-input' );

        // show loaded file name regardless of whether widget is supported
        if ( existingFileName ) {
            this._showFileName( existingFileName, this.mediaType );
        }

        if ( !fileManager || !fileManager.isFileReaderSupported() ) {
            var advice = ( fileManager.notSupportedAdvisoryMsg ) ? fileManager.notSupportedAdvisoryMsg : '';
            this._showFeedback( 'Media questions are not supported in this browser. ' + advice, 'warning' );
            return;
        }

        if ( fileManager.isWaitingForPermissions() ) {
            this._showFeedback( 'Waiting for user permissions.', 'warning' );
        }

        fileManager.init()
            .then( function() {
                that._showFeedback();
                that._changeListener();
                $input.removeAttr( 'disabled' );
                if ( existingFileName ) {
                	// Check to see if this file is in storage
                	
                	if(fileManager.isFileStorageSupported() && gLoadedInstanceID) {
                		 fileO = {
                                 newName: existingFileName,
                                 fileName: existingFileName
                             };
                		fileManager.retrieveFile( gLoadedInstanceID, fileO, {
                            success: function( fileObj ) {
                            	
                            	fileManager.getFileUrl( fileObj.file ).then( function( url ) {
                                    that._showPreview( url, that.mediaType );
                                } );
                            	
                            },
                            error: function( e ) {
                            	fileManager.getFileUrl( existingFileName ).then( function( url ) {
                                    that._showPreview( url, that.mediaType );
                                } ).catch( function() {
                                    that._showFeedback( 'File "' + existingFileName + '" could not be found (leave unchanged if already submitted and you want to preserve it).', 'error' );
                                } );
                            }
                		});
                	}
                    
                }
            } )
            .catch( function( error ) {<commit_msg>Show existing attachments when a webform is opened for editing<commit_after>define( [ 'jquery', 'enketo-js/Widget', 'file-manager' ], function( $, Widget, fileManager ) {
    "use strict";

    var pluginName = 'filepicker';

    /**
     * FilePicker that works both offline and online. It abstracts the file storage/cache away
     * with the injected fileManager.
     *
     * @constructor
     * @param {Element} element [description]
     * @param {(boolean|{touch: boolean, maxlength:number})} options options
     * @param {*=} e     event
     */

    function Filepicker( element, options, e ) {
        if ( e ) {
            e.stopPropagation();
            e.preventDefault();
        }
        this.namespace = pluginName;
        Widget.call( this, element, options );
        this._init();
    }

    // copy the prototype functions from the Widget super class
    Filepicker.prototype = Object.create( Widget.prototype );

    // ensure the constructor is the new one
    Filepicker.prototype.constructor = Filepicker;

    /**
     * Initialize
     */
    Filepicker.prototype._init = function() {
        var $input = $( this.element ),
            existingFileName = $input.attr( 'data-loaded-file-name' ),
            that = this;

        this.mediaType = $input.attr( 'accept' );

        $input
            .attr( 'disabled', 'disabled' )
            .addClass( 'transparent' )
            .parent().addClass( 'with-media clearfix' );

        this.$widget = $(
                '<div class="widget file-picker">' +
                '<div class="fake-file-input"></div>' +
                '<div class="file-feedback"></div>' +
                '<div class="file-preview"></div>' +
                '</div>' )
            .insertAfter( $input );
        this.$feedback = this.$widget.find( '.file-feedback' );
        this.$preview = this.$widget.find( '.file-preview' );
        this.$fakeInput = this.$widget.find( '.fake-file-input' );

        // show loaded file name regardless of whether widget is supported
        if ( existingFileName ) {
            this._showFileName( existingFileName, this.mediaType );
        }

        if ( !fileManager || !fileManager.isFileReaderSupported() ) {
            var advice = ( fileManager.notSupportedAdvisoryMsg ) ? fileManager.notSupportedAdvisoryMsg : '';
            this._showFeedback( 'Media questions are not supported in this browser. ' + advice, 'warning' );
            return;
        }

        if ( fileManager.isWaitingForPermissions() ) {
            this._showFeedback( 'Waiting for user permissions.', 'warning' );
        }

        fileManager.init()
            .then( function() {
                that._showFeedback();
                that._changeListener();
                $input.removeAttr( 'disabled' );
                if ( existingFileName ) {
                	// Check to see if this file is in storage
                	
                	if(fileManager.isFileStorageSupported() && gLoadedInstanceID) {
                		 fileO = {
                                 newName: existingFileName,
                                 fileName: existingFileName
                             };
                		fileManager.retrieveFile( gLoadedInstanceID, fileO, {
                            success: function( fileObj ) {
                            	
                            	if(fileObj) {
	                            	fileManager.getFileUrl( fileObj.file ).then( function( url ) {
	                                    that._showPreview( url, that.mediaType );
	                                } );
                            	} else {	// Smap show the file URL, it was presumably downloaded from the server as an existing file
                            		var url = existingFileName;
                            		if(url.charAt(0) !== '/') {
                            			url = "/" + url;
                            		}
                            		that._showPreview( url, that.mediaType );
                            	}
                            	
                            },
                            error: function( e ) {
                            	fileManager.getFileUrl( existingFileName ).then( function( url ) {
                                    that._showPreview( url, that.mediaType );
                                } ).catch( function() {
                                    that._showFeedback( 'File "' + existingFileName + '" could not be found (leave unchanged if already submitted and you want to preserve it).', 'error' );
                                } );
                            }
                		});
                	}
                    
                }
            } )
            .catch( function( error ) {
{'fraction_non_alphanumeric': 0.10950367237925662, 'fraction_numerical': 0.00011128421989761852, 'mean_word_length': 1.3495424836601306, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3077230', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2122, 'n_words': 518}