Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KELiON committed Apr 26, 2017
0 parents commit 9c66f31
Show file tree
Hide file tree
Showing 17 changed files with 397 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
"transform-react-jsx",
"syntax-jsx"
]
}
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# http://editorconfig.org
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{json,js}]
indent_style = space
indent_size = 2
46 changes: 46 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Webstorm local folder
.idea

# Yarn dependecies lock file
yarn.lock

# Application local files
dist
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
src
.babelrc
node_modules
scripts
screenshot.png
webpack.config.js
22 changes: 22 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# cerebro-google-maps

> [Cerebro](http://www.cerebroapp.com) plugin quick search of location on google maps
![](screenshot.png)

## Usage

Just enter what you want to convert!

## Features
* Supports names of locations and addresses;
* Supports english and russian language (i.e. `карта москвы` or `new york on map`).

## Related

- [Cerebro](http://github.com/KELiON/cerebro) – main repo for Cerebro app;
- [cerebro-plugin](http://github.com/KELiON/cerebro-plugin) – boilerplate to create Cerebro plugins;

## License

MIT © [Alexandr Subbotin](http://asubbotin.ru)
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "cerebro-google-maps",
"version": "1.0.0",
"description": "Cerebro plugin for google maps search",
"license": "MIT",
"repository": "KELiON/cerebro-google-maps",
"author": {
"name": "Alexandr Subbotin",
"email": "[email protected]",
"url": "asubbotin.ru"
},
"engines": {
"node": ">=4"
},
"scripts": {
"build": "webpack && babili dist -d dist --compact --no-comments",
"debug": "./scripts/debug",
"debug:windows": "scripts\\debug.bat",
"prepublish": "rimraf ./dist && npm run build"
},
"main": "dist/index.js",
"keywords": [
"cerebro",
"cerebro-plugin",
"maps",
"search"
],
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-loader": "^6.2.8",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-react-jsx": "^6.8.0",
"babili": "0.0.9",
"cerebro-tools": "^0.1.0",
"css-loader": "^0.26.0",
"react": "15.4.2",
"rimraf": "^2.6.1",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "2.1.0-beta.27"
},
"dependencies": {
"react-dom": "15.4.2",
"react-google-maps": "4.11.0"
}
}
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions scripts/debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
if [[ $1 == "dev" ]]; then
appname="Electron"
else
appname="Cerebro"
fi

case "$(uname -s)" in

Darwin)
symlink="${HOME}/Library/Application Support/${appname}/plugins/node_modules/${PWD##*/}"
;;

Linux)
symlink="${HOME}/.config/${appname}/plugins/node_modules/${PWD##*/}"
;;

CYGWIN*|MINGW32*|MINGW64*|MSYS*)
symlink="${APPDATA}\\${appname}\plugins\node_modules\\${PWD##*/}"
;;

*)
echo "Unknown system. Please, open an issue in https://github.com/KELiON/cerebro-plugin/issues"
exit
;;
esac

echo "Creating symlink: $symlink -> ${PWD}"
ln -s "${PWD}" "$symlink"
# trap "echo 'Deleting symlink' && rm -rf \"$symlink\"" SIGHUP SIGINT SIGTERM
# ./node_modules/.bin/webpack --watch
14 changes: 14 additions & 0 deletions scripts/debug.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@echo off
IF "%1%"==""dev"" (
set appname=Electron
) ELSE (
set appname=Cerebro
)

for %%* in (.) do set dirname=%%~nx*
SET symlink="%APPDATA%\%appname%\plugins\node_modules\%dirname%"

echo "Creating symlink: %symlink% -> %cd%"

mklink /d "%symlink%" "%cd%"
start "" /wait node_modules\.bin\webpack --watch
67 changes: 67 additions & 0 deletions src/Preview/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const React = require('react')
const GoogleMap = require('react-google-maps/lib/GoogleMap')
// eslint-disable-next-line no-unused-vars
const GoogleMapLoader = require('react-google-maps/lib/GoogleMapLoader')
const ScriptjsLoader = require('react-google-maps/lib/async/ScriptjsLoader')
const { triggerEvent } = require('react-google-maps/lib/utils')
const Marker = require('react-google-maps/lib/Marker')
const styles = require('./styles.css')

