BrightSide Workbench Full Report + Source Code
www/voteit/VoteItCtrl.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.voteit;
20 
21 import org.turro.string.Strings;
22 import org.turro.auth.Authentication;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.elephant.context.IConstructor;
25 import org.turro.html.HTMLHelper;
26 import org.turro.i18n.I_;
27 import org.turro.marker.ElephantMarker;
28 import org.turro.plugin.contacts.ContactList;
29 import org.turro.plugin.contacts.IContact;
30 import org.turro.util.IdGenerator;
31 import org.turro.voteit.VoteItInfo;
32 import org.turro.voteit.VoteItUtil;
33 
38 public class VoteItCtrl {
39 
40  private HTMLHelper html;
41  private String entityPath, allowedString;
42  private final IConstructor constructor;
43  private boolean forbiden, forbid, forceInternalRender;
44  private ContactList allowed;
45 
46  public VoteItCtrl(IConstructor constructor) {
47  this.constructor = constructor;
48  }
49 
50  public void setEntityPath(String entityPath) {
51  this.entityPath = entityPath;
52  }
53 
54  public void setForbiden(boolean forbiden) {
55  this.forbiden = forbiden;
56  }
57 
58  public void setAllowed(ContactList allowed) {
59  this.allowed = allowed;
60  }
61 
62  public void setAllowedString(String allowedString) {
63  this.allowedString = allowedString;
64  }
65 
66  public void setForbid(boolean forbid) {
67  this.forbid = forbid;
68  }
69 
70  public void setForceInternalRender(boolean force) {
71  forceInternalRender = force;
72  }
73 
74  public void renderTemplateVotes(boolean container) {
76  if(!forbiden && forbid) {
77  forbiden = !(allowed != null && allowed.contains(contact));
78  }
79  if(Strings.isBlank(allowedString)) {
80  allowedString = allowed == null ? null : allowed.getCommaSeparatedId();
81  }
82  VoteItInfo vii = VoteItUtil.info(entityPath, allowedString, contact);
83  String id = container ? "vic_" + IdGenerator.generate() : constructor.getParameter("domid");
84  if(contact.isValid()) {
85  ElephantMarker em = new ElephantMarker(constructor);
86  em.put("control", this);
87  em.put("contact", contact);
88  em.put("controlId", id);
89  em.put("container", container);
90  em.put("votes", vii);
91  em.put("forbiden", forbiden);
92  em.put("entityPath", entityPath);
93  em.process("widgets/webcomp", "voteIt");
94  }
95  }
96 
97  public void renderVotes(boolean container) {
98  if(!forceInternalRender && ElephantMarker.existsTemplate(constructor, false, "widgets/webcomp", "voteIt")) {
99  renderTemplateVotes(container);
100  return;
101  }
102  IContact contact = Authentication.getIContact();
103  if(!forbiden && forbid) {
104  forbiden = !(allowed != null && allowed.contains(contact));
105  }
106  if(Strings.isBlank(allowedString)) {
107  allowedString = allowed == null ? null : allowed.getCommaSeparatedId();
108  }
109  VoteItInfo vii = VoteItUtil.info(entityPath, allowedString, contact);
110  html = new HTMLHelper(constructor);
111  long positiveWidth = 0, negativeWidth = 0, nocareWidth = 0;
112  if(vii.getTotalVotes() > 0) {
113  positiveWidth = vii.getPositive() * 60 / vii.getTotalVotes();
114  negativeWidth = vii.getNegative() * 60 / vii.getTotalVotes();
115  nocareWidth = vii.getNocare() * 60 / vii.getTotalVotes();
116  }
117  String id = container ? "vic_" + IdGenerator.generate() : constructor.getParameter("domid");
118  if(container) {
119  html.startTag("div", "id='" + id + "'");
120  }
121  html.startTable("class='voteit-container'")
122  .startTableCol("colspan='3'")
123  .startTag("span", "class='voteit-votes'")
124  .write(vii.getVotes() + "")
125  .endTag()
126  .doTag("br")
127  .startTag("span", "class='voteit-string'")
128  .write(vii.getTotalVotes() + " " + I_.get("Votes"))
129  .endTag()
130  .endTableRow()
131  .startTableCol("colspan='3'")
132  .startTag("div", "style='width:60px' class='voteit-bar'")
133  .startTable("cellpadding='0' cellspacing='0'")
134  .startTableCol("class='voteit-positive' style='" + (positiveWidth == 0 ? "display:none" : "width:" + positiveWidth + "px") + "'")
135  .startTableCol("class='voteit-nocare' style='" + (nocareWidth == 0 ? "display:none" : "width:" + nocareWidth + "px") + "'")
136  .startTableCol("class='voteit-negative' style='" + (negativeWidth == 0 ? "display:none" : "width:" + negativeWidth + "px") + "'")
137  .endTable()
138  .endTableRow();
139  if(contact.isValid()) {
140  if(forbiden) {
141  html.startTableCol("colspan='3'")
142  .doTag("img", new String[] {
143  "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/locked.png'"
144  })
145  .endTableRow();
146  } else {
147  html.startTableCol("")
148  .startTag("a", "onClick='" + getVoteUrl(id, 1) + "'")
149  .doTag("img", "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/promote.png'")
150  .endTag()
151  .startTableCol("")
152  .startTag("a", "onClick='" + getVoteUrl(id, 0) + "'")
153  .doTag("img", "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/nocare.png'")
154  .endTag()
155  .startTableCol("")
156  .startTag("a", "onClick='" + getVoteUrl(id, -1) + "'")
157  .doTag("img", "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/demote.png'")
158  .endTag()
159  .endTableRow();
160  }
161  String color = "#333";
162  if(vii.getMyVote() != null) {
163  if(vii.getMyVote().getVote() == 1) {
164  color = "#11f605";
165  } else if(vii.getMyVote().getVote() == -1) {
166  color = "#f00900";
167  } else if(vii.getMyVote().getVote() == 0) {
168  color = "#ffa327";
169  }
170  html.startTableCol("colspan='3'")
171  .startTag("span", "class='voteit-string' style='color:" + color + "'")
172  .write(I_.get("My vote"));
173  } else {
174  html.startTableCol("colspan='3'")
175  .startTag("span", "class='voteit-string' style='color:" + color + "'")
176  .write(I_.get("No vote"));
177  }
178  }
179  html.endAllTags();
180  }
181 
182  public String getVoteUrl(String id, int i) {
183  return "$.post(\"" + ElephantContext.getRootWebPath() + "/xpaction/contact\"," +
184  "{ action: \"vote-it\", path: \"" + entityPath + "\"" +
185  (allowedString == null ? "" : ", allowed: \"" + allowedString.replaceAll("'", "-") + "\"") +
186  ", vote: " + i + ", domid: \"" + id + "\" })" +
187  ".done(function(data) { $(\"#" + id + "\").html(data); });";
188  }
189 
190 }
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 VoteItInfo info(Object entity, IContact contact)
void setAllowed(ContactList allowed)
void renderTemplateVotes(boolean container)
VoteItCtrl(IConstructor constructor)
void setAllowedString(String allowedString)