notice.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * 专注于提醒的js
  3. * 依赖 util.js;eventBus.js
  4. * 青格勒 2021年6月10日
  5. *
  6. *
  7. * 静默模式下只提示错误和需要确认的操作,其他提醒不提示
  8. */
  9. function GetyteLength(str) {
  10. var length = 0
  11. Array.from(str).map(function (char) {
  12. if (char.charCodeAt(0) > 255) {
  13. //字符编码大于255,说明是双字节字符
  14. length += 2
  15. } else {
  16. length++
  17. }
  18. })
  19. return length
  20. }
  21. /**
  22. *
  23. * @param {String} message required
  24. * @param {Enum} type success|warning|error
  25. * @param {Number} duration default 3 单位秒
  26. */
  27. function ShowMessage(
  28. message = '',
  29. type = 'default',
  30. duration = 3,
  31. lock = false
  32. ) {
  33. if (!message) return
  34. if (IsSilentMode() && ['default', 'success'].includes(type)) return
  35. let width = Math.max(120, GetyteLength(message) * 7 + 32)
  36. if (type !== 'default') width += 32
  37. //同步调用会堵塞,造成wps崩溃
  38. setTimeout(() => {
  39. wps.ShowDialog(
  40. GetResourcePath() +
  41. `share/ui/message.html?message=${
  42. message && encodeURIComponent(message)
  43. }&type=${type}&duration=${duration}`,
  44. 'message',
  45. width * window.devicePixelRatio,
  46. 42 * window.devicePixelRatio,
  47. lock,
  48. false
  49. )
  50. //TODO UOS 试下改成1e2q
  51. }, 1e2)
  52. }
  53. /**
  54. *
  55. * @param {String} message required
  56. * @param {Enum} type success|warning|error
  57. * @param {Number} duration default 3 单位秒
  58. */
  59. function ShowLoading(
  60. message = '',
  61. instid = '',
  62. lock = true,
  63. forceTop = false,
  64. autoclose = 0
  65. ) {
  66. if (IsSilentMode && IsSilentMode()) return
  67. var url =
  68. GetResourcePath() +
  69. `share/ui/loading.html?message=${
  70. message && encodeURIComponent(message)
  71. }&instid=${instid}`
  72. if (autoclose > 0) url += '&autoclose=' + autoclose
  73. // setTimeout(() => {
  74. wps.ShowDialog(
  75. url,
  76. 'loading',
  77. 160 * window.devicePixelRatio,
  78. 160 * window.devicePixelRatio,
  79. lock,
  80. forceTop //强制置顶需要显示Caption才能实现
  81. )
  82. // }, 100)
  83. }
  84. function ShowConfirm(message, type, opts = {}) {
  85. if (!message) return
  86. var instid = uuid(8)
  87. var url =
  88. GetResourcePath() +
  89. `share/ui/confirm.html?message=${
  90. message && encodeURIComponent(message)
  91. }&instid=${instid}`
  92. if (!!type) url += '&type=' + type
  93. if (opts.title) url += '&title=' + opts.title
  94. if (!opts.btns) {
  95. opts.btns = [{ close: true, key: 'default', text: '确定' }]
  96. }
  97. var btns = []
  98. opts.btns.forEach((item) => {
  99. var obj = Object.assign({}, item)
  100. if (obj.close === undefined) obj.close = true
  101. delete obj.callback
  102. btns.push(obj)
  103. if (item.callback)
  104. EventBus.Subscribe(`confirm$${instid}$${item.key}`, item.callback)
  105. })
  106. url += '&btns=' + encodeURIComponent(JSON.stringify(btns))
  107. setTimeout(() => {
  108. wps.ShowDialog(
  109. url,
  110. 'loading',
  111. 460 * window.devicePixelRatio,
  112. 220 * window.devicePixelRatio,
  113. true,
  114. false
  115. )
  116. }, 1e2)
  117. }
  118. /**
  119. * 关闭loading提醒
  120. */
  121. function CloseLoading(instid = '') {
  122. EventBus.Publish(`closeLoading$${instid}`, null)
  123. }