badge.wxs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var getBadgeValue = function (props) {
  2. if (props.dot) {
  3. return '';
  4. }
  5. if (isNaN(props.count) || isNaN(props.maxCount)) {
  6. return props.count;
  7. }
  8. return parseInt(props.count) > props.maxCount ? props.maxCount + '+' : props.count;
  9. };
  10. var hasUnit = function (unit) {
  11. return (
  12. unit.indexOf('px') > 0 ||
  13. unit.indexOf('rpx') > 0 ||
  14. unit.indexOf('em') > 0 ||
  15. unit.indexOf('rem') > 0 ||
  16. unit.indexOf('%') > 0 ||
  17. unit.indexOf('vh') > 0 ||
  18. unit.indexOf('vm') > 0
  19. );
  20. };
  21. var getBadgeStyles = function (props) {
  22. var styleStr = '';
  23. if (props.color) {
  24. styleStr += 'background:' + props.color + ';';
  25. }
  26. if (props.offset[0]) {
  27. styleStr +=
  28. 'left: calc(100% + ' + (hasUnit(props.offset[0].toString()) ? props.offset[0] : props.offset[0] + 'px') + ');';
  29. }
  30. if (props.offset[1]) {
  31. styleStr += 'top:' + (hasUnit(props.offset[1].toString()) ? props.offset[1] : props.offset[1] + 'px') + ';';
  32. }
  33. return styleStr;
  34. };
  35. var getBadgeOuterClass = function (props) {
  36. var baseClass = 't-badge';
  37. var classNames = [baseClass, props.shape === 'ribbon' ? baseClass + '__ribbon-outer' : ''];
  38. return classNames.join(' ');
  39. };
  40. var getBadgeInnerClass = function (props) {
  41. var baseClass = 't-badge';
  42. var classNames = [
  43. baseClass + '--basic',
  44. props.dot ? baseClass + '--dot' : '',
  45. baseClass + '--' + props.size,
  46. baseClass + '--' + props.shape,
  47. !props.dot && props.count ? baseClass + '--count' : '',
  48. ];
  49. return classNames.join(' ');
  50. };
  51. var isShowBadge = function (props) {
  52. if (props.dot) {
  53. return true;
  54. }
  55. if (!props.showZero && !isNaN(props.count) && parseInt(props.count) === 0) {
  56. return false;
  57. }
  58. if (props.count == null) return false;
  59. return true;
  60. };
  61. module.exports.getBadgeValue = getBadgeValue;
  62. module.exports.getBadgeStyles = getBadgeStyles;
  63. module.exports.getBadgeOuterClass = getBadgeOuterClass;
  64. module.exports.getBadgeInnerClass = getBadgeInnerClass;
  65. module.exports.isShowBadge = isShowBadge;