index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view :class="classes" @click="handleClick">
  3. <div
  4. v-if="destroy"
  5. :class="[
  6. 'nut-dialog-wrapper',
  7. customClass,
  8. { 'nut-dialog-image-wrapper': type === 'image' }
  9. ]"
  10. :id="id"
  11. >
  12. <transition :name="animation ? 'nutFade' : ''">
  13. <div
  14. :class="'nut-dialog-mask'"
  15. :style="{ background: maskBgStyle }"
  16. @click="modalClick"
  17. v-show="curVisible"
  18. ></div>
  19. </transition>
  20. <transition :name="animation ? 'nutEase' : ''">
  21. <div class="nut-dialog-box" v-show="curVisible" @click="modalClick">
  22. <div class="nut-dialog" @click.stop>
  23. <a
  24. href="javascript:;"
  25. v-if="closeBtn"
  26. @click="closeBtnClick"
  27. class="nut-dialog-close"
  28. ></a>
  29. <template v-if="type === 'image'">
  30. <a
  31. href="javascript:;"
  32. @click="imageLinkClick"
  33. class="nut-dialog-link"
  34. >
  35. <img :src="imgSrc" class="nut-dialog-image" alt />
  36. </a>
  37. </template>
  38. <template v-else>
  39. <div class="nut-dialog-body">
  40. <span
  41. class="nut-dialog-title"
  42. v-html="title"
  43. v-if="title"
  44. ></span>
  45. <div
  46. class="nut-dialog-content"
  47. v-if="isShowContent"
  48. :style="{ textAlign }"
  49. >
  50. <slot></slot>
  51. </div>
  52. <div
  53. class="nut-dialog-content"
  54. v-html="content"
  55. v-else-if="content"
  56. :style="{ textAlign }"
  57. ></div>
  58. </div>
  59. <div class="nut-dialog-footer" v-if="!noFooter">
  60. <button
  61. class="nut-dialog-btn nut-dialog-cancel"
  62. v-if="!noCancelBtn"
  63. @click="cancelBtnClick(cancelAutoClose)"
  64. >{{ cancelBtnTxt }}</button
  65. >
  66. <button
  67. class="nut-dialog-btn nut-dialog-ok"
  68. v-if="!noOkBtn"
  69. :class="{ disabled: okBtnDisabled }"
  70. :disabled="okBtnDisabled"
  71. @click="okBtnClick"
  72. >{{ okBtnTxt }}</button
  73. >
  74. </div>
  75. </template>
  76. </div>
  77. </div>
  78. </transition>
  79. </div>
  80. </view>
  81. </template>
  82. <script lang="ts">
  83. import { ref, onMounted, watch, watchEffect, computed } from 'vue';
  84. import { createComponent } from '@/utils/create';
  85. const { componentName, create } = createComponent('dialog');
  86. const lockMaskScroll = (bodyCls => {
  87. let scrollTop = 0;
  88. return {
  89. afterOpen: function() {
  90. scrollTop =
  91. (document.scrollingElement && document.scrollingElement.scrollTop) ||
  92. document.body.scrollTop;
  93. document.body.classList.add(bodyCls);
  94. document.body.style.top = -scrollTop + 'px';
  95. },
  96. beforeClose: function() {
  97. if (document.body.classList.contains(bodyCls)) {
  98. document.body.classList.remove(bodyCls);
  99. if (document.scrollingElement) {
  100. document.scrollingElement.scrollTop = scrollTop;
  101. }
  102. }
  103. }
  104. };
  105. })('dialog-open');
  106. export default create({
  107. props: {
  108. id: {
  109. type: String,
  110. default: ''
  111. },
  112. title: {
  113. type: String,
  114. default: ''
  115. },
  116. content: {
  117. type: String,
  118. default: ''
  119. },
  120. type: {
  121. type: String,
  122. default: ''
  123. },
  124. link: {
  125. type: String,
  126. default: ''
  127. },
  128. imgSrc: {
  129. type: String,
  130. default: ''
  131. },
  132. animation: {
  133. type: Boolean,
  134. default: true
  135. },
  136. lockBgScroll: {
  137. type: Boolean,
  138. default: false
  139. },
  140. visible: {
  141. type: Boolean,
  142. default: false
  143. },
  144. closeBtn: {
  145. type: Boolean,
  146. default: false
  147. },
  148. closeOnClickModal: {
  149. type: Boolean,
  150. default: true
  151. },
  152. noFooter: {
  153. type: Boolean,
  154. default: false
  155. },
  156. noOkBtn: {
  157. type: Boolean,
  158. default: false
  159. },
  160. noCancelBtn: {
  161. type: Boolean,
  162. default: false
  163. },
  164. cancelBtnTxt: {
  165. type: String,
  166. default: '取消'
  167. },
  168. okBtnTxt: {
  169. type: String,
  170. default: '确定'
  171. },
  172. okBtnDisabled: {
  173. type: Boolean,
  174. default: false
  175. },
  176. cancelAutoClose: {
  177. type: Boolean,
  178. default: true
  179. },
  180. textAlign: {
  181. type: String,
  182. default: 'center'
  183. },
  184. onOkBtn: {
  185. type: Function,
  186. default: null
  187. },
  188. onCloseBtn: {
  189. type: Function,
  190. default: null
  191. },
  192. onCancelBtn: {
  193. type: Function,
  194. default: null
  195. },
  196. closeCallback: {
  197. type: Function,
  198. default: null
  199. },
  200. onClickImageLink: {
  201. type: Function,
  202. default: null
  203. },
  204. maskBgStyle: {
  205. type: String,
  206. default: ''
  207. },
  208. canDestroy: {
  209. type: Boolean,
  210. default: true
  211. },
  212. customClass: {
  213. type: String,
  214. default: ''
  215. },
  216. closeOnPopstate: {
  217. type: Boolean,
  218. default: false
  219. }
  220. },
  221. // emits: ['click'],
  222. setup(props, { emit, slots }) {
  223. const curVisible = ref(false);
  224. let destroy = ref(true);
  225. onMounted(() => {
  226. curVisible.value = props.visible;
  227. });
  228. const isShowContent = computed(() => {
  229. return slots.default;
  230. });
  231. const todestroy = () => {
  232. if (!props.canDestroy) {
  233. destroy = ref(false);
  234. }
  235. };
  236. const close = (target?: string) => {
  237. emit('close', target);
  238. emit('close-callback', target);
  239. todestroy();
  240. if (
  241. typeof props.closeCallback === 'function' &&
  242. props.closeCallback(target) === false
  243. ) {
  244. return;
  245. }
  246. curVisible.value = false;
  247. };
  248. const modalClick = () => {
  249. if (!props.closeOnClickModal) {
  250. return;
  251. }
  252. close('modal');
  253. };
  254. const okBtnClick = () => {
  255. emit('ok-btn-click');
  256. if (typeof props.onOkBtn === 'function') {
  257. props.onOkBtn.call(props);
  258. }
  259. };
  260. const cancelBtnClick = (autoClose: boolean) => {
  261. emit('cancel-btn-click');
  262. if (!autoClose) {
  263. return;
  264. }
  265. if (typeof props.onCancelBtn === 'function') {
  266. if (props.onCancelBtn.call(props) === false) {
  267. return;
  268. }
  269. }
  270. close('cancelBtn');
  271. };
  272. const closeBtnClick = () => {
  273. if (typeof props.onCloseBtn === 'function') {
  274. if (props.onCloseBtn.call(props) === false) {
  275. return;
  276. }
  277. }
  278. close('closeBtn');
  279. };
  280. //图片类型弹窗中的链接点击事件,默认跳转
  281. const imageLinkClick = () => {
  282. if (
  283. props.onClickImageLink &&
  284. props.onClickImageLink.call(props) === false
  285. ) {
  286. return;
  287. }
  288. if (props.link) {
  289. location.href = props.link;
  290. }
  291. };
  292. const handleClick = (event: Event) => {
  293. emit('click', event);
  294. };
  295. onMounted(() => {
  296. if (props.closeOnPopstate) {
  297. window.addEventListener('popstate', function() {
  298. close();
  299. });
  300. }
  301. });
  302. watchEffect(() => {
  303. if (props.lockBgScroll) {
  304. //锁定or解锁页面滚动
  305. lockMaskScroll[curVisible.value ? 'afterOpen' : 'beforeClose']();
  306. }
  307. });
  308. watch(
  309. () => props.visible,
  310. val => {
  311. curVisible.value = val;
  312. }
  313. );
  314. const classes = computed(() => {
  315. return {
  316. [componentName]: true
  317. };
  318. });
  319. return {
  320. handleClick,
  321. curVisible,
  322. destroy,
  323. modalClick,
  324. close,
  325. todestroy,
  326. okBtnClick,
  327. cancelBtnClick,
  328. closeBtnClick,
  329. imageLinkClick,
  330. isShowContent,
  331. classes
  332. };
  333. }
  334. });
  335. </script>
  336. <style lang="scss">
  337. @import 'index.scss';
  338. </style>