gogs.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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, .enable-whitelist').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. // Highlight payload on first click
  838. $('.hook.history.list .toggle.button').click(function () {
  839. $($(this).data('target') + ' .nohighlight').each(function () {
  840. var $this = $(this);
  841. $this.removeClass('nohighlight');
  842. setTimeout(function(){ hljs.highlightBlock($this[0]) }, 500);
  843. })
  844. })
  845. // Test delivery
  846. $('#test-delivery').click(function () {
  847. var $this = $(this);
  848. $this.addClass('loading disabled');
  849. $.post($this.data('link'), {
  850. "_csrf": csrf
  851. }).done(
  852. setTimeout(function () {
  853. window.location.href = $this.data('redirect');
  854. }, 5000)
  855. )
  856. });
  857. }
  858. function initAdmin() {
  859. if ($('.admin').length == 0) {
  860. return;
  861. }
  862. // New user
  863. if ($('.admin.new.user').length > 0 ||
  864. $('.admin.edit.user').length > 0) {
  865. $('#login_type').change(function () {
  866. if ($(this).val().substring(0, 1) == '0') {
  867. $('#login_name').removeAttr('required');
  868. $('.non-local').hide();
  869. $('.local').show();
  870. $('#user_name').focus();
  871. if ($(this).data('password') == "required") {
  872. $('#password').attr('required', 'required');
  873. }
  874. } else {
  875. $('#login_name').attr('required', 'required');
  876. $('.non-local').show();
  877. $('.local').hide();
  878. $('#login_name').focus();
  879. $('#password').removeAttr('required');
  880. }
  881. });
  882. }
  883. function onSecurityProtocolChange() {
  884. if ($('#security_protocol').val() > 0) {
  885. $('.has-tls').show();
  886. } else {
  887. $('.has-tls').hide();
  888. }
  889. }
  890. // New authentication
  891. if ($('.admin.new.authentication').length > 0) {
  892. $('#auth_type').change(function () {
  893. $('.ldap').hide();
  894. $('.dldap').hide();
  895. $('.smtp').hide();
  896. $('.pam').hide();
  897. $('.has-tls').hide();
  898. var authType = $(this).val();
  899. switch (authType) {
  900. case '2': // LDAP
  901. $('.ldap').show();
  902. break;
  903. case '3': // SMTP
  904. $('.smtp').show();
  905. $('.has-tls').show();
  906. break;
  907. case '4': // PAM
  908. $('.pam').show();
  909. break;
  910. case '5': // LDAP
  911. $('.dldap').show();
  912. break;
  913. }
  914. if (authType == '2' || authType == '5') {
  915. onSecurityProtocolChange()
  916. }
  917. });
  918. $('#security_protocol').change(onSecurityProtocolChange)
  919. }
  920. // Edit authentication
  921. if ($('.admin.edit.authentication').length > 0) {
  922. var authType = $('#auth_type').val();
  923. if (authType == '2' || authType == '5') {
  924. $('#security_protocol').change(onSecurityProtocolChange);
  925. }
  926. }
  927. // Notice
  928. if ($('.admin.notice')) {
  929. var $detailModal = $('#detail-modal');
  930. // Attach view detail modals
  931. $('.view-detail').click(function () {
  932. $detailModal.find('.content p').text($(this).data('content'));
  933. $detailModal.modal('show');
  934. return false;
  935. });
  936. // Select actions
  937. var $checkboxes = $('.select.table .ui.checkbox');
  938. $('.select.action').click(function () {
  939. switch ($(this).data('action')) {
  940. case 'select-all':
  941. $checkboxes.checkbox('check');
  942. break;
  943. case 'deselect-all':
  944. $checkboxes.checkbox('uncheck');
  945. break;
  946. case 'inverse':
  947. $checkboxes.checkbox('toggle');
  948. break;
  949. }
  950. });
  951. $('#delete-selection').click(function () {
  952. var $this = $(this);
  953. $this.addClass("loading disabled");
  954. var ids = [];
  955. $checkboxes.each(function () {
  956. if ($(this).checkbox('is checked')) {
  957. ids.push($(this).data('id'));
  958. }
  959. });
  960. $.post($this.data('link'), {
  961. "_csrf": csrf,
  962. "ids": ids
  963. }).done(function () {
  964. window.location.href = $this.data('redirect');
  965. });
  966. });
  967. }
  968. }
  969. function buttonsClickOnEnter() {
  970. $('.ui.button').keypress(function (e) {
  971. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  972. $(this).click();
  973. });
  974. }
  975. function hideWhenLostFocus(body, parent) {
  976. $(document).click(function (e) {
  977. var target = e.target;
  978. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  979. $(body).hide();
  980. }
  981. });
  982. }
  983. function searchUsers() {
  984. if (!$('#search-user-box .results').length) {
  985. return;
  986. }
  987. var $searchUserBox = $('#search-user-box');
  988. var $results = $searchUserBox.find('.results');
  989. $searchUserBox.keyup(function () {
  990. var $this = $(this);
  991. var keyword = $this.find('input').val();
  992. if (keyword.length < 2) {
  993. $results.hide();
  994. return;
  995. }
  996. $.ajax({
  997. url: suburl + '/api/v1/users/search?q=' + keyword,
  998. dataType: "json",
  999. success: function (response) {
  1000. var notEmpty = function (str) {
  1001. return str && str.length > 0;
  1002. };
  1003. $results.html('');
  1004. if (response.ok && response.data.length) {
  1005. var html = '';
  1006. $.each(response.data, function (i, item) {
  1007. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  1008. if (notEmpty(item.full_name)) {
  1009. html += ' (' + item.full_name + ')';
  1010. }
  1011. html += '</div>';
  1012. });
  1013. $results.html(html);
  1014. $this.find('.results .item').click(function () {
  1015. $this.find('input').val($(this).find('.username').text());
  1016. $results.hide();
  1017. });
  1018. $results.show();
  1019. } else {
  1020. $results.hide();
  1021. }
  1022. }
  1023. });
  1024. });
  1025. $searchUserBox.find('input').focus(function () {
  1026. $searchUserBox.keyup();
  1027. });
  1028. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  1029. }
  1030. // FIXME: merge common parts in two functions
  1031. function searchRepositories() {
  1032. if (!$('#search-repo-box .results').length) {
  1033. return;
  1034. }
  1035. var $searchRepoBox = $('#search-repo-box');
  1036. var $results = $searchRepoBox.find('.results');
  1037. $searchRepoBox.keyup(function () {
  1038. var $this = $(this);
  1039. var keyword = $this.find('input').val();
  1040. if (keyword.length < 2) {
  1041. $results.hide();
  1042. return;
  1043. }
  1044. $.ajax({
  1045. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $searchRepoBox.data('uid'),
  1046. dataType: "json",
  1047. success: function (response) {
  1048. var notEmpty = function (str) {
  1049. return str && str.length > 0;
  1050. };
  1051. $results.html('');
  1052. if (response.ok && response.data.length) {
  1053. var html = '';
  1054. $.each(response.data, function (i, item) {
  1055. html += '<div class="item"><i class="octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  1056. });
  1057. $results.html(html);
  1058. $this.find('.results .item').click(function () {
  1059. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  1060. $results.hide();
  1061. });
  1062. $results.show();
  1063. } else {
  1064. $results.hide();
  1065. }
  1066. }
  1067. });
  1068. });
  1069. $searchRepoBox.find('input').focus(function () {
  1070. $searchRepoBox.keyup();
  1071. });
  1072. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  1073. }
  1074. function initCodeView() {
  1075. if ($('.code-view .linenums').length > 0) {
  1076. $(document).on('click', '.lines-num span', function (e) {
  1077. var $select = $(this);
  1078. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  1079. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  1080. deSelect();
  1081. });
  1082. $(window).on('hashchange', function (e) {
  1083. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1084. var $list = $('.code-view ol.linenums > li');
  1085. var $first;
  1086. if (m) {
  1087. $first = $list.filter('.' + m[1]);
  1088. selectRange($list, $first, $list.filter('.' + m[2]));
  1089. $("html, body").scrollTop($first.offset().top - 200);
  1090. return;
  1091. }
  1092. m = window.location.hash.match(/^#(L\d+)$/);
  1093. if (m) {
  1094. $first = $list.filter('.' + m[1]);
  1095. selectRange($list, $first);
  1096. $("html, body").scrollTop($first.offset().top - 200);
  1097. }
  1098. }).trigger('hashchange');
  1099. }
  1100. }
  1101. $(document).ready(function () {
  1102. csrf = $('meta[name=_csrf]').attr("content");
  1103. suburl = $('meta[name=_suburl]').attr("content");
  1104. // Show exact time
  1105. $('.time-since').each(function () {
  1106. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  1107. });
  1108. // Semantic UI modules.
  1109. $('.ui.dropdown').dropdown({
  1110. forceSelection: false,
  1111. action:'select'
  1112. });
  1113. $('.jump.dropdown').dropdown({
  1114. action: 'select',
  1115. onShow: function () {
  1116. $('.poping.up').popup('hide');
  1117. }
  1118. });
  1119. $('.slide.up.dropdown').dropdown({
  1120. transition: 'slide up'
  1121. });
  1122. $('.upward.dropdown').dropdown({
  1123. direction: 'upward'
  1124. });
  1125. $('.ui.accordion').accordion();
  1126. $('.ui.checkbox').checkbox();
  1127. $('.ui.progress').progress({
  1128. showActivity: false
  1129. });
  1130. $('.poping.up').popup();
  1131. $('.top.menu .poping.up').popup({
  1132. onShow: function () {
  1133. if ($('.top.menu .menu.transition').hasClass('visible')) {
  1134. return false;
  1135. }
  1136. }
  1137. });
  1138. $('.tabular.menu .item').tab();
  1139. $('.tabable.menu .item').tab();
  1140. $('.toggle.button').click(function () {
  1141. $($(this).data('target')).slideToggle(100);
  1142. });
  1143. // Highlight JS
  1144. if (typeof hljs != 'undefined') {
  1145. hljs.initHighlightingOnLoad();
  1146. }
  1147. // Dropzone
  1148. var $dropzone = $('#dropzone');
  1149. if ($dropzone.length > 0) {
  1150. // Disable auto discover for all elements:
  1151. Dropzone.autoDiscover = false;
  1152. var filenameDict = {};
  1153. $dropzone.dropzone({
  1154. url: $dropzone.data('upload-url'),
  1155. headers: {"X-Csrf-Token": csrf},
  1156. maxFiles: $dropzone.data('max-file'),
  1157. maxFilesize: $dropzone.data('max-size'),
  1158. acceptedFiles: ($dropzone.data('accepts') === '*/*') ? null : $dropzone.data('accepts'),
  1159. addRemoveLinks: true,
  1160. dictDefaultMessage: $dropzone.data('default-message'),
  1161. dictInvalidFileType: $dropzone.data('invalid-input-type'),
  1162. dictFileTooBig: $dropzone.data('file-too-big'),
  1163. dictRemoveFile: $dropzone.data('remove-file'),
  1164. init: function () {
  1165. this.on("success", function (file, data) {
  1166. filenameDict[file.name] = data.uuid;
  1167. var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
  1168. $('.files').append(input);
  1169. });
  1170. this.on("removedfile", function (file) {
  1171. if (file.name in filenameDict) {
  1172. $('#' + filenameDict[file.name]).remove();
  1173. }
  1174. if ($dropzone.data('remove-url') && $dropzone.data('csrf')) {
  1175. $.post($dropzone.data('remove-url'), {
  1176. file: filenameDict[file.name],
  1177. _csrf: $dropzone.data('csrf')
  1178. });
  1179. }
  1180. })
  1181. }
  1182. });
  1183. }
  1184. // Emojify
  1185. emojify.setConfig({
  1186. img_dir: suburl + '/img/emoji',
  1187. ignore_emoticons: true
  1188. });
  1189. var hasEmoji = document.getElementsByClassName('has-emoji');
  1190. for (var i = 0; i < hasEmoji.length; i++) {
  1191. emojify.run(hasEmoji[i]);
  1192. }
  1193. // Clipboard JS
  1194. var clipboard = new Clipboard('.clipboard');
  1195. clipboard.on('success', function (e) {
  1196. e.clearSelection();
  1197. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1198. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  1199. $('#' + e.trigger.getAttribute('id')).popup('show');
  1200. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1201. });
  1202. clipboard.on('error', function (e) {
  1203. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1204. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  1205. $('#' + e.trigger.getAttribute('id')).popup('show');
  1206. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1207. });
  1208. // Helpers.
  1209. $('.delete-button').click(function () {
  1210. var $this = $(this);
  1211. $('.delete.modal').modal({
  1212. closable: false,
  1213. onApprove: function () {
  1214. if ($this.data('type') == "form") {
  1215. $($this.data('form')).submit();
  1216. return;
  1217. }
  1218. $.post($this.data('url'), {
  1219. "_csrf": csrf,
  1220. "id": $this.data("id")
  1221. }).done(function (data) {
  1222. window.location.href = data.redirect;
  1223. });
  1224. }
  1225. }).modal('show');
  1226. return false;
  1227. });
  1228. $('.show-panel.button').click(function () {
  1229. $($(this).data('panel')).show();
  1230. });
  1231. $('.show-modal.button').click(function () {
  1232. $($(this).data('modal')).modal('show');
  1233. });
  1234. $('.delete-post.button').click(function () {
  1235. var $this = $(this);
  1236. $.post($this.data('request-url'), {
  1237. "_csrf": csrf
  1238. }).done(function () {
  1239. window.location.href = $this.data('done-url');
  1240. });
  1241. });
  1242. // Set anchor.
  1243. $('.markdown').each(function () {
  1244. var headers = {};
  1245. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  1246. var node = $(this);
  1247. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, '').replace(/[ ]/g, '-'));
  1248. var name = val;
  1249. if (headers[val] > 0) {
  1250. name = val + '-' + headers[val];
  1251. }
  1252. if (headers[val] == undefined) {
  1253. headers[val] = 1;
  1254. } else {
  1255. headers[val] += 1;
  1256. }
  1257. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1258. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  1259. });
  1260. });
  1261. buttonsClickOnEnter();
  1262. searchUsers();
  1263. searchRepositories();
  1264. initCommentForm();
  1265. initInstall();
  1266. initRepository();
  1267. initWikiForm();
  1268. initEditForm();
  1269. initEditor();
  1270. initOrganization();
  1271. initWebhook();
  1272. initAdmin();
  1273. initCodeView();
  1274. // Repo clone url.
  1275. if ($('#repo-clone-url').length > 0) {
  1276. switch (localStorage.getItem('repo-clone-protocol')) {
  1277. case 'ssh':
  1278. if ($('#repo-clone-ssh').click().length === 0) {
  1279. $('#repo-clone-https').click();
  1280. }
  1281. break;
  1282. default:
  1283. $('#repo-clone-https').click();
  1284. break;
  1285. }
  1286. }
  1287. var routes = {
  1288. 'div.user.settings': initUserSettings,
  1289. 'div.repository.settings.collaboration': initRepositoryCollaboration
  1290. };
  1291. var selector;
  1292. for (selector in routes) {
  1293. if ($(selector).length > 0) {
  1294. routes[selector]();
  1295. break;
  1296. }
  1297. }
  1298. });
  1299. function changeHash(hash) {
  1300. if (history.pushState) {
  1301. history.pushState(null, null, hash);
  1302. }
  1303. else {
  1304. location.hash = hash;
  1305. }
  1306. }
  1307. function deSelect() {
  1308. if (window.getSelection) {
  1309. window.getSelection().removeAllRanges();
  1310. } else {
  1311. document.selection.empty();
  1312. }
  1313. }
  1314. function selectRange($list, $select, $from) {
  1315. $list.removeClass('active');
  1316. if ($from) {
  1317. var a = parseInt($select.attr('rel').substr(1));
  1318. var b = parseInt($from.attr('rel').substr(1));
  1319. var c;
  1320. if (a != b) {
  1321. if (a > b) {
  1322. c = a;
  1323. a = b;
  1324. b = c;
  1325. }
  1326. var classes = [];
  1327. for (var i = a; i <= b; i++) {
  1328. classes.push('.L' + i);
  1329. }
  1330. $list.filter(classes.join(',')).addClass('active');
  1331. changeHash('#L' + a + '-' + 'L' + b);
  1332. return
  1333. }
  1334. }
  1335. $select.addClass('active');
  1336. changeHash('#' + $select.attr('rel'));
  1337. }
  1338. $(function () {
  1339. if ($('.user.signin').length > 0) return;
  1340. $('form').areYouSure();
  1341. });