Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #347 from alexander-heimbuch/feature/deeplink
Browse files Browse the repository at this point in the history
Feature/deeplink
  • Loading branch information
plutonik-a authored Mar 12, 2017
2 parents 76496a2 + b7f0216 commit 8502c2f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 74 deletions.
85 changes: 38 additions & 47 deletions src/embed/embed.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import get from 'lodash/get'
import Bluebird from 'bluebird'
import browser from 'detect-browser'
import request from 'superagent'
import { iframeResizer } from 'iframe-resizer'
import iframeResizerContentWindow from 'raw-loader!iframe-resizer/js/iframeResizer.contentWindow.min.js'

const findNode = selector => document.querySelectorAll(selector)
const createNode = tag => document.createElement(tag)
const appendNode = (node, child) => node.appendChild(child)

const tag = (tag, value = '', attributes = {}) => {
let attr = Object.keys(attributes).map(attribute => ` ${attribute}="${attributes[attribute]}"`)
import { findNode, createNode, appendNode, tag } from 'utils/dom'
import requestConfig from 'utils/request'
import urlConfig from 'utils/url'

attr = attr.join('')
return `<${tag}${attr}>${value}</${tag}>`
}
import { iframeResizer } from 'iframe-resizer'
import iframeResizerContentWindow from 'raw-loader!iframe-resizer/js/iframeResizer.contentWindow.min.js'

const sandbox = () => {
// Sandbox
const playerSandbox = () => {
const frame = createNode('iframe')

if (browser.name !== 'ios') {
Expand All @@ -30,60 +24,57 @@ const sandbox = () => {
return frame
}

const sandboxDocument = iframe => get(iframe, ['contentWindow', 'document'])

// Player renderer
const renderPlayer = (config, player) => anchor => {
const injector = sandbox(config)
const sandbox = playerSandbox(config)

appendNode(anchor, injector)
appendNode(anchor, sandbox)

const injectorDoc = sandboxDocument(injector)
const sandboxDoc = get(sandbox, ['contentWindow', 'document'])

injectorDoc.open()
injectorDoc.write('<!DOCTYPE html>')
injectorDoc.write('<html>')
injectorDoc.write('<head></head>')
injectorDoc.write(player)
injectorDoc.close()
sandboxDoc.open()
sandboxDoc.write('<!DOCTYPE html>')
sandboxDoc.write('<html>')
sandboxDoc.write('<head></head>')
sandboxDoc.write(player)
sandboxDoc.close()

iframeResizer({
checkOrigin: false,
log: false
}, injector)
}, sandbox)
}

const generateConfig = config => {
return Bluebird.resolve(tag('script', `window.PODLOVE = ${JSON.stringify(config)}`))
}
// Config Handling
const createConfigNode = (config = {}) =>
tag('script', `window.PODLOVE = ${JSON.stringify(config)}`)

const getConfig = config => {
const remoteConfig = (config = {}) => {
if (typeof config === 'string') {
return request
.get(config)
.query({ format: 'json' })
.set('Accept', 'application/json')
.then(res => res.body)
.then(generateConfig)
return requestConfig(config)
}

if (typeof config === 'object') {
return generateConfig(config)
}
return Bluebird.resolve(config)
}

// Bootstrap
window.podlovePlayer = (selector, config) => {
const anchor = typeof selector === 'string' ? findNode(selector) : [selector]

const logic = tag('script', '', {type: 'text/javascript', src: './window.bundle.js'})
const appLogic = tag('script', '', {type: 'text/javascript', src: './window.bundle.js'})
const dynamicResizer = tag('script', iframeResizerContentWindow)
const playerEntry = tag('PodlovePlayer')

getConfig(config).then(configObject => {
anchor.forEach(renderPlayer(config, [
playerEntry,
configObject,
logic,
dynamicResizer
].join('')))
})
remoteConfig(config)
// load parameters from url
.then(config => anchor.length > 1 ? config : Object.assign({}, config, urlConfig))
.then(createConfigNode)
.then(configObject => {
anchor.forEach(renderPlayer(config, [
playerEntry,
configObject,
appLogic,
dynamicResizer
].join('')))
})
}
9 changes: 9 additions & 0 deletions src/embed/share.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import urlConfig from 'utils/url'
import remoteConfig from 'utils/request'

import app from '../app'

remoteConfig(urlConfig.episode)
.then(config => Object.assign({}, config, urlConfig))
.then(config => Object.assign({}, config, {mode: 'share'}))
.then(app)
25 changes: 0 additions & 25 deletions src/embed/url.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/utils/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import curry from 'lodash/fp/curry'

export const findNode = selector => document.querySelectorAll(selector)
export const createNode = tag => document.createElement(tag)
export const appendNode = curry((node, child) => node.appendChild(child))

export const tag = curry((tag, value = '', attributes = {}) => {
let attr = Object.keys(attributes).map(attribute => ` ${attribute}="${attributes[attribute]}"`)

attr = attr.join('')
return `<${tag}${attr}>${value}</${tag}>`
})
8 changes: 8 additions & 0 deletions src/utils/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import request from 'superagent'

export default episode =>
request
.get(episode)
.query({ format: 'json' })
.set('Accept', 'application/json')
.then(res => res.body)
16 changes: 16 additions & 0 deletions src/utils/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import queryString from 'query-string'
import { timeToSeconds } from 'utils/time'

const params = queryString.parse(window.location.search)

const parsePlaytime = parameters => {
if (parameters.playtime) {
return {
playtime: timeToSeconds(parameters.playtime)
}
}

return {}
}

export default Object.assign({}, params, parsePlaytime(params))
2 changes: 1 addition & 1 deletion src/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config = {
entry: {
embed: path.resolve(__dirname, 'embed', 'embed.js'),
window: path.resolve(__dirname, 'embed', 'window.js'),
url: path.resolve(__dirname, 'embed', 'url.js')
share: path.resolve(__dirname, 'embed', 'share.js')
},
output: {
path: path.resolve('dist'),
Expand Down
2 changes: 1 addition & 1 deletion statics/share.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</head>
<body>
<PodlovePlayer></PodlovePlayer>
<script src="url.bundle.js"></script>
<script src="share.bundle.js"></script>
</body>
</html>

0 comments on commit 8502c2f

Please sign in to comment.