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 label for the ok button.
055     */
056    @Parameter(value = "message:private-default-confirm-ok", defaultPrefix = BindingConstants.LITERAL)
057    private String ok;
058
059    /**
060     * The label for the ok button.
061     */
062    @Parameter(value = "message:private-default-confirm-cancel", defaultPrefix = BindingConstants.LITERAL)
063    private String cancel;
064
065    void beginRender(MarkupWriter writer)
066    {
067        if (!disabled)
068        {
069            javaScriptSupport.require("t5/core/confirm-click");
070
071            writer.attributes("data-confirm-title", title,
072                    "data-confirm-message", message,
073                    "data-confirm-label-ok", ok,
074                    "data-confirm-label-cancel", cancel);
075        }
076    }
077}