String.tmpl = function(str, obj, st) { return str.replace(/\{([\w_$]+)\}/g, function(c, $1) { var a = obj[$1]; if (a === undefined || a === null) { if (st === undefined || st === null) { return ''; } switch (st) { case 0: return ''; case 1: return $1; default: return c; } } if ('[object Function]' == Object.prototype.toString.call(a)) { a = a($1); } return a; }); }; /* DataCollection */ DataCollection = function() { this.map = {}; this.keys = []; this.length = 0; }; DataCollection.prototype = { get: function(key) { return this.map[key]; }, getAt: function(index) { return this.map[this.keys[index]]; }, add: function(key, Value) { if (typeof this.map[key] == 'undefined') { this.keys.push(key); } this.map[key] = Value; this.length = this.keys.length; }, remove: function(key) { var obj; var i; for (i = 0; i < this.keys.length; i++) { if (this.keys[i] == key) { obj = this.map[key]; this.map[key] = null; delete this.map[key]; this.keys.splice(i, 1); this.length = this.keys.length; return obj; } } return null; }, removeAt: function(index) { var obj; if (index < this.keys.length) { obj = this.map[this.keys[index]]; delete this.map[this.keys[index]]; this.keys.splice(index, 1); this.length = this.keys.length; return obj; } return null; }, clear: function(key) { this.length = this.keys.length = 0; this.map = {}; this.valuetype = {}; return this; } }; var dataCollection = new DataCollection(); // 将时间转换成秒数 function localString2Second(sTime) { var temp = sTime.split(":"); var returnSeconds = 0; if(temp.length==3){ returnSeconds = parseInt((temp[0] * 3600), 10) + parseInt((temp[1] * 60), 10) + parseInt(temp[2], 10); }else if(temp.length==2){ returnSeconds = parseInt((temp[0] * 60), 10) + parseInt(temp[1], 10); }else if(temp.length==1){ returnSeconds = parseInt(temp[0], 10); } return (returnSeconds); } // 将秒数转换成时间 function second2LocalString(iTime) { var t, h, m, s, timeStr; t = Math.floor(iTime); h = Math.floor(t / 3600); t = t - h * 3600; m = Math.floor(t / 60); t = t % 60; s = Math.floor(t); if (h === 0) { timeStr = (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s); } else { timeStr = (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s); } return timeStr; } var lectureSections=[];//所有的 LectureSection 对象 var selecedSection; // 清除讲义区块的选中样式 function clearLectureBlockStyle(){ selecedSection && selecedSection.unselect(true); } function goTargetTime(startTime) { var currentSeconds = startTime; if(parent.playerObj.getDuration() < 1){ return; } parent.playerObj.setCurrentTime(currentSeconds); } // 视频同步讲义 function goTargetLectureCon(playerMediaTime) { var currentPos; if(playerMediaTime){ currentPos=playerMediaTime; }else{ currentPos=parent.playerObj.getCurrentTime(); } if(selecedSection){ if(currentPos >= selecedSection.startTime && currentPos <= selecedSection.endTime){ return; }else if(currentPos < selecedSection.startTime -10 || currentPos > selecedSection.endTime){//发现flash的跳转不是十分准确,要允许一些误差 selecedSection.unselect(); } } for(var i=0; i= section.startTime && currentPos <= section.endTime){ section.select(true); }else if(currentPos < section.startTime -10 || currentPos > section.endTime){ section.unselect(); } } if(selecedSection){ //ifScrollIntoView(selecedSection.$startNode); } } function ifScrollIntoView($el){ if(!$el[0]){ return; } var t=$el.offset().top; var st = Math.max(document.documentElement.scrollTop, document.body.scrollTop); var h=$(window).height(); if(tst+h/2){ $el[0].scrollIntoView(); } } var LectureSection=function($startMarker,$endMarker){ var me=this; this.$startMarker=$startMarker; this.$endMarker=$endMarker; this.startTime=parseFloat(this.$startMarker.attr('time')); this.endTime=parseFloat(this.$endMarker.attr('time')); var $startParents=$startMarker.parents(); var $endParents=$endMarker.parents(); var $startParent=this.$startNode=$startMarker.parent(); var $endParent=this.$endNode=$endMarker.parent(); var $sectionNode; if($startParent[0] == $endParent[0]){ $sectionNode=$startParent; }else{ var $startNexts=$startParent.nextUntil($endParents); var $endPrevs=$endParent.prevUntil($startParents); var nodes $sectionNode=$startParent.add($startNexts).add($endPrevs).add($endParent); } this.$sectionNode=$sectionNode; this.initDom(); this.buttons=[]; this.createToolbar(); this.createButton('视频跳转至本节开始',function(){ me.seek2me(); }); } var str_dbclick='双击视频跳转至本节开始' var str_current='正在播放本节内容' LectureSection.prototype={ toolbarTpl:'
', buttonTpl:'{text}', createToolbar:function(){ this.$toolbar=$(this.toolbarTpl); this.$toolbar.prependTo(this.$wrap); return this.$toolbar; }, createButton:function(text,func){ var $button=$(String.tmpl(this.buttonTpl,{'text':text})); $button.appendTo(this.$toolbar.children()); if(func){ $button.on('click',func) } this.buttons.push($button); }, initDom:function(){ var me=this; var title='时间范围:'+second2LocalString(this.startTime)+'-'+second2LocalString(this.endTime)+' '; title += str_dbclick; var $wrap= this.$wrap=$('
').insertBefore(this.$startNode); $wrap.append(this.$sectionNode); $wrap.hover( function(){ me.highlight(); }, function(){ me.unhighlight(); } ); $wrap.on('dblclick',function(){ me.seek2me(); }); }, highlight:function(scrollIntoView){ this.$wrap.addClass('highlight'); }, unhighlight:function(){ this.$wrap.removeClass('highlight'); }, hide:function(){ this.$wrap.hide(); }, show:function(){ this.$wrap.show(); }, select:function(setCurrent){ this.$wrap.show(); this.$wrap.prop('title',this.$wrap.prop('title').replace(str_dbclick,str_current)) if(setCurrent){ selecedSection=this; } }, unselect:function(setCurrent){ this.$wrap.hide(); this.$wrap.prop('title',this.$wrap.prop('title').replace(str_current,str_dbclick)) if(setCurrent){ selecedSection=null; } }, seek2me:function(scrollIntoView){ var me=this; if(selecedSection && selecedSection != me){ selecedSection.unselect(); } me.select(true); if(scrollIntoView){ //ifScrollIntoView(me.$wrap); } goTargetTime(me.startTime+0.5); } } //页面载入时,根据span.marker节点,初始化相应的 LectureSection 对象,并保存在 lectureSections数组 var lectureContentLoaded=function(){ var $markers=$('span.marker'); $markers.each(function(){ var $marker=$(this) var time=this.getAttribute('time'); time=Number(time); $marker.data('time',time); if(time){ dataCollection.add(time,$marker); } }); //dataCollection.keys.sort(function(a,b){return a-b}); //console.log(dataCollection.keys) //console.log(dataCollection.map) for(i=0;i