<commit_before>// fully load the charts list from scratch
function loadChartsList(data, collectionId) {
  var html = templates.workEditCharts(data);
  $('#charts').replaceWith(html);
  initialiseChartList(data, collectionId);
}

// refresh only the list of charts - leaving the container div that accordion works from.
function refreshChartList(data, collectionId) {
  var html = templates.workEditCharts(data);
  $('#chart-list').replaceWith($(html).find('#chart-list'));
  initialiseChartList(data, collectionId);
}

// do all the wiring up of buttons etc once the template has been rendered.
function initialiseChartList(data, collectionId) {

  $(data.charts).each(function (index, chart) {

    var basePath = data.uri;
    var chartPath = basePath + '/' + chart.filename;
    var chartJson = chartPath;

    $("#chart-edit_" + chart.filename).click(function () {
      getPageData(collectionId, chartJson,
        onSuccess = function (chartData) {

          loadChartBuilder(data, function () {
            refreshPreview();

            postContent(collectionId, basePath, JSON.stringify(data),
              success = function () {
                Florence.Editor.isDirty = false;
                refreshPreview();
                refreshChartList(data, collectionId);
              },
              error = function (response) {
                handleApiError(response);
              }
            );
          }, chartData);
        })
    });

    $("#chart-delete_" + chart.filename).click(function () {
      swal ({
        title: "Warning",
        text: "Are you sure you want to delete this chart?",
        type: "warning",
        showCancelButton: true,
        confirmButtonText: "Delete",
        cancelButtonText: "Cancel",
        closeOnConfirm: false
      }, function(result) {
        if (result === true) {
          swal({
            title: "Deleted",
            text: "This chart has been deleted",
            type: "success",
            timer: 2000
          });
          $("#chart_" + index).remove();
          deleteContent(collectionId, chartJson + '.json',
            onSuccess = function () {
              data.charts = _(data.charts).filter(function (item) {
                return item.filename !== chart.filename
              });
              postContent(collectionId, basePath, JSON.stringify(data),
                success = function () {
                  Florence.Editor.isDirty = false;
                  refreshChartList(data, collectionId);
                },
                error = function (response) {
                  handleApiError(response);
                }
              );
            }
          );
        }<commit_msg>Ensure chart is removed from the page array if there is an error deleting the chart content.<commit_after>// fully load the charts list from scratch
function loadChartsList(data, collectionId) {
  var html = templates.workEditCharts(data);
  $('#charts').replaceWith(html);
  initialiseChartList(data, collectionId);
}

// refresh only the list of charts - leaving the container div that accordion works from.
function refreshChartList(data, collectionId) {
  var html = templates.workEditCharts(data);
  $('#chart-list').replaceWith($(html).find('#chart-list'));
  initialiseChartList(data, collectionId);
}

// do all the wiring up of buttons etc once the template has been rendered.
function initialiseChartList(data, collectionId) {

  $(data.charts).each(function (index, chart) {

    var basePath = data.uri;
    var chartPath = basePath + '/' + chart.filename;
    var chartJson = chartPath;

    $("#chart-edit_" + chart.filename).click(function () {
      getPageData(collectionId, chartJson,
        onSuccess = function (chartData) {

          loadChartBuilder(data, function () {
            refreshPreview();

            postContent(collectionId, basePath, JSON.stringify(data),
              success = function () {
                Florence.Editor.isDirty = false;
                refreshPreview();
                refreshChartList(data, collectionId);
              },
              error = function (response) {
                handleApiError(response);
              }
            );
          }, chartData);
        })
    });

    $("#chart-delete_" + chart.filename).click(function () {
      swal ({
        title: "Warning",
        text: "Are you sure you want to delete this chart?",
        type: "warning",
        showCancelButton: true,
        confirmButtonText: "Delete",
        cancelButtonText: "Cancel",
        closeOnConfirm: false
      }, function(result) {
        if (result === true) {

          $("#chart_" + index).remove();

          data.charts = _(data.charts).filter(function (item) {
            return item.filename !== chart.filename
          });
          postContent(collectionId, basePath, JSON.stringify(data),
            success = function () {
              deleteContent(collectionId, chartJson + '.json', onSuccess = function () {}, onError = function() {});
              Florence.Editor.isDirty = false;
              swal({
                title: "Deleted",
                text: "This chart has been deleted",
                type: "success",
                timer: 2000
              });
              refreshChartList(data, collectionId);
            },
            error = function (response) {
              handleApiError(response);
            }
          );



        }
{'fraction_non_alphanumeric': 0.11674940246368817, 'fraction_numerical': 0.0014708586137157566, 'mean_word_length': 2.0442081701175154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5996084', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1348, 'n_words': 344}