@tarojs/taro-h5 3.3.13 → 3.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/dist/index.cjs.js +6134 -8352
  2. package/dist/index.js +9044 -0
  3. package/dist/taroApis.js +1 -1
  4. package/package.json +9 -9
  5. package/src/api/ad/index.ts +5 -0
  6. package/src/api/ai/facial.ts +6 -0
  7. package/src/api/ai/index.ts +2 -0
  8. package/src/api/ai/visual.ts +4 -0
  9. package/src/api/alipay/index.ts +4 -0
  10. package/src/api/base/crypto.ts +4 -0
  11. package/src/api/base/debug.ts +5 -0
  12. package/src/api/base/index.ts +23 -0
  13. package/src/api/base/performance.ts +5 -0
  14. package/src/api/base/system/index.ts +2 -0
  15. package/src/api/base/system/info.ts +64 -0
  16. package/src/api/base/system/network.ts +70 -0
  17. package/src/api/base/update.ts +5 -0
  18. package/src/api/base/weapp/app-event.ts +19 -0
  19. package/src/api/base/weapp/life-cycle.ts +5 -0
  20. package/src/api/canvas/CanvasContext.ts +230 -0
  21. package/src/api/canvas/canvasGetImageData.ts +29 -0
  22. package/src/api/canvas/canvasPutImageData.ts +26 -0
  23. package/src/api/canvas/canvasToTempFilePath.ts +26 -0
  24. package/src/api/canvas/createCanvasContext.ts +20 -0
  25. package/src/api/canvas/{index.js → index.ts} +9 -3
  26. package/src/api/cloud/index.ts +27 -0
  27. package/src/api/data-analysis/index.ts +6 -0
  28. package/src/api/device/accelerometer.ts +89 -0
  29. package/src/api/device/accessibility.ts +4 -0
  30. package/src/api/device/battery.ts +5 -0
  31. package/src/api/device/bluetooth-ble.ts +19 -0
  32. package/src/api/device/bluetooth-peripheral.ts +6 -0
  33. package/src/api/device/bluetooth.ts +16 -0
  34. package/src/api/device/calendar.ts +5 -0
  35. package/src/api/device/clipboard.ts +67 -0
  36. package/src/api/device/compass.ts +74 -0
  37. package/src/api/device/contact.ts +5 -0
  38. package/src/api/device/crypto.ts +4 -0
  39. package/src/api/device/gyroscope.ts +7 -0
  40. package/src/api/device/iBeacon.ts +10 -0
  41. package/src/api/device/index.ts +22 -0
  42. package/src/api/device/keyboard.ts +7 -0
  43. package/src/api/device/memory.ts +5 -0
  44. package/src/api/device/motion.ts +92 -0
  45. package/src/api/device/nfc.ts +10 -0
  46. package/src/api/device/phone.ts +30 -0
  47. package/src/api/device/scan.ts +7 -0
  48. package/src/api/device/screen.ts +9 -0
  49. package/src/api/device/vibrate.ts +36 -0
  50. package/src/api/device/wifi.ts +13 -0
  51. package/src/api/ext/index.ts +5 -0
  52. package/src/api/files/index.ts +11 -0
  53. package/src/api/framework/index.ts +10 -0
  54. package/src/api/index.ts +25 -0
  55. package/src/api/location/chooseLocation.ts +106 -0
  56. package/src/api/location/index.ts +19 -0
  57. package/src/api/location/style.css +8 -8
  58. package/src/api/media/audio/index.ts +99 -0
  59. package/src/api/media/background-audio.ts +12 -0
  60. package/src/api/media/camera.ts +4 -0
  61. package/src/api/media/editor.ts +29 -0
  62. package/src/api/media/image/chooseImage.ts +80 -0
  63. package/src/api/{image → media/image}/getImageInfo.js +0 -0
  64. package/src/api/media/image/index.ts +13 -0
  65. package/src/api/{image → media/image}/previewImage.js +4 -0
  66. package/src/api/media/index.ts +13 -0
  67. package/src/api/media/live.ts +5 -0
  68. package/src/api/media/map.ts +4 -0
  69. package/src/api/media/media-recorder.ts +4 -0
  70. package/src/api/media/recorder.ts +6 -0
  71. package/src/api/media/video/index.ts +82 -0
  72. package/src/api/media/video-decoder.ts +4 -0
  73. package/src/api/media/video-processing.ts +4 -0
  74. package/src/api/media/voip.ts +17 -0
  75. package/src/api/navigate/index.ts +7 -0
  76. package/src/api/{fileTransfer/downloadFile.js → network/download.ts} +16 -25
  77. package/src/api/network/index.ts +7 -0
  78. package/src/api/network/mdns.ts +13 -0
  79. package/src/api/{request/index.js → network/request/index.ts} +9 -6
  80. package/src/api/network/tcp.ts +4 -0
  81. package/src/api/network/udp.ts +4 -0
  82. package/src/api/{fileTransfer/uploadFile.js → network/upload.ts} +28 -30
  83. package/src/api/{fileTransfer/utils.js → network/utils.ts} +0 -0
  84. package/src/api/{webSocket/index.js → network/websocket/index.ts} +50 -57
  85. package/src/api/{webSocket/socketTask.js → network/websocket/socketTask.ts} +16 -6
  86. package/src/api/open-api/account.ts +4 -0
  87. package/src/api/open-api/address.ts +4 -0
  88. package/src/api/open-api/authorize.ts +5 -0
  89. package/src/api/open-api/card.ts +5 -0
  90. package/src/api/open-api/channels-live.ts +9 -0
  91. package/src/api/open-api/customer-service.ts +4 -0
  92. package/src/api/open-api/facial.ts +7 -0
  93. package/src/api/open-api/favorites.ts +5 -0
  94. package/src/api/open-api/group.ts +4 -0
  95. package/src/api/open-api/index.ts +18 -0
  96. package/src/api/open-api/invoice.ts +5 -0
  97. package/src/api/open-api/license-plate.ts +4 -0
  98. package/src/api/open-api/login.ts +6 -0
  99. package/src/api/open-api/red-package.ts +4 -0
  100. package/src/api/open-api/settings.ts +5 -0
  101. package/src/api/open-api/soter.ts +6 -0
  102. package/src/api/open-api/subscribe-message.ts +4 -0
  103. package/src/api/open-api/user-info.ts +5 -0
  104. package/src/api/open-api/werun.ts +5 -0
  105. package/src/api/payment/index.ts +5 -0
  106. package/src/api/route/index.ts +10 -0
  107. package/src/api/share/index.ts +13 -0
  108. package/src/api/storage/background-fetch.ts +7 -0
  109. package/src/api/storage/index.ts +193 -0
  110. package/src/api/swan/index.ts +31 -0
  111. package/src/{taro/index.js → api/taro.ts} +133 -149
  112. package/src/api/{createAnimation/index.js → ui/animation/index.ts} +26 -14
  113. package/src/api/ui/background.ts +5 -0
  114. package/src/api/ui/custom-component.ts +4 -0
  115. package/src/api/ui/fonts.ts +4 -0
  116. package/src/api/ui/index.ts +12 -0
  117. package/src/api/ui/interaction/actionSheet.ts +212 -0
  118. package/src/api/{interactive/index.js → ui/interaction/index.ts} +102 -90
  119. package/src/api/ui/interaction/modal.ts +237 -0
  120. package/src/api/{interactive/toast.js → ui/interaction/toast.ts} +35 -33
  121. package/src/api/ui/menu.ts +4 -0
  122. package/src/api/ui/navigation-bar/index.ts +52 -0
  123. package/src/api/ui/pull-down-refresh.ts +32 -0
  124. package/src/api/{scroll/index.js → ui/scroll/index.ts} +16 -28
  125. package/src/api/ui/sticky.ts +4 -0
  126. package/src/api/ui/tab-bar.ts +326 -0
  127. package/src/api/ui/window.ts +37 -0
  128. package/src/api/utils/handler.ts +105 -0
  129. package/src/api/utils/{index.js → index.ts} +24 -87
  130. package/src/api/worker/index.ts +4 -0
  131. package/src/api/wxml/index.ts +9 -0
  132. package/src/api/{createSelectorQuery/index.js → wxml/selectorQuery.ts} +86 -59
  133. package/src/index.ts +6 -0
  134. package/src/types/api.d.ts +4 -0
  135. package/src/types/define.d.ts +2 -0
  136. package/{types → src/types}/index.d.ts +0 -0
  137. package/{types → src/types}/type.ts +1 -1
  138. package/src/api/accelerometer/index.js +0 -105
  139. package/src/api/audio/index.js +0 -137
  140. package/src/api/canvas/canvasGetImageData.js +0 -44
  141. package/src/api/canvas/canvasPutImageData.js +0 -45
  142. package/src/api/canvas/canvasToTempFilePath.js +0 -49
  143. package/src/api/canvas/createCanvasContext.js +0 -340
  144. package/src/api/clipboard/index.js +0 -113
  145. package/src/api/compass/index.js +0 -99
  146. package/src/api/deviceMotion/index.js +0 -105
  147. package/src/api/fileTransfer/index.js +0 -2
  148. package/src/api/image/chooseImage.js +0 -87
  149. package/src/api/image/index.js +0 -7
  150. package/src/api/index.js +0 -29
  151. package/src/api/interactive/actionSheet.js +0 -214
  152. package/src/api/interactive/modal.js +0 -256
  153. package/src/api/location/chooseLocation.js +0 -143
  154. package/src/api/location/index.js +0 -5
  155. package/src/api/navigationBar/index.js +0 -57
  156. package/src/api/open/index.js +0 -5
  157. package/src/api/others/index.js +0 -43
  158. package/src/api/pullDownRefresh/index.js +0 -51
  159. package/src/api/storage/index.js +0 -211
  160. package/src/api/system/index.js +0 -9
  161. package/src/api/system/info.js +0 -36
  162. package/src/api/system/network.js +0 -68
  163. package/src/api/tabBar/index.js +0 -424
  164. package/src/api/unsupportedApi/index.js +0 -278
  165. package/src/api/vibrate/index.js +0 -49
  166. package/src/api/video/index.js +0 -87
  167. package/src/api/window/index.js +0 -38
  168. package/src/index.cjs.js +0 -11
  169. package/src/index.js +0 -6
