StdDialog.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <template>
  2. <q-dialog ref="dialog" v-model="active" no-route-dismiss :no-esc-dismiss="noEscDismiss" :no-backdrop-dismiss="noBackdropDismiss" @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 v-if="!noCancel" 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-if="!noCancel" 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 == 'password'" 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 v-if="!noCancel" 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. <input type="text" name="username" autocomplete="username" :value="userName" hidden />
  96. <q-input ref="input" v-model="inputValue" type="password" autocomplete="current-password" class="q-mt-xs" outlined dense />
  97. <div class="error">
  98. <span v-show="error != ''">{{ error }}</span>
  99. </div>
  100. </div>
  101. <div class="buttons row justify-end q-pa-md">
  102. <q-btn v-if="!noCancel" v-close-popup class="q-px-md q-ml-sm" dense no-caps>
  103. Отмена
  104. </q-btn>
  105. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">
  106. OK
  107. </q-btn>
  108. </div>
  109. </div>
  110. <!--------------------------------------------------->
  111. <div v-show="type == 'hotKey'" class="bg-white no-wrap">
  112. <div class="header row">
  113. <div class="caption col row items-center q-ml-md">
  114. <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" :name="iconName" size="28px"></q-icon>
  115. <div v-html="caption"></div>
  116. </div>
  117. <div class="close-icon column justify-center items-center">
  118. <q-btn v-close-popup flat round dense>
  119. <q-icon name="la la-times" size="18px"></q-icon>
  120. </q-btn>
  121. </div>
  122. </div>
  123. <div class="q-mx-md">
  124. <div v-html="message"></div>
  125. <div class="q-my-md text-center">
  126. <div v-show="hotKeyCode == ''" class="text-grey-5">
  127. Нет
  128. </div>
  129. <div>{{ hotKeyCode }}</div>
  130. </div>
  131. </div>
  132. <div class="buttons row justify-end q-pa-md">
  133. <q-btn v-close-popup class="q-px-md q-ml-sm" dense no-caps>
  134. Отмена
  135. </q-btn>
  136. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps :disabled="hotKeyCode == ''" @click="okClick">
  137. OK
  138. </q-btn>
  139. </div>
  140. </div>
  141. </q-dialog>
  142. </template>
  143. <script>
  144. //-----------------------------------------------------------------------------
  145. import vueComponent from '../vueComponent.js';
  146. import * as utils from '../../share/utils';
  147. const componentOptions = {
  148. watch: {
  149. inputValue: function(newValue) {
  150. this.validate(newValue);
  151. },
  152. }
  153. };
  154. class StdDialog {
  155. _options = componentOptions;
  156. caption = '';
  157. message = '';
  158. active = false;
  159. type = '';
  160. inputValue = '';
  161. error = '';
  162. iconColor = '';
  163. iconName = '';
  164. hotKeyCode = '';
  165. userName = '';
  166. noEscDismiss = false;
  167. noBackdropDismiss = false;
  168. noCancel = false;
  169. created() {
  170. if (this.$root.addKeyHook) {
  171. this.$root.addKeyHook(this.keyHook);
  172. }
  173. }
  174. init(message, caption, opts) {
  175. this.caption = caption;
  176. this.message = message;
  177. this.ok = false;
  178. this.type = '';
  179. this.inputValidator = null;
  180. this.inputValue = '';
  181. this.error = '';
  182. this.showed = false;
  183. this.noEscDismiss = (opts && opts.noEscDismiss) || false;
  184. this.noBackdropDismiss = (opts && opts.noBackdropDismiss) || false;
  185. this.noCancel = (opts && opts.noCancel) || false;
  186. this.iconColor = 'text-warning';
  187. if (opts && opts.color) {
  188. this.iconColor = `text-${opts.color}`;
  189. }
  190. this.iconName = 'las la-exclamation-circle';
  191. if (opts && opts.iconName) {
  192. this.iconName = opts.iconName;
  193. }
  194. this.hotKeyCode = '';
  195. if (opts && opts.hotKeyCode) {
  196. this.hotKeyCode = opts.hotKeyCode;
  197. }
  198. }
  199. onHide() {
  200. if (this.hideTrigger) {
  201. this.hideTrigger();
  202. this.hideTrigger = null;
  203. }
  204. this.showed = false;
  205. }
  206. onShow() {
  207. if (this.type == 'prompt' || this.type == 'password') {
  208. this.enableValidator = true;
  209. if (this.inputValue)
  210. this.validate(this.inputValue);
  211. this.$refs.input.focus();
  212. }
  213. this.showed = true;
  214. }
  215. validate(value) {
  216. if (!this.enableValidator)
  217. return false;
  218. if (this.inputValidator) {
  219. const result = this.inputValidator(value);
  220. if (result !== true) {
  221. this.error = result;
  222. return false;
  223. }
  224. }
  225. this.error = '';
  226. return true;
  227. }
  228. okClick() {
  229. if ((this.type == 'prompt' || this.type == 'password') && !this.validate(this.inputValue)) {
  230. this.$refs.dialog.shake();
  231. return;
  232. }
  233. if (this.type == 'hotKey' && this.hotKeyCode == '') {
  234. this.$refs.dialog.shake();
  235. return;
  236. }
  237. this.ok = true;
  238. this.$refs.dialog.hide();
  239. }
  240. alert(message, caption, opts) {
  241. return new Promise((resolve) => {
  242. this.init(message, caption, opts);
  243. this.hideTrigger = () => {
  244. if (this.ok) {
  245. resolve(true);
  246. } else {
  247. resolve(false);
  248. }
  249. };
  250. this.type = 'alert';
  251. this.active = true;
  252. });
  253. }
  254. confirm(message, caption, opts) {
  255. return new Promise((resolve) => {
  256. this.init(message, caption, opts);
  257. this.hideTrigger = () => {
  258. if (this.ok) {
  259. resolve(true);
  260. } else {
  261. resolve(false);
  262. }
  263. };
  264. this.type = 'confirm';
  265. this.active = true;
  266. });
  267. }
  268. prompt(message, caption, opts) {
  269. return new Promise((resolve) => {
  270. this.enableValidator = false;
  271. this.init(message, caption, opts);
  272. this.hideTrigger = () => {
  273. if (this.ok) {
  274. resolve({value: this.inputValue});
  275. } else {
  276. resolve(false);
  277. }
  278. };
  279. this.type = 'prompt';
  280. if (opts) {
  281. this.inputValidator = opts.inputValidator || null;
  282. this.inputValue = opts.inputValue || '';
  283. }
  284. this.active = true;
  285. });
  286. }
  287. password(message, caption, opts) {
  288. return new Promise((resolve) => {
  289. this.enableValidator = false;
  290. this.init(message, caption, opts);
  291. this.hideTrigger = () => {
  292. if (this.ok) {
  293. history.pushState({}, null);
  294. resolve({value: this.inputValue});
  295. } else {
  296. resolve(false);
  297. }
  298. };
  299. this.type = 'password';
  300. this.userName = '';
  301. if (opts) {
  302. this.inputValidator = opts.inputValidator || null;
  303. this.inputValue = opts.inputValue || '';
  304. this.userName = opts.userName || '';
  305. }
  306. this.active = true;
  307. });
  308. }
  309. getHotKey(message, caption, opts) {
  310. return new Promise((resolve) => {
  311. this.init(message, caption, opts);
  312. this.hideTrigger = () => {
  313. if (this.ok) {
  314. resolve(this.hotKeyCode);
  315. } else {
  316. resolve(false);
  317. }
  318. };
  319. this.type = 'hotKey';
  320. this.active = true;
  321. });
  322. }
  323. keyHook(event) {
  324. if (this.active && this.showed) {
  325. let handled = false;
  326. if (this.type == 'hotKey') {
  327. if (event.type == 'keydown') {
  328. this.hotKeyCode = utils.keyEventToCode(event);
  329. handled = true;
  330. }
  331. } else {
  332. if (event.key == 'Enter') {
  333. this.okClick();
  334. handled = true;
  335. }
  336. if (event.key == 'Escape' && !this.noEscDismiss) {
  337. this.$nextTick(() => {
  338. this.$refs.dialog.hide();
  339. });
  340. handled = true;
  341. }
  342. }
  343. if (handled) {
  344. event.stopPropagation();
  345. event.preventDefault();
  346. }
  347. }
  348. }
  349. }
  350. export default vueComponent(StdDialog);
  351. //-----------------------------------------------------------------------------
  352. </script>
  353. <style scoped>
  354. .header {
  355. height: 50px;
  356. }
  357. .caption {
  358. font-size: 110%;
  359. overflow: hidden;
  360. }
  361. .close-icon {
  362. width: 50px;
  363. }
  364. .buttons {
  365. height: 60px;
  366. }
  367. .error {
  368. height: 20px;
  369. font-size: 80%;
  370. color: red;
  371. }
  372. </style>