<commit_before>/*
#--
#   Copyright (C) 2007-2009 <NAME> <<EMAIL>>
#   Copyright (C) 2009 <NAME> <<EMAIL>>
#
#   This program 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, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program 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/>.
#-- 
*/

jQuery.fn.highlightSelectedLines = function() {
    var currentHighlights = [];
    if (/^#line\d+$/.test(window.location.hash)) {
      currentHighlights = [window.location.hash];
    }

    this.find("tr td.line-numbers a").click(function() {
        var element = $(this).get(0);
        currentHighlights = [element.name];
        highlightCodeLine(currentHighlights);
    });

    var jthis = this;
    var highlightCodeLine = function(lineId) {
        jQuery.each(currentHighlights, function() {
            $(jthis).find("tr#" + this + " td").removeClass("marked");
        });
        $(jthis).find("tr#" + lineId + " td").addClass("marked");
        currentHighlights = [lineId];
    };

    if (currentHighlights.length === 0) return;
    highlightCodeLine(currentHighlights);
};

jQuery.fn.changableSelection = function(options) {
  var currentContainer = $(this);
  var choices = $(options.container || $(this).next("ul.changable-selection-options"));

  choices.css({
    display:'none',
    cursor:'pointer'
  }).children("li").bind('click', function(e) {
      currentContainer.text( $(this).text() );
      choices.hide();
      if (options.onChange) options.onChange.call(this);
      return false;
  });

  currentContainer.bind('click', function(event) {
      choices.css({
        top: (event.pageY - $(this).height()) + "px",
        // TODO: Check for vicinity to screen edge and adjust left/right accordingly
        left: (event.pageX - $(this).width() - 10) + "px",
				position: "absolute",
				opacity: 1.0,
        zIndex: 1000
      }).fadeIn('fast');
      return false;
  }).css({cursor:'pointer'});

  $(document).click(function(){
      choices.fadeOut('fast');
  });
};

jQuery.fn.hoverBubble = function() {
  $(this).each(function() {
      var originalOffset = $(this).offset();
      var bubbleHeight = $(this).next(".hover-bubble-content").height();
      var triggerWidth = $(this).width();
      $(this).next(".hover-bubble-content").css({
        top: originalOffset.top - bubbleHeight - 25 + 'px',
        left: originalOffset.left - (triggerWidth/2) - 50 + 'px',
        opacity: 0
      });
      $(this).hover(function() {
          $(this).next(".hover-bubble-content").stop()
            .animate({
              top: originalOffset.top - bubbleHeight - 15 + 'px',
              opacity: 0.95
            }, "fast").show();
      }, function(){
          $(this).next(".hover-bubble-content").stop()
            .animate({
              top: originalOffset.top - bubbleHeight - 25 + 'px',
              opacity: 0
            }, "fast");
      });

  });
};

