2015-06-15 02:34:39 +01:00
import React from 'react' ;
import request from 'superagent' ;
2015-08-20 23:22:57 +01:00
import { State , Link } from 'react-router' ;
2015-06-15 02:34:39 +01:00
import Foot from './foot' ;
2015-08-20 23:22:57 +01:00
const MusicItem = React . createClass ( {
2015-06-15 02:34:39 +01:00
render : function ( ) {
if ( ! this . props . item . matched _at ) {
return (
< div className = 'col-md-3 col-xs-6' >
< div className = 'service' >
< div className = 'artwork' style = { { backgroundImage : 'url(' + this . props . items [ 0 ] . artwork . small + ')' } } >
< / d i v >
< div className = 'loading-wrap' >
< img src = '/images/eq.svg' className = 'loading' / >
< / d i v >
< div className = 'service-link' >
< img src = { '/images/' + this . props . item . service + '.png' } className = 'img-rounded' / >
< / d i v >
< / d i v >
< / d i v >
) ;
} else if ( ! this . props . item . id ) {
return (
< div className = 'col-md-3 col-xs-6' >
< div className = 'service' >
< div className = 'artwork not-found' style = { { backgroundImage : 'url(' + this . props . items [ 0 ] . artwork . small + ')' } } > < / d i v >
< div className = 'no-match' >
No Match
< / d i v >
< div className = 'service-link not-found' >
< img src = { '/images/' + this . props . item . service + '.png' } className = 'img-rounded' / >
< / d i v >
< / d i v >
< / d i v >
) ;
} else {
return (
< div className = 'col-md-3 col-xs-6' >
< div className = { 'service' + ( this . props . inc === 0 ? ' source-service' : '' ) } >
< div className = 'matching-from hidden-xs' > { this . props . inc === 0 ? 'Found matches using this link' : '' } < / d i v >
< a href = { this . props . item . streamUrl || this . props . item . purchaseUrl } >
< div className = { this . props . item . service === 'youtube' ? 'artwork-youtube artwork' : 'artwork' } style = { { backgroundImage : 'url(' + this . props . item . artwork . small + ')' } } >
< / d i v >
< div className = { this . props . item . service === 'youtube' && this . props . inc > 0 ? 'youtube' : '' } >
{ this . props . item . service === 'youtube' && this . props . inc > 0 ? this . props . item . name : '' }
< / d i v >
< / a >
< div className = 'service-link' >
< a href = { this . props . item . streamUrl || this . props . item . purchaseUrl } >
< img src = { '/images/' + this . props . item . service + '.png' } className = 'img-rounded' / >
< / a >
< / d i v >
< / d i v >
< / d i v >
) ;
}
}
} ) ;
export default React . createClass ( {
mixins : [ State ] ,
getInitialState : function ( ) {
if ( this . props . shares && this . props . shares [ 0 ] . id === this . getParams ( ) . id ) {
return {
name : this . props . shares [ 0 ] . name ,
artist : this . props . shares [ 0 ] . artist . name ,
shares : this . props . shares ,
shareUrl : 'https://match.audio/' + this . props . shares [ 0 ] . service + '/' + this . props . shares [ 0 ] . type + '/' + this . props . shares [ 0 ] . id
} ;
}
return {
name : '' ,
artist : '' ,
shares : [ ] ,
shareUrl : ''
} ;
} ,
componentWillUnmount : function ( ) {
if ( this . state . interval ) {
clearInterval ( this . state . interval ) ;
}
} ,
componentDidMount : function ( ) {
2015-08-20 23:22:57 +01:00
let complete = this . state . shares . length > 0 ;
2015-06-15 02:34:39 +01:00
this . state . shares . forEach ( function ( share ) {
if ( typeof share . matched _at === 'undefined' ) {
complete = false ;
}
} ) ;
2015-08-20 23:22:57 +01:00
const getShares = ( ) => {
request . get ( this . getPathname ( ) + '.json' ) . end ( ( err , res ) => {
const shares = res . body . shares ;
2015-06-15 02:34:39 +01:00
complete = true ;
shares . forEach ( function ( share ) {
if ( typeof share . matched _at === 'undefined' ) {
complete = false ;
}
} ) ;
if ( complete ) {
clearInterval ( this . state . interval ) ;
}
if ( shares . length ) {
this . setState ( {
name : shares [ 0 ] . name ,
artist : shares [ 0 ] . artist . name ,
shares : shares ,
shareUrl : 'https://match.audio/' + shares [ 0 ] . service + '/' + shares [ 0 ] . type + '/' + shares [ 0 ] . id
} ) ;
}
2015-08-20 23:22:57 +01:00
} ) ;
} ;
2015-06-15 02:34:39 +01:00
if ( ! this . state . shares . length ) {
getShares ( ) ;
}
// Temporary until websockets implementation
this . state . interval = setInterval ( function ( ) {
if ( ! complete ) {
getShares ( ) ;
}
} , 2000 ) ;
// Some hacks to pop open the Twitter/Facebook/Google Plus sharing dialogs without using their code.
Array . prototype . forEach . call ( document . querySelectorAll ( '.share-dialog' ) , function ( dialog ) {
2015-08-20 23:22:57 +01:00
dialog . addEventListener ( 'click' , function ( e ) {
e . preventDefault ( ) ;
const w = 845 ;
const h = 670 ;
const dualScreenLeft = window . screenLeft !== undefined ? window . screenLeft : screen . left ;
const dualScreenTop = window . screenTop !== undefined ? window . screenTop : screen . top ;
const width = window . innerWidth ? window . innerWidth : document . documentElement . clientWidth ? document . documentElement . clientWidth : screen . width ;
const height = window . innerHeight ? window . innerHeight : document . documentElement . clientHeight ? document . documentElement . clientHeight : screen . height ;
const left = ( ( width / 2 ) - ( w / 2 ) ) + dualScreenLeft ;
const top = ( ( height / 2 ) - ( h / 2 ) ) + dualScreenTop ;
const newWindow = window . open ( dialog . href , 'Share Music' , 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left ) ;
2015-06-15 02:34:39 +01:00
if ( window . focus ) {
newWindow . focus ( ) ;
}
} ) ;
} ) ;
} ,
render : function ( ) {
return (
< div >
< div className = 'page-wrap share' >
< header >
< div className = 'container' >
< div className = 'row' >
< div className = 'col-md-12' >
< h1 > < Link to = 'home' > match < span className = 'audio-lighten' > . audio < / s p a n > < / L i n k > < / h 1 >
< / d i v >
< / d i v >
< / d i v >
< / h e a d e r >
< div className = 'container' >
< div className = 'row' >
< div className = 'col-md-9 col-sm-8 col-xs-12' >
< h3 > Matched { this . state . shares [ 0 ] ? this . state . shares [ 0 ] . type + 's' : '' } for < / h 3 >
< h2 > { this . state . name } < span className = 'artist-lighten' > - { this . state . artist } < / s p a n > < / h 2 >
< / d i v >
< div className = 'col-md-3 col-sm-4 hidden-xs' >
< ul className = 'list-inline share-tools' >
< li > Share this < / l i >
< li > < a href = { 'http://twitter.com/intent/tweet/?text=' + encodeURIComponent ( this . state . name ) + ' by ' + encodeURIComponent ( this . state . artist ) + '&via=MatchAudio&url=' + this . state . shareUrl } className = 'share-dialog' > < img src = '/images/twitter.png' alt = 'Twitter' / > < / a > < / l i >
< li > < a href = { 'http://www.facebook.com/sharer/sharer.php?p[url]=' + this . state . shareUrl } className = 'share-dialog' > < img src = '/images/facebook.png' alt = 'Facebook' / > < / a > < / l i >
< li > < a href = { 'https://plus.google.com/share?url=' + this . state . shareUrl } className = 'share-dialog' > < img src = '/images/googleplus.png' alt = 'Google+' / > < / a > < / l i >
< / u l >
< / d i v >
< / d i v >
< div className = 'row' >
2015-08-20 23:22:57 +01:00
{ this . state . shares . map ( ( item , i ) => {
2015-06-15 02:34:39 +01:00
return ( < MusicItem items = { this . state . shares } item = { item } inc = { i } key = { i } / > ) ;
2015-08-20 23:22:57 +01:00
} ) }
2015-06-15 02:34:39 +01:00
< / d i v >
< / d i v >
< / d i v >
< Foot page = 'share' / >
< / d i v >
) ;
}
} ) ;