001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.extensions.ajax.markup.html.modal; 018 019import org.apache.wicket.Component; 020import org.apache.wicket.behavior.Behavior; 021import org.apache.wicket.core.util.string.CssUtils; 022import org.apache.wicket.markup.head.IHeaderResponse; 023import org.apache.wicket.markup.head.JavaScriptHeaderItem; 024import org.apache.wicket.markup.head.OnDomReadyHeaderItem; 025import org.apache.wicket.markup.head.PriorityHeaderItem; 026import org.apache.wicket.request.resource.JavaScriptResourceReference; 027import org.apache.wicket.request.resource.ResourceReference; 028 029/** 030 * Trap focus inside a component's markup. 031 * 032 * @author svenmeier 033 */ 034public class TrapFocusBehavior extends Behavior 035{ 036 037 /** 038 * Resource key for a CSS class to be applied to the current active focus-trap. 039 */ 040 public static final String CSS_CURRENT_KEY = CssUtils.key(TrapFocusBehavior.class, "current"); 041 042 private static final long serialVersionUID = 1L; 043 044 private static final ResourceReference JS = new JavaScriptResourceReference( 045 TrapFocusBehavior.class, "trap-focus.js"); 046 047 @Override 048 public void renderHead(Component component, IHeaderResponse response) 049 { 050 response.render(JavaScriptHeaderItem.forReference(JS)); 051 052 String styleClass = component.getString(CSS_CURRENT_KEY, null, "current-focus-trap"); 053 054 CharSequence script = String.format("Wicket.trapFocus('%s', '%s');", component.getMarkupId(), styleClass); 055 056 response.render(new PriorityHeaderItem(OnDomReadyHeaderItem.forScript(script))); 057 } 058}