<commit_before>/*
 * A JQuery UI Widget to create a RRule compatible inputs for use inside a form
 * Requires: rrule.js (http://jkbr.github.io/rrule/)
 *          underscore.js
 * Author: <NAME>, 2013
 */

// add helpful constants to RRule
RRule.FREQUENCY_NAMES = ['year','month','week','day','hour','minute','second'];
RRule.DAYCODES = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'];
RRule.DAYNAMES = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
RRule.MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
// note, month num for these values should be one-based, not zero-based

$.widget("rrule.recurringinput", {
  // default options
  options: {
    freq: RRule.WEEKLY,
    count: 1
  },

  _create: function() {
    //set up inputs
    // TODO: convert to underscore template
    var tmpl = "<!-- janky inline template -->";

    //frequency
    tmpl += '<label class="controls">Repeat ';
    tmpl += '<select name="freq">';
    _.each(RRule.FREQUENCIES, function(element, index) {
      var f = window['RRule'][element]; //use window[] syntax to get object from string
      tmpl += '<option value='+f+'>'+element.toLowerCase()+'</option>';
    });
    tmpl += '</select>';
    tmpl += '</label>';


    tmpl += '<label class="controls">Every ';
    tmpl += '<input type="number" value="1" min="1" max="100" name="interval"/>';
    tmpl += '&nbsp;<span id="frequency_name"></span>';
    tmpl += '</label>';

    // repeat options, frequency specific
    // data-freq should be lowercase value from FREQUENCY_NAMES
    
    //bymonth
    tmpl += '<div class="repeat-options controls form-inline" data-freq="monthly"><label>Only in ';
    _.each(RRule.MONTHS, function(element, index) {
      tmpl += '<label class="inline">';
      tmpl += '<input type="checkbox" name="bymonth" value="'+(index+1)+'" />';
      tmpl += element+'</label>';
    });
    tmpl += '</div>';

    //byweekday
    tmpl += '<div class="repeat-options controls form-inline" data-freq="weekly">';
    tmpl += '<label for="byweekday">On </label>';
    _.each(RRule.DAYCODES, function(element, index) {
      var d = window['RRule'][element];
      tmpl += '<label class="inline">';
      tmpl += '<input type="checkbox" name="byweekday" value="'+d.weekday+'" />';
      tmpl += RRule.DAYNAMES[index]+'</label>';
    });
    tmpl += '</div>';

    //byhour
    tmpl += '<label class="repeat-options" data-freq="hourly">Only at ';
    tmpl += '<input name="byhour" /> <span>o\'clock</span></label>';

    //byminute
    tmpl += '<label class="repeat-options" data-freq="minutely">Only at ';
    tmpl += '<input name="byminute" />  <span>minutes<span></label>';

    //bysecond
    tmpl += '<label class="repeat-options" data-freq="secondly">Only at ';
    tmpl += '<input name="bysecond" /> <span>seconds</span></label>';

    // end repeat options

    // starts on
    tmpl += '<label>Start ';
    tmpl += '<input name="dtstart" type="date"/>';
    tmpl += '</label>';

    // end on
    tmpl += '<div class="end-options controls">';
    tmpl += '<label for="end">End </label>';

    tmpl += '<label class="inline">';
    tmpl += '<input type="radio" name="end" value=0" checked="checked"/> Never</label>';
    tmpl += '<label class="inline">';
    tmpl += '<input type="radio" name="end" value=1" /> After <input type="number" max="1000" min="1" value="" name="count"/> occurences';
    tmpl += '</label>';
    tmpl += '<label class="inline">';
    tmpl += '<input type="radio" name="end" value=2"> On date <input type="date" name="until"/>';
    tmpl += '</label>';

    tmpl += '</div>';

    // summary
    tmpl += '<label for="output">Summary ';
    tmpl += '<em id="text-output"></em>'; // human readable
    tmpl += '<br><code id="rrule-output"></code>'; // ugly rrule
    tmpl += '</label>';

    //render template
    this.element.append(tmpl);

    //save input references to widget for later use
    this.frequency_select = this.element.find('select[name="freq"]');
    this.interval_input = this.element.find('input[name="interval"]');
    //bind event handlers
    this._on(this.element.find('select, input'), {
        'change': this._refresh
    });

    //set sensible defaults
    this.frequency_select.val(2);
    this.interval_input.val(1);

    //refresh
    this._refresh();
 },

  // called on create and when changing options
  _refresh: function() {
    //determine selected frequency
    var frequency = this.frequency_select.find("option:selected");
    // fill in frequency-name span
    this.element.find('#frequency_name').text(RRule.FREQUENCY_NAMES[frequency.val()]);
    // and pluralize
    if (this.interval_input.val() > 1) {
      this.element.find('#frequency_name').append('s');
    }

    // display appropriate repeat options
    this.element.find('.repeat-options').hide();
    if (frequency !== "") {
      this.element.find('.repeat-options').filter('[data-freq='+frequency.text()+']').show();
    }

    //determine rrule
    var rrule = this._getRRule();

    $('#rrule-output').text(rrule.toString());
    $('#text-output').text(rrule.toText());
  },

  _getFormValues: function($form) {
    //modified from rrule/tests/demo/demo.js
    var paramObj;
    paramObj = {};

    $.each($form.serializeArray(), function(_, kv) {
      if (paramObj.hasOwnProperty(kv.name)) {
        paramObj[kv.name] = $.makeArray(paramObj[kv.name]);
        return paramObj[kv.name].push(kv.value);
      } else {
        return paramObj[kv.name] = kv.value;
      }
    });
    return paramObj;
  },<commit_msg>fix weird bugs<commit_after>/*
 * A JQuery UI Widget to create a RRule compatible inputs for use inside a form
 * Requires: rrule.js (http://jkbr.github.io/rrule/)
 *          underscore.js
 * Author: <NAME>, 2013
 */

// add helpful constants to RRule
RRule.FREQUENCY_NAMES = ['year','month','week','day','hour','minute','second'];
RRule.DAYCODES = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'];
RRule.DAYNAMES = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
RRule.MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
// note, month num for these values should be one-based, not zero-based

$.widget("rrule.recurringinput", {
  // default options
  options: {
    freq: RRule.WEEKLY,
    count: 1
  },

  _create: function() {
    //set up inputs
    // TODO: convert to underscore template
    var tmpl = "<!-- janky inline template -->";

    //frequency
    tmpl += '<label class="controls">Repeat ';
    tmpl += '<select name="freq">';
    _.each(RRule.FREQUENCIES, function(element, index) {
      var f = window['RRule'][element]; //use window[] syntax to get object from string
      tmpl += '<option value='+f+'>'+element.toLowerCase()+'</option>';
    });
    tmpl += '</select>';
    tmpl += '</label>';


    tmpl += '<label class="controls">Every ';
    tmpl += '<input type="number" value="1" min="1" max="100" name="interval"/>';
    tmpl += '&nbsp;<span id="frequency_name"></span>';
    tmpl += '</label>';

    // repeat options, frequency specific
    // data-freq should be lowercase value from FREQUENCY_NAMES
    
    //bymonth
    tmpl += '<div class="repeat-options controls form-inline" data-freq="monthly"><label>Only in ';
    _.each(RRule.MONTHS, function(element, index) {
      tmpl += '<label class="inline">';
      tmpl += '<input type="checkbox" name="bymonth" value="'+(index+1)+'" />';
      tmpl += element+'</label>';
    });
    tmpl += '</div>';

    //byweekday
    tmpl += '<div class="repeat-options controls form-inline" data-freq="weekly">';
    tmpl += '<label for="byweekday">On </label>';
    _.each(RRule.DAYCODES, function(element, index) {
      var d = window['RRule'][element];
      tmpl += '<label class="inline">';
      tmpl += '<input type="checkbox" name="byweekday" value="'+d.weekday+'" />';
      tmpl += RRule.DAYNAMES[index]+'</label>';
    });
    tmpl += '</div>';

    //byhour
    tmpl += '<label class="repeat-options" data-freq="hourly">Only at ';
    tmpl += '<input name="byhour" /> <span>o\'clock</span></label>';

    //byminute
    tmpl += '<label class="repeat-options" data-freq="minutely">Only at ';
    tmpl += '<input name="byminute" />  <span>minutes<span></label>';

    //bysecond
    tmpl += '<label class="repeat-options" data-freq="secondly">Only at ';
    tmpl += '<input name="bysecond" /> <span>seconds</span></label>';

    // end repeat options

    // starts on
    tmpl += '<label>Start ';
    tmpl += '<input name="dtstart" type="date"/>';
    tmpl += '</label>';

    // end on
    tmpl += '<div class="end-options controls">';
    tmpl += '<label for="end">End </label>';

    tmpl += '<label class="inline">';
    tmpl += '<input type="radio" name="end" value="0" checked="checked"/> Never</label>';
    tmpl += '<label class="inline">';
    tmpl += '<input type="radio" name="end" value="1" /> After <input type="number" max="1000" min="1" value="" name="count"/> occurences';
    tmpl += '</label>';
    tmpl += '<label class="inline">';
    tmpl += '<input type="radio" name="end" value="2"> On date <input type="date" name="until"/>';
    tmpl += '</label>';

    tmpl += '</div>';

    // summary
    tmpl += '<label for="output">Summary ';
    tmpl += '<em id="text-output"></em>'; // human readable
    tmpl += '<br><code id="rrule-output"></code>'; // ugly rrule
    tmpl += '</label>';

    //render template
    this.element.append(tmpl);

    //save input references to widget for later use
    this.frequency_select = this.element.find('select[name="freq"]');
    this.interval_input = this.element.find('input[name="interval"]');
    this.end_input = this.element.find('input[type="radio"][name="end"]');
    end_input = this.end_input;

    //bind event handlers
    this._on(this.element.find('select, input'), {
      'change': this._refresh
    });

    //set sensible defaults
    this.frequency_select.val(2);
    this.interval_input.val(1);

    //refresh
    this._refresh();
 },

  // called on create and when changing options
  _refresh: function() {
    //determine selected frequency
    var frequency = this.frequency_select.find("option:selected");
    // fill in frequency-name span
    this.element.find('#frequency_name').text(RRule.FREQUENCY_NAMES[frequency.val()]);
    // and pluralize
    if (this.interval_input.val() > 1) {
      this.element.find('#frequency_name').append('s');
    }

    // display appropriate repeat options
    var repeatOptions = this.element.find('.repeat-options');
    repeatOptions.hide();
    
    if (frequency !== "") {
      //show options for the selected frequency
      var selectedOptions = repeatOptions.filter('[data-freq='+frequency.text()+']');
      selectedOptions.show();
      
      //and clear descendent fields for the others
      nonSelectedOptions = repeatOptions.filter('[data-freq!='+frequency.text()+']');
      nonSelectedOptions.find('input[type=checkbox]:checked').removeAttr('checked');
      nonSelectedOptions.find('select').val('');
    }

    //reset end
    switch (this.end_input.filter(':checked').val()) {
      case "0":
        //never, clear count and until
        this.end_input.siblings('input[name=count]').val('');
        this.end_input.siblings('input[name=until]').val('');
        break;
      case "1":
        //after, clear until
        this.end_input.siblings('input[name=until]').val('');
        break;
      case "2":
        //date, clear count
        this.end_input.siblings('input[name=count]').val('');
        break;
    }

    //determine rrule
    var rrule = this._getRRule();

    $('#rrule-output').text(rrule.toString());
    $('#text-output').text(rrule.toText());
  },

  _getFormValues: function($form) {
    //modified from rrule/tests/demo/demo.js
    var paramObj;
    paramObj = {};

    $.each($form.serializeArray(), function(_, kv) {
      if (paramObj.hasOwnProperty(kv.name)) {
        paramObj[kv.name] = $.makeArray(paramObj[kv.name]);
        return paramObj[kv.name].push(kv.value);
      } else {
        return paramObj[kv.name] = kv.value;
      }
    });
    return paramObj;
  },
{'fraction_non_alphanumeric': 0.20608802961744138, 'fraction_numerical': 0.0038667215137803373, 'mean_word_length': 3.2787750791974655, 'pattern_counts': {'":': 3, '<': 135, '<?xml version=': 0, '>': 137, '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': '1881117', 'n_tokens_mistral': 3950, 'n_tokens_neox': 3853, 'n_words': 835}