$(document).ready(function() {
    // Project Sluggorizin'
    $("form #project_title").keyup(function(event) {
        var slug = $("form #project_slug");
        if (slug.text() != "") return;
        var lintName = function(val) {
          var linted = val.replace(/\W+/g, ' ').replace(/\ +/g, '-');
          linted = linted.toLowerCase().replace(/\-+$/g, '');
          return linted;
        }
        
        slug.val( lintName(this.value) );
    });

    // Line highlighting/selection
    $("#codeblob").highlightSelectedLines();

    // no-op links
    $("a.link_noop").click(function(event) {
        event.preventDefault();
    });
    
    // Comment previewing
    $("input#comment_preview_button").click(function(event){
        var formElement = $(this).parents("form");
        var url = formElement.attr("action");
        url += "/preview"
        $.post(url, formElement.serialize(), function(data, responseText){
            if (responseText === "success") {
              $("#comment_preview").html(data);
              $("#comment_preview").fadeIn();
            }
        });
        event.preventDefault();
    });

    // Project previewing
    $("input#project_preview_button").click(function(event){
        var formElement = $(this).parents("form");
        var url = $(this).attr("gts:url");
        $.post(url, formElement.serialize(), function(data, response) {
            if (response === "success")
              $("#project-preview").html(data).fadeIn();
        });
        event.preventDefault();
    });

    // Message actions
    $(".message_actions a.mark_as_unread").click(function(event){
        var link = $(this);
        $.post(link.attr("href"), "_method=put", function(data, response){
            if (response === "success") {
              var parts = link.attr("href").split("/");
              $("#message_" + parts[parts.length-2]).removeClass("unread");
              link.parent().slideUp();
            }              
        });
        event.preventDefault();
    });

    // Message selection toggling
    $("a#toggle_all_messages_checked").click(function(e) {
        $(".select_msg").each(function() {
            this.checked = (this.checked ? '' : 'checked');
        });
        e.preventDefault();
    });
    $("a#toggle_all_unread_messages_checked").click(function(e) {
        $(".select_msg").each(function() { this.checked = ''; });
        $(".unread .select_msg").each(function() {
            this.checked = (this.checked ? '' : 'checked');
        });
        e.preventDefault();
    });

    // Markdown help toggling
    $(".markdown-help-toggler").click(function(event){
        $(".markdown_help").toggle();
        event.preventDefault();
    });

    $("a#advanced-search-toggler").click(function(event) {
        if ($("#search_help").is(":visible")) {
            $("#search_help").slideUp("fast");
        } else {
            $("#search_help").slideDown("fast");
        }
        event.preventDefault();
    });

    // Merge request status color picking
    $("#merge_request_statuses input.color_pickable").SevenColorPicker();

    // Toggle details of commit events
    $("a.commit_event_toggler").click(function(event){
        var callbackUrl = $(this).attr("gts:url");
        var eventId = $(this).attr("gts:id");

        $("#commits_in_event_" + eventId).toggle();
        if ($("#commits_in_event_" + eventId).is(":visible")) {
          $("#commits_in_event_" + eventId).load(callbackUrl);
        }
        event.preventDefault();
    });
    
    // frontpage for non-loggedin users
    // Unobtrusively hooking the regular/OpenID login box stuff, so that it works
    // in a semi-sensible way with javascript disabled.
    $("#big_header_login_box_to_openid, #big_header_login_box_to_regular").click(function(e){
        $("#big_header_login_box_openid").toggle("fast");
        $("#big_header_login_box_regular").toggle("fast");
        e.preventDefault();
    });

    // replace the search form input["submit"] with something fancier
    $("#main_menu_search_form").each(function(){
        var headerSearchForm = this;
        var labelText = "Search...";
        var searchInput = $(this).find("input[type=text]");
        searchInput.val(labelText);
        searchInput.click(function(event){
            if (searchInput.val() == labelText) {
              searchInput.val("");
              searchInput.removeClass("unfocused");
            }
        });
        searchInput.blur(function(event){
            if (searchInput.val() == "") {
              searchInput.val(labelText);
              searchInput.addClass("unfocused");
            }
        });
        // hide the 'native' submit button and replace it with our 
        // own awesome submit button
        var nativeSubmitButton = $(this).find("input[type=submit]");
        nativeSubmitButton.hide();
        var awesomeSubmitButton = $(document.createElement("a"));
        awesomeSubmitButton.attr({
            'id':'main_menu_search_form_graphic_submit',
            'href': '#'
        });
        awesomeSubmitButton.click(function(event){
            headerSearchForm.submit();
            event.preventDefault();
        });
        nativeSubmitButton.after(awesomeSubmitButton);
    });

    // toggling of diffs in merge-request diff browser
    $('#merge_request_diff .file-diff .header').live("click", function(event) {
        var hunksContainer = $(this).next();
        if (hunksContainer.is(":visible")) {
          $(this).removeClass("open").addClass("closed");
          hunksContainer.slideUp();
        } else {
          $(this).removeClass("closed").addClass("open");
          hunksContainer.slideDown();
        }
        event.preventDefault();
    });
    $("#merge_request_diff .file-diff-controls a#expand-all").live("click", function(e){
        var container = $(this).parent().parent().parent();
        container.find('.file-diff .header').removeClass("closed").addClass("open");
        container.find('.diff-hunks:hidden').show();
        $.cookie("merge-requests-diff-hunks-state", "expanded");
        e.preventDefault();
    });
    $("#merge_request_diff .file-diff-controls a#collapse-all").live("click", function(e){
        var container = $(this).parent().parent().parent();
        container.find('.file-diff .header').removeClass("open").addClass("closed");
        container.find('.diff-hunks').hide();
        $.cookie("merge-requests-diff-hunks-state", null);
        e.preventDefault();
    });

    // merge request diffing loading indimacator
    Gitorious.MergeRequestDiffSpinner = $("#merge_request_diff_loading").html();
    $("#merge_request_diff").html(Gitorious.MergeRequestDiffSpinner);

    // Merge request selection of branches, compact mode
    // wrapped in a function so we can reuse it when we load another version
    var diffBrowserCompactCommitSelectable = function() {
      var selectingAndUnselecting = function() {
        $("#current_shas .several_shas .label").html("Selecting:");
        var commits = $("li.ui-selecting a");
        if (!commits[0]) return true;
        var first_commit_sha = $(commits[0]).attr("data-commit-sha");
        var last_commit_sha = $(commits[commits.length - 1]).attr("data-commit-sha");
        
        var shaSpec = new Gitorious.ShaSpec();
        shaSpec.addSha(first_commit_sha);
        shaSpec.addSha(last_commit_sha);          
        shaSpec.summarizeHtml();
      };
      return jQuery("#merge_request_commit_selector.compact").selectable({
        filter: "li.single_commit",
        stop: function(e, ui) {
            var sha_spec = new Gitorious.ShaSpec();
            jQuery("li.ui-selected a", this).each(function() {
                sha = jQuery(this).attr("data-commit-sha");
                sha_spec.addSha(sha);
            });
          $("#current_shas .label").html("Selected:");
            var mr_diff_url = jQuery("#merge_request_commit_selector")
              .attr("data-merge-request-version-url");
            var diff_browser = new Gitorious.DiffBrowser(sha_spec.shaSpec());
        },
        selecting: function(e, ui) {
          selectingAndUnselecting();
        },
        unselecting: function(e,ui) {
          selectingAndUnselecting();
        }
      });
    }
    Gitorious.currentMRCompactSelectable = diffBrowserCompactCommitSelectable();

    $("#merge_request_version").changableSelection({
      onChange: function() {
          var version = $(this).text().replace(/[^0-9]+/g, '');
          var url = $(this).parent().prev().attr("gts:url") + '?version=' + version;
          $("#diff_browser_for_current_version").load(url, null, function() {
            new Gitorious.DiffBrowser(
              jQuery("#current_shas").attr("data-merge-request-current-shas") );
            // jump through hoops and beat the selectable into submission,
            // since it doesn't use live events, we have to re-create it, which sucks...
            Gitorious.currentMRCompactSelectable.selectable("destroy");
            Gitorious.currentMRCompactSelectable = diffBrowserCompactCommitSelectable();
          });
      }
    });
    
    $("#merge_request_current_version ul.compact li.single_commit").hoverBubble();
    
    // Merge request selection of branches, monster mode
    $("#large_commit_selector_toggler").live("click", function(event) {
        if ($("#large_commit_selector").is(":visible")) {
          $("#large_commit_selector").slideUp();
        } else {
          $("#large_commit_selector").slideDown();
        }<commit_msg>Cancel when hitting the merge base; resolves non wanted events<commit_after>/*
#--
#   Copyright (C) 2007-2009 <NAME> <<EMAIL>>
#   Copyright (C) 2009 <NAME> <<EMAIL>>
#
#   This program 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, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program 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/>.
#-- 
*/

jQuery.fn.highlightSelectedLines = function() {
    var currentHighlights = [];
    if (/^#line\d+$/.test(window.location.hash)) {
      currentHighlights = [window.location.hash];
    }

    this.find("tr td.line-numbers a").click(function() {
        var element = $(this).get(0);
        currentHighlights = [element.name];
        highlightCodeLine(currentHighlights);
    });

    var jthis = this;
    var highlightCodeLine = function(lineId) {
        jQuery.each(currentHighlights, function() {
            $(jthis).find("tr#" + this + " td").removeClass("marked");
        });
        $(jthis).find("tr#" + lineId + " td").addClass("marked");
        currentHighlights = [lineId];
    };

    if (currentHighlights.length === 0) return;
    highlightCodeLine(currentHighlights);
};

jQuery.fn.changableSelection = function(options) {
  var currentContainer = $(this);
  var choices = $(options.container || $(this).next("ul.changable-selection-options"));

  choices.css({
    display:'none',
    cursor:'pointer'
  }).children("li").bind('click', function(e) {
      currentContainer.text( $(this).text() );
      choices.hide();
      if (options.onChange) options.onChange.call(this);
      return false;
  });

  currentContainer.bind('click', function(event) {
      choices.css({
        top: (event.pageY - $(this).height()) + "px",
        // TODO: Check for vicinity to screen edge and adjust left/right accordingly
        left: (event.pageX - $(this).width() - 10) + "px",
				position: "absolute",
				opacity: 1.0,
        zIndex: 1000
      }).fadeIn('fast');
      return false;
  }).css({cursor:'pointer'});

  $(document).click(function(){
      choices.fadeOut('fast');
  });
};

jQuery.fn.hoverBubble = function() {
  $(this).each(function() {
      var originalOffset = $(this).offset();
      var bubbleHeight = $(this).next(".hover-bubble-content").height();
      var triggerWidth = $(this).width();
      $(this).next(".hover-bubble-content").css({
        top: originalOffset.top - bubbleHeight - 25 + 'px',
        left: originalOffset.left - (triggerWidth/2) - 50 + 'px',
        opacity: 0
      });
      $(this).hover(function() {
          $(this).next(".hover-bubble-content").stop()
            .animate({
              top: originalOffset.top - bubbleHeight - 15 + 'px',
              opacity: 0.95
            }, "fast").show();
      }, function(){
          $(this).next(".hover-bubble-content").stop()
            .animate({
              top: originalOffset.top - bubbleHeight - 25 + 'px',
              opacity: 0
            }, "fast");
      });

  });
};

$(document).ready(function() {
    // Project Sluggorizin'
    $("form #project_title").keyup(function(event) {
        var slug = $("form #project_slug");
        if (slug.text() != "") return;
        var lintName = function(val) {
          var linted = val.replace(/\W+/g, ' ').replace(/\ +/g, '-');
          linted = linted.toLowerCase().replace(/\-+$/g, '');
          return linted;
        }
        
        slug.val( lintName(this.value) );
    });

    // Line highlighting/selection
    $("#codeblob").highlightSelectedLines();

    // no-op links
    $("a.link_noop").click(function(event) {
        event.preventDefault();
    });
    
    // Comment previewing
    $("input#comment_preview_button").click(function(event){
        var formElement = $(this).parents("form");
        var url = formElement.attr("action");
        url += "/preview"
        $.post(url, formElement.serialize(), function(data, responseText){
            if (responseText === "success") {
              $("#comment_preview").html(data);
              $("#comment_preview").fadeIn();
            }
        });
        event.preventDefault();
    });

    // Project previewing
    $("input#project_preview_button").click(function(event){
        var formElement = $(this).parents("form");
        var url = $(this).attr("gts:url");
        $.post(url, formElement.serialize(), function(data, response) {
            if (response === "success")
              $("#project-preview").html(data).fadeIn();
        });
        event.preventDefault();
    });

    // Message actions
    $(".message_actions a.mark_as_unread").click(function(event){
        var link = $(this);
        $.post(link.attr("href"), "_method=put", function(data, response){
            if (response === "success") {
              var parts = link.attr("href").split("/");
              $("#message_" + parts[parts.length-2]).removeClass("unread");
              link.parent().slideUp();
            }              
        });
        event.preventDefault();
    });

    // Message selection toggling
    $("a#toggle_all_messages_checked").click(function(e) {
        $(".select_msg").each(function() {
            this.checked = (this.checked ? '' : 'checked');
        });
        e.preventDefault();
    });
    $("a#toggle_all_unread_messages_checked").click(function(e) {
        $(".select_msg").each(function() { this.checked = ''; });
        $(".unread .select_msg").each(function() {
            this.checked = (this.checked ? '' : 'checked');
        });
        e.preventDefault();
    });

    // Markdown help toggling
    $(".markdown-help-toggler").click(function(event){
        $(".markdown_help").toggle();
        event.preventDefault();
    });

    $("a#advanced-search-toggler").click(function(event) {
        if ($("#search_help").is(":visible")) {
            $("#search_help").slideUp("fast");
        } else {
            $("#search_help").slideDown("fast");
        }
        event.preventDefault();
    });

    // Merge request status color picking
    $("#merge_request_statuses input.color_pickable").SevenColorPicker();

    // Toggle details of commit events
    $("a.commit_event_toggler").click(function(event){
        var callbackUrl = $(this).attr("gts:url");
        var eventId = $(this).attr("gts:id");

        $("#commits_in_event_" + eventId).toggle();
        if ($("#commits_in_event_" + eventId).is(":visible")) {
          $("#commits_in_event_" + eventId).load(callbackUrl);
        }
        event.preventDefault();
    });
    
    // frontpage for non-loggedin users
    // Unobtrusively hooking the regular/OpenID login box stuff, so that it works
    // in a semi-sensible way with javascript disabled.
    $("#big_header_login_box_to_openid, #big_header_login_box_to_regular").click(function(e){
        $("#big_header_login_box_openid").toggle("fast");
        $("#big_header_login_box_regular").toggle("fast");
        e.preventDefault();
    });

    // replace the search form input["submit"] with something fancier
    $("#main_menu_search_form").each(function(){
        var headerSearchForm = this;
        var labelText = "Search...";
        var searchInput = $(this).find("input[type=text]");
        searchInput.val(labelText);
        searchInput.click(function(event){
            if (searchInput.val() == labelText) {
              searchInput.val("");
              searchInput.removeClass("unfocused");
            }
        });
        searchInput.blur(function(event){
            if (searchInput.val() == "") {
              searchInput.val(labelText);
              searchInput.addClass("unfocused");
            }
        });
        // hide the 'native' submit button and replace it with our 
        // own awesome submit button
        var nativeSubmitButton = $(this).find("input[type=submit]");
        nativeSubmitButton.hide();
        var awesomeSubmitButton = $(document.createElement("a"));
        awesomeSubmitButton.attr({
            'id':'main_menu_search_form_graphic_submit',
            'href': '#'
        });
        awesomeSubmitButton.click(function(event){
            headerSearchForm.submit();
            event.preventDefault();
        });
        nativeSubmitButton.after(awesomeSubmitButton);
    });

    // toggling of diffs in merge-request diff browser
    $('#merge_request_diff .file-diff .header').live("click", function(event) {
        var hunksContainer = $(this).next();
        if (hunksContainer.is(":visible")) {
          $(this).removeClass("open").addClass("closed");
          hunksContainer.slideUp();
        } else {
          $(this).removeClass("closed").addClass("open");
          hunksContainer.slideDown();
        }
        event.preventDefault();
    });
    $("#merge_request_diff .file-diff-controls a#expand-all").live("click", function(e){
        var container = $(this).parent().parent().parent();
        container.find('.file-diff .header').removeClass("closed").addClass("open");
        container.find('.diff-hunks:hidden').show();
        $.cookie("merge-requests-diff-hunks-state", "expanded");
        e.preventDefault();
    });
    $("#merge_request_diff .file-diff-controls a#collapse-all").live("click", function(e){
        var container = $(this).parent().parent().parent();
        container.find('.file-diff .header').removeClass("open").addClass("closed");
        container.find('.diff-hunks').hide();
        $.cookie("merge-requests-diff-hunks-state", null);
        e.preventDefault();
    });

    // merge request diffing loading indimacator
    Gitorious.MergeRequestDiffSpinner = $("#merge_request_diff_loading").html();
    $("#merge_request_diff").html(Gitorious.MergeRequestDiffSpinner);

    // Merge request selection of branches, compact mode
    // wrapped in a function so we can reuse it when we load another version
    var diffBrowserCompactCommitSelectable = function() {
      var selectingAndUnselecting = function() {
        $("#current_shas .several_shas .label").html("Selecting:");
        var commits = $("li.ui-selecting a");
        if (!commits[0]) return true;
        var first_commit_sha = $(commits[0]).attr("data-commit-sha");
        var last_commit_sha = $(commits[commits.length - 1]).attr("data-commit-sha");
        
        var shaSpec = new Gitorious.ShaSpec();
        shaSpec.addSha(first_commit_sha);
        shaSpec.addSha(last_commit_sha);          
        shaSpec.summarizeHtml();
      };
      return jQuery("#merge_request_commit_selector.compact").selectable({
        filter: "li.single_commit",
        stop: function(e, ui) {
            var sha_spec = new Gitorious.ShaSpec();
            jQuery("li.ui-selected a", this).each(function() {
                sha = jQuery(this).attr("data-commit-sha");
                sha_spec.addSha(sha);
            });
          $("#current_shas .label").html("Selected:");
            var mr_diff_url = jQuery("#merge_request_commit_selector")
              .attr("data-merge-request-version-url");
            var diff_browser = new Gitorious.DiffBrowser(sha_spec.shaSpec());
        },
        selecting: function(e, ui) {
          selectingAndUnselecting();
        },
        unselecting: function(e,ui) {
          selectingAndUnselecting();
        },
        cancel: ".merge_base"
      });
    }
    Gitorious.currentMRCompactSelectable = diffBrowserCompactCommitSelectable();

    $("#merge_request_version").changableSelection({
      onChange: function() {
          var version = $(this).text().replace(/[^0-9]+/g, '');
          var url = $(this).parent().prev().attr("gts:url") + '?version=' + version;
          $("#diff_browser_for_current_version").load(url, null, function() {
            new Gitorious.DiffBrowser(
              jQuery("#current_shas").attr("data-merge-request-current-shas") );
            // jump through hoops and beat the selectable into submission,
            // since it doesn't use live events, we have to re-create it, which sucks...
            Gitorious.currentMRCompactSelectable.selectable("destroy");
            Gitorious.currentMRCompactSelectable = diffBrowserCompactCommitSelectable();
          });
      }
    });
    
    $("#merge_request_current_version ul.compact li.single_commit").hoverBubble();
    
    // Merge request selection of branches, monster mode
    $("#large_commit_selector_toggler").live("click", function(event) {
        if ($("#large_commit_selector").is(":visible")) {
          $("#large_commit_selector").slideUp();
        } else {
          $("#large_commit_selector").slideDown();
        }
{'fraction_non_alphanumeric': 0.1643362015980332, 'fraction_numerical': 0.0033036263060848186, 'mean_word_length': 2.5569066812406067, 'pattern_counts': {'":': 8, '<': 17, '<?xml version=': 0, '>': 17, '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': '5088052', 'n_tokens_mistral': 7439, 'n_tokens_neox': 7343, 'n_words': 1399}