BrightSide Workbench Full Report + Source Code
StarItCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.www.starit;
20 
21 import org.turro.elephant.context.ElephantContext;
22 import org.turro.elephant.context.IConstructor;
23 import org.turro.html.HTMLHelper;
24 import org.turro.i18n.I_;
25 import org.turro.marker.ElephantMarker;
26 import org.turro.starit.StarItUtil;
27 import org.turro.starit.StarsInfo;
28 import org.turro.util.IdGenerator;
29 
34 public class StarItCtrl {
35 
36  private HTMLHelper html;
37  private String entityPath;
38  private IConstructor constructor;
39  private boolean readOnly;
40  private int maxRating = 5;
41 
42  public StarItCtrl(IConstructor constructor) {
43  this.constructor = constructor;
44  }
45 
46  public void setEntityPath(String entityPath) {
47  this.entityPath = entityPath;
48  }
49 
50  public void setReadOnly(boolean readOnly) {
51  this.readOnly = readOnly;
52  }
53 
54  public void renderTemplateStars(boolean container) {
55  String id = container ? "sic_" + IdGenerator.generate() : constructor.getParameter("domid");
56  ElephantMarker em = new ElephantMarker(constructor);
57  em.put("control", this);
58  em.put("controlId", id);
59  em.put("container", container);
60  em.put("info", StarItUtil.stars(entityPath));
61  em.put("entityPath", entityPath);
62  em.put("readOnly", readOnly);
63  em.process("widgets/webcomp", "starIt");
64  }
65 
66  public void renderStars(boolean container) {
67  if(ElephantMarker.existsTemplate(constructor, false, "widgets/webcomp", "starIt")) {
68  renderTemplateStars(container);
69  return;
70  }
71  StarsInfo si = StarItUtil.stars(entityPath);
72  html = new HTMLHelper(constructor);
73  String id = container ? "sic_" + IdGenerator.generate() : constructor.getParameter("domid");
74  if(container) {
75  html.startTag("div", "id='" + id + "'");
76  }
77  html.startTable("class='starit-container'");
78  for(int star = 1; star <= 5; star++) {
79  html.startTableCol("")
80  .startTag("a", readOnly ? "" : "onClick='" + getStarsUrl(id, star) + "'")
81  .doTag("img", "src='" + ElephantContext.getRootWebPath() +
82  "/_internal/system/images/star_" + (star <= si.getStars() ? "color" : "bn") + ".png'")
83  .endTag();
84  }
85  html.endTableRow()
86  .startTableCol("colspan='5' class='starit-string'")
87  .write(si.count + " " + I_.get("review(s)"));
88  html.endAllTags();
89  }
90 
91  public String getStarsUrl(String id, Integer i) {
92  return "$.post(\"" + ElephantContext.getRootWebPath() + "/xpaction/contact\"," +
93  "{ action: \"star-it\", path: \"" + entityPath + "\"" +
94  ", stars: " + i + ", domid: \"" + id + "\" })" +
95  ".done(function(data) { $(\"#" + id + "\").html(data); });";
96  }
97 
98  public int getMaxRating() {
99  return maxRating;
100  }
101 
102 }
HTMLGenerator write(String value)
HTMLGenerator doTag(String tag)
HTMLGenerator startTag(String tag)
HTMLGenerator startTableCol(String attributes)
HTMLGenerator startTable(String attributes)
static String get(String msg)
Definition: I_.java:41
void process(String rootTmpl, String tmpl)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)
Object put(Object key, Object value)
static StarsInfo stars(Object entity)
void setEntityPath(String entityPath)
Definition: StarItCtrl.java:46
void renderStars(boolean container)
Definition: StarItCtrl.java:66
void renderTemplateStars(boolean container)
Definition: StarItCtrl.java:54
void setReadOnly(boolean readOnly)
Definition: StarItCtrl.java:50
StarItCtrl(IConstructor constructor)
Definition: StarItCtrl.java:42
String getStarsUrl(String id, Integer i)
Definition: StarItCtrl.java:91