StdDialog.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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="dialogStyle">
  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="dialogStyle">
  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="dialogStyle">
  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. style = '';
  91. created() {
  92. if (this.$root.addKeyHook) {
  93. this.$root.addKeyHook(this.keyHook);
  94. }
  95. }
  96. init(message, caption, opts) {
  97. this.caption = caption;
  98. this.message = message;
  99. this.ok = false;
  100. this.type = '';
  101. this.inputValidator = null;
  102. this.inputValue = '';
  103. this.error = '';
  104. this.iconColor = 'text-warning';
  105. if (opts && opts.color) {
  106. this.iconColor = `text-${opts.color}`;
  107. }
  108. }
  109. get dialogStyle() {
  110. return 'min-height: 150px';
  111. }
  112. onHide() {
  113. if (this.hideTrigger) {
  114. this.hideTrigger();
  115. this.hideTrigger = null;
  116. }
  117. }
  118. onShow() {
  119. if (this.type == 'prompt') {
  120. this.enableValidator = true;
  121. if (this.inputValue)
  122. this.validate(this.inputValue);
  123. this.$refs.input.focus();
  124. }
  125. }
  126. validate(value) {
  127. if (!this.enableValidator)
  128. return false;
  129. if (this.inputValidator) {
  130. const result = this.inputValidator(value);
  131. if (result !== true) {
  132. this.error = result;
  133. return false;
  134. }
  135. }
  136. this.error = '';
  137. return true;
  138. }
  139. okClick() {
  140. if (this.type == 'prompt' && !this.validate(this.inputValue)) {
  141. this.$refs.dialog.shake();
  142. return;
  143. }
  144. this.ok = true;
  145. this.$refs.dialog.hide();
  146. }
  147. alert(message, caption, opts) {
  148. return new Promise((resolve) => {
  149. this.init(message, caption, opts);
  150. this.hideTrigger = () => {
  151. if (this.ok) {
  152. resolve(true);
  153. } else {
  154. resolve(false);
  155. }
  156. };
  157. this.type = 'alert';
  158. this.active = true;
  159. });
  160. }
  161. confirm(message, caption, opts) {
  162. return new Promise((resolve) => {
  163. this.init(message, caption, opts);
  164. this.hideTrigger = () => {
  165. if (this.ok) {
  166. resolve(true);
  167. } else {
  168. resolve(false);
  169. }
  170. };
  171. this.type = 'confirm';
  172. this.active = true;
  173. });
  174. }
  175. prompt(message, caption, opts) {
  176. return new Promise((resolve) => {
  177. this.enableValidator = false;
  178. this.init(message, caption, opts);
  179. this.hideTrigger = () => {
  180. if (this.ok) {
  181. resolve({value: this.inputValue});
  182. } else {
  183. resolve(false);
  184. }
  185. };
  186. this.type = 'prompt';
  187. if (opts) {
  188. this.inputValidator = opts.inputValidator || null;
  189. this.inputValue = opts.inputValue || '';
  190. }
  191. this.active = true;
  192. });
  193. }
  194. keyHook(event) {
  195. if (this.active && event.code == 'Enter') {
  196. this.okClick();
  197. event.stopPropagation();
  198. event.preventDefault();
  199. }
  200. }
  201. }
  202. //-----------------------------------------------------------------------------
  203. </script>
  204. <style scoped>
  205. .header {
  206. height: 50px;
  207. }
  208. .caption {
  209. font-size: 110%;
  210. overflow: hidden;
  211. }
  212. .close-icon {
  213. width: 50px;
  214. }
  215. .buttons {
  216. height: 60px;
  217. }
  218. .error {
  219. height: 20px;
  220. font-size: 80%;
  221. color: red;
  222. }
  223. </style>