UNPKG

@morjs/runtime-web

Version:
85 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FIELD_CONFIG_METHODS_MAP = exports.getDataSet = exports.mapTarget = exports.getProperties = exports.SCROLL_OFFSET_PROPERTIES = exports.RECT_PROPERTIES = void 0; const attribute_value_1 = require("../../../runtime/dsl/attribute-value"); exports.RECT_PROPERTIES = [ 'top', 'right', 'bottom', 'left', 'width', 'height', 'x', 'y' ]; exports.SCROLL_OFFSET_PROPERTIES = [ 'id', 'scrollLeft', 'scrollTop', 'scrollHeight', 'scrollWidth' ]; const getProperties = (element, properties = []) => { if (!element) return {}; return properties.reduce((pre, curr) => { pre[curr] = element[curr]; return pre; }, {}); }; exports.getProperties = getProperties; const mapTarget = (target, callback) => { if (!target) { return null; } if (target instanceof HTMLElement) { return callback(target); } else { const results = []; target.forEach((el) => { results.push(callback(el)); }); return results; } }; exports.mapTarget = mapTarget; const getDataSet = (el, transformer) => { const result = { dataset: {} }; try { const { dataset } = el; if (!dataset) return result; result.dataset = Object.keys(dataset).reduce((pre, key) => { pre[key] = (0, attribute_value_1.parseDatasetValue)(dataset[key]); return pre; }, {}); return result; } catch (e) { return result; } }; exports.getDataSet = getDataSet; exports.FIELD_CONFIG_METHODS_MAP = { id: (el) => ({ id: el.id }), rect: (el) => (0, exports.getProperties)(el.getBoundingClientRect(), exports.RECT_PROPERTIES), size: (el) => (0, exports.getProperties)(el.getBoundingClientRect(), ['width', 'height']), scrollOffset: (el) => (Object.assign(Object.assign({}, (0, exports.getDataSet)(el)), (0, exports.getProperties)(el, exports.SCROLL_OFFSET_PROPERTIES))), node: (el) => ({ node: el }), computedStyle: (el, keys) => { try { const result = {}; if (keys.length < 0) return result; const currentStyle = window.getComputedStyle(el); keys.forEach((key) => (result[key] = currentStyle.getPropertyValue(key))); return result; } catch (e) { return {}; } }, dataset: exports.getDataSet }; //# sourceMappingURL=utils.js.map