You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilterableRepositoryList.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2013 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.panels;
  17. import java.io.Serializable;
  18. import java.text.DateFormat;
  19. import java.text.MessageFormat;
  20. import java.text.SimpleDateFormat;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.apache.wicket.behavior.HeaderContributor;
  26. import org.apache.wicket.markup.html.basic.Label;
  27. import com.gitblit.Keys;
  28. import com.gitblit.models.RepositoryModel;
  29. import com.gitblit.utils.StringUtils;
  30. import com.gitblit.wicket.WicketUtils;
  31. import com.gitblit.wicket.freemarker.FreemarkerPanel;
  32. import com.gitblit.wicket.ng.NgController;
  33. /**
  34. * A client-side filterable rich repository list which uses Freemarker, Wicket,
  35. * and AngularJS.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public class FilterableRepositoryList extends BasePanel {
  41. private static final long serialVersionUID = 1L;
  42. private final List<RepositoryModel> repositories;
  43. private String title;
  44. private String iconClass;
  45. private boolean allowCreate;
  46. public FilterableRepositoryList(String id, List<RepositoryModel> repositories) {
  47. super(id);
  48. this.repositories = repositories;
  49. }
  50. public void setTitle(String title, String iconClass) {
  51. this.title = title;
  52. this.iconClass = iconClass;
  53. }
  54. public void setAllowCreate(boolean value) {
  55. this.allowCreate = value;
  56. }
  57. @Override
  58. protected void onInitialize() {
  59. super.onInitialize();
  60. String id = getId();
  61. String ngCtrl = id + "Ctrl";
  62. String ngList = id + "List";
  63. Map<String, Object> values = new HashMap<String, Object>();
  64. values.put("ngCtrl", ngCtrl);
  65. values.put("ngList", ngList);
  66. // use Freemarker to setup an AngularJS/Wicket html snippet
  67. FreemarkerPanel panel = new FreemarkerPanel("listComponent", "FilterableRepositoryList.fm", values);
  68. panel.setParseGeneratedMarkup(true);
  69. panel.setRenderBodyOnly(true);
  70. add(panel);
  71. // add the Wicket controls that are referenced in the snippet
  72. String listTitle = StringUtils.isEmpty(title) ? getString("gb.repositories") : title;
  73. panel.add(new Label(ngList + "Title", MessageFormat.format("{0} ({1})", listTitle, repositories.size())));
  74. if (StringUtils.isEmpty(iconClass)) {
  75. panel.add(new Label(ngList + "Icon").setVisible(false));
  76. } else {
  77. Label icon = new Label(ngList + "Icon");
  78. WicketUtils.setCssClass(icon, iconClass);
  79. panel.add(icon);
  80. }
  81. if (allowCreate) {
  82. panel.add(new LinkPanel(ngList + "Button", "btn btn-mini", getString("gb.newRepository"), app().getNewRepositoryPage()));
  83. } else {
  84. panel.add(new Label(ngList + "Button").setVisible(false));
  85. }
  86. String format = app().settings().getString(Keys.web.datestampShortFormat, "MM/dd/yy");
  87. final DateFormat df = new SimpleDateFormat(format);
  88. df.setTimeZone(getTimeZone());
  89. // prepare the simplified repository models list
  90. List<RepoListItem> list = new ArrayList<RepoListItem>();
  91. for (RepositoryModel repo : repositories) {
  92. String name = StringUtils.stripDotGit(repo.name);
  93. String path = "";
  94. if (name.indexOf('/') > -1) {
  95. path = name.substring(0, name.lastIndexOf('/') + 1);
  96. name = name.substring(name.lastIndexOf('/') + 1);
  97. }
  98. RepoListItem item = new RepoListItem();
  99. item.n = name;
  100. item.p = path;
  101. item.r = repo.name;
  102. item.i = repo.description;
  103. item.s = app().repositories().getStarCount(repo);
  104. item.t = getTimeUtils().timeAgo(repo.lastChange);
  105. item.d = df.format(repo.lastChange);
  106. item.c = StringUtils.getColor(StringUtils.stripDotGit(repo.name));
  107. item.wc = repo.isBare ? 0 : 1;
  108. list.add(item);
  109. }
  110. // inject an AngularJS controller with static data
  111. NgController ctrl = new NgController(ngCtrl);
  112. ctrl.addVariable(ngList, list);
  113. add(new HeaderContributor(ctrl));
  114. }
  115. protected class RepoListItem implements Serializable {
  116. private static final long serialVersionUID = 1L;
  117. String r; // repository
  118. String n; // name
  119. String p; // project/path
  120. String t; // time ago
  121. String d; // last updated
  122. String i; // information/description
  123. long s; // stars
  124. String c; // html color
  125. int wc; // working copy: 1 = true, 0 = false
  126. }
  127. }