<commit_before>'use strict';

var openDraw = null;

function rawBGsEnabled() {
    return app.enabledOptions && app.enabledOptions.indexOf('rawbg') > -1;
}

function getBrowserSettings(storage) {
    var json = {};

    function scaleBg(bg) {
        if (json.units == 'mmol') {
            return (Math.round((bg / 18) * 10) / 10).toFixed(1);
        } else {
            return bg;
        }
    }

    function appendThresholdValue(threshold) {
        return app.alarm_types.indexOf('simple') == -1 ? '' : ' (' + scaleBg(threshold) + ')';
    }

    try {
        var json = {
            'units': storage.get('units'),
            'alarmUrgentHigh': storage.get('alarmUrgentHigh'),
            'alarmHigh': storage.get('alarmHigh'),
            'alarmLow': storage.get('alarmLow'),
            'alarmUrgentLow': storage.get('alarmUrgentLow'),
            'nightMode': storage.get('nightMode'),
            'showRawbg': storage.get('showRawbg'),
            'customTitle': storage.get('customTitle'),
            'theme': storage.get('theme'),
            'timeFormat': storage.get('timeFormat')
        };

        // Default browser units to server units if undefined.
        json.units = setDefault(json.units, app.units);
        if (json.units == 'mmol') {
            $('#mmol-browser').prop('checked', true);
        } else {
            $('#mgdl-browser').prop('checked', true);
        }

        json.alarmUrgentHigh = setDefault(json.alarmUrgentHigh, app.defaults.alarmUrgentHigh);
        json.alarmHigh = setDefault(json.alarmHigh, app.defaults.alarmHigh);
        json.alarmLow = setDefault(json.alarmLow, app.defaults.alarmLow);
        json.alarmUrgentLow = setDefault(json.alarmUrgentLow, app.defaults.alarmUrgentLow);
        $('#alarm-urgenthigh-browser').prop('checked', json.alarmUrgentHigh).next().text('Urgent High Alarm' + appendThresholdValue(app.thresholds.bg_high));
        $('#alarm-high-browser').prop('checked', json.alarmHigh).next().text('High Alarm' + appendThresholdValue(app.thresholds.bg_target_top));
        $('#alarm-low-browser').prop('checked', json.alarmLow).next().text('Low Alarm' + appendThresholdValue(app.thresholds.bg_target_bottom));
        $('#alarm-urgentlow-browser').prop('checked', json.alarmUrgentLow).next().text('Urgent Low Alarm' + appendThresholdValue(app.thresholds.bg_low));

        json.nightMode = setDefault(json.nightMode, app.defaults.nightMode);
        $('#nightmode-browser').prop('checked', json.nightMode);

        if (rawBGsEnabled()) {
            $('#show-rawbg-option').show();
            json.showRawbg = setDefault(json.showRawbg, app.defaults.showRawbg);
            $('#show-rawbg-' + json.showRawbg).prop('checked', true);
        } else {
            json.showRawbg = 'never';
            $('#show-rawbg-option').hide();
        }

        json.customTitle = setDefault(json.customTitle, app.defaults.customTitle);
        $('h1.customTitle').text(json.customTitle);
        $('input#customTitle').prop('value', json.customTitle);

        json.theme = setDefault(json.theme, app.defaults.theme);
        if (json.theme == 'colors') {
            $('#theme-colors-browser').prop('checked', true);
        } else {
            $('#theme-default-browser').prop('checked', true);
        }

        json.timeFormat = setDefault(json.timeFormat, app.defaults.timeFormat);

        if (json.timeFormat == '24') {
            $('#24-browser').prop('checked', true);
        } else {
            $('#12-browser').prop('checked', true);
        }
    }
    catch(err) {
        console.error(err);
        showLocalstorageError();
    }

    return json;
}

function setDefault(variable, defaultValue) {
    if (typeof(variable) === 'object') {
        return defaultValue;
    }
    return variable;
}

