<commit_before>const _ = require('lodash');
const $ = require('wetfish-basic');
const Highlight = require('./highlight');
const timegrid = require('./timegrid');

//let test = require('./test.html.tpl');
let grid = require('../../templates/grid.html.tpl');
let previewContainer = null;

$(document).ready(function()
{
    let editForm = document.querySelector('form.edit-schedule');
    if(editForm){
        let previewContainer = editForm.querySelector('.preview');
        let formData = parseForm(formValues(editForm));
        previewContainer.innerHTML = makePreview(formData);

        //this creates a preview whenever the schedule edit form changes
        $('form.edit-schedule').on('change', function(e){
            if(e.target.name === "does_slot_repeat"){
                toggleRepeatInput(e.target);
            }

            updateEndTime();

            updatePreview();
        });

        let repeatToggle = editForm.querySelector('[name="does_slot_repeat"]');
        let slotRepeat   = editForm.querySelector('[name="slot_repeat"]');
        let slotDuration = parseTime(formData.duration);
        let totalLength  = parseTime(formData.end_time)-parseTime(formData.start_time);

        document.querySelector('form.edit-schedule .slot-repeat .slot-end').innerText = formData.end_time;
        //console.log(slotRepeat, formData, Math.floor(totalLength/slotDuration));
        if(typeof totalLength === 'number' && typeof slotDuration ==='number'){
            slotRepeat.value = Math.floor(totalLength/slotDuration);
            console.log(`slot repeats ${slotRepeat.value} times`);
            if(slotRepeat.value !== 0){
                repeatToggle.checked = true;
                editForm.querySelector('.slot-repeat').classList.remove('hidden');
            }
            updatePreview();
        }
    }

});

function toggleRepeatInput(e)
{
    let editForm = document.querySelector('form.edit-schedule');
    let slotRepeatContainer = editForm.querySelector('.slot-repeat');
    let slotRepeatField = editForm.querySelector('[name="slot_repeat"]');

    if(e.checked){
        slotRepeatContainer.classList.remove('hidden');
    }
    else{
        slotRepeatContainer.classList.add('hidden');
        slotRepeatField.value = 1;
        updateEndTime();
    }
}

function updateEndTime()
{
    let editForm    = document.querySelector('form.edit-schedule');
    let slotRepeats = parseInt(editForm.querySelector('[name="slot_repeat"]').value || 1);
    let formData    = parseForm(formValues(editForm));
    let duration    = parseTime(formData.duration);
    let shiftLength = duration * slotRepeats;
    let endTime     = parseTime(formData.start_time) + shiftLength;

    // console.log('start, duration, end', parseTime(formData.start_time), parseTime(formData.duration), endTime);

    console.log('end time is now: ',formatTime(endTime), endTime);
    document.querySelector('form.edit-schedule [name="end_time"]').value = formatTime(endTime);
    document.querySelector('form.edit-schedule .slot-repeat .slot-end').innerText = decorateTime(endTime);
}

function updatePreview()<commit_msg>added default value for repeats<commit_after>const _ = require('lodash');
const $ = require('wetfish-basic');
const Highlight = require('./highlight');
const timegrid = require('./timegrid');

//let test = require('./test.html.tpl');
let grid = require('../../templates/grid.html.tpl');
let previewContainer = null;

$(document).ready(function()
{
    let editForm = document.querySelector('form.edit-schedule');
    if(editForm){
        let previewContainer = editForm.querySelector('.preview');
        let formData = parseForm(formValues(editForm));
        previewContainer.innerHTML = makePreview(formData);

        //this creates a preview whenever the schedule edit form changes
        $('form.edit-schedule').on('change', function(e){
            if(e.target.name === "does_slot_repeat"){
                toggleRepeatInput(e.target);
            }

            updateEndTime();

            updatePreview();
        });

        let repeatToggle = editForm.querySelector('[name="does_slot_repeat"]');
        let slotRepeat   = editForm.querySelector('[name="slot_repeat"]');
        let slotDuration = parseTime(formData.duration);
        let totalLength  = parseTime(formData.end_time)-parseTime(formData.start_time);

        document.querySelector('form.edit-schedule .slot-repeat .slot-end').innerText = formData.end_time;
        //console.log(slotRepeat, formData, Math.floor(totalLength/slotDuration));
        if(typeof totalLength === 'number' && typeof slotDuration ==='number'){
            slotRepeat.value = Math.floor(totalLength/slotDuration);
            console.log(`slot repeats ${slotRepeat.value} times`);
            if(slotRepeat.value !== 0){
                repeatToggle.checked = true;
                editForm.querySelector('.slot-repeat').classList.remove('hidden');
            }
            updatePreview();
        }
    }

});

function toggleRepeatInput(e)
{
    let editForm = document.querySelector('form.edit-schedule');
    let slotRepeatContainer = editForm.querySelector('.slot-repeat');
    let slotRepeatField = editForm.querySelector('[name="slot_repeat"]');

    if(e.checked){
        if(typeof slotRepeatField.value !== 'number'){
            slotRepeatField.value = 1;
        }
        slotRepeatContainer.classList.remove('hidden');
    }
    else{
        slotRepeatContainer.classList.add('hidden');
        slotRepeatField.value = 1;
        updateEndTime();
    }
}

function updateEndTime()
{
    let editForm    = document.querySelector('form.edit-schedule');
    let slotRepeats = parseInt(editForm.querySelector('[name="slot_repeat"]').value || 1);
    let formData    = parseForm(formValues(editForm));
    let duration    = parseTime(formData.duration);
    let shiftLength = duration * slotRepeats;
    let endTime     = parseTime(formData.start_time) + shiftLength;

    // console.log('start, duration, end', parseTime(formData.start_time), parseTime(formData.duration), endTime);

    console.log('end time is now: ',formatTime(endTime), endTime);
    document.querySelector('form.edit-schedule [name="end_time"]').value = formatTime(endTime);
    document.querySelector('form.edit-schedule .slot-repeat .slot-end').innerText = decorateTime(endTime);
}

function updatePreview()
{'fraction_non_alphanumeric': 0.14243183954873082, 'fraction_numerical': 0.0010968348480100282, 'mean_word_length': 3.3333333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2212416', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1768, 'n_words': 304}