Skip to content
Browse files

live reordering

  • Loading branch information...
2 parents a765a31 + 29624e9 commit d0c09aebe74455341f7c04aa00252a05ec2c5c9a @Evangenieur committed
View
8 public/javascripts/coffee-script.min.js
8 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
74 public/javascripts/hvidio.js
@@ -168,6 +168,7 @@
hvidio.fetch(keyword, function(data) {
$main.addClass('large');
+ console.log(data[0].msgs.length, data[0].provider, data[0].msgs[0].provider, data[0].msgs[0].text );
hvidio.templatize('#videosTemplate', { search: urlify(keyword), videos: data }, '#results');
hvidio
@@ -182,6 +183,7 @@
return this;
},
+<<<<<<< HEAD
order: function(videos) {
console.log("order", videos, this.keyword, $('#results'));
hvidio.templatize('#videosTemplate', { search: urlify(this.keyword), videos: videos }, '#results');
@@ -190,39 +192,99 @@
.loading(false)
.play(videos[0].embed)
.initScroller(true);
+=======
+ insert_video: function(container, pos, video) {
+ var $html = $(hvidio.templatize('#videoTemplate', { video: video }));
+
+ elem = $(container + " > li:eq("+pos+")")
+ if ( elem.length ) {
+ elem.before($html);
+ } else {
+ $(container + " > li:eq("+(pos - 1)+")").after($html);
+ }
+ $html.css('visibility','visible').hide().fadeIn('fast');
+ },
+ get_video_score: function(video) {
+ //return - video.msgs.length;
+ return - (new Date(video.date)).valueOf();
+>>>>>>> live-reordering
},
fetch: function(keyword, callback) {
var self = this;
+ obs = new OrderByScore();
+ var videos = {};
+
search = Search(keyword)
- .on("video.new", function() {
+ .reduce(function(video) {
+ })
+ .on("video.new", function(video) {
var pos;
+<<<<<<< HEAD
+=======
+ video.msg = video.msgs[0];
+ /*
+ video.score = _.reduce(video.msgs, function(memo, num) {
+ return (memo + (parseInt(num.votes) + 1)) || 1;
+ }, 0);
+*/
+ video.score = self.get_video_score(video);
+ var pos = obs.get_pos(video.score);
+
+ video.date = video.msgs[0].post_date;
+>>>>>>> live-reordering
- if ((pos = this.embed.indexOf("?")) != -1) {
- this.embed = this.embed.substr(0, pos);
+ if ((pos = video.embed.indexOf("?")) != -1) {
+ video.embed = video.embed.substr(0, pos);
}
$('#counter').text(++counter);
if (typeof search.initiated == "undefined") {
search.initiated = true;
+<<<<<<< HEAD
callback([this]);
} else {
var html = hvidio.templatize('#videoTemplate', { video: this }),
$mylist = $("#video-list-" + urlify(keyword)),
$html = $(html);
+=======
+ scroll = false;
- $mylist.append($html);
+ callback([video]);
+ } else {
+>>>>>>> live-reordering
+
+ self.insert_video("#video-list-" + urlify(keyword), pos, video);
hvidio.initScroller();
- $html.css('visibility','visible').hide().fadeIn('slow');
}
+<<<<<<< HEAD
}).on("video.update", function() {
console.log("update", this.dom_id);
var $tip = $('#' + this.dom_id + ' .tip');
+=======
+ }).on("video.update", function(video) {
+ var tmp_score = self.get_video_score(video);
+ if (tmp_score != video.score) {
+ obs.remove_score(video.score);
+ video.score = tmp_score
+ var pos = obs.get_pos(video.score);
+ if ($("#video-list-" + urlify(keyword) + " > li:eq("+pos+")").attr("id") != video.dom_id) {
+ console.log("score != ?", tmp_score, video.score)
+ $('#' + video.dom_id).fadeOut().remove()
+ self.insert_video("#video-list-" + urlify(keyword), pos, video);
+ }
+ }
+
+ console.log("update", video.dom_id, video.provider, video.msgs[0].provider, video.msgs.length, video.msgs[0].text);
+ var $tip = $('#' + video.dom_id + ' .tip'),
+ score = parseInt($tip.text()) || 1,
+ newScore = score + 1; //(video.msgs[video.msgs.length - 1].votes || 1);
+>>>>>>> live-reordering
$tip.text(this.score + '+');
$tip.addClass('incremented animated bounce');
@@ -230,10 +292,12 @@
}).on("finished", function() {
console.log("FINISHED");
hvidio.loading(false);
+ /*
self.order(
search.videos_by_date()
//search.videos_by_posts()
);
+ */
});
return this;
View
99 public/javascripts/live-search.coffee
@@ -1,85 +1,58 @@
-window.Search = class Search
+window.OrderByScore = class OrderByScore
+ constructor: () ->
+ @scores = []
+ @scores_hit = {}
+
+ get_pos: (score) ->
+ pos = _(@scores).sortedIndex score
+ unless @scores[pos]
+ @scores[pos] = score
+ else if (@scores[pos] != score) and (@scores[pos+1] != score)
+ @scores.splice(pos, 0, score)
+
+ @scores_hit[score] or= 0
+ @scores_hit[score]++
+
+ console.log "get_pos", score, pos#, @scores
+ pos
+
+ remove_score: (score) ->
+ @scores_hit[score]--
+ if @scores_hit[score] == 0
+ @scores = _(@scores).without score
+
+window.SearchStream = class SearchStream
+
instances = {}
+
constructor: (search_term, opts) ->
- if @constructor.name is "Search"
+ if @constructor.name is "SearchStream"
@search_term = search_term
- @videos = {}
- @events or= []
+ @ee = new EventEmitter2()
my_timer = null
search_me = =>
- unless Search.socket?.emit?
+ unless SearchStream.socket?.emit?
my_timer = setInterval(search_me, 100) unless my_timer
else
clearInterval(my_timer) if my_timer
- finished = _(@events).filter((e) -> e.finished?).pop().finished
- Search.socket.emit "search", @search_term, finished
+ SearchStream.socket.emit "search", @search_term, =>
+ @ee.emit "finished"
_.defer search_me
- #instances[search_term] = @
else
- return instances[search_term] or= new Search(search_term, opts)
-
- video_reduce: (video) ->
- msg = video.msg
- delete video.msg
- video.dom_id = video.id.replace "/", "-"
- video.msgs = []
-
- (@videos[video.id] or= video).msgs.push msg
-
- # Jay: was here
- @videos[video.id] = (@videos[video.id] or= video)
- @videos[video.id].date = msg.post_date;
- @videos[video.id].score = (@videos[video.id].score || 1) + msg.score;
-
- console.log "videos ", Object.keys(@videos).length
- @when _(@videos).keys().length, video.id
-
- videos_by_posts: ->
- _(@videos).sortBy (v) -> - v.msgs.length
-
- videos_by_date: ->
- _(@videos).sortBy (v) -> - (new Date(v.date)).valueOf()
-
- videos_ids: -> _(@videos).keys()
-
- when: (num, cb) ->
- if _.isFunction cb
- @events.push
- num: num
- callback: cb
- else if @events.length > 0
- for i in [0..@events.length - 1]
- if @events[i]?.num? and @events[i].num == num
- cb = @events[i].callback
- @events.splice i, 1
- @when_done = true
- cb.call @
- else if @when_done and @events[i]?["video.new"]?
- if (not @last_videos_length) or (@last_videos_length < num)
- @events[i]["video.new"].call @videos[cb]
- else if @when_done and @events[i]?["video.update"]?
- if (not @last_videos_length) or (@last_videos_length == num)
- @events[i]["video.update"].call @videos[cb]
- @last_videos_length = num
- @
+ return instances[search_term] or= new SearchStream(search_term, opts)
- on: (msg, cb) ->
- if _.isFunction cb
- event = {}
- event[msg] = cb
- @when_done = true unless @events.length
- @events.push event
- @
+ on: (msg, cb) -> @ee.on msg, cb
+ emit: (msg, data...) -> @ee.emit msg, data...
@com_init: (socket) ->
console.log "handling init"
socket.on "search_result", (res) =>
- @get(res.search_term)?.video_reduce video for video in res.videos
+ @get(res.search_term)?.ee.emit "data", res.data
- Search.socket = socket
+ SearchStream.socket = socket
@get: (search_term) -> instances[search_term]
View
2 public/stylesheets/style.css
@@ -273,7 +273,7 @@ p.tagline {
font-size: 10px;
}
#results .video .video-cell img.preview {
- visibility: hidden;
+ /*visibility: hidden;*/
width: 210px;
height: 135px;
}
View
2 public/stylesheets/style.less
@@ -328,7 +328,7 @@ p.tagline {
}
img.preview {
- visibility: hidden;
+ //visibility: hidden;
width: 210px;
height: 135px;
}
View
4 server.coffee
@@ -33,9 +33,7 @@ require('zappajs') ->
message: (video) =>
@emit search_result:
search_term: @data
- videos: [
- video
- ]
+ data: video
exit: ->
console.log "exit #{worker}"
deferred.resolve()
View
8 views/layout.jade
@@ -10,7 +10,9 @@ html
body
block content
+
script(src='/socket.io/socket.io.js')
+ script(src='/javascripts/eventemitter2.js')
script(src='/javascripts/jquery.min.js')
script(src='/javascripts/jquery.timeago.min.js')
script(src='/javascripts/jquery.mousewheel.min.js')
@@ -18,5 +20,7 @@ html
script(src='/javascripts/jquery.mcustomscrollbar.min.js')
script(src='/javascripts/underscore.min.js')
script(src='/javascripts/canvasloader.min.js')
- script(src='/javascripts/live-search.js')
- script(src='/javascripts/hvidio.js')
+ //script(src='/javascripts/live-search.js')
+ script(src='/javascripts/coffee-script.min.js')
+ script(src='/javascripts/live-search.coffee',type="text/coffeescript")
+ script(src='/javascripts/hvidio.coffee',type="text/coffeescript")
View
2 workers/twitter_search_worker.coffee
@@ -14,7 +14,7 @@ tc = new ntwitter
video_search = (search, opts = {}) ->
_(opts).defaults
include_entities: on
- rpp: 50
+ rpp: 20
result_type: "recent"
#lang: "fr"

0 comments on commit d0c09ae

Please sign in to comment.
Something went wrong with that request. Please try again.