diff --git a/jcommune-model/src/main/java/org/jtalks/jcommune/model/entity/PollItem.java b/jcommune-model/src/main/java/org/jtalks/jcommune/model/entity/PollItem.java index 94832f94b5..d667c235c3 100644 --- a/jcommune-model/src/main/java/org/jtalks/jcommune/model/entity/PollItem.java +++ b/jcommune-model/src/main/java/org/jtalks/jcommune/model/entity/PollItem.java @@ -34,7 +34,7 @@ public class PollItem extends Entity { /** * Used only by Hibernate. */ - protected PollItem() { + public PollItem() { } diff --git a/jcommune-plugin-api/src/main/java/org/jtalks/jcommune/plugin/api/web/dto/TopicDto.java b/jcommune-plugin-api/src/main/java/org/jtalks/jcommune/plugin/api/web/dto/TopicDto.java index 99c688f571..cf3f83cc6d 100644 --- a/jcommune-plugin-api/src/main/java/org/jtalks/jcommune/plugin/api/web/dto/TopicDto.java +++ b/jcommune-plugin-api/src/main/java/org/jtalks/jcommune/plugin/api/web/dto/TopicDto.java @@ -14,27 +14,32 @@ */ package org.jtalks.jcommune.plugin.api.web.dto; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.Predicate; +import org.apache.commons.lang.StringUtils; import org.jtalks.jcommune.model.entity.Poll; +import org.jtalks.jcommune.model.entity.PollItem; import org.jtalks.jcommune.model.entity.Post; import org.jtalks.jcommune.model.entity.Topic; import org.jtalks.jcommune.plugin.api.web.validation.annotations.BbCodeAwareSize; import org.jtalks.jcommune.plugin.api.web.validation.annotations.BbCodeNesting; +import org.springframework.util.AutoPopulatingList; import javax.validation.Valid; +import java.util.ArrayList; +import java.util.List; /** * DTO for {@link Topic} objects. Used for validation and binding to form. - * + *- * @author Vitaliy Kravchenko * @author Max Malakhov * @author Eugeny Batov */ public class TopicDto { - @Valid private Topic topic; - @BbCodeAwareSize(min = Post.MIN_LENGTH, max = Post.MAX_LENGTH) @BbCodeNesting private String bodyText; @@ -43,6 +48,14 @@ public class TopicDto { private String unreadIconUrl; private String readIconUrl; private String postUrlPrefix; + /** + * Used for UI only + */ + private List pollItemsValue = new AutoPopulatingList(PollItem.class); + /** + * Contains not empty PollItem + */ + private List pollItems = new ArrayList<>(); /** * Plain object for topic creation @@ -65,10 +78,25 @@ public TopicDto(Topic topic) { /** * @return topic that used as dto between controllers and services */ + @Valid public Topic getTopic() { + copyPollItem(); return topic; } + /** + * Copy PollItem from UI collection to Poll. Need that for correct Poll validation. + */ + private void copyPollItem() { + CollectionUtils.select(pollItemsValue, new Predicate() { + @Override + public boolean evaluate(Object object) { + PollItem pollItem = (PollItem) object; + return StringUtils.isNotEmpty(pollItem.getName()) && !pollItems.contains(pollItem); + } + }, pollItems); + } + /** * Set topic in dto. Used in tests. * @@ -165,4 +193,12 @@ public Topic fillTopic(Topic persistentTopic) { persistentTopic.setSticked(topic.isSticked()); return topic; } + + /** + * @return poll options in string representation. + */ + public List getPollItemsValue() { + topic.getPoll().setPollItems(pollItems); + return pollItemsValue; + } } \ No newline at end of file diff --git a/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/TopicController.java b/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/TopicController.java index 0111856b34..2fd698f456 100644 --- a/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/TopicController.java +++ b/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/controller/TopicController.java @@ -17,8 +17,6 @@ import org.joda.time.DateTime; import org.jtalks.jcommune.model.entity.*; import org.jtalks.jcommune.plugin.api.exceptions.NotFoundException; -import org.jtalks.jcommune.service.nontransactional.LocationService; -import org.jtalks.jcommune.web.dto.EntityToDtoConverter; import org.jtalks.jcommune.plugin.api.web.dto.PostDto; import org.jtalks.jcommune.plugin.api.web.dto.TopicDto; import org.jtalks.jcommune.plugin.api.web.util.BreadcrumbBuilder; @@ -28,6 +26,7 @@ import org.jtalks.jcommune.web.dto.json.JsonResponse; import org.jtalks.jcommune.web.dto.json.JsonResponseStatus; import org.jtalks.jcommune.web.validation.editors.DateTimeEditor; +import org.jtalks.jcommune.web.view.PollItemsConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -70,6 +69,7 @@ public class TopicController { public static final String POST_DTO = "postDto"; private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class); public static final String POLL = "poll"; + public static final String POLL_CONFIG = "pollConfig"; private TopicModificationService topicModificationService; private TopicFetchService topicFetchService; @@ -81,6 +81,7 @@ public class TopicController { private LocationService locationService; private SessionRegistry sessionRegistry; private EntityToDtoConverter converter; + private PollItemsConfig pollItemsConfig = new PollItemsConfig(); /** * This method turns the trim binder on. Trim binder @@ -150,6 +151,7 @@ public ModelAndView showNewTopicPage(@RequestParam(BRANCH_ID) Long branchId) thr return new ModelAndView(TOPIC_VIEW) .addObject(TOPIC_DTO, dto) .addObject(BRANCH_ID, branchId) + .addObject(POLL_CONFIG, pollItemsConfig) .addObject(SUBMIT_URL, "/topics/new?branchId=" + branchId) .addObject(BREADCRUMB_LIST, breadcrumbBuilder.getNewTopicBreadcrumb(branch)); } @@ -172,6 +174,7 @@ public ModelAndView createTopic(@Valid @ModelAttribute TopicDto topicDto, return new ModelAndView(TOPIC_VIEW) .addObject(BRANCH_ID, branchId) .addObject(TOPIC_DTO, topicDto) + .addObject(POLL_CONFIG, pollItemsConfig) .addObject(SUBMIT_URL, "/topics/new?branchId=" + branchId) .addObject(BREADCRUMB_LIST, breadcrumbBuilder.getForumBreadcrumb(branch)); } diff --git a/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/view/PollItemsConfig.java b/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/view/PollItemsConfig.java new file mode 100644 index 0000000000..4040eda7de --- /dev/null +++ b/jcommune-view/jcommune-web-controller/src/main/java/org/jtalks/jcommune/web/view/PollItemsConfig.java @@ -0,0 +1,32 @@ +/** + * Copyright (C) 2011 JTalks.org Team + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +package org.jtalks.jcommune.web.view; + +import org.jtalks.jcommune.model.entity.Poll; + +/** + * Contains configuration for Dynamic Poll Items + * @author Andrey Ivanov + */ +public class PollItemsConfig { + + public Integer getMaxPollItems(){ + return Poll.MAX_ITEMS_NUMBER; + } + + public Integer getMinPollItems(){ + return Poll.MIN_ITEMS_NUMBER; + } +} diff --git a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_en.properties b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_en.properties index d924ea3de9..e1a0e98dd2 100644 --- a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_en.properties +++ b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_en.properties @@ -38,6 +38,8 @@ label.topic.notify_message=Notify me about the answer label.topic.no_smiles=Don't display smiles label.topic.close=Close label.topic.open=Reopen +label.addPoll=Add Poll +label.removePoll=Remove Poll #subscription label.subscribe=Subscribe label.subscribe.tooltip=Get mail notifications @@ -306,12 +308,13 @@ label.search.header.topic=Topic #new poll page label.poll.header=New Poll label.poll.title=Poll -label.poll.options.title=Option list +label.poll.options.title=Options label.poll.single.title=Single answer label.poll.multiple.title=Multiple answers are allowed label.poll.date=Ending date label.poll.date.set=Click to set date label.poll.current_end_date=Current end date +label.poll.options.addPollItem=Add new poll item #poll label.poll.vote=Vote label.poll.option.vote.info= {0} - {1}% diff --git a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_es.properties b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_es.properties index fb27d5c0dc..f8b41b295d 100644 --- a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_es.properties +++ b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_es.properties @@ -304,7 +304,7 @@ label.search.header.topic=Tema #new poll page label.poll.header=Nueva Encuesta label.poll.title=Encuesta -label.poll.options.title=Lista de Opciones +label.poll.options.title=Opciones label.poll.single.title=Respuesta simple label.poll.multiple.title=La respuesta m\u00FAltiple est\u00E1 permitida label.poll.date=Fecha de fin @@ -462,3 +462,6 @@ label.branch.header.lastMessage.tooltip=Ver \u00FAltimo mensaje label.topic.section.in=en label.tips.close=Cerrar este tema label.tips.open=Reabrir este tema +label.poll.options.addPollItem=Añadir encuesta artículo +label.addPoll=Adicionar enquete +label.removePoll=Remover Poll diff --git a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_ru.properties b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_ru.properties index ea54dd0679..9f52158480 100644 --- a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_ru.properties +++ b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_ru.properties @@ -311,7 +311,7 @@ label.errors.not_empty = \u041D\u0435 \u043C\u043E\u0436\u0435\u0442 \u0431\u044 label.poll.header=\u041D\u043E\u0432\u043E\u0435 \u0433\u043E\u043B\u043E\u0441\u043E\u0432\u0430\u043D\u0438\u0435 label.poll.title=\u0413\u043E\u043B\u043E\u0441\u043E\u0432\u0430\u043D\u0438\u0435 -label.poll.options.title=\u0421\u043F\u0438\u0441\u043E\u043A \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u043E\u0432 +label.poll.options.title=\u0412\u0430\u0440\u0438\u0430\u043d\u0442 label.poll.single.title=\u041E\u0434\u0438\u043D \u0432\u0430\u0440\u0438\u0430\u043D\u0442 label.poll.multiple.title=\u0412\u043E\u0437\u043C\u043E\u0436\u043D\u043E \u043D\u0435\u0441\u043A\u043E\u043B\u044C\u043A\u043E \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u043E\u0432 \u043E\u0442\u0432\u0435\u0442\u0430 label.poll.date=\u0414\u0430\u0442\u0430 \u043E\u043A\u043E\u043D\u0447\u0430\u043D\u0438\u044F @@ -460,3 +460,6 @@ label.branch.header.lastMessage.tooltip=\u041F\u0435\u0440\u0435\u0439\u0442\u04 label.topic.section.in=\u0432 label.tips.close=\u0417\u0430\u043A\u0440\u044B\u0442\u044C \u0442\u0435\u043C\u0443 label.tips.open=\u041E\u0442\u043A\u0440\u044B\u0442\u044C \u0442\u0435\u043C\u0443 +label.poll.options.addPollItem=\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 +label.addPoll=\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 +label.removePoll=\u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 diff --git a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_uk.properties b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_uk.properties index b749da0834..843e469f99 100644 --- a/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_uk.properties +++ b/jcommune-view/jcommune-web-view/src/main/resources/org/jtalks/jcommune/web/view/messages_uk.properties @@ -299,7 +299,7 @@ label.search.header.topic=\u0422\u0435\u043C\u0430 #new poll page label.poll.header=\u041D\u043E\u0432\u0435 \u043E\u043F\u0438\u0442\u0443\u0432\u0430\u043D\u043D\u044F label.poll.title=\u041E\u043F\u0438\u0442\u0443\u0432\u0430\u043D\u043D\u044F -label.poll.options.title=\u0421\u043F\u0438\u0441\u043E\u043A \u0432\u0430\u0440\u0456\u0430\u043D\u0442\u0456\u0432 +label.poll.options.title=\u0412\u0430\u0440\u0456\u0430\u043d\u0442 label.poll.single.title=\u041E\u0434\u0438\u043D\u043E\u0447\u043D\u0430 \u0432\u0456\u0434\u043F\u043E\u0432\u0456\u0434\u044C label.poll.multiple.title=\u0414\u043E\u0437\u0432\u043E\u043B\u0435\u043D\u043E \u0432\u0438\u0431\u0438\u0440\u0430\u0442\u0438 \u043A\u0456\u043B\u044C\u043A\u0430 \u0432\u0430\u0440\u0456\u0430\u043D\u0442\u0456\u0432 label.poll.date=\u0414\u0430\u0442\u0430 \u0437\u0430\u043A\u0456\u043D\u0447\u0435\u043D\u043D\u044F @@ -462,3 +462,6 @@ label.branch.header.lastMessage.tooltip=\u041F\u0435\u0440\u0435\u0439\u0442\u04 label.topic.section.in=\u0432 label.tips.close=\u0417\u0430\u043A\u0440\u0438\u0442\u0438 \u0442\u0435\u043C\u0443 label.tips.open=\u0412\u0456\u0434\u043A\u0440\u0438\u0442\u0438 \u0442\u0435\u043C\u0443 +label.poll.options.addPollItem=\u0414\u043e\u0434\u0430\u0442\u0438 \u043d\u043e\u0432\u0438\u0439 \u0432\u0430\u0440\u0456\u0430\u043d\u0442 +label.addPoll=\u0414\u043e\u0434\u0430\u0442\u0438 \u043e\u043f\u0438\u0442\u0443\u0432\u0430\u043d\u043d\u044f +label.removePoll=\u0412\u044b\u0434\u0430\u043b\u0438\u0442\u0438 \u043e\u043f\u0438\u0442\u0443\u0432\u0430\u043d\u043d\u044f diff --git a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/jsp/topic/topicForm.jsp b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/jsp/topic/topicForm.jsp index a935a279e4..a8f0c68d7b 100644 --- a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/jsp/topic/topicForm.jsp +++ b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/jsp/topic/topicForm.jsp @@ -78,10 +78,15 @@ postText="${topicDto.bodyText}" bodyParameterName="bodyText" back="${pageContext.request.contextPath}/branches/${branchId}"/> + + "/> + + +


- -
+
@@ -97,44 +102,80 @@ + class="post script-confirm-unsaved input-xlarge" disabled="${pollEditing}"/>
- -
- + +
+ + + +
+
+
+
+ + +
+ + + + + + + + + +
+
+ + + +
+
+
+ ${addPollItemLabel} +
+ +
+
+
+
+ + + + +   + +
+ +
- +
- + + tabindex="800" value="${poll.multipleAnswer}" disabled="${pollEditing}"/>
- -
- - - - -   - -
- -
- <%--Make parent div include floated divs explicitly, or they'll be shown out of parent container--%> + <%--Make parent div include floated divs explicitly, or they'll be shown out of parent container--%>
+
diff --git a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/tags/bbeditor.tag b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/tags/bbeditor.tag index 5249746779..848e5c8b02 100644 --- a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/tags/bbeditor.tag +++ b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/tags/bbeditor.tag @@ -161,13 +161,14 @@ - "/> + "/> " onclick="togglePreviewMode(new Array('posts', 'topics'));return null;"/> + diff --git a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/decorator.jsp b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/decorator.jsp index 90503c5c19..977cd63d9b 100644 --- a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/decorator.jsp +++ b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/decorator.jsp @@ -258,6 +258,10 @@ in the future. + + + + diff --git a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/jsMessages.jsp b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/jsMessages.jsp index 13bb77a89d..9686badc55 100644 --- a/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/jsMessages.jsp +++ b/jcommune-view/jcommune-web-view/src/main/webapp/WEB-INF/template/jsMessages.jsp @@ -153,4 +153,6 @@ $labelFailedToLoadGroups = ' minPollItems) { + $(this).closest('div.pollItemsValue').remove(); + reIndex(); + $("a#add").show(); + } + }); + + function reIndex(){ + $(".pollItemsValue input[type=text]").each(function(index) { + $(this).attr("name", name.replace("__index__", index)); + }); + } +}); \ No newline at end of file diff --git a/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/app/pollPreview.js b/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/app/pollPreview.js index ba3bb0231c..e220e289f5 100644 --- a/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/app/pollPreview.js +++ b/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/app/pollPreview.js @@ -51,11 +51,12 @@ function enterPollPreviewMode() { previewFormElement.show(); } else { exitPollPreviewMode(); + previewFormElement.show(); } } function isPollSet() { - return $("#pollTitle").val() || $("#pollItems").val(); + return $("#pollTitle").val() || getPollItemsValue().length > 0; } /** @@ -81,12 +82,22 @@ function prepareTitle() { * @return processed poll items without leading spaces and empty strings. */ function prepareItems() { - var result; - //"normalize" line endings - result = $("#pollItems")[0].value.replace(/(?:\r\n|\r)+/g, "\n"); - result = trim(result); - result = result.split("\n"); - return stringItemsArrayToHtmlItems(result); + return stringItemsArrayToHtmlItems(getPollItemsValue()); +} + +/** + * Collect all value for poll items + * @returns {Array} + */ +function getPollItemsValue(){ + var result = []; + $(".pollItemsValue input[type=text]").each(function(index){ + value = $(this).val(); + if(value != "") { + result.push(value); + } + }); + return result; } /** diff --git a/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/lib/wysiwyg-bbcode/editor.js b/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/lib/wysiwyg-bbcode/editor.js index 380d463045..dcf59161fa 100644 --- a/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/lib/wysiwyg-bbcode/editor.js +++ b/jcommune-view/jcommune-web-view/src/main/webapp/resources/javascript/lib/wysiwyg-bbcode/editor.js @@ -82,6 +82,7 @@ function initEditor(textAreaId, htmlAreaId, baseDivId) { * preview mode. */ function togglePreviewMode(allowedUrls) { + pollButton = $("input#pollButton"); if (editorVisible) { // exit preview textboxelement.style.display = ""; htmlcontentelement.style.display = "none"; @@ -91,6 +92,9 @@ function togglePreviewMode(allowedUrls) { $('#preview')[0].value = $labelPreview; $('.keymaps-caption').show(); $('#postBody').focus(); + if(pollButton.length) { + pollButton.removeClass("hide"); + } } else { // enter preview content = textboxelement.value; @@ -138,7 +142,6 @@ function bbcode2html(allowedUrls) { success:function (data) { var result = data.html; if(data.is_invalid) { - ErrorUtils.removeErrorMessage(elId); for(var a=0; a