From 84eb3399f1ee8840784faf6bbbfcf53fd6f04455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Thu, 30 Jan 2020 19:21:53 +0800 Subject: [PATCH 01/35] chore($package.json): update version to 1.1.0-beta feature/performance-enhancement-jan-30-2020 --- .env | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 03faa44..5a250c4 100644 --- a/.env +++ b/.env @@ -2,4 +2,4 @@ VUE_APP_PACKAGE_JSON='' # TAG must be corresponding with the version tag in package.json, need to modify it when new version releases -TAG=1.0.2 +TAG=1.1.0-beta diff --git a/package.json b/package.json index 2edae30..223ab1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "exrx-net-crawler", - "version": "1.0.2", + "version": "1.1.0-beta", "license": "Apache-2.0", "description": "The Crawler for ExRx.net", "author": { From 34e69270cc985843c21472f0b83819d4f9912483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Wed, 5 Feb 2020 17:35:35 +0800 Subject: [PATCH 02/35] feat($MultiWindow): support multi window communication support multi window communication BREAKING CHANGE: support multi window communication --- src/components/HelloWorld.vue | 17 +++++- src/main.ts | 2 + src/mixin/multi-window.ts | 93 +++++++++++++++++++++++++++++++++ src/router/index.ts | 5 ++ src/types.d.ts | 25 +++++++++ src/views/hello-world/index.vue | 17 ------ src/views/second-page/index.vue | 23 ++++++++ 7 files changed, 164 insertions(+), 18 deletions(-) create mode 100644 src/mixin/multi-window.ts create mode 100644 src/types.d.ts delete mode 100644 src/views/hello-world/index.vue create mode 100644 src/views/second-page/index.vue diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index 1573e87..8d7452c 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -18,8 +18,10 @@ Test Back-end Service + Open New Page - {{ response }} + Response: {{ response }} + Value from sub window: {{ JSON.stringify(valueFromSubWindow) }} @@ -36,6 +38,7 @@ export default class HelloWorld extends Vue { private version = '' private environment = '' private response = null as any + private valueFromSubWindow = null as any async mounted () { this.version = AppUtil.getVersionInfo() @@ -53,5 +56,17 @@ export default class HelloWorld extends Vue { this.$toast.error(error.message) } } + + handleClickOpenNewPage () { + this.openWindow(this, '/second-page', { + callback: 'logCallback', + version: 'this.version' + }) + } + + logCallback (value: any) { + console.info('logCallback', value) + this.valueFromSubWindow = value + } } diff --git a/src/main.ts b/src/main.ts index 80b9082..b27d9a8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,8 +9,10 @@ import '@/directives/throttled-click' import '@/directives/debounced-click' import { listenToColorScheme } from '@/plugins/adaptive-color-scheme' // import '@/plugins/axios-mock-adapter' +import { MultiWindow } from '@/mixin/multi-window' Vue.config.productionTip = false +Vue.mixin(MultiWindow) const vueInstance = new Vue({ router, diff --git a/src/mixin/multi-window.ts b/src/mixin/multi-window.ts new file mode 100644 index 0000000..b1503c2 --- /dev/null +++ b/src/mixin/multi-window.ts @@ -0,0 +1,93 @@ +import { Component, Vue } from 'vue-property-decorator' + +/** + * Multi Window Options + */ +export interface MultiWindowOptions { + /** + * Whether delay closing opened window for 300 ms. Default: true + */ + delayClosingWindow?: boolean + + /** + * Vue instance of opener callback function name + */ + callback?: string + + /** + * Other data + */ + [key: string]: any +} + +/** + * Multi window mixin + * + * Can i use mixins global in vue? i am use typescript with vue + * @see GitHub + */ +@Component +export class MultiWindow extends Vue { + private $multiWindowOptions: MultiWindowOptions = { + delayClosingWindow: true, + callback: '' + } + + mounted () { + this.$data.$multiWindowOptions.delayClosingWindow = this.$route.query.delayClosingWindow === 'true' + this.$data.$multiWindowOptions.callback = this.$route.query.callback + } + + openWindow (context: Vue, url: string, multiWindowOptions?: MultiWindowOptions): void { + // Set default delayClosingWindow as true + if (multiWindowOptions?.delayClosingWindow === undefined) { + // @ts-ignore + multiWindowOptions.delayClosingWindow = true + } + const path = /\/$/.test(url) ? url : `${url}/` + // the value of process.env.BASE_URL is equal to the `publicPath` configured in vue.config.js + const baseUrl = process.env.BASE_URL + const target = /^(http|https):\/\//.test(path) ? url : `${baseUrl}#${url}` + // Passing information to opened window by query string + let queryString = '' + for (const key in multiWindowOptions) { + // Determines whether an object has a property with the specified name. + // eslint-disable-next-line no-prototype-builtins + if (!multiWindowOptions.hasOwnProperty(key)) { + break + } + const val = ((multiWindowOptions[key] === null) || (multiWindowOptions[key] === undefined) ? '' : multiWindowOptions[key]) + queryString += queryString === '' ? `?${key}=${val}` : `&${key}=${val}` + } + const newWindow = window.open(`${target}${queryString}`) + if (!newWindow) { + window.alert('Please give us permission to open new page!') + } else { + newWindow.opener.$vue = context + } + } + + windowBack (argument?: any): void { + const context = window.opener.$vue + const callback = this.$data.$multiWindowOptions.callback + const delayClosingWindow = this.$data.$multiWindowOptions.delayClosingWindow + if (!context) { + window.alert('Cannot find context!') + return + } + // Check whether opener's callback is valid + if (callback && typeof context[callback] === 'function') { + context[callback](argument) + } else { + window.alert('Cannot find callback!') + return + } + if (delayClosingWindow) { + setTimeout(() => { + window.close() + }, 300) + } else { + window.close() + } + } +} diff --git a/src/router/index.ts b/src/router/index.ts index d4f7d53..1cf3f23 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -27,6 +27,11 @@ const routes = [ component: () => import(/* webpackChunkName: "exrx-net" */ '@/views/exrx-net/index.vue') } ] + }, + { + name: 'second-page', + path: '/second-page', + component: () => import(/* webpackChunkName: "second-page" */ '@/views/second-page/index.vue') } ] diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..890565a --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,25 @@ +/* eslint-disable no-unused-vars */ +import { MultiWindowOptions } from '@/mixin/multi-window' + +declare module 'vue/types/vue' { + /** + * Can i use mixins global in vue? i am use typescript with vue + * https://github.com/kaorun343/vue-property-decorator/issues/226#issuecomment-515568960 + */ + interface Vue { + /** + * Open a new window + * @param context opener's Vue instance + * @param url the page URL to be opened + * @param multiWindowOptions the parameters to be passed to the opened window + */ + openWindow (context: Vue, url: string, multiWindowOptions?: MultiWindowOptions): void + + /** + * Back to last window (close opened window). + * + * You may pass an object back to last window by setting an object argument to MultiWindow.windowBack() + */ + windowBack (argument?: any): void + } +} diff --git a/src/views/hello-world/index.vue b/src/views/hello-world/index.vue deleted file mode 100644 index b44e1e9..0000000 --- a/src/views/hello-world/index.vue +++ /dev/null @@ -1,17 +0,0 @@ - - - diff --git a/src/views/second-page/index.vue b/src/views/second-page/index.vue new file mode 100644 index 0000000..ecbeb46 --- /dev/null +++ b/src/views/second-page/index.vue @@ -0,0 +1,23 @@ + + + + + From 3aa0c9a62bcd4e476cb2c6c98cfad64323f7bb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Wed, 5 Feb 2020 17:52:53 +0800 Subject: [PATCH 03/35] feat($MultiWindow): support opening a unique window --- src/components/HelloWorld.vue | 3 ++- src/mixin/multi-window.ts | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index 8d7452c..680a1e8 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -60,7 +60,8 @@ export default class HelloWorld extends Vue { handleClickOpenNewPage () { this.openWindow(this, '/second-page', { callback: 'logCallback', - version: 'this.version' + windowTarget: 'secondPage', + version: this.version }) } diff --git a/src/mixin/multi-window.ts b/src/mixin/multi-window.ts index b1503c2..f88b604 100644 --- a/src/mixin/multi-window.ts +++ b/src/mixin/multi-window.ts @@ -4,6 +4,12 @@ import { Component, Vue } from 'vue-property-decorator' * Multi Window Options */ export interface MultiWindowOptions { + /** + * A DOMString specifying the name of the browsing context (window,