Aleksandr Aleksandrov 7 жил өмнө
commit
eec5cebd7f
7 өөрчлөгдсөн 371 нэмэгдсэн , 0 устгасан
  1. 37 0
      .gitignore
  2. 21 0
      package.json
  3. 55 0
      src/size.ts
  4. 93 0
      src/watch.ts
  5. 37 0
      tools/prebuild.js
  6. 23 0
      tsconfig.json
  7. 105 0
      yarn.lock

+ 37 - 0
.gitignore

@@ -0,0 +1,37 @@
+# See http://help.github.com/ignore-files/ for more about ignoring files.
+
+# compiled output
+/dist
+
+# dependencies
+/node_modules
+
+# IDEs and editors
+/.idea
+.project
+.classpath
+.c9/
+*.launch
+.settings/
+*.sublime-workspace
+
+# IDE - VSCode
+.vscode/*
+!.vscode/settings.json
+!.vscode/tasks.json
+!.vscode/launch.json
+!.vscode/extensions.json
+
+# misc
+/.sass-cache
+/connect.lock
+/coverage
+/libpeerconnection.log
+npm-debug.log
+yarn-error.log
+testem.log
+/typings
+
+# System Files
+.DS_Store
+Thumbs.db

+ 21 - 0
package.json

@@ -0,0 +1,21 @@
+{
+  "name": "@ex-helpers/element",
+  "version": "0.1.0",
+  "description": "Several element wrapper helpers",
+  "scripts": {
+    "postinstall": "tsc",
+    "build": "node tools/prebuild.js && tsc && npm pack"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "@angular/core": "^6.1.10",
+    "@ex-helpers/stack": "https://git.desudesu.ru/deswolrd/ex-helpers-stack.git",
+    "rxjs": "^6.0.0",
+    "typescript": "^2",
+    "zone.js": "~0.8.26"
+  },
+  "devDependencies": {
+    "rimraf": "^2.6.2"
+  }
+}

+ 55 - 0
src/size.ts

@@ -0,0 +1,55 @@
+export function getAbsoluteScrollAndOffset(el: HTMLElement) {
+    let _x = 0;
+    let _y = 0;
+    let _sx = window.scrollX;
+    let _sy = window.scrollY;
+    while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
+        _x += el.offsetLeft - el.scrollLeft;
+        _y += el.offsetTop - el.scrollTop;
+        _sx += el.scrollLeft;
+        _sy += el.scrollTop;
+        el = el.offsetParent as HTMLElement;
+    }
+    return {offset: {top: _y, left: _x}, scroll: {top: _sy, left: _sx}};
+}
+
+export interface SizeInterface {
+    size: { height: number, width: number };
+    offset: { top: number, left: number };
+    scroll: { top: number, left: number };
+}
+
+export function getSize(el: HTMLElement) {
+    return {
+        height: el.clientHeight,
+        width: el.clientWidth,
+    };
+}
+
+export function getDimensions(el: HTMLElement): SizeInterface {
+    const data = getAbsoluteScrollAndOffset(el);
+    return {
+        size: getSize(el),
+        scroll: data.scroll,
+        offset: data.offset
+    };
+}
+
+export function getBodySize(document: any) {
+    return {
+        height: Math.max(
+            document.documentElement.clientHeight,
+            document.body.scrollHeight,
+            document.documentElement.scrollHeight,
+            document.body.offsetHeight,
+            document.documentElement.offsetHeight
+        ),
+        width: Math.max(
+            document.documentElement.clientWidth,
+            document.body.scrollWidth,
+            document.documentElement.scrollWidth,
+            document.body.offsetWidth,
+            document.documentElement.offsetWidth
+        )
+    };
+}

+ 93 - 0
src/watch.ts

@@ -0,0 +1,93 @@
+import {ElementRef} from '@angular/core';
+import {getSize} from './size';
+import extend from 'lodash/extend';
+import {IncrementalStackManager} from '@ex-helpers/stack';
+
+declare module '@angular/core' {
+    class ElementRef {
+        nativeElement: any;
+
+        constructor(nativeElement: any);
+
+        watchSize: (callback: (height: number, width: number) => void,
+                    options?: { attributes: boolean, childList: boolean, characterData: boolean },
+                    params?: { legacy: boolean, timeout?: number }) => () => void;
+
+        triggerSizeCheck: () => void;
+    }
+}
+
+interface SizeCollectionItem {
+    triggerCheck: () => void;
+    startCountdown: () => void;
+    callback: (height: number, width: number) => void;
+    observer?: MutationObserver;
+    id?: number;
+    legacy?: boolean;
+}
+
+const DEFAULT_OPTIONS = {
+    attributes: true,
+    childList: true,
+    characterData: true
+};
+
+(ElementRef as any).prototype.watchSize = function
+(callback: (height: number, width: number) => void,
+ options?: { attributes?: boolean, childList?: boolean, characterData?: boolean },
+ params: { timeout?: number, legacy: boolean } = {legacy: false}) {
+    if (!this._watchSizeCollection) {
+        Object.defineProperty(this, '_watchSizeCollection', {
+            enumerable: false,
+            configurable: true,
+            writable: false,
+            value: new IncrementalStackManager<SizeCollectionItem>()
+        });
+    }
+    options = extend(DEFAULT_OPTIONS, options);
+    let timeout: any;
+    let lastHeight: number;
+    let lastWidth: number;
+    const triggerCheck = () => {
+        const sizes = getSize(this.nativeElement);
+        if (lastWidth === sizes.width && lastHeight === sizes.height) {
+            return;
+        }
+        lastHeight = sizes.height;
+        lastWidth = sizes.width;
+        callback(sizes.height, sizes.width);
+    };
+    const startCountdown = () => {
+        clearTimeout(timeout);
+        timeout = setTimeout(() => {
+            triggerCheck();
+        }, params.timeout || 100);
+    };
+    const item = this._watchSizeCollection.add({
+        callback,
+        startCountdown
+    });
+    if (MutationObserver && params.legacy === false) {
+        const observer = new MutationObserver(mutations => {
+            startCountdown();
+        });
+        item.observer = observer;
+        item.legacy = false;
+        startCountdown();
+        observer.observe(this.nativeElement, options);
+        return () => observer.disconnect();
+    } else {
+        item.legacy = true;
+        const checkInterval = setInterval(() => {
+            triggerCheck();
+        }, 200);
+        startCountdown();
+        return () => clearInterval(checkInterval);
+    }
+};
+
+(ElementRef as any).prototype.triggerSizeCheck = function () {
+    if (this._watchSizeCollection) {
+        this._watchSizeCollection.call('triggerCheck');
+    }
+};

+ 37 - 0
tools/prebuild.js

@@ -0,0 +1,37 @@
+const rimraf = require('rimraf');
+const path = require('path');
+const fs = require('fs');
+const util = require('util');
+const forEach = require('lodash/forEach');
+
+const root = path.resolve(__dirname, '..');
+const distPath = path.resolve(__dirname, '..', 'dist');
+
+console.log('Removing', distPath);
+const distClearPromise = util.promisify(rimraf)(distPath);
+
+const oldPacksPromise = new Promise((resolve, reject) => {
+    util.promisify(fs.readdir)(root)
+        .then(files => {
+            const promises = [];
+            forEach(files, file => {
+                if (/\.tgz$/.test(file)) {
+                    const fullPath = path.resolve(root, file);
+                    console.log('Removing', fullPath);
+                    promises.push(util.promisify(fs.unlink)(fullPath));
+                }
+            });
+            Promise.all(promises).then(resolve, reject);
+        }, reject);
+});
+
+Promise.all([
+    distClearPromise,
+    oldPacksPromise
+])
+    .then(() => {
+        console.log('Done');
+        process.exit()
+    }, e => {
+        console.error(e);
+    });

+ 23 - 0
tsconfig.json

@@ -0,0 +1,23 @@
+{
+    "compileOnSave": false,
+    "compilerOptions": {
+        "baseUrl": "src",
+        "outDir": "./dist",
+        "sourceMap": true,
+        "declaration": true,
+        "module": "commonjs",
+        "moduleResolution": "node",
+        "emitDecoratorMetadata": true,
+        "experimentalDecorators": true,
+        "allowSyntheticDefaultImports": true,
+        "esModuleInterop": true,
+        "target": "es5",
+        "typeRoots": [
+            "node_modules/@types"
+        ],
+        "lib": [
+            "es2017",
+            "dom"
+        ]
+    }
+}

+ 105 - 0
yarn.lock

@@ -0,0 +1,105 @@
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
+# yarn lockfile v1
+
+
+"@angular/core@^6.1.10":
+  version "6.1.10"
+  resolved "https://registry.yarnpkg.com/@angular/core/-/core-6.1.10.tgz#9f3d8b7e44ec0a68c88745d25a920f98bfb2bc43"
+  dependencies:
+    tslib "^1.9.0"
+
+"@ex-helpers/stack@https://git.desudesu.ru/deswolrd/ex-helpers-stack.git":
+  version "0.1.1"
+  resolved "https://git.desudesu.ru/deswolrd/ex-helpers-stack.git#892e7f7b1988ebe4ffbe61fcfa60ad900304be51"
+  dependencies:
+    lodash "^4.17.11"
+    rxjs "^6"
+
+balanced-match@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
+
+brace-expansion@^1.1.7:
+  version "1.1.11"
+  resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
+  dependencies:
+    balanced-match "^1.0.0"
+    concat-map "0.0.1"
+
+concat-map@0.0.1:
+  version "0.0.1"
+  resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
+
+fs.realpath@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
+
+glob@^7.0.5:
+  version "7.1.3"
+  resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1"
+  dependencies:
+    fs.realpath "^1.0.0"
+    inflight "^1.0.4"
+    inherits "2"
+    minimatch "^3.0.4"
+    once "^1.3.0"
+    path-is-absolute "^1.0.0"
+
+inflight@^1.0.4:
+  version "1.0.6"
+  resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
+  dependencies:
+    once "^1.3.0"
+    wrappy "1"
+
+inherits@2:
+  version "2.0.3"
+  resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
+
+lodash@^4.17.11:
+  version "4.17.11"
+  resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
+
+minimatch@^3.0.4:
+  version "3.0.4"
+  resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
+  dependencies:
+    brace-expansion "^1.1.7"
+
+once@^1.3.0:
+  version "1.4.0"
+  resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
+  dependencies:
+    wrappy "1"
+
+path-is-absolute@^1.0.0:
+  version "1.0.1"
+  resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
+
+rimraf@^2.6.2:
+  version "2.6.2"
+  resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
+  dependencies:
+    glob "^7.0.5"
+
+rxjs@^6, rxjs@^6.0.0:
+  version "6.3.3"
+  resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.3.3.tgz#3c6a7fa420e844a81390fb1158a9ec614f4bad55"
+  dependencies:
+    tslib "^1.9.0"
+
+tslib@^1.9.0:
+  version "1.9.3"
+  resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
+
+typescript@^2:
+  version "2.9.2"
+  resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c"
+
+wrappy@1:
+  version "1.0.2"
+  resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
+
+zone.js@~0.8.26:
+  version "0.8.26"
+  resolved "https://registry.yarnpkg.com/zone.js/-/zone.js-0.8.26.tgz#7bdd72f7668c5a7ad6b118148b4ea39c59d08d2d"