/* #-- # Copyright (C) 2007-2009 Johan Sørensen # Copyright (C) 2009 Marius Mathiesen # # 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 . #-- */ $(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(); }); // Markdown help toggling $(".markdown-help-toggler").click(function(event){ $(".markdown_help").toggle(); event.preventDefault(); }); $("a#advanced-search-toggler").click(function(event) { $("#search_help").slideToggle(); 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); }); // Commment editing $(".comment .edit_link a").live("click", function(){ var commentContainer = $(this).parents(".comment"); var formUrl = $(this).attr("gts:url"); var spinner = $(this).parent().next(".link-spinner").show(); var commentId = commentContainer.attr("gts:comment-id"); var commentLink = commentContainer.siblings("[name=comment_" + commentId + "]"); jQuery.ajax({url:formUrl, success:function(data) { spinner.hide(); commentContainer.append(data); commentContainer.find("form").submit(function(){ var url = $(this).attr("action"); var data = $(this).serialize(); jQuery.post(url, data, function(payload) { commentLink.remove(); commentContainer.replaceWith(payload); }); return false; }) }, error: function(){ spinner.hide(); commentContainer.append( "

We're sorry, but you're not allowed to edit the comment. " + "Only the creator of a comment may edit it, and then only for " + "a short period of time after it's been created

" ); }}); }); $(".comment .comment_form .cancel").live("click", function(){ var theForm = $(this).parents(".comment_form"); theForm.remove(); }); // Relative times based on clients browser time jQuery.extend(jQuery.timeago.settings.strings, { seconds: "a minute", minute: "a minute", minutes: "%d minutes", hour: "an hour", hours: "%d hours", day: "a day", days: "%d days", month: "a month", months: "%d months", year: "a year", years: "%d years" }); jQuery('abbr.timeago').timeago(); // Rails generates some quite obtrusive markup when // :method => whatever is used in link_to etc. jQuery("a[data-request-method]").replaceRailsGeneratedForm(); // watchable/favorite list filtering $("#watchable-list").each(function() { $this = $(this); $this.find(".filters a.all").addClass("current") $this.find(".filters a").css({'outline':'none'}); var makeCurrent = function(newCurrent) { $this.find(".filters a").removeClass("current"); $(newCurrent).addClass("current"); }; var swapAndMakeCurrent = function(klass, current) { $this.find(".favorite." + klass).show(); $this.find(".favorite:not(." + klass + ")").hide(); makeCurrent(current); } $this.find(".filters a.all").click(function() { $this.find(".favorite").show(); makeCurrent(this); return false; }); $this.find(".filters a.repositories").click(function() { swapAndMakeCurrent("repository", this); return false; }); $this.find(".filters a.merge-requests").click(function() { swapAndMakeCurrent("merge_request"); return false; }); $this.find(".filters a.mine").click(function() { swapAndMakeCurrent("mine"); return false; }); $this.find(".filters a.foreign").click(function() { swapAndMakeCurrent("foreign"); return false; }); }); // Favorite toggling and deletion on the /favorites page $("#favorite-listing tr:odd").addClass("odd"); $("#favorite-listing td.notification .favorite.update a").click(function() { $this = $(this); if ("off" == $this.text()) { payload = "_method=put&favorite[notify_by_email]=1"; } else { payload = "_method=put&favorite[notify_by_email]=0"; } $.post($this.attr("href"), payload, function(data, respTxt){ if ("success" === respTxt) { if ("off" === $this.text()) { $this.text("on").removeClass("disabled").addClass("enabled"); } else { $this.text("off").removeClass("enabled").addClass("disabled") } } }); return false; }); $("#favorite-listing td.unwatch .favorite a.watch-link").click(function() { $this = $(this); payload = "_method=delete"; $.post($this.attr("href"), payload, function(data, respTxt){ if ("success" === respTxt) { $this.parents("tr").fadeOut("normal", function(){ $(this).remove(); $("#favorite-listing tr").removeClass("odd"); $("#favorite-listing tr:odd").addClass("odd"); }); } }); return false; }); }); if (!Gitorious) var Gitorious = {}; Gitorious.DownloadChecker = { checkURL: function(url, container) { var element = $("#" + container); //element.absolutize(); var sourceLink = element.prev(); // Position the box if (sourceLink) { element.css({ 'top': parseInt(element[0].style.top) - (element.height()+10) + "px", 'width': '175px', 'height': '70px', 'position': 'absolute' }); } element.html('

