From e7a0adeec0d30d1671e87d0b01444dae2e18e354 Mon Sep 17 00:00:00 2001 From: SmileLifeIven <38740691+SmileLifeIven@users.noreply.github.com> Date: Tue, 7 Dec 2021 23:15:12 +0800 Subject: [PATCH 001/655] feature: support ie browser (#1887) * feature: support ie browser into separate es5 folder (larger target, polyfill) --- demo/web1.html | 4 +- es5/tsconfig.json | 33 +++++++++ es5/webpack.config.js | 22 ++++++ package.json | 4 +- src/gridstack-poly.js | 151 ++++++++++++++++++++++++++++++++++++++++++ src/gridstack.ts | 7 +- src/utils.ts | 2 +- 7 files changed, 215 insertions(+), 8 deletions(-) create mode 100644 es5/tsconfig.json create mode 100644 es5/webpack.config.js diff --git a/demo/web1.html b/demo/web1.html index a7f7193ea..c2a012a8d 100644 --- a/demo/web1.html +++ b/demo/web1.html @@ -13,7 +13,9 @@ - + + +
diff --git a/es5/tsconfig.json b/es5/tsconfig.json new file mode 100644 index 000000000..b9e6da3c0 --- /dev/null +++ b/es5/tsconfig.json @@ -0,0 +1,33 @@ +{ + "compilerOptions": { + /* + "allowJs": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true + */ + "declaration": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "inlineSources": true, + "lib": [ "es6", "es2015", "dom" ], + "module": "CommonJS", + "noImplicitAny": false, + "outDir": "../dist/es5", + "sourceMap": true, + "strict": false, + "target": "ES5" + }, + "exclude": [ + "../src/**/*.spec.ts", + // used for webpack h5/jq/static .js + "../src/gridstack-h5.ts", + "../src/gridstack-jq.ts", + "../src/gridstack-static.ts", + ], + "include": [ + "../src/**/*.ts" + ], + "typeroots": [ + "../node_modules/@types" + ] +} diff --git a/es5/webpack.config.js b/es5/webpack.config.js new file mode 100644 index 000000000..68e95ab37 --- /dev/null +++ b/es5/webpack.config.js @@ -0,0 +1,22 @@ +const path = require('path'); +const webpackConfig = require('../webpack.config.js'); + +const config = {...webpackConfig, + target: ['web', 'es5'], + module: { + rules: [ + { + test: /\.ts$/, + use: { + loader: 'ts-loader', + options: { + configFile: 'es5/tsconfig.json' + } + }, + exclude: ['/node_modules/', '/src/h5/', '/src/index-*.ts'], + }, + ], + }, +}; +config.output.path = path.resolve(__dirname, '../dist/es5') +module.exports = config \ No newline at end of file diff --git a/package.json b/package.json index 1c6e9e9ca..687ffd800 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ } ], "scripts": { - "build": "yarn --no-progress && rm -rf dist/* && grunt && webpack && tsc --stripInternal && yarn doc", + "build": "yarn --no-progress && rm -rf dist/* && grunt && webpack && tsc --stripInternal && webpack --config es5/webpack.config.js && tsc --stripInternal --project es5/tsconfig.json && yarn doc", "w": "rm -rf dist/* && grunt && webpack", "t": "rm -rf dist/* && grunt && tsc --stripInternal", "doc": "doctoc ./README.md && doctoc ./doc/README.md && doctoc ./doc/CHANGES.md", @@ -91,4 +91,4 @@ "webpack": "^5.3.2", "webpack-cli": "^4.6.0" } -} +} \ No newline at end of file diff --git a/src/gridstack-poly.js b/src/gridstack-poly.js index 8419d588b..80601a75f 100644 --- a/src/gridstack-poly.js +++ b/src/gridstack-poly.js @@ -146,6 +146,23 @@ if (!Array.prototype.findIndex) { }); })([Element.prototype, Document.prototype, DocumentFragment.prototype]); +// from: https://github.com/jserz/js_piece/blob/master/DOM/Element/prepend()/prepend().md +(function (arr) { + arr.forEach(function (item) { + item.prepend = item.prepend || function () { + var argArr = Array.prototype.slice.call(arguments), + docFrag = document.createDocumentFragment(); + + argArr.forEach(function (argItem) { + var isNode = argItem instanceof Node; + docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); + }); + + this.insertBefore(docFrag, this.firstChild); + }; + }); +})([Element.prototype, Document.prototype, DocumentFragment.prototype]); + // Production steps of ECMA-262, Edition 5, 15.4.4.18 // Reference: http://es5.github.io/#x15.4.4.18 @@ -202,4 +219,138 @@ if (!Array.prototype['forEach']) { } // 8. return undefined }; +} + +// https://developer.mozilla.org/zh-CN/docs/Web/API/CustomEvent/CustomEvent +(function(){ + try{ + new window.CustomEvent('T'); + }catch(e){ + var CustomEvent = function(event, params){ + params = params || { bubbles: false, cancelable: false, detail: undefined }; + + var evt = document.createEvent('CustomEvent'); + + evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail); + + return evt; + }; + + CustomEvent.prototype = window.Event.prototype; + + window.CustomEvent = CustomEvent; + } +})(); + +// https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md +(function (arr) { + arr.forEach(function (item) { + if (item.hasOwnProperty('remove')) { + return; + } + Object.defineProperty(item, 'remove', { + configurable: true, + enumerable: true, + writable: true, + value: function remove() { + this.parentNode.removeChild(this); + } + }); + }); +})([Element.prototype, CharacterData.prototype, DocumentType.prototype]); + +// https://developer.mozilla.org/zh-CN/docs/Web/API/Element/matches +if (!Element.prototype.matches) { + Element.prototype.matches = + Element.prototype.matchesSelector || + Element.prototype.mozMatchesSelector || + Element.prototype.msMatchesSelector || + Element.prototype.oMatchesSelector || + Element.prototype.webkitMatchesSelector || + function(s) { + var matches = (this.document || this.ownerDocument).querySelectorAll(s), + i = matches.length; + while (--i >= 0 && matches.item(i) !== this) {} + return i > -1; + }; +} + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from +// Production steps of ECMA-262, Edition 6, 22.1.2.1 +if (!Array.from) { + Array.from = (function () { + var toStr = Object.prototype.toString; + var isCallable = function (fn) { + return typeof fn === 'function' || toStr.call(fn) === '[object Function]'; + }; + var toInteger = function (value) { + var number = Number(value); + if (isNaN(number)) { return 0; } + if (number === 0 || !isFinite(number)) { return number; } + return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number)); + }; + var maxSafeInteger = Math.pow(2, 53) - 1; + var toLength = function (value) { + var len = toInteger(value); + return Math.min(Math.max(len, 0), maxSafeInteger); + }; + + // The length property of the from method is 1. + return function from(arrayLike/*, mapFn, thisArg */) { + // 1. Let C be the this value. + var C = this; + + // 2. Let items be ToObject(arrayLike). + var items = Object(arrayLike); + + // 3. ReturnIfAbrupt(items). + if (arrayLike == null) { + throw new TypeError("Array.from requires an array-like object - not null or undefined"); + } + + // 4. If mapfn is undefined, then let mapping be false. + var mapFn = arguments.length > 1 ? arguments[1] : void undefined; + var T; + if (typeof mapFn !== 'undefined') { + // 5. else + // 5. a If IsCallable(mapfn) is false, throw a TypeError exception. + if (!isCallable(mapFn)) { + throw new TypeError('Array.from: when provided, the second argument must be a function'); + } + + // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined. + if (arguments.length > 2) { + T = arguments[2]; + } + } + + // 10. Let lenValue be Get(items, "length"). + // 11. Let len be ToLength(lenValue). + var len = toLength(items.length); + + // 13. If IsConstructor(C) is true, then + // 13. a. Let A be the result of calling the [[Construct]] internal method + // of C with an argument list containing the single item len. + // 14. a. Else, Let A be ArrayCreate(len). + var A = isCallable(C) ? Object(new C(len)) : new Array(len); + + // 16. Let k be 0. + var k = 0; + // 17. Repeat, while k < len… (also steps a - h) + var kValue; + while (k < len) { + kValue = items[k]; + if (mapFn) { + A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k); + } else { + A[k] = kValue; + } + k += 1; + } + // 18. Let putStatus be Put(A, "length", len, true). + A.length = len; + // 20. Return A. + return A; + }; + }()); } \ No newline at end of file diff --git a/src/gridstack.ts b/src/gridstack.ts index a5fd986f3..59b8ffc98 100644 --- a/src/gridstack.ts +++ b/src/gridstack.ts @@ -5,7 +5,6 @@ * Copyright (c) 2021 Alain Dumesny * see root license https://github.com/gridstack/gridstack.js/tree/master/LICENSE */ - import { GridStackEngine } from './gridstack-engine'; import { Utils, HeightData } from './utils'; import { ColumnOptions, GridItemHTMLElement, GridStackElement, GridStackEventHandlerCallback, @@ -164,7 +163,7 @@ export class GridStack { // create the grid element, but check if the passed 'parent' already has grid styling and should be used instead let el = parent; if (!parent.classList.contains('grid-stack')) { - let doc = document.implementation.createHTMLDocument(); + let doc = document.implementation.createHTMLDocument(""); doc.body.innerHTML = ``; el = doc.body.children[0] as HTMLElement; parent.appendChild(el); @@ -400,13 +399,13 @@ export class GridStack { let el: HTMLElement; if (typeof els === 'string') { - let doc = document.implementation.createHTMLDocument(); + let doc = document.implementation.createHTMLDocument(""); doc.body.innerHTML = els; el = doc.body.children[0] as HTMLElement; } else if (arguments.length === 0 || arguments.length === 1 && isGridStackWidget(els)) { let content = els ? (els as GridStackWidget).content || '' : ''; options = els; - let doc = document.implementation.createHTMLDocument(); + let doc = document.implementation.createHTMLDocument(""); doc.body.innerHTML = `This example uses new v3.1 API to load the entire nested grid from JSON, and shows dragging between nested grid items (pink) vs dragging higher grid items (green)
-Note: HTML5 release doesn't yet support 'dragOut:false' constrain so use JQ version if you need that.
+This example uses new v3.1 API to load the entire nested grid from JSON, and shows dragging between nested grid items (pink) vs dragging higher items (green)
+Note: HTML5 release doesn't yet support 'dragOut:false' constrain so use JQ version if you need that (nested 2 case).
Add Widget Add Widget Grid1 Add Widget Grid2 @@ -46,22 +51,27 @@This example shows sub-grids only accepting pink items, while parent accept all.
+ Add Widget + Add Widget Grid1 + Add Widget Grid2 + entire save/re-create: + Save + Destroy + Create + partial save/load: + Save list + Save no content + Clear + Load +