vue-gmaps
Version:
Vue.js plugin that allow search address and places using Google Maps API
206 lines (167 loc) • 6.21 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _loadGoogleMapsApi = __webpack_require__(1);
var _loadGoogleMapsApi2 = _interopRequireDefault(_loadGoogleMapsApi);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function plugin(Vue) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$libraries = _ref.libraries,
libraries = _ref$libraries === undefined ? ['places'] : _ref$libraries,
key = _ref.key,
client = _ref.client,
_ref$version = _ref.version,
version = _ref$version === undefined ? '3' : _ref$version,
_ref$loadGoogleApi = _ref.loadGoogleApi,
loadGoogleApi = _ref$loadGoogleApi === undefined ? true : _ref$loadGoogleApi;
if (plugin.installed) {
return;
}
Vue.directive('gmaps-searchbox', {
inserted: function inserted(el, binding) {
var propertyToSet = binding.arg ? binding.arg : 'place';
ensureGoogleMaps(function (google) {
var searchBox = new google.places.SearchBox(el);
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length === 0) {
return;
}
var place = {};
var originalPlace = places[0];
var keys = Object.keys(binding.modifiers);
if (keys.length > 0) {
keys.forEach(function (key) {
place[key] = originalPlace[key];
});
} else {
place = originalPlace;
}
Vue.set(binding.value, propertyToSet, place);
});
});
}
});
function ensureGoogleMaps(fn) {
if (!loadGoogleApi) {
fn(window.google.maps ? window.google.maps : window.google);
} else if (Vue.google) {
fn(Vue.google);
} else {
(0, _loadGoogleMapsApi2.default)({
key: key, client: client, libraries: libraries, v: version
}).then(function (google) {
Vue.google = google;
Vue.prototype.$google = google;
fn(google);
});
}
}
}
plugin.version = '0.0.9';
exports.default = plugin;
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(plugin);
}
/***/ },
/* 1 */
/***/ function(module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function () {
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var client = _ref.client;
var key = _ref.key;
var language = _ref.language;
var _ref$libraries = _ref.libraries;
var libraries = _ref$libraries === undefined ? [] : _ref$libraries;
var _ref$timeout = _ref.timeout;
var timeout = _ref$timeout === undefined ? 10000 : _ref$timeout;
var v = _ref.v;
var callbackName = '__googleMapsApiOnLoadCallback';
return new Promise(function (resolve, reject) {
// Exit if not running inside a browser.
if (typeof window === 'undefined') {
return reject(new Error('Can only load the Google Maps API in the browser'));
}
// Prepare the `script` tag to be inserted into the page.
var scriptElement = document.createElement('script');
var params = ['callback=' + callbackName];
if (client) params.push('client=' + client);
if (key) params.push('key=' + key);
if (language) params.push('language=' + language);
libraries = [].concat(libraries); // Ensure that `libraries` is an array
if (libraries.length) params.push('libraries=' + libraries.join(','));
if (v) params.push('v=' + v);
scriptElement.src = 'https://maps.googleapis.com/maps/api/js?' + params.join('&');
// Timeout if necessary.
var timeoutId = null;
if (timeout) {
timeoutId = setTimeout(function () {
window[callbackName] = function () {}; // Set the on load callback to a no-op.
reject(new Error('Could not load the Google Maps API'));
}, timeout);
}
// Hook up the on load callback.
window[callbackName] = function () {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
resolve(window.google.maps);
delete window[callbackName];
};
// Insert the `script` tag.
document.body.appendChild(scriptElement);
});
};
/***/ }
/******/ ])
});
;