forgejo-federation/public/js/app.js

621 lines
20 KiB
JavaScript
Raw Normal View History

2014-05-06 01:51:43 +05:30
var Gogits = {};
2014-03-24 18:57:19 +05:30
(function ($) {
2014-03-22 23:14:02 +05:30
// extend jQuery ajax, set csrf token value
var ajax = $.ajax;
$.extend({
2014-03-24 18:57:19 +05:30
ajax: function (url, options) {
2014-03-22 23:14:02 +05:30
if (typeof url === 'object') {
options = url;
url = undefined;
}
options = options || {};
url = options.url;
var csrftoken = $('meta[name=_csrf]').attr('content');
var headers = options.headers || {};
var domain = document.domain.replace(/\./ig, '\\.');
if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
2014-03-24 18:57:19 +05:30
headers = $.extend(headers, {'X-Csrf-Token': csrftoken});
2014-03-22 23:14:02 +05:30
}
options.headers = headers;
var callback = options.success;
2014-03-24 18:57:19 +05:30
options.success = function (data) {
if (data.once) {
2014-03-22 23:14:02 +05:30
// change all _once value if ajax data.once exist
$('[name=_once]').val(data.once);
}
2014-03-24 18:57:19 +05:30
if (callback) {
2014-03-22 23:14:02 +05:30
callback.apply(this, arguments);
}
};
return ajax(url, options);
2014-03-23 19:44:29 +05:30
},
2014-03-24 18:57:19 +05:30
changeHash: function (hash) {
if (history.pushState) {
2014-03-23 19:44:29 +05:30
history.pushState(null, null, hash);
}
else {
location.hash = hash;
}
},
2014-03-24 18:57:19 +05:30
deSelect: function () {
if (window.getSelection) {
2014-03-23 19:44:29 +05:30
window.getSelection().removeAllRanges();
} else {
document.selection.empty();
}
2014-03-22 23:14:02 +05:30
}
});
2014-03-27 21:02:20 +05:30
$.fn.extend({
toggleHide: function () {
$(this).addClass("hidden");
},
toggleShow: function () {
$(this).removeClass("hidden");
2014-03-29 18:16:36 +05:30
},
toggleAjax: function (successCallback) {
var url = $(this).data("ajax");
var method = $(this).data('ajax-method') || 'get';
var ajaxName = $(this).data('ajax-name');
var data = {};
2014-05-05 22:38:01 +05:30
if (ajaxName.endsWith("preview")) {
data["mode"] = "gfm";
data["context"] = $(this).data('ajax-context');
}
2014-03-29 18:16:36 +05:30
$('[data-ajax-rel=' + ajaxName + ']').each(function () {
var field = $(this).data("ajax-field");
var t = $(this).data("ajax-val");
if (t == "val") {
data[field] = $(this).val();
return true;
}
if (t == "txt") {
data[field] = $(this).text();
return true;
}
if (t == "html") {
data[field] = $(this).html();
return true;
}
if (t == "data") {
data[field] = $(this).data("ajax-data");
return true;
}
return true;
});
$.ajax({
url: url,
method: method.toUpperCase(),
data: data,
success: function (d) {
if (successCallback) {
successCallback(d);
}
}
})
2014-03-27 21:02:20 +05:30
}
})
2014-03-22 23:14:02 +05:30
}(jQuery));
2014-03-06 20:25:32 +05:30
(function ($) {
2014-03-10 14:24:52 +05:30
Gogits.showTab = function (selector, index) {
if (!index) {
index = 0;
}
$(selector).tab("show");
$(selector).find("li:eq(" + index + ") a").tab("show");
2014-03-06 20:25:32 +05:30
};
Gogits.validateForm = function (selector, options) {
var $form = $(selector);
options = options || {};
options.showErrors = function (map, list) {
var $error = $form.find('.form-error').addClass('hidden');
$('.has-error').removeClass("has-error");
$error.text(list[0].message).show().removeClass("hidden");
$(list[0].element).parents(".form-group").addClass("has-error");
};
$form.validate(options);
};
2014-03-10 14:24:52 +05:30
// ----- init elements
Gogits.initModals = function () {
var modals = $("[data-toggle=modal]");
if (modals.length < 1) {
return;
}
$.each(modals, function (i, item) {
2014-03-10 18:42:49 +05:30
var hide = $(item).data('modal');
$(item).modal(hide ? hide : "hide");
2014-03-10 14:24:52 +05:30
});
};
Gogits.initTooltips = function () {
$("body").tooltip({
selector: "[data-toggle=tooltip]"
//container: "body"
});
};
2014-03-17 15:43:07 +05:30
Gogits.initPopovers = function () {
2014-03-20 17:42:31 +05:30
var hideAllPopovers = function () {
$('[data-toggle=popover]').each(function () {
2014-03-17 15:43:07 +05:30
$(this).popover('hide');
2014-03-20 17:42:31 +05:30
});
2014-03-17 15:43:07 +05:30
};
2014-03-20 17:42:31 +05:30
$(document).on('click', function (e) {
2014-03-17 15:43:07 +05:30
var $e = $(e.target);
2014-03-20 17:42:31 +05:30
if ($e.data('toggle') == 'popover' || $e.parents("[data-toggle=popover], .popover").length > 0) {
2014-03-17 15:43:07 +05:30
return;
}
hideAllPopovers();
});
$("body").popover({
selector: "[data-toggle=popover]"
});
};
2014-03-10 18:42:49 +05:30
Gogits.initTabs = function () {
2014-03-13 11:38:49 +05:30
var $tabs = $('[data-init=tabs]');
2014-03-25 18:44:05 +05:30
$tabs.tab("show");
2014-03-13 11:38:49 +05:30
$tabs.find("li:eq(0) a").tab("show");
2014-03-17 12:47:44 +05:30
};
2014-04-02 22:26:26 +05:30
2014-03-21 20:57:33 +05:30
// fix dropdown inside click
2014-03-24 18:57:19 +05:30
Gogits.initDropDown = function () {
$('.dropdown-menu.no-propagation').on('click', function (e) {
2014-03-21 20:57:33 +05:30
e.stopPropagation();
});
};
2014-03-17 12:47:44 +05:30
2014-04-02 22:26:26 +05:30
2014-03-17 12:47:44 +05:30
// render markdown
Gogits.renderMarkdown = function () {
2014-03-20 14:55:48 +05:30
var $md = $('.markdown');
var $pre = $md.find('pre > code').parent();
2014-03-20 21:25:27 +05:30
$pre.addClass('prettyprint linenums');
2014-03-17 12:47:44 +05:30
prettyPrint();
2014-03-20 14:55:48 +05:30
// Set anchor.
var headers = {};
$md.find('h1, h2, h3, h4, h5, h6').each(function () {
var node = $(this);
var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
var name = val;
2014-03-20 19:02:08 +05:30
if (headers[val] > 0) {
2014-03-20 14:55:48 +05:30
name = val + '-' + headers[val];
}
2014-03-20 19:02:08 +05:30
if (headers[val] == undefined) {
2014-03-20 14:55:48 +05:30
headers[val] = 1;
2014-03-20 19:02:08 +05:30
} else {
2014-03-20 14:55:48 +05:30
headers[val] += 1;
}
node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
});
2014-03-24 18:57:19 +05:30
};
2014-03-17 12:47:44 +05:30
2014-04-02 22:26:26 +05:30
// render code view
2014-03-23 18:28:12 +05:30
Gogits.renderCodeView = function () {
2014-03-24 18:57:19 +05:30
function selectRange($list, $select, $from) {
2014-03-23 19:44:29 +05:30
$list.removeClass('active');
2014-03-24 18:57:19 +05:30
if ($from) {
2014-03-23 19:44:29 +05:30
var a = parseInt($select.attr('rel').substr(1));
var b = parseInt($from.attr('rel').substr(1));
var c;
2014-03-24 18:57:19 +05:30
if (a != b) {
if (a > b) {
2014-03-23 19:44:29 +05:30
c = a;
a = b;
b = c;
}
var classes = [];
2014-03-24 18:57:19 +05:30
for (i = a; i <= b; i++) {
classes.push('.L' + i);
2014-03-23 19:44:29 +05:30
}
$list.filter(classes.join(',')).addClass('active');
$.changeHash('#L' + a + '-' + 'L' + b);
return
}
}
$select.addClass('active');
$.changeHash('#' + $select.attr('rel'));
}
$(document).on('click', '.lines-num span', function (e) {
var $select = $(this);
var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
2014-03-24 18:57:19 +05:30
selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
2014-03-23 19:44:29 +05:30
$.deSelect();
});
2014-03-24 18:57:19 +05:30
$('.code-view .lines-code > pre').each(function () {
2014-03-23 18:28:12 +05:30
var $pre = $(this);
2014-03-23 19:44:29 +05:30
var $lineCode = $pre.parent();
var $lineNums = $lineCode.siblings('.lines-num');
2014-03-23 18:28:12 +05:30
if ($lineNums.length > 0) {
var nums = $pre.find('ol.linenums > li').length;
for (var i = 1; i <= nums; i++) {
2014-03-23 19:44:29 +05:30
$lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
2014-03-23 18:28:12 +05:30
}
}
});
2014-03-23 19:44:29 +05:30
2014-05-01 15:14:22 +05:30
$(window).on('hashchange', function (e) {
2014-03-23 19:44:29 +05:30
var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
var $list = $('.code-view ol.linenums > li');
2014-03-24 18:57:19 +05:30
if (m) {
var $first = $list.filter('.' + m[1]);
selectRange($list, $first, $list.filter('.' + m[2]));
$("html, body").scrollTop($first.offset().top - 200);
2014-03-23 19:44:29 +05:30
return;
}
m = window.location.hash.match(/^#(L\d+)$/);
2014-03-24 18:57:19 +05:30
if (m) {
var $first = $list.filter('.' + m[1]);
2014-03-23 19:44:29 +05:30
selectRange($list, $first);
2014-03-24 18:57:19 +05:30
$("html, body").scrollTop($first.offset().top - 200);
2014-03-23 19:44:29 +05:30
}
}).trigger('hashchange');
2014-03-23 18:28:12 +05:30
};
2014-04-02 22:26:26 +05:30
// copy utils
Gogits.bindCopy = function (selector) {
if ($(selector).hasClass('js-copy-bind')) {
return;
}
$(selector).zclip({
path: "/js/ZeroClipboard.swf",
copy: function () {
var t = $(this).data("copy-val");
var to = $($(this).data("copy-from"));
var str = "";
if (t == "txt") {
str = to.text();
}
if (t == 'val') {
str = to.val();
}
if (t == 'html') {
str = to.html();
}
return str;
},
afterCopy: function () {
var $this = $(this);
$this.tooltip('hide')
.attr('data-original-title', 'Copied OK');
setTimeout(function () {
$this.tooltip("show");
}, 200);
setTimeout(function () {
$this.tooltip('hide')
.attr('data-original-title', 'Copy to Clipboard');
}, 3000);
}
}).addClass("js-copy-bind");
}
2014-03-10 18:42:49 +05:30
})(jQuery);
// ajax utils
(function ($) {
Gogits.ajaxDelete = function (url, data, success) {
data = data || {};
data._method = "DELETE";
$.ajax({
url: url,
data: data,
method: "POST",
dataType: "json",
success: function (json) {
if (success) {
success(json);
}
}
})
}
2014-03-03 20:10:22 +05:30
})(jQuery);
2014-03-06 20:25:32 +05:30
function initCore() {
2014-03-10 14:24:52 +05:30
Gogits.initTooltips();
2014-03-17 15:43:07 +05:30
Gogits.initPopovers();
2014-03-13 11:38:49 +05:30
Gogits.initTabs();
2014-03-10 14:24:52 +05:30
Gogits.initModals();
2014-03-21 20:57:33 +05:30
Gogits.initDropDown();
2014-03-17 12:47:44 +05:30
Gogits.renderMarkdown();
2014-03-23 18:28:12 +05:30
Gogits.renderCodeView();
2014-03-06 20:25:32 +05:30
}
2014-03-17 12:47:44 +05:30
function initUserSetting() {
// ssh confirmation
2014-03-25 16:14:37 +05:30
$('#ssh-keys .delete').confirmation({
singleton: true,
2014-03-17 12:47:44 +05:30
onConfirm: function (e, $this) {
Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
if (json.ok) {
window.location.reload();
2014-03-17 12:47:44 +05:30
} else {
alert(json.err);
}
});
}
2014-03-10 18:42:49 +05:30
});
// profile form
(function () {
$('#user-setting-username').on("keyup", function () {
var $this = $(this);
if ($this.val() != $this.attr('title')) {
$this.next('.help-block').toggleShow();
} else {
$this.next('.help-block').toggleHide();
}
});
}())
2014-03-16 20:55:01 +05:30
}
2014-03-20 17:42:31 +05:30
function initRepository() {
2014-03-21 20:57:33 +05:30
// clone group button script
2014-03-20 19:02:08 +05:30
(function () {
2014-03-21 20:57:33 +05:30
var $clone = $('.clone-group-btn');
if ($clone.length) {
var $url = $('.clone-group-url');
2014-05-01 15:14:22 +05:30
$clone.find('button[data-link]').on("click", function (e) {
2014-03-20 19:02:08 +05:30
var $this = $(this);
if (!$this.hasClass('btn-primary')) {
$clone.find('.input-group-btn .btn-primary').removeClass('btn-primary').addClass("btn-default");
2014-03-20 19:02:08 +05:30
$(this).addClass('btn-primary').removeClass('btn-default');
$url.val($this.data("link"));
2014-03-21 20:57:33 +05:30
$clone.find('span.clone-url').text($this.data('link'));
2014-03-20 19:02:08 +05:30
}
}).eq(0).trigger("click");
$("#repo-clone").on("shown.bs.dropdown", function () {
2014-04-02 22:26:26 +05:30
Gogits.bindCopy("[data-init=copy]");
});
Gogits.bindCopy("[data-init=copy]:visible");
2014-03-20 19:02:08 +05:30
}
})();
2014-03-20 20:09:10 +05:30
// watching script
(function () {
2014-03-25 16:14:37 +05:30
var $watch = $('#repo-watching'),
2014-03-20 20:09:10 +05:30
watchLink = $watch.data("watch"),
unwatchLink = $watch.data("unwatch");
2014-05-01 15:14:22 +05:30
$watch.on('click', '.to-watch', function () {
2014-03-20 20:09:10 +05:30
if ($watch.hasClass("watching")) {
return false;
}
$.get(watchLink, function (json) {
if (json.ok) {
$watch.find('.text-primary').removeClass('text-primary');
$watch.find('.to-watch h4').addClass('text-primary');
$watch.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
$watch.removeClass("no-watching").addClass("watching");
}
});
return false;
}).on('click', '.to-unwatch', function () {
if ($watch.hasClass("no-watching")) {
return false;
}
$.get(unwatchLink, function (json) {
if (json.ok) {
$watch.find('.text-primary').removeClass('text-primary');
$watch.find('.to-unwatch h4').addClass('text-primary');
$watch.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
$watch.removeClass("watching").addClass("no-watching");
}
});
return false;
});
})();
2014-03-24 18:57:19 +05:30
// repo diff counter
(function () {
var $counter = $('.diff-counter');
if ($counter.length < 1) {
return;
}
$counter.each(function (i, item) {
var $item = $(item);
var addLine = $item.find('span[data-line].add').data("line");
var delLine = $item.find('span[data-line].del').data("line");
var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
$item.find(".bar .add").css("width", addPercent + "%");
});
}());
// repo setting form
(function () {
$('#repo-setting-name').on("keyup", function () {
var $this = $(this);
if ($this.val() != $this.attr('title')) {
$this.next('.help-block').toggleShow();
} else {
$this.next('.help-block').toggleHide();
}
});
}())
2014-03-20 17:42:31 +05:30
}
2014-03-27 21:02:20 +05:30
function initInstall() {
// database type change
2014-03-29 18:46:06 +05:30
(function () {
2014-05-01 15:14:22 +05:30
var mysql_default = '127.0.0.1:3306'
var postgres_default = '127.0.0.1:5432'
2014-03-29 18:46:06 +05:30
$('#install-database').on("change", function () {
var val = $(this).val();
2014-04-27 10:04:48 +05:30
if (val != "SQLite3") {
2014-03-29 18:46:06 +05:30
$('.server-sql').show();
$('.sqlite-setting').addClass("hide");
2014-04-27 10:04:48 +05:30
if (val == "PostgreSQL") {
2014-03-29 18:46:06 +05:30
$('.pgsql-setting').removeClass("hide");
// Change the host value to the Postgres default, but only
// if the user hasn't already changed it from the MySQL
// default.
if ($('#database-host').val() == mysql_default) {
$('#database-host').val(postgres_default);
}
} else if (val == 'MySQL') {
$('.pgsql-setting').addClass("hide");
if ($('#database-host').val() == postgres_default) {
$('#database-host').val(mysql_default);
}
2014-03-29 18:46:06 +05:30
} else {
$('.pgsql-setting').addClass("hide");
}
2014-03-27 18:09:18 +05:30
} else {
2014-03-29 18:46:06 +05:30
$('.server-sql').hide();
$('.sqlite-setting').removeClass("hide");
2014-03-27 18:09:18 +05:30
}
2014-03-29 18:46:06 +05:30
});
}());
2014-03-27 18:09:18 +05:30
}
2014-03-27 21:02:20 +05:30
function initIssue() {
// close button
(function () {
var $closeBtn = $('#issue-close-btn');
var $openBtn = $('#issue-open-btn');
$('#issue-reply-content').on("keyup", function () {
if ($(this).val().length) {
2014-03-29 18:16:36 +05:30
$closeBtn.val($closeBtn.data("text"));
$openBtn.val($openBtn.data("text"));
2014-03-27 21:02:20 +05:30
} else {
2014-03-29 18:16:36 +05:30
$closeBtn.val($closeBtn.data("origin"));
$openBtn.val($openBtn.data("origin"));
2014-03-27 21:02:20 +05:30
}
});
}());
// issue edit mode
(function () {
$("#issue-edit-btn").on("click", function () {
$('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
$('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
});
$('.issue-edit-cancel').on("click", function () {
$('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
$('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
})
}());
2014-03-29 18:16:36 +05:30
// issue ajax update
2014-03-29 18:46:06 +05:30
(function () {
$('.issue-edit-save').on("click", function () {
$(this).toggleAjax(function (json) {
if (json.ok) {
$('.issue-head h1.title').text(json.title);
$('.issue-main > .issue-content .content').html(json.content);
2014-03-29 20:33:01 +05:30
$('.issue-edit-cancel').trigger("click");
2014-03-29 18:46:06 +05:30
}
});
2014-03-29 18:16:36 +05:30
});
2014-03-29 18:46:06 +05:30
}());
// issue ajax preview
(function () {
$('[data-ajax-name=issue-preview]').on("click", function () {
var $this = $(this);
2014-05-05 22:38:01 +05:30
$this.toggleAjax(function (resp) {
$($this.data("preview")).html(resp);
2014-03-29 18:46:06 +05:30
})
});
$('.issue-write a[data-toggle]').on("click", function () {
$('.issue-preview-content').html("loading...");
});
}())
2014-03-27 21:02:20 +05:30
}
2014-04-06 13:59:45 +05:30
function initRelease() {
// release new ajax preview
(function () {
$('[data-ajax-name=release-preview]').on("click", function () {
var $this = $(this);
$this.toggleAjax(function (json) {
if (json.ok) {
$($this.data("preview")).html(json.content);
}
})
});
$('.release-write a[data-toggle]').on("click", function () {
$('.release-preview-content').html("loading...");
});
}());
// release new target selection
(function () {
$('#release-new-target-branch-list').on('click', 'a', function () {
$('#tag-target').val($(this).text());
$('#release-new-target-name').text(" " + $(this).text());
});
}());
2014-04-06 13:59:45 +05:30
}
2014-05-01 15:14:22 +05:30
function initRepoSetting() {
// repo member add
$('#repo-collaborator').on('keyup', function () {
var $this = $(this);
if (!$this.val()) {
$this.next().toggleHide();
return;
}
$.ajax({
url: '/api/v1/users/search?q=' + $this.val(),
dataType: "json",
success: function (json) {
2014-05-01 21:01:35 +05:30
if (json.ok && json.data.length) {
2014-05-01 15:14:22 +05:30
var html = '';
$.each(json.data, function (i, item) {
html += '<li><img src="' + item.avatar + '">' + item.username + '</li>';
});
$this.next().toggleShow();
$this.next().find('ul').html(html);
}else{
$this.next().toggleHide();
}
}
});
}).on('focus', function () {
if (!$(this).val()) {
$(this).next().toggleHide();
}
}).next().on("click",'li',function(){
$('#repo-collaborator').val($(this).text());
});
}
2014-03-17 12:47:44 +05:30
(function ($) {
$(function () {
initCore();
2014-03-25 16:14:37 +05:30
var body = $("#body");
2014-03-17 12:47:44 +05:30
if (body.data("page") == "user") {
initUserSetting();
}
2014-03-25 16:14:37 +05:30
if ($('.repo-nav').length) {
2014-03-20 17:42:31 +05:30
initRepository();
}
2014-03-27 21:02:20 +05:30
if ($('#install-card').length) {
2014-03-27 18:09:18 +05:30
initInstall();
}
2014-03-27 21:02:20 +05:30
if ($('#issue').length) {
initIssue();
}
2014-04-06 13:59:45 +05:30
if ($('#release').length) {
initRelease();
}
2014-05-01 15:14:22 +05:30
if ($('#repo-setting-container').length) {
initRepoSetting();
}
2014-03-16 20:55:01 +05:30
});
})(jQuery);