<commit_before><%# encoding: utf-8 %>
//= require_self
//= require admin/product_autcomplete

/**
This is a collection of javascript functions and whatnot
under the spree namespace that do stuff we find helpful.
Hopefully, this will evolve into a propper class.
**/

$(document).ajaxStart(function(){
  $("#progress").slideDown();
});

$(document).ajaxStop(function(){
  $("#progress").slideUp();
});

$.fn.visible = function(cond) { this[cond ? 'show' : 'hide' ]() };

show_flash_error = function(message) {
  error_div = $('.flash.error');
  if (error_div.length > 0) {
    error_div.html(message);
    error_div.show();
  } else {
    if ($("#content .toolbar").length > 0) {
      $("#content .toolbar").before('<div class="flash error">' + message + '</div>');
    } else {
      $("#content h1").before('<div class="flash error">' + message + '</div>');
    }
  }
}

// Apply to individual radio button that makes another element visible when checked
$.fn.radioControlsVisibilityOfElement = function(dependentElementSelector){
  if(!this.get(0)){ return  }
  showValue = this.get(0).value;
  radioGroup = $("input[name='" + this.get(0).name + "']");
  radioGroup.each(function(){
    $(this).click(function(){
      $(dependentElementSelector).visible(this.checked && this.value == showValue)
    });
    if(this.checked){ this.click() }
  });
}


$.fn.objectPicker = function(url){
  $(this).tokenInput(url + "&authenticity_token=" + escape(AUTH_TOKEN), {
    searchDelay          : 600,
    hintText             : strings.type_to_search,
    noResultsText        : strings.no_results,
    searchingText        : strings.searching,
    prePopulateFromInput : true
  });
};

$.fn.productPicker = function(){
  $(this).objectPicker(Spree.routes.product_search_basic);
}

$(".observe_field").on('change', function() {
  target = $(this).attr("data-update");
  ajax_indicator = $(this).attr("data-ajax-indicator") || '#busy_indicator';
  $(target).hide();
  $(ajax_indicator).show();
  $.ajax({ dataType: 'html',
           url: $(this).attr("data-base-url")+encodeURIComponent($(this).val()),
           type: 'get',
           success: function(data){
             $(target).html(data);
             $(ajax_indicator).hide();
             $(target).show();
           }
  });
});

// Defined as a function to be re-used in admin/products/new.js.erb
handle_date_picker_fields = function(){
  $('.datepicker').datepicker({
    dateFormat: "<%= ::I18n.t(:format, :scope => 'spree.date_picker', :default => 'yy/mm/dd') %>",
    dayNames: <%= ::I18n.t(:day_names, :scope => :date).to_json %>,
    dayNamesMin: <%= ::I18n.t(:abbr_day_names, :scope => :date).to_json %>,
    monthNames: <%= (::I18n.t(:month_names, :scope => :date).delete_if(&:blank?)).to_json %>,
    prevText: '<%= ::I18n.t(:previous) %>',
    nextText: '<%= ::I18n.t(:next) %>',
    showOn: "button",
    buttonImage: "<%= asset_path 'datepicker/cal.gif' %>",
    buttonImageOnly: true
  });
}

$(document).ready(function(){
  $('.add_fields').click(function() {
    var target = $(this).data("target");
    var new_table_row = $(target + ' tr:visible:last').clone();
    var new_id = new Date().getTime();
    new_table_row.find("input").each(function () {
      var el = $(this);
      el.val("");
      el.attr("id", el.attr("id").replace(/\d+/, new_id))
      el.attr("name", el.attr("name").replace(/\d+/, new_id))
    })
    $(target).append(new_table_row);
  })

  $('body').on('click', '.delete-resource', function() {
    var el = $(this);
    if (confirm(el.data("confirm"))) {
      $.ajax({
        type: 'POST',
        url: $(this).attr("href"),
        data: {
          _method: 'delete',
          authenticity_token: AUTH_TOKEN
        },
        dataType: 'script',
        success: function(response) {
          el.parents("tr").fadeOut('hide');
        },
        error: function(response, textStatus, errorThrown) {
          show_flash_error(response.responseText);
        }
      });
    }
    return false;
  });

  $('body').on('click', 'a.remove_fields', function() {
    $(this).prev("input[type=hidden]").val("1");
    $(this).closest(".fields").hide();
    return false;
  });

  $('.tokeninput.products').productPicker();

  handle_date_picker_fields();

  $(".select_properties_from_prototype").on("click", function(){
    $("#busy_indicator").show();
    var clicked_link = $(this);
    $.ajax({ dataType: 'script', url: clicked_link.attr("href"), type: 'get',
        success: function(data){
          clicked_link.parent("td").parent("tr").hide();
          $("#busy_indicator").hide();
        }
    });
    return false;
  });


  $('table.sortable').ready(function(){
    $('table.sortable tbody').sortable(
      {
        handle: '.handle',
        update: function(event, ui) {
          $("#progress").show();
          positions = {};
          $.each($('table.sortable tbody tr'), function(position, obj){
            reg = /spree_(\w+_?)+_(\d+)/;
            parts = reg.exec($(obj).attr('id'));
            if (parts) {
              positions['positions['+parts[2]+']'] = position;
            }
          });
          $.ajax({
            type: 'POST',
            dataType: 'script',
            url: $(ui.item).closest("table.sortable").data("sortable-link"),
            data: positions,
            success: function(data){ $("#progress").hide(); }
          });
        }
      });
  });

});

jQuery(document).ready(function() {
  $('a.dismiss').click(function() {
    $(this).parent().fadeOut();
  });
});
<commit_msg>Move handle_date_picker_fields to top of document ready call in admin.js.erb<commit_after><%# encoding: utf-8 %>
//= require_self
//= require admin/product_autcomplete

