@gobistories/gobi-web-integration
Version:
Welcome to Gobi Web Integration. This library will let you put your Gobi stories on your site.
165 lines (162 loc) • 6.78 kB
text/coffeescript
utils = require('@/utils/utils')
socket_io_client_1 = require('socket.io-client')
Function::property = (name, getset) -> Object.defineProperty , name, getset
class Story
constructor: (options) ->
= []
= ''
= options
=
title: .querySelector '.gobi-popup-story__title'
description: .querySelector '.gobi-popup-story__description'
avatar: .querySelector '.gobi-popup-story__avatar'
avatarContainer: .querySelector '.gobi-popup-story__avatar-container'
= options.id or ''
= options.viewKey or ''
= options.secretKey or ''
= options.title or ''
= options.avatarSrc or ''
if or
if !options.avatarSrc or !
promise = undefined
if
promise = utils.fetchAvatarAndTitleGivenViewKey
promise.catch (error) =>
# story likely empty, assume it is empty
# assume storyName is viewKey, not always true
storyName =
if then storyName,
else
promise = utils.fetchAvatarAndTitleGivenStoryId
promise.then (data) =>
= or data.src
= or data.title
else
= utils.makeRandomStorySecretKey()
= utils.makeViewKey
storyName = .slice(0, 20)
storyName,
# User now scans this QR with their phone, and adds a video
= options.description or ''
= options.color or ''
options.onSelect
options.container?.appendChild
if options.titleColor
.title.style.color = options.titleColor
if options.descriptionColor
.description.style.color = options.descriptionColor
if options.titleSize
.title.style.fontSize = options.titleSize
if options.descriptionSize
.description.style.fontSize = options.descriptionSize
s = options.avatarSize or '96px'
.avatarContainer.style.width = s
.avatarContainer.style.margin = 'calc(.1*' + s + ') calc(.2*' + s + ')'
# .avatarContainer.style.padding = Math.max((parseInt(s or 96)/9-5), 4) + 'px'
.avatarContainer.style.padding = 'calc(' + s + '/9-5px)'
.avatarContainer.addEventListener 'mouseenter', (e) =>
.avatarContainer.style.width = 'calc(1.2*' + s + ')'
.avatarContainer.style.margin = '0px calc(.1*' + s + ')'
.avatarContainer.addEventListener 'mouseleave', (e) =>
.avatarContainer.style.width = s
.avatarContainer.style.margin = 'calc(.1*' + s + ') calc(.2*' + s + ')'
= ! !options.selected
=
=
=
setupOnSelectListener: (onSelect) ->
onSelect = onSelect.bind @, @
selectArea = .querySelector '[data-select-area]'
selectArea.addEventListener 'click', onSelect
.push ->
selectArea.removeEventListener 'click', onSelect
'avatarSrc',
get: ->
set: (src) ->
= src
.avatar.style.backgroundImage = 'url(' + src + ')'
checkForVideoInStory: ->
promise = utils.fetchAvatarAndTitleGivenViewKey
promise.then (data) ->
= data.src
= data.title
mediaDetected: (media) ->
.disconnect()
socketConnected: ->
.on 'media', =>
.emit 'subscribe_to_story_media', viewKey:
setupSocketToListenForNewMediaInStory: ->
= socket_io_client_1 'https://live.gobiapp.com'
.on 'connect', =>
putQrInAvatar: (storyName, secretKey) ->
data = utils.makeBranchQueryData storyName, secretKey
result = await utils.getBranchLink data
qrData = result.url
= qrData
dataUrl = await utils.qrDataToDataUrl qrData
= dataUrl
destroy: ->
if .parentElement
.parentElement.removeChild
i = .length
while i--
[i]()
'selected',
get: ->
set: (selected) ->
if selected
.classList.add 'gobi-story--selected'
else
.classList.remove 'gobi-story--selected'
= selected
'title',
get: ->
set: (title) ->
= title
if title.match(/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/)
.title.href = title
.title.textContent = title.replace(new RegExp('^https?://'), '')
else
.title.textContent = title
delete .title.href
'description',
get: ->
set: (description) ->
= description
.description.textContent = description
'color',
get: ->
set: (color) ->
= color
avatarCircleBorder = .avatarContainer.querySelector '.gobi-popup-story__avatar-circle'
avatarCircleBorder?.style.stroke = color
_createTemplate: (options) ->
# "desktop": const classPrefix = 'gobi-story';
classPrefix = 'gobi-popup-story'
container = document.createElement('div')
container.classList.add 'gobi-popup-story'
playIconSvg = ''
if options.showPlayIcon
playIconSvg = require('../play-icon-circle.svg').default
container.innerHTML = '<div class="gobi-popup-story__avatar-container" data-select-area data-avatarContainer>' +
'<div class="gobi-popup-story__avatar" data-avatar>' +
'</div>' +
'<svg class="gobi-popup-story__avatar-svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120">' +
' <circle class="gobi-popup-story__avatar-circle" cx="60" cy="60" r="57" fill="none" stroke="#15d6ea" stroke-width="3" stroke-dasharray="370.52" stroke-dashoffset="370.52" />' +
'</svg>' +
'<div class="svg-holder">' +
playIconSvg +
'</div>' +
'</div>' +
'<a class="gobi-popup-story__title" target="_blank" data-title></a>' +
'<div class="gobi-popup-story__description"><div class="gobi-popup-story__description-text"></div></div>'
container
module.exports = Story