<commit_before>/**
 * Copyright (C) 2012 LShift Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$(function() {

Diffa.Views.InventoryUploader = Backbone.View.extend({
  events: {
    'submit form': 'uploadInventory',
    'change select[name=endpoint]': 'selectEndpoint'
  },
  setListColumnThreshold: 10,
  templates: {
    rangeConstraint: _.template('<div class="category" data-constraint="<%= name %>" data-constraint-type="<%= dataType %>">' +
                      '<h5 class="name"><%= name %> (<%= dataType %> range)</h5>' +
                      '<div>' +
                        '<label for="<%= name %>_range_start">Start:</label>' +
                        '<span class="clearable_input">' +
                          '<input id="<%= name %>_range_start" type="text" name="start" placeholder="<%= placeholder %>">' +
                          '<span class="clear"></span>' +
                        '</span>' +
                      '</div>' +
                      '<div>' +
                        '<label for="<%= name %>_range_end">End:</label>' +
                        '<span class="clearable_input">' +
                          '<input id="<%= name %>_range_end" type="text" name="end" placeholder="<%= placeholder %>">' +
                          '<span class="clear"></span>' +
                        '</span>' +
                      '</div>' +
                     '</div>'),
    prefixConstraint: _.template('<div class="category" data-constraint="<%= name %>">' +
                        '<h5 class="name"><%= name %> (prefix)</h5>' +
                        '<input type="text" name="prefix">' +
                      '</div>'),
    setConstraint: _.template('<div class="category" data-constraint="<%= name %>">' +
                        '<h5 class="name"><%= name %> (set)</h5>' +
                        '<% _.each(values, function(value) { %>' +
                          '<input type="checkbox" value="<%= value %>" id="constraint_<%= name %>_<%= value %>">' +
                          '<label for="constraint_<%= name %>_<%= value %>"><%= value %></label>' +
                          '<br>' +
                        '<% }); %>' +
                      '</div>'),
    setConstraintWithColumns: _.template('<div class="category" data-constraint="<%= name %>">' +
                        '<h5 class="name"><%= name %> (set)</h5>' +
                        '<div class="column_1">' +
                        '<% _.each(valuesFirst, function(value) { %>' +
                          '<input type="checkbox" value="<%= value %>" id="constraint_<%= name %>_<%= value %>">' +
                          '<label for="constraint_<%= name %>_<%= value %>"><%= value %></label>' +
                          '<br>' +
                        '<% }); %>' +
                        '</div>' +
                        '<div class="column_2">' +
                        '<% _.each(valuesSecond, function(value) { %>' +
                          '<input type="checkbox" value="<%= value %>" id="constraint_<%= name %>_<%= value %>">' +
                          '<label for="constraint_<%= name %>_<%= value %>"><%= value %></label>' +
                          '<br>' +
                        '<% }); %>' +
                        '</div>' +
                      '</div>')
  },

  initialize: function() {
    var self = this;

    _.bindAll(this, "render", "addOne", "onEndpointListUpdate");

    this.collection.bind('reset', this.render);
    this.collection.bind('add', this.addOne);
    this.collection.bind('remove', this.onEndpointListUpdate);

    $(this.el).html(window.JST['data/inventory-upload'])
    this.delegateEvents(this.events);
    
    this.selectList = this.$('select[name=endpoint]');

    this.render();
    this.collection.ensureFetched();
  },

  render: function() {
    var self = this;

    this.selectList.empty();
    this.collection.each(this.addOne);

    this.onEndpointListUpdate();

    return this;
  },

  addOne: function(e) {
    $('<option value="' + e.id + '">' + e.get('name') + '</option>').appendTo(this.selectList);
    this.onEndpointListUpdate();
  },

  onEndpointListUpdate: function() {
    this.selectEndpoint();    // Trigger an endpoint selection to ensure the attributes are shown
  },

  selectEndpoint: function(e) {
    var self = this;

    var selectedEndpoint = this.collection.get(this.selectList.val());
    if (selectedEndpoint && this.currentEndpoint && selectedEndpoint.id == this.currentEndpoint.id) return;

    this.currentEndpoint = selectedEndpoint;

    var constraints = this.$('.constraints');
    constraints.empty();

    if (!selectedEndpoint) return;

    selectedEndpoint.rangeCategories.each(function(rangeCat) {
      var templateVars = rangeCat.toJSON();

      if (rangeCat.get("dataType") == "date") {
        templateVars["placeholder"] = "Unbounded date";
      } else {
        templateVars["placeholder"] = "";
      }

      constraints.append(self.templates.rangeConstraint(templateVars));

      // add date picking to the range inputs

      var lowerBound = rangeCat.get("lower");
      var upperBound = rangeCat.get("upper");

      if (lowerBound) {
        var allowOld = false;<commit_msg>Display placeholders for other range datatypes.<commit_after>/**
 * Copyright (C) 2012 LShift Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$(function() {

Diffa.Views.InventoryUploader = Backbone.View.extend({
  events: {
    'submit form': 'uploadInventory',
    'change select[name=endpoint]': 'selectEndpoint'
  },
  setListColumnThreshold: 10,
  templates: {
    rangeConstraint: _.template('<div class="category" data-constraint="<%= name %>" data-constraint-type="<%= dataType %>">' +
                      '<h5 class="name"><%= name %> (<%= dataType %> range)</h5>' +
                      '<div>' +
                        '<label for="<%= name %>_range_start">Start:</label>' +
                        '<span class="clearable_input">' +
                          '<input id="<%= name %>_range_start" type="text" name="start" placeholder="<%= placeholder %>">' +
                          '<span class="clear"></span>' +
                        '</span>' +
                      '</div>' +
                      '<div>' +
                        '<label for="<%= name %>_range_end">End:</label>' +
                        '<span class="clearable_input">' +
                          '<input id="<%= name %>_range_end" type="text" name="end" placeholder="<%= placeholder %>">' +
                          '<span class="clear"></span>' +
                        '</span>' +
                      '</div>' +
                     '</div>'),
    prefixConstraint: _.template('<div class="category" data-constraint="<%= name %>">' +
                        '<h5 class="name"><%= name %> (prefix)</h5>' +
                        '<input type="text" name="prefix">' +
                      '</div>'),
    setConstraint: _.template('<div class="category" data-constraint="<%= name %>">' +
                        '<h5 class="name"><%= name %> (set)</h5>' +
                        '<% _.each(values, function(value) { %>' +
                          '<input type="checkbox" value="<%= value %>" id="constraint_<%= name %>_<%= value %>">' +
                          '<label for="constraint_<%= name %>_<%= value %>"><%= value %></label>' +
                          '<br>' +
                        '<% }); %>' +
                      '</div>'),
    setConstraintWithColumns: _.template('<div class="category" data-constraint="<%= name %>">' +
                        '<h5 class="name"><%= name %> (set)</h5>' +
                        '<div class="column_1">' +
                        '<% _.each(valuesFirst, function(value) { %>' +
                          '<input type="checkbox" value="<%= value %>" id="constraint_<%= name %>_<%= value %>">' +
                          '<label for="constraint_<%= name %>_<%= value %>"><%= value %></label>' +
                          '<br>' +
                        '<% }); %>' +
                        '</div>' +
                        '<div class="column_2">' +
                        '<% _.each(valuesSecond, function(value) { %>' +
                          '<input type="checkbox" value="<%= value %>" id="constraint_<%= name %>_<%= value %>">' +
                          '<label for="constraint_<%= name %>_<%= value %>"><%= value %></label>' +
                          '<br>' +
                        '<% }); %>' +
                        '</div>' +
                      '</div>')
  },

  initialize: function() {
    var self = this;

    _.bindAll(this, "render", "addOne", "onEndpointListUpdate");

    this.collection.bind('reset', this.render);
    this.collection.bind('add', this.addOne);
    this.collection.bind('remove', this.onEndpointListUpdate);

    $(this.el).html(window.JST['data/inventory-upload'])
    this.delegateEvents(this.events);
    
    this.selectList = this.$('select[name=endpoint]');

    this.render();
    this.collection.ensureFetched();
  },

  render: function() {
    var self = this;

    this.selectList.empty();
    this.collection.each(this.addOne);

    this.onEndpointListUpdate();

    return this;
  },

  addOne: function(e) {
    $('<option value="' + e.id + '">' + e.get('name') + '</option>').appendTo(this.selectList);
    this.onEndpointListUpdate();
  },

  onEndpointListUpdate: function() {
    this.selectEndpoint();    // Trigger an endpoint selection to ensure the attributes are shown
  },

  selectEndpoint: function(e) {
    var self = this;

    var selectedEndpoint = this.collection.get(this.selectList.val());
    if (selectedEndpoint && this.currentEndpoint && selectedEndpoint.id == this.currentEndpoint.id) return;

    this.currentEndpoint = selectedEndpoint;

    var constraints = this.$('.constraints');
    constraints.empty();

    if (!selectedEndpoint) return;

    selectedEndpoint.rangeCategories.each(function(rangeCat) {
      var templateVars = rangeCat.toJSON();
      var type = rangeCat.get("dataType");
      var placeholder = "";

      switch(type) {
        case "date":     placeholder = "Unbounded date";     break;
        case "datetime": placeholder = "Unbounded datetime"; break;
        case "int":      placeholder = "Unbounded";     break;
      }

      templateVars["placeholder"] = placeholder;

      constraints.append(self.templates.rangeConstraint(templateVars));

      // add date picking to the range inputs

      var lowerBound = rangeCat.get("lower");
      var upperBound = rangeCat.get("upper");

      if (lowerBound) {
        var allowOld = false;
{'fraction_non_alphanumeric': 0.17463172319287426, 'fraction_numerical': 0.0034258307639602604, 'mean_word_length': 2.0165331955567036, 'pattern_counts': {'":': 3, '<': 189, '<?xml version=': 0, '>': 189, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2953523', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3119, 'n_words': 703}