/**
This is a collection of javascript functions and whatnot
under the spree namespace that do stuff we find helpful.
Hopefully, this will evolve into a propper class.
**/

$(document).ajaxStart(function(){
  $("#progress").slideDown();
});

$(document).ajaxStop(function(){
  $("#progress").slideUp();
});

$.fn.visible = function(cond) { this[cond ? 'show' : 'hide' ]() };

show_flash_error = function(message) {
  error_div = $('.flash.error');
  if (error_div.length > 0) {
    error_div.html(message);
    error_div.show();
  } else {
    if ($("#content .toolbar").length > 0) {
      $("#content .toolbar").before('<div class="flash error">' + message + '</div>');
    } else {
      $("#content h1").before('<div class="flash error">' + message + '</div>');
    }
  }
}

// Apply to individual radio button that makes another element visible when checked
$.fn.radioControlsVisibilityOfElement = function(dependentElementSelector){
  if(!this.get(0)){ return  }
  showValue = this.get(0).value;
  radioGroup = $("input[name='" + this.get(0).name + "']");
  radioGroup.each(function(){
    $(this).click(function(){
      $(dependentElementSelector).visible(this.checked && this.value == showValue)
    });
    if(this.checked){ this.click() }
  });
}


$.fn.objectPicker = function(url){
  $(this).tokenInput(url + "&authenticity_token=" + escape(AUTH_TOKEN), {
    searchDelay          : 600,
    hintText             : strings.type_to_search,
    noResultsText        : strings.no_results,
    searchingText        : strings.searching,
    prePopulateFromInput : true
  });
};

$.fn.productPicker = function(){
  $(this).objectPicker(Spree.routes.product_search_basic);
}

$(".observe_field").on('change', function() {
  target = $(this).attr("data-update");
  ajax_indicator = $(this).attr("data-ajax-indicator") || '#busy_indicator';
  $(target).hide();
  $(ajax_indicator).show();
  $.ajax({ dataType: 'html',
           url: $(this).attr("data-base-url")+encodeURIComponent($(this).val()),
           type: 'get',
           success: function(data){
             $(target).html(data);
             $(ajax_indicator).hide();
             $(target).show();
           }
  });
});

// Defined as a function to be re-used in admin/products/new.js.erb
handle_date_picker_fields = function(){
  $('.datepicker').datepicker({
    dateFormat: "<%= ::I18n.t(:format, :scope => 'spree.date_picker', :default => 'yy/mm/dd') %>",
    dayNames: <%= ::I18n.t(:day_names, :scope => :date).to_json %>,
    dayNamesMin: <%= ::I18n.t(:abbr_day_names, :scope => :date).to_json %>,
    monthNames: <%= (::I18n.t(:month_names, :scope => :date).delete_if(&:blank?)).to_json %>,
    prevText: '<%= ::I18n.t(:previous) %>',
    nextText: '<%= ::I18n.t(:next) %>',
    showOn: "button",
    buttonImage: "<%= asset_path 'datepicker/cal.gif' %>",
    buttonImageOnly: true
  });
}

$(document).ready(function(){
  handle_date_picker_fields();

  $('.add_fields').click(function() {
    var target = $(this).data("target");
    var new_table_row = $(target + ' tr:visible:last').clone();
    var new_id = new Date().getTime();
    new_table_row.find("input").each(function () {
      var el = $(this);
      el.val("");
      el.attr("id", el.attr("id").replace(/\d+/, new_id))
      el.attr("name", el.attr("name").replace(/\d+/, new_id))
    })
    $(target).append(new_table_row);
  })

  $('body').on('click', '.delete-resource', function() {
    var el = $(this);
    if (confirm(el.data("confirm"))) {
      $.ajax({
        type: 'POST',
        url: $(this).attr("href"),
        data: {
          _method: 'delete',
          authenticity_token: AUTH_TOKEN
        },
        dataType: 'script',
        success: function(response) {
          el.parents("tr").fadeOut('hide');
        },
        error: function(response, textStatus, errorThrown) {
          show_flash_error(response.responseText);
        }
      });
    }
    return false;
  });

  $('body').on('click', 'a.remove_fields', function() {
    $(this).prev("input[type=hidden]").val("1");
    $(this).closest(".fields").hide();
    return false;
  });

  $('.tokeninput.products').productPicker();

  $(".select_properties_from_prototype").on("click", function(){
    $("#busy_indicator").show();
    var clicked_link = $(this);
    $.ajax({ dataType: 'script', url: clicked_link.attr("href"), type: 'get',
        success: function(data){
          clicked_link.parent("td").parent("tr").hide();
          $("#busy_indicator").hide();
        }
    });
    return false;
  });


  $('table.sortable').ready(function(){
    $('table.sortable tbody').sortable(
      {
        handle: '.handle',
        update: function(event, ui) {
          $("#progress").show();
          positions = {};
          $.each($('table.sortable tbody tr'), function(position, obj){
            reg = /spree_(\w+_?)+_(\d+)/;
            parts = reg.exec($(obj).attr('id'));
            if (parts) {
              positions['positions['+parts[2]+']'] = position;
            }
          });
          $.ajax({
            type: 'POST',
            dataType: 'script',
            url: $(ui.item).closest("table.sortable").data("sortable-link"),
            data: positions,
            success: function(data){ $("#progress").hide(); }
          });
        }
      });
  });

});

jQuery(document).ready(function() {
  $('a.dismiss').click(function() {
    $(this).parent().fadeOut();
  });
});

{'fraction_non_alphanumeric': 0.21106428763561375, 'fraction_numerical': 0.0043037747691204165, 'mean_word_length': 2.94692144373673, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, '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': '405736', 'n_tokens_mistral': 3597, 'n_tokens_neox': 3540, 'n_words': 605}