View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.wicket.markup.html.form;
20  
21  import static de.agilecoders.wicket.jquery.JQuery.$;
22  
23  import de.agilecoders.wicket.jquery.function.JavaScriptInlineFunction;
24  import java.util.ArrayList;
25  import org.apache.syncope.client.console.SyncopeConsoleSession;
26  import org.apache.wicket.Component;
27  import org.apache.wicket.behavior.Behavior;
28  import org.apache.wicket.markup.head.IHeaderResponse;
29  import org.apache.wicket.markup.head.JavaScriptHeaderItem;
30  import org.apache.wicket.model.ResourceModel;
31  
32  public class ConfirmBehavior extends Behavior {
33  
34      private static final long serialVersionUID = 2210125898183667592L;
35  
36      private final Component parent;
37  
38      private final String msg;
39  
40      public ConfirmBehavior(final Component parent, final String msg) {
41          this.parent = parent;
42          this.msg = msg;
43      }
44  
45      @Override
46      public void renderHead(final Component component, final IHeaderResponse response) {
47          super.renderHead(component, response);
48  
49          response.render(JavaScriptHeaderItem.forScript("proceed = false;", null));
50          response.render($(parent).on("click",
51                  new JavaScriptInlineFunction(""
52                          + "var element = $(this);"
53                          + "evt.preventDefault();"
54                          + "if (proceed == false) {"
55                          + "  evt.stopImmediatePropagation();"
56                          + "  bootbox.confirm({"
57                          + "message:'" + new ResourceModel(msg).getObject() + "', "
58                          + "buttons: {"
59                          + "    confirm: {"
60                          + "        className: 'btn-success'"
61                          + "    },"
62                          + "    cancel: {"
63                          + "        className: 'btn-danger'"
64                          + "    }"
65                          + "},"
66                          + "locale: '" + SyncopeConsoleSession.get().getLocale().getLanguage() + "',"
67                          + "callback: function(result) {"
68                          + "    if (result == true) {"
69                          + "      proceed = true;"
70                          + "      element.click();"
71                          + "    } else {"
72                          + "      proceed = false;"
73                          + "    }"
74                          + "  return true;"
75                          + "  }})"
76                          + "} else {"
77                          + "  proceed = false;"
78                          + "};", new ArrayList<>()
79                  )).asDomReadyScript());
80      }
81  }