@@ -1,87 +0,0 @@
1
- import { shouleBeObject, getParameterError } from '../utils'
2
-
3
- /**
4
- * 从本地相册选择图片或使用相机拍照。
5
- * @param {Object} object 参数
6
- * @param {string[]} [object.sourceType=['album', 'camera']] 选择图片的来源(h5端未实现)
7
- * @param {string[]} [object.sizeType=['original', 'compressed']] 所选的图片的尺寸(h5端未实现)
8
- * @param {number} [object.count=9] 最多可以选择的图片张数
9
- * @param {function} [object.success] 接口调用成功的回调函数
10
- * @param {function} [object.fail] 接口调用失败的回调函数
11
- * @param {function} [object.complete] 接口调用结束的回调函数(调用成功、失败都会执行)
12
- * @param {string} [object.imageId] 用来上传的input元素ID(仅h5端)
13
- */
14
- const chooseImage = function (options) {
15
- // options must be an Object
16
- const isObject = shouleBeObject(options)
17
- if (!isObject.res) {
18
- const res = { errMsg: `chooseImage${isObject.msg}` }
19
- console.error(res.errMsg)
20
- return Promise.reject(res)
21
- }
22
-
23
- const { count = 1, success, fail, complete, imageId = 'taroChooseImage' } = options
24
- const res = {
25
- errMsg: 'chooseImage:ok',
26
- tempFilePaths: [],
27
- tempFiles: []
28
- }
29
-
30
- if (count && typeof count !== 'number') {
31
- res.errMsg = getParameterError({
32
- name: 'chooseImage',
33
- para: 'count',
34
- correct: 'Number',
35
- wrong: count
36
- })
37
- console.error(res.errMsg)
38
- typeof fail === 'function' && fail(res)
39
- typeof complete === 'function' && complete(res)
40
- return Promise.reject(res)
41
- }
42
-
43
- let taroChooseImageId = document.getElementById(imageId)
44
- if (!taroChooseImageId) {
45
- const obj = document.createElement('input')
46
- obj.setAttribute('type', 'file')
47
- obj.setAttribute('id', imageId)
48
- if (count > 1) {
49
- obj.setAttribute('multiple', 'multiple')
50
- }
51
- obj.setAttribute('accept', 'image/*')
52
- obj.setAttribute('style', 'position: fixed; top: -4000px; left: -3000px; z-index: -300;')
53
- document.body.appendChild(obj)
54
- taroChooseImageId = document.getElementById(imageId)
55
- } else {
56
- if (count > 1) {
57
- taroChooseImageId.setAttribute('multiple', 'multiple')
58
- } else {
59
- taroChooseImageId.removeAttribute('multiple')
60
- }
61
- }
62
- let taroChooseImageCallback
63
- const taroChooseImagePromise = new Promise(resolve => {
64
- taroChooseImageCallback = resolve
65
- })
66
- const TaroMouseEvents = document.createEvent('MouseEvents')
67
- TaroMouseEvents.initEvent('click', true, true)
68
- taroChooseImageId.dispatchEvent(TaroMouseEvents)
69
- taroChooseImageId.onchange = function (e) {
70
- const arr = [...e.target.files]
71
- arr && arr.forEach(item => {
72
- const blob = new Blob([item], {
73
- type: item.type
74
- })
75
- const url = URL.createObjectURL(blob)
76
- res.tempFilePaths.push(url)
77
- res.tempFiles.push({ path: url, size: item.size, type: item.type, originalFileObj: item })
78
- })
79
- typeof success === 'function' && success(res)
80
- typeof complete === 'function' && complete(res)
81
- taroChooseImageCallback(res)
82
- e.target.value = ''
83
- }
84
- return taroChooseImagePromise
85
- }
86
-
87
- export default chooseImage
@@ -1,7 +0,0 @@
1
- /**
2
- * previewImage api基于开源的React组件[react-wx-images-viewer](https://github.com/react-ld/react-wx-images-viewer)开发,感谢!
3
- */
4
-
5
- export { default as chooseImage } from './chooseImage'
6
- export { default as getImageInfo } from './getImageInfo'
7
- export { previewImage } from './previewImage'
package/src/api/index.js DELETED
@@ -1,29 +0,0 @@
1
- /* 未支持的api */
2
- export * from './unsupportedApi'
3
-
4
- /* 已实现api */
5
- export * from './accelerometer'
6
- export * from './audio'
7
- export * from './canvas'
8
- export * from './clipboard'
9
- export * from './compass'
10
- export * from './createAnimation'
11
- export * from './createSelectorQuery'
12
- export * from './deviceMotion'
13
- export * from './fileTransfer'
14
- export * from './image'
15
- export * from './interactive'
16
- export * from './location'
17
- export * from './navigationBar'
18
- export * from './open'
19
- export * from './others'
20
- export * from './pullDownRefresh'
21
- export * from './request'
22
- export * from './scroll'
23
- export * from './storage'
24
- export * from './system'
25
- export * from './tabBar'
26
- export * from './vibrate'
27
- export * from './video'
28
- export * from './webSocket'
29
- export * from './window'
@@ -1,214 +0,0 @@
1
- import { inlineStyle, setTransform } from '../utils'
2
-
3
- const noop = function () {}
4
- export default class ActionSheet {
5
- options = {
6
- itemList: [],
7
- itemColor: '#000000',
8
- success: noop,
9
- fail: noop,
10
- complete: noop
11
- }
12
-
13
- style = {
14
- maskStyle: {
15
- position: 'fixed',
16
- 'z-index': '1000',
17
- top: '0',
18
- right: '0',
19
- left: '0',
20
- bottom: '0',
21
- background: 'rgba(0,0,0,0.6)'
22
- },
23
- actionSheetStyle: {
24
- 'z-index': '4999',
25
- position: 'fixed',
26
- left: '0',
27
- bottom: '0',
28
- '-webkit-transform': 'translate(0, 100%)',
29
- transform: 'translate(0, 100%)',
30
- width: '100%',
31
- 'line-height': '1.6',
32
- background: '#EFEFF4',
33
- '-webkit-transition': '-webkit-transform .3s',
34
- transition: 'transform .3s'
35
- },
36
- menuStyle: {
37
- 'background-color': '#FCFCFD'
38
- },
39
- cellStyle: {
40
- position: 'relative',
41
- padding: '10px 0',
42
- 'text-align': 'center',
43
- 'font-size': '18px'
44
- },
45
- cancelStyle: {
46
- 'margin-top': '6px',
47
- padding: '10px 0',
48
- 'text-align': 'center',
49
- 'font-size': '18px',
50
- color: '#000000',
51
- 'background-color': '#FCFCFD'
52
- }
53
- }
54
-
55
- lastConfig = {}
56
-
57
- create (options = {}) {
58
- // style
59
- const { maskStyle, actionSheetStyle, menuStyle, cellStyle, cancelStyle } = this.style
60
-
61
- // configuration
62
- const config = {
63
- ...this.options,
64
- ...options
65
- }
66
-
67
- this.lastConfig = config
68
-
69
- // wrapper
70
- this.el = document.createElement('div')
71
- this.el.className = 'taro__actionSheet'
72
- this.el.style.opacity = '0'
73
- this.el.style.transition = 'opacity 0.2s linear'
74
-
75
- // mask
76
- const mask = document.createElement('div')
77
- mask.setAttribute('style', inlineStyle(maskStyle))
78
-
79
- // actionSheet
80
- this.actionSheet = document.createElement('div')
81
- this.actionSheet.setAttribute('style', inlineStyle(actionSheetStyle))
82
-
83
- // menu
84
- this.menu = document.createElement('div')
85
- this.menu.setAttribute('style', inlineStyle({
86
- ...menuStyle,
87
- color: config.itemColor
88
- }))
89
-
90
- // cells
91
- this.cells = config.itemList.map((item, index) => {
92
- const cell = document.createElement('div')
93
- cell.className = 'taro-actionsheet__cell'
94
- cell.setAttribute('style', inlineStyle(cellStyle))
95
- cell.textContent = item
96
- cell.dataset.tapIndex = index
97
- cell.onclick = e => this.onCellClick(e)
98
- return cell
99
- })
100
-
101
- // cancel
102
- this.cancel = document.createElement('div')
103
- this.cancel.setAttribute('style', inlineStyle(cancelStyle))
104
- this.cancel.textContent = '取消'
105
-
106
- // result
107
- this.cells.forEach(item => this.menu.appendChild(item))
108
- this.actionSheet.appendChild(this.menu)
109
- this.actionSheet.appendChild(this.cancel)
110
- this.el.appendChild(mask)
111
- this.el.appendChild(this.actionSheet)
112
-
113
- // callbacks
114
- const cb = () => {
115
- this.hide()
116
- const res = { errMsg: 'showActionSheet:fail cancel' }
117
- config.fail(res)
118
- config.complete(res)
119
- this.rejectHandler(res)
120
- }
121
- mask.onclick = cb
122
- this.cancel.onclick = cb
123
-
124
- // show immediately
125
- document.body.appendChild(this.el)
126
- setTimeout(() => {
127
- this.el.style.opacity = '1'
128
- setTransform(this.actionSheet, 'translate(0, 0)')
129
- }, 0)
130
-
131
- return new Promise((resolve, reject) => {
132
- this.resolveHandler = resolve
133
- this.rejectHandler = reject
134
- })
135
- }
136
-
137
- show (options = {}) {
138
- const config = {
139
- ...this.options,
140
- ...options
141
- }
142
-
143
- this.lastConfig = config
144
-
145
- if (this.hideOpacityTimer) clearTimeout(this.hideOpacityTimer)
146
- if (this.hideDisplayTimer) clearTimeout(this.hideDisplayTimer)
147
-
148
- // itemColor
149
- if (config.itemColor) this.menu.style.color = config.itemColor
150
-
151
- // cells
152
- const { cellStyle } = this.style
153
-
154
- config.itemList.forEach((item, index) => {
155
- let cell
156
- if (this.cells[index]) {
157
- // assign new content
158
- cell = this.cells[index]
159
- } else {
160
- // create new cell
161
- cell = document.createElement('div')
162
- cell.className = 'taro-actionsheet__cell'
163
- cell.setAttribute('style', inlineStyle(cellStyle))
164
- cell.dataset.tapIndex = index
165
- this.cells.push(cell)
166
- this.menu.appendChild(cell)
167
- }
168
- cell.textContent = item
169
- cell.onclick = e => this.onCellClick(e)
170
- })
171
- const cellsLen = this.cells.length
172
- const itemListLen = config.itemList.length
173
- if (cellsLen > itemListLen) {
174
- for (let i = itemListLen; i < cellsLen; i++) {
175
- this.menu.removeChild(this.cells[i])
176
- }
177
- this.cells.splice(itemListLen)
178
- }
179
-
180
- // show
181
- this.el.style.display = 'block'
182
- setTimeout(() => {
183
- this.el.style.opacity = '1'
184
- setTransform(this.actionSheet, 'translate(0, 0)')
185
- }, 0)
186
-
187
- return new Promise((resolve, reject) => {
188
- this.resolveHandler = resolve
189
- this.rejectHandler = reject
190
- })
191
- }
192
-
193
- onCellClick (e) {
194
- this.hide()
195
- const res = {
196
- errMsg: 'showActionSheet:ok',
197
- tapIndex: +e.currentTarget.dataset.tapIndex
198
- }
199
- this.lastConfig.success && this.lastConfig.success(res)
200
- this.lastConfig.complete && this.lastConfig.complete(res)
201
- this.resolveHandler(res)
202
- }
203
-
204
- hide () {
205
- if (this.hideOpacityTimer) clearTimeout(this.hideOpacityTimer)
206
- if (this.hideDisplayTimer) clearTimeout(this.hideDisplayTimer)
207
-
208
- this.hideOpacityTimer = setTimeout(() => {
209
- this.el.style.opacity = '0'
210
- setTransform(this.actionSheet, 'translate(0, 100%)')
211
- this.hideDisplayTimer = setTimeout(() => { this.el.style.display = 'none' }, 200)
212
- }, 0)
213
- }
214
- }
@@ -1,256 +0,0 @@
1
- import { inlineStyle } from '../utils'
2
-
3
- const noop = function () {}
4
-
5
- export default class Modal {
6
- options = {
7
- title: '',
8
- content: '',
9
- showCancel: true,
10
- cancelText: '取消',
11
- cancelColor: '#000000',
12
- confirmText: '确定',
13
- confirmColor: '#3CC51F',
14
- success: noop,
15
- fail: noop,
16
- complete: noop
17
- }
18
-
19
- style = {
20
- maskStyle: {
21
- position: 'fixed',
22
- 'z-index': '1000',
23
- top: '0',
24
- right: '0',
25
- left: '0',
26
- bottom: '0',
27
- background: 'rgba(0,0,0,0.6)'
28
- },
29
- modalStyle: {
30
- 'z-index': '4999',
31
- position: 'fixed',
32
- top: '50%',
33
- left: '50%',
34
- transform: 'translate(-50%, -50%)',
35
- width: '80%',
36
- 'max-width': '300px',
37
- 'border-radius': '3px',
38
- 'text-align': 'center',
39
- 'line-height': '1.6',
40
- overflow: 'hidden',
41
- background: '#FFFFFF'
42
- },
43
- titleStyle: {
44
- padding: '20px 24px 9px',
45
- 'font-size': '18px'
46
- },
47
- textStyle: {
48
- padding: '0 24px 12px',
49
- 'min-height': '40px',
50
- 'font-size': '15px',
51
- 'line-height': '1.3',
52
- color: '#808080'
53
- },
54
- footStyle: {
55
- position: 'relative',
56
- 'line-height': '48px',
57
- 'font-size': '18px',
58
- display: 'flex'
59
- },
60
- btnStyle: {
61
- position: 'relative',
62
- '-webkit-box-flex': '1',
63
- '-webkit-flex': '1',
64
- flex: '1'
65
- }
66
- }
67
-
68
- create (options = {}) {
69
- // style
70
- const { maskStyle, modalStyle, titleStyle, textStyle, footStyle, btnStyle } = this.style
71
-
72
- // configuration
73
- const config = {
74
- ...this.options,
75
- ...options
76
- }
77
-
78
- // wrapper
79
- this.el = document.createElement('div')
80
- this.el.className = 'taro__modal'
81
- this.el.style.opacity = '0'
82
- this.el.style.transition = 'opacity 0.2s linear'
83
-
84
- // mask
85
- const mask = document.createElement('div')
86
- mask.className = 'taro-modal__mask'
87
- mask.setAttribute('style', inlineStyle(maskStyle))
88
-
89
- // modal
90
- const modal = document.createElement('div')
91
- modal.className = 'taro-modal__content'
92
- modal.setAttribute('style', inlineStyle(modalStyle))
93
-
94
- // title
95
- const titleCSS = config.title ? titleStyle : {
96
- ...titleStyle,
97
- display: 'none'
98
- }
99
- this.title = document.createElement('div')
100
-
101
- this.title.className = 'taro-modal__title'
102
- this.title.setAttribute('style', inlineStyle(titleCSS))
103
- this.title.textContent = config.title
104
-
105
- // text
106
- const textCSS = config.title ? textStyle : {
107
- ...textStyle,
108
- padding: '40px 20px 26px',
109
- color: '#353535'
110
- }
111
- this.text = document.createElement('div')
112
- this.text.className = 'taro-modal__text'
113
- this.text.setAttribute('style', inlineStyle(textCSS))
114
- this.text.textContent = config.content
115
-
116
- // foot
117
- const foot = document.createElement('div')
118
- foot.className = 'taro-modal__foot'
119
- foot.setAttribute('style', inlineStyle(footStyle))
120
-
121
- // cancel button
122
- const cancelCSS = {
123
- ...btnStyle,
124
- color: config.cancelColor,
125
- display: config.showCancel ? 'block' : 'none'
126
- }
127
- this.cancel = document.createElement('div')
128
- this.cancel.className = 'taro-model__btn taro-model__cancel'
129
- this.cancel.setAttribute('style', inlineStyle(cancelCSS))
130
- this.cancel.textContent = config.cancelText
131
- this.cancel.onclick = () => {
132
- this.hide()
133
- const res = this.getRes('cancel')
134
- config.success(res)
135
- config.complete(res)
136
- this.resolveHandler(res)
137
- }
138
-
139
- // confirm button
140
- this.confirm = document.createElement('div')
141
- this.confirm.className = 'taro-model__btn taro-model__confirm'
142
- this.confirm.setAttribute('style', inlineStyle(btnStyle))
143
- this.confirm.style.color = config.confirmColor
144
- this.confirm.textContent = config.confirmText
145
- this.confirm.onclick = () => {
146
- this.hide()
147
- const res = this.getRes('confirm')
148
- config.success(res)
149
- config.complete(res)
150
- this.resolveHandler(res)
151
- }
152
-
153
- // result
154
- foot.appendChild(this.cancel)
155
- foot.appendChild(this.confirm)
156
- modal.appendChild(this.title)
157
- modal.appendChild(this.text)
158
- modal.appendChild(foot)
159
- this.el.appendChild(mask)
160
- this.el.appendChild(modal)
161
-
162
- // show immediately
163
- document.body.appendChild(this.el)
164
- setTimeout(() => { this.el.style.opacity = '1' }, 0)
165
-
166
- return new Promise(resolve => (this.resolveHandler = resolve))
167
- }
168
-
169
- getRes (type) {
170
- const res = {
171
- errMsg: 'showModal:ok',
172
- cancel: false,
173
- confirm: false
174
- }
175
- res[type] = true
176
- return res
177
- }
178
-
179
- show (options = {}) {
180
- const config = {
181
- ...this.options,
182
- ...options
183
- }
184
-
185
- if (this.hideOpacityTimer) clearTimeout(this.hideOpacityTimer)
186
- if (this.hideDisplayTimer) clearTimeout(this.hideDisplayTimer)
187
-
188
- // title & text
189
- const { textStyle } = this.style
190
-
191
- if (config.title) {
192
- this.title.textContent = config.title
193
- // none => block
194
- this.title.style.display = 'block'
195
- this.text.setAttribute('style', inlineStyle(textStyle))
196
- } else {
197
- // block => none
198
- this.title.style.display = 'none'
199
- const textCSS = {
200
- ...textStyle,
201
- padding: '40px 20px 26px',
202
- color: '#353535'
203
- }
204
- this.text.setAttribute('style', inlineStyle(textCSS))
205
- }
206
-
207
- this.text.textContent = config.content || ''
208
-
209
- // showCancel
210
- this.cancel.style.display = config.showCancel ? 'block' : 'none'
211
-
212
- // cancelText
213
- this.cancel.textContent = config.cancelText || ''
214
-
215
- // cancelColor
216
- this.cancel.style.color = config.cancelColor || undefined
217
-
218
- // confirmText
219
- this.confirm.textContent = config.confirmText || ''
220
-
221
- // confirmColor
222
- this.confirm.style.color = config.confirmColor || undefined
223
-
224
- // cbs
225
- this.cancel.onclick = () => {
226
- this.hide()
227
- const res = this.getRes('cancel')
228
- config.success(res)
229
- config.complete(res)
230
- this.resolveHandler(res)
231
- }
232
- this.confirm.onclick = () => {
233
- this.hide()
234
- const res = this.getRes('confirm')
235
- config.success(res)
236
- config.complete(res)
237
- this.resolveHandler(res)
238
- }
239
-
240
- // show
241
- this.el.style.display = 'block'
242
- setTimeout(() => { this.el.style.opacity = '1' }, 0)
243
-
244
- return new Promise(resolve => (this.resolveHandler = resolve))
245
- }
246
-
247
- hide () {
248
- if (this.hideOpacityTimer) clearTimeout(this.hideOpacityTimer)
249
- if (this.hideDisplayTimer) clearTimeout(this.hideDisplayTimer)
250
-
251
- this.hideOpacityTimer = setTimeout(() => {
252
- this.el.style.opacity = '0'
253
- this.hideDisplayTimer = setTimeout(() => { this.el.style.display = 'none' }, 200)
254
- }, 0)
255
- }
256
- }