Search in sources :

Example 1 with JSStatementList

use of com.helger.html.jscode.JSStatementList in project peppol-practical by phax.

the class CommentUI method getCreateComment.

@Nonnull
public static IHCNode getCreateComment(@Nonnull final ILayoutExecutionContext aLEC, @Nonnull final String sResultDivID, @Nonnull final ITypedObject<String> aObject, @Nullable final ICommentThread aCommentThread, @Nullable final IComment aParentComment, @Nullable final CommentFormErrors aFormErrors, @Nullable final IHCNode aMessageBox) {
    final IRequestWebScopeWithoutResponse aRequestScope = aLEC.getRequestScope();
    final Locale aDisplayLocale = aLEC.getDisplayLocale();
    final IUser aLoggedInUser = LoggedInUserManager.getInstance().getCurrentUser();
    final boolean bIsCreateNewThread = aCommentThread == null || aParentComment == null;
    final HCDiv aFormContainer = new HCDiv();
    if (bIsCreateNewThread)
        if (aFormErrors == null || aFormErrors.isEmpty())
            aFormContainer.addStyle(CCSSProperties.DISPLAY_NONE);
    aFormContainer.addClass(CCommentCSS.CSS_CLASS_COMMENT_CREATE);
    if (aFormErrors != null && !aFormErrors.isEmpty())
        aFormContainer.addChild(new BootstrapErrorBox().addChild(EPhotonCoreText.ERR_INCORRECT_INPUT.getDisplayText(aDisplayLocale)));
    final BootstrapViewForm aForm = aFormContainer.addAndReturnChild(new BootstrapViewForm());
    aForm.setTitle(ECommentText.MSG_CREATE_COMMENT.getDisplayText(aDisplayLocale));
    HCEdit aEditAuthor = null;
    if (aLoggedInUser != null) {
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory(ECommentText.MSG_FIELD_AUTHOR.getDisplayText(aDisplayLocale)).setCtrl(aLoggedInUser.getDisplayName()));
    } else {
        aEditAuthor = new HCEdit(new RequestField(FIELD_COMMENT_AUTHOR));
        aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory(ECommentText.MSG_FIELD_AUTHOR.getDisplayText(aDisplayLocale)).setCtrl(aEditAuthor).setHelpText(ECommentText.DESC_FIELD_AUTHOR.getDisplayText(aDisplayLocale)).setErrorList(aFormErrors == null ? null : aFormErrors.getListOfField(FIELD_COMMENT_AUTHOR)));
    }
    final HCEdit aEditTitle = new HCEdit(new RequestField(FIELD_COMMENT_TITLE));
    aForm.addFormGroup(new BootstrapFormGroup().setLabel(ECommentText.MSG_FIELD_TITLE.getDisplayText(aDisplayLocale)).setCtrl(aEditTitle).setHelpText(ECommentText.DESC_FIELD_TITLE.getDisplayText(aDisplayLocale)).setErrorList(aFormErrors == null ? null : aFormErrors.getListOfField(FIELD_COMMENT_TITLE)));
    final HCTextAreaAutosize aTextAreaContent = new HCTextAreaAutosize(new RequestField(FIELD_COMMENT_TEXT)).setRows(5);
    aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory(ECommentText.MSG_FIELD_TEXT.getDisplayText(aDisplayLocale)).setCtrl(aTextAreaContent).setHelpText(ECommentText.DESC_FIELD_TEXT.getDisplayText(aDisplayLocale)).setErrorList(aFormErrors == null ? null : aFormErrors.getListOfField(FIELD_COMMENT_TEXT)));
    final BootstrapButtonToolbar aToolbar = new BootstrapButtonToolbar(aLEC);
    // What to do on save?
    {
        final JSAnonymousFunction aOnSuccess = new JSAnonymousFunction();
        final JSVar aJSData = aOnSuccess.param("data");
        aOnSuccess.body().add(JQuery.idRef(sResultDivID).replaceWith(aJSData.ref(PhotonUnifiedResponse.HtmlHelper.PROPERTY_HTML)));
        JQueryInvocation aSaveAction;
        if (bIsCreateNewThread) {
            // Create a new thread
            aSaveAction = new JQueryAjaxBuilder().url(CAjax.COMMENT_CREATE_THREAD.getInvocationURL(aRequestScope)).data(new JSAssocArray().add(AjaxExecutorCommentCreateThread.PARAM_OBJECT_TYPE, aObject.getObjectType().getName()).add(AjaxExecutorCommentCreateThread.PARAM_OBJECT_ID, aObject.getID()).add(AjaxExecutorCommentCreateThread.PARAM_AUTHOR, aLoggedInUser != null ? JSExpr.lit("") : JQuery.idRef(aEditAuthor).val()).add(AjaxExecutorCommentCreateThread.PARAM_TITLE, JQuery.idRef(aEditTitle).val()).add(AjaxExecutorCommentCreateThread.PARAM_TEXT, JQuery.idRef(aTextAreaContent).val())).success(JSJQueryHelper.jqueryAjaxSuccessHandler(aOnSuccess, null)).build();
        } else {
            // Reply to a previous comment
            aSaveAction = new JQueryAjaxBuilder().url(CAjax.COMMENT_ADD.getInvocationURL(aRequestScope)).data(new JSAssocArray().add(AjaxExecutorCommentAdd.PARAM_OBJECT_TYPE, aObject.getObjectType().getName()).add(AjaxExecutorCommentAdd.PARAM_OBJECT_ID, aObject.getID()).add(AjaxExecutorCommentAdd.PARAM_COMMENT_THREAD_ID, aCommentThread.getID()).add(AjaxExecutorCommentAdd.PARAM_COMMENT_ID, aParentComment.getID()).add(AjaxExecutorCommentAdd.PARAM_OBJECT_ID, aObject.getID()).add(AjaxExecutorCommentAdd.PARAM_AUTHOR, aLoggedInUser != null ? JSExpr.lit("") : JQuery.idRef(aEditAuthor).val()).add(AjaxExecutorCommentAdd.PARAM_TITLE, JQuery.idRef(aEditTitle).val()).add(AjaxExecutorCommentAdd.PARAM_TEXT, JQuery.idRef(aTextAreaContent).val())).success(JSJQueryHelper.jqueryAjaxSuccessHandler(aOnSuccess, null)).build();
        }
        aToolbar.addButtonSave(aDisplayLocale, aSaveAction);
    }
    BootstrapButton aButtonCreate = null;
    if (bIsCreateNewThread) {
        // The create button
        aButtonCreate = new BootstrapButton().addChild(ECommentText.MSG_CREATE_COMMENT.getDisplayText(aDisplayLocale));
        aButtonCreate.setOnClick(new JSStatementList(JQuery.idRef(aFormContainer).show(), JQuery.jQueryThis().disable()));
    }
    // What to do on cancel?
    {
        final JSStatementList aCancelAction = new JSStatementList(JQuery.idRefMultiple(aEditTitle, aTextAreaContent).val(""), JQuery.idRef(aFormContainer).hide());
        if (aButtonCreate != null)
            aCancelAction.add(JQuery.idRef(aButtonCreate).enable());
        if (aEditAuthor != null)
            aCancelAction.add(JQuery.idRef(aEditAuthor).val(""));
        aToolbar.addButtonCancel(aDisplayLocale, aCancelAction);
    }
    aFormContainer.addChild(aToolbar);
    // Show create comment button
    final HCNodeList ret = new HCNodeList();
    ret.addChild(aButtonCreate);
    ret.addChild(aFormContainer);
    ret.addChild(aMessageBox);
    return ret;
}
Also used : Locale(java.util.Locale) HCDiv(com.helger.html.hc.html.grouping.HCDiv) AbstractHCDiv(com.helger.html.hc.html.grouping.AbstractHCDiv) JSAnonymousFunction(com.helger.html.jscode.JSAnonymousFunction) JQueryInvocation(com.helger.html.jquery.JQueryInvocation) HCNodeList(com.helger.html.hc.impl.HCNodeList) BootstrapErrorBox(com.helger.photon.bootstrap4.alert.BootstrapErrorBox) HCTextAreaAutosize(com.helger.photon.uictrls.autosize.HCTextAreaAutosize) JSVar(com.helger.html.jscode.JSVar) BootstrapViewForm(com.helger.photon.bootstrap4.form.BootstrapViewForm) HCEdit(com.helger.html.hc.html.forms.HCEdit) JSStatementList(com.helger.html.jscode.JSStatementList) IRequestWebScopeWithoutResponse(com.helger.web.scope.IRequestWebScopeWithoutResponse) JQueryAjaxBuilder(com.helger.html.jquery.JQueryAjaxBuilder) IUser(com.helger.photon.security.user.IUser) JSAssocArray(com.helger.html.jscode.JSAssocArray) BootstrapButton(com.helger.photon.bootstrap4.button.BootstrapButton) BootstrapButtonToolbar(com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) RequestField(com.helger.photon.core.form.RequestField) Nonnull(javax.annotation.Nonnull)

