music.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. function Music() {
  2. this.init();
  3. }
  4. (function () {
  5. var pages = [],
  6. panels = [],
  7. selectedItem = null;
  8. Music.prototype = {
  9. total:70,
  10. pageSize:10,
  11. dataUrl:"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.search.common",
  12. playerUrl:"http://box.baidu.com/widget/flash/bdspacesong.swf",
  13. init:function () {
  14. var me = this;
  15. domUtils.on($G("J_searchName"), "keyup", function (event) {
  16. var e = window.event || event;
  17. if (e.keyCode == 13) {
  18. me.dosearch();
  19. }
  20. });
  21. domUtils.on($G("J_searchBtn"), "click", function () {
  22. me.dosearch();
  23. });
  24. },
  25. callback:function (data) {
  26. var me = this;
  27. me.data = data.song_list;
  28. setTimeout(function () {
  29. $G('J_resultBar').innerHTML = me._renderTemplate(data.song_list);
  30. }, 300);
  31. },
  32. dosearch:function () {
  33. var me = this;
  34. selectedItem = null;
  35. var key = $G('J_searchName').value;
  36. if (utils.trim(key) == "")return false;
  37. key = encodeURIComponent(key);
  38. me._sent(key);
  39. },
  40. doselect:function (i) {
  41. var me = this;
  42. if (typeof i == 'object') {
  43. selectedItem = i;
  44. } else if (typeof i == 'number') {
  45. selectedItem = me.data[i];
  46. }
  47. },
  48. onpageclick:function (id) {
  49. var me = this;
  50. for (var i = 0; i < pages.length; i++) {
  51. $G(pages[i]).className = 'pageoff';
  52. $G(panels[i]).className = 'paneloff';
  53. }
  54. $G('page' + id).className = 'pageon';
  55. $G('panel' + id).className = 'panelon';
  56. },
  57. listenTest:function (elem) {
  58. var me = this,
  59. view = $G('J_preview'),
  60. is_play_action = (elem.className == 'm-try'),
  61. old_trying = me._getTryingElem();
  62. if (old_trying) {
  63. old_trying.className = 'm-try';
  64. view.innerHTML = '';
  65. }
  66. if (is_play_action) {
  67. elem.className = 'm-trying';
  68. view.innerHTML = me._buildMusicHtml(me._getUrl(true));
  69. }
  70. },
  71. _sent:function (param) {
  72. var me = this;
  73. $G('J_resultBar').innerHTML = '<div class="loading"></div>';
  74. utils.loadFile(document, {
  75. src:me.dataUrl + '&query=' + param + '&page_size=' + me.total + '&callback=music.callback&.r=' + Math.random(),
  76. tag:"script",
  77. type:"text/javascript",
  78. defer:"defer"
  79. });
  80. },
  81. _removeHtml:function (str) {
  82. var reg = /<\s*\/?\s*[^>]*\s*>/gi;
  83. return str.replace(reg, "");
  84. },
  85. _getUrl:function (isTryListen) {
  86. var me = this;
  87. var param = 'from=tiebasongwidget&url=&name=' + encodeURIComponent(me._removeHtml(selectedItem.title)) + '&artist='
  88. + encodeURIComponent(me._removeHtml(selectedItem.author)) + '&extra='
  89. + encodeURIComponent(me._removeHtml(selectedItem.album_title))
  90. + '&autoPlay='+isTryListen+'' + '&loop=true';
  91. return me.playerUrl + "?" + param;
  92. },
  93. _getTryingElem:function () {
  94. var s = $G('J_listPanel').getElementsByTagName('span');
  95. for (var i = 0; i < s.length; i++) {
  96. if (s[i].className == 'm-trying')
  97. return s[i];
  98. }
  99. return null;
  100. },
  101. _buildMusicHtml:function (playerUrl) {
  102. var html = '<embed class="BDE_try_Music" allowfullscreen="false" pluginspage="http://www.macromedia.com/go/getflashplayer"';
  103. html += ' src="' + playerUrl + '"';
  104. html += ' width="1" height="1" style="position:absolute;left:-2000px;"';
  105. html += ' type="application/x-shockwave-flash" wmode="transparent" play="true" loop="false"';
  106. html += ' menu="false" allowscriptaccess="never" scale="noborder">';
  107. return html;
  108. },
  109. _byteLength:function (str) {
  110. return str.replace(/[^\u0000-\u007f]/g, "\u0061\u0061").length;
  111. },
  112. _getMaxText:function (s) {
  113. var me = this;
  114. s = me._removeHtml(s);
  115. if (me._byteLength(s) > 12)
  116. return s.substring(0, 5) + '...';
  117. if (!s) s = "&nbsp;";
  118. return s;
  119. },
  120. _rebuildData:function (data) {
  121. var me = this,
  122. newData = [],
  123. d = me.pageSize,
  124. itembox;
  125. for (var i = 0; i < data.length; i++) {
  126. if ((i + d) % d == 0) {
  127. itembox = [];
  128. newData.push(itembox)
  129. }
  130. itembox.push(data[i]);
  131. }
  132. return newData;
  133. },
  134. _renderTemplate:function (data) {
  135. var me = this;
  136. if (data.length == 0)return '<div class="empty">' + lang.emptyTxt + '</div>';
  137. data = me._rebuildData(data);
  138. var s = [], p = [], t = [];
  139. s.push('<div id="J_listPanel" class="listPanel">');
  140. p.push('<div class="page">');
  141. for (var i = 0, tmpList; tmpList = data[i++];) {
  142. panels.push('panel' + i);
  143. pages.push('page' + i);
  144. if (i == 1) {
  145. s.push('<div id="panel' + i + '" class="panelon">');
  146. if (data.length != 1) {
  147. t.push('<div id="page' + i + '" onclick="music.onpageclick(' + i + ')" class="pageon">' + (i ) + '</div>');
  148. }
  149. } else {
  150. s.push('<div id="panel' + i + '" class="paneloff">');
  151. t.push('<div id="page' + i + '" onclick="music.onpageclick(' + i + ')" class="pageoff">' + (i ) + '</div>');
  152. }
  153. s.push('<div class="m-box">');
  154. s.push('<div class="m-h"><span class="m-t">' + lang.chapter + '</span><span class="m-s">' + lang.singer
  155. + '</span><span class="m-z">' + lang.special + '</span><span class="m-try-t">' + lang.listenTest + '</span></div>');
  156. for (var j = 0, tmpObj; tmpObj = tmpList[j++];) {
  157. s.push('<label for="radio-' + i + '-' + j + '" class="m-m">');
  158. s.push('<input type="radio" id="radio-' + i + '-' + j + '" name="musicId" class="m-l" onclick="music.doselect(' + (me.pageSize * (i-1) + (j-1)) + ')"/>');
  159. s.push('<span class="m-t">' + me._getMaxText(tmpObj.title) + '</span>');
  160. s.push('<span class="m-s">' + me._getMaxText(tmpObj.author) + '</span>');
  161. s.push('<span class="m-z">' + me._getMaxText(tmpObj.album_title) + '</span>');
  162. s.push('<span class="m-try" onclick="music.doselect(' + (me.pageSize * (i-1) + (j-1)) + ');music.listenTest(this)"></span>');
  163. s.push('</label>');
  164. }
  165. s.push('</div>');
  166. s.push('</div>');
  167. }
  168. t.reverse();
  169. p.push(t.join(''));
  170. s.push('</div>');
  171. p.push('</div>');
  172. return s.join('') + p.join('');
  173. },
  174. exec:function () {
  175. var me = this;
  176. if (selectedItem == null) return;
  177. $G('J_preview').innerHTML = "";
  178. editor.execCommand('music', {
  179. url:me._getUrl(false),
  180. width:400,
  181. height:95
  182. });
  183. }
  184. };
  185. })();