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. var $editFilename = $("#file-name");
  666. $editFilename.keyup(function (e) {
  667. var $section = $('.breadcrumb span.section');
  668. var $divider = $('.breadcrumb div.divider');
  669. if (e.keyCode == 8) {
  670. if ($(this).getCursorPosition() == 0) {
  671. if ($section.length > 0) {
  672. var value = $section.last().find('a').text();
  673. $(this).val(value + $(this).val());
  674. $(this)[0].setSelectionRange(value.length, value.length);
  675. $section.last().remove();
  676. $divider.last().remove();
  677. }
  678. }
  679. }
  680. if (e.keyCode == 191) {
  681. var parts = $(this).val().split('/');
  682. for (var i = 0; i < parts.length; ++i) {
  683. var value = parts[i];
  684. if (i < parts.length - 1) {
  685. if (value.length) {
  686. $('<span class="section"><a href="#">' + value + '</a></span>').insertBefore($(this));
  687. $('<div class="divider"> / </div>').insertBefore($(this));
  688. }
  689. }
  690. else {
  691. $(this).val(value);
  692. }
  693. $(this)[0].setSelectionRange(0, 0);
  694. }
  695. }
  696. var parts = [];
  697. $('.breadcrumb span.section').each(function (i, element) {
  698. element = $(element);
  699. if (element.find('a').length) {
  700. parts.push(element.find('a').text());
  701. } else {
  702. parts.push(element.text());
  703. }
  704. });
  705. if ($(this).val())
  706. parts.push($(this).val());
  707. $('#tree_path').val(parts.join('/'));
  708. }).trigger('keyup');
  709. var $editArea = $('.repository.editor textarea#edit_area');
  710. if (!$editArea.length)
  711. return;
  712. var markdownFileExts = $editArea.data("markdown-file-exts").split(",");
  713. var lineWrapExtensions = $editArea.data("line-wrap-extensions").split(",");
  714. $editFilename.on("keyup", function (e) {
  715. var val = $editFilename.val(), m, mode, spec, extension, extWithDot, previewLink, dataUrl, apiCall;
  716. extension = extWithDot = "";
  717. if (m = /.+\.([^.]+)$/.exec(val)) {
  718. extension = m[1];
  719. extWithDot = "." + extension;
  720. }
  721. var info = CodeMirror.findModeByExtension(extension);
  722. previewLink = $('a[data-tab=preview]');
  723. if (info) {
  724. mode = info.mode;
  725. spec = info.mime;
  726. apiCall = mode;
  727. }
  728. else {
  729. apiCall = extension
  730. }
  731. if (previewLink.length && apiCall && previewFileModes && previewFileModes.length && previewFileModes.indexOf(apiCall) >= 0) {
  732. dataUrl = previewLink.data('url');
  733. previewLink.data('url', dataUrl.replace(/(.*)\/.*/i, '$1/' + mode));
  734. previewLink.show();
  735. }
  736. else {
  737. previewLink.hide();
  738. }
  739. // If this file is a Markdown extensions, we will load that editor and return
  740. if (markdownFileExts.indexOf(extWithDot) >= 0) {
  741. if (setSimpleMDE($editArea)) {
  742. return;
  743. }
  744. }
  745. // Else we are going to use CodeMirror
  746. if (!codeMirrorEditor && !setCodeMirror($editArea)) {
  747. return;
  748. }
  749. if (mode) {
  750. codeMirrorEditor.setOption("mode", spec);
  751. CodeMirror.autoLoadMode(codeMirrorEditor, mode);
  752. }
  753. if (lineWrapExtensions.indexOf(extWithDot) >= 0) {
  754. codeMirrorEditor.setOption("lineWrapping", true);
  755. }
  756. else {
  757. codeMirrorEditor.setOption("lineWrapping", false);
  758. }
  759. }).trigger('keyup');
  760. $('.js-quick-pull-choice-option').change(function () {
  761. if ($(this).val() == 'commit-to-new-branch') {
  762. $('.quick-pull-branch-name').show();
  763. } else {
  764. $('.quick-pull-branch-name').hide();
  765. }
  766. });
  767. }
  768. function initOrganization() {
  769. if ($('.organization').length == 0) {
  770. return;
  771. }
  772. // Options
  773. if ($('.organization.settings.options').length > 0) {
  774. $('#org_name').keyup(function () {
  775. var $prompt = $('#org-name-change-prompt');
  776. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  777. $prompt.show();
  778. } else {
  779. $prompt.hide();
  780. }
  781. });
  782. }
  783. }
  784. function initUserSettings() {
  785. console.log('initUserSettings');
  786. // Options
  787. if ($('.user.settings.profile').length > 0) {
  788. $('#username').keyup(function () {
  789. var $prompt = $('#name-change-prompt');
  790. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  791. $prompt.show();
  792. } else {
  793. $prompt.hide();
  794. }
  795. });
  796. }
  797. }
  798. function initWebhook() {
  799. if ($('.new.webhook').length == 0) {
  800. return;
  801. }
  802. $('.events.checkbox input').change(function () {
  803. if ($(this).is(':checked')) {
  804. $('.events.fields').show();
  805. }
  806. });
  807. $('.non-events.checkbox input').change(function () {
  808. if ($(this).is(':checked')) {
  809. $('.events.fields').hide();
  810. }
  811. });
  812. // Test delivery
  813. $('#test-delivery').click(function () {
  814. var $this = $(this);
  815. $this.addClass('loading disabled');
  816. $.post($this.data('link'), {
  817. "_csrf": csrf
  818. }).done(
  819. setTimeout(function () {
  820. window.location.href = $this.data('redirect');
  821. }, 5000)
  822. )
  823. });
  824. }
  825. function initAdmin() {
  826. if ($('.admin').length == 0) {
  827. return;
  828. }
  829. // New user
  830. if ($('.admin.new.user').length > 0 ||
  831. $('.admin.edit.user').length > 0) {
  832. $('#login_type').change(function () {
  833. if ($(this).val().substring(0, 1) == '0') {
  834. $('#login_name').removeAttr('required');
  835. $('.non-local').hide();
  836. $('.local').show();
  837. $('#user_name').focus();
  838. if ($(this).data('password') == "required") {
  839. $('#password').attr('required', 'required');
  840. }
  841. } else {
  842. $('#login_name').attr('required', 'required');
  843. $('.non-local').show();
  844. $('.local').hide();
  845. $('#login_name').focus();
  846. $('#password').removeAttr('required');
  847. }
  848. });
  849. }
  850. function onSecurityProtocolChange() {
  851. if ($('#security_protocol').val() > 0) {
  852. $('.has-tls').show();
  853. } else {
  854. $('.has-tls').hide();
  855. }
  856. }
  857. // New authentication
  858. if ($('.admin.new.authentication').length > 0) {
  859. $('#auth_type').change(function () {
  860. $('.ldap').hide();
  861. $('.dldap').hide();
  862. $('.smtp').hide();
  863. $('.pam').hide();
  864. $('.has-tls').hide();
  865. var authType = $(this).val();
  866. switch (authType) {
  867. case '2': // LDAP
  868. $('.ldap').show();
  869. break;
  870. case '3': // SMTP
  871. $('.smtp').show();
  872. $('.has-tls').show();
  873. break;
  874. case '4': // PAM
  875. $('.pam').show();
  876. break;
  877. case '5': // LDAP
  878. $('.dldap').show();
  879. break;
  880. }
  881. if (authType == '2' || authType == '5') {
  882. onSecurityProtocolChange()
  883. }
  884. });
  885. $('#security_protocol').change(onSecurityProtocolChange)
  886. }
  887. // Edit authentication
  888. if ($('.admin.edit.authentication').length > 0) {
  889. var authType = $('#auth_type').val();
  890. if (authType == '2' || authType == '5') {
  891. $('#security_protocol').change(onSecurityProtocolChange);
  892. }
  893. }
  894. // Notice
  895. if ($('.admin.notice')) {
  896. var $detailModal = $('#detail-modal');
  897. // Attach view detail modals
  898. $('.view-detail').click(function () {
  899. $detailModal.find('.content p').text($(this).data('content'));
  900. $detailModal.modal('show');
  901. return false;
  902. });
  903. // Select actions
  904. var $checkboxes = $('.select.table .ui.checkbox');
  905. $('.select.action').click(function () {
  906. switch ($(this).data('action')) {
  907. case 'select-all':
  908. $checkboxes.checkbox('check');
  909. break;
  910. case 'deselect-all':
  911. $checkboxes.checkbox('uncheck');
  912. break;
  913. case 'inverse':
  914. $checkboxes.checkbox('toggle');
  915. break;
  916. }
  917. });
  918. $('#delete-selection').click(function () {
  919. var $this = $(this);
  920. $this.addClass("loading disabled");
  921. var ids = [];
  922. $checkboxes.each(function () {
  923. if ($(this).checkbox('is checked')) {
  924. ids.push($(this).data('id'));
  925. }
  926. });
  927. $.post($this.data('link'), {
  928. "_csrf": csrf,
  929. "ids": ids
  930. }).done(function () {
  931. window.location.href = $this.data('redirect');
  932. });
  933. });
  934. }
  935. }
  936. function buttonsClickOnEnter() {
  937. $('.ui.button').keypress(function (e) {
  938. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  939. $(this).click();
  940. });
  941. }
  942. function hideWhenLostFocus(body, parent) {
  943. $(document).click(function (e) {
  944. var target = e.target;
  945. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  946. $(body).hide();
  947. }
  948. });
  949. }
  950. function searchUsers() {
  951. if (!$('#search-user-box .results').length) {
  952. return;
  953. }
  954. var $searchUserBox = $('#search-user-box');
  955. var $results = $searchUserBox.find('.results');
  956. $searchUserBox.keyup(function () {
  957. var $this = $(this);
  958. var keyword = $this.find('input').val();
  959. if (keyword.length < 2) {
  960. $results.hide();
  961. return;
  962. }
  963. $.ajax({
  964. url: suburl + '/api/v1/users/search?q=' + keyword,
  965. dataType: "json",
  966. success: function (response) {
  967. var notEmpty = function (str) {
  968. return str && str.length > 0;
  969. };
  970. $results.html('');
  971. if (response.ok && response.data.length) {
  972. var html = '';
  973. $.each(response.data, function (i, item) {
  974. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  975. if (notEmpty(item.full_name)) {
  976. html += ' (' + item.full_name + ')';
  977. }
  978. html += '</div>';
  979. });
  980. $results.html(html);
  981. $this.find('.results .item').click(function () {
  982. $this.find('input').val($(this).find('.username').text());
  983. $results.hide();
  984. });
  985. $results.show();
  986. } else {
  987. $results.hide();
  988. }
  989. }
  990. });
  991. });
  992. $searchUserBox.find('input').focus(function () {
  993. $searchUserBox.keyup();
  994. });
  995. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  996. }
  997. // FIXME: merge common parts in two functions
  998. function searchRepositories() {
  999. if (!$('#search-repo-box .results').length) {
  1000. return;
  1001. }
  1002. var $searchRepoBox = $('#search-repo-box');
  1003. var $results = $searchRepoBox.find('.results');
  1004. $searchRepoBox.keyup(function () {
  1005. var $this = $(this);
  1006. var keyword = $this.find('input').val();
  1007. if (keyword.length < 2) {
  1008. $results.hide();
  1009. return;
  1010. }
  1011. $.ajax({
  1012. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $searchRepoBox.data('uid'),
  1013. dataType: "json",
  1014. success: function (response) {
  1015. var notEmpty = function (str) {
  1016. return str && str.length > 0;
  1017. };
  1018. $results.html('');
  1019. if (response.ok && response.data.length) {
  1020. var html = '';
  1021. $.each(response.data, function (i, item) {
  1022. html += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  1023. });
  1024. $results.html(html);
  1025. $this.find('.results .item').click(function () {
  1026. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  1027. $results.hide();
  1028. });
  1029. $results.show();
  1030. } else {
  1031. $results.hide();
  1032. }
  1033. }
  1034. });
  1035. });
  1036. $searchRepoBox.find('input').focus(function () {
  1037. $searchRepoBox.keyup();
  1038. });
  1039. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  1040. }
  1041. function initCodeView() {
  1042. if ($('.code-view .linenums').length > 0) {
  1043. $(document).on('click', '.lines-num span', function (e) {
  1044. var $select = $(this);
  1045. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  1046. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  1047. deSelect();
  1048. });
  1049. $(window).on('hashchange', function (e) {
  1050. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1051. var $list = $('.code-view ol.linenums > li');
  1052. var $first;
  1053. if (m) {
  1054. $first = $list.filter('.' + m[1]);
  1055. selectRange($list, $first, $list.filter('.' + m[2]));
  1056. $("html, body").scrollTop($first.offset().top - 200);
  1057. return;
  1058. }
  1059. m = window.location.hash.match(/^#(L\d+)$/);
  1060. if (m) {
  1061. $first = $list.filter('.' + m[1]);
  1062. selectRange($list, $first);
  1063. $("html, body").scrollTop($first.offset().top - 200);
  1064. }
  1065. }).trigger('hashchange');
  1066. }
  1067. }
  1068. $(document).ready(function () {
  1069. csrf = $('meta[name=_csrf]').attr("content");
  1070. suburl = $('meta[name=_suburl]').attr("content");
  1071. // Show exact time
  1072. $('.time-since').each(function () {
  1073. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  1074. });
  1075. // Semantic UI modules.
  1076. $('.dropdown').dropdown();
  1077. $('.jump.dropdown').dropdown({
  1078. action: 'hide',
  1079. onShow: function () {
  1080. $('.poping.up').popup('hide');
  1081. }
  1082. });
  1083. $('.slide.up.dropdown').dropdown({
  1084. transition: 'slide up'
  1085. });
  1086. $('.upward.dropdown').dropdown({
  1087. direction: 'upward'
  1088. });
  1089. $('.ui.accordion').accordion();
  1090. $('.ui.checkbox').checkbox();
  1091. $('.ui.progress').progress({
  1092. showActivity: false
  1093. });
  1094. $('.poping.up').popup();
  1095. $('.top.menu .poping.up').popup({
  1096. onShow: function () {
  1097. if ($('.top.menu .menu.transition').hasClass('visible')) {
  1098. return false;
  1099. }
  1100. }
  1101. });
  1102. $('.tabular.menu .item').tab();
  1103. $('.tabable.menu .item').tab();
  1104. $('.toggle.button').click(function () {
  1105. $($(this).data('target')).slideToggle(100);
  1106. });
  1107. // Highlight JS
  1108. if (typeof hljs != 'undefined') {
  1109. hljs.initHighlightingOnLoad();
  1110. }
  1111. // Dropzone
  1112. var $dropzone = $('#dropzone');
  1113. if ($dropzone.length > 0) {
  1114. // Disable auto discover for all elements:
  1115. Dropzone.autoDiscover = false;
  1116. var filenameDict = {};
  1117. $dropzone.dropzone({
  1118. url: $dropzone.data('upload-url'),
  1119. headers: {"X-Csrf-Token": csrf},
  1120. maxFiles: $dropzone.data('max-file'),
  1121. maxFilesize: $dropzone.data('max-size'),
  1122. acceptedFiles: ($dropzone.data('accepts') === '*/*') ? null : $dropzone.data('accepts'),
  1123. addRemoveLinks: true,
  1124. dictDefaultMessage: $dropzone.data('default-message'),
  1125. dictInvalidFileType: $dropzone.data('invalid-input-type'),
  1126. dictFileTooBig: $dropzone.data('file-too-big'),
  1127. dictRemoveFile: $dropzone.data('remove-file'),
  1128. init: function () {
  1129. this.on("success", function (file, data) {
  1130. filenameDict[file.name] = data.uuid;
  1131. var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
  1132. $('.files').append(input);
  1133. });
  1134. this.on("removedfile", function (file) {
  1135. if (file.name in filenameDict) {
  1136. $('#' + filenameDict[file.name]).remove();
  1137. }
  1138. if ($dropzone.data('remove-url') && $dropzone.data('csrf')) {
  1139. $.post($dropzone.data('remove-url'), {
  1140. file: filenameDict[file.name],
  1141. _csrf: $dropzone.data('csrf')
  1142. });
  1143. }
  1144. })
  1145. }
  1146. });
  1147. }
  1148. // Emojify
  1149. emojify.setConfig({
  1150. img_dir: suburl + '/img/emoji',
  1151. ignore_emoticons: true
  1152. });
  1153. var hasEmoji = document.getElementsByClassName('has-emoji');
  1154. for (var i = 0; i < hasEmoji.length; i++) {
  1155. emojify.run(hasEmoji[i]);
  1156. }
  1157. // Clipboard JS
  1158. var clipboard = new Clipboard('.clipboard');
  1159. clipboard.on('success', function (e) {
  1160. e.clearSelection();
  1161. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1162. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  1163. $('#' + e.trigger.getAttribute('id')).popup('show');
  1164. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1165. });
  1166. clipboard.on('error', function (e) {
  1167. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1168. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  1169. $('#' + e.trigger.getAttribute('id')).popup('show');
  1170. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1171. });
  1172. // Helpers.
  1173. $('.delete-button').click(function () {
  1174. var $this = $(this);
  1175. $('.delete.modal').modal({
  1176. closable: false,
  1177. onApprove: function () {
  1178. if ($this.data('type') == "form") {
  1179. $($this.data('form')).submit();
  1180. return;
  1181. }
  1182. $.post($this.data('url'), {
  1183. "_csrf": csrf,
  1184. "id": $this.data("id")
  1185. }).done(function (data) {
  1186. window.location.href = data.redirect;
  1187. });
  1188. }
  1189. }).modal('show');
  1190. return false;
  1191. });
  1192. $('.show-panel.button').click(function () {
  1193. $($(this).data('panel')).show();
  1194. });
  1195. $('.show-modal.button').click(function () {
  1196. $($(this).data('modal')).modal('show');
  1197. });
  1198. $('.delete-post.button').click(function () {
  1199. var $this = $(this);
  1200. $.post($this.data('request-url'), {
  1201. "_csrf": csrf
  1202. }).done(function () {
  1203. window.location.href = $this.data('done-url');
  1204. });
  1205. });
  1206. // Set anchor.
  1207. $('.markdown').each(function () {
  1208. var headers = {};
  1209. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  1210. var node = $(this);
  1211. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  1212. var name = val;
  1213. if (headers[val] > 0) {
  1214. name = val + '-' + headers[val];
  1215. }
  1216. if (headers[val] == undefined) {
  1217. headers[val] = 1;
  1218. } else {
  1219. headers[val] += 1;
  1220. }
  1221. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1222. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  1223. });
  1224. });
  1225. buttonsClickOnEnter();
  1226. searchUsers();
  1227. searchRepositories();
  1228. initCommentForm();
  1229. initInstall();
  1230. initRepository();
  1231. initWikiForm();
  1232. initIssueForm();
  1233. initEditForm();
  1234. initEditor();
  1235. initOrganization();
  1236. initWebhook();
  1237. initAdmin();
  1238. initCodeView();
  1239. // Repo clone url.
  1240. if ($('#repo-clone-url').length > 0) {
  1241. switch (localStorage.getItem('repo-clone-protocol')) {
  1242. case 'ssh':
  1243. if ($('#repo-clone-ssh').click().length === 0) {
  1244. $('#repo-clone-https').click();
  1245. }
  1246. break;
  1247. default:
  1248. $('#repo-clone-https').click();
  1249. break;
  1250. }
  1251. }
  1252. var routes = {
  1253. 'div.user.settings': initUserSettings,
  1254. 'div.repository.settings.collaboration': initRepositoryCollaboration
  1255. };
  1256. var selector;
  1257. for (selector in routes) {
  1258. if ($(selector).length > 0) {
  1259. routes[selector]();
  1260. break;
  1261. }
  1262. }
  1263. });
  1264. function changeHash(hash) {
  1265. if (history.pushState) {
  1266. history.pushState(null, null, hash);
  1267. }
  1268. else {
  1269. location.hash = hash;
  1270. }
  1271. }
  1272. function deSelect() {
  1273. if (window.getSelection) {
  1274. window.getSelection().removeAllRanges();
  1275. } else {
  1276. document.selection.empty();
  1277. }
  1278. }
  1279. function selectRange($list, $select, $from) {
  1280. $list.removeClass('active');
  1281. if ($from) {
  1282. var a = parseInt($select.attr('rel').substr(1));
  1283. var b = parseInt($from.attr('rel').substr(1));
  1284. var c;
  1285. if (a != b) {
  1286. if (a > b) {
  1287. c = a;
  1288. a = b;
  1289. b = c;
  1290. }
  1291. var classes = [];
  1292. for (var i = a; i <= b; i++) {
  1293. classes.push('.L' + i);
  1294. }
  1295. $list.filter(classes.join(',')).addClass('active');
  1296. changeHash('#L' + a + '-' + 'L' + b);
  1297. return
  1298. }
  1299. }
  1300. $select.addClass('active');
  1301. changeHash('#' + $select.attr('rel'));
  1302. }
  1303. $(function () {
  1304. if ($('.user.signin').length > 0) return;
  1305. $('form').areYouSure();
  1306. });