general.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * General Plugin
  3. * @version 1.0
  4. * @author dron
  5. * @create 2013-04-21
  6. */
  7. Tracker.setupGeneralPlugin = function(){
  8. Tracker.Plugins.addOn( "general", function(){
  9. var template, data, calculate, render, update, eventDelegate,
  10. i, l, r, q, tmpl, now, window, document, mainBody, mainBodyId;
  11. tmpl = Tracker.Util.tmpl;
  12. now = function( time ){
  13. time = new Date();
  14. time = [ time.getFullYear(), time.getMonth() + 1, time.getDate() ].join( "-" ) + " " +
  15. [ time.getHours(), time.getMinutes(), time.getSeconds() ].join( ":" );
  16. time = time.replace( /\b(\d)\b/g, "0$1" );
  17. return time;
  18. };
  19. Tracker.Plugins.addStyle( [
  20. "#plugin-general-page .toolbar{ height: 42px; background-color: #fafafa; border-bottom: 1px solid #d5d5d5; }",
  21. "#plugin-general-page .toolbar li{ display: block; float: left; }",
  22. "#plugin-general-page .toolbar li.button-like{ padding: 5px 0 0 5px; }",
  23. "#plugin-general-page .toolbar li.text-like{ padding: 12px 0 0 20px; line-height: 20px; }",
  24. "#plugin-general-page .toolbar li.first{ padding-left: 30px; }",
  25. "#plugin-general-page .body{ position: absolute; top: 43px; bottom: 0; left: 0; right: 0; padding: 10px 30px; }",
  26. "#plugin-general-page .body .table{ margin-bottom: 30px; width: 900px; table-layout:fixed; }",
  27. "#plugin-general-page .body .table td{ }",
  28. "#plugin-general-page .body .table td .ellipsisable{ width: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }",
  29. "#plugin-general-page .body .unit{ font-style: italic; font-weight: 400; }",
  30. "#plugin-general-page .body .close-up{ position: relative; top: -4px; }",
  31. "#plugin-general-page .body .low{ color: #ff814e; }",
  32. "#plugin-general-page .body .high{ color: #2bb027; }",
  33. "#plugin-general-page .body .close-up b{ font-family: Constantia, Georgia; font-size: 24px; font-weight: 400; position: absolute; }"
  34. ].join( "" ) );
  35. template = {
  36. page: tmpl( [
  37. "<ul class='toolbar unstyled clearfix'>",
  38. "<li class='first button-like'>",
  39. "<button class='btn' action='update'>&#21047;&#26032;</button>",
  40. "</li>",
  41. "<li class='text-like muted'>&#26368;&#21518;&#26356;&#26032;&#26102;&#38388;&#65306;<%= now() %></li>",
  42. "</ul>",
  43. "<div id='<%= mainBodyId %>' class='body scrollable'>",
  44. "<% if( count > 0 ){ %>",
  45. "<h5>&#24403;&#21069;&#32593;&#39029;&#20849;&#21253;&#21547; <%= count %> &#20010;&#33050;&#26412;&#36164;&#28304;&#65306;<%= embedCount %> &#20010;&#20869;&#23884;&#65292;<%= linkCount %> &#20010;&#22806;&#38142;&#25991;&#20214;&#65292;<%= appendCount %> &#20010;&#21160;&#24577;&#21152;&#36733;&#12290;</h5>",
  46. "<table class='table table-bordered table-striped'>",
  47. "<thead>",
  48. "<tr>",
  49. "<th width='*'>&#20351;&#29992;&#37327; <span class='unit'>(%)</span></th>",
  50. "<th width='90'>&#20887;&#20313;&#37327; <span class='unit'>(%)</span></th>",
  51. "<th width='120'>&#20351;&#29992;&#34892;&#25968;</th>",
  52. "<th width='90'>&#24635;&#34892;&#25968;</th>",
  53. "<th width='90'>&#24635;&#22823;&#23567; <span class='unit'>(k)</span></th>",
  54. "<th width='120'>&#24635;&#21152;&#36733;&#32791;&#26102; <span class='unit'>(ms)</span></th>",
  55. "<th width='120'>&#24635;&#36816;&#34892;&#32791;&#26102; <span class='unit'>(ms)</span></th>",
  56. "</tr>",
  57. "</thead>",
  58. "<tbody>",
  59. "<tr>",
  60. "<td><span class='close-up <%= usefulRate < 50 ? 'low' : 'high' %>'><b><%= usefulRate %><b></span></td>",
  61. "<td><%= redundancyRate %></td>",
  62. "<td><%= usefulLines %></td>",
  63. "<td><%= totalLines %></td>",
  64. "<td><%= totalSize %></td>",
  65. "<td><%= totalLoadConsum %></td>",
  66. "<td><%= totalRunConsum %></td>",
  67. "</tr>",
  68. "</tbody>",
  69. "</table>",
  70. "<h5>&#20887;&#20313;&#37327;&#26368;&#39640; top <%= showCodeCount %> &#33050;&#26412;&#65306;</h5>",
  71. "<table class='table table-bordered table-striped table-condensed'>",
  72. "<thead>",
  73. "<tr>",
  74. "<th width='*'>&#25991;&#20214;&#21517;</th>",
  75. "<th width='70'>&#31867;&#22411;</th>",
  76. "<th width='90'>&#20887;&#20313;&#37327; <span class='unit'>(%)</span></th>",
  77. "<th width='40'></th>",
  78. "</tr>",
  79. "</thead>",
  80. "<tbody>",
  81. "<% for( var i = 0, r; i < redundancyRateTableData.length; i ++ ){ %>",
  82. "<% r = redundancyRateTableData[ i ]; %>",
  83. "<tr>",
  84. "<td><div class='ellipsisable'><%= r.fullUrl || '-' %></div></td>",
  85. "<td>",
  86. "<% switch( r.type ){ ",
  87. "case 'embed':",
  88. "print( '&#20869;&#23884;' );",
  89. "break;",
  90. "case 'link':",
  91. "print( '&#25991;&#20214;&#38142;&#25509;' );",
  92. "break;",
  93. "case 'append':",
  94. "print( '&#21160;&#24577;&#25554;&#20837;' );",
  95. "break;",
  96. "} %>",
  97. "</td>",
  98. "<td><%= Math.round( ( 1 - r.prop( 'usefulRate' ) ) * 1e4 ) / 1e2 %></td>",
  99. "<td><button class='btn btn-mini' action='show-code' data-code-id='<%= r.id %>'>&#26597;&#30475;</button></td>",
  100. "</tr>",
  101. "<% } %>",
  102. "</tbody>",
  103. "</table>",
  104. "<h5>&#20307;&#31215;&#26368;&#22823; top <%= showCodeCount %> &#33050;&#26412;&#65306;</h5>",
  105. "<table class='table table-bordered table-striped table-condensed'>",
  106. "<thead>",
  107. "<tr>",
  108. "<th width='*'>&#25991;&#20214;&#21517;</th>",
  109. "<th width='70'>&#31867;&#22411;</th>",
  110. "<th width='90'>&#22823;&#23567; <span class='unit'>(k)</span></th>",
  111. "<th width='40'></th>",
  112. "</tr>",
  113. "</thead>",
  114. "<tbody>",
  115. "<% for( var i = 0, r; i < sizeTableData.length; i ++ ){ %>",
  116. "<% r = sizeTableData[ i ]; %>",
  117. "<tr>",
  118. "<td><div class='ellipsisable'><%= r.fullUrl || '-' %></div></td>",
  119. "<td>",
  120. "<% switch( r.type ){ ",
  121. "case 'embed':",
  122. "print( '&#20869;&#23884;' );",
  123. "break;",
  124. "case 'link':",
  125. "print( '&#25991;&#20214;&#38142;&#25509;' );",
  126. "break;",
  127. "case 'append':",
  128. "print( '&#21160;&#24577;&#25554;&#20837;' );",
  129. "break;",
  130. "} %>",
  131. "</td>",
  132. "<td><%= ( r.size / 1024 ).toFixed( 2 ) %></td>",
  133. "<td><button class='btn btn-mini' action='show-code' data-code-id='<%= r.id %>'>&#26597;&#30475;</button></td>",
  134. "</tr>",
  135. "<% } %>",
  136. "</tbody>",
  137. "</table>",
  138. "<h5>&#36816;&#26102;&#32791;&#26102;&#26368;&#39640; top <%= showCodeCount %> &#33050;&#26412;&#65306;</h5>",
  139. "<table class='table table-bordered table-striped table-condensed'>",
  140. "<thead>",
  141. "<tr>",
  142. "<th width='*'>&#25991;&#20214;&#21517;</th>",
  143. "<th width='70'>&#31867;&#22411;</th>",
  144. "<th width='90'>&#32791;&#26102; <span class='unit'>(ms)</span></th>",
  145. "<th width='40'></th>",
  146. "</tr>",
  147. "</thead>",
  148. "<tbody>",
  149. "<% for( var i = 0, r; i < runConsumTableData.length; i ++ ){ %>",
  150. "<% r = runConsumTableData[ i ]; %>",
  151. "<tr>",
  152. "<td><div class='ellipsisable'><%= r.fullUrl || '-' %></div></td>",
  153. "<td>",
  154. "<% switch( r.type ){ ",
  155. "case 'embed':",
  156. "print( '&#20869;&#23884;' );",
  157. "break;",
  158. "case 'link':",
  159. "print( '&#25991;&#20214;&#38142;&#25509;' );",
  160. "break;",
  161. "case 'append':",
  162. "print( '&#21160;&#24577;&#25554;&#20837;' );",
  163. "break;",
  164. "} %>",
  165. "</td>",
  166. "<td><%= r.runConsum %></td>",
  167. "<td><button class='btn btn-mini' action='show-code' data-code-id='<%= r.id %>'>&#26597;&#30475;</button></td>",
  168. "</tr>",
  169. "<% } %>",
  170. "</tbody>",
  171. "</table>",
  172. "<% } else { %>",
  173. "&#35813;&#32593;&#39029;&#27809;&#26377;&#20219;&#20309; JS &#20195;&#30721;&#12290;",
  174. "<% } %>",
  175. "</div>"
  176. ].join( "" ) )
  177. };
  178. data = function(){};
  179. data.prototype = {
  180. count: 0,
  181. embedCount: 0,
  182. linkCount: 0,
  183. appendCount: 0,
  184. usefulRate: 0,
  185. redundancyRate: 100,
  186. usefulLines: 0,
  187. totalLines: 0,
  188. totalSize: 0,
  189. totalLoadConsum: 0,
  190. totalRunConsum: 0,
  191. now: now
  192. };
  193. data = new data;
  194. calculate = function(){
  195. var redundancyRate = function( code1, code2 ){
  196. return code1.prop( "usefulRate" ) - code2.prop( "usefulRate" );
  197. };
  198. var size = function( code1, code2 ){
  199. return code2.size - code1.size;
  200. };
  201. var runConsum = function( code1, code2 ){
  202. return code2.runConsum - code1.runConsum;
  203. };
  204. return function (){
  205. for( i in data )
  206. delete data[ i ];
  207. l = data.count = Tracker.CodeList.count();
  208. data.showCodeCount = l > 2 ? 3 : l;
  209. for( i = 0; i < l; i ++ ){
  210. r = Tracker.CodeList.get( i );
  211. if( r.type == "embed" ){
  212. data.embedCount ++;
  213. }else if( r.type == "link" ){
  214. data.linkCount ++;
  215. }else if( r.type == "append" ){
  216. data.appendCount ++;
  217. }
  218. data.usefulLines += r.arriveRowsCount;
  219. data.totalLines += r.rowsCount;
  220. data.totalSize += r.size;
  221. data.totalLoadConsum += r.loadConsum;
  222. data.totalRunConsum += r.runConsum;
  223. r.prop( "usefulRate", r.arriveRowsCount / r.rowsCount );
  224. }
  225. r = data.usefulLines / data.totalLines;
  226. data.usefulRate = Math.round( ( r ) * 1e4 ) / 1e2;
  227. data.redundancyRate = Math.round( ( 1 - r ) * 1e4 ) / 1e2;
  228. data.totalSize = ( data.totalSize / 1000 ).toFixed( 2 );
  229. q = Tracker.CodeList.list().slice( 0 );
  230. q.sort( redundancyRate );
  231. data.redundancyRateTableData = q.slice( 0, data.showCodeCount );
  232. q = Tracker.CodeList.list().slice( 0 );
  233. q.sort( size );
  234. data.sizeTableData = q.slice( 0, data.showCodeCount );
  235. q = Tracker.CodeList.list().slice( 0 );
  236. q.sort( runConsum );
  237. data.runConsumTableData = q.slice( 0, data.showCodeCount );
  238. mainBodyId = data.mainBodyId = Tracker.Util.id();
  239. }
  240. }();
  241. render = Tracker.Util.bind( function( text ){
  242. this.body.innerHTML = text || template.page( data );
  243. }, this );
  244. update = function(){
  245. calculate();
  246. render();
  247. };
  248. eventDelegate = function( el ){
  249. Tracker.Event.add( el, "click", function( e ){
  250. var target, action, codeId;
  251. target = e.target;
  252. action = target.getAttribute( "action" );
  253. codeId = target.getAttribute( "data-code-id" );
  254. if( action == "show-code" ){
  255. Tracker.View.ControlPanel.activeTab( "code-list" );
  256. Tracker.View.ControlPanel.showCodeDetail( codeId );
  257. }else if( action == "update" ){
  258. document.getElementById( mainBodyId ).innerHTML = "loading...";
  259. window.setTimeout( update, 1e2 );
  260. }
  261. } );
  262. };
  263. this.onStartUp( function( win, doc ){
  264. window = win;
  265. document = doc;
  266. eventDelegate( this.body );
  267. } );
  268. this.onActive( update );
  269. } );
  270. };