From 9a60d35e1004ffa55b3e261a2afc1ee7f00f8e75 Mon Sep 17 00:00:00 2001 From: smohan Date: Sun, 5 Feb 2017 22:13:55 +0800 Subject: [PATCH] update --- build/smusic.min.js | 2 +- src/js/smusic.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/build/smusic.min.js b/build/smusic.min.js index 6961e8b..a92776a 100644 --- a/build/smusic.min.js +++ b/build/smusic.min.js @@ -5,4 +5,4 @@ * https://smohan.im/lab/smusic * https://github.com/S-mohan/smusic */ -function _classCallCheck(s,t){if(!(s instanceof t))throw new TypeError("Cannot call a class as a function")}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(s){return typeof s}:function(s){return s&&"function"==typeof Symbol&&s.constructor===Symbol&&s!==Symbol.prototype?"symbol":typeof s};!function(){"use strict";var s=Array.prototype,t=function(s){return s?s.trim().replace(/\s+/,",").split(","):[]},i=function(s){return s.ownerDocument.defaultView.getComputedStyle(s,null)},e={contains:function(s,t){return s.classList?s.classList.contains(t):new RegExp("(^| )"+t+"( |$)","gi").test(s.className)},add:function(s,i){t(i).forEach(function(t){t=t.trim(),t&&!e.contains(s,t)&&(s.classList?s.classList.add(t):(s.className+=" "+t,s.className=s.className.trim()))})},remove:function(s,i){t(i).forEach(function(t){t=t.trim(),t&&e.contains(s,t)&&(s.classList?s.classList.remove(t):(s.className=s.className.replace(new RegExp("(^|\\b)"+t.split(" ").join("|")+"(\\b|$)","gi")," "),s.className=s.className.trim()))})},toggle:function(s,i){t(i).forEach(function(t){return s.classList?s.classList.toggle(t):e.contains(s,t)?e.remove(s,t):e.add(s,t)})}},n=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};s=Object(s);for(var t=1,i=arguments.length;t0&&void 0!==arguments[0]?arguments[0]:"*",i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document;if("string"==typeof t){t=t.trim();var e=[];return a.id.test(t)?(e=document.getElementById(RegExp.$1),e=e?[e]:[]):e=a.className.test(t)?i.getElementsByClassName(RegExp.$1):a.tagName.test(t)?i.getElementsByTagName(t):i.querySelectorAll(t),s.slice.call(e)}return[]},o=function(){var s=Element.prototype,t=s.matches||s.matchesSelector||s.mozMatchesSelector||s.msMatchesSelector||s.oMatchesSelector||s.webkitMatchesSelector;return function(s,i){return t.call(s,i)}}(),r=function(s,t){if("object"===("undefined"==typeof s?"undefined":_typeof(s))&&"function"==typeof t)if(Array.isArray(s))for(var i=0,e=s.length;i0&&void 0!==arguments[0]?arguments[0]:{},t=s.url||window.location.href,i=s.done||null,e=s.fail||null,n=s.data,a=d(n),l=s.type&&/^(get|post)$/i.test(s.type)?s.type.toUpperCase():"GET",o=new XMLHttpRequest,r=s.headers||{};r.accept="application/json, text/javascript","POST"===l?r["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8":(r["Content-Type"]="application/json; charset=UTF-8",t=t.indexOf("?")>-1?t+a:t,a=void 0),o.open(l,t,!0);for(var c in r)o.setRequestHeader(c,r[c]);o.onload=function(){if(o.status>=200&&o.status<400){var s=o.responseText;try{s=JSON.parse(s)}catch(s){}i&&i.call(null,s,o)}else e&&e.call(o.status,"error",o)},o.send(a)},h={$:function(s,t){return l(s,t)[0]},$$:l,matchSelector:o,addClass:e.add,removeClass:e.remove,hasClass:e.contains,toggleClass:e.toggle,getStyles:i,assign:Object.assign||n,each:r,bind:c,unbind:u,trigger:m,http:p};window.MoUtils=h}(),function(){"use strict";var s=document,t=window,i=MoUtils,e="ontouchend"in document,n="2.1.0",a="https://smohan.im/lab/smusic",l="//demo.smohan.net/static/img/smusic.jpg",o=[i.$,i.$$,i.bind,function(){}],r=o[0],c=o[1],u=o[2],m=o[3],d=e?"touchstart":"mousedown",p=e?"touchmove":"mousemove",h=e?"touchend":"mouseup",v=function(e,n){var a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"horizontal",l=arguments.length>3&&void 0!==arguments[3]?arguments[3]:m,o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:m,c=[e,r(n,e)],v=c[0],f=c[1],y={width:v.offsetWidth,height:v.offsetHeight};u(f,"click",function(s){return s.stopPropagation()}),u(f,d,function(e){e=e||t.event,e.stopPropagation();var n=void 0,r=void 0;n={x:e.clientX||e.touches[0].clientX,y:e.clientY||e.touches[0].clientY},r={x:this.offsetLeft,y:this.offsetTop};var c="horizontal"==a?y.width:y.height,m=function(s){s=s||t.event,s.stopPropagation();var i=s.clientX||s.touches[0].clientX,e=s.clientY||s.touches[0].clientY,o=Math.min(c,Math.max(0,"horizontal"==a?r.x+(i-n.x):r.y+(e-n.y)));l&&"function"==typeof l&&l(o)};u(s,p,m),u(s,h,function(t){o&&"function"==typeof o&&o(),i.unbind(s,p,m)})})},f={container:s.body,playIndex:0,playMode:1,volume:.5,autoPlay:!0},y=1,g=function(s){throw new Error("Smusic Error:"+s)},b=function(s){return t.console.log("Smusic Log:"+s)},j=function(s){var t=void 0,i=void 0,e=void 0,n="";return t=String(parseInt(s/3600,10)),i=String(parseInt(s%3600/60,10)),e=String(parseInt(s%60,10)),"0"!=t&&(1==t.length&&(t="0"+t),n+=t+":"),1==i.length&&(i="0"+i),n+=i+":",1==e.length&&(e="0"+e),n+=e},x='\n
\n
\n
\n
\n Smusic\n smohan\n
\n
\n
\n '+a+'\n
\n \n
\n \n \n \n
\n \n
\n
\n
\n
\n
    \n
    \n
    \n
      \n
      \n
      \n
      \n
      \n',C={},M=/\[\d*:\d*((\.|:)\d*)*]/g,w=function(s){var t=s.replace(/(\[\d*:\d*((\.|:)\d*)*])/gm,"\n$1").trim().replace(/\\n/gm,"\n").split("\n"),i={},e=0,n=void 0,a=t.length;for(e;e0&&void 0!==arguments[0]?arguments[0]:s.body,i=y++,e=s.createElement("div");return e.id="smohan-smusic-"+i,e.className="smusic-container",e.setAttribute("data-smusic-version",n),e.setAttribute("data-smusic-homepage",a),e.setAttribute("data-smusic-id",i.toString()),e.innerHTML=x+'',t.appendChild(e),{element:e,id:i}},I=function(){var s=this.smusic;this.dom={scroll:{title:r(".js-smusic-scroll--title",s),panel:r(".js-smusic-scroll--panel",s),lyric:r(".js-smusic-scroll--lyric",this.panel),list:r(".js-smusic-scroll--list",this.panel)},song:{title:r(".js-smusic-song--title",s),singer:r(".js-smusic-song--singer",s),thumbnail:r(".js-smusic-song--thumbnail",s)},btn:{prev:r(".js-smusic-btn--prev",s),play:r(".js-smusic-btn--play",s),next:r(".js-smusic-btn--next",s),volume:r(".js-smusic-btn--volume",s),mode:r(".js-smusic-btn--mode",s),lyric:r(".js-smusic-btn--lyric",s),list:r(".js-smusic-btn--list",s)},time:r(".js-smusic-time",s),progress:{bar:r(".js-smusic-progress",s),buffer:r(".js-smusic-progress--buffer",this.bar),value:r(".js-smusic-progress--value",this.bar),slider:r(".js-smusic-progress--slider",this.bar)},volume:{bar:r(".js-smusic-volume--bar",s),value:r(".js-smusic-volume--value",this.bar),slider:r(".js-smusic-volume--slider",this.bar)}}},E=function(){var s=this,t=s.dom.scroll.list,i=s.playList,e="";i.forEach(function(t,i){var n=i===s.playIndex?" active":"";e+='
    • \n\n'+t.title+'\n'+t.singer+"\n
    • "}),t.innerHTML=e},T=function(){var s=this,t=30,i=s.dom.scroll.lyric.parentNode.offsetHeight,e="",n=0;L(this.playList,this.playIndex,function(l){if("number"==typeof l){switch(l){case 0:e+='
    • 暂无歌词
    • ';break;case-1:e+='
    • 歌词解析失败
    • ';break;case-2:e+='
    • 歌词加载失败
    • '}s.dom.scroll.lyric.innerHTML=e}else{for(var o in l){var r=t*n,c=r>=i/2-t,u=l[o]||a;"string"!=typeof u&&(u=u.content),l[o]={index:n,content:u,top:r,scrollTop:c?r-(i/2-t):0,toScroll:c},n++,e+="
    • "+u+"
    • "}s.dom.scroll.lyric.innerHTML=e+('
    • '+a+"
    • ")}})},k={},A=function(){var s=[this.dom,this.audio,this.smusicId],t=s[0],i=s[1],e=s[2];k[e]&&clearInterval(k[e]);var n=parseFloat(t.progress.bar.offsetWidth);if(!isNaN(i.duration)){var a=j(i.duration);t.time.textContent="00:00/"+a}k[e]=setInterval(function(){var s=i.buffered.length;if(s>0&&void 0!=i.buffered){var a=i.buffered.end(s-1)/i.duration*n;t.progress.buffer.style.width=a+"px",Math.abs(i.duration-i.buffered.end(s-1))<1&&(t.progress.buffer.style.width=n+"px",clearInterval(k[e]))}},1e3)},$=function(){var s=[this,this.dom,this.audio],t=s[0],e=s[1],n=s[2],a=Math.round(e.progress.bar.offsetWidth),l=Math.round(e.volume.bar.offsetHeight);u(t.smusic,"click",".js-smusic-panel--tab",function(s){if(s.stopPropagation(),i.hasClass(this,"active"))return!1;var t=this.getAttribute("data-panel");i.addClass(this,"active"),"list"===t?(i.removeClass(e.btn.lyric,"active"),i.addClass(e.scroll.panel,"show-list")):(i.removeClass(e.btn.list,"active"),i.removeClass(e.scroll.panel,"show-list"))}),u(n,"timeupdate",function(){if(!isNaN(n.duration)){var s=j(n.currentTime),l=j(n.duration),o=n.currentTime/n.duration*a;e.time.textContent=s+"/"+l;var r=Math.min(o,a);e.progress.value.style.width=r+"px";var u="_smusic_lyric_"+t.playIndex,m=C[u];if(!m)return;var d=Math.round(n.currentTime),p=m[d];if(!p||"number"==typeof p)return;var h=p.index,v=p.toScroll,f=c("li",e.scroll.lyric)[h],y=c("li.active",e.scroll.lyric);y.forEach(function(s){s&&i.removeClass(s,"active")}),f&&i.addClass(f,"active"),e.scroll.lyric.style.transform=v?"translate3d(0, -"+p.scrollTop+"px, 0)":"translate3d(0, 0, 0)"}}),u(e.btn.mode,"click",function(){var s=this.getAttribute("data-play-mode"),i=void 0;switch(Number(s)){case 1:i=2;break;case 2:i=3;break;case 3:i=1}t.setMode(i)}),u(e.btn.play,"click",function(){var s=i.hasClass(this,"smusic-music-play");s?t.pause():t.play()}),u(n,"ended",function(){return t.playByMode("ended")}),u(e.btn.prev,"click",function(){return t.prev()}),u(e.btn.next,"click",function(){return t.next()}),v(e.progress.bar,".js-smusic-progress--slider","horizontal",function(s){var t=Math.max(0,Math.min(s/a,a));n.currentTime&&n.duration&&(n.currentTime=Math.round(t*n.duration))}),u(e.progress.bar,"click",function(s){s.stopPropagation();var i=this.getBoundingClientRect(),e=Math.min(a,Math.abs(s.clientX-i.left))/a;n.currentTime&&n.duration&&(n.currentTime=Math.round(e*n.duration),t.play())}),v(e.volume.bar,".js-smusic-volume--slider","vertical",function(s){var i=Number((l-s)/l);t.setVolume(i)}),u(e.btn.volume,"click",function(s){n.muted?(i.removeClass(this,"smusic-volume--mute"),this.setAttribute("title","静音"),n.muted=!1):(i.addClass(this,"smusic-volume--mute"),this.setAttribute("title","取消静音"),n.muted=!0)}),u(e.scroll.list,"click",".js-smusic-song--item",function(s){console.log(this);var e=this.getAttribute("data-song-index");i.hasClass(this,"active")?t.play():H.call(t,e)})},H=function(s,t,e){var n=[this.dom,this.audio,this.playList.length],a=n[0],o=n[1],r=n[2];s>=r-1&&(s=r-1),s<=0&&(s=0),this.playIndex=s;var u=this.playList[this.playIndex];if(!u)return b("没有要播放的歌曲"),!1;var m=function(){return A.call(this)}.bind(this);o.removeEventListener("canplay",m,!1),k[this.smusicId]&&clearInterval(k[this.smusicId]),a.progress.buffer.style.width="0px",a.progress.value.style.width="0px",a.time.textContent="00:00/00:00",a.scroll.lyric.style.transform="translate3d(0, 0, 0)",a.scroll.lyric.innerHTML='
    • 正在加载歌词...
    • ',c(".js-smusic-song--item.active",a.scroll.list).forEach(function(s){s&&i.removeClass(s,"active, pause")});var d=c(".js-smusic-song--item",a.scroll.list)[s];d&&i.addClass(d,"active"),o.src=u.audio,o.load(),o.addEventListener("canplay",m,!1),e?this.config.autoPlay&&o.play():o.play();var p=u.title||"Smusic",h=u.singer||"singer",v=u.thumbnail||l;a.song.title.textContent=p,a.song.singer.textContent=h,a.song.thumbnail.src=v,a.scroll.title.setAttribute("title",h+" - "+p),T.call(this),t&&t.call(this,u)},_=function(){function s(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,s),Array.isArray(t)||g("播放列表必须是一个数组"),this.playList=t,this.audio=null,this.version=n,this.config=i.assign({},f,e);var a=parseInt(this.config.playIndex),l=parseInt(this.config.playMode),o=parseFloat(this.config.volume);a<0&&(a=0),a>this.playList.length-1&&(a=this.playList.length-1),(l<1||l>3)&&(l=1),(o<0||o>1)&&(o=.5),this.playIndex=a,this.playMode=l,this.volume=o}return s.prototype.playByMode=function(s,t,i){var e=[Number(this.playMode),this.playIndex,this.playList.length],n=e[0],a=e[1],l=e[2],o=a;if(s||2==n)switch(n){case 1:"prev"==s?o=a<=l-1&&a>0?a-1:l-1:"next"!=s&&"ended"!=s||(o=a>=l-1?0:a+1);break;case 2:o=S(a,l);break;case 3:o="prev"==s?a<=l-1&&a>0?a-1:l-1:"next"==s?a>=l-1?0:a+1:a}else;H.call(this,o,t,i)},s.prototype.addSong=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:m;return s&&s.audio?(this.playList.push({title:s.title||"歌曲名",singer:s.singer||"歌手名",audio:s.audio,thumbnail:s.thumbnail||l,lyric:""|s.lyric}),this.refreshList(),t&&t()):t&&t("添加失败,参数不是一个对象或者未设置audio属性"),this.playList},s.prototype.refreshList=function(){E.call(this)},s.prototype.next=function(s){this.playByMode("next",s)},s.prototype.prev=function(s){this.playByMode("next",s)},s.prototype.play=function(s){var t=this.dom.btn.play;i.removeClass(t,"smusic-music-pause"),i.addClass(t,"smusic-music-play"),t.setAttribute("title","暂停");var e=r(".js-smusic-song--item.active",this.dom.scroll.list);i.removeClass(e,"pause"),this.playList.length&&this.audio.play(),s&&s.call(this,this.playList[this.playIndex])},s.prototype.pause=function(s){var t=this.dom.btn.play;i.addClass(t,"smusic-music-pause"),i.removeClass(t,"smusic-music-play");var e=r(".js-smusic-song--item.active",this.dom.scroll.list);i.addClass(e,"pause"),t.setAttribute("title","播放"),this.playList.length&&this.audio.pause(),s&&s.call(this,this.playList[this.playIndex])},s.prototype.setVolume=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.5;s=Number(s),s<0&&(s=0),s>1&&(s=1);var t=this.dom.volume.bar.offsetHeight,e=this.dom.volume.slider.offsetHeight,n=t*s,a=Math.min((1-s)*t,t-e);0===s?(i.addClass(this.dom.btn.volume,"smusic-volume--mute"),this.dom.btn.volume.setAttribute("title","取消静音"),this.audio.muted=!0):(i.removeClass(this.dom.btn.volume,"smusic-volume--mute"),this.audio.muted=!1,this.dom.btn.volume.setAttribute("title","静音")),this.dom.volume.value.style.height=n+"px",this.dom.volume.slider.style.top=a+"px",this.volume=s,this.audio.volume=s},s.prototype.setMode=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;s=Number(s);var t=void 0,i=void 0,e=this.dom.btn.mode;switch(s){case 1:default:t="列表循环",i="smusic-mode--loop";break;case 2:t="单曲循环",i="smusic-mode--single";break;case 3:t="随机播放",i="smusic-mode--random"}e.setAttribute("data-play-mode",s.toString()),e.setAttribute("title",t),e.className=e.className.replace(/smusic-mode--\w+/,i),this.playMode=s},s.prototype.init=function(){var s=[this.config,N(this.config.container)],t=s[0],e=s[1];this.smusic=e.element,this.smusicId=e.id,this.audio=this.smusic.getElementsByTagName("audio")[0],I.call(this),E.call(this),$.call(this),this.setVolume(this.volume),this.setMode(this.playMode),this.playList.length?this.playByMode(void 0,function(){t.autoPlay||i.trigger(this.dom.btn.play,"click")},!0):b("歌曲列表为空")},s.prototype.getCurrentInfo=function(){if(this.playList.length){var s=this.playList[this.playIndex];return s.index=this.playIndex,s.volume=this.volume,s.mode=this.playMode,s}return null},s.prototype.destroy=function(){},s}();t.SMusic=function(s,t){return new _(s,t)}}(); \ No newline at end of file +function _classCallCheck(s,t){if(!(s instanceof t))throw new TypeError("Cannot call a class as a function")}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(s){return typeof s}:function(s){return s&&"function"==typeof Symbol&&s.constructor===Symbol&&s!==Symbol.prototype?"symbol":typeof s};!function(){"use strict";var s=Array.prototype,t=function(s){return s?s.trim().replace(/\s+/,",").split(","):[]},i=function(s){return s.ownerDocument.defaultView.getComputedStyle(s,null)},e={contains:function(s,t){return s.classList?s.classList.contains(t):new RegExp("(^| )"+t+"( |$)","gi").test(s.className)},add:function(s,i){t(i).forEach(function(t){t=t.trim(),t&&!e.contains(s,t)&&(s.classList?s.classList.add(t):(s.className+=" "+t,s.className=s.className.trim()))})},remove:function(s,i){t(i).forEach(function(t){t=t.trim(),t&&e.contains(s,t)&&(s.classList?s.classList.remove(t):(s.className=s.className.replace(new RegExp("(^|\\b)"+t.split(" ").join("|")+"(\\b|$)","gi")," "),s.className=s.className.trim()))})},toggle:function(s,i){t(i).forEach(function(t){return s.classList?s.classList.toggle(t):e.contains(s,t)?e.remove(s,t):e.add(s,t)})}},n=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};s=Object(s);for(var t=1,i=arguments.length;t0&&void 0!==arguments[0]?arguments[0]:"*",i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document;if("string"==typeof t){t=t.trim();var e=[];return a.id.test(t)?(e=document.getElementById(RegExp.$1),e=e?[e]:[]):e=a.className.test(t)?i.getElementsByClassName(RegExp.$1):a.tagName.test(t)?i.getElementsByTagName(t):i.querySelectorAll(t),s.slice.call(e)}return[]},o=function(){var s=Element.prototype,t=s.matches||s.matchesSelector||s.mozMatchesSelector||s.msMatchesSelector||s.oMatchesSelector||s.webkitMatchesSelector;return function(s,i){return t.call(s,i)}}(),r=function(s,t){if("object"===("undefined"==typeof s?"undefined":_typeof(s))&&"function"==typeof t)if(Array.isArray(s))for(var i=0,e=s.length;i0&&void 0!==arguments[0]?arguments[0]:{},t=s.url||window.location.href,i=s.done||null,e=s.fail||null,n=s.data,a=d(n),l=s.type&&/^(get|post)$/i.test(s.type)?s.type.toUpperCase():"GET",o=new XMLHttpRequest,r=s.headers||{};r.accept="application/json, text/javascript","POST"===l?r["Content-Type"]="application/x-www-form-urlencoded; charset=UTF-8":(r["Content-Type"]="application/json; charset=UTF-8",t=t.indexOf("?")>-1?t+a:t,a=void 0),o.open(l,t,!0);for(var c in r)o.setRequestHeader(c,r[c]);o.onload=function(){if(o.status>=200&&o.status<400){var s=o.responseText;try{s=JSON.parse(s)}catch(s){}i&&i.call(null,s,o)}else e&&e.call(o.status,"error",o)},o.send(a)},h={$:function(s,t){return l(s,t)[0]},$$:l,matchSelector:o,addClass:e.add,removeClass:e.remove,hasClass:e.contains,toggleClass:e.toggle,getStyles:i,assign:Object.assign||n,each:r,bind:c,unbind:u,trigger:m,http:p};window.MoUtils=h}(),function(){"use strict";var s=document,t=window,i=MoUtils,e="ontouchend"in document,n="2.1.0",a="https://smohan.im/lab/smusic",l="//demo.smohan.net/static/img/smusic.jpg",o=[i.$,i.$$,i.bind,function(){}],r=o[0],c=o[1],u=o[2],m=o[3],d=e?"touchstart":"mousedown",p=e?"touchmove":"mousemove",h=e?"touchend":"mouseup",v=function(e,n){var a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"horizontal",l=arguments.length>3&&void 0!==arguments[3]?arguments[3]:m,o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:m,c=[e,r(n,e)],v=c[0],f=c[1],y={width:v.offsetWidth,height:v.offsetHeight};u(f,"click",function(s){return s.stopPropagation()}),u(f,d,function(e){e=e||t.event,e.stopPropagation();var n=void 0,r=void 0;n={x:e.clientX||e.touches[0].clientX,y:e.clientY||e.touches[0].clientY},r={x:this.offsetLeft,y:this.offsetTop};var c="horizontal"==a?y.width:y.height,m=function(s){s=s||t.event,s.stopPropagation();var i=s.clientX||s.touches[0].clientX,e=s.clientY||s.touches[0].clientY,o=Math.min(c,Math.max(0,"horizontal"==a?r.x+(i-n.x):r.y+(e-n.y)));l&&"function"==typeof l&&l(o)};u(s,p,m),u(s,h,function(t){o&&"function"==typeof o&&o(),i.unbind(s,p,m)})})},f={container:s.body,playIndex:0,playMode:1,volume:.5,autoPlay:!0},y=1,g=function(s){throw new Error("Smusic Error:"+s)},b=function(s){return t.console.log("Smusic Log:"+s)},j=function(s){var t=void 0,i=void 0,e=void 0,n="";return t=String(parseInt(s/3600,10)),i=String(parseInt(s%3600/60,10)),e=String(parseInt(s%60,10)),"0"!=t&&(1==t.length&&(t="0"+t),n+=t+":"),1==i.length&&(i="0"+i),n+=i+":",1==e.length&&(e="0"+e),n+=e},x='\n
      \n
      \n
      \n
      \n Smusic\n smohan\n
      \n
      \n
      \n '+a+'\n
      \n \n
      \n \n \n \n
      \n \n
      \n
      \n
      \n
      \n
        \n
        \n
        \n
          \n
          \n
          \n
          \n
          \n',C={},M=/\[\d*:\d*((\.|:)\d*)*]/g,w=function(s){var t=s.replace(/(\[\d*:\d*((\.|:)\d*)*])/gm,"\n$1").trim().replace(/\\n/gm,"\n").split("\n"),i={},e=0,n=void 0,a=t.length;for(e;e0&&void 0!==arguments[0]?arguments[0]:s.body,i=y++,e=s.createElement("div");return e.id="smohan-smusic-"+i,e.className="smusic-container",e.setAttribute("data-smusic-version",n),e.setAttribute("data-smusic-homepage",a),e.setAttribute("data-smusic-id",i.toString()),e.innerHTML=x+'',t.appendChild(e),{element:e,id:i}},I=function(){var s=this.smusic;this.dom={scroll:{title:r(".js-smusic-scroll--title",s),panel:r(".js-smusic-scroll--panel",s),lyric:r(".js-smusic-scroll--lyric",this.panel),list:r(".js-smusic-scroll--list",this.panel)},song:{title:r(".js-smusic-song--title",s),singer:r(".js-smusic-song--singer",s),thumbnail:r(".js-smusic-song--thumbnail",s)},btn:{prev:r(".js-smusic-btn--prev",s),play:r(".js-smusic-btn--play",s),next:r(".js-smusic-btn--next",s),volume:r(".js-smusic-btn--volume",s),mode:r(".js-smusic-btn--mode",s),lyric:r(".js-smusic-btn--lyric",s),list:r(".js-smusic-btn--list",s)},time:r(".js-smusic-time",s),progress:{bar:r(".js-smusic-progress",s),buffer:r(".js-smusic-progress--buffer",this.bar),value:r(".js-smusic-progress--value",this.bar),slider:r(".js-smusic-progress--slider",this.bar)},volume:{bar:r(".js-smusic-volume--bar",s),value:r(".js-smusic-volume--value",this.bar),slider:r(".js-smusic-volume--slider",this.bar)}}},E=function(){var s=this,t=s.dom.scroll.list,i=s.playList,e="";i.forEach(function(t,i){var n=i===s.playIndex?" active":"";e+='
        • \n\n'+t.title+'\n'+t.singer+"\n
        • "}),t.innerHTML=e},T=function(){var s=this,t=30,i=s.dom.scroll.lyric.parentNode.offsetHeight,e="",n=0;L(this.playList,this.playIndex,function(l){if("number"==typeof l){switch(l){case 0:e+='
        • 暂无歌词
        • ';break;case-1:e+='
        • 歌词解析失败
        • ';break;case-2:e+='
        • 歌词加载失败
        • '}s.dom.scroll.lyric.innerHTML=e}else{for(var o in l){var r=t*n,c=r>=i/2-t,u=l[o]||a;"string"!=typeof u&&(u=u.content),l[o]={index:n,content:u,top:r,scrollTop:c?r-(i/2-t):0,toScroll:c},n++,e+="
        • "+u+"
        • "}s.dom.scroll.lyric.innerHTML=e+('
        • '+a+"
        • ")}})},k={},A=function(){var s=[this.dom,this.audio,this.smusicId],t=s[0],i=s[1],e=s[2];k[e]&&clearInterval(k[e]);var n=parseFloat(t.progress.bar.offsetWidth);if(!isNaN(i.duration)){var a=j(i.duration);t.time.textContent="00:00/"+a}k[e]=setInterval(function(){var s=i.buffered.length;if(s>0&&void 0!=i.buffered){var a=i.buffered.end(s-1)/i.duration*n;t.progress.buffer.style.width=a+"px",Math.abs(i.duration-i.buffered.end(s-1))<1&&(t.progress.buffer.style.width=n+"px",clearInterval(k[e]))}},1e3)},$=function(){var s=[this,this.dom,this.audio],t=s[0],e=s[1],n=s[2],a=Math.round(e.progress.bar.offsetWidth),l=Math.round(e.volume.bar.offsetHeight);u(t.smusic,"click",".js-smusic-panel--tab",function(s){if(s.stopPropagation(),i.hasClass(this,"active"))return!1;var t=this.getAttribute("data-panel");i.addClass(this,"active"),"list"===t?(i.removeClass(e.btn.lyric,"active"),i.addClass(e.scroll.panel,"show-list")):(i.removeClass(e.btn.list,"active"),i.removeClass(e.scroll.panel,"show-list"))}),u(n,"timeupdate",function(){if(!isNaN(n.duration)){var s=j(n.currentTime),l=j(n.duration),o=n.currentTime/n.duration*a;e.time.textContent=s+"/"+l;var r=Math.min(o,a);e.progress.value.style.width=r+"px";var u="_smusic_lyric_"+t.playIndex,m=C[u];if(!m)return;var d=Math.round(n.currentTime),p=m[d];if(!p||"number"==typeof p)return;var h=p.index,v=p.toScroll,f=c("li",e.scroll.lyric)[h],y=c("li.active",e.scroll.lyric);y.forEach(function(s){s&&i.removeClass(s,"active")}),f&&i.addClass(f,"active"),e.scroll.lyric.style.transform=v?"translate3d(0, -"+p.scrollTop+"px, 0)":"translate3d(0, 0, 0)"}}),u(e.btn.mode,"click",function(){var s=this.getAttribute("data-play-mode"),i=void 0;switch(Number(s)){case 1:i=2;break;case 2:i=3;break;case 3:i=1}t.setMode(i)}),u(e.btn.play,"click",function(){var s=i.hasClass(this,"smusic-music-play");s?t.pause():t.play()}),u(n,"ended",function(){return t.playByMode("ended")}),u(e.btn.prev,"click",function(){return t.prev()}),u(e.btn.next,"click",function(){return t.next()}),v(e.progress.bar,".js-smusic-progress--slider","horizontal",function(s){var t=Math.max(0,Math.min(s/a,a));n.currentTime&&n.duration&&(n.currentTime=Math.round(t*n.duration))}),u(e.progress.bar,"click",function(s){s.stopPropagation();var i=this.getBoundingClientRect(),e=Math.min(a,Math.abs(s.clientX-i.left))/a;n.currentTime&&n.duration&&(n.currentTime=Math.round(e*n.duration),t.play())}),v(e.volume.bar,".js-smusic-volume--slider","vertical",function(s){var i=Number((l-s)/l);t.setVolume(i)}),u(e.btn.volume,"click",function(s){n.muted?(i.removeClass(this,"smusic-volume--mute"),this.setAttribute("title","静音"),n.muted=!1):(i.addClass(this,"smusic-volume--mute"),this.setAttribute("title","取消静音"),n.muted=!0)}),u(e.scroll.list,"click",".js-smusic-song--item",function(s){var e=this.getAttribute("data-song-index");i.hasClass(this,"active")?t.play():H.call(t,e)})},H=function(s,t,e){var n=[this.dom,this.audio,this.playList.length],a=n[0],o=n[1],r=n[2];s>=r-1&&(s=r-1),s<=0&&(s=0),this.playIndex=s;var u=this.playList[this.playIndex];if(!u)return b("没有要播放的歌曲"),!1;var m=function(){return A.call(this)}.bind(this);o.removeEventListener("canplay",m,!1),k[this.smusicId]&&clearInterval(k[this.smusicId]),a.progress.buffer.style.width="0px",a.progress.value.style.width="0px",a.time.textContent="00:00/00:00",a.scroll.lyric.style.transform="translate3d(0, 0, 0)",a.scroll.lyric.innerHTML='
        • 正在加载歌词...
        • ',c(".js-smusic-song--item.active",a.scroll.list).forEach(function(s){s&&i.removeClass(s,"active, pause")});var d=c(".js-smusic-song--item",a.scroll.list)[s];d&&i.addClass(d,"active"),o.src=u.audio,o.load(),o.addEventListener("canplay",m,!1),e?this.config.autoPlay&&o.play():o.play();var p=u.title||"Smusic",h=u.singer||"singer",v=u.thumbnail||l;a.song.title.textContent=p,a.song.singer.textContent=h,a.song.thumbnail.src=v,a.scroll.title.setAttribute("title",h+" - "+p),T.call(this),t&&t.call(this,u)},_=function(){function s(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,s),Array.isArray(t)||g("播放列表必须是一个数组"),this.playList=t,this.audio=null,this.version=n,this.config=i.assign({},f,e);var a=parseInt(this.config.playIndex),l=parseInt(this.config.playMode),o=parseFloat(this.config.volume);a<0&&(a=0),a>this.playList.length-1&&(a=this.playList.length-1),(l<1||l>3)&&(l=1),(o<0||o>1)&&(o=.5),this.playIndex=a,this.playMode=l,this.volume=o}return s.prototype.playByMode=function(s,t,i){var e=[Number(this.playMode),this.playIndex,this.playList.length],n=e[0],a=e[1],l=e[2],o=a;if(s||2==n)switch(n){case 1:"prev"==s?o=a<=l-1&&a>0?a-1:l-1:"next"!=s&&"ended"!=s||(o=a>=l-1?0:a+1);break;case 2:o=S(a,l);break;case 3:o="prev"==s?a<=l-1&&a>0?a-1:l-1:"next"==s?a>=l-1?0:a+1:a}else;H.call(this,o,t,i)},s.prototype.addSong=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:m;return s&&s.audio?(this.playList.push({title:s.title||"歌曲名",singer:s.singer||"歌手名",audio:s.audio,thumbnail:s.thumbnail||l,lyric:""|s.lyric}),this.refreshList(),t&&t()):t&&t("添加失败,参数不是一个对象或者未设置audio属性"),this.playList},s.prototype.refreshList=function(){E.call(this)},s.prototype.next=function(s){this.playByMode("next",s)},s.prototype.prev=function(s){this.playByMode("next",s)},s.prototype.play=function(s){var t=this.dom.btn.play;i.removeClass(t,"smusic-music-pause"),i.addClass(t,"smusic-music-play"),t.setAttribute("title","暂停");var e=r(".js-smusic-song--item.active",this.dom.scroll.list);i.removeClass(e,"pause"),this.playList.length&&this.audio.play(),s&&s.call(this,this.playList[this.playIndex])},s.prototype.pause=function(s){var t=this.dom.btn.play;i.addClass(t,"smusic-music-pause"),i.removeClass(t,"smusic-music-play");var e=r(".js-smusic-song--item.active",this.dom.scroll.list);i.addClass(e,"pause"),t.setAttribute("title","播放"),this.playList.length&&this.audio.pause(),s&&s.call(this,this.playList[this.playIndex])},s.prototype.setVolume=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:.5;s=Number(s),s<0&&(s=0),s>1&&(s=1);var t=this.dom.volume.bar.offsetHeight,e=this.dom.volume.slider.offsetHeight,n=t*s,a=Math.min((1-s)*t,t-e);0===s?(i.addClass(this.dom.btn.volume,"smusic-volume--mute"),this.dom.btn.volume.setAttribute("title","取消静音"),this.audio.muted=!0):(i.removeClass(this.dom.btn.volume,"smusic-volume--mute"),this.audio.muted=!1,this.dom.btn.volume.setAttribute("title","静音")),this.dom.volume.value.style.height=n+"px",this.dom.volume.slider.style.top=a+"px",this.volume=s,this.audio.volume=s},s.prototype.setMode=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;s=Number(s);var t=void 0,i=void 0,e=this.dom.btn.mode;switch(s){case 1:default:t="列表循环",i="smusic-mode--loop";break;case 2:t="单曲循环",i="smusic-mode--single";break;case 3:t="随机播放",i="smusic-mode--random"}e.setAttribute("data-play-mode",s.toString()),e.setAttribute("title",t),e.className=e.className.replace(/smusic-mode--\w+/,i),this.playMode=s},s.prototype.init=function(){var s=[this.config,N(this.config.container)],t=s[0],e=s[1];this.smusic=e.element,this.smusicId=e.id,this.audio=this.smusic.getElementsByTagName("audio")[0],I.call(this),E.call(this),$.call(this),this.setVolume(this.volume),this.setMode(this.playMode),this.playList.length?this.playByMode(void 0,function(){t.autoPlay||i.trigger(this.dom.btn.play,"click")},!0):b("歌曲列表为空")},s.prototype.getCurrentInfo=function(){if(this.playList.length){var s=this.playList[this.playIndex];return s.index=this.playIndex,s.volume=this.volume,s.mode=this.playMode,s}return null},s.prototype.destroy=function(){},s}();t.SMusic=function(s,t){return new _(s,t)}}(); \ No newline at end of file diff --git a/src/js/smusic.js b/src/js/smusic.js index ddfb87a..4ee962e 100644 --- a/src/js/smusic.js +++ b/src/js/smusic.js @@ -562,7 +562,6 @@ //点击列表 bind(DOM.scroll.list, 'click', '.js-smusic-song--item', function(event) { - console.log(this) const index = this.getAttribute('data-song-index'); if (utils.hasClass(this, 'active')) { self.play();