Improve code quality
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"presets": ["@babel/preset-env"],
|
||||
"plugins": [
|
||||
"@babel/plugin-proposal-object-rest-spread",
|
||||
"transform-inline-environment-variables",
|
||||
"minify-dead-code-elimination"
|
||||
]
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import lazy from './utils/lazyValue'
|
||||
import { loadGmapApi } from './manager'
|
||||
import { loadGMapApi } from './manager'
|
||||
import { createApp } from 'vue'
|
||||
import Marker from './components/marker'
|
||||
import Polyline from './components/polyline'
|
||||
@@ -18,7 +18,7 @@ let GmapApi = null
|
||||
|
||||
// export everything
|
||||
export {
|
||||
loadGmapApi,
|
||||
loadGMapApi,
|
||||
Marker,
|
||||
Polyline,
|
||||
Polygon,
|
||||
@@ -100,7 +100,7 @@ function makeGmapApiPromiseLazy(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
window['vueGoogleMapsInit'] = resolve
|
||||
loadGmapApi(options.load, options.loadCn)
|
||||
loadGMapApi(options.load, options.loadCn)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
|
||||
@@ -1,74 +1,16 @@
|
||||
let isApiSetUp = false
|
||||
import {Env} from "@/utils/env";
|
||||
import {createMapScript} from "@/utils/create-map-script";
|
||||
|
||||
/**
|
||||
* @param apiKey API Key, or object with the URL parameters. For example
|
||||
* to use Google Maps Premium API, pass
|
||||
* `{ client: <YOUR-CLIENT-ID> }`.
|
||||
* You may pass the libraries and/or version (as `v`) parameter into
|
||||
* this parameter and skip the next two parameters
|
||||
* @param version Google Maps version
|
||||
* @param libraries Libraries to load (@see
|
||||
* https://developers.google.com/maps/documentation/javascript/libraries)
|
||||
* @param loadCn Boolean. If set to true, the map will be loaded from google maps China
|
||||
* (@see https://developers.google.com/maps/documentation/javascript/basics#GoogleMapsChina)
|
||||
*
|
||||
* Example:
|
||||
* ```
|
||||
* import {load} from 'vue-google-maps'
|
||||
*
|
||||
* load(<YOUR-API-KEY>)
|
||||
*
|
||||
* load({
|
||||
* key: <YOUR-API-KEY>,
|
||||
* })
|
||||
*
|
||||
* load({
|
||||
* client: <YOUR-CLIENT-ID>,
|
||||
* channel: <YOUR CHANNEL>
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
export const loadGmapApi = (options, loadCn) => {
|
||||
if (typeof document === 'undefined') {
|
||||
// Do nothing if run from server-side
|
||||
return
|
||||
let isApiSetUp = false
|
||||
export function loadGMapApi (options) {
|
||||
|
||||
if (Env.isServer()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isApiSetUp) {
|
||||
isApiSetUp = true
|
||||
|
||||
const googleMapScript = document.createElement('SCRIPT')
|
||||
|
||||
// Allow options to be an object.
|
||||
// This is to support more esoteric means of loading Google Maps,
|
||||
// such as Google for business
|
||||
// https://developers.google.com/maps/documentation/javascript/get-api-key#premium-auth
|
||||
if (typeof options !== 'object') {
|
||||
throw new Error('options should be an object')
|
||||
}
|
||||
|
||||
// libraries
|
||||
/* eslint-disable no-prototype-builtins */
|
||||
if (Array.prototype.isPrototypeOf(options.libraries)) {
|
||||
options.libraries = options.libraries.join(',')
|
||||
}
|
||||
options['callback'] = 'vueGoogleMapsInit'
|
||||
|
||||
let baseUrl = 'https://maps.googleapis.com/'
|
||||
|
||||
if (typeof loadCn === 'boolean' && loadCn === true) {
|
||||
baseUrl = 'https://maps.google.cn/'
|
||||
}
|
||||
|
||||
let url =
|
||||
baseUrl +
|
||||
'maps/api/js?' +
|
||||
Object.keys(options)
|
||||
.map((key) => encodeURIComponent(key) + '=' + encodeURIComponent(options[key]))
|
||||
.join('&')
|
||||
|
||||
googleMapScript.setAttribute('src', url)
|
||||
googleMapScript.setAttribute('async', '')
|
||||
googleMapScript.setAttribute('defer', '')
|
||||
const googleMapScript = createMapScript(options);
|
||||
document.head.appendChild(googleMapScript)
|
||||
} else {
|
||||
throw new Error('You already started the loading of google maps')
|
||||
|
||||
32
src/utils/create-map-script.js
Normal file
32
src/utils/create-map-script.js
Normal file
@@ -0,0 +1,32 @@
|
||||
export function createMapScript(options) {
|
||||
const googleMapScript = document.createElement('SCRIPT')
|
||||
|
||||
// Allow options to be an object.
|
||||
// This is to support more esoteric means of loading Google Maps,
|
||||
// such as Google for business
|
||||
// https://developers.google.com/maps/documentation/javascript/get-api-key#premium-auth
|
||||
if (typeof options !== 'object') {
|
||||
throw new Error('options should be an object')
|
||||
}
|
||||
|
||||
// libraries
|
||||
/* eslint-disable no-prototype-builtins */
|
||||
if (Array.prototype.isPrototypeOf(options.libraries)) {
|
||||
options.libraries = options.libraries.join(',')
|
||||
}
|
||||
options['callback'] = 'vueGoogleMapsInit'
|
||||
let baseUrl = 'https://maps.googleapis.com/'
|
||||
|
||||
let url =
|
||||
baseUrl +
|
||||
'maps/api/js?' +
|
||||
Object.keys(options)
|
||||
.map((key) => encodeURIComponent(key) + '=' + encodeURIComponent(options[key]))
|
||||
.join('&')
|
||||
|
||||
googleMapScript.setAttribute('src', url)
|
||||
googleMapScript.setAttribute('async', '')
|
||||
googleMapScript.setAttribute('defer', '')
|
||||
|
||||
return googleMapScript;
|
||||
}
|
||||
5
src/utils/env.js
Normal file
5
src/utils/env.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export class Env {
|
||||
static isServer() {
|
||||
return typeof document === 'undefined';
|
||||
}
|
||||
}
|
||||
3
src/utils/event.js
Normal file
3
src/utils/event.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export class eventUtils {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user