function storeInBrowser(data) {

    for (var k in data) {
        if (data.hasOwnProperty(k)) {
            browserStorage.set(k, data[k]);
        }
    }

}

function getQueryParms() {
    var params = {};
    if (location.search) {
        location.search.substr(1).split('&').forEach(function(item) {
            params[item.split('=')[0]] = item.split('=')[1].replace(/[_\+]/g, ' ');
        });
    }
    return params;
}

function isTouch() {
    try { document.createEvent('TouchEvent'); return true; }
    catch (e) { return false; }
}

function closeDrawer(id, callback) {
    openDraw = null;
    $("html, body").animate({ scrollTop: 0 });
    $(id).animate({right: '-300px'}, 300, function () {
        $(id).css('display', 'none');
        if (callback) callback();
    });
}

function toggleDrawer(id, openCallback, closeCallback) {

    function openDrawer(id, callback) {
        function closeOpenDraw(callback) {
            if (openDraw) {
                closeDrawer(openDraw, callback);
            } else {
                callback()
            }
        }

        closeOpenDraw(function () {
            openDraw = id;
            $(id).css('display', 'block').animate({right: '0'}, 300, function () {
                if (callback) callback();
            });
        });

    }

    if (openDraw == id) {
        closeDrawer(id, closeCallback);
    } else {
        openDrawer(id, openCallback);
    }

}

function initTreatmentDrawer()  {
    $('#eventType').val('BG Check').focus();
    $('#glucoseValue').val('').attr('placeholder', 'Value in ' + browserSettings.units);
    $('#meter').prop('checked', true);
    $('#carbsGiven').val('');
    $('#insulinGiven').val('');
    $('#preBolus').val(0);
    $('#notes').val('');
    $('#enteredBy').val(browserStorage.get('enteredBy') || '');
    $('#nowtime').prop('checked', true);
    $('#eventTimeValue').val(currentTime());
}

