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.

FilterableProjectList.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Collections;
  23. import java.util.Comparator;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import org.apache.wicket.behavior.HeaderContributor;
  28. import org.apache.wicket.markup.html.basic.Label;
  29. import com.gitblit.GitBlit;
  30. import com.gitblit.Keys;
  31. import com.gitblit.models.ProjectModel;
  32. import com.gitblit.utils.StringUtils;
  33. import com.gitblit.wicket.WicketUtils;
  34. import com.gitblit.wicket.freemarker.FreemarkerPanel;
  35. import com.gitblit.wicket.ng.NgController;
  36. /**
  37. * A client-side filterable rich project list which uses Freemarker, Wicket,
  38. * and AngularJS.
  39. *
  40. * @author James Moger
  41. *
  42. */
  43. public class FilterableProjectList extends BasePanel {
  44. private static final long serialVersionUID = 1L;
  45. private final List<ProjectModel> projects;
  46. private String title;
  47. private String iconClass;
  48. public FilterableProjectList(String id, List<ProjectModel> projects) {
  49. super(id);
  50. this.projects = projects;
  51. }
  52. public void setTitle(String title, String iconClass) {
  53. this.title = title;
  54. this.iconClass = iconClass;
  55. }
  56. @Override
  57. protected void onInitialize() {
  58. super.onInitialize();
  59. String id = getId();
  60. String ngCtrl = id + "Ctrl";
  61. String ngList = id + "List";
  62. Map<String, Object> values = new HashMap<String, Object>();
  63. values.put("ngCtrl", ngCtrl);
  64. values.put("ngList", ngList);
  65. // use Freemarker to setup an AngularJS/Wicket html snippet
  66. FreemarkerPanel panel = new FreemarkerPanel("listComponent", "FilterableProjectList.fm", values);
  67. panel.setParseGeneratedMarkup(true);
  68. panel.setRenderBodyOnly(true);
  69. add(panel);
  70. // add the Wicket controls that are referenced in the snippet
  71. String listTitle = StringUtils.isEmpty(title) ? getString("gb.projects") : title;
  72. panel.add(new Label(ngList + "Title", MessageFormat.format("{0} ({1})", listTitle, projects.size())));
  73. if (StringUtils.isEmpty(iconClass)) {
  74. panel.add(new Label(ngList + "Icon").setVisible(false));
  75. } else {
  76. Label icon = new Label(ngList + "Icon");
  77. WicketUtils.setCssClass(icon, iconClass);
  78. panel.add(icon);
  79. }
  80. String format = GitBlit.getString(Keys.web.datestampShortFormat, "MM/dd/yy");
  81. final DateFormat df = new SimpleDateFormat(format);
  82. df.setTimeZone(getTimeZone());
  83. Collections.sort(projects, new Comparator<ProjectModel>() {
  84. @Override
  85. public int compare(ProjectModel o1, ProjectModel o2) {
  86. return o2.lastChange.compareTo(o1.lastChange);
  87. }
  88. });
  89. List<ProjectListItem> list = new ArrayList<ProjectListItem>();
  90. for (ProjectModel proj : projects) {
  91. if (proj.isUserProject() || proj.repositories.isEmpty()) {
  92. // exclude user projects from list
  93. continue;
  94. }
  95. ProjectListItem item = new ProjectListItem();
  96. item.p = proj.name;
  97. item.n = StringUtils.isEmpty(proj.title) ? proj.name : proj.title;
  98. item.i = proj.description;
  99. item.t = getTimeUtils().timeAgo(proj.lastChange);
  100. item.d = df.format(proj.lastChange);
  101. item.c = proj.repositories.size();
  102. list.add(item);
  103. }
  104. // inject an AngularJS controller with static data
  105. NgController ctrl = new NgController(ngCtrl);
  106. ctrl.addVariable(ngList, list);
  107. add(new HeaderContributor(ctrl));
  108. }
  109. protected class ProjectListItem implements Serializable {
  110. private static final long serialVersionUID = 1L;
  111. String p; // path
  112. String n; // name
  113. String t; // time ago
  114. String d; // last updated
  115. String i; // information/description
  116. long c; // repository count
  117. }
  118. }