Browse Source

more browser backwards compat, polyfills

Alexander Rose 5 years ago
parent
commit
85dcef1b2e

+ 2 - 2
src/mol-canvas3d/canvas3d.ts

@@ -261,8 +261,8 @@ namespace Canvas3D {
 
             if (oldRO) {
                 if (!SetUtils.areEqual(newRO, oldRO)) {
-                    for (const o of Array.from(newRO)) { if (!oldRO.has(o)) scene.add(o) }
-                    for (const o of Array.from(oldRO)) { if (!newRO.has(o)) scene.remove(o) }
+                    newRO.forEach(o => { if (!oldRO.has(o)) scene.add(o) })
+                    oldRO.forEach(o => { if (!newRO.has(o)) scene.remove(o) })
                 }
             } else {
                 repr.renderObjects.forEach(o => scene.add(o))

+ 1 - 1
src/mol-model-props/rcsb/util.ts

@@ -13,7 +13,7 @@ export function getAssemblyIds(units: ReadonlyArray<Unit>) {
     units.forEach(u => {
         if (u.conformation.operator.assembly) ids.add(u.conformation.operator.assembly.id)
     })
-    return Array.from(ids)
+    return Array.from(ids.values())
 }
 
 export function getSymmetrySelectParam(structure?: Structure) {

+ 19 - 19
src/mol-model/structure/model/types.ts

@@ -410,26 +410,26 @@ export function isProtein(moleculeType: MoleculeType) {
  * ) AS t1;
  */
 export const IonNames = new Set([
-  '118', '119', '1AL', '1CU', '2FK', '2HP', '2OF', '3CO',
-  '3MT', '3NI', '3OF', '3P8', '4MO', '4PU', '543', '6MO', 'ACT', 'AG', 'AL',
-  'ALF', 'AM', 'ATH', 'AU', 'AU3', 'AUC', 'AZI', 'BA', 'BCT', 'BEF', 'BF4', 'BO4',
-  'BR', 'BS3', 'BSY', 'CA', 'CAC', 'CD', 'CD1', 'CD3', 'CD5', 'CE', 'CHT', 'CL',
-  'CO', 'CO3', 'CO5', 'CON', 'CR', 'CS', 'CSB', 'CU', 'CU1', 'CU3', 'CUA', 'CUZ',
-  'CYN', 'DME', 'DMI', 'DSC', 'DTI', 'DY', 'E4N', 'EDR', 'EMC', 'ER3', 'EU',
-  'EU3', 'F', 'FE', 'FE2', 'FPO', 'GA', 'GD3', 'GEP', 'HAI', 'HG', 'HGC', 'IN',
-  'IOD', 'IR', 'IR3', 'IRI', 'IUM', 'K', 'KO4', 'LA', 'LCO', 'LCP', 'LI', 'LU',
-  'MAC', 'MG', 'MH2', 'MH3', 'MLI', 'MLT', 'MMC', 'MN', 'MN3', 'MN5', 'MN6',
-  'MO1', 'MO2', 'MO3', 'MO4', 'MO5', 'MO6', 'MOO', 'MOS', 'MOW', 'MW1', 'MW2',
-  'MW3', 'NA', 'NA2', 'NA5', 'NA6', 'NAO', 'NAW', 'NCO', 'NET', 'NH4', 'NI',
-  'NI1', 'NI2', 'NI3', 'NO2', 'NO3', 'NRU', 'O4M', 'OAA', 'OC1', 'OC2', 'OC3',
-  'OC4', 'OC5', 'OC6', 'OC7', 'OC8', 'OCL', 'OCM', 'OCN', 'OCO', 'OF1', 'OF2',
-  'OF3', 'OH', 'OS', 'OS4', 'OXL', 'PB', 'PBM', 'PD', 'PDV', 'PER', 'PI', 'PO3',
-  'PO4', 'PR', 'PT', 'PT4', 'PTN', 'RB', 'RH3', 'RHD', 'RU', 'SB', 'SCN', 'SE4',
-  'SEK', 'SM', 'SMO', 'SO3', 'SO4', 'SR', 'T1A', 'TB', 'TBA', 'TCN', 'TEA', 'TH',
-  'THE', 'TL', 'TMA', 'TRA', 'UNX', 'V', 'VN3', 'VO4', 'W', 'WO5', 'Y1', 'YB',
-  'YB2', 'YH', 'YT3', 'ZCM', 'ZN', 'ZN2', 'ZN3', 'ZNO', 'ZO3',
+    '118', '119', '1AL', '1CU', '2FK', '2HP', '2OF', '3CO',
+    '3MT', '3NI', '3OF', '3P8', '4MO', '4PU', '543', '6MO', 'ACT', 'AG', 'AL',
+    'ALF', 'AM', 'ATH', 'AU', 'AU3', 'AUC', 'AZI', 'BA', 'BCT', 'BEF', 'BF4', 'BO4',
+    'BR', 'BS3', 'BSY', 'CA', 'CAC', 'CD', 'CD1', 'CD3', 'CD5', 'CE', 'CHT', 'CL',
+    'CO', 'CO3', 'CO5', 'CON', 'CR', 'CS', 'CSB', 'CU', 'CU1', 'CU3', 'CUA', 'CUZ',
+    'CYN', 'DME', 'DMI', 'DSC', 'DTI', 'DY', 'E4N', 'EDR', 'EMC', 'ER3', 'EU',
+    'EU3', 'F', 'FE', 'FE2', 'FPO', 'GA', 'GD3', 'GEP', 'HAI', 'HG', 'HGC', 'IN',
+    'IOD', 'IR', 'IR3', 'IRI', 'IUM', 'K', 'KO4', 'LA', 'LCO', 'LCP', 'LI', 'LU',
+    'MAC', 'MG', 'MH2', 'MH3', 'MLI', 'MLT', 'MMC', 'MN', 'MN3', 'MN5', 'MN6',
+    'MO1', 'MO2', 'MO3', 'MO4', 'MO5', 'MO6', 'MOO', 'MOS', 'MOW', 'MW1', 'MW2',
+    'MW3', 'NA', 'NA2', 'NA5', 'NA6', 'NAO', 'NAW', 'NCO', 'NET', 'NH4', 'NI',
+    'NI1', 'NI2', 'NI3', 'NO2', 'NO3', 'NRU', 'O4M', 'OAA', 'OC1', 'OC2', 'OC3',
+    'OC4', 'OC5', 'OC6', 'OC7', 'OC8', 'OCL', 'OCM', 'OCN', 'OCO', 'OF1', 'OF2',
+    'OF3', 'OH', 'OS', 'OS4', 'OXL', 'PB', 'PBM', 'PD', 'PDV', 'PER', 'PI', 'PO3',
+    'PO4', 'PR', 'PT', 'PT4', 'PTN', 'RB', 'RH3', 'RHD', 'RU', 'SB', 'SCN', 'SE4',
+    'SEK', 'SM', 'SMO', 'SO3', 'SO4', 'SR', 'T1A', 'TB', 'TBA', 'TCN', 'TEA', 'TH',
+    'THE', 'TL', 'TMA', 'TRA', 'UNX', 'V', 'VN3', 'VO4', 'W', 'WO5', 'Y1', 'YB',
+    'YB2', 'YH', 'YT3', 'ZCM', 'ZN', 'ZN2', 'ZN3', 'ZNO', 'ZO3',
     // additional ion names
-  'OHX'
+    'OHX'
 ])
 
 export type SecondaryStructureType = BitFlags<SecondaryStructureType.Flag>

+ 1 - 1
src/mol-plugin/state/actions/data-format.ts

@@ -125,7 +125,7 @@ export const OpenFile = StateAction.build({
     params: (a, ctx: PluginContext) => {
         const { extensions, options } = ctx.dataFormat.registry
         return {
-            file: PD.File({ accept: Array.from(extensions).map(e => `.${e}`).join(',')}),
+            file: PD.File({ accept: Array.from(extensions.values()).map(e => `.${e}`).join(',')}),
             format: PD.Select('auto', options),
             visuals: PD.Boolean(true, { description: 'Add default visuals' }),
         }

+ 2 - 2
src/mol-plugin/util/structure-selection-helper.ts

@@ -68,7 +68,7 @@ const backbone = StructureSelectionQuery('Backbone', MS.struct.modifier.union([
                     ])
                 ]),
                 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
-                'atom-test': MS.core.set.has([MS.set(...Array.from(ProteinBackboneAtoms)), MS.ammp('label_atom_id')])
+                'atom-test': MS.core.set.has([MS.set(...Array.from(ProteinBackboneAtoms.values())), MS.ammp('label_atom_id')])
             })
         ]),
         MS.struct.modifier.union([
@@ -81,7 +81,7 @@ const backbone = StructureSelectionQuery('Backbone', MS.struct.modifier.union([
                     ])
                 ]),
                 'chain-test': MS.core.rel.eq([MS.ammp('objectPrimitive'), 'atomistic']),
-                'atom-test': MS.core.set.has([MS.set(...Array.from(NucleicBackboneAtoms)), MS.ammp('label_atom_id')])
+                'atom-test': MS.core.set.has([MS.set(...Array.from(NucleicBackboneAtoms.values())), MS.ammp('label_atom_id')])
             })
         ])
     ])

