fcp-tabs.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * 注册命名空间
  3. */
  4. baidu.namespace.register("baidu.fcptabs");
  5. /**
  6. * tab相关处理
  7. * @author zhaoxianlie
  8. */
  9. baidu.fcptabs = (function(){
  10. /**
  11. * 存储临时变量
  12. */
  13. var _tempvar = {};
  14. /**
  15. * 创建主面板
  16. */
  17. var _createMainTab = function(){
  18. //首先检测面板箱是否存在
  19. var $tabBox = jQuery("#fe-helper-box");
  20. var $mainTab = jQuery("#fe-helper-main-tab");
  21. //有了则删掉
  22. if($tabBox[0]){
  23. $tabBox.remove();
  24. }
  25. $tabBox = jQuery('<div id="fe-helper-box" class="fe-helper-hide"></div>').appendTo("body");
  26. jQuery('<iframe id="fe-helper-main-ifr" src="about:blank" frameborder="0"></iframe>').appendTo($tabBox);
  27. $mainTab = jQuery('<div id="fe-helper-main-tab"></div>').appendTo($tabBox).html('\
  28. <ul id="fe-helper-main-ul">\
  29. <li id="fe-helper-closethick"><span class="ui-icon ui-icon-closethick" title="关闭面板">Close</span></li>\
  30. <li id="fe-helper-plusthick" class="fe-helper-hide"><span class="ui-icon ui-icon-plusthick" title="最大化面板">Maximun</span></li>\
  31. <li id="fe-helper-minusthick"><span class="ui-icon ui-icon-minusthick" title="最小化面板">Minimun</span></li>\
  32. </ul>\
  33. ');
  34. //最大化显示
  35. $tabBox.css({
  36. height : jQuery(window).height()
  37. });
  38. // 初始化mainTab,并增加tab方法
  39. $mainTab.tabs({
  40. tabTemplate: "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
  41. add: function( event, ui ) {
  42. //设置子面板大小
  43. jQuery('#fe-helper-tab-' + _tempvar.type).css({
  44. height : jQuery(window).height() - 80
  45. });
  46. var _t_id = 'fe-helper-acc-' + _tempvar.type;
  47. jQuery( ui.panel ).append( "<div id='" + _t_id + "'>" + _tempvar.tabContent + "</div>" );
  48. //设置子面板大小
  49. jQuery('#' + _t_id).css({
  50. height : jQuery(window).height() - 80
  51. });
  52. //是否需要将内容部分以Accordion形式展现
  53. if(_tempvar.isAccordion) {
  54. jQuery("#" + _t_id).accordion({
  55. collapsible: true,
  56. active:false
  57. });
  58. //.rst-content
  59. var _rst_content = jQuery("#" + _t_id + " .rst-content");
  60. var _height = jQuery(window).height() - 120 - _rst_content.length * 30;
  61. _rst_content.css({
  62. height : _height
  63. });
  64. }
  65. }
  66. });
  67. //对新产生的tab,增加移除事件
  68. jQuery( "#fe-helper-main-ul span.ui-icon-close" ).live( "click", function() {
  69. var $allTabs = jQuery( "#fe-helper-main-ul li");
  70. var index = $allTabs.index( jQuery( this ).parent() ) - 3;
  71. $mainTab.tabs( "remove", index );
  72. //如果所有tab都关闭了,则关闭整个tabBox
  73. if($allTabs.length == 4) {
  74. _tempvar.tabBox.remove();
  75. }
  76. });
  77. //三个按钮的事件
  78. jQuery("#fe-helper-closethick").click(function(e){
  79. _tempvar.tabBox.hide("slide");
  80. });
  81. jQuery("#fe-helper-plusthick").click(function(e){
  82. _tempvar.tabBox.css({
  83. height : jQuery(window).height()
  84. });
  85. jQuery(this).hide().next().show();
  86. });
  87. jQuery("#fe-helper-minusthick").click(function(e){
  88. _tempvar.tabBox.css({
  89. height : 38
  90. });
  91. jQuery(this).hide().prev().show();
  92. });
  93. //window大小改变时候
  94. jQuery(window).resize(function(e){
  95. _tempvar.tabBox.css({
  96. height : jQuery(window).height()
  97. });
  98. });
  99. //保存mainTab
  100. _tempvar.tabBox = $tabBox;
  101. _tempvar.mainTab = $mainTab;
  102. return $tabBox;
  103. };
  104. /**
  105. * 根据不同的标题,在页面上增加面板
  106. * @param {Object} type 面板的类型:HTML,CSS,Javascript
  107. * @param {Object} tabTitle 面板标题
  108. * @param {Object} tabContent 面板内容
  109. * @param {Object} isAccordion 是否生成Accordion
  110. */
  111. var _addTab = function(type,tabTitle,tabContent,isAccordion) {
  112. //保存这个值,创建tab时用到
  113. _tempvar.type = type;
  114. _tempvar.tabContent = tabContent;
  115. _tempvar.isAccordion = isAccordion;
  116. //创建新的面板
  117. return _tempvar.mainTab.tabs(
  118. "add",
  119. "#fe-helper-tab-" + _tempvar.type,
  120. tabTitle);
  121. };
  122. /**
  123. * 根据不同的标题,在页面上增加HTML面板
  124. * @param {Object} type 面板的类型:HTML,CSS,Javascript
  125. * @param {Object} tabContent HTML面板内容
  126. */
  127. var _addIssueSuggestionTab = function(type,tabContent) {
  128. //创建面板
  129. return _addTab(type + '-issue-sug',baidu.i18n.getMessage('msg0061',[type]),tabContent,false);
  130. };
  131. /**
  132. * 根据不同的标题,在页面上增加HTML面板
  133. * @param {Object} tabContent HTML面板内容
  134. */
  135. var _addHtmlTab = function(tabContent) {
  136. //创建面板
  137. return _addTab('html',baidu.i18n.getMessage('msg0001'),tabContent,true);
  138. };
  139. /**
  140. * 在页面上创建Javascript面板
  141. * @param {Object} tabContent HTML面板内容
  142. */
  143. var _addJavascriptTab = function(tabContent) {
  144. //创建新的面板
  145. return _addTab('js',baidu.i18n.getMessage('msg0003'),tabContent,true);
  146. };
  147. /**
  148. * 根据不同的标题,在页面上增加CSS面板
  149. * @param {Object} tabTitle 面板标题
  150. * @param {Object} tabContent 面板内容
  151. */
  152. var _addCssTab = function(tabTitle,tabContent) {
  153. //保存这个值,创建tab时用到
  154. _tempvar.cssTabContent = tabContent;
  155. _tempvar.cssTabCount = _tempvar.cssTabCount || 0;
  156. _tempvar.cssTabCount++
  157. _tempvar.cssTab = jQuery('#fe-helper-tab-css');
  158. if(!_tempvar.cssTab[0]) {
  159. //创建面板
  160. _addTab('css',baidu.i18n.getMessage('msg0002'),'',false);
  161. _tempvar.cssTab = jQuery('#fe-helper-tab-css').html('<ul id="fe-helper-css-ul"></ul>');
  162. // 初始化mainTab,并增加tab方法
  163. _tempvar.cssTab.tabs({
  164. tabTemplate: "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
  165. add: function( event, ui ) {
  166. var _t_id = 'fe-helper-css-acc-' + _tempvar.cssTabCount;
  167. jQuery( ui.panel ).append( "<div id='" + _t_id + "'>" + _tempvar.cssTabContent + "</div>" );
  168. jQuery("#" + _t_id).accordion({
  169. collapsible: true,
  170. active:false
  171. });
  172. //.rst-content
  173. var _rst_content = jQuery("#" + _t_id + " .rst-content");
  174. var _height = jQuery(window).height() - 180 - _rst_content.length * 30;
  175. _rst_content.css({
  176. height : _height
  177. });
  178. }
  179. });
  180. //对新产生的tab,增加移除事件
  181. jQuery( "#fe-helper-css-ul span.ui-icon-close" ).live( "click", function() {
  182. var $allTabs = jQuery( "#fe-helper-css-ul li");
  183. var index = $allTabs.index( jQuery( this ).parent() );
  184. _tempvar.cssTab.tabs( "remove", index );
  185. });
  186. }
  187. //创建新的面板
  188. return _tempvar.cssTab.tabs(
  189. "add",
  190. "#fe-helper-tab-css-" + _tempvar.cssTabCount,
  191. tabTitle);
  192. };
  193. /**
  194. * 创建进度条
  195. */
  196. var _createProgressBar = function(){
  197. var _startTime = new Date();
  198. //先创建主面板
  199. baidu.fcptabs.createMainTab();
  200. if(_tempvar.progressbar) {
  201. _tempvar.mask.remove();
  202. }
  203. //创建遮罩面板
  204. _tempvar.mask = jQuery('<div id="fe-helper-pb-mask">' +
  205. '<div id="f-h-p-m"></div>' +
  206. '<div id="fe-helper-progress-bar-img">正在进行页面检测,请稍后...</div>' +
  207. '<div id="fe-helper-progress-bar"></div>' +
  208. '</div>').appendTo('body');
  209. //遮罩层大小
  210. jQuery('#f-h-p-m').css({
  211. height : jQuery(window).height(),
  212. width : jQuery(window).width()
  213. });
  214. //进度条背景
  215. var pbarGif = chrome.extension.getURL('static/img/pbar-ani.gif');
  216. jQuery('#fe-helper-progress-bar-img').css({
  217. 'background' : 'url(' + pbarGif + ') repeat-x'
  218. });
  219. //产生滚动条,初始化进度为0
  220. _tempvar.progressbar = jQuery('#fe-helper-progress-bar')
  221. .progressbar({
  222. value : 0,
  223. complete : function(event,ui){
  224. var _pbImg = jQuery('#fe-helper-progress-bar-img').html('页面检测完成,共计耗时:' + (new Date() - _startTime) / 1000 + ' s');
  225. //完成以后展示检测结果
  226. _tempvar.tabBox.show('slide',{},500);
  227. jQuery('#f-h-p-m').fadeOut(500);
  228. _pbImg.fadeOut(3000);
  229. }
  230. });
  231. jQuery('#fe-helper-progress-bar-img').css({
  232. top : jQuery(window).height() / 2 - 40,
  233. left : ( jQuery(window).width() - 800) / 2
  234. });
  235. };
  236. /**
  237. * 更新进度条
  238. * @param {Object} _value
  239. */
  240. var _updateProgressBar = function(_value){
  241. _tempvar.progressbar.progressbar('value',_value);
  242. };
  243. return {
  244. createMainTab : _createMainTab,
  245. addHtmlTab : _addHtmlTab,
  246. addJavascriptTab : _addJavascriptTab,
  247. addCssTab : _addCssTab,
  248. addIssueSuggestionTab : _addIssueSuggestionTab,
  249. createProgressBar : _createProgressBar,
  250. updateProgressBar : _updateProgressBar
  251. };
  252. })();