Working search! Weee!
This commit is contained in:
@@ -1,84 +1,51 @@
|
||||
class window.Ajax
|
||||
|
||||
referer: false
|
||||
|
||||
loadArtistData: (name) ->
|
||||
_search.showSpinner()
|
||||
name = name.split(' ').join('+')
|
||||
$.get '/artist/' +name+ '/', (data) ->
|
||||
if data.status?
|
||||
if data.status is 'loading'
|
||||
_search.showArtistPics data.pics
|
||||
setTimeout () ->
|
||||
_ajax.loadArtistData name
|
||||
, 3000
|
||||
else if data.status is 'corrected'
|
||||
_ajax.loadArtistData data.page
|
||||
else if data.status is 'suggestions'
|
||||
_search.hideSpinner()
|
||||
_search.showSuggestions data.values
|
||||
else if data.status == 'loading_failed'
|
||||
_search.hideSpinner()
|
||||
_search.showError()
|
||||
else
|
||||
_ajax.setArchor '/artist/' +name+ '/'
|
||||
_pages.renderArtist data
|
||||
#_search.hideSpinner()
|
||||
false
|
||||
|
||||
loadSearchPage: ->
|
||||
|
||||
false
|
||||
|
||||
loadSettingsPage: ->
|
||||
$.get '/templates/settings.html', (data) ->
|
||||
_ajax.setArchor '/settings/'
|
||||
_pages.renderSettings _beathaven.localizeHTML $(data)
|
||||
false
|
||||
|
||||
load404Page: ->
|
||||
$.get '/404.html', (data) ->
|
||||
$('.data-container .inner').html data
|
||||
_beathaven.redrawScrollbar()
|
||||
false
|
||||
|
||||
loadAboutPage: ->
|
||||
$.get '/templates/about.html', (data) ->
|
||||
_pages.renderTextpage data
|
||||
_ajax.setTitle 'About'
|
||||
false
|
||||
|
||||
setArchor: (anchor) ->
|
||||
@referer = this.getAnchor()
|
||||
window.location.hash = '#' +anchor
|
||||
|
||||
getAnchor: () ->
|
||||
window.location.hash.substring 1;
|
||||
|
||||
setTitle: (title) ->
|
||||
document.title = title+ ' @ BeatHaven'
|
||||
|
||||
go: (url) ->
|
||||
this.setArchor url
|
||||
false
|
||||
|
||||
referer: false
|
||||
|
||||
loadSettingsPage: ->
|
||||
$.get '/templates/settings.html', (data) ->
|
||||
_ajax.setArchor '/settings/'
|
||||
_page.renderSettings _beathaven.localizeHTML $(data)
|
||||
false
|
||||
|
||||
load404Page: ->
|
||||
$.get '/404.html', (data) ->
|
||||
$('.data-container .inner').html data
|
||||
false
|
||||
|
||||
loadAboutPage: ->
|
||||
$.get '/templates/about.html', (data) ->
|
||||
_page.renderTextpage data
|
||||
_ajax.setTitle 'About'
|
||||
false
|
||||
|
||||
setArchor: (anchor) ->
|
||||
@referer = this.getAnchor()
|
||||
window.location.hash = '#' +anchor
|
||||
|
||||
getAnchor: () ->
|
||||
window.location.hash.substring 1;
|
||||
|
||||
setTitle: (title) ->
|
||||
document.title = title+ ' @ BeatHaven'
|
||||
|
||||
go: (url) ->
|
||||
this.setArchor url
|
||||
false
|
||||
|
||||
detectPage: () ->
|
||||
if m = _ajax.getAnchor().match /\/artist\/(.+)\//
|
||||
_ajax.loadArtistData m[1]
|
||||
else if _ajax.getAnchor() == '' or _ajax.getAnchor().match /\/search\//
|
||||
_ajax.loadSearchPage();
|
||||
else if _ajax.getAnchor().match /\/settings\//
|
||||
_ajax.loadSettingsPage()
|
||||
else if _ajax.getAnchor().match /\/about\//
|
||||
_ajax.loadAboutPage()
|
||||
else
|
||||
_ajax.loadSearchPage()
|
||||
false
|
||||
detectPage: () ->
|
||||
if m = _ajax.getAnchor().match /\/artist\/(.+)\//
|
||||
_search.loadArtistData m[1]
|
||||
else if _ajax.getAnchor() == '' or _ajax.getAnchor().match /\/search\//
|
||||
_ajax.loadSearchPage();
|
||||
else if _ajax.getAnchor().match /\/settings\//
|
||||
_ajax.loadSettingsPage()
|
||||
else if _ajax.getAnchor().match /\/about\//
|
||||
_ajax.loadAboutPage()
|
||||
else
|
||||
#_ajax.loadSearchPage()
|
||||
false
|
||||
|
||||
|
||||
$('a.data.artist').live 'click', ->
|
||||
_ajax.loadArtistData $(this).html()
|
||||
false
|
||||
$(window).bind 'hashchange', ->
|
||||
_ajax.detectPage()
|
||||
false
|
||||
#_ajax.detectPage()
|
||||
false
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
//= require jquery/jplayer/jquery.jplayer/jquery.jplayer
|
||||
|
||||
//= require locale
|
||||
//= require vkontakte
|
||||
//= require session
|
||||
//= require vkontakte
|
||||
//= require ajax
|
||||
//= require player
|
||||
//= require search
|
||||
//= require pages
|
||||
//= require page
|
||||
//= require settings
|
||||
|
||||
//= require beathaven
|
||||
@@ -5,7 +5,7 @@ window._vkontakte = null
|
||||
window._ajax = null
|
||||
window._player = null
|
||||
window._search = null
|
||||
window._pages = null
|
||||
window._page = null
|
||||
window._settings = null
|
||||
|
||||
$ ->
|
||||
@@ -16,7 +16,7 @@ $ ->
|
||||
window._beathaven = new BeatHaven()
|
||||
window._beathaven.init()
|
||||
|
||||
class BeatHaven
|
||||
class window.BeatHaven
|
||||
|
||||
last_height: false
|
||||
lang: 'ru'
|
||||
@@ -32,7 +32,7 @@ class BeatHaven
|
||||
|
||||
window._search = new Search()
|
||||
|
||||
window._pages = new Pages()
|
||||
window._page = new Page()
|
||||
|
||||
window._settings = new Settings()
|
||||
|
||||
@@ -53,34 +53,7 @@ class BeatHaven
|
||||
containerId: 'autocomplete-container'
|
||||
containerItemsId: 'autocomplete-items'
|
||||
onSelect: ->
|
||||
_ajax.loadArtistData $('#search').first().val()
|
||||
|
||||
adjustSizes: ->
|
||||
$('.data-container').height $(window).height() - $('.header-container').height() - $('.pulldown').height()
|
||||
$('.data-container').width $(window).width() - $('.player').width()
|
||||
$('.player-container').height $(window).height()
|
||||
$('.playlist').height $(window).height() - $('.player').height() - $('.player-container .additional-controls').height()
|
||||
|
||||
$('.data-container').scrollbar()
|
||||
$('.playlist').scrollbar()
|
||||
false
|
||||
|
||||
checkRedrawScrollbar: ->
|
||||
focused_id = false
|
||||
if document.activeElement.id?
|
||||
focused_id = document.activeElement.id;
|
||||
outer_height = $('.data-container > div').outerHeight()
|
||||
if outer_height > 300 and outer_height != _beathaven.last_height
|
||||
_beathaven.last_height = outer_height
|
||||
_beathaven.redrawScrollbar()
|
||||
if focused_id
|
||||
document.getElementById(focused_id).focus()
|
||||
focused_id = false
|
||||
window.setTimeout ->
|
||||
_beathaven.checkRedrawScrollbar()
|
||||
false
|
||||
, 500
|
||||
false
|
||||
_search.loadArtistData $('#search').val()
|
||||
|
||||
localizeHTML: (obj, lang) ->
|
||||
unless obj?
|
||||
@@ -102,15 +75,6 @@ class BeatHaven
|
||||
_locale[id][lang]
|
||||
else
|
||||
id
|
||||
|
||||
pdShowSpinner: ->
|
||||
$('.pulldown').html '<div class="pd-spinner"><img src="/images/loader.gif" alt=""/></div>'
|
||||
false
|
||||
|
||||
pdHideSpinner: ->
|
||||
$('.pulldown').html ''
|
||||
false
|
||||
|
||||
|
||||
String::htmlsafe = ->
|
||||
replaces = [
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
class window.Pages
|
||||
class window.Page
|
||||
|
||||
print: (html) ->
|
||||
$('#content').html(html)
|
||||
false
|
||||
|
||||
renderArtist: (data) ->
|
||||
$('#content').html(data)
|
||||
@@ -1,15 +1,37 @@
|
||||
class window.Search
|
||||
|
||||
showSpinner: ->
|
||||
$('#search').val("").attr(disabled: 'disabled').blur()
|
||||
$('#search').attr(disabled: 'disabled').blur()
|
||||
$('#autocomplete-container').hide()
|
||||
$('#artist-load-spinner').show()
|
||||
this.hideSuggestions()
|
||||
false
|
||||
|
||||
hideSpinner: ->
|
||||
$('#search').removeAttr 'disabled'
|
||||
$('.search_field').first().focus()
|
||||
$('.search-container img').first().hide()
|
||||
$('#search').val("").removeAttr 'disabled'
|
||||
$('#artist-load-spinner').hide()
|
||||
false
|
||||
|
||||
loadArtistData: (name) ->
|
||||
_search.showSpinner()
|
||||
name = name.split(' ').join('+')
|
||||
$.get '/artist/' +name+ '/', (data) ->
|
||||
if data.status in ['ok', 'loading']
|
||||
_ajax.setArchor '/artist/' +name+ '/'
|
||||
_page.print data.html
|
||||
_search.hideSpinner()
|
||||
if data.status is 'loading'
|
||||
setTimeout () ->
|
||||
_search.loadArtistData name
|
||||
, 5000
|
||||
else if data.status is 'corrected'
|
||||
_search.loadArtistData data.correct_name
|
||||
else if data.status is 'suggestions'
|
||||
_search.hideSpinner()
|
||||
_page.print data.html
|
||||
else if data.status == 'fail'
|
||||
_search.hideSpinner()
|
||||
_page.print data.html
|
||||
false
|
||||
|
||||
showSuggestions: (values) ->
|
||||
@@ -59,13 +81,10 @@ $('.search').live 'click', ->
|
||||
_beathaven.adjustSizes()
|
||||
_beathaven.redrawScrollbar()
|
||||
false
|
||||
$('.search_form').live 'submit', ->
|
||||
$('.autocomplete-container').remove()
|
||||
_ajax.loadArtistData $('.search_field').first().val()
|
||||
false
|
||||
$('.suggestions a').live 'click', ->
|
||||
$('.search_field').first().val $(this).text()
|
||||
$('#search-form').live 'submit', ->
|
||||
$('#autocomplete-container').remove()
|
||||
_search.loadArtistData $('#search').val()
|
||||
false
|
||||
$('.data.artist').live 'click', ->
|
||||
_ajax.go('/artist/'+$(this).text().replace(' ', '+')+'/');
|
||||
false;
|
||||
_search.loadArtistData $(this).html()
|
||||
false
|
||||
@@ -1,140 +1,140 @@
|
||||
class window.Vkontakte
|
||||
|
||||
qr: null
|
||||
api_id: null
|
||||
|
||||
constructor: (@api_id) ->
|
||||
|
||||
getApiId: ->
|
||||
@api_id
|
||||
|
||||
init: ->
|
||||
@qr = []
|
||||
|
||||
window.vkAsyncInit = ->
|
||||
VK.init apiId: _vkontakte.getApiId()
|
||||
VK.Auth.getLoginStatus (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
|
||||
qr: null
|
||||
api_id: null
|
||||
|
||||
constructor: (@api_id) ->
|
||||
|
||||
getApiId: ->
|
||||
@api_id
|
||||
|
||||
init: ->
|
||||
@qr = []
|
||||
|
||||
window.vkAsyncInit = ->
|
||||
VK.init apiId: _vkontakte.getApiId()
|
||||
VK.Auth.getLoginStatus (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
|
||||
|
||||
setTimeout ->
|
||||
$('#vk_api_transport').append('<script async="async" type="text/javascript" src="http://vkontakte.ru/js/api/openapi.js"></script>')
|
||||
, 0
|
||||
|
||||
authInfo: (response) ->
|
||||
if typeof response isnt 'undefined' and response.session
|
||||
_session = new Session(response.session)
|
||||
setTimeout ->
|
||||
$('#vk_api_transport').append('<script async="async" type="text/javascript" src="http://vkontakte.ru/js/api/openapi.js"></script>')
|
||||
, 0
|
||||
|
||||
authInfo: (response) ->
|
||||
if typeof response isnt 'undefined' and response.session
|
||||
_session = new Session(response.session)
|
||||
|
||||
$('#vk_login, .auth-notice').hide()
|
||||
|
||||
_session.query '/user/auth', {}, (ar) ->
|
||||
if ar.newbie
|
||||
VK.Api.call 'getVariable', key: 1281, (r) ->
|
||||
_session.query '/user/update', name: r.response, (ar2) ->
|
||||
_session.setUser ar2.user
|
||||
$('.header-container .hello .greating')
|
||||
.html _beathaven.ls('HELLO')+', <span class="settings">' +(if _session.getUser().name then _session.getUser().name else '%username%')+ '</span>!'
|
||||
window._session = _session
|
||||
_ajax.detectPage()
|
||||
$('.fullscreen').hide();
|
||||
else
|
||||
_session.setUser ar.user
|
||||
|
||||
$('.header-container .hello').show()
|
||||
$('.header-container .hello .greating')
|
||||
.html _beathaven.ls('HELLO')+', <span class="settings">' +(if _session.getUser().name then _session.getUser().name else '%username%')+ '</span>!'
|
||||
window._session = _session
|
||||
_ajax.detectPage()
|
||||
$('.fullscreen').hide();
|
||||
|
||||
if response.session.expire?
|
||||
setTimeout ->
|
||||
_vkontakte.auth()
|
||||
false
|
||||
, response.session.expire * 1000 - new Date().getTime() + 1000
|
||||
else
|
||||
_session = new Session({})
|
||||
_session.setUser {}
|
||||
$('#vk_login').css display: 'block'
|
||||
$('.auth-notice').css('left', $('#vk_login').offset().left).show()
|
||||
$('.header-container .hello').hide()
|
||||
window._session = _session
|
||||
_ajax.detectPage()
|
||||
$('.fullscreen').hide();
|
||||
|
||||
auth: ->
|
||||
VK.Auth.getLoginStatus (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
false
|
||||
, 8
|
||||
false
|
||||
|
||||
loadTracksData: (artist, track, duration, callback) ->
|
||||
track_prepared = track.replace(/\(.*\)/i, '').split('/')[0];
|
||||
query = artist+' '+track_prepared;
|
||||
if url = _vkontakte.getQR query
|
||||
callback url
|
||||
else
|
||||
VK.Api.call 'audio.search', q: query, (r) ->
|
||||
url = _vkontakte.matchPerfectResult r.response, artist, track, duration
|
||||
_vkontakte.addQR query, url
|
||||
callback url
|
||||
|
||||
matchPerfectResult: (data, artist, track, duration) ->
|
||||
duration = duration.split ':'
|
||||
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
|
||||
best_score = 0;
|
||||
best_result = null;
|
||||
for item in data
|
||||
if typeof item is 'object'
|
||||
score = 0;
|
||||
item.artist = item.artist.trim();
|
||||
item.title = item.title.trim();
|
||||
|
||||
if item.artist == artist
|
||||
score += 10
|
||||
else if item.artist.split(artist).length is 2
|
||||
score += 5
|
||||
else if item.title.split(artist).length is 2
|
||||
score += 4
|
||||
|
||||
if item.title == track
|
||||
score += 10
|
||||
else if item.title.split(track).length is 2
|
||||
score += 5
|
||||
|
||||
if parseInt(item.duration, 10) == duration
|
||||
score += 15
|
||||
else
|
||||
delta = Math.abs parseInt(item.duration, 10) - duration
|
||||
score += (10 - delta) if delta < 10
|
||||
|
||||
if score > best_score
|
||||
best_score = score
|
||||
best_result = item
|
||||
|
||||
if score is 35
|
||||
return best_result.url
|
||||
$('#vk_login, .auth-notice').hide()
|
||||
|
||||
_session.query '/user/auth', {}, (ar) ->
|
||||
if ar.newbie
|
||||
VK.Api.call 'getVariable', key: 1281, (r) ->
|
||||
_session.query '/user/update', name: r.response, (ar2) ->
|
||||
_session.setUser ar2.user
|
||||
$('.header-container .hello .greating')
|
||||
.html _beathaven.ls('HELLO')+', <span class="settings">' +(if _session.getUser().name then _session.getUser().name else '%username%')+ '</span>!'
|
||||
window._session = _session
|
||||
_ajax.detectPage()
|
||||
$('.fullscreen').hide();
|
||||
else
|
||||
_session.setUser ar.user
|
||||
|
||||
$('.header-container .hello').show()
|
||||
$('.header-container .hello .greating')
|
||||
.html _beathaven.ls('HELLO')+', <span class="settings">' +(if _session.getUser().name then _session.getUser().name else '%username%')+ '</span>!'
|
||||
window._session = _session
|
||||
_ajax.detectPage()
|
||||
$('.fullscreen').hide();
|
||||
|
||||
if response.session.expire?
|
||||
setTimeout ->
|
||||
_vkontakte.auth()
|
||||
false
|
||||
, response.session.expire * 1000 - new Date().getTime() + 1000
|
||||
else
|
||||
_session = new Session({})
|
||||
_session.setUser {}
|
||||
$('#vk_login').css display: 'block'
|
||||
$('.auth-notice').css('left', $('#vk_login').offset().left).show()
|
||||
$('.header-container .hello').hide()
|
||||
window._session = _session
|
||||
_ajax.detectPage()
|
||||
$('.fullscreen').hide();
|
||||
|
||||
auth: ->
|
||||
VK.Auth.getLoginStatus (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
false
|
||||
, 8
|
||||
false
|
||||
|
||||
loadTracksData: (artist, track, duration, callback) ->
|
||||
track_prepared = track.replace(/\(.*\)/i, '').split('/')[0];
|
||||
query = artist+' '+track_prepared;
|
||||
if url = _vkontakte.getQR query
|
||||
callback url
|
||||
else
|
||||
VK.Api.call 'audio.search', q: query, (r) ->
|
||||
url = _vkontakte.matchPerfectResult r.response, artist, track, duration
|
||||
_vkontakte.addQR query, url
|
||||
callback url
|
||||
|
||||
matchPerfectResult: (data, artist, track, duration) ->
|
||||
duration = duration.split ':'
|
||||
duration = parseInt(duration[0], 10) * 60 + parseInt(duration[1], 10)
|
||||
best_score = 0;
|
||||
best_result = null;
|
||||
for item in data
|
||||
if typeof item is 'object'
|
||||
score = 0;
|
||||
item.artist = item.artist.trim();
|
||||
item.title = item.title.trim();
|
||||
|
||||
if item.artist == artist
|
||||
score += 10
|
||||
else if item.artist.split(artist).length is 2
|
||||
score += 5
|
||||
else if item.title.split(artist).length is 2
|
||||
score += 4
|
||||
|
||||
if item.title == track
|
||||
score += 10
|
||||
else if item.title.split(track).length is 2
|
||||
score += 5
|
||||
|
||||
if parseInt(item.duration, 10) == duration
|
||||
score += 15
|
||||
else
|
||||
delta = Math.abs parseInt(item.duration, 10) - duration
|
||||
score += (10 - delta) if delta < 10
|
||||
|
||||
if score > best_score
|
||||
best_score = score
|
||||
best_result = item
|
||||
|
||||
if score is 35
|
||||
return best_result.url
|
||||
|
||||
return best_result.url
|
||||
|
||||
addQR: (query, url) ->
|
||||
@qr[query] = url;
|
||||
|
||||
getQR: (query) ->
|
||||
if @qr[query]?
|
||||
@qr[query]
|
||||
false
|
||||
return best_result.url
|
||||
|
||||
addQR: (query, url) ->
|
||||
@qr[query] = url;
|
||||
|
||||
getQR: (query) ->
|
||||
if @qr[query]?
|
||||
@qr[query]
|
||||
false
|
||||
|
||||
$('#vk_login, .auth-notice').live 'click', ->
|
||||
VK.Auth.login (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
false
|
||||
, 8
|
||||
false
|
||||
VK.Auth.login (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
false
|
||||
, 8
|
||||
false
|
||||
$('#vk_logout').live 'click', ->
|
||||
_ajax.go '/search/';
|
||||
VK.Auth.logout (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
false
|
||||
false
|
||||
_ajax.go '/search/';
|
||||
VK.Auth.logout (response) ->
|
||||
_vkontakte.authInfo(response)
|
||||
false
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user