dialog.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <div
  3. :class="['nut-dialog-wrapper',customClass,{'nut-dialog-image-wrapper':type==='image'}]"
  4. :id="id"
  5. >
  6. <transition :name="animation?'nutFade':''">
  7. <div
  8. :class="'nut-dialog-mask'"
  9. :style="{'background':maskBgStyle}"
  10. @click="modalClick"
  11. v-show="curVisible"
  12. ></div>
  13. </transition>
  14. <transition :name="animation?'nutEase':''">
  15. <div class="nut-dialog-box" v-show="curVisible" @click="modalClick">
  16. <div class="nut-dialog" @click.stop>
  17. <a href="javascript:;" v-if="closeBtn" @click="closeBtnClick" class="nut-dialog-close"></a>
  18. <template v-if="type==='image'">
  19. <a href="javascript:;" @click="imageLinkClick" class="nut-dialog-link">
  20. <img :src="imgSrc" class="nut-dialog-image" alt>
  21. </a>
  22. </template>
  23. <template v-else>
  24. <div class="nut-dialog-body">
  25. <span class="nut-dialog-title" v-html="title" v-if="title"></span>
  26. <div class="nut-dialog-content" v-if="$slots.default" :style="{textAlign}">
  27. <slot></slot>
  28. </div>
  29. <div
  30. class="nut-dialog-content"
  31. v-html="content"
  32. v-else-if="content"
  33. :style="{textAlign}"
  34. ></div>
  35. </div>
  36. <div class="nut-dialog-footer" v-if="!noFooter">
  37. <button
  38. class="nut-dialog-btn nut-dialog-cancel"
  39. v-if="!noCancelBtn"
  40. @click="cancelBtnClick(cancelAutoClose)"
  41. >{{cancelBtnTxt || nutTranslate('lang.cancelBtnTxt')}}</button>
  42. <button
  43. class="nut-dialog-btn nut-dialog-ok"
  44. v-if="!noOkBtn"
  45. :class="{'disabled':okBtnDisabled}"
  46. :disabled="okBtnDisabled"
  47. @click="okBtnClick"
  48. >{{okBtnTxt || nutTranslate('lang.okBtnTxt')}}</button>
  49. </div>
  50. </template>
  51. </div>
  52. </div>
  53. </transition>
  54. </div>
  55. </template>
  56. <script>
  57. import locale from "../../mixins/locale";
  58. const lockMaskScroll = (bodyCls => {
  59. let scrollTop;
  60. return {
  61. afterOpen: function() {
  62. scrollTop =
  63. document.scrollingElement.scrollTop || document.body.scrollTop;
  64. document.body.classList.add(bodyCls);
  65. document.body.style.top = -scrollTop + "px";
  66. },
  67. beforeClose: function() {
  68. if (document.body.classList.contains(bodyCls)) {
  69. document.body.classList.remove(bodyCls);
  70. document.scrollingElement.scrollTop = scrollTop;
  71. }
  72. }
  73. };
  74. })("dialog-open");
  75. export default {
  76. name: "nut-dialog",
  77. mixins: [locale],
  78. props: {
  79. id: {
  80. default: null
  81. },
  82. title: {
  83. default: ""
  84. },
  85. content: {
  86. default: ""
  87. },
  88. type: {
  89. default: ""
  90. },
  91. link: {
  92. default: ""
  93. },
  94. imgSrc: {
  95. default: ""
  96. },
  97. animation: {
  98. type: Boolean,
  99. default: true
  100. },
  101. lockBgScroll: {
  102. type: Boolean,
  103. default: false
  104. },
  105. visible: {
  106. type: Boolean,
  107. default: false
  108. },
  109. closeBtn: {
  110. type: Boolean,
  111. default: false
  112. },
  113. closeOnClickModal: {
  114. type: Boolean,
  115. default: true
  116. },
  117. noFooter: {
  118. type: Boolean,
  119. default: false
  120. },
  121. noOkBtn: {
  122. type: Boolean,
  123. default: false
  124. },
  125. noCancelBtn: {
  126. type: Boolean,
  127. default: false
  128. },
  129. cancelBtnTxt: {
  130. default: ""
  131. },
  132. okBtnTxt: {
  133. default: ""
  134. },
  135. okBtnDisabled: {
  136. type: Boolean,
  137. default: false
  138. },
  139. cancelAutoClose: {
  140. type: Boolean,
  141. default: true
  142. },
  143. textAlign: {
  144. default: "center"
  145. },
  146. onOkBtn: {
  147. default: null
  148. },
  149. onCloseBtn: {
  150. default: null
  151. },
  152. onCancelBtn: {
  153. default: null
  154. },
  155. closeCallback: {
  156. default: null
  157. },
  158. onClickImageLink: {
  159. default: null
  160. },
  161. maskBgStyle: {
  162. default: ""
  163. },
  164. customClass: {
  165. default: ""
  166. }
  167. },
  168. data() {
  169. return {
  170. curVisible: false
  171. };
  172. },
  173. methods: {
  174. modalClick() {
  175. if (!this.closeOnClickModal) return;
  176. this.close("modal");
  177. },
  178. close(target) {
  179. this.$emit("close", target);
  180. this.$emit("close-callback", target);
  181. if (
  182. typeof this.closeCallback === "function" &&
  183. this.closeCallback(target) === false
  184. ) {
  185. return;
  186. }
  187. this.curVisible = false;
  188. },
  189. okBtnClick() {
  190. this.$emit("ok-btn-click");
  191. if (typeof this.onOkBtn === "function") {
  192. this.onOkBtn.call(this);
  193. }
  194. },
  195. cancelBtnClick(autoClose) {
  196. if(!autoClose){
  197. return
  198. }
  199. this.$emit("cancel-btn-click");
  200. if (typeof this.onCancelBtn === "function") {
  201. if (this.onCancelBtn.call(this) === false) return;
  202. }
  203. this.close("cancelBtn");
  204. },
  205. closeBtnClick() {
  206. if (typeof this.onCloseBtn === "function") {
  207. if (this.onCloseBtn.call(this) === false) return;
  208. }
  209. this.close("closeBtn");
  210. },
  211. //图片类型弹窗中的链接点击事件,默认跳转
  212. imageLinkClick() {
  213. if (this.onClickImageLink && this.onClickImageLink.call(this) === false)
  214. return;
  215. if (this.link) location.href = this.link;
  216. }
  217. },
  218. created() {},
  219. watch: {
  220. visible: {
  221. handler(val) {
  222. this.curVisible = val;
  223. },
  224. immediate: true
  225. },
  226. curVisible(val) {
  227. if (this.lockBgScroll) {
  228. //锁定or解锁页面滚动
  229. lockMaskScroll[val ? "afterOpen" : "beforeClose"]();
  230. }
  231. }
  232. }
  233. };
  234. </script>