UNPKG

6.94 kBJavaScriptView Raw
1
2!(function(window, $, undefined) {
3
4 var cheatsheetShown = false;
5
6 var $toolbar;
7
8 var Jingo = {
9
10 init: function() {
11 var navh = $(".navbar").height(),
12 $tools = $(".tools"),
13 qs, hl = null;
14
15 if (location.search !== "") {
16 $("input[name=term]").focus();
17 qs = $.map(location.search.substr(1).split("&"), function(kv) {
18 kv = kv.split("=");
19 return { k: kv[0], v: decodeURIComponent(kv[1]) };
20 });
21 $.each(qs, function(i, t) { if (t.k == "hl") { hl = t.v; } });
22 if (hl) {
23 if (window.find && window.getSelection) {
24 document.designMode = "on";
25 var sel = window.getSelection();
26 sel.collapse(document.body, 0);
27 while (window.find(hl)) {
28 document.execCommand("HiliteColor", false, "yellow");
29 sel.collapseToEnd();
30 }
31 sel.collapse(document.body, 0);
32 window.find(hl);
33 sel.collapseToEnd();
34 document.designMode = "off";
35 } else if (document.body.createTextRange) {
36 var textRange = document.body.createTextRange();
37 while (textRange.findText(hl)) {
38 textRange.execCommand("BackColor", false, "yellow");
39 textRange.collapse(false);
40 }
41 }
42 }
43 }
44
45 $("#login").attr("href", function() { return $(this).attr("href").replace("destination", "destination=" + encodeURIComponent(location.pathname)); });
46
47 $(".tools").height(navh);
48
49 if ($(".tools > ul > li").length > 0) {
50 var $pah = $("<li class=\"tools-handle\">Tools</li>");
51 var pahTo, bodyPadding = $('body').css('padding-top');
52 $pah.on("mouseover", function() {
53 $tools.animate({"margin-top": bodyPadding == "40px" ? "0" : "-20"});
54 $pah.slideUp();
55 });
56 $tools.on("mouseenter", function() {
57 clearTimeout(pahTo);
58 }).on("mouseleave", function() {
59 pahTo = setTimeout(function() {
60 $tools.animate({"margin-top": "-62"});
61 $pah.slideDown();
62 }, 500);
63 });
64 $(".tools > ul").append($pah);
65 } else {
66 }
67
68 $(".confirm-delete-page").on("click", function(evt) {
69 return window.confirm("Do you really want to delete this page?");
70 });
71
72 $(".confirm-revert").on("click", function(evt) {
73 return window.confirm("Do you really want to revert to this revision?");
74 });
75
76 var $hCol1 = $(".history td:first-child");
77
78 if ($("#content").hasClass("edit")) {
79 $("#editor").focus();
80 } else {
81 $("#pageTitle").focus();
82 }
83
84 $("#rev-compare").attr("disabled", true);
85
86 toggleCompareCheckboxes();
87 $hCol1.find("input").on("click", function() {
88 toggleCompareCheckboxes();
89 });
90
91 $("#rev-compare").on("click", function() {
92 if ($hCol1.find(":checked").length < 2) {
93 return false;
94 }
95 window.location.href = "/wiki/" + $(this).data("pagename") + "/compare/" + $hCol1.find(":checked").map(function() { return $(this).val(); }).toArray().reverse().join("..");
96 return false;
97 });
98
99 if (/^\/pages\/.*\/edit/.test(window.location.pathname) ||
100 /^\/pages\/new/.test(window.location.pathname)) {
101 $("#editor").closest("form").on("submit", function () {
102 if (Jingo.cmInstance) {
103 Jingo.cmInstance.save();
104 }
105 window.sessionStorage.setItem("jingo-page", $("#editor").val());
106 });
107 if (window.location.search == "?e=1") {
108 // Edit page in error: restore the body
109 var content;
110 if (content = window.sessionStorage.getItem("jingo-page")) {
111 $("#editor").val(content);
112 }
113 }
114 else {
115 window.sessionStorage.removeItem("jingo-page");
116 }
117 }
118
119 if (/^\/wiki\//.test(window.location.pathname)) {
120 var pages = []
121 , match
122 , href;
123
124 $("#content a.internal").each(function(i, a) {
125 href = $(a).attr("href");
126 if (match = /\/wiki\/(.+)/.exec(href)) {
127 pages.push(decodeURIComponent(match[1]));
128 }
129 });
130
131 $.getJSON("/misc/existence", {data: pages}, function(result) {
132 $.each(result.data, function(href, a) {
133 $("#content a[href='\\/wiki\\/" + encodeURIComponent(a) + "']").addClass("absent");
134 });
135 });
136 }
137
138 function toggleCompareCheckboxes() {
139
140 $("#rev-compare").attr("disabled", true);
141
142 if ($hCol1.find(":checkbox").length == 1) {
143 $hCol1.find(":checkbox").hide();
144 return;
145 }
146 if ($hCol1.find(":checked").length == 2) {
147 $("#rev-compare").attr("disabled", false);
148 $hCol1.find(":not(:checked)")
149 .hide();
150 $hCol1.parent("tr")
151 .css({"color": "silver"});
152 $hCol1.find(":checked")
153 .parents("tr")
154 .css({"color": "black"});
155 } else {
156 $hCol1.find("input")
157 .show()
158 .parents("tr")
159 .css({"color": "black"});
160 }
161 }
162
163 },
164
165 preview: function() {
166 $("#preview").modal({keyboard: true, show: true, backdrop: false});
167 $.post("/misc/preview", {data: $("#editor").val()}, function(data) {
168 $("#preview .modal-body").html(data).get(0).scrollTop = 0;
169 });
170 },
171
172 toggleFullscreen: function() {
173
174 var isFullscreen = Jingo.cmInstance.getOption("fullScreen");
175
176 Jingo.cmInstance.setOption("fullScreen", !Jingo.cmInstance.getOption("fullScreen"));
177 Jingo.cmInstance.focus();
178
179 $toolbar.toggleClass("fullscreen", !isFullscreen);
180 },
181
182 toolbar: function() {
183
184 $toolbar = $("<ul class='toolbar'>");
185 $toolbar.append("<li title=\"Toggle fullscreen (Ctrl/Cmd+Enter)\" class=\"fullscreen\"><span></span></li>\
186 <li title=\"Syntax help\" class=\"info\"><span></span></li>\
187 <li title=\"Preview\" class=\"preview\"><span></span></li></ul>").insertBefore($("form.edit textarea:first").closest("div"));
188
189 $("ul.toolbar").on("click", "span", function() {
190 if (this.parentNode.className == "info") {
191 Jingo.markdownSyntax();
192 }
193 if (this.parentNode.className == "preview") {
194 Jingo.cmInstance.save();
195 Jingo.preview();
196 }
197 if (this.parentNode.className == "fullscreen") {
198 Jingo.toggleFullscreen();
199 }
200 });
201 },
202
203 markdownSyntax: function() {
204 $("#syntax-reference").modal({keyboard: true, show: true, backdrop: false});
205 if (!cheatsheetShown) {
206 $("#syntax-reference .modal-body").load("/misc/syntax-reference");
207 cheatsheetShown = true;
208 }
209 }
210 };
211
212 window.Jingo = Jingo;
213
214})(this, jQuery);