Aggregations

HCEdit (com.helger.html.hc.html.forms.HCEdit)1 AbstractHCDiv (com.helger.html.hc.html.grouping.AbstractHCDiv)1 HCDiv (com.helger.html.hc.html.grouping.HCDiv)1 HCNodeList (com.helger.html.hc.impl.HCNodeList)1 JQueryAjaxBuilder (com.helger.html.jquery.JQueryAjaxBuilder)1 JQueryInvocation (com.helger.html.jquery.JQueryInvocation)1 JSAnonymousFunction (com.helger.html.jscode.JSAnonymousFunction)1 JSAssocArray (com.helger.html.jscode.JSAssocArray)1 JSStatementList (com.helger.html.jscode.JSStatementList)1 JSVar (com.helger.html.jscode.JSVar)1 BootstrapErrorBox (com.helger.photon.bootstrap4.alert.BootstrapErrorBox)1 BootstrapButton (com.helger.photon.bootstrap4.button.BootstrapButton)1 BootstrapButtonToolbar (com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar)1 BootstrapFormGroup (com.helger.photon.bootstrap4.form.BootstrapFormGroup)1 BootstrapViewForm (com.helger.photon.bootstrap4.form.BootstrapViewForm)1 RequestField (com.helger.photon.core.form.RequestField)1 IUser (com.helger.photon.security.user.IUser)1 HCTextAreaAutosize (com.helger.photon.uictrls.autosize.HCTextAreaAutosize)1 IRequestWebScopeWithoutResponse (com.helger.web.scope.IRequestWebScopeWithoutResponse)1 Locale (java.util.Locale)1