gogs.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. 'use strict';
  2. var csrf;
  3. var suburl;
  4. function initCommentPreviewTab($form) {
  5. var $tab_menu = $form.find('.tabular.menu');
  6. $tab_menu.find('.item').tab();
  7. $tab_menu.find('.item[data-tab="' + $tab_menu.data('preview') + '"]').click(function () {
  8. var $this = $(this);
  9. $.post($this.data('url'), {
  10. "_csrf": csrf,
  11. "mode": "gfm",
  12. "context": $this.data('context'),
  13. "text": $form.find('.tab.segment[data-tab="' + $tab_menu.data('write') + '"] textarea').val()
  14. },
  15. function (data) {
  16. var $preview_tab = $form.find('.tab.segment[data-tab="' + $tab_menu.data('preview') + '"]');
  17. $preview_tab.html(data);
  18. emojify.run($preview_tab[0]);
  19. }
  20. );
  21. });
  22. buttonsClickOnEnter();
  23. }
  24. function initCommentForm() {
  25. if ($('.comment.form').length == 0) {
  26. return
  27. }
  28. initCommentPreviewTab($('.comment.form'));
  29. // Labels
  30. var $list = $('.ui.labels.list');
  31. var $no_select = $list.find('.no-select');
  32. var $label_menu = $('.select-label .menu');
  33. var has_label_update_action = $label_menu.data('action') == 'update';
  34. function updateIssueMeta(url, action, id) {
  35. $.post(url, {
  36. "_csrf": csrf,
  37. "action": action,
  38. "id": id
  39. });
  40. }
  41. $label_menu.find('.item:not(.no-select)').click(function () {
  42. if ($(this).hasClass('checked')) {
  43. $(this).removeClass('checked');
  44. $(this).find('.octicon').removeClass('octicon-check');
  45. if (has_label_update_action) {
  46. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  47. }
  48. } else {
  49. $(this).addClass('checked');
  50. $(this).find('.octicon').addClass('octicon-check');
  51. if (has_label_update_action) {
  52. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  53. }
  54. }
  55. var label_ids = "";
  56. $(this).parent().find('.item').each(function () {
  57. if ($(this).hasClass('checked')) {
  58. label_ids += $(this).data('id') + ",";
  59. $($(this).data('id-selector')).removeClass('hide');
  60. } else {
  61. $($(this).data('id-selector')).addClass('hide');
  62. }
  63. });
  64. if (label_ids.length == 0) {
  65. $no_select.removeClass('hide');
  66. } else {
  67. $no_select.addClass('hide');
  68. }
  69. $($(this).parent().data('id')).val(label_ids);
  70. return false;
  71. });
  72. $label_menu.find('.no-select.item').click(function () {
  73. if (has_label_update_action) {
  74. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  75. }
  76. $(this).parent().find('.item').each(function () {
  77. $(this).removeClass('checked');
  78. $(this).find('.octicon').removeClass('octicon-check');
  79. });
  80. $list.find('.item').each(function () {
  81. $(this).addClass('hide');
  82. });
  83. $no_select.removeClass('hide');
  84. $($(this).parent().data('id')).val('');
  85. });
  86. function selectItem(select_id, input_id) {
  87. var $menu = $(select_id + ' .menu');
  88. var $list = $('.ui' + select_id + '.list');
  89. var has_update_action = $menu.data('action') == 'update';
  90. $menu.find('.item:not(.no-select)').click(function () {
  91. $(this).parent().find('.item').each(function () {
  92. $(this).removeClass('selected active')
  93. });
  94. $(this).addClass('selected active');
  95. if (has_update_action) {
  96. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  97. }
  98. switch (input_id) {
  99. case '#milestone_id':
  100. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  101. $(this).text() + '</a>');
  102. break;
  103. case '#assignee_id':
  104. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  105. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  106. $(this).text() + '</a>');
  107. }
  108. $('.ui' + select_id + '.list .no-select').addClass('hide');
  109. $(input_id).val($(this).data('id'));
  110. });
  111. $menu.find('.no-select.item').click(function () {
  112. $(this).parent().find('.item:not(.no-select)').each(function () {
  113. $(this).removeClass('selected active')
  114. });
  115. if (has_update_action) {
  116. updateIssueMeta($menu.data('update-url'), '', '');
  117. }
  118. $list.find('.selected').html('');
  119. $list.find('.no-select').removeClass('hide');
  120. $(input_id).val('');
  121. });
  122. }
  123. // Milestone and assignee
  124. selectItem('.select-milestone', '#milestone_id');
  125. selectItem('.select-assignee', '#assignee_id');
  126. }
  127. function initInstall() {
  128. if ($('.install').length == 0) {
  129. return;
  130. }
  131. // Database type change detection.
  132. $("#db_type").change(function () {
  133. var sqlite_default = 'data/gogs.db';
  134. var tidb_default = 'data/gogs_tidb';
  135. var db_type = $(this).val();
  136. if (db_type === "SQLite3" || db_type === "TiDB") {
  137. $('#sql_settings').hide();
  138. $('#pgsql_settings').hide();
  139. $('#sqlite_settings').show();
  140. if (db_type === "SQLite3" && $('#db_path').val() == tidb_default) {
  141. $('#db_path').val(sqlite_default);
  142. } else if (db_type === "TiDB" && $('#db_path').val() == sqlite_default) {
  143. $('#db_path').val(tidb_default);
  144. }
  145. return;
  146. }
  147. var mysql_default = '127.0.0.1:3306';
  148. var postgres_default = '127.0.0.1:5432';
  149. $('#sqlite_settings').hide();
  150. $('#sql_settings').show();
  151. if (db_type === "PostgreSQL") {
  152. $('#pgsql_settings').show();
  153. if ($('#db_host').val() == mysql_default) {
  154. $('#db_host').val(postgres_default);
  155. }
  156. } else {
  157. $('#pgsql_settings').hide();
  158. if ($('#db_host').val() == postgres_default) {
  159. $('#db_host').val(mysql_default);
  160. }
  161. }
  162. });
  163. $('#offline-mode input').change(function () {
  164. if ($(this).is(':checked')) {
  165. $('#disable-gravatar').checkbox('check');
  166. }
  167. });
  168. $('#disable-registration input').change(function () {
  169. if ($(this).is(':checked')) {
  170. $('#enable-captcha').checkbox('uncheck');
  171. }
  172. });
  173. $('#enable-captcha input').change(function () {
  174. if ($(this).is(':checked')) {
  175. $('#disable-registration').checkbox('uncheck');
  176. }
  177. });
  178. }
  179. function initRepository() {
  180. if ($('.repository').length == 0) {
  181. return;
  182. }
  183. function initFilterSearchDropdown(selector) {
  184. var $dropdown = $(selector);
  185. $dropdown.dropdown({
  186. fullTextSearch: true,
  187. onChange: function (text, value, $choice) {
  188. window.location.href = $choice.data('url');
  189. console.log($choice.data('url'))
  190. },
  191. message: {noResults: $dropdown.data('no-results')}
  192. });
  193. }
  194. // File list
  195. if ($('.repository.file.list').length > 0) {
  196. initFilterSearchDropdown('.choose.reference .dropdown');
  197. $('.reference.column').click(function () {
  198. $('.choose.reference .scrolling.menu').css('display', 'none');
  199. $('.choose.reference .text').removeClass('black');
  200. $($(this).data('target')).css('display', 'block');
  201. $(this).find('.text').addClass('black');
  202. return false;
  203. });
  204. }
  205. // Options
  206. if ($('.repository.settings.options').length > 0) {
  207. $('#repo_name').keyup(function () {
  208. var $prompt_span = $('#repo-name-change-prompt');
  209. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  210. $prompt_span.show();
  211. } else {
  212. $prompt_span.hide();
  213. }
  214. });
  215. }
  216. // Labels
  217. if ($('.repository.labels').length > 0) {
  218. // Create label
  219. var $new_label_panel = $('.new-label.segment');
  220. $('.new-label.button').click(function () {
  221. $new_label_panel.show();
  222. });
  223. $('.new-label.segment .cancel').click(function () {
  224. $new_label_panel.hide();
  225. });
  226. $('.color-picker').each(function () {
  227. $(this).minicolors();
  228. });
  229. $('.precolors .color').click(function () {
  230. var color_hex = $(this).data('color-hex');
  231. $('.color-picker').val(color_hex);
  232. $('.minicolors-swatch-color').css("background-color", color_hex);
  233. });
  234. $('.edit-label-button').click(function () {
  235. $('#label-modal-id').val($(this).data('id'));
  236. $('.edit-label .new-label-input').val($(this).data('title'));
  237. $('.edit-label .color-picker').val($(this).data('color'));
  238. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  239. $('.edit-label.modal').modal({
  240. onApprove: function () {
  241. $('.edit-label.form').submit();
  242. }
  243. }).modal('show');
  244. return false;
  245. });
  246. }
  247. // Milestones
  248. if ($('.repository.milestones').length > 0) {
  249. }
  250. if ($('.repository.new.milestone').length > 0) {
  251. var $datepicker = $('.milestone.datepicker');
  252. $datepicker.datetimepicker({
  253. lang: $datepicker.data('lang'),
  254. inline: true,
  255. timepicker: false,
  256. startDate: $datepicker.data('start-date'),
  257. formatDate: 'Y-m-d',
  258. onSelectDate: function (ct) {
  259. $('#deadline').val(ct.dateFormat('Y-m-d'));
  260. }
  261. });
  262. $('#clear-date').click(function () {
  263. $('#deadline').val('');
  264. return false;
  265. });
  266. }
  267. // Issues
  268. if ($('.repository.view.issue').length > 0) {
  269. // Edit issue title
  270. var $issue_title = $('#issue-title');
  271. var $edit_input = $('#edit-title-input input');
  272. var editTitleToggle = function () {
  273. $issue_title.toggle();
  274. $('.not-in-edit').toggle();
  275. $('#edit-title-input').toggle();
  276. $('.in-edit').toggle();
  277. $edit_input.focus();
  278. return false;
  279. };
  280. $('#edit-title').click(editTitleToggle);
  281. $('#cancel-edit-title').click(editTitleToggle);
  282. $('#save-edit-title').click(editTitleToggle).
  283. click(function () {
  284. if ($edit_input.val().length == 0 ||
  285. $edit_input.val() == $issue_title.text()) {
  286. $edit_input.val($issue_title.text());
  287. return false;
  288. }
  289. $.post($(this).data('update-url'), {
  290. "_csrf": csrf,
  291. "title": $edit_input.val()
  292. },
  293. function (data) {
  294. $edit_input.val(data.title);
  295. $issue_title.text(data.title);
  296. });
  297. return false;
  298. });
  299. // Edit issue or comment content
  300. $('.edit-content').click(function () {
  301. var $segment = $(this).parent().parent().next();
  302. var $edit_content_zone = $segment.find('.edit-content-zone');
  303. var $render_content = $segment.find('.render-content');
  304. var $raw_content = $segment.find('.raw-content');
  305. var $textarea;
  306. // Setup new form
  307. if ($edit_content_zone.html().length == 0) {
  308. $edit_content_zone.html($('#edit-content-form').html());
  309. $textarea = $segment.find('textarea');
  310. // Give new write/preview data-tab name to distinguish from others
  311. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  312. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  313. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  314. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  315. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  316. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  317. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  318. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  319. initCommentPreviewTab($edit_content_form);
  320. $edit_content_zone.find('.cancel.button').click(function () {
  321. $render_content.show();
  322. $edit_content_zone.hide();
  323. });
  324. $edit_content_zone.find('.save.button').click(function () {
  325. $render_content.show();
  326. $edit_content_zone.hide();
  327. $.post($edit_content_zone.data('update-url'), {
  328. "_csrf": csrf,
  329. "content": $textarea.val(),
  330. "context": $edit_content_zone.data('context')
  331. },
  332. function (data) {
  333. if (data.length == 0) {
  334. $render_content.html($('#no-content').html());
  335. } else {
  336. $render_content.html(data.content);
  337. emojify.run($render_content[0]);
  338. }
  339. });
  340. });
  341. } else {
  342. $textarea = $segment.find('textarea');
  343. }
  344. // Show write/preview tab and copy raw content as needed
  345. $edit_content_zone.show();
  346. $render_content.hide();
  347. if ($textarea.val().length == 0) {
  348. $textarea.val($raw_content.text());
  349. }
  350. $textarea.focus();
  351. return false;
  352. });
  353. // Change status
  354. var $status_btn = $('#status-button');
  355. $('#content').keyup(function () {
  356. if ($(this).val().length == 0) {
  357. $status_btn.text($status_btn.data('status'))
  358. } else {
  359. $status_btn.text($status_btn.data('status-and-comment'))
  360. }
  361. });
  362. $status_btn.click(function () {
  363. $('#status').val($status_btn.data('status-val'));
  364. $('#comment-form').submit();
  365. })
  366. }
  367. // Diff
  368. if ($('.repository.diff').length > 0) {
  369. var $counter = $('.diff-counter');
  370. if ($counter.length >= 1) {
  371. $counter.each(function (i, item) {
  372. var $item = $(item);
  373. var addLine = $item.find('span[data-line].add').data("line");
  374. var delLine = $item.find('span[data-line].del').data("line");
  375. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  376. $item.find(".bar .add").css("width", addPercent + "%");
  377. });
  378. }
  379. }
  380. // Quick start and repository home
  381. $('#repo-clone-ssh').click(function () {
  382. $('.clone-url').text($(this).data('link'));
  383. $('#repo-clone-url').val($(this).data('link'));
  384. $(this).addClass('blue');
  385. $('#repo-clone-https').removeClass('blue');
  386. });
  387. $('#repo-clone-https').click(function () {
  388. $('.clone-url').text($(this).data('link'));
  389. $('#repo-clone-url').val($(this).data('link'));
  390. $(this).addClass('blue');
  391. $('#repo-clone-ssh').removeClass('blue');
  392. });
  393. $('#repo-clone-url').click(function () {
  394. $(this).select();
  395. });
  396. // Pull request
  397. if ($('.repository.compare.pull').length > 0) {
  398. initFilterSearchDropdown('.choose.branch .dropdown');
  399. }
  400. }
  401. function initOrganization() {
  402. if ($('.organization').length == 0) {
  403. return;
  404. }
  405. // Options
  406. if ($('.organization.settings.options').length > 0) {
  407. $('#org_name').keyup(function () {
  408. var $prompt_span = $('#org-name-change-prompt');
  409. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  410. $prompt_span.show();
  411. } else {
  412. $prompt_span.hide();
  413. }
  414. });
  415. }
  416. }
  417. function initUser() {
  418. if ($('.user').length == 0) {
  419. return;
  420. }
  421. // Options
  422. if ($('.user.settings.profile').length > 0) {
  423. $('#username').keyup(function () {
  424. var $prompt_span = $('#name-change-prompt');
  425. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  426. $prompt_span.show();
  427. } else {
  428. $prompt_span.hide();
  429. }
  430. });
  431. }
  432. }
  433. function initWebhook() {
  434. if ($('.new.webhook').length == 0) {
  435. return;
  436. }
  437. $('.events.checkbox input').change(function () {
  438. if ($(this).is(':checked')) {
  439. $('.events.fields').show();
  440. }
  441. });
  442. $('.non-events.checkbox input').change(function () {
  443. if ($(this).is(':checked')) {
  444. $('.events.fields').hide();
  445. }
  446. });
  447. }
  448. function initAdmin() {
  449. if ($('.admin').length == 0) {
  450. return;
  451. }
  452. // New user
  453. if ($('.admin.new.user').length > 0 ||
  454. $('.admin.edit.user').length > 0) {
  455. $('#login_type').change(function () {
  456. if ($(this).val().substring(0, 1) == '0') {
  457. $('#login_name').removeAttr('required');
  458. $('.non-local').hide();
  459. $('.local').show();
  460. $('#user_name').focus();
  461. if ($(this).data('password') == "required") {
  462. $('#password').attr('required', 'required');
  463. }
  464. } else {
  465. $('#login_name').attr('required', 'required');
  466. $('.non-local').show();
  467. $('.local').hide();
  468. $('#login_name').focus();
  469. $('#password').removeAttr('required');
  470. }
  471. });
  472. }
  473. // New authentication
  474. if ($('.admin.new.authentication').length > 0) {
  475. $('#auth_type').change(function () {
  476. $('.ldap').hide();
  477. $('.dldap').hide();
  478. $('.smtp').hide();
  479. $('.pam').hide();
  480. var auth_type = $(this).val();
  481. switch (auth_type) {
  482. case '2': // LDAP
  483. $('.ldap').show();
  484. break;
  485. case '3': // SMTP
  486. $('.smtp').show();
  487. break;
  488. case '4': // PAM
  489. $('.pam').show();
  490. break;
  491. case '5': // LDAP
  492. $('.dldap').show();
  493. break;
  494. }
  495. });
  496. }
  497. }
  498. function buttonsClickOnEnter() {
  499. $('.ui.button').keypress(function (e) {
  500. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  501. $(this).click();
  502. });
  503. }
  504. $(document).ready(function () {
  505. csrf = $('meta[name=_csrf]').attr("content");
  506. suburl = $('meta[name=_suburl]').attr("content");
  507. // Show exact time
  508. $('.time-since').each(function () {
  509. $(this).addClass('poping up').
  510. attr('data-content', $(this).attr('title')).
  511. attr('data-variation', 'inverted tiny').
  512. attr('title', '');
  513. });
  514. // Semantic UI modules.
  515. $('.dropdown').dropdown();
  516. $('.jump.dropdown').dropdown({
  517. action: 'hide',
  518. onShow: function () {
  519. $('.poping.up').popup('hide');
  520. }
  521. });
  522. $('.slide.up.dropdown').dropdown({
  523. transition: 'slide up'
  524. });
  525. $('.ui.accordion').accordion();
  526. $('.ui.checkbox').checkbox();
  527. $('.ui.progress').progress({
  528. showActivity: false
  529. });
  530. $('.poping.up').popup();
  531. $('.top.menu .poping.up').popup({
  532. onShow: function () {
  533. if ($('.top.menu .menu.transition').hasClass('visible')) {
  534. return false;
  535. }
  536. }
  537. });
  538. $('.tabular.menu .item').tab();
  539. $('.tabable.menu .item').tab();
  540. $('.toggle.button').click(function () {
  541. $($(this).data('target')).slideToggle(100);
  542. });
  543. // Highlight JS
  544. if (typeof hljs != 'undefined') {
  545. hljs.initHighlightingOnLoad();
  546. }
  547. // Dropzone
  548. if ($('#dropzone').length > 0) {
  549. // Disable auto discover for all elements:
  550. Dropzone.autoDiscover = false;
  551. var filenameDict = {};
  552. var $dropz = $('#dropzone');
  553. $dropz.dropzone({
  554. url: $dropz.data('upload-url'),
  555. headers: {"X-Csrf-Token": csrf},
  556. maxFiles: $dropz.data('max-file'),
  557. maxFilesize: $dropz.data('max-size'),
  558. acceptedFiles: $dropz.data('accepts'),
  559. addRemoveLinks: true,
  560. dictDefaultMessage: $dropz.data('default-message'),
  561. dictInvalidFileType: $dropz.data('invalid-input-type'),
  562. dictFileTooBig: $dropz.data('file-too-big'),
  563. dictRemoveFile: $dropz.data('remove-file'),
  564. init: function () {
  565. this.on("success", function (file, data) {
  566. filenameDict[file.name] = data.uuid;
  567. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  568. });
  569. this.on("removedfile", function (file) {
  570. if (file.name in filenameDict) {
  571. $('#' + filenameDict[file.name]).remove();
  572. }
  573. })
  574. }
  575. });
  576. }
  577. // Emojify
  578. emojify.setConfig({
  579. img_dir: suburl + '/img/emoji'
  580. });
  581. emojify.run();
  582. // Clipboard JS
  583. var clipboard = new Clipboard('.clipboard');
  584. clipboard.on('success', function (e) {
  585. e.clearSelection();
  586. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  587. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  588. $('#' + e.trigger.getAttribute('id')).popup('show');
  589. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  590. });
  591. clipboard.on('error', function (e) {
  592. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  593. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  594. $('#' + e.trigger.getAttribute('id')).popup('show');
  595. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  596. });
  597. // Helpers.
  598. $('.delete-button').click(function () {
  599. var $this = $(this);
  600. $('.delete.modal').modal({
  601. closable: false,
  602. onApprove: function () {
  603. if ($this.data('type') == "form") {
  604. $($this.data('form')).submit();
  605. return;
  606. }
  607. $.post($this.data('url'), {
  608. "_csrf": csrf,
  609. "id": $this.data("id")
  610. }).done(function (data) {
  611. window.location.href = data.redirect;
  612. });
  613. }
  614. }).modal('show');
  615. return false;
  616. });
  617. $('.show-panel.button').click(function () {
  618. $($(this).data('panel')).show();
  619. });
  620. $('.show-modal.button').click(function () {
  621. $($(this).data('modal')).modal('show');
  622. });
  623. // Set anchor.
  624. $('.markdown').each(function () {
  625. var headers = {};
  626. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  627. var node = $(this);
  628. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  629. var name = val;
  630. if (headers[val] > 0) {
  631. name = val + '-' + headers[val];
  632. }
  633. if (headers[val] == undefined) {
  634. headers[val] = 1;
  635. } else {
  636. headers[val] += 1;
  637. }
  638. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  639. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  640. });
  641. });
  642. buttonsClickOnEnter();
  643. initCommentForm();
  644. initInstall();
  645. initRepository();
  646. initOrganization();
  647. initUser();
  648. initWebhook();
  649. initAdmin();
  650. });
  651. $(window).load(function () {
  652. function changeHash(hash) {
  653. if (history.pushState) {
  654. history.pushState(null, null, hash);
  655. }
  656. else {
  657. location.hash = hash;
  658. }
  659. }
  660. function deSelect() {
  661. if (window.getSelection) {
  662. window.getSelection().removeAllRanges();
  663. } else {
  664. document.selection.empty();
  665. }
  666. }
  667. function selectRange($list, $select, $from) {
  668. $list.removeClass('active');
  669. if ($from) {
  670. var a = parseInt($select.attr('rel').substr(1));
  671. var b = parseInt($from.attr('rel').substr(1));
  672. var c;
  673. if (a != b) {
  674. if (a > b) {
  675. c = a;
  676. a = b;
  677. b = c;
  678. }
  679. var classes = [];
  680. for (i = a; i <= b; i++) {
  681. classes.push('.L' + i);
  682. }
  683. $list.filter(classes.join(',')).addClass('active');
  684. changeHash('#L' + a + '-' + 'L' + b);
  685. return
  686. }
  687. }
  688. $select.addClass('active');
  689. changeHash('#' + $select.attr('rel'));
  690. }
  691. // Code view.
  692. if ($('.code-view').length > 0) {
  693. var $block = $('.code-view .linenums');
  694. var lines = $block.html().split("\n");
  695. $block.html('');
  696. var $num_list = $('.code-view .lines-num');
  697. // Building blocks.
  698. for (var i = 0; i < lines.length; i++) {
  699. $block.append('<li class="L' + (i + 1) + '" rel="L' + (i + 1) + '">' + lines[i] + '</li>');
  700. $num_list.append('<span id="L' + (i + 1) + '">' + (i + 1) + '</span>');
  701. }
  702. $(document).on('click', '.lines-num span', function (e) {
  703. var $select = $(this);
  704. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  705. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  706. deSelect();
  707. });
  708. $(window).on('hashchange', function (e) {
  709. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  710. var $list = $('.code-view ol.linenums > li');
  711. var $first;
  712. if (m) {
  713. $first = $list.filter('.' + m[1]);
  714. selectRange($list, $first, $list.filter('.' + m[2]));
  715. $("html, body").scrollTop($first.offset().top - 200);
  716. return;
  717. }
  718. m = window.location.hash.match(/^#(L\d+)$/);
  719. if (m) {
  720. $first = $list.filter('.' + m[1]);
  721. selectRange($list, $first);
  722. $("html, body").scrollTop($first.offset().top - 200);
  723. }
  724. }).trigger('hashchange');
  725. }
  726. });