segmented-controllers.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * segmented-controllers
  3. * @param {type} $
  4. * @param {type} window
  5. * @param {type} document
  6. * @param {type} undefined
  7. * @returns {undefined}
  8. */
  9. (function($, window, document, name, undefined) {
  10. var CLASS_CONTROL_ITEM = $.className('control-item');
  11. var CLASS_SEGMENTED_CONTROL = $.className('segmented-control');
  12. var CLASS_SEGMENTED_CONTROL_VERTICAL = $.className('segmented-control-vertical');
  13. var CLASS_CONTROL_CONTENT = $.className('control-content');
  14. var CLASS_TAB_BAR = $.className('bar-tab');
  15. var CLASS_TAB_ITEM = $.className('tab-item');
  16. var CLASS_SLIDER_ITEM = $.className('slider-item');
  17. var handle = function(event, target) {
  18. if (target.classList && (target.classList.contains(CLASS_CONTROL_ITEM) || target.classList.contains(CLASS_TAB_ITEM))) {
  19. if (target.parentNode && target.parentNode.classList && target.parentNode.classList.contains(CLASS_SEGMENTED_CONTROL_VERTICAL)) {
  20. //vertical 如果preventDefault会导致无法滚动
  21. } else {
  22. event.preventDefault();
  23. // if(target.tagName == 'A') {
  24. // // fixed 底部选项卡href 无法跳转 && stop hash change
  25. // var curr_href = location.hostname + location.pathname,
  26. // target_href = target.hostname + target.pathname;
  27. // if (curr_href == target_href && target.hash !== "") {
  28. // event.preventDefault();
  29. // return target;
  30. // }else{
  31. // return false
  32. // }
  33. // }
  34. }
  35. // if (target.hash) {
  36. return target;
  37. // }
  38. }
  39. return false;
  40. };
  41. $.registerTarget({
  42. name: name,
  43. index: 80,
  44. handle: handle,
  45. target: false
  46. });
  47. window.addEventListener('tap', function(e) {
  48. var targetTab = $.targets.tab;
  49. if (!targetTab) {
  50. return;
  51. }
  52. var activeTab;
  53. var activeBodies;
  54. var targetBody;
  55. var className = $.className('active');
  56. var classSelector = '.' + className;
  57. var segmentedControl = targetTab.parentNode;
  58. for (; segmentedControl && segmentedControl !== document; segmentedControl = segmentedControl.parentNode) {
  59. if (segmentedControl.classList.contains(CLASS_SEGMENTED_CONTROL)) {
  60. activeTab = segmentedControl.querySelector(classSelector + '.' + CLASS_CONTROL_ITEM);
  61. break;
  62. } else if (segmentedControl.classList.contains(CLASS_TAB_BAR)) {
  63. activeTab = segmentedControl.querySelector(classSelector + '.' + CLASS_TAB_ITEM);
  64. }
  65. }
  66. if (activeTab) {
  67. activeTab.classList.remove(className);
  68. }
  69. var isLastActive = targetTab === activeTab;
  70. if (targetTab) {
  71. targetTab.classList.add(className);
  72. }
  73. if (!targetTab.hash) {
  74. return;
  75. }
  76. targetBody = document.getElementById(targetTab.hash.replace('#', ''));
  77. if (!targetBody) {
  78. return;
  79. }
  80. if (!targetBody.classList.contains(CLASS_CONTROL_CONTENT)) { //tab bar popover
  81. targetTab.classList[isLastActive ? 'remove' : 'add'](className);
  82. return;
  83. }
  84. if (isLastActive) { //same
  85. return;
  86. }
  87. var parentNode = targetBody.parentNode;
  88. activeBodies = parentNode.querySelectorAll('.' + CLASS_CONTROL_CONTENT + classSelector);
  89. for (var i = 0; i < activeBodies.length; i++) {
  90. var activeBody = activeBodies[i];
  91. activeBody.parentNode === parentNode && activeBody.classList.remove(className);
  92. }
  93. targetBody.classList.add(className);
  94. var contents = [];
  95. var _contents = parentNode.querySelectorAll('.' + CLASS_CONTROL_CONTENT);
  96. for (var i = 0; i < _contents.length; i++) { //查找直属子节点
  97. _contents[i].parentNode === parentNode && (contents.push(_contents[i]));
  98. }
  99. $.trigger(targetBody, $.eventName('shown', name), {
  100. tabNumber: Array.prototype.indexOf.call(contents, targetBody)
  101. });
  102. e.detail && e.detail.gesture.preventDefault(); //fixed hashchange
  103. });
  104. })(mui, window, document, 'tab');