Skip to content

Commit

Permalink
feat(map): Added support for map embed with option place, view and st…
Browse files Browse the repository at this point in the history
…reetview
  • Loading branch information
ritz078 committed Nov 4, 2015
1 parent 5abdb64 commit 9a5d8b4
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 253 deletions.
1 change: 1 addition & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"SMILEY" : true,
"HIGHLIGHTCODE" : true,

"MAP" : true,

"YOUTUBE" : true,
"VIMEO" : true,
Expand Down
5 changes: 3 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>Loading embed.js in a block</h1>

<div id="block">
Pulchritudines nocere, :heart: tanquam talis hibrida.A falsis, brodium :+1: salvus gabalium.Nunquam
promissio
promissio @(iit roorkee)
http://iitr.ac.in caesium.Cum rumor
https://twitter.com/IGN/status/652941146054881281
peregrinationes https://www.flickr.com/photos/xdjio/226228060/, omnes ususes http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_50mb.mp4
Expand Down Expand Up @@ -69,7 +69,8 @@ <h1>Loading embed.js in a block</h1>
element: document.getElementById('block'),
videoDetails:false,
videoJS:true,
videoWidth:720
videoWidth:720,
googleAuthKey:'AIzaSyCqFouT8h5DKAbxlrTZmjXEmNBjC69f0ts',
});
x.render();
x.text(function(data){
Expand Down
6 changes: 3 additions & 3 deletions dist/embed.min.js

Large diffs are not rendered by default.

592 changes: 346 additions & 246 deletions src/embed.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/embed.js.map

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/js/embed.es6
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if (build.SMILEY) var Smiley = require('./modules/emoticons/smiley.es6');
if (build.LINK) var Url = require('./modules/url.es6');

if (build.TWITTER) var Twitter = require('./modules/twitter/twitter.es6');
if (build.MAP) var Gmap = require('./modules/map/map.es6');

const Code = require('./modules/code/code.es6');
const Video = require('./modules/video/video.es6');
Expand Down Expand Up @@ -54,6 +55,10 @@ const helper = require('./modules/video/helper.es6');
fluid : true,
preload : 'metadata'
},
locationEmbed : true,
mapOptions : {
mode : 'place'
},
tweetsEmbed: true,
tweetOptions: {
maxWidth : 550,
Expand Down Expand Up @@ -124,7 +129,7 @@ const helper = require('./modules/video/helper.es6');
output = options.fontIcons && build.SMILEY ? (new Smiley(output, options).process()) : output;
[output, embeds] = (new Code(input, output, options, embeds).process());
[output, embeds] = await (new Video(input, output, options, embeds).process());

[output,embeds] = options.locationEmbed ? await (new Gmap(input, output, options, embeds).process()) : [output, embeds];
[output, embeds] = (new Audio(input, output, options, embeds).process());
[output, embeds] = (new Image(input, output, options, embeds).process());
if (options.tweetsEmbed && build.TWITTER) {
Expand Down
56 changes: 56 additions & 0 deletions src/js/modules/map/map.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const utils = require('../utils.es6');

class Gmap {
constructor(input, output, options, embeds) {
this.input = input;
this.output = output;
this.options = options;
this.embeds = embeds;
this.regex = /@\((.+)\)/gi;
}

async getCoordinate(location) {
let url = `http://maps.googleapis.com/maps/api/geocode/json?address=${location}&sensor=false`;
let response = await fetch(url);
let data = await response.json();
console.log(data);
let [latitude, longitude] = [data.results[0].geometry.location.lat, data.results[0].geometry.location.lng];
return [latitude, longitude]
}

template(match, latitude, longitude) {
let template;
let location = match.split('(')[1].split(')')[0];
let config = this.options.mapOptions;
let dimensions = utils.dimensions(this.options);
if (config.mode === 'place') {
template =
`<div class="ejs-embed ejs-map"><iframe width="${dimensions.width}" height="${dimensions.height}" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=${this.options.googleAuthKey}&q=${location}"></iframe></div>`;
} else if (config.mode === 'streetview') {
template =
`<div class="ejs-embed ejs-map"><iframe width="${dimensions.width}" height="${dimensions.height}" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/streetview?key=${this.options.googleAuthKey}&location=${latitude},${longitude}&heading=210&pitch=10&fov=35"></iframe></div>`;
} else if (config.mode === 'view') {
template =
`<div class="ejs-embed ejs-map"><iframe width="${dimensions.width}" height="${dimensions.height}" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/view?key=${this.options.googleAuthKey}&center=${latitude},${longitude}&zoom=18&maptype=satellite"></iframe></div>`
}
return template;
}

async process() {
let match;
while ((match = utils.matches(this.regex, this.input)) !== null) {
let [latitude, longitude] = this.options.mapOptions.mode !== 'place' ? await this.getCoordinate(match[0]) : [null, null];
let text = this.template(match[0], latitude, longitude);
this.embeds.push({
text: text,
index: match.index
})
}
this.output = this.output.replace(this.regex, function(match) {
return '<span class="ejs-location">' + match.split('(')[1].split(')')[0] + '</span>';
});
return [this.output, this.embeds];
}
}

module.exports = Gmap;

0 comments on commit 9a5d8b4

Please sign in to comment.