tab.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div class="nut-tab-part" >
  3. <div class="nut-tab" :class="{'nut-tab-horizontal' : positionNavCss}">
  4. <div v-if="positionNav=='right' || positionNav=='bottom'" class="nut-tab-item" ref="items">
  5. <slot></slot>
  6. </div>
  7. <div :class="titleCLass">
  8. <b v-if="isShowLine" :class="navBarClass" :style="navBarStyle"></b>
  9. <span
  10. v-for="(value,index) in tabTitleList"
  11. :key="index"
  12. :class="[titleNavList,'nut-title-nav',{'nut-tab-disable':value.disable},{'nut-tab-active' : activeIndex == index}]"
  13. >
  14. <a class="nut-tab-link" v-on:click="switchTab(index,$event,value.disable)">
  15. <i class="nut-tab-icon" :style="{backgroundImage: 'url('+value.iconUrl+')'}" v-if="value.iconUrl"></i>
  16. {{value.tabTitle}}
  17. </a>
  18. </span>
  19. </div>
  20. <div v-if="positionNav=='top' || positionNav=='left'" class="nut-tab-item" ref="items">
  21. <slot></slot>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. name:'nut-tab',
  29. props: {
  30. 'isShowLine':{
  31. type:Boolean,
  32. default:true
  33. },
  34. 'defIndex':{
  35. type:Number,
  36. default:0
  37. },
  38. 'positionNav':{
  39. type:String,
  40. default:'top'
  41. },
  42. 'initData':{
  43. type:Array,
  44. default:function(){
  45. return [];
  46. }
  47. }
  48. },
  49. data() {
  50. return {
  51. tabTitleList:[],
  52. activeIndex:this.defIndex,
  53. initX:'0',
  54. navWidth:0,
  55. };
  56. },
  57. watch:{
  58. defIndex(){
  59. this.updeteTab();
  60. },
  61. initData:{
  62. handler(){
  63. this.updeteTab();
  64. },
  65. deep:true
  66. }
  67. },
  68. computed:{
  69. //下面有些样式名称是为了兼容之前的版本
  70. positionNavCss:function(){
  71. if(this.positionNav==='left' || this.positionNav==='right') return true;
  72. },
  73. titleCLass:function() {
  74. if(this.positionNav == 'top'){
  75. return "nut-tab-title"
  76. }
  77. return "nut-tab-title-" + this.positionNav +"nav";
  78. },
  79. navBarClass:function() {
  80. if(this.positionNav == 'top'){
  81. return "nav-bar"
  82. }
  83. return "nav-bar-"+ this.positionNav;
  84. },
  85. titleNavList:function(){
  86. if(this.positionNav == 'top' || this.positionNav == 'bottom'){
  87. return "nut-title-nav-list"
  88. }
  89. return "nut-title-nav-"+ this.positionNav + 'nav';
  90. },
  91. navBarStyle:function(){
  92. if(this.positionNav==="top"||this.positionNav==="bottom"){
  93. return {
  94. "transform": `translateX(${this.initX}px)`,
  95. "width": this.navWidth+'px'
  96. }
  97. }else{
  98. return {
  99. "transform": `translateY(${this.initX}px)`,
  100. "height": this.navWidth+'px'
  101. }
  102. }
  103. }
  104. },
  105. mounted() {
  106. this.$nextTick(()=>{
  107. this.$slots.default && this.updeteTab(this.$slots.default);
  108. })
  109. },
  110. methods: {
  111. updeteTab:function(){
  112. this.$nextTick(()=>{;
  113. this.tabTitleList = [];
  114. this.activeIndex = this.defIndex;
  115. this.initTab([...this.$slots.default]);
  116. });
  117. },
  118. initTab:function(slot){
  119. for(let i = 0; i < slot.length; i++) {
  120. let slotTag = slot[i].tag;
  121. if(typeof(slotTag)=='string' && slotTag.indexOf('nut-tab-panel') != -1) {
  122. let attrs = slot[i].data.attrs;
  123. let item ={
  124. 'tabTitle':attrs['tab-title'] || attrs['tabTitle'],
  125. 'disable':attrs.disable===false,
  126. 'iconUrl':attrs['iconUrl'] || attrs['icon-url'],
  127. }
  128. this.tabTitleList.push(item);
  129. let slotElm = slot[i].elm;
  130. if(slotElm){
  131. slotElm.classList.add('hide');
  132. if(this.activeIndex == i) {
  133. slotElm.classList.remove('hide');
  134. }
  135. }
  136. }
  137. }
  138. this.$nextTick(()=>{
  139. this.getTabWidth();
  140. })
  141. },
  142. getStyle:function(obj,styleName){
  143. if(!obj){
  144. return ''
  145. }
  146. if(obj.currentStyle){
  147. return obj.currentStyle[styleName];
  148. }else{
  149. return getComputedStyle(obj,null)[styleName];
  150. }
  151. },
  152. getTabWidth:function(){
  153. if(this.positionNav=='top' || this.positionNav=='bottom'){
  154. let tabTitle = document.querySelector('.nut-tab-title');
  155. let tabWidth = this.getStyle(tabTitle,'width');
  156. this.setInitX(tabWidth);
  157. }else{
  158. let tabTitle = document.querySelector('.nut-tab-title-leftnav')|| document.querySelector('.nut-tab-title-rightnav');
  159. let tabWidth = this.getStyle(tabTitle,'height');
  160. this.setInitX(tabWidth);
  161. }
  162. },
  163. setInitX(tabWidth){
  164. let tabWidthNum = tabWidth.substring(0,tabWidth.length-2);
  165. let navBarWidth = (tabWidthNum/this.tabTitleList.length);
  166. this.navWidth = navBarWidth;
  167. this.initX= parseInt(this.navWidth * this.defIndex);
  168. },
  169. findParent(event,myclass){
  170. let parentCpt = event.target;
  171. let flag = 0;//避免死循环
  172. while (parentCpt && flag<10) {
  173. flag++;
  174. if (parentCpt.className && parentCpt.className === myclass) {
  175. break;
  176. }
  177. parentCpt = parentCpt.parentNode;
  178. }
  179. return parentCpt;
  180. },
  181. switchTab:function(index,event,disable){
  182. if(!disable){
  183. this.activeIndex=index;
  184. this.initX= parseInt(this.navWidth * index);
  185. let nutTab = this.findParent(event,'nut-tab-part');
  186. let items = this.$refs.items.children;
  187. for(let i=0;i<items.length;i++){
  188. if(i==index){
  189. items[i].classList.remove('hide');
  190. }else{
  191. items[i].classList.add('hide');
  192. }
  193. }
  194. this.$emit('tab-switch',index,event);
  195. this.$emit('tabSwitch',index,event); //兼容以前驼峰法命名
  196. }
  197. }
  198. }
  199. }
  200. </script>