<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/>.
 */

define([
  'INST' /* INST */,
  'i18n!select_content_dialog',
  'jquery' /* $ */,
  'react',
  'jsx/context_modules/FileSelectBox',
  'jquery.instructure_date_and_time' /* datetime_field */,
  'jquery.ajaxJSON' /* ajaxJSON */,
  'jquery.instructure_forms' /* formSubmit, ajaxJSONFiles, getFormData, errorBox */,
  'jqueryui/dialog',
  'compiled/jquery/fixDialogButtons' /* fix dialog formatting */,
  'jquery.instructure_misc_helpers' /* replaceTags, getUserServices, findLinkForService */,
  'jquery.instructure_misc_plugins' /* showIf */,
  'jquery.keycodes' /* keycodes */,
  'jquery.loadingImg' /* loadingImage */,
  'jquery.templateData' /* fillTemplateData */
], function(INST, I18n, $, React, FileSelectBox) {

  $(document).ready(function() {

    var external_services = null;
    var $dialog = $("#select_context_content_dialog");
    INST = INST || {};
    INST.selectContentDialog = function(options) {
      var options = options || {};
      var for_modules = options.for_modules;
      var select_button_text = options.select_button_text || I18n.t('buttons.add_item', "Add Item");
      var holder_name = options.holder_name || "module";
      var dialog_title = options.dialog_title || I18n.t('titles.add_item_to_module', "Add Item to Module");
      var allow_external_urls = for_modules;
      $dialog.data('submitted_function', options.submit);
      $dialog.find(".context_module_content").showIf(for_modules);
      $dialog.find(".holder_name").text(holder_name);
      $dialog.find(".add_item_button").text(select_button_text);
      $dialog.find(".select_item_name").showIf(!options.no_name_input);
      if(allow_external_urls && !external_services) {
        var $services = $("#content_tag_services").empty();
        $.getUserServices('BookmarkService', function(data) {
          for(var idx in data) {
            service = data[idx].user_service;
            $service = $("<a href='#' class='bookmark_service no-hover'/>");
            $service.addClass(service.service);
            $service.data('service', service);
            $service.attr('title', I18n.t('titles.find_links_using_service', 'Find links using %{service}', {service: service.service}));
            var $img = $("<img/>");
            $img.attr('src', '/images/' + service.service + '_small_icon.png');
            $service.append($img);
            $service.click(function(event) {
              event.preventDefault();
              $.findLinkForService($(this).data('service').service, function(data) {
                $("#content_tag_create_url").val(data.url);
                $("#content_tag_create_title").val(data.title);
              });
            });
            $services.append($service);
            $services.append("&nbsp;&nbsp;");
          }
        });
      }
      $("#select_context_content_dialog #external_urls_select :text").val("");
      $("#select_context_content_dialog #context_module_sub_headers_select :text").val("");
      $('#add_module_item_select').change();
      $("#select_context_content_dialog .module_item_select").change();
      $("#select_context_content_dialog").dialog({
        title: dialog_title,
        width: options.width || 400,
        height: options.height || 400,
        close: function() {
          if (options.close) {
            options.close();
          }
        }
      }).fixDialogButtons();
      $("#select_context_content_dialog").dialog('option', 'title', dialog_title);
    }
    $("#select_context_content_dialog .cancel_button").click(function() {
      $dialog.find('.alert').remove();
      $dialog.dialog('close');
    });
    $("#select_context_content_dialog select, #select_context_content_dialog input[type=text], .module_item_select").keycodes('return', function(event) {
      $(event.currentTarget).blur();
      $(this).parents(".ui-dialog").find(".add_item_button").last().click();
    });
    $("#select_context_content_dialog .add_item_button").click(function() {
      var submit = function(item_data) {
        $dialog.dialog('close');
        $dialog.find('.alert').remove();
        var submitted = $dialog.data('submitted_function');
        if(submitted && $.isFunction(submitted)) {
          submitted(item_data);
        }
      };

      var item_type = $("#add_module_item_select").val();
      if(item_type == 'external_url') {
        var item_data = {
          'item[type]': $("#add_module_item_select").val(),
          'item[id]': $("#select_context_content_dialog .module_item_option:visible:first .module_item_select").val(),
          'item[new_tab]': $("#external_url_create_new_tab").attr('checked') ? '1' : '0',
          'item[indent]': $("#content_tag_indent").val()
        }
        item_data['item[url]'] = $("#content_tag_create_url").val();
        item_data['item[title]'] = $("#content_tag_create_title").val();
        submit(item_data);

      } else if(item_type == 'context_external_tool') {

        var tool = $("#context_external_tools_select .tools .tool.selected").data('tool');
        var tool_type = 'context_external_tool';
        var tool_id = 0;
        if(tool){
          if(tool.definition_type == 'Lti::MessageHandler') { tool_type = 'lti/message_handler'}
          tool_id = tool.definition_id
        }
        var item_data = {
          'item[type]': tool_type,
          'item[id]': tool_id,
          'item[new_tab]': $("#external_tool_create_new_tab").attr('checked') ? '1' : '0',
          'item[indent]': $("#content_tag_indent").val()
        }
        item_data['item[url]'] = $("#external_tool_create_url").val();
        item_data['item[title]'] = $("#external_tool_create_title").val();
        $dialog.find('.alert-error').remove();
        if (item_data['item[url]'] === '') {
          var $errorBox = $('<div />', { 'class': 'alert alert-error', role: 'alert' }).css({marginTop: 8 });
          $errorBox.text(I18n.t('errors.external_tool_url', "An external tool can't be saved without a URL."));
          $dialog.prepend($errorBox);
        } else {
          submit(item_data);
        }

      } else if(item_type == 'context_module_sub_header') {

        var item_data = {
          'item[type]': $("#add_module_item_select").val(),
          'item[id]': $("#select_context_content_dialog .module_item_option:visible:first .module_item_select").val(),
          'item[indent]': $("#content_tag_indent").val()
        }
        item_data['item[title]'] = $("#sub_header_title").val();
        submit(item_data);

      } else {

        var $options = $("#select_context_content_dialog .module_item_option:visible:first .module_item_select option:selected");
        $options.each(function() {
          var $option = $(this);
          var item_data = {
            'item[type]': item_type,
            'item[id]': $option.val(),
            'item[title]': $option.text(),
            'item[indent]': $("#content_tag_indent").val()
          }
          if(item_data['item[id]'] == 'new') {
            $("#select_context_content_dialog").loadingImage();
            var url = $("#select_context_content_dialog .module_item_option:visible:first .new .add_item_url").attr('href');
            var data = $("#select_context_content_dialog .module_item_option:visible:first").getFormData();
            var callback = function(data) {
              var obj;

              // discussion_topics will come from real api v1 and so wont be nested behind a `discussion_topic` or 'wiki_page' root object
              if (item_data['item[type]'] === 'discussion_topic' || item_data['item[type]'] === 'wiki_page') {
                obj = data;
              } else {
                obj = data[item_data['item[type]']]; // e.g. data['wiki_page'] for wiki pages
              }

              $("#select_context_content_dialog").loadingImage('remove');
              if (item_data['item[type]'] === 'wiki_page') {
                item_data['item[id]'] = obj.page_id;
              } else {
                item_data['item[id]'] = obj.id;
              }
              if (item_data['item[type]'] === 'attachment') {
                // some browsers return a fake path in the file input value, so use the name returned by the server
                item_data['item[title]'] = obj.display_name;
              } else {
                item_data['item[title]'] = $("#select_context_content_dialog .module_item_option:visible:first .item_title").val();
                item_data['item[title]'] = item_data['item[title]'] || obj.display_name;
              }
              var $option = $(document.createElement('option'));
              $option.val(obj.id).text(item_data['item[title]']);
              $("#" + item_data['item[type]'] + "s_select").find(".module_item_select option:last").after($option);
              submit(item_data);
            };

            if(item_data['item[type]'] == 'attachment') {
              $.ajaxJSONFiles(url, 'POST', data, $("#module_attachment_uploaded_data"), function(data) {
                callback(data);
              }, function(data) {
                $("#select_context_content_dialog").loadingImage('remove');
                $("#select_context_content_dialog").errorBox(I18n.t('errors.failed_to_create_item', 'Failed to Create new Item'));
              });
            } else {
              $.ajaxJSON(url, 'POST', data, function(data) {
                callback(data);
              }, function(data) {
                $("#select_context_content_dialog").loadingImage('remove');<commit_msg>feat(LMS-1504):fixed LMS-1504 - The File input to upload is not being cleaned after adding a file in the module section<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/>.
 */

define([
  'INST' /* INST */,
  'i18n!select_content_dialog',
  'jquery' /* $ */,
  'react',
  'jsx/context_modules/FileSelectBox',
  'jquery.instructure_date_and_time' /* datetime_field */,
  'jquery.ajaxJSON' /* ajaxJSON */,
  'jquery.instructure_forms' /* formSubmit, ajaxJSONFiles, getFormData, errorBox */,
  'jqueryui/dialog',
  'compiled/jquery/fixDialogButtons' /* fix dialog formatting */,
  'jquery.instructure_misc_helpers' /* replaceTags, getUserServices, findLinkForService */,
  'jquery.instructure_misc_plugins' /* showIf */,
  'jquery.keycodes' /* keycodes */,
  'jquery.loadingImg' /* loadingImage */,
  'jquery.templateData' /* fillTemplateData */
], function(INST, I18n, $, React, FileSelectBox) {

  $(document).ready(function() {

    var external_services = null;
    var $dialog = $("#select_context_content_dialog");
    INST = INST || {};
    INST.selectContentDialog = function(options) {
      var options = options || {};
      var for_modules = options.for_modules;
      var select_button_text = options.select_button_text || I18n.t('buttons.add_item', "Add Item");
      var holder_name = options.holder_name || "module";
      var dialog_title = options.dialog_title || I18n.t('titles.add_item_to_module', "Add Item to Module");
      var allow_external_urls = for_modules;
      $dialog.data('submitted_function', options.submit);
      $dialog.find(".context_module_content").showIf(for_modules);
      $dialog.find(".holder_name").text(holder_name);
      $dialog.find(".add_item_button").text(select_button_text);
      $dialog.find(".select_item_name").showIf(!options.no_name_input);
      if(allow_external_urls && !external_services) {
        var $services = $("#content_tag_services").empty();
        $.getUserServices('BookmarkService', function(data) {
          for(var idx in data) {
            service = data[idx].user_service;
            $service = $("<a href='#' class='bookmark_service no-hover'/>");
            $service.addClass(service.service);
            $service.data('service', service);
            $service.attr('title', I18n.t('titles.find_links_using_service', 'Find links using %{service}', {service: service.service}));
            var $img = $("<img/>");
            $img.attr('src', '/images/' + service.service + '_small_icon.png');
            $service.append($img);
            $service.click(function(event) {
              event.preventDefault();
              $.findLinkForService($(this).data('service').service, function(data) {
                $("#content_tag_create_url").val(data.url);
                $("#content_tag_create_title").val(data.title);
              });
            });
            $services.append($service);
            $services.append("&nbsp;&nbsp;");
          }
        });
      }
      $("#select_context_content_dialog #external_urls_select :text").val("");
      $("#select_context_content_dialog #context_module_sub_headers_select :text").val("");
      $('#add_module_item_select').change();
      $("#select_context_content_dialog .module_item_select").change();
      $("#select_context_content_dialog").dialog({
        title: dialog_title,
        width: options.width || 400,
        height: options.height || 400,
        close: function() {
          if (options.close) {
            options.close();
          }
        }
      }).fixDialogButtons();
      $("#select_context_content_dialog").dialog('option', 'title', dialog_title);
    }
    $("#select_context_content_dialog .cancel_button").click(function() {
      $dialog.find('.alert').remove();
      $dialog.dialog('close');
    });
    $("#select_context_content_dialog select, #select_context_content_dialog input[type=text], .module_item_select").keycodes('return', function(event) {
      $(event.currentTarget).blur();
      $(this).parents(".ui-dialog").find(".add_item_button").last().click();
    });
    $("#select_context_content_dialog .add_item_button").click(function() {
      var submit = function(item_data) {
        $dialog.dialog('close');
        $dialog.find('.alert').remove();
        var submitted = $dialog.data('submitted_function');
        if(submitted && $.isFunction(submitted)) {
          submitted(item_data);
        }
      };

      var item_type = $("#add_module_item_select").val();
      if(item_type == 'external_url') {
        var item_data = {
          'item[type]': $("#add_module_item_select").val(),
          'item[id]': $("#select_context_content_dialog .module_item_option:visible:first .module_item_select").val(),
          'item[new_tab]': $("#external_url_create_new_tab").attr('checked') ? '1' : '0',
          'item[indent]': $("#content_tag_indent").val()
        }
        item_data['item[url]'] = $("#content_tag_create_url").val();
        item_data['item[title]'] = $("#content_tag_create_title").val();
        submit(item_data);

      } else if(item_type == 'context_external_tool') {

        var tool = $("#context_external_tools_select .tools .tool.selected").data('tool');
        var tool_type = 'context_external_tool';
        var tool_id = 0;
        if(tool){
          if(tool.definition_type == 'Lti::MessageHandler') { tool_type = 'lti/message_handler'}
          tool_id = tool.definition_id
        }
        var item_data = {
          'item[type]': tool_type,
          'item[id]': tool_id,
          'item[new_tab]': $("#external_tool_create_new_tab").attr('checked') ? '1' : '0',
          'item[indent]': $("#content_tag_indent").val()
        }
        item_data['item[url]'] = $("#external_tool_create_url").val();
        item_data['item[title]'] = $("#external_tool_create_title").val();
        $dialog.find('.alert-error').remove();
        if (item_data['item[url]'] === '') {
          var $errorBox = $('<div />', { 'class': 'alert alert-error', role: 'alert' }).css({marginTop: 8 });
          $errorBox.text(I18n.t('errors.external_tool_url', "An external tool can't be saved without a URL."));
          $dialog.prepend($errorBox);
        } else {
          submit(item_data);
        }

      } else if(item_type == 'context_module_sub_header') {

        var item_data = {
          'item[type]': $("#add_module_item_select").val(),
          'item[id]': $("#select_context_content_dialog .module_item_option:visible:first .module_item_select").val(),
          'item[indent]': $("#content_tag_indent").val()
        }
        item_data['item[title]'] = $("#sub_header_title").val();
        submit(item_data);

      } else {

        var $options = $("#select_context_content_dialog .module_item_option:visible:first .module_item_select option:selected");
        $options.each(function() {
          var $option = $(this);
          var item_data = {
            'item[type]': item_type,
            'item[id]': $option.val(),
            'item[title]': $option.text(),
            'item[indent]': $("#content_tag_indent").val()
          }
          if(item_data['item[id]'] == 'new') {
            $("#select_context_content_dialog").loadingImage();
            var url = $("#select_context_content_dialog .module_item_option:visible:first .new .add_item_url").attr('href');
            var data = $("#select_context_content_dialog .module_item_option:visible:first").getFormData();
            var callback = function(data) {
              var obj;

              // discussion_topics will come from real api v1 and so wont be nested behind a `discussion_topic` or 'wiki_page' root object
              if (item_data['item[type]'] === 'discussion_topic' || item_data['item[type]'] === 'wiki_page') {
                obj = data;
              } else {
                obj = data[item_data['item[type]']]; // e.g. data['wiki_page'] for wiki pages
              }

              $("#select_context_content_dialog").loadingImage('remove');
              if (item_data['item[type]'] === 'wiki_page') {
                item_data['item[id]'] = obj.page_id;
              } else {
                item_data['item[id]'] = obj.id;
              }
              if (item_data['item[type]'] === 'attachment') {
                // some browsers return a fake path in the file input value, so use the name returned by the server
                item_data['item[title]'] = obj.display_name;
                // show file upload button and hide label from previews file selected.
                $("#module_attachment_uploaded_data").show();                       
                $('#select_context_content_dialog #attachments_select .new span').hide();
              } else {
                item_data['item[title]'] = $("#select_context_content_dialog .module_item_option:visible:first .item_title").val();
                item_data['item[title]'] = item_data['item[title]'] || obj.display_name;
              }
              var $option = $(document.createElement('option'));
              $option.val(obj.id).text(item_data['item[title]']);
              $("#" + item_data['item[type]'] + "s_select").find(".module_item_select option:last").after($option);
              submit(item_data);
            };

            if(item_data['item[type]'] == 'attachment') {
              $.ajaxJSONFiles(url, 'POST', data, $("#module_attachment_uploaded_data"), function(data) {
                callback(data);
              }, function(data) {
                $("#select_context_content_dialog").loadingImage('remove');
                $("#select_context_content_dialog").errorBox(I18n.t('errors.failed_to_create_item', 'Failed to Create new Item'));
              });
            } else {
              $.ajaxJSON(url, 'POST', data, function(data) {
                callback(data);
              }, function(data) {
                $("#select_context_content_dialog").loadingImage('remove');
{'fraction_non_alphanumeric': 0.16032922603003466, 'fraction_numerical': 0.0034655371582595304, 'mean_word_length': 2.820003677146534, 'pattern_counts': {'":': 0, '<': 11, '<?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': '7547640', 'n_tokens_mistral': 6024, 'n_tokens_neox': 5943, 'n_words': 1095}