js/playscreen.js
changeset 9 5b743d461f99
parent 7 a2d5b669f663
child 10 ab587bf164f9
equal deleted inserted replaced
7:a2d5b669f663 9:5b743d461f99
    59     
    59     
    60     var sortedTopics = data.topics.filter(function(t) {
    60     var sortedTopics = data.topics.filter(function(t) {
    61         return pageParams.visibletopics.indexOf(t.index.toString()) !==-1 && t.index !== topicPoubelle;
    61         return pageParams.visibletopics.indexOf(t.index.toString()) !==-1 && t.index !== topicPoubelle;
    62     });
    62     });
    63     
    63     
       
    64     
       
    65     function goToNext() {
       
    66     	var topics = Array.prototype.slice.call($(".topic.selected").map(function(){return $(this).attr("data-topic-id")}));
       
    67     	for (var i = 0; i < data.segments.length; i++) {
       
    68     		var mmso = data.segments[i];
       
    69     		if (mmso.start >= localpos) {
       
    70     			for (var j = 0; j < mmso.topics.length; j++) {
       
    71     				if (topics.indexOf(mmso.topics[j].topic.toString()) !== -1) {
       
    72     					localpos = mmso.start;
       
    73     					showLocal();
       
    74     					return;
       
    75     				}
       
    76     			}
       
    77     		}
       
    78     	}
       
    79     	console.log("We haven't found a segment");
       
    80     }
    64     
    81     
    65     function showTopics(topiclist) {
    82     function showTopics(topiclist) {
    66         var tbhtml = topiclist.reduce(function(mem, topic) {
    83         var tbhtml = topiclist.reduce(function(mem, topic) {
    67             var wordsToShow = topic.words.slice(0,3),
    84             var wordsToShow = topic.words.slice(0,3),
    68                 max = wordsToShow[0].weight,
    85                 max = wordsToShow[0].weight,
   253     	higherFiveSecs,
   270     	higherFiveSecs,
   254     	localyscale;
   271     	localyscale;
   255 	
   272 	
   256 	entonnoir.toBack();
   273 	entonnoir.toBack();
   257     
   274     
   258     var cloudTemplate = _.template('<li style="font-size: <%- size %>px;"><%- word %></li>');
   275     var cloudTemplate = _.template('<li style="font-size: <%- size %>px;"<% if (selected) {%> class="selected"<% } %>><%- word %></li>');
   259     
   276     
   260     function showLocal() {
   277     function showLocal() {
   261 		localyscale = ph / localduration;
   278 		localyscale = ph / localduration;
   262     	var localstart = localpos - localduration/2;
   279     	var localstart = localpos - localduration/2;
   263     		localend = localpos + localduration/2;
   280     		localend = localpos + localduration/2;
   386 	    		return { word: k, score: v }
   403 	    		return { word: k, score: v }
   387 	    	})
   404 	    	})
   388 	    	.sortBy(function(w) {
   405 	    	.sortBy(function(w) {
   389 	    		return -w.score;
   406 	    		return -w.score;
   390 	    	})
   407 	    	})
   391 	    	.first(30)
   408 	    	.first(40)
   392 	    	.value();
   409 	    	.value();
   393     	
   410     	
   394     	var values = _(localkeywords).pluck('score'),
   411     	var values = _(localkeywords).pluck('score'),
   395     		max = Math.max.apply(Math, values),
   412     		max = Math.max.apply(Math, values),
   396     		min = Math.min.apply(Math, values),
   413     		min = Math.min.apply(Math, values),
   397     		scale = 10 / (max - Math.min(max - .1, min));
   414     		scale = 10 / (max - Math.min(max - .1, min)),
       
   415     		selectedVisible = false;
   398 		
   416 		
   399 		localkeywords.forEach(function(w) {
   417 		localkeywords.forEach(function(w) {
   400 			w.size = 10 + (w.score - min) * scale;
   418 			w.size = 10 + (w.score - min) * scale;
   401 		});
   419 			w.selected = (w.word === selectedWord);
       
   420 			selectedVisible = selectedVisible || w.selected;
       
   421 		});
       
   422     	
       
   423     	if (!selectedVisible) {
       
   424     		selectedWord = false;
       
   425     	}
   402     	
   426     	
   403     	$(".play-tagcloud").html(localkeywords.map(cloudTemplate).join(""));
   427     	$(".play-tagcloud").html(localkeywords.map(cloudTemplate).join(""));
   404 	    
   428 	    
   405 		throttledGetTweets();
   429 		throttledGetTweets();
   406 	    
   430 	    
   407 		showTopicViz();
   431 		showTopicViz();
   408     }
   432     }
   409     
   433     
       
   434     $(".play-tagcloud").on("click","li", function() {
       
   435     	if ($(this).hasClass("selected")) {
       
   436     		selectedWord = false;
       
   437     	} else {
       
   438     		selectedWord = $(this).text();
       
   439     	}
       
   440     	throttledShowLocal();
       
   441     });
       
   442     
   410     
   443     
   411     var lastPos, lastDuration, lastTopics;
   444     var lastPos, lastDuration, lastTopics;
   412     
   445     
   413     var tweetTemplate = _.template('<li class="tweet" data-timestamp="<%= timestamp %>" data-topic-id="<%= topic.topic %>"><img src="<%- data.profile_image_url %>" /><p>@<%- data.from_user_name %>: <%- data.text %></p></li>'),
   446     var tweetTemplate = _.template('<li class="tweet" data-timestamp="<%= timestamp %>" data-topic-id="<%= topic.topic %>"><img src="<%- data.profile_image_url %>" /><p>@<%- data.from_user_name %>: <%= data.htext %></p></li>'),
   414     	callnum = 0,
   447     	callnum = 0,
   415     	tweetstructure = {},
   448     	tweetstructure = {},
   416     	requestedtweets = {},
   449     	requestedtweets = {},
   417     	_NTWEETS = 10;
   450     	_NTWEETS = 20,
       
   451     	selectedWord = false;
   418     
   452     
   419     function showTweets() {
   453     function showTweets() {
   420     	    	
   454     	    	
   421     	var toshow = [];
   455     	var toshow = [];
   422     	var topics = Array.prototype.join.call($(".topic.selected").map(function(){return $(this).attr("data-topic-id")})).split(",");
   456     	var topics = Array.prototype.join.call($(".topic.selected").map(function(){return $(this).attr("data-topic-id")})).split(",");
   441     	var tweetstoshow = toshow.map(function(tid) {
   475     	var tweetstoshow = toshow.map(function(tid) {
   442     		return requestedtweets[tid];
   476     		return requestedtweets[tid];
   443     	}).filter(function(tw) {
   477     	}).filter(function(tw) {
   444     		return ((tw.status == 2) && (tw.timestamp > (localpos - localduration / 2)) && (tw.timestamp < (localpos + localduration / 2)));
   478     		return ((tw.status == 2) && (tw.timestamp > (localpos - localduration / 2)) && (tw.timestamp < (localpos + localduration / 2)));
   445 		});
   479 		});
       
   480 				
       
   481 		if (selectedWord) {
       
   482 			var rx = new RegExp(selectedWord.replace(/(\W)/gm,'\\$1'),'im');
       
   483 			tweetstoshow = tweetstoshow.filter(function(tw) {
       
   484 				return rx.test(tw.data.text);
       
   485 			});
       
   486 		}
   446 		
   487 		
   447 		tweetstoshow.forEach(function(tw) {
   488 		tweetstoshow.forEach(function(tw) {
   448 			tw.topic = tw.topics.filter(function(t) {
   489 			tw.topic = tw.topics.filter(function(t) {
   449 				return topics.indexOf(t.topic.toString()) !== -1;
   490 				return topics.indexOf(t.topic.toString()) !== -1;
   450 			}).sort(function(a,b) {
   491 			}).sort(function(a,b) {
   452 			})[0];
   493 			})[0];
   453 		});
   494 		});
   454 		
   495 		
   455 		tweetstoshow = tweetstoshow.filter(function(t) {
   496 		tweetstoshow = tweetstoshow.filter(function(t) {
   456 			return !!t.topic;
   497 			return !!t.topic;
   457 		})
   498 		});
   458 				
   499 				
   459 		tweetstoshow.sort(function(a, b) {
   500 		tweetstoshow.sort(function(a, b) {
   460 			return b.topic.weight - a.topic.weight; 
   501 			return b.topic.weight - a.topic.weight; 
   461 		});
   502 		});
   462 		
   503 		
   463 		tweetstoshow = tweetstoshow.slice(0,10);
   504 		tweetstoshow = tweetstoshow.slice(0,10);
       
   505 		
       
   506 		if (selectedWord) {
       
   507 			var rx = new RegExp( '(' + selectedWord.replace(/(\W)/gm,'\\$1') + ')', 'gim' );
       
   508 			tweetstoshow.forEach(function(tw) {
       
   509 				tw.data.htext = _(tw.data.text).escape().replace(rx,'<span class="highlight">$1</span>');
       
   510 			});
       
   511 		} else {
       
   512 			tweetstoshow.forEach(function(tw) {
       
   513 				tw.data.htext = _(tw.data.text).escape();
       
   514 			})
       
   515 		}
   464 		
   516 		
   465 		tweetstoshow.sort(function(a, b) {
   517 		tweetstoshow.sort(function(a, b) {
   466 			return a.timestamp - b.timestamp;
   518 			return a.timestamp - b.timestamp;
   467 		});
   519 		});
   468 		
   520 		
   506 				debouncedGetTweetData();
   558 				debouncedGetTweetData();
   507 			},
   559 			},
   508 			errorHandler: function(err,info) {
   560 			errorHandler: function(err,info) {
   509 				mmstruct.status = 0;
   561 				mmstruct.status = 0;
   510 				console.error(err,info);
   562 				console.error(err,info);
   511 				throttledGetTweetIds();
   563 				debouncedGetTweetIds();
   512 			}
   564 			}
   513 		});
   565 		});
   514     }
   566     }
   515     
   567     
   516     var _MAX_BATCH = 20;
   568     var _MAX_BATCH = 20;
   550     	_(requestedtweets).each(function(v) {
   602     	_(requestedtweets).each(function(v) {
   551 			if (!v.status) {
   603 			if (!v.status) {
   552 				toload.push(v.id);
   604 				toload.push(v.id);
   553 			}
   605 			}
   554 		});
   606 		});
   555     	
   607 		
   556     	console.log("Loading tweet data");
   608 		if (toload.length) {
   557 		
   609 			toload = toload.slice(0,200);
   558 		$.getJSON(
   610 			$.getJSON(
   559 			solrUrl(
   611 			solrUrl(
   560 				"twitter",
   612 				"twitter",
   561 				{
   613 				{
   562 					q:"id:(" + toload.join(" OR ") + ")",
   614 					q:"id:(" + toload.join(" OR ") + ")",
   563 					fl: "id_str,created_at,from_user_name,text,profile_image_url",
   615 					fl: "id_str,created_at,from_user_name,text,profile_image_url",
   571 					requestedtweets[tweet.id_str].data = tweet;
   623 					requestedtweets[tweet.id_str].data = tweet;
   572 					requestedtweets[tweet.id_str].status = 2;
   624 					requestedtweets[tweet.id_str].status = 2;
   573 					requestedtweets[tweet.id_str].timestamp = timestamp;
   625 					requestedtweets[tweet.id_str].timestamp = timestamp;
   574 				});
   626 				});
   575 				throttledShowTweets();
   627 				throttledShowTweets();
   576 			}
   628 				debouncedGetTweetData();
   577 		);
   629 			});
       
   630 		}
       
   631 		
       
   632 		
   578 		
   633 		
   579     }
   634     }
   580     
   635     
   581     debouncedGetTweetData = _(getTweetData).debounce(125);
   636     debouncedGetTweetData = _(getTweetData).debounce(125);
   582     
   637     
   583     throttledGetTweetIds = _(getTweetIds).throttle(10000);
   638     throttledGetTweetIds = _(getTweetIds).throttle(10000);
       
   639     
       
   640     debouncedGetTweetIds = _(throttledGetTweetIds).debounce(125);
   584     
   641     
   585     throttledShowTweets = _(showTweets).throttle(200);
   642     throttledShowTweets = _(showTweets).throttle(200);
   586     
   643     
   587     function getLocalTweets() {
   644     function getLocalTweets() {
   588     	
   645     	
   615     });
   672     });
   616     
   673     
   617     var localpos = 300,
   674     var localpos = 300,
   618     	localduration = 600;
   675     	localduration = 600;
   619     	
   676     	
   620     showLocal();
   677     goToNext();
   621     
   678     
   622     $(".topics-block").on("mouseenter", ".topic", function() {
   679     $(".topics-block").on("mouseenter", ".topic", function() {
   623         var el = $(this);
   680         var el = $(this);
   624         el.addClass("hover");
   681         el.addClass("hover");
   625         showTopicViz();
   682         showTopicViz();
   649 	    	e.preventDefault();
   706 	    	e.preventDefault();
   650 		}
   707 		}
   651     }).mousemove(function(e) {
   708     }).mousemove(function(e) {
   652     	if (mouseIsDown) {
   709     	if (mouseIsDown) {
   653     		if (isDragging) {
   710     		if (isDragging) {
   654     			var limit = $(this).offset().left + 140,
   711     			var deltaY = e.pageY - startY,
   655     				deltaY = e.pageY - startY,
       
   656     				delta = Math.floor(deltaY / (scrollGlobal ? yscale : - localyscale));
   712     				delta = Math.floor(deltaY / (scrollGlobal ? yscale : - localyscale));
   657 				localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, startPos + delta));
   713 				localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, startPos + delta));
   658 				throttledShowLocal();
   714 				throttledShowLocal();
   659     		} else {
   715     		} else {
   660     			isDragging = true;
   716     			isDragging = true;
   661     		}
   717     		}
   662     	}
   718     	}
   663     }).mouseup(function(e) {
   719     }).mouseup(function(e) {
   664     	if (scrollGlobal && !isDragging) {
   720     	if (scrollGlobal && !isDragging) {
   665     		
   721     		var _o = $(this).offset(),
   666     		var posY = e.pageY - $(this).offset().top;
   722     			posX = e.pageX - _o.left,
   667     		localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, Math.floor(posY / yscale)));
   723     			posY = e.pageY - _o.top;
   668 			throttledShowLocal();
   724 			if (posX < 140) {
       
   725 				localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, Math.floor(posY / yscale)));
       
   726 				throttledShowLocal();
       
   727 			}
   669     	}
   728     	}
   670     });
   729     });
   671     
   730     
   672     var totalScroll = 0, zoomlevels = [ 1800, 900, 600, 300, 120, 60 ], currentlevel = 2;
   731     var totalScroll = 0, zoomlevels = [ 1800, 900, 600, 300, 120, 60 ], currentlevel = 2;
   673     
   732     
   746     
   805     
   747     loadJson("data/minutes.json", "minutes");
   806     loadJson("data/minutes.json", "minutes");
   748     loadJson("data/5secs.json", "fiveseconds");
   807     loadJson("data/5secs.json", "fiveseconds");
   749     
   808     
   750     loadJson(
   809     loadJson(
   751         solrUrl("MMSO", {q: "*:*", fl: "topic*,MMSO_id,multimediaSegment,keywordsFromSocial,keywordsFromAudio", rows: 250 }),
   810         solrUrl("MMSO", {q: "*:*", fl: "topic*,MMSO_id,multimediaSegment,keywordsFromSocial", rows: 250 }),
   752         "segments",
   811         "segments",
   753         function(d) {
   812         function(d) {
   754             return d.response.docs.map(function(mmso) {
   813             return d.response.docs.map(function(mmso) {
   755                 var tc = mmso.multimediaSegment.match(/\d+/g),
   814                 var tc = mmso.multimediaSegment.match(/\d+/g),
   756                     start = parseInt(tc[0]),
   815                     start = parseInt(tc[0]),
   770 	            			}
   829 	            			}
   771 	            		});
   830 	            		});
   772 	                });
   831 	                });
   773                 }
   832                 }
   774                 
   833                 
   775                 getKeywords("keywordsFromAudio");
   834                 //getKeywords("keywordsFromAudio");
   776                 getKeywords("keywordsFromSocial");
   835                 getKeywords("keywordsFromSocial");
   777                 
   836                 
   778                 for (var k in mmso) {
   837                 for (var k in mmso) {
   779                     if (k.substr(0,5) === "topic" && mmso[k] > .01) {
   838                     if (k.substr(0,5) === "topic" && mmso[k] > .01) {
   780                         topics.push({
   839                         topics.push({