mui.target.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * mui target(action>popover>modal>tab>toggle)
  3. */
  4. (function($, window, document) {
  5. /**
  6. * targets
  7. */
  8. $.targets = {};
  9. /**
  10. * target handles
  11. */
  12. $.targetHandles = [];
  13. /**
  14. * register target
  15. * @param {type} target
  16. * @returns {$.targets}
  17. */
  18. $.registerTarget = function(target) {
  19. target.index = target.index || 1000;
  20. $.targetHandles.push(target);
  21. $.targetHandles.sort(function(a, b) {
  22. return a.index - b.index;
  23. });
  24. return $.targetHandles;
  25. };
  26. window.addEventListener($.EVENT_START, function(event) {
  27. var target = event.target;
  28. var founds = {};
  29. for (; target && target !== document; target = target.parentNode) {
  30. var isFound = false;
  31. $.each($.targetHandles, function(index, targetHandle) {
  32. var name = targetHandle.name;
  33. if (!isFound && !founds[name] && targetHandle.hasOwnProperty('handle')) {
  34. $.targets[name] = targetHandle.handle(event, target);
  35. if ($.targets[name]) {
  36. founds[name] = true;
  37. if (targetHandle.isContinue !== true) {
  38. isFound = true;
  39. }
  40. }
  41. } else {
  42. if (!founds[name]) {
  43. if (targetHandle.isReset !== false)
  44. $.targets[name] = false;
  45. }
  46. }
  47. });
  48. if (isFound) {
  49. break;
  50. }
  51. }
  52. });
  53. window.addEventListener('click', function(event) { //解决touch与click的target不一致的问题(比如链接边缘点击时,touch的target为html,而click的target为A)
  54. var target = event.target;
  55. var isFound = false;
  56. for (; target && target !== document; target = target.parentNode) {
  57. if (target.tagName === 'A') {
  58. $.each($.targetHandles, function(index, targetHandle) {
  59. var name = targetHandle.name;
  60. if (targetHandle.hasOwnProperty('handle')) {
  61. if (targetHandle.handle(event, target)) {
  62. isFound = true;
  63. event.preventDefault();
  64. return false;
  65. }
  66. }
  67. });
  68. if (isFound) {
  69. break;
  70. }
  71. }
  72. }
  73. });
  74. })(mui, window, document);