PageTab.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * tab控件。
  3. * @param divContainerId
  4. * @param total
  5. * @param conf
  6. * @returns {PageTab}
  7. */
  8. PageTab = function(divContainerId,total,conf){
  9. {
  10. var addCallBack=function(){
  11. alert("addCallBack");
  12. };
  13. var beforeActive=function(prePage){
  14. alert("beforeActive:" +prePage);
  15. };
  16. var activeCallBack=function(prePage,currentPage){
  17. alert("activeCallBack:" +prePage +"," + currentPage);
  18. };
  19. var beforeDell=function(curPage){
  20. alert("beforeDell:curPage:" + curPage);
  21. };
  22. var delCallBack=function(currentPage){
  23. alert("delCallBack:" + currentPage);
  24. };
  25. var txtCallBack=function(){
  26. alert("txtCallBack");
  27. };
  28. this.conf= $.extend({},{addCallBack:addCallBack,beforeActive:beforeActive,activeCallBack:activeCallBack,beforeDell:beforeDell,delCallBack:delCallBack,txtCallBack:txtCallBack}, conf);
  29. //当前tab。
  30. this.currentTab=null;
  31. var imgPlus=__ctx +"/styles/default/css/tab/icon_plus.gif";
  32. var imgDelete=__ctx +"/styles/default/css/tab/icon_minus.gif";
  33. var str='<div class="pagetab" ><ul id="pageList"></ul><span class="add" ><img id="addPage" src="'+imgPlus+'" class="imgPlus" title="在当前页后插入" /><img id="delPage" src="'+imgDelete+'" class="imgDelete" title="删除当前页" /></span></div>';
  34. this.totalPage=(total==undefined) ?1:total;
  35. //前一页
  36. this.prePage=1;
  37. $("#" + divContainerId).append(str);
  38. var _self=this;
  39. //在tab上双击进行编辑。
  40. $("#pageList").delegate("li>b", "dblclick", function() {
  41. var obj=$(this);
  42. var hasInput=obj.has("input").length==1;
  43. //是否有输入框。
  44. if(hasInput) return;
  45. var txtObj=$("<input type='text' maxlength='20' value='"+obj.text()+"' style='border-style:none;height:16px;' size='5' />");
  46. txtObj.blur(function(){
  47. var tmpObj=$(this);
  48. var val=tmpObj.val();
  49. tmpObj.parent().text(val);
  50. tmpObj.remove();
  51. var curPage=_self.getCurrentIndex();
  52. _self.conf.txtCallBack(curPage);
  53. });
  54. //回车处理
  55. txtObj.keydown(function(event) {
  56. if (event.keyCode == '13') {
  57. txtObj.parent().text(txtObj.val());
  58. txtObj.remove();
  59. var curPage=_self.getCurrentIndex();
  60. _self.conf.txtCallBack(curPage);
  61. }
  62. });
  63. obj.empty();
  64. obj.append(txtObj);
  65. txtObj.focus();
  66. });
  67. }
  68. /**
  69. * 控件初始化。
  70. */
  71. this.init=function(aryTitle,aryData){
  72. this.initPages(aryTitle);
  73. var _self=this;
  74. $("#addPage").click(function(){
  75. _self.addPage();
  76. });
  77. $("#delPage").click(function(){
  78. _self.deletePage();
  79. });
  80. },
  81. /**
  82. * 初始化tab选项卡。
  83. */
  84. this.initPages=function(aryTitle){
  85. var pageList = $("#pageList");
  86. pageList.empty();
  87. this.totalPage=aryTitle.length;
  88. //添加tab
  89. for(var i = 1;i <= this.totalPage;i++){
  90. var title=aryTitle[i-1];
  91. if(title=="")
  92. title="页 " + i ;
  93. var li=$("<li><b>"+title+"</b></li>");
  94. //设置点击事件。
  95. this.attachTabEvent(li);
  96. pageList.append(li);
  97. }
  98. this.currentTab=pageList.children().first();
  99. this.setActivePage();
  100. },
  101. /**
  102. * 设置点击选项卡点击事件。
  103. */
  104. this.attachTabEvent =function (obj){
  105. var self=this;
  106. obj.click(function(){
  107. var tabObj=$(this);
  108. var prePage=self.getCurrentIndex();
  109. var result=self.conf.beforeActive(prePage);
  110. if(!result)return;
  111. self.currentTab=tabObj;
  112. //前一页的号码
  113. var curPage=self.getCurrentIndex();
  114. self.setActivePage();
  115. self.conf.activeCallBack(prePage,curPage);
  116. });
  117. },
  118. /**
  119. * 添加tab
  120. */
  121. this.addPage =function(){
  122. this.totalPage ++;
  123. var li=$("<li><b>新页面</b></li>");
  124. this.attachTabEvent(li);
  125. this.currentTab.after(li);
  126. this.currentTab=li;
  127. this.setActivePage();
  128. //添加回调函数
  129. this.conf.addCallBack();
  130. },
  131. /**
  132. * 设置激活页
  133. */
  134. this.setActivePage=function (){
  135. var curTab =this.currentTab;
  136. if(curTab.siblings()){
  137. curTab.siblings().removeClass("current");
  138. }
  139. curTab.addClass("current");
  140. },
  141. /**
  142. * 取得当前tab的索引。
  143. * @returns
  144. */
  145. this.getCurrentIndex=function(){
  146. return $("#pageList").children().index(this.currentTab) +1;
  147. },
  148. /**
  149. * 删除tab选项。
  150. */
  151. this.deletePage=function(){
  152. if (this.totalPage==1) return;
  153. var curPage=this.getCurrentIndex();
  154. var next=this.currentTab.next();
  155. var prev=this.currentTab.prev();
  156. var result=this.conf.beforeDell(curPage);
  157. if(!result)return;
  158. this.currentTab.remove();
  159. if(next.length>0){
  160. this.currentTab=next;
  161. }
  162. else{
  163. this.currentTab=prev;
  164. }
  165. this.totalPage--;
  166. this.setActivePage();
  167. //回调删除事件处理
  168. this.conf.delCallBack(curPage);
  169. };
  170. };