StdDialog.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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.showed = false;
  130. this.iconColor = 'text-warning';
  131. if (opts && opts.color) {
  132. this.iconColor = `text-${opts.color}`;
  133. }
  134. this.hotKeyCode = '';
  135. if (opts && opts.hotKeyCode) {
  136. this.hotKeyCode = opts.hotKeyCode;
  137. }
  138. }
  139. onHide() {
  140. if (this.hideTrigger) {
  141. this.hideTrigger();
  142. this.hideTrigger = null;
  143. }
  144. this.showed = false;
  145. }
  146. onShow() {
  147. if (this.type == 'prompt') {
  148. this.enableValidator = true;
  149. if (this.inputValue)
  150. this.validate(this.inputValue);
  151. this.$refs.input.focus();
  152. }
  153. this.showed = true;
  154. }
  155. validate(value) {
  156. if (!this.enableValidator)
  157. return false;
  158. if (this.inputValidator) {
  159. const result = this.inputValidator(value);
  160. if (result !== true) {
  161. this.error = result;
  162. return false;
  163. }
  164. }
  165. this.error = '';
  166. return true;
  167. }
  168. okClick() {
  169. if (this.type == 'prompt' && !this.validate(this.inputValue)) {
  170. this.$refs.dialog.shake();
  171. return;
  172. }
  173. if (this.type == 'hotKey' && this.hotKeyCode == '') {
  174. this.$refs.dialog.shake();
  175. return;
  176. }
  177. this.ok = true;
  178. this.$refs.dialog.hide();
  179. }
  180. alert(message, caption, opts) {
  181. return new Promise((resolve) => {
  182. this.init(message, caption, opts);
  183. this.hideTrigger = () => {
  184. if (this.ok) {
  185. resolve(true);
  186. } else {
  187. resolve(false);
  188. }
  189. };
  190. this.type = 'alert';
  191. this.active = true;
  192. });
  193. }
  194. confirm(message, caption, opts) {
  195. return new Promise((resolve) => {
  196. this.init(message, caption, opts);
  197. this.hideTrigger = () => {
  198. if (this.ok) {
  199. resolve(true);
  200. } else {
  201. resolve(false);
  202. }
  203. };
  204. this.type = 'confirm';
  205. this.active = true;
  206. });
  207. }
  208. prompt(message, caption, opts) {
  209. return new Promise((resolve) => {
  210. this.enableValidator = false;
  211. this.init(message, caption, opts);
  212. this.hideTrigger = () => {
  213. if (this.ok) {
  214. resolve({value: this.inputValue});
  215. } else {
  216. resolve(false);
  217. }
  218. };
  219. this.type = 'prompt';
  220. if (opts) {
  221. this.inputValidator = opts.inputValidator || null;
  222. this.inputValue = opts.inputValue || '';
  223. }
  224. this.active = true;
  225. });
  226. }
  227. getHotKey(message, caption, opts) {
  228. return new Promise((resolve) => {
  229. this.init(message, caption, opts);
  230. this.hideTrigger = () => {
  231. if (this.ok) {
  232. resolve(this.hotKeyCode);
  233. } else {
  234. resolve(false);
  235. }
  236. };
  237. this.type = 'hotKey';
  238. this.active = true;
  239. });
  240. }
  241. keyHook(event) {
  242. if (this.active && this.showed) {
  243. let handled = false;
  244. if (this.type == 'hotKey') {
  245. if (event.type == 'keydown') {
  246. this.hotKeyCode = utils.keyEventToCode(event);
  247. handled = true;
  248. }
  249. } else {
  250. if (event.key == 'Enter') {
  251. this.okClick();
  252. handled = true;
  253. }
  254. if (event.key == 'Escape') {
  255. this.$nextTick(() => {
  256. this.$refs.dialog.hide();
  257. });
  258. handled = true;
  259. }
  260. }
  261. if (handled) {
  262. event.stopPropagation();
  263. event.preventDefault();
  264. }
  265. }
  266. }
  267. }
  268. //-----------------------------------------------------------------------------
  269. </script>
  270. <style scoped>
  271. .header {
  272. height: 50px;
  273. }
  274. .caption {
  275. font-size: 110%;
  276. overflow: hidden;
  277. }
  278. .close-icon {
  279. width: 50px;
  280. }
  281. .buttons {
  282. height: 60px;
  283. }
  284. .error {
  285. height: 20px;
  286. font-size: 80%;
  287. color: red;
  288. }
  289. </style>