You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gogs.js 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. 'use strict';
  2. var csrf;
  3. var suburl;
  4. function initCommentPreviewTab($form) {
  5. var $tab_menu = $form.find('.tabular.menu');
  6. $tab_menu.find('.item').tab();
  7. $tab_menu.find('.item[data-tab="' + $tab_menu.data('preview') + '"]').click(function () {
  8. var $this = $(this);
  9. $.post($this.data('url'), {
  10. "_csrf": csrf,
  11. "mode": "gfm",
  12. "context": $this.data('context'),
  13. "text": $form.find('.tab.segment[data-tab="' + $tab_menu.data('write') + '"] textarea').val()
  14. },
  15. function (data) {
  16. var $preview_tab = $form.find('.tab.segment[data-tab="' + $tab_menu.data('preview') + '"]');
  17. $preview_tab.html(data);
  18. emojify.run($preview_tab[0]);
  19. $('pre code', $preview_tab[0]).each(function (i, block) {
  20. hljs.highlightBlock(block);
  21. });
  22. }
  23. );
  24. });
  25. buttonsClickOnEnter();
  26. }
  27. var previewTab;
  28. var previewFileModes;
  29. function initEditPreviewTab($form) {
  30. var $tab_menu = $form.find('.tabular.menu');
  31. $tab_menu.find('.item').tab();
  32. previewTab = $tab_menu.find('.item[data-tab="' + $tab_menu.data('preview') + '"]');
  33. if (previewTab.length) {
  34. previewFileModes = previewTab.data('preview-file-modes').split(',');
  35. previewTab.click(function () {
  36. var $this = $(this);
  37. $.post($this.data('url'), {
  38. "_csrf": csrf,
  39. "mode": "gfm",
  40. "context": $this.data('context'),
  41. "text": $form.find('.tab.segment[data-tab="' + $tab_menu.data('write') + '"] textarea').val()
  42. },
  43. function (data) {
  44. var $preview_tab = $form.find('.tab.segment[data-tab="' + $tab_menu.data('preview') + '"]');
  45. $preview_tab.html(data);
  46. emojify.run($preview_tab[0]);
  47. $('pre code', $preview_tab[0]).each(function (i, block) {
  48. hljs.highlightBlock(block);
  49. });
  50. }
  51. );
  52. });
  53. }
  54. buttonsClickOnEnter();
  55. }
  56. function initEditDiffTab($form) {
  57. var $tab_menu = $form.find('.tabular.menu');
  58. $tab_menu.find('.item').tab();
  59. $tab_menu.find('.item[data-tab="' + $tab_menu.data('diff') + '"]').click(function () {
  60. var $this = $(this);
  61. $.post($this.data('url'), {
  62. "_csrf": csrf,
  63. "context": $this.data('context'),
  64. "content": $form.find('.tab.segment[data-tab="' + $tab_menu.data('write') + '"] textarea').val()
  65. },
  66. function (data) {
  67. var $diff_tab = $form.find('.tab.segment[data-tab="' + $tab_menu.data('diff') + '"]');
  68. $diff_tab.html(data);
  69. emojify.run($diff_tab[0]);
  70. initCodeView()
  71. }
  72. );
  73. });
  74. buttonsClickOnEnter();
  75. }
  76. function initCommentForm() {
  77. if ($('.comment.form').length == 0) {
  78. return
  79. }
  80. initCommentPreviewTab($('.comment.form'));
  81. // Labels
  82. var $list = $('.ui.labels.list');
  83. var $no_select = $list.find('.no-select');
  84. var $label_menu = $('.select-label .menu');
  85. var has_label_update_action = $label_menu.data('action') == 'update';
  86. function updateIssueMeta(url, action, id) {
  87. $.post(url, {
  88. "_csrf": csrf,
  89. "action": action,
  90. "id": id
  91. });
  92. }
  93. $label_menu.find('.item:not(.no-select)').click(function () {
  94. if ($(this).hasClass('checked')) {
  95. $(this).removeClass('checked');
  96. $(this).find('.octicon').removeClass('octicon-check');
  97. if (has_label_update_action) {
  98. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  99. }
  100. } else {
  101. $(this).addClass('checked');
  102. $(this).find('.octicon').addClass('octicon-check');
  103. if (has_label_update_action) {
  104. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  105. }
  106. }
  107. var label_ids = "";
  108. $(this).parent().find('.item').each(function () {
  109. if ($(this).hasClass('checked')) {
  110. label_ids += $(this).data('id') + ",";
  111. $($(this).data('id-selector')).removeClass('hide');
  112. } else {
  113. $($(this).data('id-selector')).addClass('hide');
  114. }
  115. });
  116. if (label_ids.length == 0) {
  117. $no_select.removeClass('hide');
  118. } else {
  119. $no_select.addClass('hide');
  120. }
  121. $($(this).parent().data('id')).val(label_ids);
  122. return false;
  123. });
  124. $label_menu.find('.no-select.item').click(function () {
  125. if (has_label_update_action) {
  126. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  127. }
  128. $(this).parent().find('.item').each(function () {
  129. $(this).removeClass('checked');
  130. $(this).find('.octicon').removeClass('octicon-check');
  131. });
  132. $list.find('.item').each(function () {
  133. $(this).addClass('hide');
  134. });
  135. $no_select.removeClass('hide');
  136. $($(this).parent().data('id')).val('');
  137. });
  138. function selectItem(select_id, input_id) {
  139. var $menu = $(select_id + ' .menu');
  140. var $list = $('.ui' + select_id + '.list');
  141. var has_update_action = $menu.data('action') == 'update';
  142. $menu.find('.item:not(.no-select)').click(function () {
  143. $(this).parent().find('.item').each(function () {
  144. $(this).removeClass('selected active')
  145. });
  146. $(this).addClass('selected active');
  147. if (has_update_action) {
  148. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  149. }
  150. switch (input_id) {
  151. case '#milestone_id':
  152. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  153. $(this).text() + '</a>');
  154. break;
  155. case '#assignee_id':
  156. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  157. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  158. $(this).text() + '</a>');
  159. }
  160. $('.ui' + select_id + '.list .no-select').addClass('hide');
  161. $(input_id).val($(this).data('id'));
  162. });
  163. $menu.find('.no-select.item').click(function () {
  164. $(this).parent().find('.item:not(.no-select)').each(function () {
  165. $(this).removeClass('selected active')
  166. });
  167. if (has_update_action) {
  168. updateIssueMeta($menu.data('update-url'), '', '');
  169. }
  170. $list.find('.selected').html('');
  171. $list.find('.no-select').removeClass('hide');
  172. $(input_id).val('');
  173. });
  174. }
  175. // Milestone and assignee
  176. selectItem('.select-milestone', '#milestone_id');
  177. selectItem('.select-assignee', '#assignee_id');
  178. }
  179. function initEditForm() {
  180. initEditPreviewTab($('.edit.form'));
  181. initEditDiffTab($('.edit.form'));
  182. }
  183. function initInstall() {
  184. if ($('.install').length == 0) {
  185. return;
  186. }
  187. // Database type change detection.
  188. $("#db_type").change(function () {
  189. var sqlite_default = 'data/gogs.db';
  190. var tidb_default = 'data/gogs_tidb';
  191. var db_type = $(this).val();
  192. if (db_type === "SQLite3" || db_type === "TiDB") {
  193. $('#sql_settings').hide();
  194. $('#pgsql_settings').hide();
  195. $('#sqlite_settings').show();
  196. if (db_type === "SQLite3" && $('#db_path').val() == tidb_default) {
  197. $('#db_path').val(sqlite_default);
  198. } else if (db_type === "TiDB" && $('#db_path').val() == sqlite_default) {
  199. $('#db_path').val(tidb_default);
  200. }
  201. return;
  202. }
  203. var mysql_default = '127.0.0.1:3306';
  204. var postgres_default = '127.0.0.1:5432';
  205. $('#sqlite_settings').hide();
  206. $('#sql_settings').show();
  207. if (db_type === "PostgreSQL") {
  208. $('#pgsql_settings').show();
  209. if ($('#db_host').val() == mysql_default) {
  210. $('#db_host').val(postgres_default);
  211. }
  212. } else {
  213. $('#pgsql_settings').hide();
  214. if ($('#db_host').val() == postgres_default) {
  215. $('#db_host').val(mysql_default);
  216. }
  217. }
  218. });
  219. // TODO: better handling of exclusive relations.
  220. $('#offline-mode input').change(function () {
  221. if ($(this).is(':checked')) {
  222. $('#disable-gravatar').checkbox('check');
  223. $('#federated-avatar-lookup').checkbox('uncheck');
  224. }
  225. });
  226. $('#disable-gravatar input').change(function () {
  227. if ($(this).is(':checked')) {
  228. $('#federated-avatar-lookup').checkbox('uncheck');
  229. } else {
  230. $('#offline-mode').checkbox('uncheck');
  231. }
  232. });
  233. $('#federated-avatar-lookup input').change(function () {
  234. if ($(this).is(':checked')) {
  235. $('#disable-gravatar').checkbox('uncheck');
  236. $('#offline-mode').checkbox('uncheck');
  237. }
  238. });
  239. $('#disable-registration input').change(function () {
  240. if ($(this).is(':checked')) {
  241. $('#enable-captcha').checkbox('uncheck');
  242. }
  243. });
  244. $('#enable-captcha input').change(function () {
  245. if ($(this).is(':checked')) {
  246. $('#disable-registration').checkbox('uncheck');
  247. }
  248. });
  249. }
  250. function initRepository() {
  251. if ($('.repository').length == 0) {
  252. return;
  253. }
  254. function initFilterSearchDropdown(selector) {
  255. var $dropdown = $(selector);
  256. $dropdown.dropdown({
  257. fullTextSearch: true,
  258. onChange: function (text, value, $choice) {
  259. window.location.href = $choice.data('url');
  260. console.log($choice.data('url'))
  261. },
  262. message: {noResults: $dropdown.data('no-results')}
  263. });
  264. }
  265. // File list and commits
  266. if ($('.repository.file.list').length > 0 ||
  267. ('.repository.commits').length > 0) {
  268. initFilterSearchDropdown('.choose.reference .dropdown');
  269. $('.reference.column').click(function () {
  270. $('.choose.reference .scrolling.menu').css('display', 'none');
  271. $('.choose.reference .text').removeClass('black');
  272. $($(this).data('target')).css('display', 'block');
  273. $(this).find('.text').addClass('black');
  274. return false;
  275. });
  276. }
  277. // Wiki
  278. if ($('.repository.wiki.view').length > 0) {
  279. initFilterSearchDropdown('.choose.page .dropdown');
  280. }
  281. // Options
  282. if ($('.repository.settings.options').length > 0) {
  283. $('#repo_name').keyup(function () {
  284. var $prompt_span = $('#repo-name-change-prompt');
  285. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  286. $prompt_span.show();
  287. } else {
  288. $prompt_span.hide();
  289. }
  290. });
  291. // Enable or select internal/external wiki system and issue tracker.
  292. $('.enable-system').change(function () {
  293. if (this.checked) {
  294. $($(this).data('target')).removeClass('disabled');
  295. } else {
  296. $($(this).data('target')).addClass('disabled');
  297. }
  298. });
  299. $('.enable-system-radio').change(function () {
  300. if (this.value == 'false') {
  301. $($(this).data('target')).addClass('disabled');
  302. } else if (this.value == 'true') {
  303. $($(this).data('target')).removeClass('disabled');
  304. }
  305. });
  306. }
  307. // Labels
  308. if ($('.repository.labels').length > 0) {
  309. // Create label
  310. var $new_label_panel = $('.new-label.segment');
  311. $('.new-label.button').click(function () {
  312. $new_label_panel.show();
  313. });
  314. $('.new-label.segment .cancel').click(function () {
  315. $new_label_panel.hide();
  316. });
  317. $('.color-picker').each(function () {
  318. $(this).minicolors();
  319. });
  320. $('.precolors .color').click(function () {
  321. var color_hex = $(this).data('color-hex');
  322. $('.color-picker').val(color_hex);
  323. $('.minicolors-swatch-color').css("background-color", color_hex);
  324. });
  325. $('.edit-label-button').click(function () {
  326. $('#label-modal-id').val($(this).data('id'));
  327. $('.edit-label .new-label-input').val($(this).data('title'));
  328. $('.edit-label .color-picker').val($(this).data('color'));
  329. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  330. $('.edit-label.modal').modal({
  331. onApprove: function () {
  332. $('.edit-label.form').submit();
  333. }
  334. }).modal('show');
  335. return false;
  336. });
  337. }
  338. // Milestones
  339. if ($('.repository.milestones').length > 0) {
  340. }
  341. if ($('.repository.new.milestone').length > 0) {
  342. var $datepicker = $('.milestone.datepicker');
  343. $datepicker.datetimepicker({
  344. lang: $datepicker.data('lang'),
  345. inline: true,
  346. timepicker: false,
  347. startDate: $datepicker.data('start-date'),
  348. formatDate: 'Y-m-d',
  349. onSelectDate: function (ct) {
  350. $('#deadline').val(ct.dateFormat('Y-m-d'));
  351. }
  352. });
  353. $('#clear-date').click(function () {
  354. $('#deadline').val('');
  355. return false;
  356. });
  357. }
  358. // Issues
  359. if ($('.repository.view.issue').length > 0) {
  360. // Edit issue title
  361. var $issue_title = $('#issue-title');
  362. var $edit_input = $('#edit-title-input input');
  363. var editTitleToggle = function () {
  364. $issue_title.toggle();
  365. $('.not-in-edit').toggle();
  366. $('#edit-title-input').toggle();
  367. $('.in-edit').toggle();
  368. $edit_input.focus();
  369. return false;
  370. };
  371. $('#edit-title').click(editTitleToggle);
  372. $('#cancel-edit-title').click(editTitleToggle);
  373. $('#save-edit-title').click(editTitleToggle).click(function () {
  374. if ($edit_input.val().length == 0 ||
  375. $edit_input.val() == $issue_title.text()) {
  376. $edit_input.val($issue_title.text());
  377. return false;
  378. }
  379. $.post($(this).data('update-url'), {
  380. "_csrf": csrf,
  381. "title": $edit_input.val()
  382. },
  383. function (data) {
  384. $edit_input.val(data.title);
  385. $issue_title.text(data.title);
  386. });
  387. return false;
  388. });
  389. // Edit issue or comment content
  390. $('.edit-content').click(function () {
  391. var $segment = $(this).parent().parent().parent().next();
  392. var $edit_content_zone = $segment.find('.edit-content-zone');
  393. var $render_content = $segment.find('.render-content');
  394. var $raw_content = $segment.find('.raw-content');
  395. var $textarea;
  396. // Setup new form
  397. if ($edit_content_zone.html().length == 0) {
  398. $edit_content_zone.html($('#edit-content-form').html());
  399. $textarea = $segment.find('textarea');
  400. // Give new write/preview data-tab name to distinguish from others
  401. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  402. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  403. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  404. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  405. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  406. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  407. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  408. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  409. initCommentPreviewTab($edit_content_form);
  410. $edit_content_zone.find('.cancel.button').click(function () {
  411. $render_content.show();
  412. $edit_content_zone.hide();
  413. });
  414. $edit_content_zone.find('.save.button').click(function () {
  415. $render_content.show();
  416. $edit_content_zone.hide();
  417. $.post($edit_content_zone.data('update-url'), {
  418. "_csrf": csrf,
  419. "content": $textarea.val(),
  420. "context": $edit_content_zone.data('context')
  421. },
  422. function (data) {
  423. if (data.length == 0) {
  424. $render_content.html($('#no-content').html());
  425. } else {
  426. $render_content.html(data.content);
  427. emojify.run($render_content[0]);
  428. $('pre code', $render_content[0]).each(function (i, block) {
  429. hljs.highlightBlock(block);
  430. });
  431. }
  432. });
  433. });
  434. } else {
  435. $textarea = $segment.find('textarea');
  436. }
  437. // Show write/preview tab and copy raw content as needed
  438. $edit_content_zone.show();
  439. $render_content.hide();
  440. if ($textarea.val().length == 0) {
  441. $textarea.val($raw_content.text());
  442. }
  443. $textarea.focus();
  444. return false;
  445. });
  446. // Delete comment
  447. $('.delete-comment').click(function () {
  448. var $this = $(this);
  449. if (confirm($this.data('locale'))) {
  450. $.post($this.data('url'), {
  451. "_csrf": csrf
  452. }).success(function () {
  453. $('#' + $this.data('comment-id')).remove();
  454. });
  455. }
  456. return false;
  457. });
  458. // Change status
  459. var $status_btn = $('#status-button');
  460. $('#edit_area').keyup(function () {
  461. if ($(this).val().length == 0) {
  462. $status_btn.text($status_btn.data('status'))
  463. } else {
  464. $status_btn.text($status_btn.data('status-and-comment'))
  465. }
  466. });
  467. $status_btn.click(function () {
  468. $('#status').val($status_btn.data('status-val'));
  469. $('#comment-form').submit();
  470. });
  471. }
  472. // Diff
  473. if ($('.repository.diff').length > 0) {
  474. var $counter = $('.diff-counter');
  475. if ($counter.length >= 1) {
  476. $counter.each(function (i, item) {
  477. var $item = $(item);
  478. var addLine = $item.find('span[data-line].add').data("line");
  479. var delLine = $item.find('span[data-line].del').data("line");
  480. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  481. $item.find(".bar .add").css("width", addPercent + "%");
  482. });
  483. }
  484. }
  485. // Quick start and repository home
  486. $('#repo-clone-ssh').click(function () {
  487. $('.clone-url').text($(this).data('link'));
  488. $('#repo-clone-url').val($(this).data('link'));
  489. $(this).addClass('blue');
  490. $('#repo-clone-https').removeClass('blue');
  491. localStorage.setItem('repo-clone-protocol', 'ssh');
  492. });
  493. $('#repo-clone-https').click(function () {
  494. $('.clone-url').text($(this).data('link'));
  495. $('#repo-clone-url').val($(this).data('link'));
  496. $(this).addClass('blue');
  497. $('#repo-clone-ssh').removeClass('blue');
  498. localStorage.setItem('repo-clone-protocol', 'https');
  499. });
  500. $('#repo-clone-url').click(function () {
  501. $(this).select();
  502. });
  503. // Pull request
  504. if ($('.repository.compare.pull').length > 0) {
  505. initFilterSearchDropdown('.choose.branch .dropdown');
  506. }
  507. }
  508. function initRepositoryCollaboration() {
  509. console.log('initRepositoryCollaboration');
  510. // Change collaborator access mode
  511. $('.access-mode.menu .item').click(function () {
  512. var $menu = $(this).parent();
  513. $.post($menu.data('url'), {
  514. "_csrf": csrf,
  515. "uid": $menu.data('uid'),
  516. "mode": $(this).data('value')
  517. })
  518. });
  519. }
  520. function initWikiForm() {
  521. var $edit_area = $('.repository.wiki textarea#edit_area');
  522. if ($edit_area.length > 0) {
  523. new SimpleMDE({
  524. autoDownloadFontAwesome: false,
  525. element: $edit_area[0],
  526. forceSync: true,
  527. previewRender: function (plainText, preview) { // Async method
  528. setTimeout(function () {
  529. // FIXME: still send render request when return back to edit mode
  530. $.post($edit_area.data('url'), {
  531. "_csrf": csrf,
  532. "mode": "gfm",
  533. "context": $edit_area.data('context'),
  534. "text": plainText
  535. },
  536. function (data) {
  537. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  538. emojify.run($('.editor-preview')[0]);
  539. }
  540. );
  541. }, 0);
  542. return "Loading...";
  543. },
  544. renderingConfig: {
  545. singleLineBreaks: false
  546. },
  547. indentWithTabs: false,
  548. tabSize: 4,
  549. spellChecker: false,
  550. toolbar: ["bold", "italic", "strikethrough", "|",
  551. "heading-1", "heading-2", "heading-3", "heading-bigger", "heading-smaller", "|",
  552. "code", "quote", "|",
  553. "unordered-list", "ordered-list", "|",
  554. "link", "image", "table", "horizontal-rule", "|",
  555. "clean-block", "preview", "fullscreen", "side-by-side"]
  556. })
  557. }
  558. }
  559. function initIssueForm() {
  560. var $edit_area = $('.repository.issue textarea.edit_area');
  561. if ($edit_area.length > 0) {
  562. $edit_area.each(function (i, edit_area) {
  563. new SimpleMDE({
  564. autoDownloadFontAwesome: false,
  565. element: edit_area[0],
  566. forceSync: true,
  567. previewRender: function (plainText, preview) { // Async method
  568. setTimeout(function () {
  569. // FIXME: still send render request when return back to edit mode
  570. $.post($edit_area.data('url'), {
  571. "_csrf": csrf,
  572. "mode": "gfm",
  573. "context": $edit_area.data('context'),
  574. "text": plainText
  575. },
  576. function (data) {
  577. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  578. emojify.run($('.editor-preview')[0]);
  579. }
  580. );
  581. }, 0);
  582. return "Loading...";
  583. },
  584. renderingConfig: {
  585. singleLineBreaks: false
  586. },
  587. indentWithTabs: false,
  588. tabSize: 4,
  589. spellChecker: false,
  590. toolbar: ["bold", "italic", "strikethrough", "|",
  591. "code", "quote", "|",
  592. "unordered-list", "ordered-list", "|",
  593. "link", "image", "table"]
  594. })
  595. });
  596. }
  597. }
  598. var editArea;
  599. var editFilename;
  600. var smdEditor;
  601. var cmEditor;
  602. var markdownFileExts;
  603. var lineWrapExtensions;
  604. // For IE
  605. String.prototype.endsWith = function (pattern) {
  606. var d = this.length - pattern.length;
  607. return d >= 0 && this.lastIndexOf(pattern) === d;
  608. };
  609. // Adding function to get the cursor position in a text field to jquery objects
  610. (function ($, undefined) {
  611. $.fn.getCursorPosition = function () {
  612. var el = $(this).get(0);
  613. var pos = 0;
  614. if ('selectionStart' in el) {
  615. pos = el.selectionStart;
  616. } else if ('selection' in document) {
  617. el.focus();
  618. var Sel = document.selection.createRange();
  619. var SelLength = document.selection.createRange().text.length;
  620. Sel.moveStart('character', -el.value.length);
  621. pos = Sel.text.length - SelLength;
  622. }
  623. return pos;
  624. }
  625. })(jQuery);
  626. function initEditor() {
  627. editFilename = $("#file-name");
  628. editFilename.keyup(function (e) {
  629. var sections = $('.breadcrumb span.section');
  630. var dividers = $('.breadcrumb div.divider');
  631. if (e.keyCode == 8) {
  632. if ($(this).getCursorPosition() == 0) {
  633. if (sections.length > 0) {
  634. var value = sections.last().find('a').text();
  635. $(this).val(value + $(this).val());
  636. $(this)[0].setSelectionRange(value.length, value.length);
  637. sections.last().remove();
  638. dividers.last().remove();
  639. }
  640. }
  641. }
  642. if (e.keyCode == 191) {
  643. var parts = $(this).val().split('/');
  644. for (var i = 0; i < parts.length; ++i) {
  645. var value = parts[i];
  646. if (i < parts.length - 1) {
  647. if (value.length) {
  648. $('<span class="section"><a href="#">' + value + '</a></span>').insertBefore($(this));
  649. $('<div class="divider"> / </div>').insertBefore($(this));
  650. }
  651. }
  652. else {
  653. $(this).val(value);
  654. }
  655. $(this)[0].setSelectionRange(0, 0);
  656. }
  657. }
  658. var parts = [];
  659. $('.breadcrumb span.section').each(function (i, element) {
  660. element = $(element);
  661. if (element.find('a').length) {
  662. parts.push(element.find('a').text());
  663. } else {
  664. parts.push(element.text());
  665. }
  666. });
  667. if ($(this).val())
  668. parts.push($(this).val());
  669. $('#tree-name').val(parts.join('/'));
  670. }).trigger('keyup');
  671. editArea = $('.repository.editor textarea#edit_area');
  672. if (!editArea.length)
  673. return;
  674. markdownFileExts = editArea.data("markdown-file-exts").split(",");
  675. lineWrapExtensions = editArea.data("line-wrap-extensions").split(",");
  676. editFilename.on("keyup", function (e) {
  677. var val = editFilename.val(), m, mode, spec, extension, extWithDot, previewLink, dataUrl, apiCall;
  678. extension = extWithDot = "";
  679. if (m = /.+\.([^.]+)$/.exec(val)) {
  680. extension = m[1];
  681. extWithDot = "." + extension;
  682. }
  683. var info = CodeMirror.findModeByExtension(extension);
  684. previewLink = $('a[data-tab=preview]');
  685. if (info) {
  686. mode = info.mode;
  687. spec = info.mime;
  688. apiCall = mode;
  689. }
  690. else {
  691. apiCall = extension
  692. }
  693. if (previewLink.length && apiCall && previewFileModes && previewFileModes.length && previewFileModes.indexOf(apiCall) >= 0) {
  694. dataUrl = previewLink.data('url');
  695. previewLink.data('url', dataUrl.replace(/(.*)\/.*/i, '$1/' + mode));
  696. previewLink.show();
  697. }
  698. else {
  699. previewLink.hide();
  700. }
  701. // If this file is a Markdown extensions, we will load that editor and return
  702. if (markdownFileExts.indexOf(extWithDot) >= 0) {
  703. if (setSimpleMDE()) {
  704. return;
  705. }
  706. }
  707. // Else we are going to use CodeMirror
  708. if (!cmEditor && !setCodeMirror()) {
  709. return;
  710. }
  711. if (mode) {
  712. cmEditor.setOption("mode", spec);
  713. CodeMirror.autoLoadMode(cmEditor, mode);
  714. }
  715. if (lineWrapExtensions.indexOf(extWithDot) >= 0) {
  716. cmEditor.setOption("lineWrapping", true);
  717. }
  718. else {
  719. cmEditor.setOption("lineWrapping", false);
  720. }
  721. }).trigger('keyup');
  722. }
  723. function setSimpleMDE() {
  724. if (cmEditor) {
  725. cmEditor.toTextArea();
  726. cmEditor = null;
  727. }
  728. if (smdEditor) {
  729. return true;
  730. }
  731. smdEditor = new SimpleMDE({
  732. autoDownloadFontAwesome: false,
  733. element: editArea[0],
  734. forceSync: true,
  735. renderingConfig: {
  736. singleLineBreaks: false
  737. },
  738. indentWithTabs: false,
  739. tabSize: 4,
  740. spellChecker: false,
  741. previewRender: function (plainText, preview) { // Async method
  742. setTimeout(function () {
  743. // FIXME: still send render request when return back to edit mode
  744. $.post(editArea.data('url'), {
  745. "_csrf": csrf,
  746. "mode": "gfm",
  747. "context": editArea.data('context'),
  748. "text": plainText
  749. },
  750. function (data) {
  751. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  752. emojify.run($('.editor-preview')[0]);
  753. }
  754. );
  755. }, 0);
  756. return "Loading...";
  757. },
  758. toolbar: ["bold", "italic", "strikethrough", "|",
  759. "heading-1", "heading-2", "heading-3", "heading-bigger", "heading-smaller", "|",
  760. "code", "quote", "|",
  761. "unordered-list", "ordered-list", "|",
  762. "link", "image", "table", "horizontal-rule", "|",
  763. "clean-block", "preview", "fullscreen", "side-by-side"]
  764. });
  765. return true;
  766. }
  767. function setCodeMirror() {
  768. if (smdEditor) {
  769. smdEditor.toTextArea();
  770. smdEditor = null;
  771. }
  772. if (cmEditor) {
  773. return true;
  774. }
  775. cmEditor = CodeMirror.fromTextArea(editArea[0], {
  776. lineNumbers: true
  777. });
  778. cmEditor.on("change", function (cm, change) {
  779. editArea.val(cm.getValue());
  780. });
  781. return true;
  782. }
  783. function initQuickPull() {
  784. $('.js-quick-pull-choice-option').change(function () {
  785. quickPullChoiceChange();
  786. });
  787. quickPullChoiceChange();
  788. }
  789. function quickPullChoiceChange() {
  790. var radio = $('.js-quick-pull-choice-option:checked');
  791. if (radio.val() == 'commit-to-new-branch')
  792. $('.quick-pull-branch-name').show();
  793. else
  794. $('.quick-pull-branch-name').hide();
  795. }
  796. function initOrganization() {
  797. if ($('.organization').length == 0) {
  798. return;
  799. }
  800. // Options
  801. if ($('.organization.settings.options').length > 0) {
  802. $('#org_name').keyup(function () {
  803. var $prompt_span = $('#org-name-change-prompt');
  804. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  805. $prompt_span.show();
  806. } else {
  807. $prompt_span.hide();
  808. }
  809. });
  810. }
  811. }
  812. function initUserSettings() {
  813. console.log('initUserSettings');
  814. // Options
  815. if ($('.user.settings.profile').length > 0) {
  816. $('#username').keyup(function () {
  817. var $prompt_span = $('#name-change-prompt');
  818. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  819. $prompt_span.show();
  820. } else {
  821. $prompt_span.hide();
  822. }
  823. });
  824. }
  825. }
  826. function initWebhook() {
  827. if ($('.new.webhook').length == 0) {
  828. return;
  829. }
  830. $('.events.checkbox input').change(function () {
  831. if ($(this).is(':checked')) {
  832. $('.events.fields').show();
  833. }
  834. });
  835. $('.non-events.checkbox input').change(function () {
  836. if ($(this).is(':checked')) {
  837. $('.events.fields').hide();
  838. }
  839. });
  840. // Test delivery
  841. $('#test-delivery').click(function () {
  842. var $this = $(this);
  843. $this.addClass('loading disabled');
  844. $.post($this.data('link'), {
  845. "_csrf": csrf
  846. }).done(
  847. setTimeout(function () {
  848. window.location.href = $this.data('redirect');
  849. }, 5000)
  850. )
  851. });
  852. }
  853. function initAdmin() {
  854. if ($('.admin').length == 0) {
  855. return;
  856. }
  857. // New user
  858. if ($('.admin.new.user').length > 0 ||
  859. $('.admin.edit.user').length > 0) {
  860. $('#login_type').change(function () {
  861. if ($(this).val().substring(0, 1) == '0') {
  862. $('#login_name').removeAttr('required');
  863. $('.non-local').hide();
  864. $('.local').show();
  865. $('#user_name').focus();
  866. if ($(this).data('password') == "required") {
  867. $('#password').attr('required', 'required');
  868. }
  869. } else {
  870. $('#login_name').attr('required', 'required');
  871. $('.non-local').show();
  872. $('.local').hide();
  873. $('#login_name').focus();
  874. $('#password').removeAttr('required');
  875. }
  876. });
  877. }
  878. function onSecurityProtocolChange() {
  879. if ($('#security_protocol').val() > 0) {
  880. $('.has-tls').show();
  881. } else {
  882. $('.has-tls').hide();
  883. }
  884. }
  885. // New authentication
  886. if ($('.admin.new.authentication').length > 0) {
  887. $('#auth_type').change(function () {
  888. $('.ldap').hide();
  889. $('.dldap').hide();
  890. $('.smtp').hide();
  891. $('.pam').hide();
  892. $('.has-tls').hide();
  893. var auth_type = $(this).val();
  894. switch (auth_type) {
  895. case '2': // LDAP
  896. $('.ldap').show();
  897. break;
  898. case '3': // SMTP
  899. $('.smtp').show();
  900. $('.has-tls').show();
  901. break;
  902. case '4': // PAM
  903. $('.pam').show();
  904. break;
  905. case '5': // LDAP
  906. $('.dldap').show();
  907. break;
  908. }
  909. if (auth_type == '2' || auth_type == '5') {
  910. onSecurityProtocolChange()
  911. }
  912. });
  913. $('#security_protocol').change(onSecurityProtocolChange)
  914. }
  915. // Edit authentication
  916. if ($('.admin.edit.authentication').length > 0) {
  917. var auth_type = $('#auth_type').val();
  918. if (auth_type == '2' || auth_type == '5') {
  919. $('#security_protocol').change(onSecurityProtocolChange);
  920. }
  921. }
  922. // Notice
  923. if ($('.admin.notice')) {
  924. var $detail_modal = $('#detail-modal');
  925. // Attach view detail modals
  926. $('.view-detail').click(function () {
  927. $detail_modal.find('.content p').text($(this).data('content'));
  928. $detail_modal.modal('show');
  929. return false;
  930. });
  931. // Select actions
  932. var $checkboxes = $('.select.table .ui.checkbox');
  933. $('.select.action').click(function () {
  934. switch ($(this).data('action')) {
  935. case 'select-all':
  936. $checkboxes.checkbox('check');
  937. break;
  938. case 'deselect-all':
  939. $checkboxes.checkbox('uncheck');
  940. break;
  941. case 'inverse':
  942. $checkboxes.checkbox('toggle');
  943. break;
  944. }
  945. });
  946. $('#delete-selection').click(function () {
  947. var $this = $(this);
  948. $this.addClass("loading disabled");
  949. var ids = [];
  950. $checkboxes.each(function () {
  951. if ($(this).checkbox('is checked')) {
  952. ids.push($(this).data('id'));
  953. }
  954. });
  955. $.post($this.data('link'), {
  956. "_csrf": csrf,
  957. "ids": ids
  958. }).done(function () {
  959. window.location.href = $this.data('redirect');
  960. });
  961. });
  962. }
  963. }
  964. function buttonsClickOnEnter() {
  965. $('.ui.button').keypress(function (e) {
  966. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  967. $(this).click();
  968. });
  969. }
  970. function hideWhenLostFocus(body, parent) {
  971. $(document).click(function (e) {
  972. var target = e.target;
  973. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  974. $(body).hide();
  975. }
  976. });
  977. }
  978. function searchUsers() {
  979. if (!$('#search-user-box .results').length) {
  980. return;
  981. }
  982. var $search_user_box = $('#search-user-box');
  983. var $result_list = $search_user_box.find('.results');
  984. $search_user_box.keyup(function () {
  985. var $this = $(this);
  986. var keyword = $this.find('input').val();
  987. if (keyword.length < 2) {
  988. $result_list.hide();
  989. return;
  990. }
  991. $.ajax({
  992. url: suburl + '/api/v1/users/search?q=' + keyword,
  993. dataType: "json",
  994. success: function (response) {
  995. var notEmpty = function (str) {
  996. return str && str.length > 0;
  997. };
  998. $result_list.html('');
  999. if (response.ok && response.data.length) {
  1000. var html = '';
  1001. $.each(response.data, function (i, item) {
  1002. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  1003. if (notEmpty(item.full_name)) {
  1004. html += ' (' + item.full_name + ')';
  1005. }
  1006. html += '</div>';
  1007. });
  1008. $result_list.html(html);
  1009. $this.find('.results .item').click(function () {
  1010. $this.find('input').val($(this).find('.username').text());
  1011. $result_list.hide();
  1012. });
  1013. $result_list.show();
  1014. } else {
  1015. $result_list.hide();
  1016. }
  1017. }
  1018. });
  1019. });
  1020. $search_user_box.find('input').focus(function () {
  1021. $search_user_box.keyup();
  1022. });
  1023. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  1024. }
  1025. // FIXME: merge common parts in two functions
  1026. function searchRepositories() {
  1027. if (!$('#search-repo-box .results').length) {
  1028. return;
  1029. }
  1030. var $search_repo_box = $('#search-repo-box');
  1031. var $result_list = $search_repo_box.find('.results');
  1032. $search_repo_box.keyup(function () {
  1033. var $this = $(this);
  1034. var keyword = $this.find('input').val();
  1035. if (keyword.length < 2) {
  1036. $result_list.hide();
  1037. return;
  1038. }
  1039. $.ajax({
  1040. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $search_repo_box.data('uid'),
  1041. dataType: "json",
  1042. success: function (response) {
  1043. var notEmpty = function (str) {
  1044. return str && str.length > 0;
  1045. };
  1046. $result_list.html('');
  1047. if (response.ok && response.data.length) {
  1048. var html = '';
  1049. $.each(response.data, function (i, item) {
  1050. html += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  1051. });
  1052. $result_list.html(html);
  1053. $this.find('.results .item').click(function () {
  1054. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  1055. $result_list.hide();
  1056. });
  1057. $result_list.show();
  1058. } else {
  1059. $result_list.hide();
  1060. }
  1061. }
  1062. });
  1063. });
  1064. $search_repo_box.find('input').focus(function () {
  1065. $search_repo_box.keyup();
  1066. });
  1067. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  1068. }
  1069. function initCodeView() {
  1070. if ($('.code-view .linenums').length > 0) {
  1071. $(document).on('click', '.lines-num span', function (e) {
  1072. var $select = $(this);
  1073. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  1074. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  1075. deSelect();
  1076. });
  1077. $(window).on('hashchange', function (e) {
  1078. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1079. var $list = $('.code-view ol.linenums > li');
  1080. var $first;
  1081. if (m) {
  1082. $first = $list.filter('.' + m[1]);
  1083. selectRange($list, $first, $list.filter('.' + m[2]));
  1084. $("html, body").scrollTop($first.offset().top - 200);
  1085. return;
  1086. }
  1087. m = window.location.hash.match(/^#(L\d+)$/);
  1088. if (m) {
  1089. $first = $list.filter('.' + m[1]);
  1090. selectRange($list, $first);
  1091. $("html, body").scrollTop($first.offset().top - 200);
  1092. }
  1093. }).trigger('hashchange');
  1094. }
  1095. }
  1096. var $dropz;
  1097. $(document).ready(function () {
  1098. csrf = $('meta[name=_csrf]').attr("content");
  1099. suburl = $('meta[name=_suburl]').attr("content");
  1100. // Show exact time
  1101. $('.time-since').each(function () {
  1102. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  1103. });
  1104. // Semantic UI modules.
  1105. $('.dropdown').dropdown();
  1106. $('.jump.dropdown').dropdown({
  1107. action: 'hide',
  1108. onShow: function () {
  1109. $('.poping.up').popup('hide');
  1110. }
  1111. });
  1112. $('.slide.up.dropdown').dropdown({
  1113. transition: 'slide up'
  1114. });
  1115. $('.upward.dropdown').dropdown({
  1116. direction: 'upward'
  1117. });
  1118. $('.ui.accordion').accordion();
  1119. $('.ui.checkbox').checkbox();
  1120. $('.ui.progress').progress({
  1121. showActivity: false
  1122. });
  1123. $('.poping.up').popup();
  1124. $('.top.menu .poping.up').popup({
  1125. onShow: function () {
  1126. if ($('.top.menu .menu.transition').hasClass('visible')) {
  1127. return false;
  1128. }
  1129. }
  1130. });
  1131. $('.tabular.menu .item').tab();
  1132. $('.tabable.menu .item').tab();
  1133. $('.toggle.button').click(function () {
  1134. $($(this).data('target')).slideToggle(100);
  1135. });
  1136. // Highlight JS
  1137. if (typeof hljs != 'undefined') {
  1138. hljs.initHighlightingOnLoad();
  1139. }
  1140. // Dropzone
  1141. var $dropz = $('#dropzone');
  1142. if ($dropz.length > 0) {
  1143. // Disable auto discover for all elements:
  1144. Dropzone.autoDiscover = false;
  1145. var filenameDict = {};
  1146. $dropz.dropzone({
  1147. url: $dropz.data('upload-url'),
  1148. headers: {"X-Csrf-Token": csrf},
  1149. maxFiles: $dropz.data('max-file'),
  1150. maxFilesize: $dropz.data('max-size'),
  1151. acceptedFiles: ($dropz.data('accepts') === '*/*') ? null : $dropz.data('accepts'),
  1152. addRemoveLinks: true,
  1153. dictDefaultMessage: $dropz.data('default-message'),
  1154. dictInvalidFileType: $dropz.data('invalid-input-type'),
  1155. dictFileTooBig: $dropz.data('file-too-big'),
  1156. dictRemoveFile: $dropz.data('remove-file'),
  1157. init: function () {
  1158. this.on("success", function (file, data) {
  1159. filenameDict[file.name] = data.uuid;
  1160. var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
  1161. $('.files').append(input);
  1162. });
  1163. this.on("removedfile", function (file) {
  1164. if (file.name in filenameDict) {
  1165. $('#' + filenameDict[file.name]).remove();
  1166. }
  1167. if ($dropz.data('remove-url') && $dropz.data('csrf')) {
  1168. $.post($dropz.data('remove-url'), {file: filenameDict[file.name], _csrf: $dropz.data('csrf')});
  1169. }
  1170. })
  1171. }
  1172. });
  1173. }
  1174. // Emojify
  1175. emojify.setConfig({
  1176. img_dir: suburl + '/img/emoji',
  1177. ignore_emoticons: true
  1178. });
  1179. var hasEmoji = document.getElementsByClassName('has-emoji');
  1180. for (var i = 0; i < hasEmoji.length; i++) {
  1181. emojify.run(hasEmoji[i]);
  1182. }
  1183. // Clipboard JS
  1184. var clipboard = new Clipboard('.clipboard');
  1185. clipboard.on('success', function (e) {
  1186. e.clearSelection();
  1187. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1188. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  1189. $('#' + e.trigger.getAttribute('id')).popup('show');
  1190. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1191. });
  1192. clipboard.on('error', function (e) {
  1193. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1194. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  1195. $('#' + e.trigger.getAttribute('id')).popup('show');
  1196. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1197. });
  1198. // Clipboard for copying filename on edit page
  1199. if ($('.clipboard-tree-name').length) {
  1200. new Clipboard(document.querySelector('.clipboard-tree-name'), {
  1201. text: function () {
  1202. return $('#tree-name').val();
  1203. }
  1204. });
  1205. }
  1206. // Helpers.
  1207. $('.delete-button').click(function () {
  1208. var $this = $(this);
  1209. $('.delete.modal').modal({
  1210. closable: false,
  1211. onApprove: function () {
  1212. if ($this.data('type') == "form") {
  1213. $($this.data('form')).submit();
  1214. return;
  1215. }
  1216. $.post($this.data('url'), {
  1217. "_csrf": csrf,
  1218. "id": $this.data("id")
  1219. }).done(function (data) {
  1220. window.location.href = data.redirect;
  1221. });
  1222. }
  1223. }).modal('show');
  1224. return false;
  1225. });
  1226. $('.show-panel.button').click(function () {
  1227. $($(this).data('panel')).show();
  1228. });
  1229. $('.show-modal.button').click(function () {
  1230. $($(this).data('modal')).modal('show');
  1231. });
  1232. $('.delete-post.button').click(function () {
  1233. var $this = $(this);
  1234. $.post($this.data('request-url'), {
  1235. "_csrf": csrf
  1236. }).done(function () {
  1237. window.location.href = $this.data('done-url');
  1238. });
  1239. });
  1240. // Set anchor.
  1241. $('.markdown').each(function () {
  1242. var headers = {};
  1243. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  1244. var node = $(this);
  1245. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  1246. var name = val;
  1247. if (headers[val] > 0) {
  1248. name = val + '-' + headers[val];
  1249. }
  1250. if (headers[val] == undefined) {
  1251. headers[val] = 1;
  1252. } else {
  1253. headers[val] += 1;
  1254. }
  1255. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1256. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  1257. });
  1258. });
  1259. buttonsClickOnEnter();
  1260. searchUsers();
  1261. searchRepositories();
  1262. initCommentForm();
  1263. initInstall();
  1264. initRepository();
  1265. initWikiForm();
  1266. initIssueForm();
  1267. initEditForm();
  1268. initEditor();
  1269. initOrganization();
  1270. initWebhook();
  1271. initAdmin();
  1272. initQuickPull();
  1273. var routes = {
  1274. 'div.user.settings': initUserSettings,
  1275. 'div.repository.settings.collaboration': initRepositoryCollaboration
  1276. };
  1277. var selector;
  1278. for (selector in routes) {
  1279. if ($(selector).length > 0) {
  1280. routes[selector]();
  1281. break;
  1282. }
  1283. }
  1284. });
  1285. function changeHash(hash) {
  1286. if (history.pushState) {
  1287. history.pushState(null, null, hash);
  1288. }
  1289. else {
  1290. location.hash = hash;
  1291. }
  1292. }
  1293. function deSelect() {
  1294. if (window.getSelection) {
  1295. window.getSelection().removeAllRanges();
  1296. } else {
  1297. document.selection.empty();
  1298. }
  1299. }
  1300. function selectRange($list, $select, $from) {
  1301. $list.removeClass('active');
  1302. if ($from) {
  1303. var a = parseInt($select.attr('rel').substr(1));
  1304. var b = parseInt($from.attr('rel').substr(1));
  1305. var c;
  1306. if (a != b) {
  1307. if (a > b) {
  1308. c = a;
  1309. a = b;
  1310. b = c;
  1311. }
  1312. var classes = [];
  1313. for (var i = a; i <= b; i++) {
  1314. classes.push('.L' + i);
  1315. }
  1316. $list.filter(classes.join(',')).addClass('active');
  1317. changeHash('#L' + a + '-' + 'L' + b);
  1318. return
  1319. }
  1320. }
  1321. $select.addClass('active');
  1322. changeHash('#' + $select.attr('rel'));
  1323. }
  1324. $(window).load(function () {
  1325. initCodeView();
  1326. // Repo clone url.
  1327. if ($('#repo-clone-url').length > 0) {
  1328. switch (localStorage.getItem('repo-clone-protocol')) {
  1329. case 'ssh':
  1330. if ($('#repo-clone-ssh').click().length === 0) {
  1331. $('#repo-clone-https').click();
  1332. }
  1333. break;
  1334. default:
  1335. $('#repo-clone-https').click();
  1336. break;
  1337. }
  1338. }
  1339. });
  1340. $(function () {
  1341. if ($('.user.signin').length > 0) return;
  1342. $('form').areYouSure();
  1343. });