UNPKG

jsf.js_next_gen

Version:

A next generation typescript reimplementation of jsf.js

187 lines 8.5 kB
"use strict"; /*! Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to you 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveDefaults = exports.getEventTarget = exports.resolveWindowId = exports.resolveDelay = exports.resolveTimeout = exports.resoveNamingContainerMapper = exports.resolveViewRootId = exports.resolveViewId = exports.resolveForm = exports.resolveFinalUrl = exports.resolveTargetUrl = exports.resolveHandlerFunc = void 0; const mona_dish_1 = require("mona-dish"); const Const_1 = require("../core/Const"); const Lang_1 = require("../util/Lang"); const ExtDomQuery_1 = require("../util/ExtDomQuery"); const Assertions_1 = require("../util/Assertions"); /** * Resolver functions for various aspects of the request data * * stateless because it might be called from various * parts of the response classes */ /** * resolves the event handlers lazily * so that if some decoration happens in between we can deal with it * * @param requestContext * @param responseContext * @param funcName */ function resolveHandlerFunc(requestContext, responseContext, funcName) { responseContext = responseContext || new mona_dish_1.Config({}); return responseContext.getIf(funcName) .orElseLazy(() => requestContext.getIf(funcName).value) .orElse(Const_1.EMPTY_FUNC).value; } exports.resolveHandlerFunc = resolveHandlerFunc; function resolveTargetUrl(srcFormElement) { return (typeof srcFormElement.elements[Const_1.ENCODED_URL] == 'undefined') ? srcFormElement.action : srcFormElement.elements[Const_1.ENCODED_URL].value; } exports.resolveTargetUrl = resolveTargetUrl; function resolveFinalUrl(sourceForm, formData, ajaxType = Const_1.REQ_TYPE_POST) { let targetUrl = resolveTargetUrl(sourceForm.getAsElem(0).value); return targetUrl + (ajaxType == Const_1.REQ_TYPE_GET ? "?" + formData.toString() : Const_1.EMPTY_STR); } exports.resolveFinalUrl = resolveFinalUrl; /** * form resolution the same way our old implementation did * it is either the id or the parent form of the element or an embedded form * of the element * * @param elem * @param event */ function resolveForm(elem, event) { return Lang_1.ExtLang.getForm(elem.getAsElem(0).value, event); } exports.resolveForm = resolveForm; function resolveViewId(form) { const viewState = form.querySelectorAll(`input[type='hidden'][name*='${(0, Const_1.$nsp)(Const_1.P_VIEWSTATE)}']`).id.orElse("").value; const divider = (0, Const_1.$faces)().separatorchar; const viewId = viewState.split(divider, 2)[0]; const viewStateViewId = viewId.indexOf((0, Const_1.$nsp)(Const_1.P_VIEWSTATE)) === -1 ? viewId : ""; // myfaces specific, we in non portlet environments prepend the viewId // even without being in a naming container, the other components ignore that return form.id.value.indexOf(viewStateViewId) === 0 ? viewStateViewId : ""; } exports.resolveViewId = resolveViewId; function resolveViewRootId(form) { const viewState = form.querySelectorAll(`input[type='hidden'][name*='${(0, Const_1.$nsp)(Const_1.P_VIEWSTATE)}']`).attr("name").orElse("").value; const divider = (0, Const_1.$faces)().separatorchar; const viewId = viewState.split(divider, 2)[0]; //different to the identifier the form id is never prepended to the viewstate return viewId.indexOf((0, Const_1.$nsp)(Const_1.P_VIEWSTATE)) === -1 ? viewId : ""; } exports.resolveViewRootId = resolveViewRootId; /** * as per jsdoc before the request it must be ensured that every post argument * is prefixed with the naming container id (there is an exception in mojarra with * the element=element param, which we have to follow here as well. * (inputs are prefixed by name anyway normally this only affects our standard parameters) * @private */ function resoveNamingContainerMapper(internalContext) { const isNamedViewRoot = internalContext.getIf(Const_1.NAMED_VIEWROOT).isPresent(); if (!isNamedViewRoot) { return (key, value) => [key, value]; } const partialId = internalContext.getIf(Const_1.NAMING_CONTAINER_ID).value; const SEP = (0, Const_1.$faces)().separatorchar; const prefix = partialId + SEP; return (key, value) => (key.indexOf(prefix) == 0) ? [key, value] : [prefix + key, value]; } exports.resoveNamingContainerMapper = resoveNamingContainerMapper; function resolveTimeout(options) { var _a; let getCfg = Lang_1.ExtLang.getLocalOrGlobalConfig; return (_a = options.getIf(Const_1.CTX_OPTIONS_TIMEOUT).value) !== null && _a !== void 0 ? _a : getCfg(options.value, Const_1.CTX_OPTIONS_TIMEOUT, 0); } exports.resolveTimeout = resolveTimeout; /** * resolve the delay from the options and/or the request context and or the configuration * * @param options ... the options object, in most cases it will host the delay value */ function resolveDelay(options) { // null, 'none', or undefined will automatically be mapped to 0 aka no delay // the config delay will be dropped not needed anymore, it does not really // make sense anymore now that it is part of a local spec let ret = options.getIf(Const_1.CTX_OPTIONS_DELAY).orElse(0).value; // if delay === none, no delay must be used, aka delay 0 ret = (Const_1.DELAY_NONE === ret) ? 0 : ret; // negative, or invalid values will automatically get a js exception Assertions_1.Assertions.assertDelay(ret); return ret; } exports.resolveDelay = resolveDelay; /** * resolves the window-id from various sources * * @param options */ function resolveWindowId(options) { var _a, _b; return (_b = (_a = options === null || options === void 0 ? void 0 : options.value) === null || _a === void 0 ? void 0 : _a.windowId) !== null && _b !== void 0 ? _b : ExtDomQuery_1.ExtDomQuery.windowId.value; } exports.resolveWindowId = resolveWindowId; /** * cross port from the dojo lib * browser save event resolution * @param evt the event object * (with a fallback for ie events if none is present) * @deprecated soon will be removed */ function getEventTarget(evt) { var _a, _b; // ie6 and 7 fallback let finalEvent = evt; /* * evt source is defined in the jsf events * seems like some component authors use our code, * so we add it here see also * https://issues.apache.org/jira/browse/MYFACES-2458 * not entirely a bug but makes sense to add this * behavior. I don´t use it that way but nevertheless it * does not break anything so why not */ let t = (_b = (_a = finalEvent === null || finalEvent === void 0 ? void 0 : finalEvent.srcElement) !== null && _a !== void 0 ? _a : finalEvent === null || finalEvent === void 0 ? void 0 : finalEvent.target) !== null && _b !== void 0 ? _b : finalEvent === null || finalEvent === void 0 ? void 0 : finalEvent.source; while ((t) && (t.nodeType != 1)) { t = t.parentNode; } return t; } exports.getEventTarget = getEventTarget; /** * resolves a bunch of default values * which can be further processed from the given * call parameters of faces.ajax.request * * @param event * @param opts * @param el */ function resolveDefaults(event, opts, el = null) { var _a; //deep copy the options, so that further transformations to not backfire into the callers const elem = mona_dish_1.DQ.byId(el || event.target, true); const options = new ExtDomQuery_1.ExtConfig(opts).deepCopy; return { options: options, elem: elem, elementId: elem.id.value, windowId: resolveWindowId(options), isResetValues: true === ((_a = options.value) === null || _a === void 0 ? void 0 : _a.resetValues) }; } exports.resolveDefaults = resolveDefaults; //# sourceMappingURL=RequestDataResolver.js.map