* Construct a grid item from the given element and options * @param el the HTML element tied to this grid after it's been initialized * @param opts grid options - public for classes to access, but use methods to modify!
(public el: GridHTMLElement, public opts: GridStackOptions = {})
| 264 | * @param opts grid options - public for classes to access, but use methods to modify! |
| 265 | */ |
| 266 | public constructor(public el: GridHTMLElement, public opts: GridStackOptions = {}) { |
| 267 | el.gridstack = this; |
| 268 | this.opts = opts = opts || {}; // handles null/undefined/0 |
| 269 | |
| 270 | if (!el.classList.contains('grid-stack')) { |
| 271 | this.el.classList.add('grid-stack'); |
| 272 | } |
| 273 | |
| 274 | // if row property exists, replace minRow and maxRow instead |
| 275 | if (opts.row) { |
| 276 | opts.minRow = opts.maxRow = opts.row; |
| 277 | delete opts.row; |
| 278 | } |
| 279 | const rowAttr = Utils.toNumber(el.getAttribute('gs-row')); |
| 280 | |
| 281 | // flag only valid in sub-grids (handled by parent, not here) |
| 282 | if (opts.column === 'auto') { |
| 283 | delete opts.column; |
| 284 | } |
| 285 | // save original setting so we can restore on save |
| 286 | if (opts.alwaysShowResizeHandle !== undefined) { |
| 287 | (opts as InternalGridStackOptions)._alwaysShowResizeHandle = opts.alwaysShowResizeHandle; |
| 288 | } |
| 289 | |
| 290 | // cleanup responsive opts (must have columnWidth | breakpoints) then sort breakpoints by size (so we can match during resize) |
| 291 | const resp = opts.columnOpts; |
| 292 | if (resp) { |
| 293 | const bk = resp.breakpoints; |
| 294 | if (!resp.columnWidth && !bk?.length) { |
| 295 | delete opts.columnOpts; |
| 296 | } else { |
| 297 | resp.columnMax = resp.columnMax || 12; |
| 298 | if (bk?.length > 1) bk.sort((a, b) => (b.w || 0) - (a.w || 0)); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // elements DOM attributes override any passed options (like CSS style) - merge the two together |
| 303 | const defaults: GridStackOptions = { |
| 304 | ...Utils.cloneDeep(gridDefaults), |
| 305 | column: Utils.toNumber(el.getAttribute('gs-column')) || gridDefaults.column, |
| 306 | minRow: rowAttr ? rowAttr : Utils.toNumber(el.getAttribute('gs-min-row')) || gridDefaults.minRow, |
| 307 | maxRow: rowAttr ? rowAttr : Utils.toNumber(el.getAttribute('gs-max-row')) || gridDefaults.maxRow, |
| 308 | staticGrid: Utils.toBool(el.getAttribute('gs-static')) || gridDefaults.staticGrid, |
| 309 | sizeToContent: Utils.toBool(el.getAttribute('gs-size-to-content')) || undefined, |
| 310 | draggable: { |
| 311 | handle: (opts.handleClass ? '.' + opts.handleClass : (opts.handle ? opts.handle : '')) || gridDefaults.draggable.handle, |
| 312 | }, |
| 313 | removableOptions: { |
| 314 | accept: opts.itemClass || gridDefaults.removableOptions.accept, |
| 315 | decline: gridDefaults.removableOptions.decline |
| 316 | }, |
| 317 | }; |
| 318 | if (el.getAttribute('gs-animate')) { // default to true, but if set to false use that instead |
| 319 | defaults.animate = Utils.toBool(el.getAttribute('gs-animate')) |
| 320 | } |
| 321 | |
| 322 | opts = Utils.defaults(opts, defaults); |
| 323 | this._initMargin(); // part of settings defaults... |
nothing calls this directly
no test coverage detected