您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DocsPage.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.util.List;
  18. import org.apache.wicket.Component;
  19. import org.apache.wicket.PageParameters;
  20. import org.apache.wicket.behavior.SimpleAttributeModifier;
  21. import org.apache.wicket.markup.html.basic.Label;
  22. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  23. import org.apache.wicket.markup.html.link.ExternalLink;
  24. import org.apache.wicket.markup.html.panel.Fragment;
  25. import org.apache.wicket.markup.repeater.Item;
  26. import org.apache.wicket.markup.repeater.data.DataView;
  27. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  28. import org.apache.wicket.model.StringResourceModel;
  29. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  30. import org.eclipse.jgit.lib.Repository;
  31. import org.eclipse.jgit.revwalk.RevCommit;
  32. import com.gitblit.models.PathModel;
  33. import com.gitblit.models.UserModel;
  34. import com.gitblit.servlet.RawServlet;
  35. import com.gitblit.utils.ByteFormat;
  36. import com.gitblit.utils.JGitUtils;
  37. import com.gitblit.utils.StringUtils;
  38. import com.gitblit.wicket.CacheControl;
  39. import com.gitblit.wicket.GitBlitWebSession;
  40. import com.gitblit.wicket.CacheControl.LastModified;
  41. import com.gitblit.wicket.MarkupProcessor;
  42. import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
  43. import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
  44. import com.gitblit.wicket.WicketUtils;
  45. import com.gitblit.wicket.panels.LinkPanel;
  46. @CacheControl(LastModified.REPOSITORY)
  47. public class DocsPage extends RepositoryPage {
  48. public DocsPage(PageParameters params) {
  49. super(params);
  50. String objectId = WicketUtils.getObject(params);
  51. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  52. Repository r = getRepository();
  53. UserModel currentUser = (GitBlitWebSession.get().getUser() != null) ? GitBlitWebSession.get().getUser() : UserModel.ANONYMOUS;
  54. final boolean userCanEdit = currentUser.canEdit(getRepositoryModel());
  55. RevCommit head = JGitUtils.getCommit(r, objectId);
  56. final String commitId = getBestCommitId(head);
  57. List<String> extensions = processor.getAllExtensions();
  58. List<PathModel> paths = JGitUtils.getDocuments(r, extensions);
  59. List<MarkupDocument> roots = processor.getRootDocs(r, repositoryName, commitId);
  60. Fragment fragment = null;
  61. if (roots.isEmpty()) {
  62. // no identified root documents
  63. fragment = new Fragment("docs", "noIndexFragment", this);
  64. setResponsePage(NoDocsPage.class, params);
  65. } else {
  66. // root documents, use tabbed ui of index/root and document list
  67. fragment = new Fragment("docs", "tabsFragment", this);
  68. ListDataProvider<MarkupDocument> docDp = new ListDataProvider<MarkupDocument>(roots);
  69. // tab titles
  70. DataView<MarkupDocument> tabTitles = new DataView<MarkupDocument>("tabTitle", docDp) {
  71. private static final long serialVersionUID = 1L;
  72. int counter;
  73. @Override
  74. public void populateItem(final Item<MarkupDocument> item) {
  75. MarkupDocument doc = item.getModelObject();
  76. String file = StringUtils.getLastPathElement(doc.documentPath);
  77. file = StringUtils.stripFileExtension(file);
  78. String name = file.replace('_', ' ').replace('-', ' ');
  79. ExternalLink link = new ExternalLink("link", "#" + file);
  80. link.add(new Label("label", name.toUpperCase()).setRenderBodyOnly(true));
  81. item.add(link);
  82. if (counter == 0) {
  83. counter++;
  84. item.add(new SimpleAttributeModifier("class", "active"));
  85. }
  86. }
  87. };
  88. fragment.add(tabTitles);
  89. // tab content
  90. DataView<MarkupDocument> tabsView = new DataView<MarkupDocument>("tabContent", docDp) {
  91. private static final long serialVersionUID = 1L;
  92. int counter;
  93. @Override
  94. public void populateItem(final Item<MarkupDocument> item) {
  95. MarkupDocument doc = item.getModelObject();
  96. item.add(new BookmarkablePageLink<Void>("editLink", EditFilePage.class,
  97. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath))
  98. .setEnabled(userCanEdit));
  99. // document page links
  100. item.add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  101. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  102. item.add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  103. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  104. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, doc.documentPath);
  105. item.add(new ExternalLink("rawLink", rawUrl));
  106. // document content
  107. String file = StringUtils.getLastPathElement(doc.documentPath);
  108. file = StringUtils.stripFileExtension(file);
  109. Component content = new Label("content", doc.html)
  110. .setEscapeModelStrings(false);
  111. if (!MarkupSyntax.PLAIN.equals(doc.syntax)) {
  112. content.add(new SimpleAttributeModifier("class", "markdown"));
  113. }
  114. item.add(content);
  115. item.add(new SimpleAttributeModifier("id", file));
  116. if (counter == 0) {
  117. counter++;
  118. item.add(new SimpleAttributeModifier("class", "tab-pane active"));
  119. }
  120. }
  121. };
  122. fragment.add(tabsView);
  123. }
  124. // document list
  125. final ByteFormat byteFormat = new ByteFormat();
  126. Fragment docs = new Fragment("documents", "documentsFragment", this);
  127. ListDataProvider<PathModel> pathsDp = new ListDataProvider<PathModel>(paths);
  128. DataView<PathModel> pathsView = new DataView<PathModel>("document", pathsDp) {
  129. private static final long serialVersionUID = 1L;
  130. int counter;
  131. @Override
  132. public void populateItem(final Item<PathModel> item) {
  133. PathModel entry = item.getModelObject();
  134. item.add(WicketUtils.newImage("docIcon", "file_world_16x16.png"));
  135. item.add(new Label("docSize", byteFormat.format(entry.size)));
  136. item.add(new LinkPanel("docName", "list", StringUtils.stripFileExtension(entry.name),
  137. DocPage.class, WicketUtils.newPathParameter(repositoryName, commitId, entry.path)));
  138. // links
  139. item.add(new BookmarkablePageLink<Void>("view", DocPage.class, WicketUtils
  140. .newPathParameter(repositoryName, commitId, entry.path)));
  141. item.add(new BookmarkablePageLink<Void>("edit", EditFilePage.class, WicketUtils
  142. .newPathParameter(repositoryName, commitId, entry.path))
  143. .setEnabled(userCanEdit));
  144. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, entry.path);
  145. item.add(new ExternalLink("raw", rawUrl));
  146. item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
  147. .newPathParameter(repositoryName, commitId, entry.path)));
  148. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  149. .newPathParameter(repositoryName, commitId, entry.path)));
  150. WicketUtils.setAlternatingBackground(item, counter);
  151. counter++;
  152. }
  153. };
  154. docs.add(pathsView);
  155. fragment.add(docs);
  156. add(fragment);
  157. }
  158. @Override
  159. protected String getPageName() {
  160. return getString("gb.docs");
  161. }
  162. @Override
  163. protected boolean isCommitPage() {
  164. return true;
  165. }
  166. }