This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Feature/ExternalScriptLoader): Add with GoogleMaps support (#236)
* feat(Feature/ExternalScriptLoader): Added ExternalScriptLoader feature for APIs and other external r * refactor(Feature-ExternalScriptLoader): Removed filler word
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import $ from 'jquery' | ||
|
||
let status | ||
|
||
export function init (options) { | ||
if (!status) { | ||
const apiKey = options.apiKey | ||
const callback = `googleMaps_${Date.now()}` | ||
const url = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&callback=${callback}` | ||
status = new Promise((resolve, reject) => { | ||
window[callback] = () => { | ||
resolve() | ||
} | ||
}) | ||
$.getScript(url) | ||
} | ||
return status | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Flynt\Features\ExternalScriptLoader; | ||
|
||
use Flynt\Utils\Asset; | ||
|
||
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\'); | ||
|
||
function enqueueComponentScripts() | ||
{ | ||
Asset::enqueue([ | ||
'type' => 'script', | ||
'name' => 'Flynt/Features/ExternalScriptLoader', | ||
'path' => 'Features/ExternalScriptLoader/script.js' | ||
]); | ||
} | ||
|
||
add_action('wp_enqueue_scripts', NS . 'enqueueComponentScripts'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
let providers = {} | ||
|
||
// load all files from ./Providers folder into providers variable | ||
function importAll (moduleRequire) { | ||
moduleRequire.keys().forEach(key => { | ||
providers[key.substr(2, key.length - 5)] = moduleRequire(key).init | ||
}) | ||
} | ||
importAll(require.context('./Providers/', true, /\.js$/)) | ||
|
||
let instance | ||
|
||
class ExternalScriptLoader { | ||
constructor () { | ||
if (!instance) { | ||
instance = this | ||
} | ||
return instance | ||
} | ||
|
||
static getInstance () { | ||
return instance || new ExternalScriptLoader() | ||
} | ||
|
||
initialize (type, options = {}) { | ||
if (!this[type]) { | ||
this[type] = providers[type](options) | ||
} | ||
return this[type] | ||
} | ||
} | ||
|
||
window.FlyntExternalScriptLoader = ExternalScriptLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters