/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2008 - 2017 Regenstrief Institute, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * #L%
 */
package org.fujion.client;

import org.fujion.component.BaseComponent;
import org.fujion.component.BaseUIComponent;
import org.fujion.websocket.WebSocketHandler;

/**
 * Static convenience methods for client-side operations.
 */
public class ClientUtil {
    
    /**
     * Invoke a function on the client.
     *
     * @param function Name of the function to invoke.
     * @param args Arguments to pass to the function.
     */
    public static void invoke(String function, Object... args) {
        ClientInvocation invocation = new ClientInvocation((String) null, function, args);
        WebSocketHandler.send(invocation);
    }
    
    /**
     * Redirects the client.
     *
     * @param target URL of the redirect target.
     */
    public static void redirect(String target) {
        redirect(target, null);
    }
    
    /**
     * Redirects the client.
     *
     * @param target URL of the redirect target.
     * @param window Name of the window that will be redirected. A null value redirects the current
     *            window.
     */
    public static void redirect(String target, String window) {
        invoke("fujion.redirect", target, window);
    }
    
    /**
     * Invokes a JavaScript expression on the client.
     *
     * @param expression A valid JavaScript expression.
     */
    public static void eval(String expression) {
        invoke("fujion.eval", expression);
    }
    
    /**
     * Submits a form.
     *
     * @param form Root component of the form.
     */
    public static void submit(BaseComponent form) {
        invoke("fujion.submit", form);
    }
    
    /**
     * Creates a busy message covering the specified target. A busy message consists of a mask the
     * covers and prevents interaction with the target component and a message centered within the
     * mask.
     *
     * @param target The target of the busy message.
     * @param message The message to be displayed. If null, any existing message is removed.
     */
    public static void busy(BaseUIComponent target, String message) {
        if (message == null || message.isEmpty()) {
            target.removeMask();
        } else {
            target.addMask(message);
        }
    }
    
    /**
     * Saves content as a file on the client machine.
     *
     * @param content Content to save.
     * @param mimeType The MIME type of the content.
     * @param fileName The name of the file to be created.
     */
    public static void saveToFile(String content, String mimeType, String fileName) {
        invoke("fujion.saveToFile", content, mimeType, fileName);
    }
    
    private ClientUtil() {
    }
}

@@ -907,21 +907,17 @@
 tUtil %7B%0A
-    
 %0A
+
     /**%0A
@@ -1277,37 +1277,33 @@
 ocation);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * R
@@ -1466,37 +1466,33 @@
 t, null);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * R
@@ -1815,37 +1815,33 @@
  window);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * I
@@ -2047,37 +2047,33 @@
 ression);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * S
@@ -2242,29 +2242,25 @@
 orm);%0A    %7D%0A
-    
 %0A
+
     /**%0A    
@@ -2847,24 +2847,594 @@
      %7D%0A    %7D
+%0A%0A    /**%0A     * Sets the canClose parameter on the client. When set to true (the default value), the browser%0A     * window may be closed without challenge. When set to false, the browser will present a%0A     * confirmation dialog before allowing the window to be closed.%0A     *%0A     * @param value The value for the canClose parameter.%0A     * @deprecated Use %7B@link org.fujion.component.Page#setClosable Page.closable%7D property instead.%0A     */%0A    @Deprecated%0A    public static void canClose(boolean value) %7B%0A        ExecutionContext.getPage().setClosable(value);%0A    %7D
 %0A    %0A    /*
@@ -3809,28 +3809,24 @@
 ame);%0A    %7D%0A
-    
 %0A    private

Add back ClientUtil.canClose, but deprecate it.