hjz.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //window.tlsj && window.tlsj.invokeDanmuSwitch && window.tlsj.invokeDanmuSwitch();
  2. window.turn_dm = function(state, _tag){
  3. if(window._re){
  4. _tag ? Zepto('#dm-switch').css('display', 'none') : Zepto('#dm-switch').css('display', 'block');
  5. switch(state){
  6. case 'on':
  7. window._re._open();
  8. break;
  9. case 'off':
  10. window._re._close();
  11. break;
  12. case 'pause':
  13. window._re._pause();
  14. break;
  15. case 'start':
  16. window._re._start();
  17. break;
  18. default:
  19. console.log('arguments error');
  20. }
  21. }else{
  22. if(window.localStorage){
  23. if(state === 'on'){
  24. localStorage.dmState = 'on';
  25. }else if(state === 'off'){
  26. localStorage.dmState = 'off';
  27. }
  28. }
  29. }
  30. }
  31. var DMRE = function(mgr, is_full_control){
  32. this.manager = mgr;
  33. this.DISPLAY_TIME = 1500;
  34. this.ANIM_TIME = 600;
  35. this.FADE_OUT_TIME = 300; // fade-out的速度
  36. this.itemMargin = 10; // 下边距
  37. this.timer = null;
  38. this.itemOpacity = 0;
  39. this.is_full_control = is_full_control;
  40. this.dmItemClass = '.dm-item';
  41. this._init();
  42. this.PIVOT_FOR_START_GETTING_NEW_DMS = 4;
  43. }
  44. DMRE.prototype._getUrlParam = function(name){
  45. var _reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  46. var _target = window.location.search.substr(1).match(_reg);
  47. // if(_target != null) return unescape(_target[2]);
  48. if(_target != null) return true;
  49. return false;
  50. }
  51. DMRE.prototype._init = function(){
  52. var that = this;
  53. this.dmWrap = Zepto('#dm');
  54. this.dmWrapHeight = Zepto('#dm').height();
  55. this.dmSwitch = Zepto('#dm-switch');
  56. this.dmSwitchIcon = Zepto('#dm-switch .dm-switch-icon');
  57. this._ua = window.navigator.userAgent.toLowerCase();
  58. if(!/webkit/i.test(this._ua)){
  59. Zepto(this.dmSwitch).css('display', 'none');
  60. Zepto(this.dmWrap).css('display', 'none');
  61. //alert('弹幕暂不支持此终端');
  62. return;
  63. }
  64. this._isDown = this._getUrlParam('down');
  65. Zepto(this.dmWrap).empty();
  66. typeof this.is_full_control == 'boolean' && this.is_full_control ? Zepto(this.dmSwitch).css('display', 'none') : Zepto(this.dmSwitch).css('display', 'block');
  67. if(window.localStorage && !localStorage.dmState){
  68. localStorage.dmState = 'on'; // 初始化弹幕状态,默认打开,关闭为off
  69. this.manager.emit('dm_on');
  70. }
  71. Zepto(this.dmSwitch).bind('click', function(){
  72. if(localStorage.dmState === 'on'){
  73. that._close();
  74. tips('弹幕已关闭');
  75. }else{
  76. tips('弹幕已开启');
  77. that._open();
  78. }
  79. });
  80. // if(/android/i.test(this._ua)){
  81. // this.itemOpacity = 1;
  82. // if(localStorage.dmState === 'on'){
  83. // Zepto(this.dmSwitchIcon).css('background-position', '-20px 0');
  84. // }
  85. // }else{
  86. if(localStorage.dmState === 'on'){
  87. Zepto(this.dmSwitchIcon).addClass('open');
  88. }
  89. // }
  90. if(localStorage.dmState === 'off'){
  91. if(Math.floor(Math.random()*11) > 8){
  92. var t_html = '<div class="dm-item" style="top: ' + this.dmWrapHeight + 'px; opacity: ' + this.itemOpacity + '"><div class="avatar"><img src="http://qzapp.qlogo.cn/qzapp/100560706/D18F2CCE0413E7AA5C559BBC0C31AF7F/30"></div><p class="txt">鹿棣说,开启弹幕更加有趣!</p></div>';
  93. }
  94. Zepto(this.dmWrap).html(t_html);
  95. var tip_item = Zepto(this.dmWrap).find(this.dmItemClass).first();
  96. Zepto(tip_item).animate({
  97. 'opacity': '1',
  98. '-webkit-transform': 'translateY(-92px)'
  99. }, that.ANIM_TIME, 'ease-out', function(){
  100. window.setTimeout(function(){
  101. Zepto(tip_item).animate({
  102. 'opacity': '0',
  103. '-webkit-transform': 'translateY(-160px)'
  104. }, that.ANIM_TIME, 'ease-out', function(){
  105. Zepto(tip_item).remove();
  106. });
  107. }, that.DISPLAY_TIME);
  108. });
  109. }
  110. }
  111. DMRE.prototype.log = function(text){
  112. jlog('[DMRE] ' + text);
  113. }
  114. DMRE.prototype._cutStr = function(str, len){
  115. var str_length = 0;
  116. var str_len = 0;
  117. str_cut = new String();
  118. str_len = str.length;
  119. for(var i = 0; i < str_len; i++){
  120. a = str.charAt(i);
  121. str_length++;
  122. if(escape(a).length > 4){
  123. str_length++;
  124. }
  125. str_cut = str_cut.concat(a);
  126. if(str_length >= len){
  127. str_cut = str_cut.concat("...");
  128. return str_cut;
  129. }
  130. }
  131. if(str_length < len){
  132. return str;
  133. }
  134. }
  135. DMRE.prototype._getDM = function(){
  136. var obj = this.manager.readSync();
  137. if(!obj){
  138. this.log('get_dm, get null value');
  139. return null;
  140. }
  141. var dm = obj.dm;
  142. if(!dm){
  143. this.log('get_dm, dm is null');
  144. return null;
  145. }
  146. // 如果队列中的dm数量少于阈值,通知this.manager开始获取下一批数据
  147. if(obj.len && obj.len < this.PIVOT_FOR_START_GETTING_NEW_DMS){
  148. this.log('get_dm, EVENT --> no_more_data');
  149. this.manager.emit('no_more_data');
  150. }
  151. return dm;
  152. }
  153. DMRE.prototype._cutStr = function(str, len){
  154. var str_length = 0;
  155. var str_len = 0;
  156. str_cut = new String();
  157. str_len = str.length;
  158. for(var i = 0; i < str_len; i++){
  159. a = str.charAt(i);
  160. str_length++;
  161. if(escape(a).length > 4){
  162. str_length++;
  163. }
  164. str_cut = str_cut.concat(a);
  165. if(str_length >= len){
  166. str_cut = str_cut.concat("...");
  167. return str_cut;
  168. }
  169. }
  170. if(str_length < len){
  171. return str;
  172. }
  173. }
  174. DMRE.prototype._renderDM = function(dm){
  175. if(!dm){
  176. return null;
  177. }
  178. var html = [];
  179. if(dm.type && dm.type == 'realtime'){ //是否为实时
  180. html.push('<div onclick=\'ga("send", "event", "dm", "click", "dm-item")\' class="dm-item new" style="top: ' + this.dmWrapHeight + 'px; opacity: ' + this.itemOpacity + '">');
  181. }else{
  182. html.push('<a data-id="'+dm.id+'" data-nickname="'+dm.nickname+'" class="dm-item" style="top: ' + this.dmWrapHeight + 'px; opacity: ' + this.itemOpacity + '" tapmode="">');
  183. }
  184. /*if(!this._isDown){
  185. html.push('<a class="dm-wrap" href="http://news.jiecao.fm/art_cmt.htm?id=' + dm.id + '&amp;uid1=' + (dm.member_id || "") + '&amp;nickname=' + encodeURIComponent(dm.nickname) + '&amp;cmt_id=' + dm.id + '">');
  186. }*/
  187. if(dm.isRe){ //是否有@
  188. html.push('<div class="avatar"><img src="' + dm.avatar + '" cc-username="'+ dm.nickname +'"><span>@</span><img src="' + dm.re_avatar + '" cc-username="'+ dm.re_nickname +'"></div>');
  189. }else{
  190. html.push('<div class="avatar"><img src="' + dm.avatar + '" cc-username="'+ dm.nickname +'"></div>');
  191. }
  192. html.push('<p class="txt">' + this._cutStr(dm.content, 80) + '</p>');
  193. /*if(!this._isDown){
  194. html.push('</a>');
  195. }*/
  196. html.push('</a>');
  197. return html.join('');
  198. }
  199. DMRE.prototype._load = function(){
  200. var _html = this._renderDM(this._getDM());
  201. if(_html != null){
  202. return _html;
  203. }else{
  204. return null;
  205. }
  206. }
  207. DMRE.prototype._start = function(){
  208. if(localStorage.dmState === 'off'){
  209. return;
  210. }
  211. var that = this;
  212. var _html = this._load();
  213. if(_html != null){
  214. Zepto(this.dmWrap).append(_html);
  215. this._anmite(false);
  216. }else{
  217. this._anmite(true);
  218. }
  219. this.timer = window.setTimeout(function(){
  220. that._start();
  221. }, that.DISPLAY_TIME);
  222. // if(/android/i.test(this._ua)){
  223. // Zepto(this.dmSwitchIcon).css('background-position', '-20px 0');
  224. // }else{
  225. Zepto(this.dmSwitchIcon).addClass('open');
  226. // }
  227. }
  228. DMRE.prototype._pause = function(){
  229. clearTimeout(this.timer);
  230. // if(/android/i.test(this._ua)){
  231. // Zepto(this.dmSwitchIcon).css('background-position', '0 0');
  232. // }else{
  233. Zepto(this.dmSwitchIcon).removeClass('open');
  234. // }
  235. }
  236. DMRE.prototype._close = function(){
  237. clearTimeout(this.timer);
  238. var dmWrap = Zepto(this.dmWrap);
  239. dmWrap.animate({ 'opacity': 0 }, this.ANIM_TIME, 'ease-out', function(){
  240. dmWrap[0].style.display="none";
  241. });
  242. // if(/android/i.test(this._ua)){
  243. // Zepto(this.dmSwitchIcon).css('background-position', '0 0');
  244. // }else{
  245. Zepto(this.dmSwitchIcon).removeClass('open');
  246. // }
  247. localStorage.dmState = 'off';
  248. this.manager.emit('dm_off');
  249. }
  250. DMRE.prototype._open = function(){
  251. clearTimeout(this.timer);
  252. var that = this,
  253. dmWrap = Zepto(this.dmWrap);
  254. dmWrap.animate({ 'opacity': 1}, this.ANIM_TIME, 'ease-out', function(){
  255. dmWrap[0].style.display='';
  256. that._start();
  257. });
  258. // if(/android/i.test(this._ua)){
  259. // Zepto(this.dmSwitchIcon).css('background-position', '-20px 0');
  260. // }else{
  261. Zepto(this.dmSwitchIcon).addClass('open');
  262. // }
  263. localStorage.dmState = 'on';
  264. this.manager.emit('dm_on');
  265. }
  266. DMRE.prototype._getTranslateY = function(item){
  267. return parseInt(Zepto(item).css('-webkit-transform').match(/\-?[0-9]+/g)[0]);
  268. }
  269. DMRE.prototype._anmite = function(isEnd){
  270. var that = this;
  271. var dmItems = Zepto(this.dmWrap).find(this.dmItemClass);
  272. var newItemHeight = Zepto(dmItems).last().height();
  273. var oldItemHeight = Zepto(dmItems).first().height();
  274. if(isEnd){
  275. Zepto(dmItems).each(function(index, item){
  276. if(that.dmWrapHeight + that._getTranslateY(this) <= oldItemHeight){
  277. Zepto(this).animate({
  278. 'opacity': '0'
  279. }, that.FADE_OUT_TIME, 'ease-out', function(){
  280. Zepto(this).remove();
  281. // if(Zepto(dmItems).size() - 1 == 0){
  282. // clearTimeout(that.timer);
  283. // console.log('clearTimeout');
  284. // }
  285. });
  286. }else{
  287. Zepto(this).animate({
  288. '-webkit-transform': 'translateY(' + (that._getTranslateY(this) - oldItemHeight - that.itemMargin) + 'px)'
  289. }, that.ANIM_TIME, 'ease-out');
  290. }
  291. });
  292. }else{
  293. Zepto(dmItems).each(function(index, item){
  294. if(index == Zepto(dmItems).size() - 1){
  295. Zepto(this).animate({
  296. 'opacity': '1',
  297. '-webkit-transform': 'translateY(' + (-newItemHeight-that.itemMargin) + 'px)'
  298. }, that.ANIM_TIME, 'ease-out');
  299. }else{
  300. if(that.dmWrapHeight + that._getTranslateY(this) <= newItemHeight){
  301. Zepto(this).animate({
  302. 'opacity': '0'
  303. }, that.FADE_OUT_TIME, 'ease-out', function(){
  304. Zepto(this).remove();
  305. });
  306. }else{
  307. Zepto(this).animate({
  308. '-webkit-transform': 'translateY(' + (that._getTranslateY(this) - newItemHeight - that.itemMargin) + 'px)'
  309. }, that.ANIM_TIME, 'ease-out');
  310. }
  311. }
  312. });
  313. }
  314. }
  315. jlog('DMRE loaded');