class Preview extends React.Component {
/**
* Fit google maps to geocoded viewport
*
* @param {GoogleMap} map Ref to GoogleMap component
*/
fitBounds() {
const { map } = this
if (!map) return
// eslint-disable-next-line no-undef
const bounds = new google.maps.LatLngBounds()
bounds.extend(this.props.geometry.location)
const { viewport } = this.props.geometry
if (viewport) {
Object.keys(viewport).forEach(key => bounds.extend(viewport[key]))
}
map.fitBounds(bounds)
}
render() {
const { location } = this.props.geometry
const { name } = this.props

const marker = {
position: location,
key: name
}

return (
<ScriptjsLoader
protocol={"https"}
hostname={"maps.googleapis.com"}
pathname={"/maps/api/js"}
query={{ libraries: 'geometry,drawing,places' }}
loadingElement={<div />}
containerElement={<div className={styles.container} />}
googleMapElement={
<GoogleMap
ref={(map) => (this.map = map) && this.fitBounds()}
defaultZoom={3}
defaultCenter={location}
>
<Marker {...marker} />
</GoogleMap>
}
/>
)
}
}

Preview.propTypes = {
name: React.PropTypes.string,
geometry: React.PropTypes.shape({
location: React.PropTypes.object,
viewport: React.PropTypes.object
})
}

module.exports = Preview
7 changes: 7 additions & 0 deletions src/Preview/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.container {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
10 changes: 10 additions & 0 deletions src/geocode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { memoize } = require('cerebro-tools')

const geocode = (term, userLang) => {
const url = `http://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(term)}&language=${userLang}`
return fetch(url)
.then(response => response.json())
.then(json => json.results)
}

module.exports = memoize(geocode)
15 changes: 15 additions & 0 deletions src/getPlaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { memoize } = require('cerebro-tools')

// Expire places cache in 30 minutes
const EXPIRATION = 30 * 60 * 1000

const getPlaces = ({ keyword, location, lang }) => {
const url = `https://maps.googleapis.com/maps/api/place/textsearch/json?key=AIzaSyA-O4pwGMszqqnKslBjG1ADC1Ol7HCyBd8&query=${encodeURIComponent(keyword)}&language=${lang}&location=${location}`
return fetch(url)
.then(response => response.json())
.then(json => json.results)
}

module.exports = memoize(getPlaces, {
maxAge: EXPIRATION
})
14 changes: 14 additions & 0 deletions src/getUserLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { memoize } = require('cerebro-tools')

// Expire geoip information in 30 minutes
const EXPIRATION = 30 * 60 * 1000

const getUserLocation = () => (
fetch('http://freegeoip.net/json/')
.then(response => response.json())
.then(json => `${json.latitude},${json.longitude}`)
)

module.exports = memoize(getUserLocation, {
maxAge: EXPIRATION
})
Binary file added src/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint camelcase:0 */
const React = require('react')
const Preview = require('./Preview')
const geocode = require('./geocode')
const getPlaces = require('./getPlaces')
const getUserLocation = require('./getUserLocation')

const icon = require('./icon.png')

const toResultFn = (address, openUrl) => ({ name, geometry, formatted_address, place_id }) => ({
icon,
id: place_id,
title: name,
subtitle: formatted_address,
term: formatted_address,
onSelect: () => {
const q = encodeURIComponent(address)
openUrl(`https://maps.google.com/?q=${q}`)
},
getPreview: () => <Preview geometry={geometry} name={formatted_address} />
})

/**
* Plugin to search & display google maps
*
* @param {String} options.term
* @param {Object} options.actions
* @param {Function} options.display
*/
const fn = ({ term, actions, display, config }) => {
let match = term.match(/^(?:maps?|карт(?:а|ы))\s+(.+)/i)
match = match || term.match(/(.+)\s(?:maps?|карт(?:а|ы))$/i)
if (!match) return
const address = match[1]
const lang = config.get('lang')
const toResult = toResultFn(address, actions.open)
getUserLocation().then(location => (
getPlaces({
location,
lang,
keyword: address
})
)).then(points => {
const result = points.map(toResult)
display(result)
})
geocode(address, lang).then(points => {
const result = points.map(toResult)
display(result)
})
}

module.exports = {
icon,
fn,
name: 'Search on google maps',
keyword: 'map',
}
Loading

0 comments on commit 9c66f31

Please sign in to comment.