StdDialog.vue 10 KB

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