StdDialog.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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="bg-white no-wrap">
  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="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="bg-white no-wrap">
  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="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="bg-white no-wrap">
  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="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. <!--------------------------------------------------->
  69. <div v-show="type == 'hotKey'" class="bg-white no-wrap">
  70. <div class="header row">
  71. <div class="caption col row items-center q-ml-md">
  72. <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" name="las la-exclamation-circle" size="28px"></q-icon>
  73. <div v-html="caption"></div>
  74. </div>
  75. <div class="close-icon column justify-center items-center">
  76. <q-btn flat round dense v-close-popup>
  77. <q-icon name="la la-times" size="18px"></q-icon>
  78. </q-btn>
  79. </div>
  80. </div>
  81. <div class="q-mx-md">
  82. <div v-html="message"></div>
  83. <div class="q-my-md text-center">
  84. <div v-show="hotKeyCode == ''" class="text-grey-5">Нет</div>
  85. <div>{{ hotKeyCode }}</div>
  86. </div>
  87. </div>
  88. <div class="buttons row justify-end q-pa-md">
  89. <q-btn class="q-px-md q-ml-sm" dense no-caps v-close-popup>Отмена</q-btn>
  90. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick" :disabled="hotKeyCode == ''">OK</q-btn>
  91. </div>
  92. </div>
  93. </q-dialog>
  94. </template>
  95. <script>
  96. //-----------------------------------------------------------------------------
  97. import Vue from 'vue';
  98. import Component from 'vue-class-component';
  99. import * as utils from '../../share/utils';
  100. export default @Component({
  101. watch: {
  102. inputValue: function(newValue) {
  103. this.validate(newValue);
  104. },
  105. }
  106. })
  107. class StdDialog extends Vue {
  108. caption = '';
  109. message = '';
  110. active = false;
  111. type = '';
  112. inputValue = '';
  113. error = '';
  114. iconColor = '';
  115. hotKeyCode = '';
  116. created() {
  117. if (this.$root.addKeyHook) {
  118. //this.$root.addKeyHook(this.keyHook);
  119. }
  120. }
  121. init(message, caption, opts) {
  122. this.caption = caption;
  123. this.message = message;
  124. this.ok = false;
  125. this.type = '';
  126. this.inputValidator = null;
  127. this.inputValue = '';
  128. this.error = '';
  129. this.iconColor = 'text-warning';
  130. if (opts && opts.color) {
  131. this.iconColor = `text-${opts.color}`;
  132. }
  133. this.hotKeyCode = '';
  134. if (opts && opts.hotKeyCode) {
  135. this.hotKeyCode = opts.hotKeyCode;
  136. }
  137. }
  138. onHide() {
  139. if (this.hideTrigger) {
  140. this.hideTrigger();
  141. this.hideTrigger = null;
  142. }
  143. }
  144. onShow() {
  145. if (this.type == 'prompt') {
  146. this.enableValidator = true;
  147. if (this.inputValue)
  148. this.validate(this.inputValue);
  149. this.$refs.input.focus();
  150. }
  151. }
  152. validate(value) {
  153. if (!this.enableValidator)
  154. return false;
  155. if (this.inputValidator) {
  156. const result = this.inputValidator(value);
  157. if (result !== true) {
  158. this.error = result;
  159. return false;
  160. }
  161. }
  162. this.error = '';
  163. return true;
  164. }
  165. okClick() {
  166. if (this.type == 'prompt' && !this.validate(this.inputValue)) {
  167. this.$refs.dialog.shake();
  168. return;
  169. }
  170. if (this.type == 'hotKey' && this.hotKeyCode == '') {
  171. this.$refs.dialog.shake();
  172. return;
  173. }
  174. this.ok = true;
  175. this.$refs.dialog.hide();
  176. }
  177. alert(message, caption, opts) {
  178. return new Promise((resolve) => {
  179. this.init(message, caption, opts);
  180. this.hideTrigger = () => {
  181. if (this.ok) {
  182. resolve(true);
  183. } else {
  184. resolve(false);
  185. }
  186. };
  187. this.type = 'alert';
  188. this.active = true;
  189. });
  190. }
  191. confirm(message, caption, opts) {
  192. return new Promise((resolve) => {
  193. this.init(message, caption, opts);
  194. this.hideTrigger = () => {
  195. if (this.ok) {
  196. resolve(true);
  197. } else {
  198. resolve(false);
  199. }
  200. };
  201. this.type = 'confirm';
  202. this.active = true;
  203. });
  204. }
  205. prompt(message, caption, opts) {
  206. return new Promise((resolve) => {
  207. this.enableValidator = false;
  208. this.init(message, caption, opts);
  209. this.hideTrigger = () => {
  210. if (this.ok) {
  211. resolve({value: this.inputValue});
  212. } else {
  213. resolve(false);
  214. }
  215. };
  216. this.type = 'prompt';
  217. if (opts) {
  218. this.inputValidator = opts.inputValidator || null;
  219. this.inputValue = opts.inputValue || '';
  220. }
  221. this.active = true;
  222. });
  223. }
  224. getHotKey(message, caption, opts) {
  225. return new Promise((resolve) => {
  226. this.init(message, caption, opts);
  227. this.hideTrigger = () => {
  228. if (this.ok) {
  229. resolve(this.hotKeyCode);
  230. } else {
  231. resolve(false);
  232. }
  233. };
  234. this.type = 'hotKey';
  235. this.active = true;
  236. });
  237. }
  238. keyHook(event) {
  239. if (this.active) {
  240. let handled = false;
  241. if (this.type == 'hotKey') {
  242. if (event.type == 'keydown') {
  243. this.hotKeyCode = utils.keyEventToCode(event);
  244. handled = true;
  245. }
  246. } else {
  247. if (event.code == 'Enter') {
  248. this.okClick();
  249. handled = true;
  250. }
  251. if (event.code == 'Escape') {
  252. this.$nextTick(() => {
  253. this.$refs.dialog.hide();
  254. });
  255. handled = true;
  256. }
  257. }
  258. if (handled) {
  259. event.stopPropagation();
  260. event.preventDefault();
  261. }
  262. }
  263. }
  264. }
  265. //-----------------------------------------------------------------------------
  266. </script>
  267. <style scoped>
  268. .header {
  269. height: 50px;
  270. }
  271. .caption {
  272. font-size: 110%;
  273. overflow: hidden;
  274. }
  275. .close-icon {
  276. width: 50px;
  277. }
  278. .buttons {
  279. height: 60px;
  280. }
  281. .error {
  282. height: 20px;
  283. font-size: 80%;
  284. color: red;
  285. }
  286. </style>