wide/static/js/menu.js

491 lines
18 KiB
JavaScript
Raw Normal View History

2015-01-01 05:06:33 +03:00
/*
* Copyright (c) 2015, B3log
*
2014-11-12 18:13:14 +03:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* http://www.apache.org/licenses/LICENSE-2.0
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2014-11-18 05:03:08 +03:00
*/
2014-11-12 18:13:14 +03:00
2014-09-10 14:08:35 +04:00
var menu = {
2014-10-13 13:01:44 +04:00
init: function () {
2014-09-10 14:08:35 +04:00
this.subMenu();
2014-11-18 05:03:08 +03:00
this._initPreference();
this._initAbout();
2014-12-14 07:20:39 +03:00
this._initShare();
2014-09-13 09:05:50 +04:00
// 点击子菜单后消失
2015-01-03 11:01:24 +03:00
$(".menu .frame li").click(function () {
$(".menu > ul > li").unbind().removeClass("selected");
menu.subMenu();
2014-09-13 09:05:50 +04:00
});
2014-12-14 07:20:39 +03:00
},
_initShare: function () {
$(".menu .ico-share").hover(function () {
$(".menu .share-panel").show();
});
$(".share-panel .font-ico").click(function () {
var key = $(this).attr('class').split('-')[2];
2014-12-18 11:46:34 +03:00
var url = "https://wide.b3log.org", pic = 'https://wide.b3log.org/static/images/wide-logo.png';
2014-12-14 07:20:39 +03:00
var urls = {};
2014-12-18 11:46:34 +03:00
urls.email = "mailto:?subject=" + $('title').text()
2014-12-14 08:06:53 +03:00
+ "&body=" + $('meta[name=description]').attr('content') + ' ' + url;
2014-12-18 11:46:34 +03:00
var twitterShare = encodeURIComponent($('meta[name=description]').attr('content') + " " + url + " #golang");
urls.twitter = "https://twitter.com/intent/tweet?status=" + twitterShare;
2014-12-14 07:20:39 +03:00
urls.facebook = "https://www.facebook.com/sharer/sharer.php?u=" + url;
2014-12-18 11:46:34 +03:00
urls.googleplus = "https://plus.google.com/share?url=" + url;
var title = encodeURIComponent($('title').text() + '. \n' + $('meta[name=description]').attr('content')
+ " #golang#");
urls.weibo = "http://v.t.sina.com.cn/share/share.php?title=" + title + "&url=" + url + "&pic=" + pic;
2014-12-14 07:20:39 +03:00
urls.tencent = "http://share.v.t.qq.com/index.php?c=share&a=index&title=" + title +
"&url=" + url + "&pic=" + pic;
2015-01-03 11:01:24 +03:00
2014-12-14 07:20:39 +03:00
window.open(urls[key], "_blank", "top=100,left=200,width=648,height=618");
});
2014-09-10 14:08:35 +04:00
},
2014-11-18 05:03:08 +03:00
_initAbout: function () {
2014-12-11 10:32:24 +03:00
$("#dialogAbout").load(config.context + '/about', function () {
2014-11-18 05:03:08 +03:00
$("#dialogAbout").dialog({
"modal": true,
"height": 460,
"width": 800,
"title": config.label.about,
"hideFooter": true,
"afterOpen": function () {
$.ajax({
2014-12-15 05:30:42 +03:00
url: "https://rhythm.b3log.org/version/wide/latest",
2014-11-18 05:03:08 +03:00
type: "GET",
dataType: "jsonp",
jsonp: "callback",
success: function (data, textStatus) {
if ($("#dialogAbout .version").text() === data.wideVersion) {
$(".upgrade").text(config.label.uptodate);
} else {
$(".upgrade").html(config.label.new_version_available + config.label.colon
+ "<a href='" + data.wideDownload
+ "' target='_blank'>" + data.wideVersion + "</a>");
}
}
});
}
});
});
},
2014-10-13 13:01:44 +04:00
disabled: function (list) {
for (var i = 0, max = list.length; i < max; i++) {
$(".menu li." + list[i]).addClass("disabled");
}
},
undisabled: function (list) {
for (var i = 0, max = list.length; i < max; i++) {
$(".menu li." + list[i]).removeClass("disabled");
}
},
2014-09-13 09:05:50 +04:00
// 焦点不在菜单上时需点击展开子菜单,否则为鼠标移动展开
2014-10-13 13:01:44 +04:00
subMenu: function () {
2015-01-03 11:01:24 +03:00
$(".menu > ul > li").click(function (event) {
if ($(event.target).closest(".frame").length === 1) {
return;
}
2014-09-10 14:08:35 +04:00
var $it = $(this);
2015-01-03 11:01:24 +03:00
$it.find('.frame').show();
$(".menu > ul > li").removeClass("selected");
$(this).addClass("selected");
2014-09-10 14:08:35 +04:00
2015-01-03 11:01:24 +03:00
$(".menu > ul > li").unbind();
2014-09-10 14:08:35 +04:00
2015-01-03 11:01:24 +03:00
$(".menu > ul > li").mouseover(function () {
if ($(event.target).closest(".frame").length === 1) {
return;
}
2015-01-04 06:21:44 +03:00
$(".menu .frame").hide();
2015-01-03 11:01:24 +03:00
$(this).find('.frame').show();
$(".menu > ul > li").removeClass("selected");
$(this).addClass("selected");
2014-09-10 14:08:35 +04:00
});
});
2014-11-18 05:03:08 +03:00
},
openPreference: function () {
$("#dialogPreference").dialog("open");
},
saveAllFiles: function () {
if ($(".menu li.save-all").hasClass("disabled")) {
return false;
}
for (var i = 0, ii = editors.data.length; i < ii; i++) {
var path = tree.fileTree.getNodeByTId(editors.data[i].id).path;
var editor = editors.data[i].editor;
if ("text/x-go" === editor.getOption("mode")) {
wide.fmt(path, editor);
} else {
wide._save(path, editor);
}
}
},
closeAllFiles: function () {
if ($(".menu li.close-all").hasClass("disabled")) {
return false;
}
// 设置全部关闭标识
var removeData = [];
$(".edit-panel .tabs > div").each(function (i) {
if (i !== 0) {
removeData.push($(this).data("index"));
}
});
$("#dialogCloseEditor").data("removeData", removeData);
// 开始关闭
$(".edit-panel .tabs .ico-close:eq(0)").click();
},
exit: function () {
var request = newWideRequest();
$.ajax({
type: 'POST',
2014-12-11 10:32:24 +03:00
url: config.context + '/logout',
2014-11-18 05:03:08 +03:00
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (data.succ) {
window.location.href = "/login";
}
}
});
},
openAbout: function () {
$("#dialogAbout").dialog("open");
},
goget: function () {
menu.saveAllFiles();
var currentPath = editors.getCurrentPath();
if (!currentPath) {
return false;
}
if ($(".menu li.go-get").hasClass("disabled")) {
return false;
}
var request = newWideRequest();
request.file = currentPath;
$.ajax({
type: 'POST',
2014-12-11 10:32:24 +03:00
url: config.context + '/go/get',
2014-11-18 05:03:08 +03:00
data: JSON.stringify(request),
dataType: "json",
beforeSend: function (data) {
bottomGroup.resetOutput();
},
success: function (data) {
}
});
},
goinstall: function () {
menu.saveAllFiles();
var currentPath = editors.getCurrentPath();
if (!currentPath) {
return false;
}
if ($(".menu li.go-install").hasClass("disabled")) {
return false;
}
var request = newWideRequest();
request.file = currentPath;
$.ajax({
type: 'POST',
2014-12-11 10:32:24 +03:00
url: config.context + '/go/install',
2014-11-18 05:03:08 +03:00
data: JSON.stringify(request),
dataType: "json",
beforeSend: function (data) {
bottomGroup.resetOutput();
},
success: function (data) {
}
});
},
2014-12-25 05:41:19 +03:00
// go test.
2014-11-18 05:03:08 +03:00
test: function () {
menu.saveAllFiles();
var currentPath = editors.getCurrentPath();
if (!currentPath) {
return false;
}
2014-12-31 13:02:04 +03:00
if ($(".menu li.go-test").hasClass("disabled")) {
2014-11-18 05:03:08 +03:00
return false;
}
var request = newWideRequest();
request.file = currentPath;
$.ajax({
type: 'POST',
2014-12-11 10:32:24 +03:00
url: config.context + '/go/test',
2014-11-18 05:03:08 +03:00
data: JSON.stringify(request),
dataType: "json",
beforeSend: function (data) {
bottomGroup.resetOutput();
},
success: function (data) {
2014-12-31 13:02:04 +03:00
}
});
},
// go vet.
govet: function () {
menu.saveAllFiles();
var currentPath = editors.getCurrentPath();
if (!currentPath) {
return false;
}
if ($(".menu li.go-vet").hasClass("disabled")) {
return false;
}
var request = newWideRequest();
request.file = currentPath;
$.ajax({
type: 'POST',
url: config.context + '/go/vet',
data: JSON.stringify(request),
dataType: "json",
beforeSend: function (data) {
bottomGroup.resetOutput();
},
success: function (data) {
2014-11-18 05:03:08 +03:00
}
});
},
2014-11-19 04:39:06 +03:00
// Build & Run.
2014-11-18 05:03:08 +03:00
run: function () {
menu.saveAllFiles();
var currentPath = editors.getCurrentPath();
if (!currentPath) {
return false;
}
if ($(".menu li.run").hasClass("disabled")) {
return false;
}
2014-12-13 17:58:43 +03:00
if ($("#buildRun").hasClass("ico-stop")) {
2014-11-18 05:03:08 +03:00
wide.stop();
return false;
}
var request = newWideRequest();
request.file = currentPath;
request.code = wide.curEditor.getValue();
request.nextCmd = "run";
$.ajax({
type: 'POST',
2014-12-11 10:32:24 +03:00
url: config.context + '/build',
2014-11-18 05:03:08 +03:00
data: JSON.stringify(request),
dataType: "json",
beforeSend: function (data) {
bottomGroup.resetOutput();
},
success: function (data) {
2014-12-13 17:58:43 +03:00
$("#buildRun").addClass("ico-stop")
2014-11-18 05:03:08 +03:00
.removeClass("ico-buildrun").attr("title", config.label.stop);
}
});
},
2014-11-19 04:39:06 +03:00
// Build.
2014-11-18 05:03:08 +03:00
build: function () {
menu.saveAllFiles();
var currentPath = editors.getCurrentPath();
if (!currentPath) {
return false;
}
if ($(".menu li.build").hasClass("disabled")) {
return false;
}
var request = newWideRequest();
request.file = currentPath;
request.code = wide.curEditor.getValue();
2014-12-25 05:41:19 +03:00
request.nextCmd = ""; // build only, no following operation
2014-11-18 05:03:08 +03:00
$.ajax({
type: 'POST',
2014-12-11 10:32:24 +03:00
url: config.context + '/build',
2014-11-18 05:03:08 +03:00
data: JSON.stringify(request),
dataType: "json",
beforeSend: function (data) {
bottomGroup.resetOutput();
},
success: function (data) {
}
});
},
_initPreference: function () {
2014-12-11 10:32:24 +03:00
$("#dialogPreference").load(config.context + '/preference', function () {
2014-11-18 05:03:08 +03:00
$("#dialogPreference input").keyup(function () {
2014-12-08 10:18:28 +03:00
var isChange = false,
emptys = [],
emptysTip = '';
2014-11-18 05:03:08 +03:00
$("#dialogPreference input").each(function () {
2014-12-08 10:18:28 +03:00
var $it = $(this);
// data-value 如为数字,则不会和 value 一样转换为 String再次不使用全等
if ($it.val() != $it.data("value")) {
2014-11-18 05:03:08 +03:00
isChange = true;
}
2014-12-08 10:18:28 +03:00
if ($.trim($it.val()) === '') {
emptys.push($it);
}
2014-11-18 05:03:08 +03:00
});
var $okBtn = $("#dialogPreference").closest(".dialog-main").find(".dialog-footer > button:eq(0)");
if (isChange) {
$okBtn.prop("disabled", false);
} else {
$okBtn.prop("disabled", true);
}
2014-12-08 10:18:28 +03:00
if (emptys.length === 0) {
$("#dialogPreference").find(".tip").html("");
2014-12-09 10:07:26 +03:00
$okBtn.prop("disabled", false);
2014-12-08 10:18:28 +03:00
} else {
for (var i = 0, max = emptys.length; i < max; i++) {
2014-12-09 10:07:26 +03:00
var tabIndex = emptys[i].closest('div').data("index"),
text = $.trim(emptys[i].parent().text());
emptysTip += '[' + $("#dialogPreference .tabs > div[data-index=" + tabIndex + "]").text()
+ '] -> [' + text.substr(0, text.length - 1)
2014-12-08 11:11:43 +03:00
+ ']: ' + config.label.no_empty + "<br/>";
2014-12-08 10:18:28 +03:00
}
$("#dialogPreference").find(".tip").html(emptysTip);
2014-12-09 10:07:26 +03:00
$okBtn.prop("disabled", true);
2014-12-08 10:18:28 +03:00
}
2014-11-18 05:03:08 +03:00
});
2014-11-26 06:24:24 +03:00
$("#dialogPreference select").on("change", function () {
var isChange = false;
2014-12-08 10:18:28 +03:00
$("#dialogPreference select").each(function () {
2014-11-26 06:24:24 +03:00
if ($(this).val() !== $(this).data("value")) {
isChange = true;
}
});
var $okBtn = $("#dialogPreference").closest(".dialog-main").find(".dialog-footer > button:eq(0)");
if (isChange) {
$okBtn.prop("disabled", false);
} else {
$okBtn.prop("disabled", true);
}
});
2014-11-18 05:03:08 +03:00
$("#dialogPreference").dialog({
"modal": true,
2014-12-01 11:54:22 +03:00
"height": 280,
2014-11-18 05:03:08 +03:00
"width": 800,
"title": config.label.perference,
"okText": config.label.apply,
"cancelText": config.label.cancel,
"afterOpen": function () {
var $okBtn = $("#dialogPreference").closest(".dialog-main").find(".dialog-footer > button:eq(0)");
$okBtn.prop("disabled", true);
},
"ok": function () {
var request = newWideRequest(),
$dialogPreference = $("#dialogPreference"),
$fontFamily = $dialogPreference.find("input[name=fontFamily]"),
$fontSize = $dialogPreference.find("input[name=fontSize]"),
2014-12-08 11:11:43 +03:00
$goFmt = $dialogPreference.find("select[name=goFmt]"),
2014-11-18 05:03:08 +03:00
$workspace = $dialogPreference.find("input[name=workspace]"),
$password = $dialogPreference.find("input[name=password]"),
2014-12-08 09:02:39 +03:00
$email = $dialogPreference.find("input[name=email]"),
2014-12-08 11:11:43 +03:00
$locale = $dialogPreference.find("select[name=locale]"),
$theme = $dialogPreference.find("select[name=theme]"),
2014-11-30 05:18:17 +03:00
$editorFontFamily = $dialogPreference.find("input[name=editorFontFamily]"),
$editorFontSize = $dialogPreference.find("input[name=editorFontSize]"),
$editorLineHeight = $dialogPreference.find("input[name=editorLineHeight]"),
2014-12-08 11:11:43 +03:00
$editorTheme = $dialogPreference.find("select[name=editorTheme]"),
2014-12-01 10:25:21 +03:00
$editorTabSize = $dialogPreference.find("input[name=editorTabSize]");
2014-11-18 05:03:08 +03:00
$.extend(request, {
"fontFamily": $fontFamily.val(),
"fontSize": $fontSize.val(),
"goFmt": $goFmt.val(),
"workspace": $workspace.val(),
"password": $password.val(),
2014-12-08 09:02:39 +03:00
"email": $email.val(),
2014-11-30 05:18:17 +03:00
"locale": $locale.val(),
"theme": $theme.val(),
"editorFontFamily": $editorFontFamily.val(),
"editorFontSize": $editorFontSize.val(),
"editorLineHeight": $editorLineHeight.val(),
2014-12-01 09:49:16 +03:00
"editorTheme": $editorTheme.val(),
"editorTabSize": $editorTabSize.val()
2014-11-18 05:03:08 +03:00
});
$.ajax({
type: 'POST',
2014-12-11 10:32:24 +03:00
url: config.context + '/preference',
2014-11-18 05:03:08 +03:00
data: JSON.stringify(request),
success: function (data, textStatus, jqXHR) {
if (!data.succ) {
return false;
}
$fontFamily.data("value", $fontFamily.val());
$fontSize.data("value", $fontSize.val());
$goFmt.data("value", $goFmt.val());
$workspace.data("value", $workspace.val());
$password.data("value", $password.val());
2014-12-08 09:02:39 +03:00
$email.data("value", $email.val());
2014-11-18 05:03:08 +03:00
$locale.data("value", $locale.val());
2014-11-30 05:18:17 +03:00
$theme.data("value", $theme.val());
$editorFontFamily.data("value", $editorFontFamily.val());
$editorFontSize.data("value", $editorFontSize.val());
$editorLineHeight.data("value", $editorLineHeight.val());
$editorTheme.data("value", $editorTheme.val());
2014-12-01 09:49:16 +03:00
$editorTabSize.data("value", $editorTabSize.val());
2014-11-18 05:03:08 +03:00
var $okBtn = $("#dialogPreference").closest(".dialog-main").find(".dialog-footer > button:eq(0)");
$okBtn.prop("disabled", true);
2014-12-01 10:19:59 +03:00
$("#themesLink").attr("href", config.staticServer + '/static/css/themes/' + $theme.val() + '.css');
2014-12-01 10:25:21 +03:00
config.editorTheme = $editorTheme.val();
for (var i = 0, ii = editors.data.length; i < ii; i++) {
editors.data[i].editor.setOption("theme", $editorTheme.val());
}
2014-11-18 05:03:08 +03:00
}
});
}
});
new Tabs({
id: ".preference"
});
});
2014-12-01 10:19:59 +03:00
}
2014-09-10 14:08:35 +04:00
};