Paul Martin
2016-03-14 713d27789bc0b5d82b607fed20e5f16ad9216898
commit | author | age
f13c4c 1 /*
JM 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  */
c1c3c6 16 package com.gitblit.wicket.pages;
JM 17
18 import java.util.List;
19
a7317a 20 import org.apache.wicket.Component;
c1c3c6 21 import org.apache.wicket.PageParameters;
e13171 22 import org.apache.wicket.behavior.SimpleAttributeModifier;
c1c3c6 23 import org.apache.wicket.markup.html.basic.Label;
JM 24 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
e13171 25 import org.apache.wicket.markup.html.link.ExternalLink;
a7317a 26 import org.apache.wicket.markup.html.panel.Fragment;
c1c3c6 27 import org.apache.wicket.markup.repeater.Item;
JM 28 import org.apache.wicket.markup.repeater.data.DataView;
29 import org.apache.wicket.markup.repeater.data.ListDataProvider;
713d27 30 import org.apache.wicket.model.StringResourceModel;
PM 31 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
c1c3c6 32 import org.eclipse.jgit.lib.Repository;
a7317a 33 import org.eclipse.jgit.revwalk.RevCommit;
c1c3c6 34
1f9dae 35 import com.gitblit.models.PathModel;
713d27 36 import com.gitblit.models.UserModel;
ff17f7 37 import com.gitblit.servlet.RawServlet;
c1c3c6 38 import com.gitblit.utils.ByteFormat;
JM 39 import com.gitblit.utils.JGitUtils;
a7317a 40 import com.gitblit.utils.StringUtils;
a7db57 41 import com.gitblit.wicket.CacheControl;
713d27 42 import com.gitblit.wicket.GitBlitWebSession;
a7db57 43 import com.gitblit.wicket.CacheControl.LastModified;
dc7c2f 44 import com.gitblit.wicket.MarkupProcessor;
JM 45 import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
e13171 46 import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
699e71 47 import com.gitblit.wicket.WicketUtils;
1f9dae 48 import com.gitblit.wicket.panels.LinkPanel;
c1c3c6 49
a7db57 50 @CacheControl(LastModified.REPOSITORY)
c1c3c6 51 public class DocsPage extends RepositoryPage {
JM 52
53     public DocsPage(PageParameters params) {
54         super(params);
55
062519 56         String objectId = WicketUtils.getObject(params);
JM 57
11a173 58         MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
dc7c2f 59
c1c3c6 60         Repository r = getRepository();
713d27 61         UserModel currentUser = (GitBlitWebSession.get().getUser() != null) ? GitBlitWebSession.get().getUser() : UserModel.ANONYMOUS;
PM 62         final boolean userCanEdit = currentUser.canEdit(getRepositoryModel());
63         
062519 64         RevCommit head = JGitUtils.getCommit(r, objectId);
e13171 65         final String commitId = getBestCommitId(head);
062519 66
e13171 67         List<String> extensions = processor.getAllExtensions();
c1c3c6 68         List<PathModel> paths = JGitUtils.getDocuments(r, extensions);
JM 69
e13171 70         List<MarkupDocument> roots = processor.getRootDocs(r, repositoryName, commitId);
a7317a 71         Fragment fragment = null;
e13171 72         if (roots.isEmpty()) {
c8a833 73             // no identified root documents
a7317a 74             fragment = new Fragment("docs", "noIndexFragment", this);
c8a833 75             setResponsePage(NoDocsPage.class, params);
a7317a 76         } else {
e13171 77             // root documents, use tabbed ui of index/root and document list
JM 78             fragment = new Fragment("docs", "tabsFragment", this);
79             ListDataProvider<MarkupDocument> docDp = new ListDataProvider<MarkupDocument>(roots);
80
81             // tab titles
82             DataView<MarkupDocument> tabTitles = new DataView<MarkupDocument>("tabTitle", docDp) {
83                 private static final long serialVersionUID = 1L;
84                 int counter;
85
86                 @Override
87                 public void populateItem(final Item<MarkupDocument> item) {
88                     MarkupDocument doc = item.getModelObject();
89                     String file = StringUtils.getLastPathElement(doc.documentPath);
90                     file = StringUtils.stripFileExtension(file);
91                     String name = file.replace('_', ' ').replace('-',  ' ');
92
93                     ExternalLink link = new ExternalLink("link", "#" + file);
94                     link.add(new Label("label", name.toUpperCase()).setRenderBodyOnly(true));
95                     item.add(link);
96                     if (counter == 0) {
97                         counter++;
98                         item.add(new SimpleAttributeModifier("class", "active"));
99                     }
100                 }
101             };
102             fragment.add(tabTitles);
103
104             // tab content
105             DataView<MarkupDocument> tabsView = new DataView<MarkupDocument>("tabContent", docDp) {
106                 private static final long serialVersionUID = 1L;
107                 int counter;
108
109                 @Override
110                 public void populateItem(final Item<MarkupDocument> item) {
111                     MarkupDocument doc = item.getModelObject();
713d27 112                     
275496 113                     item.add(new BookmarkablePageLink<Void>("editLink", EditFilePage.class,
713d27 114                             WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath))
PM 115                             .setEnabled(userCanEdit));
116                     
117                     // document page links                    
e13171 118                     item.add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
JM 119                             WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
120                     item.add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
121                             WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
ff17f7 122                     String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, doc.documentPath);
JM 123                     item.add(new ExternalLink("rawLink", rawUrl));
e13171 124
JM 125                     // document content
126                     String file = StringUtils.getLastPathElement(doc.documentPath);
127                     file = StringUtils.stripFileExtension(file);
128                     Component content = new Label("content", doc.html)
129                         .setEscapeModelStrings(false);
130                     if (!MarkupSyntax.PLAIN.equals(doc.syntax)) {
131                         content.add(new SimpleAttributeModifier("class", "markdown"));
132                     }
133                     item.add(content);
134                     item.add(new SimpleAttributeModifier("id", file));
135                     if (counter == 0) {
136                         counter++;
137                         item.add(new SimpleAttributeModifier("class", "tab-pane active"));
138                     }
139                 }
140             };
141             fragment.add(tabsView);
a7317a 142         }
JM 143
144         // document list
c1c3c6 145         final ByteFormat byteFormat = new ByteFormat();
a7317a 146         Fragment docs = new Fragment("documents", "documentsFragment", this);
c1c3c6 147         ListDataProvider<PathModel> pathsDp = new ListDataProvider<PathModel>(paths);
JM 148         DataView<PathModel> pathsView = new DataView<PathModel>("document", pathsDp) {
149             private static final long serialVersionUID = 1L;
2a7306 150             int counter;
c1c3c6 151
699e71 152             @Override
c1c3c6 153             public void populateItem(final Item<PathModel> item) {
JM 154                 PathModel entry = item.getModelObject();
155                 item.add(WicketUtils.newImage("docIcon", "file_world_16x16.png"));
156                 item.add(new Label("docSize", byteFormat.format(entry.size)));
6d29e0 157                 item.add(new LinkPanel("docName", "list", StringUtils.stripFileExtension(entry.name),
062519 158                         DocPage.class, WicketUtils.newPathParameter(repositoryName, commitId, entry.path)));
c1c3c6 159
JM 160                 // links
dc7c2f 161                 item.add(new BookmarkablePageLink<Void>("view", DocPage.class, WicketUtils
062519 162                         .newPathParameter(repositoryName, commitId, entry.path)));
275496 163                 item.add(new BookmarkablePageLink<Void>("edit", EditFilePage.class, WicketUtils
713d27 164                         .newPathParameter(repositoryName, commitId, entry.path))
PM 165                         .setEnabled(userCanEdit));
062519 166                 String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, entry.path);
ff17f7 167                 item.add(new ExternalLink("raw", rawUrl));
85c2e6 168                 item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
062519 169                         .newPathParameter(repositoryName, commitId, entry.path)));
2a7306 170                 item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
062519 171                         .newPathParameter(repositoryName, commitId, entry.path)));
c1c3c6 172                 WicketUtils.setAlternatingBackground(item, counter);
JM 173                 counter++;
174             }
175         };
a7317a 176         docs.add(pathsView);
JM 177         fragment.add(docs);
178         add(fragment);
c1c3c6 179     }
JM 180
181     @Override
182     protected String getPageName() {
183         return getString("gb.docs");
184     }
5d5e55 185
JM 186     @Override
187     protected boolean isCommitPage() {
188         return true;
189     }
190
c1c3c6 191 }