MCPcopy Index your code
hub / github.com/gridstack/gridstack.js / _readAttr

Method _readAttr

src/gridstack.ts:1963–2005  ·  view source on GitHub ↗

@internal call to read any default attributes from element

(el: HTMLElement, clearDefaultAttr = true)

Source from the content-addressed store, hash-verified

1961
1962 /** @internal call to read any default attributes from element */
1963 protected _readAttr(el: HTMLElement, clearDefaultAttr = true): GridStackWidget {
1964 const n: GridStackNode = {};
1965 n.x = Utils.toNumber(el.getAttribute('gs-x'));
1966 n.y = Utils.toNumber(el.getAttribute('gs-y'));
1967 n.w = Utils.toNumber(el.getAttribute('gs-w'));
1968 n.h = Utils.toNumber(el.getAttribute('gs-h'));
1969 n.autoPosition = Utils.toBool(el.getAttribute('gs-auto-position'));
1970 n.noResize = Utils.toBool(el.getAttribute('gs-no-resize'));
1971 n.noMove = Utils.toBool(el.getAttribute('gs-no-move'));
1972 n.locked = Utils.toBool(el.getAttribute('gs-locked'));
1973 const attr = el.getAttribute('gs-size-to-content');
1974 if (attr) {
1975 if (attr === 'true' || attr === 'false') n.sizeToContent = Utils.toBool(attr);
1976 else n.sizeToContent = parseInt(attr, 10);
1977 }
1978 n.id = el.getAttribute('gs-id');
1979
1980 // read but never written out
1981 n.maxW = Utils.toNumber(el.getAttribute('gs-max-w'));
1982 n.minW = Utils.toNumber(el.getAttribute('gs-min-w'));
1983 n.maxH = Utils.toNumber(el.getAttribute('gs-max-h'));
1984 n.minH = Utils.toNumber(el.getAttribute('gs-min-h'));
1985
1986 // v8.x optimization to reduce un-needed attr that don't render or are default CSS
1987 if (clearDefaultAttr) {
1988 if (n.w === 1) el.removeAttribute('gs-w');
1989 if (n.h === 1) el.removeAttribute('gs-h');
1990 if (n.maxW) el.removeAttribute('gs-max-w');
1991 if (n.minW) el.removeAttribute('gs-min-w');
1992 if (n.maxH) el.removeAttribute('gs-max-h');
1993 if (n.minH) el.removeAttribute('gs-min-h');
1994 }
1995
1996 // remove any key not found (null or false which is default, unless sizeToContent=false override)
1997 for (const key in n) {
1998 if (!n.hasOwnProperty(key)) return;
1999 if (!n[key] && n[key] !== 0 && key !== 'sizeToContent') { // 0 can be valid value (x,y only really)
2000 delete n[key];
2001 }
2002 }
2003
2004 return n;
2005 }
2006
2007 /** @internal */
2008 protected _setStaticClass(): GridStack {

Callers 3

addWidgetMethod · 0.95
_prepareElementMethod · 0.95
_setupAcceptWidgetMethod · 0.95

Calls 2

toNumberMethod · 0.80
toBoolMethod · 0.80

Tested by

no test coverage detected