<commit_before>/* global systemLang, M, FORBIDDEN_CHARS, showdown */

function Hosts(main) {
    'use strict';

    var that      = this;
    this.main     = main;
    this.list     = [];
    this.$tab     = $('#tab-hosts');
    this.$grid    = this.$tab.find('#hosts');
    this.$table   = this.$tab.find('#grid-hosts');
    this.inited   = false;
    this.isTiles  = true;
    this.words    = {};
    
    showdown.setFlavor('github');

    this.prepare  = function () {
        this.isTiles = (this.main.config.hostsIsTiles !== undefined && this.main.config.hostsIsTiles !== null) ? this.main.config.hostsIsTiles : true;

        // fix for IE
        if (this.main.browser === 'ie' && this.main.browserVersion <= 10) {
            this.isTiles = false;
            this.$tab.find('.btn-switch-tiles').hide();
        }

        this.$tab.find('.btn-reload')
            .attr('title', _('Update'))
            .on('click', function () {
                that.init(true);
            });

        this.$tab.find('.btn-switch-tiles').off('click').on('click', function () {
            that.isTiles = !that.isTiles;

            if (that.isTiles) {
                $(this).find('i').text('view_list');
            } else {
                $(this).find('i').text('view_module');
            }

            that.main.saveConfig('hostsIsTiles', that.isTiles);

            setTimeout(function () {
                that._postInit();
            }, 50);
        });

        if (this.isTiles) {
            this.$tab.find('.btn-switch-tiles').find('i').text('view_list');
        } else {
            this.$tab.find('.btn-switch-tiles').find('i').text('view_module');
        }

        this.$tab.find('.filter-clear').on('click', function () {
            that.$tab.find('.filter-input').val('').trigger('change');
        });

        var $hostsFilter = this.$tab.find('.filter-input');
        $hostsFilter.on('change', function () {
            var filter = $(this).val();
            if (filter) {
                $(this).addClass('input-not-empty');
                that.$tab.find('.filter-clear').show();
            } else {
                that.$tab.find('.filter-clear').hide();
                $(this).removeClass('input-not-empty');
            }

            that.main.saveConfig('hostsFilter', filter);
            applyFilter(filter);
        }).on('keyup', function () {
            if (that.filterTimeout) clearTimeout(that.filterTimeout);
            that.filterTimeout = setTimeout(function () {
                that.$tab.find('.filter-input').trigger('change');
            }, 300);
        });

        if (this.main.config.hostsFilter && this.main.config.hostsFilter[0] !== '{') {
            $hostsFilter.val(that.main.config.hostsFilter).addClass('input-not-empty');
            this.$tab.find('.filter-clear').show();
        } else {
            this.$tab.find('.filter-clear').hide();
        }

        // cache translations
        this.words['Title']     = _('Title');
        this.words['OS']        = _('OS');
        this.words['Available'] = _('Available');
        this.words['Installed'] = _('Installed');
        this.words['Events']    = _('Events');
        this.words['Title']     = _('Title');
        that.words['Type']      = _('Type');
    };

    // ----------------------------- Hosts show and Edit ------------------------------------------------
    this.initButtons = function (id) {
        var selector = id ? '[data-host-id="' + id + '"]' : '';

        this.$tab.find('.host-update-submit' + selector).off('click').on('click', function () {
            that.main.cmdExec($(this).attr('data-host-name'), 'upgrade self', function (exitCode) {
                if (!exitCode) that.init(true);
            });
        });

        this.$tab.find('.host-restart-submit' + selector).off('click').on('click', function () {
            that.main.waitForRestart = true;
            that.main.cmdExec($(this).attr('data-host-name'), '_restart');
        });
        this.$tab.find('.host-delete' + selector).off('click').on('click', function () {
            that.main.cmdExec(that.main.currentHost, 'host remove ' + $(this).attr('data-host-name'));
        });

        this.$tab.find('.host-edit' + selector).off('click').on('click', function () {
            editHost($(this).attr('data-host-id'));
        });

        this.$tab.find('.host-update-hint-submit' + selector).off('click').on('click', function () {
            var infoTimeout = setTimeout(function () {
                showUpdateInfo();
                infoTimeout = null;
            }, 1000);

            that.main.socket.emit('sendToHost', $(this).attr('data-host-name'), 'getLocationOnDisk', null, function (data) {
                if (infoTimeout) clearTimeout(infoTimeout);
                infoTimeout = null;
                showUpdateInfo(data);
            });
        });

    };
    
    async function showUpdateInfo(data) {
        const $dialog = $('#dialog-host-update');
        const updateInfo = await fetch("https://raw.githubusercontent.com/ioBroker/ioBroker.docs/master/admin/" + systemLang + "/controller-upgrade.md");
        const html = new showdown.Converter().makeHtml(updateInfo).replace(/<img/g, '<img class="img-responsive"');
        
        if (data) {
            var path = data.path;
            path = path.replace(/\\/g, '/');
            var parts = path.split('/');<commit_msg>Fix file load<commit_after>/* global systemLang, M, FORBIDDEN_CHARS, showdown */

function Hosts(main) {
    'use strict';

    var that      = this;
    this.main     = main;
    this.list     = [];
    this.$tab     = $('#tab-hosts');
    this.$grid    = this.$tab.find('#hosts');
    this.$table   = this.$tab.find('#grid-hosts');
    this.inited   = false;
    this.isTiles  = true;
    this.words    = {};
    
    showdown.setFlavor('github');

    this.prepare  = function () {
        this.isTiles = (this.main.config.hostsIsTiles !== undefined && this.main.config.hostsIsTiles !== null) ? this.main.config.hostsIsTiles : true;

        // fix for IE
        if (this.main.browser === 'ie' && this.main.browserVersion <= 10) {
            this.isTiles = false;
            this.$tab.find('.btn-switch-tiles').hide();
        }

        this.$tab.find('.btn-reload')
            .attr('title', _('Update'))
            .on('click', function () {
                that.init(true);
            });

        this.$tab.find('.btn-switch-tiles').off('click').on('click', function () {
            that.isTiles = !that.isTiles;

            if (that.isTiles) {
                $(this).find('i').text('view_list');
            } else {
                $(this).find('i').text('view_module');
            }

            that.main.saveConfig('hostsIsTiles', that.isTiles);

            setTimeout(function () {
                that._postInit();
            }, 50);
        });

        if (this.isTiles) {
            this.$tab.find('.btn-switch-tiles').find('i').text('view_list');
        } else {
            this.$tab.find('.btn-switch-tiles').find('i').text('view_module');
        }

        this.$tab.find('.filter-clear').on('click', function () {
            that.$tab.find('.filter-input').val('').trigger('change');
        });

        var $hostsFilter = this.$tab.find('.filter-input');
        $hostsFilter.on('change', function () {
            var filter = $(this).val();
            if (filter) {
                $(this).addClass('input-not-empty');
                that.$tab.find('.filter-clear').show();
            } else {
                that.$tab.find('.filter-clear').hide();
                $(this).removeClass('input-not-empty');
            }

            that.main.saveConfig('hostsFilter', filter);
            applyFilter(filter);
        }).on('keyup', function () {
            if (that.filterTimeout) clearTimeout(that.filterTimeout);
            that.filterTimeout = setTimeout(function () {
                that.$tab.find('.filter-input').trigger('change');
            }, 300);
        });

        if (this.main.config.hostsFilter && this.main.config.hostsFilter[0] !== '{') {
            $hostsFilter.val(that.main.config.hostsFilter).addClass('input-not-empty');
            this.$tab.find('.filter-clear').show();
        } else {
            this.$tab.find('.filter-clear').hide();
        }

        // cache translations
        this.words['Title']     = _('Title');
        this.words['OS']        = _('OS');
        this.words['Available'] = _('Available');
        this.words['Installed'] = _('Installed');
        this.words['Events']    = _('Events');
        this.words['Title']     = _('Title');
        that.words['Type']      = _('Type');
    };

    // ----------------------------- Hosts show and Edit ------------------------------------------------
    this.initButtons = function (id) {
        var selector = id ? '[data-host-id="' + id + '"]' : '';

        this.$tab.find('.host-update-submit' + selector).off('click').on('click', function () {
            that.main.cmdExec($(this).attr('data-host-name'), 'upgrade self', function (exitCode) {
                if (!exitCode) that.init(true);
            });
        });

        this.$tab.find('.host-restart-submit' + selector).off('click').on('click', function () {
            that.main.waitForRestart = true;
            that.main.cmdExec($(this).attr('data-host-name'), '_restart');
        });
        this.$tab.find('.host-delete' + selector).off('click').on('click', function () {
            that.main.cmdExec(that.main.currentHost, 'host remove ' + $(this).attr('data-host-name'));
        });

        this.$tab.find('.host-edit' + selector).off('click').on('click', function () {
            editHost($(this).attr('data-host-id'));
        });

        this.$tab.find('.host-update-hint-submit' + selector).off('click').on('click', function () {
            var infoTimeout = setTimeout(function () {
                showUpdateInfo();
                infoTimeout = null;
            }, 1000);

            that.main.socket.emit('sendToHost', $(this).attr('data-host-name'), 'getLocationOnDisk', null, function (data) {
                if (infoTimeout) clearTimeout(infoTimeout);
                infoTimeout = null;
                showUpdateInfo(data);
            });
        });

    };
    
    async function showUpdateInfo(data) {
        const $dialog = $('#dialog-host-update');
        
        let updateInfo;
	await fetch("https://raw.githubusercontent.com/ioBroker/ioBroker.docs/master/admin/" + systemLang + "/controller-upgrade.md")
        .then( async (resp) => {
            updateInfo = await resp.text();
        });
        const html = new showdown.Converter().makeHtml(updateInfo).replace(/<img/g, '<img class="img-responsive"');
        
        if (data) {
            var path = data.path;
            path = path.replace(/\\/g, '/');
            var parts = path.split('/');
{'fraction_non_alphanumeric': 0.20254090119733115, 'fraction_numerical': 0.0021935837674801205, 'mean_word_length': 1.8361845515811301, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '5860509', 'n_tokens_mistral': 3207, 'n_tokens_neox': 3083, 'n_words': 441}