'); element.show(); // load the status element.load(url, function(responseText, textStatus, XMLHttpRequest){ if (textStatus == "success") { $(this).html(responseText); } }); return false; } }; // Gitorious.Wordwrapper = { // wrap: function(elements) { // elements.each(function(e) { // //e.addClassName("softwrapped"); // e.removeClassName("unwrapped"); // }); // }, // unwrap: function(elements) { // elements.each(function(e) { // //e.removeClassName("softwrapped"); // e.addClassName("unwrapped"); // }); // }, // toggle: function(elements) { // if (/unwrapped/.test(elements.first().className)) { // Gitorious.Wordwrapper.wrap(elements); // } else { // Gitorious.Wordwrapper.unwrap(elements); // } // } // } // A class used for selecting ranges of objects function CommitRangeSelector(commitListUrl, targetBranchesUrl, statusElement) { this.commitListUrl = commitListUrl this.targetBranchesUrl = targetBranchesUrl; this.statusElement = statusElement; this.endsAt = null; this.sourceBranchName = null; this.targetBranchName = null; this.REASONABLY_SANE_RANGE_SIZE = 50; this.endSelected = function(el) { this.endsAt = $(el); this.update(); }; this.onSourceBranchChange = function(event) { if (sourceBranch = $('#merge_request_source_branch')) { this.sourceBranchSelected(sourceBranch); } }; this.onTargetRepositoryChange = function(event) { $("#spinner").fadeIn(); $.post(this.targetBranchesUrl, $("#new_merge_request").serialize(), function(data, responseText) { if (responseText === "success") { $("#target_branch_selection").html(data); $("#spinner").fadeOut(); } }); this._updateCommitList(); }; this.onTargetBranchChange = function(event) { if (targetBranch = $('#merge_request_target_branch').val()) { this.targetBranchSelected(targetBranch); } }; this.targetBranchSelected = function(branchName) { if (branchName != this.targetBranchName) { this.targetBranchName = branchName; this._updateCommitList(); } }; this.sourceBranchSelected = function(branchName) { if (branchName != this.sourceBranchName) { this.sourceBranchName = branchName; this._updateCommitList(); } }; this.update = function() { if (this.endsAt) { $(".commit_row").each(function(){ $(this).removeClass("selected") }); var selectedTr = this.endsAt.parent().parent(); selectedTr.addClass('selected'); var selectedTrCount = 1; selectedTr.nextAll().each(function() { $(this).addClass('selected'); selectedTrCount++; }); if (selectedTrCount > this.REASONABLY_SANE_RANGE_SIZE) { $("#large_selection_warning").slideDown(); } else { $("#large_selection_warning").slideUp(); } // update the status field with the selected range var to = selectedTr.find(".sha-abbrev a").html(); var from = $(".commit_row:last .sha-abbrev a").html(); $("." + this.statusElement).each(function() { $(this).html(from + ".." + to); }); } }; this._updateCommitList = function() { $("#commit_table").replaceWith('

Loading commits… ' + '

'); $.post(this.commitListUrl, $("#new_merge_request").serialize(), function(data, responseText) { if (responseText === "success") $("#commit_selection").html(data); }); } } function toggle_wiki_preview(target_url) { var wiki_preview = $('#page_preview'); var wiki_edit = $('#page_content'); var wiki_form = wiki_edit[0].form; var toggler = $('#wiki_preview_toggler'); if (toggler.val() == "Hide preview") { toggler.val("Show preview"); } else { toggler.val("Hide preview"); wiki_preview.html(""); $.post(target_url, $(wiki_form).serialize(), function(data, textStatus){ if (textStatus == "success") { wiki_preview.html(data); } }); } jQuery.each([wiki_preview, wiki_edit], function(){ $(this).toggle() }); } // function load_commit_status() // { // var merge_request_uri = document.location.pathname; // ['merged','unmerged'].each(function(s) // { // var i1 = new Image(); // i1.src = "/images/merge_requests/" + s + ".png"; // }); // $$('tr.commit_row').each(function(commit_row) // { // id = commit_row.getAttribute('data-merge-request-commit-id'); // new Ajax.Request(merge_request_uri + "/commit_status?commit_id=" + id, {method:'get', onSuccess: function(transport){ // commit_row.removeClassName("unknown-status"); // if (transport.responseText == 'false'){ // commit_row.addClassName("unmerged"); // } // else{ // commit_row.addClassName("merged"); // } // }}); // }); // }