<commit_before>{% load i18n %}
{% load jsonify from ndf_tags %}
<!--Example insert
<li> <a class="crossPublish-assetContent" title="Click on the checkbox on the files below to Cross Publish"><i class="fa fa-external-link"></i> {% trans "Cross Publish" %}</a></li>
-->
<div id="publish_resource" class="reveal-modal" data-reveal style="position:absolute;"> 
  <h4 class="inner">{% trans "Select Groups" %} </h4> <h4 class="inner selGroupCount"></h4> 
  <div id="switchgrp" class="content">
    <select id = "selGroup" multiple = "multiple" size = "10" style="height: 500px;"> </select>
  </div>
    <div class="row">
            <a href="#" class="button-save-new" id="save_switch_group">{% trans "Save" %}</a>
        <button class="button-cancel-new">
            <a href="#" class="cancel-page-create">{% trans "Cancel" %}</a>
        </button>
    </div>

  <a class="close-reveal-modal">&#215;</a>
</div>

<script type="text/javascript">
  var existing_group_ids = {{node.group_set|jsonify|safe}}

  $(document).ready(function(){
    $(".selGroupCount").html("("+ {{node.group_set|length}} + ")")
  })

  function select_option(i) {
    $('select#selGroup option[value="' + i + '"]').prop('selected', true);
  }

  $(document).on('open.fndtn.reveal', '#publish_resource', function () {
    for (var ind=0; ind < existing_group_ids.length; ind++){
      select_option(existing_group_ids[ind])
    }
    $(".selGroupCount").html("("+ existing_group_ids.length + ")")
  })

  $(document).on('click', '.button-cancel-new', function(){
    $("#publish_resource").foundation('reveal', 'close');
  })

  $(document).on('click', '.crossPublish', function(){
    $('#selGroup').find('option').remove()

    $.ajax({
      type: "GET",
      url: "{% url 'cross_publish' group_id %}",
      datatype: "html",
      success: function(data) {
        data = JSON.parse(data)
        data_length = data.length
        while(data_length--){
          dict_obj = data[data_length]
          var option = document.createElement('option');
          option.text = dict_obj["altnames"] ? dict_obj["altnames"] : dict_obj["name"];
          option.value = dict_obj["_id"];
          option.selected = (existing_group_ids.indexOf(dict_obj["_id"]) > -1) ? true : false;
          $("#selGroup").append(option)
        }
        $("#publish_resource").foundation('reveal', 'open');
      }
    })
  })

  $(document).on('change', '#selGroup', function(event){
    $(".selGroupCount").html("("+ $(selGroup).val().length + ")")
  })

  $(document).on('click', '#save_switch_group', function(){
    selectedGroups = $("#selGroup").val()
    if (selectedGroups){

    $.ajax({
      type: "POST",
      url: "{% url 'cross_publish' group_id %}",
      data: {
        "group_ids": selectedGroups,
        "node_id": "{{node.pk}}",
        "publishChildren": "True",
        "csrfmiddlewaretoken": "{{csrf_token}}"
      },
      datatype: "html",
      success: function(data) {
        for (var i = selectedGroups.length - 1; i >= 0; i--) {
          existing_group_ids.push(selectedGroups[i])
        }
        $("#publish_resource").foundation('reveal', 'close');
      }
    })
    }<commit_msg>intial phase of cross-publish widget<commit_after>{% load i18n %}
{% load jsonify from ndf_tags %}
<!--Example insert
<li> <a class="crossPublish-assetContent" title="Click on the checkbox on the files below to Cross Publish"><i class="fa fa-external-link"></i> {% trans "Cross Publish" %}</a></li>
-->
<div id="publish_resource-{{node.pk}}" class="reveal-modal" data-reveal style="position:absolute;" data-options="close_on_background_click:false;close_on_esc:false;"> 
  <h4 class="inner">{% trans "Select Groups" %} </h4> <h4 class="inner selGroupCount"></h4> 
  <p class="inner">Use Ctrl + Click to select multiple Groups</p>
  <div id="switchgrp" class="content">
    <select id="selGroup-{{node.pk}}" multiple = "multiple" size = "10" style="height: 500px;"> </select>
  </div>
    <div class="row">
            <a href="#" class="button-save-new" id="save_switch_group-{{node.pk}}">{% trans "Save" %}</a>
        <button class="button-cancel-new">
            <a href="#" class="cancel-page-create">{% trans "Cancel" %}</a>
        </button>
    </div>
  <input type="hidden" name="existing_group_ids-{{node.pk}}">
  <a class="close-reveal-modal">&#215;</a>
</div>

<script type="text/javascript">

  $(document).ready(function(){
    $("#publish_resource-{{node.pk}}").find(".selGroupCount").html("("+ {{node.group_set|length}} + ")")
    $("input[name=existing_group_ids-{{node.pk}}]").attr("value", '{{node.group_set|jsonify|safe}}')
  })

  function select_option(i) {
    $('select#selGroup-{{node.pk}} option[value="' + i + '"]').prop('selected', true);
  }

  /*
  $(document).on('open.fndtn.reveal', '#publish_resource-{{node.pk}}', function () {
    var existing_group_ids = $("input[name=existing_group_ids-{{node.pk}}]").attr("value")
    console.log(typeof existing_group_ids)
    if (typeof existing_group_ids == 'string'){
      existing_group_ids = JSON.parse($("input[name=existing_group_ids-{{node.pk}}]").attr("value"))
      $("input[name=existing_group_ids-{{node.pk}}]").attr("value", JSON.stringify(existing_group_ids))
    }
    console.log("existing_group_ids")
    console.log(existing_group_ids)
    for (var ind=0; ind < existing_group_ids.length; ind++){
      select_option(existing_group_ids[ind])
    }
    $("#publish_resource-{{node.pk}}").find(".selGroupCount").html("("+ existing_group_ids.length + ")")
  })
  */
  $(document).on('click', '.button-cancel-new', function(){
    $("#publish_resource-{{node.pk}}").foundation('reveal', 'close');
  })

  $(document).on('click', '.crossPublish-{{node.pk}}', function(){
    var existing_group_ids = $("input[name=existing_group_ids-{{node.pk}}]").attr("value")

    $("select#selGroup-{{node.pk}}").find("option").remove()

    $.ajax({
      type: "GET",
      url: "{% url 'cross_publish' group_id %}",
      datatype: "html",
      success: function(data) {
        data = JSON.parse(data)
        data_length = data.length
        console.log(data)
        while(data_length--){
          dict_obj = data[data_length]
          var option = document.createElement('option');
          option.text = dict_obj["altnames"] ? dict_obj["altnames"] : dict_obj["name"];
          option.value = dict_obj["_id"];
          option.selected = (existing_group_ids.indexOf(dict_obj["_id"]) > -1) ? true : false;
          $("#selGroup-{{node.pk}}").append(option)
        }
        $("#publish_resource-{{node.pk}}").foundation('reveal', 'open');
      }
    })
  })

  $(document).on('change', '#selGroup-{{node.pk}}', function(event){
    $("#publish_resource-{{node.pk}}").find(".selGroupCount").html("("+ $("#selGroup-{{node.pk}}").val().length + ")")
  })

  $(document).on('click', '#save_switch_group-{{node.pk}}', function(){

    selectedGroups = $("#selGroup-{{node.pk}}").val()
    console.log("selectedGroups")
    if (selectedGroups){
    console.log(selectedGroups)

    $.ajax({
      type: "POST",
      url: "{% url 'cross_publish' group_id %}",
      data: {
        "group_ids": selectedGroups,
        "node_id": "{{node.pk}}",
        "publishChildren": "True",
        "csrfmiddlewaretoken": "{{csrf_token}}"
      },
      datatype: "html",
      success: function(data) {
        $("input[name=existing_group_ids-{{node.pk}}]").attr("value", selectedGroups)
        $("#publish_resource-{{node.pk}}").foundation('reveal', 'close');
        location.reload();
      }
    })
    }
{'fraction_non_alphanumeric': 0.21693121693121692, 'fraction_numerical': 0.004497354497354497, 'mean_word_length': 3.6301285976729947, 'pattern_counts': {'":': 8, '<': 64, '<?xml version=': 0, '>': 65, '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': '7321418', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2462, 'n_words': 415}