    001// Licensed under the Apache License, Version 2.0 (the "License");
    002// you may not use this file except in compliance with the License.
    003// You may obtain a copy of the License at
    004//
    005//     http://www.apache.org/licenses/LICENSE-2.0
    006//
    007// Unless required by applicable law or agreed to in writing, software
    008// distributed under the License is distributed on an "AS IS" BASIS,
    009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    010// See the License for the specific language governing permissions and
    011// limitations under the License.
    012
    013package org.apache.tapestry5.corelib.mixins;
    014
    015import org.apache.tapestry5.BindingConstants;
    016import org.apache.tapestry5.MarkupWriter;
    017import org.apache.tapestry5.annotations.Environmental;
    018import org.apache.tapestry5.annotations.MixinAfter;
    019import org.apache.tapestry5.annotations.Parameter;
    020import org.apache.tapestry5.services.javascript.JavaScriptSupport;
    021
    022/**
    023 * A mixin that can be placed on a clickable component, such as {@link org.apache.tapestry5.corelib.components.LinkSubmit},
    024 * and will raise a confirmation dialog when the element is clicked.
    025 *
    026 * @tapestrydoc
    027 * @since 5.4
    028 */
    029@MixinAfter
    030public class Confirm
    031{
    032    /**
    033     * The message to present to the user in the body of the modal dialog.
    034     */
    035    @Parameter(value = "message:private-default-confirm-message", defaultPrefix = BindingConstants.LITERAL)
    036    private String message;
    037
    038    /**
    039     * The title for the modal dialog.
    040     */
    041    @Parameter(value = "message:private-default-confirm-title", defaultPrefix = BindingConstants.LITERAL)
    042    private String title;
    043
    044    /**
    045     * If true, then the mixin does nothing (no attributes added, no module imported).
    046     */
    047    @Parameter("false")
    048    private boolean disabled;
    049
    050    @Environmental
    051    private JavaScriptSupport javaScriptSupport;
    052
    053    /*
    054     * The CSS class for the ok button
    055     */
    056    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    057    private String okClass;
    058
    059    /**
    060     * The label for the ok button.
    061     */
    062    @Parameter(value = "message:private-default-confirm-ok", defaultPrefix = BindingConstants.LITERAL)
    063    private String ok;
    064
    065    /**
    066     * The label for the ok button.
    067     */
    068    @Parameter(value = "message:private-default-confirm-cancel", defaultPrefix = BindingConstants.LITERAL)
    069    private String cancel;
    070
    071    void beginRender(MarkupWriter writer)
    072    {
    073        if (!disabled)
    074        {
    075            javaScriptSupport.require("t5/core/confirm-click");
    076
    077            writer.attributes("data-confirm-title", title,
    078                    "data-confirm-message", message,
    079                    "data-confirm-class-ok", okClass,
    080                    "data-confirm-label-ok", ok,
    081                    "data-confirm-label-cancel", cancel);
    082        }
    083    }
    084}