Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014-2022 Web Firm Framework
*
* 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.
*/
package com.webfirmframework.wffweb.js;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.webfirmframework.wffweb.InvalidValueException;
import com.webfirmframework.wffweb.util.StringUtil;
/**
* Utility methods to generate JavaScript code.
*
* @author WFF
* @since 2.1.1
*/
public final class JsUtil {
private static final int SPACE_CODE_POINT;
private static final int SLASH_R_CODE_POINT;
private static final int SLASH_N_CODE_POINT;
private static final int SLASH_T_CODE_POINT;
static {
final int[] codePoints = " \r\n\t".codePoints().toArray();
SPACE_CODE_POINT = codePoints[0];
SLASH_R_CODE_POINT = codePoints[1];
SLASH_N_CODE_POINT = codePoints[2];
SLASH_T_CODE_POINT = codePoints[3];
}
private JsUtil() {
throw new AssertionError();
}
/**
* @param jsKeyAndElementId The map containing key values. The key in the map
* will be used as the key in the generated js object.
* The value in the map should be the id of the field.
* @return the JavaScript object for the fields value. Sample :
* {username:document.getElementById('uId').value}
* @since 2.1.1
* @author WFF
*/
public static String getJsObjectForFieldsValue(final Map jsKeyAndElementId) {
final StringBuilder builder = new StringBuilder(38);
builder.append('{');
for (final Entry entry : jsKeyAndElementId.entrySet()) {
builder.append(entry.getKey()).append(":document.getElementById('").append(entry.getValue().toString())
.append("').value,");
}
builder.replace(builder.length() - 1, builder.length(), "}");
return builder.toString();
}
/**
* @param jsKeyAndElementId The map containing key values. The key in the map
* will be used as the key in the generated js
* object. The value in the map should be the id of
* the field.
* @param alternativeFunction alternative function name for
* document.getElementById
*
* @return the JavaScript object for the fields value. Sample :
* {username:gebi('uId').value} if the alternativeFunction
* is gebi
* @since 3.0.1
* @author WFF
*/
public static String getJsObjectForFieldsValue(final Map jsKeyAndElementId,
final String alternativeFunction) {
if (StringUtil.isBlank(alternativeFunction)) {
throw new InvalidValueException("alternativeFunction cannot be blank");
}
final StringBuilder builder = new StringBuilder(38);
builder.append('{');
for (final Entry entry : jsKeyAndElementId.entrySet()) {
builder.append(entry.getKey()).append(':').append(alternativeFunction).append("('")
.append(entry.getValue().toString()).append("').value,");
}
builder.replace(builder.length() - 1, builder.length(), "}");
return builder.toString();
}
/**
* @param ids The set containing element ids. The id will be used as the key in
* the generated js object. The value in the set should be the id of
* the field. The id in the set should be a valid JavaScript object
* key.
* @return the JavaScript object for the fields value. Sample :
* {uId:document.getElementById('uId').value}
* @since 2.1.1
* @author WFF
*/
public static String getJsObjectForFieldsValue(final Set