index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. Component({
  2. properties: {
  3. text: String,
  4. color: {
  5. type: String,
  6. value: '#fff'
  7. },
  8. backgroundColor: {
  9. type: String,
  10. value: '#e64340'
  11. },
  12. duration: {
  13. type: Number,
  14. value: 3000
  15. }
  16. },
  17. methods: {
  18. show() {
  19. const { duration } = this.data;
  20. clearTimeout(this.timer);
  21. this.setData({
  22. show: true
  23. });
  24. if (duration > 0 && duration !== Infinity) {
  25. this.timer = setTimeout(() => {
  26. this.hide();
  27. }, duration);
  28. }
  29. },
  30. hide() {
  31. clearTimeout(this.timer);
  32. this.setData({
  33. show: false
  34. });
  35. }
  36. }
  37. });
  38. const defaultOptions = {
  39. selector: '#van-notify',
  40. duration: 3000
  41. };
  42. function Notify(options = {}) {
  43. const pages = getCurrentPages();
  44. const ctx = pages[pages.length - 1];
  45. options = Object.assign({}, defaultOptions, parseParam(options));
  46. const el = ctx.selectComponent(options.selector);
  47. delete options.selector;
  48. if (el) {
  49. el.setData({
  50. ...options
  51. });
  52. el.show();
  53. }
  54. }
  55. function parseParam(params = '') {
  56. return typeof params === 'object' ? params : { text: params };
  57. }
  58. module.exports = Notify;