js/playscreen.js
changeset 11 9833f3e24105
parent 10 ab587bf164f9
child 12 81a8bb1d872a
equal deleted inserted replaced
10:ab587bf164f9 11:9833f3e24105
    11 function showData() {
    11 function showData() {
    12     
    12     
    13     var topicHash = document.location.hash || "#selectedtopics=5,15&visibletopics=5,10,15";
    13     var topicHash = document.location.hash || "#selectedtopics=5,15&visibletopics=5,10,15";
    14     
    14     
    15     var pageParams = {};
    15     var pageParams = {};
       
    16     
       
    17     var syncVideo = true,
       
    18     	syncTimer;
       
    19     
       
    20     function deSync() {
       
    21     	syncVideo = false;
       
    22     	clearTimeout(syncTimer);
       
    23     	syncTimer = setTimeout(function() {
       
    24     		syncVideo = true;
       
    25     	},5000);
       
    26     }
    16     
    27     
    17     topicHash
    28     topicHash
    18     	.replace(/^#/,'')
    29     	.replace(/^#/,'')
    19     	.split('&')
    30     	.split('&')
    20     	.forEach(function(p) {
    31     	.forEach(function(p) {
    59     
    70     
    60     var sortedTopics = data.topics.filter(function(t) {
    71     var sortedTopics = data.topics.filter(function(t) {
    61         return pageParams.visibletopics.indexOf(t.index.toString()) !==-1 && t.index !== topicPoubelle;
    72         return pageParams.visibletopics.indexOf(t.index.toString()) !==-1 && t.index !== topicPoubelle;
    62     });
    73     });
    63     
    74     
       
    75     function hasTopics(mmso, topics) {
       
    76     	for (var j = 0; j < mmso.topics.length; j++) {
       
    77 			if (topics.indexOf(mmso.topics[j].topic) !== -1) {
       
    78 				return true;
       
    79 			}
       
    80 		}
       
    81 		return false;
       
    82     }
       
    83     
       
    84     function checkOrGoNext() {
       
    85     	var topics = Array.prototype.slice.call(
       
    86     		$(".topic.selected").map(function() {
       
    87     			return parseInt($(this).attr("data-topic-id"))
       
    88 			})
       
    89 		);
       
    90 		var currentMmso = _(data.segments).find(function(s) {
       
    91 			return s.start <= player.currentTime && s.end > player.currentTime;
       
    92 		});
       
    93 		
       
    94 		if (hasTopics(currentMmso, topics)) {
       
    95 			throttledShowLocal();
       
    96 		} else {
       
    97 			goToNext();
       
    98 		}
       
    99 		
       
   100     }
    64     
   101     
    65     function goToNext() {
   102     function goToNext() {
    66     	var topics = Array.prototype.slice.call($(".topic.selected").map(function(){return $(this).attr("data-topic-id")}));
   103     	var topics = Array.prototype.slice.call(
       
   104     		$(".topic.selected").map(function() {
       
   105     			return parseInt($(this).attr("data-topic-id"));
       
   106 			})
       
   107 		);
    67     	for (var i = 0; i < data.segments.length; i++) {
   108     	for (var i = 0; i < data.segments.length; i++) {
    68     		var mmso = data.segments[i];
   109     		var mmso = data.segments[i];
    69     		if (mmso.start >= localpos) {
   110     		if (mmso.start >= player.currentTime && hasTopics(mmso, topics)) {
    70     			for (var j = 0; j < mmso.topics.length; j++) {
   111     			player.setCurrentTime(mmso.start);
    71     				if (topics.indexOf(mmso.topics[j].topic.toString()) !== -1) {
   112 				throttledShowLocal();
    72     					localpos = mmso.start;
   113 				return;
    73     					showLocal();
       
    74     					return;
       
    75     				}
       
    76     			}
       
    77     		}
   114     		}
    78     	}
   115     	}
    79     	console.log("We haven't found a segment");
       
    80     }
   116     }
    81     
   117     
    82     function showTopics(topiclist) {
   118     function showTopics(topiclist) {
    83         var tbhtml = topiclist.reduce(function(mem, topic) {
   119         var tbhtml = topiclist.reduce(function(mem, topic) {
    84             var wordsToShow = topic.words.slice(0,3),
   120             var wordsToShow = topic.words.slice(0,3),
   173         }
   209         }
   174         
   210         
   175         tweetLines.forEach(function(tl) { tl.remove(); });
   211         tweetLines.forEach(function(tl) { tl.remove(); });
   176         tweetLines = [];
   212         tweetLines = [];
   177         
   213         
   178         var deltaY = $(".play-dataviz").offset().top;
   214         var deltaY = $(".play-bottom").offset().top;
   179         
   215         
   180         $(".play-localtweets .tweet:visible").each(function() {
   216         $(".play-localtweets .tweet:visible").each(function() {
   181         	var el = $(this),
   217         	var el = $(this),
   182         		liY = + el.offset().top + el.outerHeight() / 2 - deltaY,
   218         		liY = + el.offset().top + el.outerHeight() / 2 - deltaY,
   183         		tY = localyscale * (+el.attr("data-timestamp") - localpos + localduration / 2),
   219         		tY = localyscale * (+el.attr("data-timestamp") - localpos + localduration / 2),
   205         localTimeR = (localL + localW) + globL,
   241         localTimeR = (localL + localW) + globL,
   206         globR = (globW + globL),
   242         globR = (globW + globL),
   207         yscale = ph / data.duration,
   243         yscale = ph / data.duration,
   208         mx = Math.max.apply(Math, data.minutes.map(function(s) { return s.count})),
   244         mx = Math.max.apply(Math, data.minutes.map(function(s) { return s.count})),
   209         xscale = globW/mx;
   245         xscale = globW/mx;
   210     
       
   211     var segmentrects = data.segments.map(function(mmso) {
   246     var segmentrects = data.segments.map(function(mmso) {
   212         var rect = paper.rect(globL, yscale * mmso.start, globW, yscale * mmso.duration);
   247         var rect = paper.rect(globL, yscale * mmso.start, globW, yscale * mmso.duration);
   213         rect.attr({stroke: "none"});
   248         rect.attr({stroke: "none"});
   214         return rect;
   249         return rect;
   215     });
   250     });
   271     	localyscale;
   306     	localyscale;
   272 	
   307 	
   273 	entonnoir.toBack();
   308 	entonnoir.toBack();
   274     
   309     
   275     var cloudTemplate = _.template('<li style="font-size: <%- size %>px;"<% if (selected) {%> class="selected"<% } %>><%- word %></li>');
   310     var cloudTemplate = _.template('<li style="font-size: <%- size %>px;"<% if (selected) {%> class="selected"<% } %>><%- word %></li>');
       
   311     
       
   312     var globalIndic = $(".global-position"),
       
   313     	localIndic = $(".local-position"),
       
   314     	playTime = $(".current-time");
   276     
   315     
   277     function showLocal() {
   316     function showLocal() {
   278 		localyscale = ph / localduration;
   317 		localyscale = ph / localduration;
   279     	var localstart = localpos - localduration/2;
   318     	var localstart = localpos - localduration/2;
   280     		localend = localpos + localduration/2;
   319     		localend = localpos + localduration/2;
   291     			+ betweenx + "," + Math.max(ph - curve, betweenyb) +"L" + betweenx + "," + Math.min(globbottom + curve, betweenyb)
   330     			+ betweenx + "," + Math.max(ph - curve, betweenyb) +"L" + betweenx + "," + Math.min(globbottom + curve, betweenyb)
   292     			+ "Q" + betweenx + "," + globbottom + "," + globR + "," + globbottom + "L0," + globbottom,
   331     			+ "Q" + betweenx + "," + globbottom + "," + globR + "," + globbottom + "L0," + globbottom,
   293 			localkeywords = {};
   332 			localkeywords = {};
   294 			
   333 			
   295 		entonnoir.attr("path",entonnoird);
   334 		entonnoir.attr("path",entonnoird);
       
   335     	localIndic.css("top",(player.currentTime - localstart) * localyscale);
   296 		localTimes.forEach(function(t) {
   336 		localTimes.forEach(function(t) {
   297 			t.text.remove();
   337 			t.text.remove();
   298 			t.line.remove();
   338 			t.line.remove();
   299 		});
   339 		});
   300 		localMmsos.forEach(function(t) {
   340 		localMmsos.forEach(function(t) {
   445     
   485     
   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>'),
   486     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>'),
   447     	callnum = 0,
   487     	callnum = 0,
   448     	tweetstructure = {},
   488     	tweetstructure = {},
   449     	requestedtweets = {},
   489     	requestedtweets = {},
   450     	_NTWEETS = 20,
   490     	_NTWEETS = 50,
   451     	selectedWord = false;
   491     	selectedWord = false;
   452     
   492     
   453     function showTweets() {
   493     function showTweets() {
   454     	    	
   494     	    	
   455     	var toshow = [];
   495     	var toshow = [];
   674     	$(".topic[data-topic-id=" + id + "]").addClass("selected").attr("data-timestamp",++ordertag);
   714     	$(".topic[data-topic-id=" + id + "]").addClass("selected").attr("data-timestamp",++ordertag);
   675     });
   715     });
   676     
   716     
   677     var localpos = 300,
   717     var localpos = 300,
   678     	localduration = 600;
   718     	localduration = 600;
   679     	
   719    	
   680     goToNext();
   720    	var player = new Player();
       
   721    	
       
   722    	player.duration = data.duration;
       
   723    	
       
   724    	player.on("play", function() {
       
   725    		$(".play-button").html("▐&nbsp;▌");
       
   726    	});
       
   727    	player.on("pause", function() {
       
   728    		$(".play-button").text("▶");
       
   729    	});
       
   730    	player.on("timeupdate", function(t) {
       
   731    		playTime.text(secsToString(t));
       
   732    		globalIndic.css("top", yscale * t);
       
   733    		if (localyscale) {
       
   734    			var localy = (+t - localpos + localduration / 2) * localyscale;
       
   735    			localIndic.css("top", localy);
       
   736    		}
       
   737     	if (syncVideo) {
       
   738     		localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, t));
       
   739     		throttledShowLocal();
       
   740     	}
       
   741    	});
       
   742    	
       
   743    	$(".play-button").click(function() {
       
   744    		if (player.paused) {
       
   745    			player.play();
       
   746    		} else {
       
   747    			player.pause();
       
   748    		}
       
   749    	});
       
   750     
       
   751     $(".next-button").click(goToNext);
   681     
   752     
   682     $(".topics-block").on("mouseenter", ".topic", function() {
   753     $(".topics-block").on("mouseenter", ".topic", function() {
   683         var el = $(this);
   754         var el = $(this);
   684         el.addClass("hover");
   755         el.addClass("hover");
   685         showTopicViz();
   756         showTopicViz();
   690         var el = $(this);
   761         var el = $(this);
   691         $(this).toggleClass("selected");
   762         $(this).toggleClass("selected");
   692         el.attr("data-timestamp", el.hasClass("selected") ? ++ordertag : 999999);
   763         el.attr("data-timestamp", el.hasClass("selected") ? ++ordertag : 999999);
   693         showTopicViz();
   764         showTopicViz();
   694     });
   765     });
   695     
   766 
   696     var mouseIsDown, isDragging, startY, startT, startPos, scrollGlobal, speedscale, slowiterations;
   767 	var h = Hammer($(".play-bottom")[0]);
   697     
   768 	
   698     $(document).mouseup(function() { mouseIsDown = false; isDragging = false; });
   769 	var scrollGlobal, isDragging, startPos, startLevel, scaleStep = 1/Math.log(Math.sqrt(2));
   699     
   770 	
   700     $(".play-dataviz").mousedown(function(e) {
   771 	h.on("tap", function(e) {
   701     	var l = $(this).offset().left,
   772 		var _o = $(this).offset(),
   702     		scrollLimit = l + 380;
   773 			posX = e.gesture.center.pageX - _o.left,
   703 		if (e.pageX < scrollLimit) {
   774 			posY = e.gesture.center.pageY - _o.top;
   704 			mouseIsDown = true;
   775 		if (posX < 140) {
   705 	    	startY = e.pageY;
   776 			deSync();
   706 	    	startT = new Date();
   777 			localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, Math.floor(posY / yscale)));
   707 	    	startPos = localpos;
   778 			throttledShowLocal();
   708 	    	scrollGlobal = e.pageX < (l + 140);
   779 		}
   709 	    	e.preventDefault();
   780 	})
   710 		}
   781 	.on("dragstart", function(e) {
   711     }).mousemove(function(e) {
   782 		startPos = localpos;
   712     	if (mouseIsDown) {
   783 		var x = e.gesture.center.pageX - $(this).offset().left;
   713     		if (isDragging) {
   784 		isDragging = (x < 380);
   714     			var deltaY = e.pageY - startY,
   785 		scrollGlobal = (x < 140);
   715     				delta = Math.floor(deltaY / (scrollGlobal ? yscale : - localyscale));
   786 	})
   716 				localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, startPos + delta));
   787 	.on("drag", function(e) {
   717 				throttledShowLocal();
   788 		if (isDragging && e.gesture) {
   718     		} else {
   789 			var delta = Math.floor(e.gesture.deltaY / (scrollGlobal ? yscale : - localyscale));
   719     			isDragging = true;
   790 			deSync();
   720     		}
   791 			localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, startPos + delta));
   721     	}
   792 			throttledShowLocal();
   722     }).mouseup(function(e) {
   793 		}
   723     	if (scrollGlobal && !isDragging) {
   794 	})
   724     		var _o = $(this).offset(),
   795 	.on("touch", function(e) {
   725     			posX = e.pageX - _o.left,
   796 		startLevel = currentlevel;
   726     			posY = e.pageY - _o.top;
   797 	})
   727 			if (posX < 140) {
   798 	.on("pinchin", function(e) {
   728 				localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, Math.floor(posY / yscale)));
   799 		var newlevel = Math.max(0, Math.min(zoomlevels.length - 1, startLevel + Math.round(Math.log(e.gesture.scale)*scaleStep)));
   729 				throttledShowLocal();
   800 		if (newlevel !== currentlevel) {
   730 			}
   801 			currentlevel = newlevel;
   731     	}
   802 			localduration = zoomlevels[currentlevel];
   732     });
   803 			localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, localpos));
   733     
   804 			throttledShowLocal();
       
   805 		}
       
   806 	});
       
   807    
   734     var totalScroll = 0, zoomlevels = [ 1800, 900, 600, 300, 120, 60 ], currentlevel = 2;
   808     var totalScroll = 0, zoomlevels = [ 1800, 900, 600, 300, 120, 60 ], currentlevel = 2;
   735     
   809     
   736     $(".play-dataviz").mousewheel(function(_event, _scrolldelta) {
   810     $(".play-bottom")
       
   811     .mousedown(function(_e) {
       
   812     	_e.preventDefault();
       
   813     })
       
   814     .on("touchstart", function(_e) {
       
   815     	_e.preventDefault();
       
   816     })
       
   817     .mousewheel(function(_event, _scrolldelta) {
   737     	totalScroll += _scrolldelta;
   818     	totalScroll += _scrolldelta;
   738 	    if (Math.abs(totalScroll) >= 1) {
   819 	    if (Math.abs(totalScroll) >= 1) {
   739 	    	var d = (totalScroll > 0 ? 1 : -1),
   820 	    	var d = (totalScroll > 0 ? 1 : -1),
   740 	    		newlevel = Math.max(0, Math.min(zoomlevels.length - 1, currentlevel + d));
   821 	    		newlevel = Math.max(0, Math.min(zoomlevels.length - 1, currentlevel + d));
   741     		if (newlevel !== currentlevel) {
   822     		if (newlevel !== currentlevel) {
   744     			localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, localpos));
   825     			localpos = Math.max(localduration / 2, Math.min(data.duration - localduration / 2, localpos));
   745     			throttledShowLocal();
   826     			throttledShowLocal();
   746     		}
   827     		}
   747 	        totalScroll = 0;
   828 	        totalScroll = 0;
   748 	    }
   829 	    }
       
   830 	    _event.preventDefault();
   749     });
   831     });
   750     
   832     
   751     if (pageParams.keywords && pageParams.keywords.length) {
   833     if (pageParams.keywords && pageParams.keywords.length) {
   752     	$(".keyword-search a").removeClass("placeholder").text(pageParams.keywords.join(", "));
   834     	$(".keyword-search a").removeClass("placeholder").text(pageParams.keywords.join(", "));
   753     }
   835     }
   754     
   836     
       
   837     checkOrGoNext();
   755 }
   838 }
   756 
   839 
   757 var data = { duration: 10200, topics: [] },
   840 var data = { duration: 10200, topics: [] },
   758     colorset = ["#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#A65628", "#F781BF"];
   841     colorset = ["#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#A65628", "#F781BF"];
   759 
   842