gogs.js 49 KB

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