StdDialog.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <q-dialog ref="dialog" v-model="active" @show="onShow" @hide="onHide">
  3. <slot></slot>
  4. <!--------------------------------------------------->
  5. <div v-show="type == 'alert'" class="dialog column bg-white no-wrap" style="min-height: 150px">
  6. <div class="header row">
  7. <div class="caption col row items-center q-ml-md">
  8. <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" name="las la-exclamation-circle" size="28px"></q-icon>
  9. <div v-html="caption"></div>
  10. </div>
  11. <div class="close-icon column justify-center items-center">
  12. <q-btn flat round dense v-close-popup>
  13. <q-icon name="la la-times" size="18px"></q-icon>
  14. </q-btn>
  15. </div>
  16. </div>
  17. <div class="col q-mx-md">
  18. <div v-html="message"></div>
  19. </div>
  20. <div class="buttons row justify-end q-pa-md">
  21. <q-btn class="q-px-md" dense no-caps @click="okClick">OK</q-btn>
  22. </div>
  23. </div>
  24. <!--------------------------------------------------->
  25. <div v-show="type == 'confirm'" class="dialog column bg-white no-wrap" style="min-height: 150px">
  26. <div class="header row">
  27. <div class="caption col row items-center q-ml-md">
  28. <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" name="las la-exclamation-circle" size="28px"></q-icon>
  29. <div v-html="caption"></div>
  30. </div>
  31. <div class="close-icon column justify-center items-center">
  32. <q-btn flat round dense v-close-popup>
  33. <q-icon name="la la-times" size="18px"></q-icon>
  34. </q-btn>
  35. </div>
  36. </div>
  37. <div class="col q-mx-md">
  38. <div v-html="message"></div>
  39. </div>
  40. <div class="buttons row justify-end q-pa-md">
  41. <q-btn class="q-px-md q-ml-sm" dense no-caps v-close-popup>Отмена</q-btn>
  42. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">OK</q-btn>
  43. </div>
  44. </div>
  45. <!--------------------------------------------------->
  46. <div v-show="type == 'prompt'" class="dialog column bg-white no-wrap" style="min-height: 250px">
  47. <div class="header row">
  48. <div class="caption col row items-center q-ml-md">
  49. <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" name="las la-exclamation-circle" size="28px"></q-icon>
  50. <div v-html="caption"></div>
  51. </div>
  52. <div class="close-icon column justify-center items-center">
  53. <q-btn flat round dense v-close-popup>
  54. <q-icon name="la la-times" size="18px"></q-icon>
  55. </q-btn>
  56. </div>
  57. </div>
  58. <div class="col q-mx-md">
  59. <div v-html="message"></div>
  60. <q-input ref="input" class="q-mt-xs" outlined dense v-model="inputValue"/>
  61. <div class="error"><span v-show="error != ''">{{ error }}</span></div>
  62. </div>
  63. <div class="buttons row justify-end q-pa-md">
  64. <q-btn class="q-px-md q-ml-sm" dense no-caps v-close-popup>Отмена</q-btn>
  65. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">OK</q-btn>
  66. </div>
  67. </div>
  68. </q-dialog>
  69. </template>
  70. <script>
  71. //-----------------------------------------------------------------------------
  72. import Vue from 'vue';
  73. import Component from 'vue-class-component';
  74. //import * as utils from '../../share/utils';
  75. export default @Component({
  76. watch: {
  77. inputValue: function(newValue) {
  78. this.validate(newValue);
  79. },
  80. }
  81. })
  82. class StdDialog extends Vue {
  83. caption = '';
  84. message = '';
  85. active = false;
  86. type = '';
  87. inputValue = '';
  88. error = '';
  89. iconColor = '';
  90. created() {
  91. if (this.$root.addKeyHook) {
  92. this.$root.addKeyHook(this.keyHook);
  93. }
  94. }
  95. init(message, caption, opts) {
  96. this.caption = caption;
  97. this.message = message;
  98. this.ok = false;
  99. this.type = '';
  100. this.inputValidator = null;
  101. this.inputValue = '';
  102. this.error = '';
  103. this.iconColor = 'text-warning';
  104. if (opts && opts.type) {
  105. this.iconColor = `text-${opts.type}`;
  106. }
  107. }
  108. onHide() {
  109. if (this.hideTrigger) {
  110. this.hideTrigger();
  111. this.hideTrigger = null;
  112. }
  113. }
  114. onShow() {
  115. if (this.type == 'prompt') {
  116. this.enableValidator = true;
  117. if (this.inputValue)
  118. this.validate(this.inputValue);
  119. this.$refs.input.focus();
  120. }
  121. }
  122. validate(value) {
  123. if (!this.enableValidator)
  124. return false;
  125. if (this.inputValidator) {
  126. const result = this.inputValidator(value);
  127. if (result !== true) {
  128. this.error = result;
  129. return false;
  130. }
  131. }
  132. this.error = '';
  133. return true;
  134. }
  135. okClick() {
  136. if (this.type == 'prompt' && !this.validate(this.inputValue)) {
  137. this.$refs.dialog.shake();
  138. return;
  139. }
  140. this.ok = true;
  141. this.$refs.dialog.hide();
  142. }
  143. alert(message, caption, opts) {
  144. return new Promise((resolve) => {
  145. this.init(message, caption, opts);
  146. this.hideTrigger = () => {
  147. if (this.ok) {
  148. resolve(true);
  149. } else {
  150. resolve(false);
  151. }
  152. };
  153. this.type = 'alert';
  154. this.active = true;
  155. });
  156. }
  157. confirm(message, caption, opts) {
  158. return new Promise((resolve) => {
  159. this.init(message, caption, opts);
  160. this.hideTrigger = () => {
  161. if (this.ok) {
  162. resolve(true);
  163. } else {
  164. resolve(false);
  165. }
  166. };
  167. this.type = 'confirm';
  168. this.active = true;
  169. });
  170. }
  171. prompt(message, caption, opts) {
  172. return new Promise((resolve) => {
  173. this.enableValidator = false;
  174. this.init(message, caption, opts);
  175. this.hideTrigger = () => {
  176. if (this.ok) {
  177. resolve({value: this.inputValue});
  178. } else {
  179. resolve(false);
  180. }
  181. };
  182. this.type = 'prompt';
  183. if (opts) {
  184. this.inputValidator = opts.inputValidator || null;
  185. this.inputValue = opts.inputValue || '';
  186. }
  187. this.active = true;
  188. });
  189. }
  190. keyHook(event) {
  191. if (this.active && event.code == 'Enter') {
  192. this.okClick();
  193. event.stopPropagation();
  194. event.preventDefault();
  195. }
  196. }
  197. }
  198. //-----------------------------------------------------------------------------
  199. </script>
  200. <style scoped>
  201. .header {
  202. height: 50px;
  203. }
  204. .caption {
  205. font-size: 110%;
  206. overflow: hidden;
  207. }
  208. .close-icon {
  209. width: 50px;
  210. }
  211. .buttons {
  212. height: 60px;
  213. }
  214. .error {
  215. height: 20px;
  216. font-size: 80%;
  217. color: red;
  218. }
  219. </style>