UNPKG

@aappddeevv/dynamics-client-ui

Version:

## What is it? A library to help you create great dynamics applications.

180 lines 6.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Hack module to handle WebResource handling problems in both * the unified and traditional web UI. Includes functions when * shimming or working with WebResource initialization processing. */ const BuildSettings_1 = require("BuildSettings"); /** * In targetWindow (defaults to this window), search for an iframe with * the specific id and return the attribute. */ function getAttributeFromIFrame(frameId, attribute, targetWindow = window) { for (let i = 0; i < targetWindow.frames.length; i++) { const w = targetWindow.frames[i]; const fel = w.frameElement; if (fel.id === frameId) { if (typeof window[attribute] !== "undefined") return window[attribute]; return null; } } return null; } exports.getAttributeFromIFrame = getAttributeFromIFrame; /** Return the frame of this window, which is an iframe. */ function findIFrame() { return window.frameElement; } exports.findIFrame = findIFrame; /** Find the parent element (whatever it is) of this iframe. */ function findParentOfIFrame() { return findIFrame().parentElement; } exports.findParentOfIFrame = findParentOfIFrame; /** * Set this iframe's display style to none. visibility would still * leave it participating in layout calculations. */ function setDisplayNoneForThisIFrame() { // @ts-ignore findIFrame().style.display = "none"; } exports.setDisplayNoneForThisIFrame = setDisplayNoneForThisIFrame; /** * Return WEB|UCI|null based on the iframe parent layout. You can also use * Utils.isUci. This is highly hacky and probably subject to change. Use * your own if you can. */ function guessLayout() { var p = findParentOfIFrame(); if (p) { switch (p.nodeName) { case "SPAN": return "WEB"; case "DIV": return "UNIFIED"; } } return null; } exports.guessLayout = guessLayout; /** * Set multliple styles on a an HTMLELement. */ function setStyle(el, propertyObject) { for (var property in propertyObject) { el.style[property] = propertyObject[property]; } } exports.uciFormLineProps = { borderBottom: "1px solid rgb(216, 216, 216)", flexDirection: "row", minHeight: "49px", marginLeft: "23px", height: "49px" }; /** * Adjust parent object assuming "this" el is * a web resource. Do nothing if layout is not UNIFIED. Uses "guessLayout". * Write your own adjustment function if you do not like this version. */ function adjustFormLineForUci(props = exports.uciFormLineProps) { if (guessLayout() === "UNIFIED") { const p = findParentOfIFrame(); if (p) setStyle(p, props); } } exports.adjustFormLineForUci = adjustFormLineForUci; /** * Search up the parent, starting at `start`'s parent, until * predicate is true or return null. */ function findParent(start, predicate) { if (!start) return null; let current = start.parentNode; while (current != null) { if (predicate(current)) return current; current = current.parentNode; } return null; } exports.findParent = findParent; /** * Given a target, if its is an <iframe>, create a new target * as a peer that takes into account UCI or non-UCI interfaces. Otherwise, * find the target if its a string or return it directly if its already * an HTMLElement (of any kind except iframe). `newProps` is used if a * new element needs to be created otherwise a simple div with no id or * classname is created. */ function getOrMakeTarget(target, newProps) { if (target === null) return null; else if (typeof target === "string") { const el = document.getElementById(target); if (el && el.nodeName !== "IFRAME") return el; } else if (target instanceof HTMLElement && target.nodeName !== "IFRAME") return target; // 2nd DOM traversal const blah = typeof target === "string" ? document.getElementById(target) : target; if (blah && blah.nodeName === "IFRAME") { const parent = findParent(blah, n => n.nodeName === "TD"); if (BuildSettings_1.DEBUG) console.log("getOrMakeTarget.parent", parent); if (parent) { if (BuildSettings_1.DEBUG) console.log("getOrMakeTarget: Adding new element"); const el = newProps && newProps.div ? newProps.div : "div"; const portalTarget = document.createElement(el); if (newProps && newProps.id) portalTarget.id = newProps.id; if (newProps && newProps.className) portalTarget.className = newProps.className; parent.appendChild(portalTarget); return portalTarget; } } else if (blah !== null) return blah; return null; } exports.getOrMakeTarget = getOrMakeTarget; /** * Move stylesheets via importNode and appendChil from source to target * documents. */ function moveStylesheetsToParent(source = document, target = window.parent.document) { if (BuildSettings_1.DEBUG) console.log("copyStylesheetsToParent"); const parentss = window.parent.document.styleSheets; const currentss = document.styleSheets; if (BuildSettings_1.DEBUG) for (const ss in currentss) console.log("stylesheet", currentss[ss]); for (let i = 0; i < currentss.length; i++) { const currentCssSS = currentss[i]; if (currentCssSS.href !== null) continue; // only locally inserted styles, not web loaded const n = window.parent.document.importNode(currentCssSS.ownerNode, true); window.parent.document.head.appendChild(n); } // print out parent SS if (BuildSettings_1.DEBUG) for (let i = 0; i < parentss.length; i++) console.log(`parent ss ${i}`, parentss[i]); if (BuildSettings_1.DEBUG) console.log("parent head", window.parent.document.head); } exports.moveStylesheetsToParent = moveStylesheetsToParent; // ???, anything special to *always* do when this module is loaded? // and CLIENT is set in BuildSetting vs say, Utils.isUci. if (BuildSettings_1.CLIENT === "UNIFIED") { // unified only stuff here } //# sourceMappingURL=WebResources.js.map