badge.wxs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 += 'top:' + (hasUnit(props.offset[0].toString()) ? props.offset[0] : props.offset[0] + 'px') + ';';
  28. }
  29. if (props.offset[1]) {
  30. styleStr += 'right:' + (hasUnit(props.offset[1].toString()) ? props.offset[1] : props.offset[1] + 'px') + ';';
  31. }
  32. return styleStr;
  33. };
  34. var getBadgeOuterClass = function (props) {
  35. var baseClass = 't-badge';
  36. var classNames = [baseClass, props.shape === 'ribbon' ? baseClass + '__ribbon--outer' : ''];
  37. return classNames.join(' ');
  38. };
  39. var getBadgeInnerClass = function (props) {
  40. var baseClass = 't-badge';
  41. var classNames = [
  42. baseClass + '--basic',
  43. props.dot ? baseClass + '--dot' : '',
  44. props.size === 'small' ? baseClass + '--small' : '',
  45. baseClass + '--' + props.shape,
  46. !props.dot && props.count ? baseClass + '--count' : '',
  47. ];
  48. return classNames.join(' ');
  49. };
  50. var isShowBadge = function (props) {
  51. if (props.dot) {
  52. return true;
  53. }
  54. if (!props.showZero && !isNaN(props.count) && parseInt(props.count) === 0) {
  55. return false;
  56. }
  57. return true;
  58. };
  59. module.exports.getBadgeValue = getBadgeValue;
  60. module.exports.getBadgeStyles = getBadgeStyles;
  61. module.exports.getBadgeOuterClass = getBadgeOuterClass;
  62. module.exports.getBadgeInnerClass = getBadgeInnerClass;
  63. module.exports.isShowBadge = isShowBadge;