summaryrefslogtreecommitdiff
path: root/frontend/static/js/src/search.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/static/js/src/search.js')
-rw-r--r--frontend/static/js/src/search.js190
1 files changed, 190 insertions, 0 deletions
diff --git a/frontend/static/js/src/search.js b/frontend/static/js/src/search.js
new file mode 100644
index 0000000..85014af
--- /dev/null
+++ b/frontend/static/js/src/search.js
@@ -0,0 +1,190 @@
+YOUTUBE_SEARCH_URL = "https://gdata.youtube.com/feeds/api/videos"
+YOUTUBE_URL_PREFIX = "http://youtube.com/watch?v="
+function courtesy_s (quantity, noun)
+ {
+ if (quantity > 1)
+ return quantity + " " + noun + "s"
+ return quantity + " " + noun
+ }
+var Search =
+ {
+ start: 0,
+ limit: 20,
+ sj: function ()
+ {
+ Search.start = 0
+ Search.terms = $("#search-terms").val()
+ Search.sjSearch (Search.terms, Search.start)
+ },
+ sjSearch: function (terms, start)
+ {
+ var params =
+ {
+ "q": terms,
+ "start": Search.start,
+ "limit": Search.limit,
+ "session": Auth.session,
+ }
+ $.post(API.URL.video.search, params, Search.sjCallback)
+ $("#search-instructions").hide()
+ $("#search-results").html("").hide()
+ $("#search-loading").show()
+ $("#search-results-container").show()
+ },
+ sjCallback: function (raw)
+ {
+ var lines = API.parse ("/video/search", raw)
+ var items = []
+ for (var i = 0; i < lines.length; i++)
+ {
+ // 0 id 1 score 2 user 3 usercount 4 title 5 url 6 thumbnail
+ var line = lines[i].split("\t")
+ if (line.length < 7)
+ continue
+ var video =
+ {
+ url: line[5],
+ thumbnail: line[6],
+ title: line[4],
+ user: line[2],
+ quantify: "",
+ }
+ if (parseInt(line[3]) > 1)
+ video['user'] += " + " + courtesy_s (parseInt(line[3])-1, "other")
+ if (parseInt(line[1]) > 0)
+ video['quantify'] = courtesy_s (parseInt(line[1]), "like")
+ var tag = Search.resultTag (video)
+ items.push(tag)
+ }
+ if (items.length === Search.limit)
+ {
+ Search.start += Search.limit
+ $("#search-next-page").show()
+ }
+ else
+ {
+ $("#search-next-page").hide()
+ }
+ $("#search-loading").hide()
+ $("#search-results").html(items.join("")).show()
+ $("#search-instructions").show()
+ $("#curtain").bind("click", Search.close).css({"background-color": "transparent", "z-index": 99}).show()
+ },
+ youtube: function ()
+ {
+ var terms = $("#search-terms").val()
+ var params =
+ {
+ "q": terms,
+ "v": 2,
+ "alt": "jsonc",
+ }
+ $.get(YOUTUBE_SEARCH_URL, params, Search.youtubeCallback, "jsonp")
+ $("#search-results-container").show()
+ $("#search-results").html("").hide()
+ $("#search-loading").show()
+ },
+ durationToString: function (duration)
+ {
+ return Math.floor(duration / 60) + ":" + (duration % 60)
+ },
+ viewCountToString: function (viewCount)
+ {
+ if (! viewCount)
+ return '0'
+ var vc = viewCount.toString ()
+ var commas = /(\d+)(\d{3})/;
+ while (commas.test(vc))
+ {
+ vc = vc.replace(commas, '$1' + ',' + '$2');
+ }
+ return vc
+ },
+ resultTag: function (video)
+ {
+ var tag = "<li data-url='"+video['url']+"'>"
+ tag += "<div class='thumb' style='background-image: url(" + video['thumbnail'] + ")'></div>"
+ tag += "<h4>" + video['title'] + "</h4>"
+ tag += "<span class='metadata'>"
+ tag += video['user']
+ tag += "<br/>"
+ tag += video['quantify']
+ tag += "</span>"
+ tag += "<a href='"+video['url']+"' target='_blank' class='preview'>Preview</a>"
+ tag += "</li>"
+ return tag
+ },
+ youtubeCallback: function (data)
+ {
+ var items = []
+ for (var i = 0; i < data['data']['items'].length; i++)
+ {
+ var item = data['data']['items'][i]
+ var video =
+ {
+ url: YOUTUBE_URL_PREFIX+item['id'],
+ thumbnail: item['thumbnail']['sqDefault'],
+ title: item['title'],
+ user: item['uploader'],
+ quantify: Search.viewCountToString(item['viewCount']) + "views",
+ }
+ var tag = Search.resultTag (video)
+ items.push(tag)
+ }
+ $("#search-loading").hide()
+ $("#search-results").html(items.join("")).show()
+ },
+ keydown: function (e)
+ {
+ if (e.keyCode === 13)
+ {
+ Search.sj ()
+ }
+ if (e.keyCode === 27)
+ {
+ Search.close ()
+ Keyboard.focusTextarea ()
+ }
+ },
+ nextPage: function ()
+ {
+ Search.sjSearch (Search.terms, Search.start)
+ },
+ loadResult: function ()
+ {
+ var url = $(this).parent().data("url")
+ $.post(API.URL.room.say, {room: Room.name, session: Auth.session, msg: url})
+ Search.close ()
+ },
+ close: function ()
+ {
+ $("#curtain").unbind("click", Search.close).hide()
+ $("#search-results-container").hide()
+ $("#search-terms").val("")
+ },
+ blurSearchTextarea: function ()
+ {
+ $(window).unbind("keydown")
+ $("#chat-message").unbind("keydown").bind("keydown", Keyboard.textareaMap)
+ $("#chat-message").unbind("focus").focus().bind("focus", Keyboard.focusTextarea)
+ if ($("#chat-message").val().length === 0)
+ Keyboard.enteredText = false
+ },
+ focusSearchTextarea: function ()
+ {
+ $(window).unbind("keydown")
+ $("#chat-message").unbind("keydown")
+ },
+ init: function ()
+ {
+ $("#search-results li div").live("click", Search.loadResult)
+ $("#search-results li h4").live("click", Search.loadResult)
+ $("#search-results li span").live("click", Search.loadResult)
+ $("#search-terms").bind("keydown", Search.keydown)
+ $("#search-terms").bind("focus", Search.focusSearchTextarea)
+ $("#search-terms").bind("blur", Search.blurSearchTextarea)
+ // $("#search-terms").val("glock n my hand")
+ // Search.sj ()
+ }
+ }
+Search.init ()