001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.widget;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
020import org.kuali.rice.krad.uif.UifConstants;
021import org.kuali.rice.krad.uif.component.Component;
022import org.kuali.rice.krad.uif.container.Group;
023import org.kuali.rice.krad.uif.view.View;
024
025/**
026 * Allows client-side reordering of the group contents
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class Reorderer extends WidgetBase {
031    private static final long serialVersionUID = 6142957061046219120L;
032
033    private String movableStyleClass;
034
035    public Reorderer() {
036        super();
037    }
038
039    /**
040     * The following initialization is performed:
041     *
042     * <ul>
043     * <li>Adds the movable style class to each group item</li>
044     * <li>Prepares the movable widget option based on the movable style class</li>
045     * </ul>
046     */
047    @Override
048    public void performFinalize(View view, Object model, Component component) {
049        super.performFinalize(view, model, component);
050
051        if (!(component instanceof Group)) {
052            throw new RiceIllegalArgumentException("Parent component for Reorderer widget must be a group.");
053        }
054
055        if (StringUtils.isNotBlank(movableStyleClass)) {
056            for (Component item : ((Group) component).getItems()) {
057                item.addStyleClass(movableStyleClass);
058            }
059
060            // add the default movable class to the selectors option if not already configured
061            if (!getComponentOptions().containsKey(UifConstants.ReordererOptionKeys.SELECTORS)) {
062                String selectorsOption =
063                        "{" + UifConstants.ReordererOptionKeys.MOVABLES + " : 'span." + movableStyleClass + "' }";
064                getComponentOptions().put(UifConstants.ReordererOptionKeys.SELECTORS, selectorsOption);
065            }
066        }
067    }
068
069    /**
070     * Returns the style class for the item spans that will identify a movable element
071     *
072     * <p>
073     * Given style class will be used to build a jQuery selector that is then passed to the
074     * reorderer widget through the options
075     * </p>
076     *
077     * @return String style class
078     */
079    public String getMovableStyleClass() {
080        return movableStyleClass;
081    }
082
083    /**
084     * Setter for the style class that identifies movable elements (spans)
085     *
086     * @param movableStyleClass
087     */
088    public void setMovableStyleClass(String movableStyleClass) {
089        this.movableStyleClass = movableStyleClass;
090    }
091}