<commit_before>// handles the article selection and saving on add-articles.html
jQuery(function(d, $){
  (function(){

    var $list_count = $('.list-count');
    var initial_list_count = parseInt($list_count.text(), 10);
    var preview_list_count = initial_list_count;
    var $preview_list_count_elem = $('.preview-list-count');
    var results_span_pages = ($('.pagination-number').length > 1);

    return {
      init : function() {
        // update the header and the sidebar with the current list count
        this.updateListCount(initial_list_count);

        // set up event handlers
        $('.check-save-article').on("click", jQuery.proxy(this.checkboxClickHandler, this));
        $('.select-all-articles-link').on("click", { 'mode' : "SAVE" }, jQuery.proxy(this.toggleAllArticles, this));
        $('.unselect-all-articles-link').on("click", { 'mode' : "REMOVE" }, jQuery.proxy(this.toggleAllArticles, this));
        $('.reset-btn').on("click", { 'mode' : "REMOVE" }, jQuery.proxy(this.toggleAllArticles, this));
        $('#select_all_searchresults').on("click", jQuery.proxy(this.selectAllSearchResults, this));
        
        // We want the preview list count to be accurate even if the user navigates
        // with the back button.  So we always load the current preview list count
        // via ajax.
        $.ajax("/get-article-count", {
          type: "GET",
          cache: false,
          success: jQuery.proxy(function(resp) {
            this.updateListCount(resp);
          }, this)
        });
      },

      // Replaces the preview list counts in the UI with the new value.
      updateListCount : function(new_count) {
        preview_list_count = new_count;
        
        // update "your list" box in header
        $list_count.text(new_count);

        // update preview list button
        $preview_list_count_elem.val("Preview List (" + new_count + ")");
      },
      
      // Increments the preview list counts in the UI by the specified delta, which
      // can be positive or negative.
      incrementListCount : function(delta) {
        this.updateListCount(preview_list_count + delta);
      },

      checkboxClickHandler : function(e) {
        var $checkbox = $(e.target);

        // If we are over the limit, and it's a check event, don't do anything
        // (and uncheck the checkbox).
        if ($checkbox.prop("checked") && preview_list_count >= ARTICLE_LIMIT) {
          $checkbox.prop("checked", false);
          this.showErrorDialog("article-limit-error-message");
          return;
        }

        // create some data that we want to send to the server
        var ajax_data = {
          // grab the article ID from the checkbox value
          article_ids : [$checkbox.val()],

          // set the "mode" based on what state the checkbox is transitioning to
          // NOTE: this handler runs *after* the checkbox element has been 
          // updated so we check the "checked" prop.
          mode : $checkbox.prop("checked") ? "SAVE" : "REMOVE"
        };

        var $container = $checkbox.parent('.article-info');

        // show the user immediate visual feedback that we're doing something
        this.displayProgressIndicators($container, ajax_data.mode);

        // pass the data to the server to update the session
        // NOTE: update server expects an array (or collection) of checkboxes 
        // and containers to iterate over later on
        this.updateServer(ajax_data, $checkbox, $container);
      },

      // expects a jquery collection of containers to operate on
      displayProgressIndicators : function($containers, mode) {
        // we need to clear out the result message before trying to display a 
        // "updating" message since they occupy the same space
        // $containers.find('.result').remove();

        // compose a message based on the "mode"
        // var initial_msg = (mode == "SAVE") ? "Saving..." : "Removing...";

        // insert the message into the DOM
        // $containers.find('a').after("<span class='updating'>" + initial_msg + "</span>");
        $containers.find('a').after("<span class='updating'><\/span>");
      },

      toggleAllArticles : function(e) {
        // operate only on the checkboxes that we need to based on what's 
        // selected and what "mode" we're in. :checked/:not(:checked) FTW!
        var $checkboxes = $(
          (e.data['mode'] == 'SAVE') ? 
            ".check-save-article:not(:checked)" : 
            ".check-save-article:checked"
        );

        var $containers = []; // this will eventually be a jquery collection

        // this data will be serialized in the ajax request
        var ajax_data = {
          article_ids : [],
          mode : e.data['mode']
        };

        // this doesn't feel idiomatic to jquery. it also needs refactoring
        // FIXME: refactor this so it can be used by checkboxClickHandler
        $checkboxes.each(jQuery.proxy(function(idx, c) {
          var checkbox = $(c);

          // actually check the checkbox since we did not physically interact with it<commit_msg>ALM-315: enforce article limit on client side when selecting all search results.<commit_after>// handles the article selection and saving on add-articles.html
jQuery(function(d, $){
  (function(){

    var $list_count = $('.list-count');
    var initial_list_count = parseInt($list_count.text(), 10);
    var preview_list_count = initial_list_count;
    var $preview_list_count_elem = $('.preview-list-count');
    var results_span_pages = ($('.pagination-number').length > 1);

    return {
      init : function() {
        // update the header and the sidebar with the current list count
        this.updateListCount(initial_list_count);

        // set up event handlers
        $('.check-save-article').on("click", jQuery.proxy(this.checkboxClickHandler, this));
        $('.select-all-articles-link').on("click", { 'mode' : "SAVE" }, jQuery.proxy(this.toggleAllArticles, this));
        $('.unselect-all-articles-link').on("click", { 'mode' : "REMOVE" }, jQuery.proxy(this.toggleAllArticles, this));
        $('.reset-btn').on("click", { 'mode' : "REMOVE" }, jQuery.proxy(this.toggleAllArticles, this));
        $('#select_all_searchresults').on("click", jQuery.proxy(this.selectAllSearchResults, this));
        
        // We want the preview list count to be accurate even if the user navigates
        // with the back button.  So we always load the current preview list count
        // via ajax.
        $.ajax("/get-article-count", {
          type: "GET",
          cache: false,
          success: jQuery.proxy(function(resp) {
            this.updateListCount(resp);
          }, this)
        });
      },

      // Replaces the preview list counts in the UI with the new value.
      updateListCount : function(new_count) {
        preview_list_count = new_count;
        
        // update "your list" box in header
        $list_count.text(new_count);

        // update preview list button
        $preview_list_count_elem.val("Preview List (" + new_count + ")");
      },
      
      // Increments the preview list counts in the UI by the specified delta, which
      // can be positive or negative.
      incrementListCount : function(delta) {
        this.updateListCount(preview_list_count + delta);
      },

      checkboxClickHandler : function(e) {
        var $checkbox = $(e.target);

        // If we are over the limit, and it's a check event, don't do anything
        // (and uncheck the checkbox).
        if ($checkbox.prop("checked") && preview_list_count >= ARTICLE_LIMIT) {
          $checkbox.prop("checked", false);
          this.showErrorDialog("article-limit-error-message");
          return;
        }

        // create some data that we want to send to the server
        var ajax_data = {
          // grab the article ID from the checkbox value
          article_ids : [$checkbox.val()],

          // set the "mode" based on what state the checkbox is transitioning to
          // NOTE: this handler runs *after* the checkbox element has been 
          // updated so we check the "checked" prop.
          mode : $checkbox.prop("checked") ? "SAVE" : "REMOVE"
        };

        var $container = $checkbox.parent('.article-info');

        // show the user immediate visual feedback that we're doing something
        this.displayProgressIndicators($container, ajax_data.mode);

        // pass the data to the server to update the session
        // NOTE: update server expects an array (or collection) of checkboxes 
        // and containers to iterate over later on
        this.updateServer(ajax_data, $checkbox, $container);
      },

      // expects a jquery collection of containers to operate on
      displayProgressIndicators : function($containers, mode) {
        // we need to clear out the result message before trying to display a 
        // "updating" message since they occupy the same space
        // $containers.find('.result').remove();

        // compose a message based on the "mode"
        // var initial_msg = (mode == "SAVE") ? "Saving..." : "Removing...";

        // insert the message into the DOM
        // $containers.find('a').after("<span class='updating'>" + initial_msg + "</span>");
        $containers.find('a').after("<span class='updating'><\/span>");
      },

      toggleAllArticles : function(e) {
        if (e.data['mode'] == 'SAVE') {
          
          // Enforce article limit if necessary by only selecting the
          // first articles.
          var max = ARTICLE_LIMIT - preview_list_count;
          if (max < RESULTS_PER_PAGE) {
            this.showErrorDialog("article-limit-error-message");
          }
          var $checkboxes = $(".check-save-article:not(:checked)").slice(0, max);
        } else {
          var $checkboxes = $(".check-save-article:checked");
        }
        var $containers = []; // this will eventually be a jquery collection

        // this data will be serialized in the ajax request
        var ajax_data = {
          article_ids : [],
          mode : e.data['mode']
        };

        // this doesn't feel idiomatic to jquery. it also needs refactoring
        // FIXME: refactor this so it can be used by checkboxClickHandler
        $checkboxes.each(jQuery.proxy(function(idx, c) {
          var checkbox = $(c);

          // actually check the checkbox since we did not physically interact with it
{'fraction_non_alphanumeric': 0.12286396181384249, 'fraction_numerical': 0.0009546539379474941, 'mean_word_length': 2.3363057324840764, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 15, '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': '5194102', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2754, 'n_words': 888}