gogs.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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 tidbDefault = 'data/gogs_tidb';
  190. var dbType = $(this).val();
  191. if (dbType === "SQLite3" || dbType === "TiDB") {
  192. $('#sql_settings').hide();
  193. $('#pgsql_settings').hide();
  194. $('#sqlite_settings').show();
  195. if (dbType === "SQLite3" && $('#db_path').val() == tidbDefault) {
  196. $('#db_path').val(sqliteDefault);
  197. } else if (dbType === "TiDB" && $('#db_path').val() == sqliteDefault) {
  198. $('#db_path').val(tidbDefault);
  199. }
  200. return;
  201. }
  202. var mysqlDefault = '127.0.0.1:3306';
  203. var postgresDefault = '127.0.0.1:5432';
  204. $('#sqlite_settings').hide();
  205. $('#sql_settings').show();
  206. if (dbType === "PostgreSQL") {
  207. $('#pgsql_settings').show();
  208. if ($('#db_host').val() == mysqlDefault) {
  209. $('#db_host').val(postgresDefault);
  210. }
  211. } else {
  212. $('#pgsql_settings').hide();
  213. if ($('#db_host').val() == postgresDefault) {
  214. $('#db_host').val(mysqlDefault);
  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 = $('#repo-name-change-prompt');
  284. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  285. $prompt.show();
  286. } else {
  287. $prompt.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 $newLabelPanel = $('.new-label.segment');
  310. $('.new-label.button').click(function () {
  311. $newLabelPanel.show();
  312. });
  313. $('.new-label.segment .cancel').click(function () {
  314. $newLabelPanel.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 $issueTitle = $('#issue-title');
  361. var $editInput = $('#edit-title-input input');
  362. var editTitleToggle = function () {
  363. $issueTitle.toggle();
  364. $('.not-in-edit').toggle();
  365. $('#edit-title-input').toggle();
  366. $('.in-edit').toggle();
  367. $editInput.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 ($editInput.val().length == 0 ||
  374. $editInput.val() == $issueTitle.text()) {
  375. $editInput.val($issueTitle.text());
  376. return false;
  377. }
  378. $.post($(this).data('update-url'), {
  379. "_csrf": csrf,
  380. "title": $editInput.val()
  381. },
  382. function (data) {
  383. $editInput.val(data.title);
  384. $issueTitle.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 $editContentZone = $segment.find('.edit-content-zone');
  392. var $renderContent = $segment.find('.render-content');
  393. var $rawContent = $segment.find('.raw-content');
  394. var $textarea;
  395. // Setup new form
  396. if ($editContentZone.html().length == 0) {
  397. $editContentZone.html($('#edit-content-form').html());
  398. $textarea = $segment.find('textarea');
  399. // Give new write/preview data-tab name to distinguish from others
  400. var $editContentForm = $editContentZone.find('.ui.comment.form');
  401. var $tabMenu = $editContentForm.find('.tabular.menu');
  402. $tabMenu.attr('data-write', $editContentZone.data('write'));
  403. $tabMenu.attr('data-preview', $editContentZone.data('preview'));
  404. $tabMenu.find('.write.item').attr('data-tab', $editContentZone.data('write'));
  405. $tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
  406. $editContentForm.find('.write.segment').attr('data-tab', $editContentZone.data('write'));
  407. $editContentForm.find('.preview.segment').attr('data-tab', $editContentZone.data('preview'));
  408. initCommentPreviewTab($editContentForm);
  409. $editContentZone.find('.cancel.button').click(function () {
  410. $renderContent.show();
  411. $editContentZone.hide();
  412. });
  413. $editContentZone.find('.save.button').click(function () {
  414. $renderContent.show();
  415. $editContentZone.hide();
  416. $.post($editContentZone.data('update-url'), {
  417. "_csrf": csrf,
  418. "content": $textarea.val(),
  419. "context": $editContentZone.data('context')
  420. },
  421. function (data) {
  422. if (data.length == 0) {
  423. $renderContent.html($('#no-content').html());
  424. } else {
  425. $renderContent.html(data.content);
  426. emojify.run($renderContent[0]);
  427. $('pre code', $renderContent[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. $editContentZone.show();
  438. $renderContent.hide();
  439. if ($textarea.val().length == 0) {
  440. $textarea.val($rawContent.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 $statusButton = $('#status-button');
  459. $('#edit_area').keyup(function () {
  460. if ($(this).val().length == 0) {
  461. $statusButton.text($statusButton.data('status'))
  462. } else {
  463. $statusButton.text($statusButton.data('status-and-comment'))
  464. }
  465. });
  466. $statusButton.click(function () {
  467. $('#status').val($statusButton.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 $editArea = $('.repository.wiki textarea#edit_area');
  521. if ($editArea.length > 0) {
  522. new SimpleMDE({
  523. autoDownloadFontAwesome: false,
  524. element: $editArea[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($editArea.data('url'), {
  530. "_csrf": csrf,
  531. "mode": "gfm",
  532. "context": $editArea.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 $editArea = $('.repository.issue textarea.edit_area');
  560. if ($editArea.length > 0) {
  561. $editArea.each(function (i, edit_area) {
  562. new SimpleMDE({
  563. autoDownloadFontAwesome: false,
  564. element: edit_area[0],
  565. forceSync: true,
  566. renderingConfig: {
  567. singleLineBreaks: false
  568. },
  569. indentWithTabs: false,
  570. tabSize: 4,
  571. spellChecker: false,
  572. toolbar: ["bold", "italic", "strikethrough", "|",
  573. "code", "quote", "|",
  574. "unordered-list", "ordered-list", "|",
  575. "link", "image", "table"]
  576. })
  577. });
  578. }
  579. }
  580. var simpleMDEditor;
  581. var codeMirrorEditor;
  582. // For IE
  583. String.prototype.endsWith = function (pattern) {
  584. var d = this.length - pattern.length;
  585. return d >= 0 && this.lastIndexOf(pattern) === d;
  586. };
  587. // Adding function to get the cursor position in a text field to jQuery object.
  588. (function ($, undefined) {
  589. $.fn.getCursorPosition = function () {
  590. var el = $(this).get(0);
  591. var pos = 0;
  592. if ('selectionStart' in el) {
  593. pos = el.selectionStart;
  594. } else if ('selection' in document) {
  595. el.focus();
  596. var Sel = document.selection.createRange();
  597. var SelLength = document.selection.createRange().text.length;
  598. Sel.moveStart('character', -el.value.length);
  599. pos = Sel.text.length - SelLength;
  600. }
  601. return pos;
  602. }
  603. })(jQuery);
  604. function setSimpleMDE($editArea) {
  605. if (codeMirrorEditor) {
  606. codeMirrorEditor.toTextArea();
  607. codeMirrorEditor = null;
  608. }
  609. if (simpleMDEditor) {
  610. return true;
  611. }
  612. simpleMDEditor = new SimpleMDE({
  613. autoDownloadFontAwesome: false,
  614. element: $editArea[0],
  615. forceSync: true,
  616. renderingConfig: {
  617. singleLineBreaks: false
  618. },
  619. indentWithTabs: false,
  620. tabSize: 4,
  621. spellChecker: false,
  622. previewRender: function (plainText, preview) { // Async method
  623. setTimeout(function () {
  624. // FIXME: still send render request when return back to edit mode
  625. $.post($editArea.data('url'), {
  626. "_csrf": csrf,
  627. "mode": "gfm",
  628. "context": $editArea.data('context'),
  629. "text": plainText
  630. },
  631. function (data) {
  632. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  633. emojify.run($('.editor-preview')[0]);
  634. }
  635. );
  636. }, 0);
  637. return "Loading...";
  638. },
  639. toolbar: ["bold", "italic", "strikethrough", "|",
  640. "heading-1", "heading-2", "heading-3", "heading-bigger", "heading-smaller", "|",
  641. "code", "quote", "|",
  642. "unordered-list", "ordered-list", "|",
  643. "link", "image", "table", "horizontal-rule", "|",
  644. "clean-block", "preview", "fullscreen", "side-by-side"]
  645. });
  646. return true;
  647. }
  648. function setCodeMirror($editArea) {
  649. if (simpleMDEditor) {
  650. simpleMDEditor.toTextArea();
  651. simpleMDEditor = null;
  652. }
  653. if (codeMirrorEditor) {
  654. return true;
  655. }
  656. codeMirrorEditor = CodeMirror.fromTextArea($editArea[0], {
  657. lineNumbers: true
  658. });
  659. codeMirrorEditor.on("change", function (cm, change) {
  660. $editArea.val(cm.getValue());
  661. });
  662. return true;
  663. }
  664. function initEditor() {
  665. $('.js-quick-pull-choice-option').change(function () {
  666. if ($(this).val() == 'commit-to-new-branch') {
  667. $('.quick-pull-branch-name').show();
  668. $('.quick-pull-branch-name input').prop('required',true);
  669. } else {
  670. $('.quick-pull-branch-name').hide();
  671. $('.quick-pull-branch-name input').prop('required',false);
  672. }
  673. });
  674. var $editFilename = $("#file-name");
  675. $editFilename.keyup(function (e) {
  676. var $section = $('.breadcrumb span.section');
  677. var $divider = $('.breadcrumb div.divider');
  678. if (e.keyCode == 8) {
  679. if ($(this).getCursorPosition() == 0) {
  680. if ($section.length > 0) {
  681. var value = $section.last().find('a').text();
  682. $(this).val(value + $(this).val());
  683. $(this)[0].setSelectionRange(value.length, value.length);
  684. $section.last().remove();
  685. $divider.last().remove();
  686. }
  687. }
  688. }
  689. if (e.keyCode == 191) {
  690. var parts = $(this).val().split('/');
  691. for (var i = 0; i < parts.length; ++i) {
  692. var value = parts[i];
  693. if (i < parts.length - 1) {
  694. if (value.length) {
  695. $('<span class="section"><a href="#">' + value + '</a></span>').insertBefore($(this));
  696. $('<div class="divider"> / </div>').insertBefore($(this));
  697. }
  698. }
  699. else {
  700. $(this).val(value);
  701. }
  702. $(this)[0].setSelectionRange(0, 0);
  703. }
  704. }
  705. var parts = [];
  706. $('.breadcrumb span.section').each(function (i, element) {
  707. element = $(element);
  708. if (element.find('a').length) {
  709. parts.push(element.find('a').text());
  710. } else {
  711. parts.push(element.text());
  712. }
  713. });
  714. if ($(this).val())
  715. parts.push($(this).val());
  716. $('#tree_path').val(parts.join('/'));
  717. }).trigger('keyup');
  718. var $editArea = $('.repository.editor textarea#edit_area');
  719. if (!$editArea.length)
  720. return;
  721. var markdownFileExts = $editArea.data("markdown-file-exts").split(",");
  722. var lineWrapExtensions = $editArea.data("line-wrap-extensions").split(",");
  723. $editFilename.on("keyup", function (e) {
  724. var val = $editFilename.val(), m, mode, spec, extension, extWithDot, previewLink, dataUrl, apiCall;
  725. extension = extWithDot = "";
  726. if (m = /.+\.([^.]+)$/.exec(val)) {
  727. extension = m[1];
  728. extWithDot = "." + extension;
  729. }
  730. var info = CodeMirror.findModeByExtension(extension);
  731. previewLink = $('a[data-tab=preview]');
  732. if (info) {
  733. mode = info.mode;
  734. spec = info.mime;
  735. apiCall = mode;
  736. }
  737. else {
  738. apiCall = extension
  739. }
  740. if (previewLink.length && apiCall && previewFileModes && previewFileModes.length && previewFileModes.indexOf(apiCall) >= 0) {
  741. dataUrl = previewLink.data('url');
  742. previewLink.data('url', dataUrl.replace(/(.*)\/.*/i, '$1/' + mode));
  743. previewLink.show();
  744. }
  745. else {
  746. previewLink.hide();
  747. }
  748. // If this file is a Markdown extensions, we will load that editor and return
  749. if (markdownFileExts.indexOf(extWithDot) >= 0) {
  750. if (setSimpleMDE($editArea)) {
  751. return;
  752. }
  753. }
  754. // Else we are going to use CodeMirror
  755. if (!codeMirrorEditor && !setCodeMirror($editArea)) {
  756. return;
  757. }
  758. if (mode) {
  759. codeMirrorEditor.setOption("mode", spec);
  760. CodeMirror.autoLoadMode(codeMirrorEditor, mode);
  761. }
  762. if (lineWrapExtensions.indexOf(extWithDot) >= 0) {
  763. codeMirrorEditor.setOption("lineWrapping", true);
  764. }
  765. else {
  766. codeMirrorEditor.setOption("lineWrapping", false);
  767. }
  768. }).trigger('keyup');
  769. }
  770. function initOrganization() {
  771. if ($('.organization').length == 0) {
  772. return;
  773. }
  774. // Options
  775. if ($('.organization.settings.options').length > 0) {
  776. $('#org_name').keyup(function () {
  777. var $prompt = $('#org-name-change-prompt');
  778. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  779. $prompt.show();
  780. } else {
  781. $prompt.hide();
  782. }
  783. });
  784. }
  785. }
  786. function initUserSettings() {
  787. console.log('initUserSettings');
  788. // Options
  789. if ($('.user.settings.profile').length > 0) {
  790. $('#username').keyup(function () {
  791. var $prompt = $('#name-change-prompt');
  792. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  793. $prompt.show();
  794. } else {
  795. $prompt.hide();
  796. }
  797. });
  798. }
  799. }
  800. function initWebhook() {
  801. if ($('.new.webhook').length == 0) {
  802. return;
  803. }
  804. $('.events.checkbox input').change(function () {
  805. if ($(this).is(':checked')) {
  806. $('.events.fields').show();
  807. }
  808. });
  809. $('.non-events.checkbox input').change(function () {
  810. if ($(this).is(':checked')) {
  811. $('.events.fields').hide();
  812. }
  813. });
  814. // Test delivery
  815. $('#test-delivery').click(function () {
  816. var $this = $(this);
  817. $this.addClass('loading disabled');
  818. $.post($this.data('link'), {
  819. "_csrf": csrf
  820. }).done(
  821. setTimeout(function () {
  822. window.location.href = $this.data('redirect');
  823. }, 5000)
  824. )
  825. });
  826. }
  827. function initAdmin() {
  828. if ($('.admin').length == 0) {
  829. return;
  830. }
  831. // New user
  832. if ($('.admin.new.user').length > 0 ||
  833. $('.admin.edit.user').length > 0) {
  834. $('#login_type').change(function () {
  835. if ($(this).val().substring(0, 1) == '0') {
  836. $('#login_name').removeAttr('required');
  837. $('.non-local').hide();
  838. $('.local').show();
  839. $('#user_name').focus();
  840. if ($(this).data('password') == "required") {
  841. $('#password').attr('required', 'required');
  842. }
  843. } else {
  844. $('#login_name').attr('required', 'required');
  845. $('.non-local').show();
  846. $('.local').hide();
  847. $('#login_name').focus();
  848. $('#password').removeAttr('required');
  849. }
  850. });
  851. }
  852. function onSecurityProtocolChange() {
  853. if ($('#security_protocol').val() > 0) {
  854. $('.has-tls').show();
  855. } else {
  856. $('.has-tls').hide();
  857. }
  858. }
  859. // New authentication
  860. if ($('.admin.new.authentication').length > 0) {
  861. $('#auth_type').change(function () {
  862. $('.ldap').hide();
  863. $('.dldap').hide();
  864. $('.smtp').hide();
  865. $('.pam').hide();
  866. $('.has-tls').hide();
  867. var authType = $(this).val();
  868. switch (authType) {
  869. case '2': // LDAP
  870. $('.ldap').show();
  871. break;
  872. case '3': // SMTP
  873. $('.smtp').show();
  874. $('.has-tls').show();
  875. break;
  876. case '4': // PAM
  877. $('.pam').show();
  878. break;
  879. case '5': // LDAP
  880. $('.dldap').show();
  881. break;
  882. }
  883. if (authType == '2' || authType == '5') {
  884. onSecurityProtocolChange()
  885. }
  886. });
  887. $('#security_protocol').change(onSecurityProtocolChange)
  888. }
  889. // Edit authentication
  890. if ($('.admin.edit.authentication').length > 0) {
  891. var authType = $('#auth_type').val();
  892. if (authType == '2' || authType == '5') {
  893. $('#security_protocol').change(onSecurityProtocolChange);
  894. }
  895. }
  896. // Notice
  897. if ($('.admin.notice')) {
  898. var $detailModal = $('#detail-modal');
  899. // Attach view detail modals
  900. $('.view-detail').click(function () {
  901. $detailModal.find('.content p').text($(this).data('content'));
  902. $detailModal.modal('show');
  903. return false;
  904. });
  905. // Select actions
  906. var $checkboxes = $('.select.table .ui.checkbox');
  907. $('.select.action').click(function () {
  908. switch ($(this).data('action')) {
  909. case 'select-all':
  910. $checkboxes.checkbox('check');
  911. break;
  912. case 'deselect-all':
  913. $checkboxes.checkbox('uncheck');
  914. break;
  915. case 'inverse':
  916. $checkboxes.checkbox('toggle');
  917. break;
  918. }
  919. });
  920. $('#delete-selection').click(function () {
  921. var $this = $(this);
  922. $this.addClass("loading disabled");
  923. var ids = [];
  924. $checkboxes.each(function () {
  925. if ($(this).checkbox('is checked')) {
  926. ids.push($(this).data('id'));
  927. }
  928. });
  929. $.post($this.data('link'), {
  930. "_csrf": csrf,
  931. "ids": ids
  932. }).done(function () {
  933. window.location.href = $this.data('redirect');
  934. });
  935. });
  936. }
  937. }
  938. function buttonsClickOnEnter() {
  939. $('.ui.button').keypress(function (e) {
  940. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  941. $(this).click();
  942. });
  943. }
  944. function hideWhenLostFocus(body, parent) {
  945. $(document).click(function (e) {
  946. var target = e.target;
  947. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  948. $(body).hide();
  949. }
  950. });
  951. }
  952. function searchUsers() {
  953. if (!$('#search-user-box .results').length) {
  954. return;
  955. }
  956. var $searchUserBox = $('#search-user-box');
  957. var $results = $searchUserBox.find('.results');
  958. $searchUserBox.keyup(function () {
  959. var $this = $(this);
  960. var keyword = $this.find('input').val();
  961. if (keyword.length < 2) {
  962. $results.hide();
  963. return;
  964. }
  965. $.ajax({
  966. url: suburl + '/api/v1/users/search?q=' + keyword,
  967. dataType: "json",
  968. success: function (response) {
  969. var notEmpty = function (str) {
  970. return str && str.length > 0;
  971. };
  972. $results.html('');
  973. if (response.ok && response.data.length) {
  974. var html = '';
  975. $.each(response.data, function (i, item) {
  976. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  977. if (notEmpty(item.full_name)) {
  978. html += ' (' + item.full_name + ')';
  979. }
  980. html += '</div>';
  981. });
  982. $results.html(html);
  983. $this.find('.results .item').click(function () {
  984. $this.find('input').val($(this).find('.username').text());
  985. $results.hide();
  986. });
  987. $results.show();
  988. } else {
  989. $results.hide();
  990. }
  991. }
  992. });
  993. });
  994. $searchUserBox.find('input').focus(function () {
  995. $searchUserBox.keyup();
  996. });
  997. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  998. }
  999. // FIXME: merge common parts in two functions
  1000. function searchRepositories() {
  1001. if (!$('#search-repo-box .results').length) {
  1002. return;
  1003. }
  1004. var $searchRepoBox = $('#search-repo-box');
  1005. var $results = $searchRepoBox.find('.results');
  1006. $searchRepoBox.keyup(function () {
  1007. var $this = $(this);
  1008. var keyword = $this.find('input').val();
  1009. if (keyword.length < 2) {
  1010. $results.hide();
  1011. return;
  1012. }
  1013. $.ajax({
  1014. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $searchRepoBox.data('uid'),
  1015. dataType: "json",
  1016. success: function (response) {
  1017. var notEmpty = function (str) {
  1018. return str && str.length > 0;
  1019. };
  1020. $results.html('');
  1021. if (response.ok && response.data.length) {
  1022. var html = '';
  1023. $.each(response.data, function (i, item) {
  1024. html += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  1025. });
  1026. $results.html(html);
  1027. $this.find('.results .item').click(function () {
  1028. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  1029. $results.hide();
  1030. });
  1031. $results.show();
  1032. } else {
  1033. $results.hide();
  1034. }
  1035. }
  1036. });
  1037. });
  1038. $searchRepoBox.find('input').focus(function () {
  1039. $searchRepoBox.keyup();
  1040. });
  1041. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  1042. }
  1043. function initCodeView() {
  1044. if ($('.code-view .linenums').length > 0) {
  1045. $(document).on('click', '.lines-num span', function (e) {
  1046. var $select = $(this);
  1047. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  1048. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  1049. deSelect();
  1050. });
  1051. $(window).on('hashchange', function (e) {
  1052. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1053. var $list = $('.code-view ol.linenums > li');
  1054. var $first;
  1055. if (m) {
  1056. $first = $list.filter('.' + m[1]);
  1057. selectRange($list, $first, $list.filter('.' + m[2]));
  1058. $("html, body").scrollTop($first.offset().top - 200);
  1059. return;
  1060. }
  1061. m = window.location.hash.match(/^#(L\d+)$/);
  1062. if (m) {
  1063. $first = $list.filter('.' + m[1]);
  1064. selectRange($list, $first);
  1065. $("html, body").scrollTop($first.offset().top - 200);
  1066. }
  1067. }).trigger('hashchange');
  1068. }
  1069. }
  1070. $(document).ready(function () {
  1071. csrf = $('meta[name=_csrf]').attr("content");
  1072. suburl = $('meta[name=_suburl]').attr("content");
  1073. // Show exact time
  1074. $('.time-since').each(function () {
  1075. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  1076. });
  1077. // Semantic UI modules.
  1078. $('.dropdown').dropdown();
  1079. $('.jump.dropdown').dropdown({
  1080. action: 'hide',
  1081. onShow: function () {
  1082. $('.poping.up').popup('hide');
  1083. }
  1084. });
  1085. $('.slide.up.dropdown').dropdown({
  1086. transition: 'slide up'
  1087. });
  1088. $('.upward.dropdown').dropdown({
  1089. direction: 'upward'
  1090. });
  1091. $('.ui.accordion').accordion();
  1092. $('.ui.checkbox').checkbox();
  1093. $('.ui.progress').progress({
  1094. showActivity: false
  1095. });
  1096. $('.poping.up').popup();
  1097. $('.top.menu .poping.up').popup({
  1098. onShow: function () {
  1099. if ($('.top.menu .menu.transition').hasClass('visible')) {
  1100. return false;
  1101. }
  1102. }
  1103. });
  1104. $('.tabular.menu .item').tab();
  1105. $('.tabable.menu .item').tab();
  1106. $('.toggle.button').click(function () {
  1107. $($(this).data('target')).slideToggle(100);
  1108. });
  1109. // Highlight JS
  1110. if (typeof hljs != 'undefined') {
  1111. hljs.initHighlightingOnLoad();
  1112. }
  1113. // Dropzone
  1114. var $dropzone = $('#dropzone');
  1115. if ($dropzone.length > 0) {
  1116. // Disable auto discover for all elements:
  1117. Dropzone.autoDiscover = false;
  1118. var filenameDict = {};
  1119. $dropzone.dropzone({
  1120. url: $dropzone.data('upload-url'),
  1121. headers: {"X-Csrf-Token": csrf},
  1122. maxFiles: $dropzone.data('max-file'),
  1123. maxFilesize: $dropzone.data('max-size'),
  1124. acceptedFiles: ($dropzone.data('accepts') === '*/*') ? null : $dropzone.data('accepts'),
  1125. addRemoveLinks: true,
  1126. dictDefaultMessage: $dropzone.data('default-message'),
  1127. dictInvalidFileType: $dropzone.data('invalid-input-type'),
  1128. dictFileTooBig: $dropzone.data('file-too-big'),
  1129. dictRemoveFile: $dropzone.data('remove-file'),
  1130. init: function () {
  1131. this.on("success", function (file, data) {
  1132. filenameDict[file.name] = data.uuid;
  1133. var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
  1134. $('.files').append(input);
  1135. });
  1136. this.on("removedfile", function (file) {
  1137. if (file.name in filenameDict) {
  1138. $('#' + filenameDict[file.name]).remove();
  1139. }
  1140. if ($dropzone.data('remove-url') && $dropzone.data('csrf')) {
  1141. $.post($dropzone.data('remove-url'), {
  1142. file: filenameDict[file.name],
  1143. _csrf: $dropzone.data('csrf')
  1144. });
  1145. }
  1146. })
  1147. }
  1148. });
  1149. }
  1150. // Emojify
  1151. emojify.setConfig({
  1152. img_dir: suburl + '/img/emoji',
  1153. ignore_emoticons: true
  1154. });
  1155. var hasEmoji = document.getElementsByClassName('has-emoji');
  1156. for (var i = 0; i < hasEmoji.length; i++) {
  1157. emojify.run(hasEmoji[i]);
  1158. }
  1159. // Clipboard JS
  1160. var clipboard = new Clipboard('.clipboard');
  1161. clipboard.on('success', function (e) {
  1162. e.clearSelection();
  1163. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1164. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  1165. $('#' + e.trigger.getAttribute('id')).popup('show');
  1166. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1167. });
  1168. clipboard.on('error', function (e) {
  1169. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1170. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  1171. $('#' + e.trigger.getAttribute('id')).popup('show');
  1172. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1173. });
  1174. // Helpers.
  1175. $('.delete-button').click(function () {
  1176. var $this = $(this);
  1177. $('.delete.modal').modal({
  1178. closable: false,
  1179. onApprove: function () {
  1180. if ($this.data('type') == "form") {
  1181. $($this.data('form')).submit();
  1182. return;
  1183. }
  1184. $.post($this.data('url'), {
  1185. "_csrf": csrf,
  1186. "id": $this.data("id")
  1187. }).done(function (data) {
  1188. window.location.href = data.redirect;
  1189. });
  1190. }
  1191. }).modal('show');
  1192. return false;
  1193. });
  1194. $('.show-panel.button').click(function () {
  1195. $($(this).data('panel')).show();
  1196. });
  1197. $('.show-modal.button').click(function () {
  1198. $($(this).data('modal')).modal('show');
  1199. });
  1200. $('.delete-post.button').click(function () {
  1201. var $this = $(this);
  1202. $.post($this.data('request-url'), {
  1203. "_csrf": csrf
  1204. }).done(function () {
  1205. window.location.href = $this.data('done-url');
  1206. });
  1207. });
  1208. // Set anchor.
  1209. $('.markdown').each(function () {
  1210. var headers = {};
  1211. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  1212. var node = $(this);
  1213. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  1214. var name = val;
  1215. if (headers[val] > 0) {
  1216. name = val + '-' + headers[val];
  1217. }
  1218. if (headers[val] == undefined) {
  1219. headers[val] = 1;
  1220. } else {
  1221. headers[val] += 1;
  1222. }
  1223. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1224. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  1225. });
  1226. });
  1227. buttonsClickOnEnter();
  1228. searchUsers();
  1229. searchRepositories();
  1230. initCommentForm();
  1231. initInstall();
  1232. initRepository();
  1233. initWikiForm();
  1234. initIssueForm();
  1235. initEditForm();
  1236. initEditor();
  1237. initOrganization();
  1238. initWebhook();
  1239. initAdmin();
  1240. initCodeView();
  1241. // Repo clone url.
  1242. if ($('#repo-clone-url').length > 0) {
  1243. switch (localStorage.getItem('repo-clone-protocol')) {
  1244. case 'ssh':
  1245. if ($('#repo-clone-ssh').click().length === 0) {
  1246. $('#repo-clone-https').click();
  1247. }
  1248. break;
  1249. default:
  1250. $('#repo-clone-https').click();
  1251. break;
  1252. }
  1253. }
  1254. var routes = {
  1255. 'div.user.settings': initUserSettings,
  1256. 'div.repository.settings.collaboration': initRepositoryCollaboration
  1257. };
  1258. var selector;
  1259. for (selector in routes) {
  1260. if ($(selector).length > 0) {
  1261. routes[selector]();
  1262. break;
  1263. }
  1264. }
  1265. });
  1266. function changeHash(hash) {
  1267. if (history.pushState) {
  1268. history.pushState(null, null, hash);
  1269. }
  1270. else {
  1271. location.hash = hash;
  1272. }
  1273. }
  1274. function deSelect() {
  1275. if (window.getSelection) {
  1276. window.getSelection().removeAllRanges();
  1277. } else {
  1278. document.selection.empty();
  1279. }
  1280. }
  1281. function selectRange($list, $select, $from) {
  1282. $list.removeClass('active');
  1283. if ($from) {
  1284. var a = parseInt($select.attr('rel').substr(1));
  1285. var b = parseInt($from.attr('rel').substr(1));
  1286. var c;
  1287. if (a != b) {
  1288. if (a > b) {
  1289. c = a;
  1290. a = b;
  1291. b = c;
  1292. }
  1293. var classes = [];
  1294. for (var i = a; i <= b; i++) {
  1295. classes.push('.L' + i);
  1296. }
  1297. $list.filter(classes.join(',')).addClass('active');
  1298. changeHash('#L' + a + '-' + 'L' + b);
  1299. return
  1300. }
  1301. }
  1302. $select.addClass('active');
  1303. changeHash('#' + $select.attr('rel'));
  1304. }
  1305. $(function () {
  1306. if ($('.user.signin').length > 0) return;
  1307. $('form').areYouSure();
  1308. });