function currentTime() {
    var now = new Date();<commit_msg>don't set focus to eventType since it causes iOS to open the selection wheel<commit_after>'use strict';

var openDraw = null;

function rawBGsEnabled() {
    return app.enabledOptions && app.enabledOptions.indexOf('rawbg') > -1;
}

function getBrowserSettings(storage) {
    var json = {};

    function scaleBg(bg) {
        if (json.units == 'mmol') {
            return (Math.round((bg / 18) * 10) / 10).toFixed(1);
        } else {
            return bg;
        }
    }

    function appendThresholdValue(threshold) {
        return app.alarm_types.indexOf('simple') == -1 ? '' : ' (' + scaleBg(threshold) + ')';
    }

    try {
        var json = {
            'units': storage.get('units'),
            'alarmUrgentHigh': storage.get('alarmUrgentHigh'),
            'alarmHigh': storage.get('alarmHigh'),
            'alarmLow': storage.get('alarmLow'),
            'alarmUrgentLow': storage.get('alarmUrgentLow'),
            'nightMode': storage.get('nightMode'),
            'showRawbg': storage.get('showRawbg'),
            'customTitle': storage.get('customTitle'),
            'theme': storage.get('theme'),
            'timeFormat': storage.get('timeFormat')
        };

        // Default browser units to server units if undefined.
        json.units = setDefault(json.units, app.units);
        if (json.units == 'mmol') {
            $('#mmol-browser').prop('checked', true);
        } else {
            $('#mgdl-browser').prop('checked', true);
        }

        json.alarmUrgentHigh = setDefault(json.alarmUrgentHigh, app.defaults.alarmUrgentHigh);
        json.alarmHigh = setDefault(json.alarmHigh, app.defaults.alarmHigh);
        json.alarmLow = setDefault(json.alarmLow, app.defaults.alarmLow);
        json.alarmUrgentLow = setDefault(json.alarmUrgentLow, app.defaults.alarmUrgentLow);
        $('#alarm-urgenthigh-browser').prop('checked', json.alarmUrgentHigh).next().text('Urgent High Alarm' + appendThresholdValue(app.thresholds.bg_high));
        $('#alarm-high-browser').prop('checked', json.alarmHigh).next().text('High Alarm' + appendThresholdValue(app.thresholds.bg_target_top));
        $('#alarm-low-browser').prop('checked', json.alarmLow).next().text('Low Alarm' + appendThresholdValue(app.thresholds.bg_target_bottom));
        $('#alarm-urgentlow-browser').prop('checked', json.alarmUrgentLow).next().text('Urgent Low Alarm' + appendThresholdValue(app.thresholds.bg_low));

        json.nightMode = setDefault(json.nightMode, app.defaults.nightMode);
        $('#nightmode-browser').prop('checked', json.nightMode);

        if (rawBGsEnabled()) {
            $('#show-rawbg-option').show();
            json.showRawbg = setDefault(json.showRawbg, app.defaults.showRawbg);
            $('#show-rawbg-' + json.showRawbg).prop('checked', true);
        } else {
            json.showRawbg = 'never';
            $('#show-rawbg-option').hide();
        }

        json.customTitle = setDefault(json.customTitle, app.defaults.customTitle);
        $('h1.customTitle').text(json.customTitle);
        $('input#customTitle').prop('value', json.customTitle);

        json.theme = setDefault(json.theme, app.defaults.theme);
        if (json.theme == 'colors') {
            $('#theme-colors-browser').prop('checked', true);
        } else {
            $('#theme-default-browser').prop('checked', true);
        }

        json.timeFormat = setDefault(json.timeFormat, app.defaults.timeFormat);

        if (json.timeFormat == '24') {
            $('#24-browser').prop('checked', true);
        } else {
            $('#12-browser').prop('checked', true);
        }
    }
    catch(err) {
        console.error(err);
        showLocalstorageError();
    }

    return json;
}

function setDefault(variable, defaultValue) {
    if (typeof(variable) === 'object') {
        return defaultValue;
    }
    return variable;
}

function storeInBrowser(data) {

    for (var k in data) {
        if (data.hasOwnProperty(k)) {
            browserStorage.set(k, data[k]);
        }
    }

}

function getQueryParms() {
    var params = {};
    if (location.search) {
        location.search.substr(1).split('&').forEach(function(item) {
            params[item.split('=')[0]] = item.split('=')[1].replace(/[_\+]/g, ' ');
        });
    }
    return params;
}

function isTouch() {
    try { document.createEvent('TouchEvent'); return true; }
    catch (e) { return false; }
}

function closeDrawer(id, callback) {
    openDraw = null;
    $("html, body").animate({ scrollTop: 0 });
    $(id).animate({right: '-300px'}, 300, function () {
        $(id).css('display', 'none');
        if (callback) callback();
    });
}

function toggleDrawer(id, openCallback, closeCallback) {

    function openDrawer(id, callback) {
        function closeOpenDraw(callback) {
            if (openDraw) {
                closeDrawer(openDraw, callback);
            } else {
                callback()
            }
        }

        closeOpenDraw(function () {
            openDraw = id;
            $(id).css('display', 'block').animate({right: '0'}, 300, function () {
                if (callback) callback();
            });
        });

    }

    if (openDraw == id) {
        closeDrawer(id, closeCallback);
    } else {
        openDrawer(id, openCallback);
    }

}

function initTreatmentDrawer()  {
    $('#eventType').val('BG Check');
    $('#glucoseValue').val('').attr('placeholder', 'Value in ' + browserSettings.units);
    $('#meter').prop('checked', true);
    $('#carbsGiven').val('');
    $('#insulinGiven').val('');
    $('#preBolus').val(0);
    $('#notes').val('');
    $('#enteredBy').val(browserStorage.get('enteredBy') || '');
    $('#nowtime').prop('checked', true);
    $('#eventTimeValue').val(currentTime());
}

function currentTime() {
    var now = new Date();
{'fraction_non_alphanumeric': 0.1779199446701824, 'fraction_numerical': 0.005360076078499179, 'mean_word_length': 2.6048613275163603, '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': '3586809', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3459, 'n_words': 562}