index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. Component({
  3. options: {
  4. addGlobalClass: true,
  5. pureDataPattern: /^_/
  6. },
  7. properties: {
  8. duration: {
  9. type: Number,
  10. value: 500
  11. },
  12. easingFunction: {
  13. type: String,
  14. value: 'default'
  15. },
  16. loop: {
  17. type: Boolean,
  18. value: true
  19. },
  20. videoList: {
  21. type: Array,
  22. value: [],
  23. observer: function observer() {
  24. var newVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  25. this._videoListChanged(newVal);
  26. }
  27. },
  28. nextMargin: {
  29. type: String,
  30. value: '400rpx'
  31. }
  32. },
  33. data: {
  34. nextQueue: [],
  35. prevQueue: [],
  36. curQueue: [],
  37. circular: true,
  38. // nextMargin: '400rpx',
  39. _last: 1,
  40. _change: -1,
  41. _invalidUp: 0,
  42. _invalidDown: 0,
  43. _videoContexts: []
  44. },
  45. lifetimes: {
  46. attached: function attached() {
  47. this.data._videoContexts = [wx.createVideoContext('video_0', this), wx.createVideoContext('video_1', this), wx.createVideoContext('video_2', this)];
  48. }
  49. },
  50. methods: {
  51. _videoListChanged: function _videoListChanged(newVal) {
  52. var _this = this;
  53. var data = this.data;
  54. newVal.forEach(function (item) {
  55. data.nextQueue.push(item);
  56. });
  57. if (data.curQueue.length === 0) {
  58. this.setData({
  59. curQueue: data.nextQueue.splice(0, 3)
  60. }, function () {
  61. _this.playCurrent(1);
  62. });
  63. }
  64. },
  65. animationfinish: function animationfinish(e) {
  66. var _data = this.data,
  67. _last = _data._last,
  68. _change = _data._change,
  69. curQueue = _data.curQueue,
  70. prevQueue = _data.prevQueue,
  71. nextQueue = _data.nextQueue;
  72. var current = e.detail.current;
  73. var diff = current - _last;
  74. if (diff === 0) return;
  75. this.data._last = current;
  76. this.playCurrent(current);
  77. this.triggerEvent('change', {
  78. activeId: curQueue[current].id
  79. });
  80. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  81. if (direction === 'up') {
  82. if (this.data._invalidDown === 0) {
  83. var change = (_change + 1) % 3;
  84. var add = nextQueue.shift();
  85. var remove = curQueue[change];
  86. if (add) {
  87. prevQueue.push(remove);
  88. curQueue[change] = add;
  89. this.data._change = change;
  90. } else {
  91. this.data._invalidUp += 1;
  92. }
  93. } else {
  94. this.data._invalidDown -= 1;
  95. }
  96. }
  97. if (direction === 'down') {
  98. if (this.data._invalidUp === 0) {
  99. var _change2 = _change;
  100. var _remove = curQueue[_change2];
  101. var _add = prevQueue.pop();
  102. if (_add) {
  103. curQueue[_change2] = _add;
  104. nextQueue.unshift(_remove);
  105. this.data._change = (_change2 - 1 + 3) % 3;
  106. } else {
  107. this.data._invalidDown += 1;
  108. }
  109. } else {
  110. this.data._invalidUp -= 1;
  111. }
  112. }
  113. var circular = true;
  114. if (nextQueue.length === 0 && current !== 0) {
  115. circular = false;
  116. }
  117. if (prevQueue.length === 0 && current !== 2) {
  118. circular = false;
  119. }
  120. this.setData({
  121. curQueue: curQueue,
  122. circular: circular
  123. });
  124. },
  125. playCurrent: function playCurrent(current) {
  126. return;// 注掉自动播放
  127. this.data._videoContexts.forEach(function (ctx, index) {
  128. index !== current ? ctx.pause() : ctx.play();
  129. });
  130. },
  131. onPlay: function onPlay(e) {
  132. this.trigger(e, 'play');
  133. },
  134. onPause: function onPause(e) {
  135. this.trigger(e, 'pause');
  136. },
  137. onEnded: function onEnded(e) {
  138. this.trigger(e, 'ended');
  139. },
  140. onError: function onError(e) {
  141. this.trigger(e, 'error');
  142. },
  143. onTimeUpdate: function onTimeUpdate(e) {
  144. this.trigger(e, 'timeupdate');
  145. },
  146. onWaiting: function onWaiting(e) {
  147. this.trigger(e, 'wait');
  148. },
  149. onProgress: function onProgress(e) {
  150. this.trigger(e, 'progress');
  151. },
  152. onLoadedMetaData: function onLoadedMetaData(e) {
  153. this.trigger(e, 'loadedmetadata');
  154. },
  155. onTimeUpdate: function onTimeUpdate(e) {
  156. },
  157. openComment: function openComment(e) {
  158. console.log(e)
  159. this.trigger(e, 'openComment')
  160. },
  161. // 点击头部
  162. headTap: function headTap(e){
  163. this.trigger(e,'headTap')
  164. },
  165. // 去朗读
  166. goToReading: function goToReading(e){
  167. this.trigger(e,'goToReading')
  168. },
  169. trigger: function trigger(e, type) {
  170. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  171. // var detail = e.detail;
  172. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  173. this.triggerEvent(type, {activeId: activeId},{bubbles:false});
  174. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  175. activeId: activeId
  176. }), ext)); */
  177. }
  178. }
  179. });