StdDialog.vue 10 KB

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