gogs.js 49 KB

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