+ 1 - 1
src/mol-plugin/util/viewport-screenshot.ts

@@ -102,7 +102,7 @@ class ViewportScreenshotHelper {
         const models = this.plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Model)).map(s => s.obj!.data)
         const uniqueIds = new Set<string>()
         models.forEach(m => uniqueIds.add(m.entryId.toUpperCase()))
-        const idString = Array.from(uniqueIds).join('-')
+        const idString = Array.from(uniqueIds.values()).join('-')
         return `${idString || 'molstar-image'}.png`
     }
 

+ 1 - 1
src/mol-theme/color/operator-hkl.ts

@@ -63,7 +63,7 @@ function getOperatorHklSerialMap(structure: Structure) {
         const k = hklKey(structure.units[i].conformation.operator.hkl)
         set.add(k)
     }
-    const arr = Array.from(set).sort()
+    const arr = Array.from(set.values()).sort()
     arr.forEach(k => map.set(k, map.size))
     const min = hklKeySplit(arr[0])
     const max = hklKeySplit(arr[arr.length - 1])

+ 100 - 0
src/mol-util/polyfill.ts

@@ -592,4 +592,104 @@ if (!Array.prototype.fill) {
             return O;
         }
     });
+}
+
+if (!Array.prototype.copyWithin) {
+    Object.defineProperty(Array.prototype, 'copyWithin', {
+        value: function(target: any, start: any/* , end*/) {
+            // Steps 1-2.
+            if (this == null) {
+                throw new TypeError('this is null or not defined');
+            }
+
+            let O = Object(this);
+
+            // Steps 3-5.
+            let len = O.length >>> 0;
+
+            // Steps 6-8.
+            let relativeTarget = target >> 0;
+
+            let to = relativeTarget < 0 ?
+                Math.max(len + relativeTarget, 0) :
+                Math.min(relativeTarget, len);
+
+            // Steps 9-11.
+            let relativeStart = start >> 0;
+
+            let from = relativeStart < 0 ?
+                Math.max(len + relativeStart, 0) :
+                Math.min(relativeStart, len);
+
+            // Steps 12-14.
+            let end = arguments[2];
+            let relativeEnd = end === undefined ? len : end >> 0;
+
+            let final = relativeEnd < 0 ?
+                Math.max(len + relativeEnd, 0) :
+                Math.min(relativeEnd, len);
+
+            // Step 15.
+            let count = Math.min(final - from, len - to);
+
+            // Steps 16-17.
+            let direction = 1;
+
+            if (from < to && to < (from + count)) {
+                direction = -1;
+                from += count - 1;
+                to += count - 1;
+            }
+
+            // Step 18.
+            while (count > 0) {
+                if (from in O) {
+                    O[to] = O[from];
+                } else {
+                    delete O[to];
+                }
+
+                from += direction;
+                to += direction;
+                count--;
+            }
+
+            // Step 19.
+            return O;
+        },
+        configurable: true,
+        writable: true
+    });
+}
+
+if (!Int8Array.prototype.copyWithin) {
+    Object.defineProperty(Int8Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
+}
+
+if (!Int16Array.prototype.copyWithin) {
+    Object.defineProperty(Int16Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
+}
+
+if (!Int32Array.prototype.copyWithin) {
+    Object.defineProperty(Int32Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
+}
+
+if (!Uint8Array.prototype.copyWithin) {
+    Object.defineProperty(Uint8Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
+}
+
+if (!Uint16Array.prototype.copyWithin) {
+    Object.defineProperty(Uint16Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
+}
+
+if (!Uint32Array.prototype.copyWithin) {
+    Object.defineProperty(Uint32Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
+}
+
+if (!Float32Array.prototype.copyWithin) {
+    Object.defineProperty(Float32Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
+}
+
+if (!Float64Array.prototype.copyWithin) {
+    Object.defineProperty(Float64Array.prototype, 'copyWithin', { value: Array.prototype.copyWithin })
 }

+ 24 - 21
src/mol-util/set.ts

@@ -1,24 +1,25 @@
 /**
- * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-// TODO remove Array.from workaround when targeting ES6
+// TODO use set@@iterator when targeting es6
 
 export namespace SetUtils {
     /** Test if set a contains all elements of set b. */
     export function isSuperset<T>(setA: ReadonlySet<T>, setB: ReadonlySet<T>) {
-        for (const elm of Array.from(setB)) {
-            if (!setA.has(elm)) return false;
-        }
-        return true;
+        let flag = true
+        setB.forEach(elem => {
+            if (!setA.has(elem)) flag = false;
+        })
+        return flag
     }
 
     /** Add all elements from `sets` to `out` */
     export function add<T>(out: Set<T>, ...sets: ReadonlySet<T>[]): Set<T> {
         for (let i = 0; i < sets.length; i++) {
-            for (const elem of Array.from(sets[i])) out.add(elem);
+            sets[i].forEach(elem => out.add(elem))
         }
         return out;
     }
@@ -26,7 +27,7 @@ export namespace SetUtils {
     /** Create set containing elements of both set a and set b. */
     export function union<T>(setA: ReadonlySet<T>, setB: ReadonlySet<T>): Set<T> {
         const union = new Set(setA);
-        for (const elem of Array.from(setB)) union.add(elem);
+        setB.forEach(elem => union.add(elem))
         return union;
     }
 
@@ -34,8 +35,8 @@ export namespace SetUtils {
         if (sets.length === 0) return new Set<T>();
         if (sets.length === 1) new Set(sets[0]);
         const union = new Set(sets[0]);
-        for (let i = 1; i < sets.length; i++) {
-            for (const elem of Array.from(sets[i])) union.add(elem);
+        for (let i = 1, il = sets.length; i < il; i++) {
+            sets[i].forEach(elem => union.add(elem))
         }
         return union;
     }
@@ -52,32 +53,34 @@ export namespace SetUtils {
     /** Create set containing elements of set a that are also in set b. */
     export function intersection<T>(setA: ReadonlySet<T>, setB: ReadonlySet<T>): Set<T> {
         const intersection = new Set<T>();
-        for (const elem of Array.from(setB)) {
+        setB.forEach(elem => {
             if (setA.has(elem)) intersection.add(elem);
-        }
+        })
         return intersection;
     }
 
     export function areIntersecting<T>(setA: ReadonlySet<T>, setB: ReadonlySet<T>): boolean {
-        for (const elem of Array.from(setB)) {
-            if (setA.has(elem)) return true;
-        }
-        return false;
+        let flag = false
+        setB.forEach(elem => {
+            if (setA.has(elem)) flag = true;
+        })
+        return flag;
     }
 
     /** Create set containing elements of set a that are not in set b. */
     export function difference<T>(setA: ReadonlySet<T>, setB: ReadonlySet<T>): Set<T> {
         const difference = new Set(setA);
-        for (const elem of Array.from(setB)) difference.delete(elem);
+        setB.forEach(elem => difference.delete(elem))
         return difference;
     }
 
     /** Test if set a and b contain the same elements. */
     export function areEqual<T>(setA: ReadonlySet<T>, setB: ReadonlySet<T>) {
         if (setA.size !== setB.size) return false
-        for (const elm of Array.from(setB)) {
-            if (!setA.has(elm)) return false;
-        }
-        return true;
+        let flag = true
+        setB.forEach(elem => {
+            if (!setA.has(elem)) flag = false;
+        })
+        return flag;
     }
 }