<commit_before>/**
 * Copyright (C) 2011 Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
/*jshint evil:true*/

define([
  'jquery' /* $ */,
  'jquery.instructure_forms' /* formSubmit, defaultAjaxError */
], function($) {

  $.originalGetJSON = $.getJSON;
  $.getJSON = function(url, data, callback) {
    var xhr = $.originalGetJSON(url, data, callback);
    $.ajaxJSON.storeRequest(xhr, url, 'GET', data);
    return xhr;
  };
  var assert_option = function(data, arg) {
    if(!data[arg]) {
      throw arg + " option is required";
    }
  };
  $.ajaxJSONPreparedFiles = function(options) {
    assert_option(options, 'context_code');
    var list = [];
    var $this = this;
    var pre_list = options.files || options.file_elements || [];
    for(var idx = 0; idx < pre_list.length; idx++) {
      var item = pre_list[idx];
      item.name = (item.value || item.name).split(/(\/|\\)/).pop();
      list.push(item);
    }
    var attachments = [];
    var ready = function() {
      var data = options.formDataTarget == 'url' ? options.formData : {};
      if(options.handle_files) {
        var result = attachments;
        if(options.single_file) {
          result = attachments[0];
        }
        data = options.handle_files.call(this, result, data);
      }
      if(options.url && options.success && data !== false) {
        $.ajaxJSON(options.url, options.method, data, options.success, options.error);
      }
    };
    var uploadFile = function(parameters, file) {
      $.ajaxJSON(options.uploadDataUrl || "/files/pending", 'POST', parameters, function(data) {
        try {
        if(data && data.upload_url) {
          var post_params = data.upload_params;
          var old_name = $(file).attr('name');
          $(file).attr('name', data.file_param);
          $.ajaxJSONFiles(data.upload_url, 'POST', post_params, $(file), function(data) {
            attachments.push(data);
            $(file).attr('name', old_name);
            next.call($this);
          }, function(data) {
            $(file).attr('name', old_name);
            (options.upload_error || options.error).call($this, data);
          }, {onlyGivenParameters: data.remote_url});
        } else {
          (options.upload_error || options.error).call($this, data);
        }
        } catch(e) {
          var ex = e;
        }
      }, function() { 
        return (options.upload_error || options.error).apply(this, arguments);
      });
    };
    var next = function() {
      var item = list.shift();
      if(item) {
        uploadFile.call($this, $.extend({
          'attachment[folder_id]': options.folder_id,
          'attachment[intent]': options.intent,
          'attachment[asset_string]': options.asset_string,
          'attachment[filename]': item.name,
          'attachment[context_code]': options.context_code
        }, options.formDataTarget == 'uploadDataUrl' ? options.formData : {}), item);
      } else {
        ready.call($this);
      }
    };
    next.call($this);
  };
  $.ajaxJSONFiles = function(url, submit_type, formData, files, success, error, options) {
    var $newForm = $(document.createElement("form"));
    $newForm.attr('action', url).attr('method', submit_type);
    if(!formData.authenticity_token) {
      formData.authenticity_token = $("#ajax_authenticity_token").text();
    }
    var fileNames = {};
    files.each(function() {
      fileNames[$(this).attr('name')] = true;
    });
    for(var idx in formData) {
      if(!fileNames[idx]) {
        var $input = $(document.createElement('input'));
        $input.attr('type', 'hidden').attr('name', idx).attr('value', formData[idx]);
        $newForm.append($input);
      }
    }
    files.each(function() {
      var $newFile = $(this).clone(true);
      $(this).after($newFile);
      $newForm.append($(this));
      $(this).removeAttr('id');
    });
    $("body").append($newForm.hide());
    $newForm.formSubmit({
      fileUpload: true,
      success: success,
      onlyGivenParameters: options ? options.onlyGivenParameters : false,
      error: error
    });
    (function() {
      $newForm.submit();
    }).call($newForm);
  };
  // Wrapper for default $.ajax behavior.  On error will call
  // the default error method if no error method is provided.
  $.ajaxJSON = function(url, submit_type, data, success, error, options) {
    data = data || {};
    if(!url && error) {
      error(null, null, "URL required for requests", null);
      return;
    }
    url = url || ".";
    if(submit_type != "GET") {
      data._method = submit_type;
      submit_type = "POST";
      if(!data.authenticity_token) {
        data.authenticity_token = $("#ajax_authenticity_token").text();
      }
    }
    if($("#page_view_id").length > 0 && !data.page_view_id && (!options || !options.skipPageViewLog)) {
      data.page_view_id = $("#page_view_id").text();
    }
    var ajaxError = function(xhr, textStatus, errorThrown) {
      var data = xhr;
      if(xhr.responseText) {
        var text = xhr.responseText.replace(/(<([^>]+)>)/ig,"");
        data = { message: text };
        try {
          data = eval("("  + xhr.responseText + ")");
        } catch(e) { }
      }
      if(options && options.skipDefaultError) {
        $.ajaxJSON.ignoredXHRs.push(xhr);
      }
      if(error && $.isFunction(error)) {
        error(data, xhr, textStatus, errorThrown);
      } else {
        $.ajaxJSON.unhandledXHRs.push(xhr);
      }
    };
    var params = {
      url: url,
      dataType: "json",
      type: submit_type,
      success: function(data) {
        data = data || {};
        var page_view_id = null;
        if(xhr && xhr.getResponseHeader && (page_view_id = xhr.getResponseHeader("X-Canvas-Page-View-Id"))) {
          setTimeout(function() {<commit_msg>don't use eval in error handler<commit_after>/**
 * Copyright (C) 2011 Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
/*jshint evil:true*/

define([
  'jquery' /* $ */,
  'jquery.instructure_forms' /* formSubmit, defaultAjaxError */
], function($) {

  $.originalGetJSON = $.getJSON;
  $.getJSON = function(url, data, callback) {
    var xhr = $.originalGetJSON(url, data, callback);
    $.ajaxJSON.storeRequest(xhr, url, 'GET', data);
    return xhr;
  };
  var assert_option = function(data, arg) {
    if(!data[arg]) {
      throw arg + " option is required";
    }
  };
  $.ajaxJSONPreparedFiles = function(options) {
    assert_option(options, 'context_code');
    var list = [];
    var $this = this;
    var pre_list = options.files || options.file_elements || [];
    for(var idx = 0; idx < pre_list.length; idx++) {
      var item = pre_list[idx];
      item.name = (item.value || item.name).split(/(\/|\\)/).pop();
      list.push(item);
    }
    var attachments = [];
    var ready = function() {
      var data = options.formDataTarget == 'url' ? options.formData : {};
      if(options.handle_files) {
        var result = attachments;
        if(options.single_file) {
          result = attachments[0];
        }
        data = options.handle_files.call(this, result, data);
      }
      if(options.url && options.success && data !== false) {
        $.ajaxJSON(options.url, options.method, data, options.success, options.error);
      }
    };
    var uploadFile = function(parameters, file) {
      $.ajaxJSON(options.uploadDataUrl || "/files/pending", 'POST', parameters, function(data) {
        try {
        if(data && data.upload_url) {
          var post_params = data.upload_params;
          var old_name = $(file).attr('name');
          $(file).attr('name', data.file_param);
          $.ajaxJSONFiles(data.upload_url, 'POST', post_params, $(file), function(data) {
            attachments.push(data);
            $(file).attr('name', old_name);
            next.call($this);
          }, function(data) {
            $(file).attr('name', old_name);
            (options.upload_error || options.error).call($this, data);
          }, {onlyGivenParameters: data.remote_url});
        } else {
          (options.upload_error || options.error).call($this, data);
        }
        } catch(e) {
          var ex = e;
        }
      }, function() { 
        return (options.upload_error || options.error).apply(this, arguments);
      });
    };
    var next = function() {
      var item = list.shift();
      if(item) {
        uploadFile.call($this, $.extend({
          'attachment[folder_id]': options.folder_id,
          'attachment[intent]': options.intent,
          'attachment[asset_string]': options.asset_string,
          'attachment[filename]': item.name,
          'attachment[context_code]': options.context_code
        }, options.formDataTarget == 'uploadDataUrl' ? options.formData : {}), item);
      } else {
        ready.call($this);
      }
    };
    next.call($this);
  };
  $.ajaxJSONFiles = function(url, submit_type, formData, files, success, error, options) {
    var $newForm = $(document.createElement("form"));
    $newForm.attr('action', url).attr('method', submit_type);
    if(!formData.authenticity_token) {
      formData.authenticity_token = $("#ajax_authenticity_token").text();
    }
    var fileNames = {};
    files.each(function() {
      fileNames[$(this).attr('name')] = true;
    });
    for(var idx in formData) {
      if(!fileNames[idx]) {
        var $input = $(document.createElement('input'));
        $input.attr('type', 'hidden').attr('name', idx).attr('value', formData[idx]);
        $newForm.append($input);
      }
    }
    files.each(function() {
      var $newFile = $(this).clone(true);
      $(this).after($newFile);
      $newForm.append($(this));
      $(this).removeAttr('id');
    });
    $("body").append($newForm.hide());
    $newForm.formSubmit({
      fileUpload: true,
      success: success,
      onlyGivenParameters: options ? options.onlyGivenParameters : false,
      error: error
    });
    (function() {
      $newForm.submit();
    }).call($newForm);
  };
  // Wrapper for default $.ajax behavior.  On error will call
  // the default error method if no error method is provided.
  $.ajaxJSON = function(url, submit_type, data, success, error, options) {
    data = data || {};
    if(!url && error) {
      error(null, null, "URL required for requests", null);
      return;
    }
    url = url || ".";
    if(submit_type != "GET") {
      data._method = submit_type;
      submit_type = "POST";
      if(!data.authenticity_token) {
        data.authenticity_token = $("#ajax_authenticity_token").text();
      }
    }
    if($("#page_view_id").length > 0 && !data.page_view_id && (!options || !options.skipPageViewLog)) {
      data.page_view_id = $("#page_view_id").text();
    }
    var ajaxError = function(xhr, textStatus, errorThrown) {
      var data = xhr;
      if(xhr.responseText) {
        var text = xhr.responseText.replace(/(<([^>]+)>)/ig,"");
        data = { message: text };
        try {
          data = $.parseJSON(xhr.responseText);
        } catch(e) { }
      }
      if(options && options.skipDefaultError) {
        $.ajaxJSON.ignoredXHRs.push(xhr);
      }
      if(error && $.isFunction(error)) {
        error(data, xhr, textStatus, errorThrown);
      } else {
        $.ajaxJSON.unhandledXHRs.push(xhr);
      }
    };
    var params = {
      url: url,
      dataType: "json",
      type: submit_type,
      success: function(data) {
        data = data || {};
        var page_view_id = null;
        if(xhr && xhr.getResponseHeader && (page_view_id = xhr.getResponseHeader("X-Canvas-Page-View-Id"))) {
          setTimeout(function() {
{'fraction_non_alphanumeric': 0.1582942097026604, 'fraction_numerical': 0.0012519561815336462, 'mean_word_length': 2.61045197740113, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '6328962', 'n_tokens_mistral': 3899, 'n_tokens_neox': 3853, 'n_words': 874}