Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Commit

Permalink
feat(Feature/ExternalScriptLoader): Add with GoogleMaps support (#236)
Browse files Browse the repository at this point in the history
* feat(Feature/ExternalScriptLoader): Added ExternalScriptLoader feature for APIs and other external r

* refactor(Feature-ExternalScriptLoader): Removed filler word
  • Loading branch information
Dome authored and domtra committed Jul 3, 2017
1 parent 1877e13 commit cf5c83e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Features/ExternalScriptLoader/Providers/googleMaps.js
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
}
17 changes: 17 additions & 0 deletions Features/ExternalScriptLoader/functions.php
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');
33 changes: 33 additions & 0 deletions Features/ExternalScriptLoader/script.js
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
1 change: 1 addition & 0 deletions lib/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function initTheme()
add_theme_support('post-formats', array('aside', 'gallery', 'link', 'image', 'quote', 'video', 'audio'));

add_theme_support('flynt-password-form');
add_theme_support('flynt-external-script-loader');
}
add_action('after_setup_theme', __NAMESPACE__ . '\\initTheme');

Expand Down

0 comments on commit cf5c83e

Please sign in to comment.