<commit_before>// Add volatile events to Element, Window and Events
// from http://jsfiddle.net/ZVbWP/
Events.implement({
    addVolatileEvent: function(type, fn, counter, internal){
        if(!counter) {
            counter = 1;
        }
        var volatileFn = function(){
            fn.apply(this, arguments);
            counter -= 1;
            if(counter < 1) {
                this.removeEvent(type, volatileFn);
            }
        }
        this.addEvent(type, volatileFn, internal);
    }
});

/*
 * Class: FlightDeck
 * Initializes all needed functionality
 */

var FlightDeck = new Class({
    Implements: [Options, Events],
    options: {
        menu_el: 'UI_Editor_Menu',
        try_in_browser_class: 'XPI_test',
        xpi_hashtag: '',        // hashtag for the XPI creation
        max_request_number: 50, // how many times should system try to download XPI
        request_interval: 5000  // try to download XPI every 5 sec
        //user: ''
    },
    initialize: function() {
        this.tests = {}; // placeholder for testing spinners
        this.uri = new URI();
        if (this.uri.getData('redirect','fragment')) {
            window.location.href = this.uri.getData('redirect', 'fragment');
        }
        this.warning = this.error = this.message = {
            'alert': function(title, message) {
                alert(title+"\n"+message);
            }
        };
        this.editors = [];
        this.parseTooltips();
        this.createActionSections();
        this.parseTestButtons.bind(this).delay(10);
        this.addEvent('xpi_downloaded', this.whenXpiDownloaded);
        this.addEvent('xpi_installed', this.whenXpiInstalled);
        this.addEvent('xpi_uninstalled', this.whenXpiUninstalled);
        //if (!this.isAddonInstalled()) $log('FD: No Addon Builder Helper')
    },

    parseTooltips: function() {
        this.tips = new Tips({
            fixed: false,
            className: 'UI_tooltip',
            offset: {
                x: 0,
                y: 16
            }
        });

        $$('div.UI_tooltip_source').each(function(tipSource){
            tipSource.hide();
            var target = $(tipSource.get('data-tooltip-for'));
            target.set('title', '');
            target.store('tip:title', tipSource.get('data-tooltip-title'));
            target.store('tip:text', tipSource.get('html'));
            this.tips.attach(target);
        }, this);
    },

    setURIRedirect: function(url) {
        // change the URL add #/path/to/saved/revision
        if (this.uri.get('directory') != url) {
            this.uri.setData({'redirect': url}, false, 'fragment');
            this.uri.go();
        }
    },

    whenXpiInstalled: function(name) {
        this.parseTestButtons();
        this.message.alert('Add-ons Builder', 'Add-on installed');
        $log('FD: INFO: Add-on installed');
    },

    whenXpiDownloaded: function(hashtag) {
        
        fd.item.generateHashtag();
        
    },

    whenXpiUninstalled: function() {
        this.parseTestButtons();
        this.message.alert('Add-ons Builder', 'Add-on uninstalled');
    },

    /*
    Method: whenAddonInstalled
    create listener for a callback function
     */
    whenAddonInstalled: function(callback) {
        var removeListener = function() {
            document.body.removeEventListener('addonbuilderhelperstart', callback, false);
        }
        document.body.addEventListener('addonbuilderhelperstart', callback, false);
        (function() {
            $log('FD: Warning: not listening to addonbuilderhelperstart, is Helper installed?');
            removeListener();
        }).delay(100000);
    },

    parseTestButtons: function() {
        var installed = (this.isAddonInstalled()) ? this.isXpiInstalled() : false;
        if (installed) {
            $$('.{try_in_browser_class} a'.substitute(this.options)).each(function(test_button){
                if (installed && installed.installedID == test_button.get('rel')) {
                    test_button.getParent('li').addClass('pressed');
                } else {
                    test_button.getParent('li').removeClass('pressed');
                }
            }, this);
        }
    },

    /*
     * Method: downloadXPI it's running in Request's scope
     */
    downloadXPI: function(response) {
        $log('FD: DEBUG: XPI delayed ... try to load every ' + fd.options.request_interval/1000 + ' seconds' );
        var hashtag = this.options.data.hashtag;
        var filename = this.options.data.filename;
        fd.tests[hashtag].download_request_number = 0;
        fd.tryDownloadXPI.delay(1000, fd, [hashtag, filename]);
        fd.tests[hashtag].download_ID = fd.tryDownloadXPI.periodical(
                fd.options.request_interval, fd, [hashtag, filename]);
    },

    /*
     * Method: tryDownloadXPI
     *
     * Try to download XPI
     * if finished - stop periodical, stop spinner
     */
    tryDownloadXPI: function(hashtag, filename) {
        var test_request = this.tests[hashtag];
        if (!test_request.download_xpi_request || (
                    test_request.download_xpi_request &&
                    !test_request.download_xpi_request.isRunning())) {
            test_request.download_request_number++;
            var url = '/xpi/check_download/'+hashtag+'/';
            $log('FD: DEBUG: checking if ' + url + ' is prepared');
            test_request.download_xpi_request = new Request.JSON({
                method: 'get',
                url: url,
                onSuccess: function(response) {
                    if (response.ready || test_request.download_request_number > 50) {
                        clearInterval(test_request.download_ID);
                        test_request.spinner.destroy();
                    }
                    if (response.ready) {
                        var url = '/xpi/download/'+hashtag+'/'+filename+'/';
                        $log('FD: downloading ' + filename + '.xpi from ' + url );
                        window.open(url, 'dl');
                    }
                },
                addOnFailure: function() {
                    clearInterval(test_request.download_ID);
                    test_request.spinner.destroy();
                }
            }).send();
        }
    },<commit_msg>fixes Bug 651617 - Reduce check_download interval<commit_after>// Add volatile events to Element, Window and Events
// from http://jsfiddle.net/ZVbWP/
Events.implement({
    addVolatileEvent: function(type, fn, counter, internal){
        if(!counter) {
            counter = 1;
        }
        var volatileFn = function(){
            fn.apply(this, arguments);
            counter -= 1;
            if(counter < 1) {
                this.removeEvent(type, volatileFn);
            }
        }
        this.addEvent(type, volatileFn, internal);
    }
});

/*
 * Class: FlightDeck
 * Initializes all needed functionality
 */

var FlightDeck = new Class({
    Implements: [Options, Events],
    options: {
        menu_el: 'UI_Editor_Menu',
        try_in_browser_class: 'XPI_test',
        xpi_hashtag: '',        // hashtag for the XPI creation
        max_request_number: 50, // how many times should system try to download XPI
        request_interval: 2000  // try to download XPI every 2 sec
        //user: ''
    },
    initialize: function() {
        this.tests = {}; // placeholder for testing spinners
        this.uri = new URI();
        if (this.uri.getData('redirect','fragment')) {
            window.location.href = this.uri.getData('redirect', 'fragment');
        }
        this.warning = this.error = this.message = {
            'alert': function(title, message) {
                alert(title+"\n"+message);
            }
        };
        this.editors = [];
        this.parseTooltips();
        this.createActionSections();
        this.parseTestButtons.bind(this).delay(10);
        this.addEvent('xpi_downloaded', this.whenXpiDownloaded);
        this.addEvent('xpi_installed', this.whenXpiInstalled);
        this.addEvent('xpi_uninstalled', this.whenXpiUninstalled);
        //if (!this.isAddonInstalled()) $log('FD: No Addon Builder Helper')
    },

    parseTooltips: function() {
        this.tips = new Tips({
            fixed: false,
            className: 'UI_tooltip',
            offset: {
                x: 0,
                y: 16
            }
        });

        $$('div.UI_tooltip_source').each(function(tipSource){
            tipSource.hide();
            var target = $(tipSource.get('data-tooltip-for'));
            target.set('title', '');
            target.store('tip:title', tipSource.get('data-tooltip-title'));
            target.store('tip:text', tipSource.get('html'));
            this.tips.attach(target);
        }, this);
    },

    setURIRedirect: function(url) {
        // change the URL add #/path/to/saved/revision
        if (this.uri.get('directory') != url) {
            this.uri.setData({'redirect': url}, false, 'fragment');
            this.uri.go();
        }
    },

    whenXpiInstalled: function(name) {
        this.parseTestButtons();
        this.message.alert('Add-ons Builder', 'Add-on installed');
        $log('FD: INFO: Add-on installed');
    },

    whenXpiDownloaded: function(hashtag) {
        
        fd.item.generateHashtag();
        
    },

    whenXpiUninstalled: function() {
        this.parseTestButtons();
        this.message.alert('Add-ons Builder', 'Add-on uninstalled');
    },

    /*
    Method: whenAddonInstalled
    create listener for a callback function
     */
    whenAddonInstalled: function(callback) {
        var removeListener = function() {
            document.body.removeEventListener('addonbuilderhelperstart', callback, false);
        }
        document.body.addEventListener('addonbuilderhelperstart', callback, false);
        (function() {
            $log('FD: Warning: not listening to addonbuilderhelperstart, is Helper installed?');
            removeListener();
        }).delay(100000);
    },

    parseTestButtons: function() {
        var installed = (this.isAddonInstalled()) ? this.isXpiInstalled() : false;
        if (installed) {
            $$('.{try_in_browser_class} a'.substitute(this.options)).each(function(test_button){
                if (installed && installed.installedID == test_button.get('rel')) {
                    test_button.getParent('li').addClass('pressed');
                } else {
                    test_button.getParent('li').removeClass('pressed');
                }
            }, this);
        }
    },

    /*
     * Method: downloadXPI it's running in Request's scope
     */
    downloadXPI: function(response) {
        var time = fd.options.request_interval
        
        $log('FD: DEBUG: XPI delayed ... try to load every ' + time/1000 + ' seconds' );
        var hashtag = this.options.data.hashtag;
        var filename = this.options.data.filename;
        fd.tests[hashtag].download_request_number = 0;
        
        fd.tests[hashtag].download_ID = fd.tryDownloadXPI.periodical(
                time, fd, [hashtag, filename]);
    },

    /*
     * Method: tryDownloadXPI
     *
     * Try to download XPI
     * if finished - stop periodical, stop spinner
     */
    tryDownloadXPI: function(hashtag, filename) {
        var test_request = this.tests[hashtag];
        if (!test_request.download_xpi_request || (
                    test_request.download_xpi_request &&
                    !test_request.download_xpi_request.isRunning())) {
            test_request.download_request_number++;
            var url = '/xpi/check_download/'+hashtag+'/';
            $log('FD: DEBUG: checking if ' + url + ' is prepared');
            test_request.download_xpi_request = new Request.JSON({
                method: 'get',
                url: url,
                timeout: fd.options.request_interval,
                onSuccess: function(response) {
                    if (response.ready || test_request.download_request_number > 50) {
                        clearInterval(test_request.download_ID);
                        test_request.spinner.destroy();
                    }
                    if (response.ready) {
                        var url = '/xpi/download/'+hashtag+'/'+filename+'/';
                        $log('FD: downloading ' + filename + '.xpi from ' + url );
                        window.open(url, 'dl');
                    }
                },
                addOnFailure: function() {
                    clearInterval(test_request.download_ID);
                    test_request.spinner.destroy();
                }
            }).send();
        }
    },
{'fraction_non_alphanumeric': 0.1292811589638611, 'fraction_numerical': 0.005196441225100386, 'mean_word_length': 1.8802721088435375, 'pattern_counts': {'":': 0, '<': 5, '<?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': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4596899', 'n_tokens_mistral': 3621, 'n_tokens_neox': 3466, 'n_words': 672}