gogs.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. });
  1112. $('.jump.dropdown').dropdown({
  1113. action: 'hide',
  1114. onShow: function () {
  1115. $('.poping.up').popup('hide');
  1116. }
  1117. });
  1118. $('.slide.up.dropdown').dropdown({
  1119. transition: 'slide up'
  1120. });
  1121. $('.upward.dropdown').dropdown({
  1122. direction: 'upward'
  1123. });
  1124. $('.ui.accordion').accordion();
  1125. $('.ui.checkbox').checkbox();
  1126. $('.ui.progress').progress({
  1127. showActivity: false
  1128. });
  1129. $('.poping.up').popup();
  1130. $('.top.menu .poping.up').popup({
  1131. onShow: function () {
  1132. if ($('.top.menu .menu.transition').hasClass('visible')) {
  1133. return false;
  1134. }
  1135. }
  1136. });
  1137. $('.tabular.menu .item').tab();
  1138. $('.tabable.menu .item').tab();
  1139. $('.toggle.button').click(function () {
  1140. $($(this).data('target')).slideToggle(100);
  1141. });
  1142. // Highlight JS
  1143. if (typeof hljs != 'undefined') {
  1144. hljs.initHighlightingOnLoad();
  1145. }
  1146. // Dropzone
  1147. var $dropzone = $('#dropzone');
  1148. if ($dropzone.length > 0) {
  1149. // Disable auto discover for all elements:
  1150. Dropzone.autoDiscover = false;
  1151. var filenameDict = {};
  1152. $dropzone.dropzone({
  1153. url: $dropzone.data('upload-url'),
  1154. headers: {"X-Csrf-Token": csrf},
  1155. maxFiles: $dropzone.data('max-file'),
  1156. maxFilesize: $dropzone.data('max-size'),
  1157. acceptedFiles: ($dropzone.data('accepts') === '*/*') ? null : $dropzone.data('accepts'),
  1158. addRemoveLinks: true,
  1159. dictDefaultMessage: $dropzone.data('default-message'),
  1160. dictInvalidFileType: $dropzone.data('invalid-input-type'),
  1161. dictFileTooBig: $dropzone.data('file-too-big'),
  1162. dictRemoveFile: $dropzone.data('remove-file'),
  1163. init: function () {
  1164. this.on("success", function (file, data) {
  1165. filenameDict[file.name] = data.uuid;
  1166. var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
  1167. $('.files').append(input);
  1168. });
  1169. this.on("removedfile", function (file) {
  1170. if (file.name in filenameDict) {
  1171. $('#' + filenameDict[file.name]).remove();
  1172. }
  1173. if ($dropzone.data('remove-url') && $dropzone.data('csrf')) {
  1174. $.post($dropzone.data('remove-url'), {
  1175. file: filenameDict[file.name],
  1176. _csrf: $dropzone.data('csrf')
  1177. });
  1178. }
  1179. })
  1180. }
  1181. });
  1182. }
  1183. // Emojify
  1184. emojify.setConfig({
  1185. img_dir: suburl + '/img/emoji',
  1186. ignore_emoticons: true
  1187. });
  1188. var hasEmoji = document.getElementsByClassName('has-emoji');
  1189. for (var i = 0; i < hasEmoji.length; i++) {
  1190. emojify.run(hasEmoji[i]);
  1191. }
  1192. // Clipboard JS
  1193. var clipboard = new Clipboard('.clipboard');
  1194. clipboard.on('success', function (e) {
  1195. e.clearSelection();
  1196. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1197. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  1198. $('#' + e.trigger.getAttribute('id')).popup('show');
  1199. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1200. });
  1201. clipboard.on('error', function (e) {
  1202. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1203. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  1204. $('#' + e.trigger.getAttribute('id')).popup('show');
  1205. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1206. });
  1207. // Helpers.
  1208. $('.delete-button').click(function () {
  1209. var $this = $(this);
  1210. $('.delete.modal').modal({
  1211. closable: false,
  1212. onApprove: function () {
  1213. if ($this.data('type') == "form") {
  1214. $($this.data('form')).submit();
  1215. return;
  1216. }
  1217. $.post($this.data('url'), {
  1218. "_csrf": csrf,
  1219. "id": $this.data("id")
  1220. }).done(function (data) {
  1221. window.location.href = data.redirect;
  1222. });
  1223. }
  1224. }).modal('show');
  1225. return false;
  1226. });
  1227. $('.show-panel.button').click(function () {
  1228. $($(this).data('panel')).show();
  1229. });
  1230. $('.show-modal.button').click(function () {
  1231. $($(this).data('modal')).modal('show');
  1232. });
  1233. $('.delete-post.button').click(function () {
  1234. var $this = $(this);
  1235. $.post($this.data('request-url'), {
  1236. "_csrf": csrf
  1237. }).done(function () {
  1238. window.location.href = $this.data('done-url');
  1239. });
  1240. });
  1241. // Set anchor.
  1242. $('.markdown').each(function () {
  1243. var headers = {};
  1244. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  1245. var node = $(this);
  1246. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, '').replace(/[ ]/g, '-'));
  1247. var name = val;
  1248. if (headers[val] > 0) {
  1249. name = val + '-' + headers[val];
  1250. }
  1251. if (headers[val] == undefined) {
  1252. headers[val] = 1;
  1253. } else {
  1254. headers[val] += 1;
  1255. }
  1256. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1257. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  1258. });
  1259. });
  1260. buttonsClickOnEnter();
  1261. searchUsers();
  1262. searchRepositories();
  1263. initCommentForm();
  1264. initInstall();
  1265. initRepository();
  1266. initWikiForm();
  1267. initEditForm();
  1268. initEditor();
  1269. initOrganization();
  1270. initWebhook();
  1271. initAdmin();
  1272. initCodeView();
  1273. // Repo clone url.
  1274. if ($('#repo-clone-url').length > 0) {
  1275. switch (localStorage.getItem('repo-clone-protocol')) {
  1276. case 'ssh':
  1277. if ($('#repo-clone-ssh').click().length === 0) {
  1278. $('#repo-clone-https').click();
  1279. }
  1280. break;
  1281. default:
  1282. $('#repo-clone-https').click();
  1283. break;
  1284. }
  1285. }
  1286. var routes = {
  1287. 'div.user.settings': initUserSettings,
  1288. 'div.repository.settings.collaboration': initRepositoryCollaboration
  1289. };
  1290. var selector;
  1291. for (selector in routes) {
  1292. if ($(selector).length > 0) {
  1293. routes[selector]();
  1294. break;
  1295. }
  1296. }
  1297. });
  1298. function changeHash(hash) {
  1299. if (history.pushState) {
  1300. history.pushState(null, null, hash);
  1301. }
  1302. else {
  1303. location.hash = hash;
  1304. }
  1305. }
  1306. function deSelect() {
  1307. if (window.getSelection) {
  1308. window.getSelection().removeAllRanges();
  1309. } else {
  1310. document.selection.empty();
  1311. }
  1312. }
  1313. function selectRange($list, $select, $from) {
  1314. $list.removeClass('active');
  1315. if ($from) {
  1316. var a = parseInt($select.attr('rel').substr(1));
  1317. var b = parseInt($from.attr('rel').substr(1));
  1318. var c;
  1319. if (a != b) {
  1320. if (a > b) {
  1321. c = a;
  1322. a = b;
  1323. b = c;
  1324. }
  1325. var classes = [];
  1326. for (var i = a; i <= b; i++) {
  1327. classes.push('.L' + i);
  1328. }
  1329. $list.filter(classes.join(',')).addClass('active');
  1330. changeHash('#L' + a + '-' + 'L' + b);
  1331. return
  1332. }
  1333. }
  1334. $select.addClass('active');
  1335. changeHash('#' + $select.attr('rel'));
  1336. }
  1337. $(function () {
  1338. if ($('.user.signin').length > 0) return;
  1339. $('form').areYouSure();
  1340. });