123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- <template>
- <q-dialog ref="dialog" v-model="active" no-route-dismiss :no-esc-dismiss="noEscDismiss" :no-backdrop-dismiss="noBackdropDismiss" @show="onShow" @hide="onHide">
- <slot></slot>
- <!--------------------------------------------------->
- <div v-show="type == 'alert'" class="bg-white no-wrap">
- <div class="header row">
- <div class="caption col row items-center q-ml-md">
- <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" :name="iconName" size="28px"></q-icon>
- <div v-html="caption"></div>
- </div>
- <div class="close-icon column justify-center items-center">
- <q-btn v-close-popup flat round dense>
- <q-icon name="la la-times" size="18px"></q-icon>
- </q-btn>
- </div>
- </div>
- <div class="q-mx-md">
- <div v-html="message"></div>
- </div>
- <div class="buttons row justify-end q-pa-md">
- <q-btn class="q-px-md" dense no-caps @click="okClick">
- OK
- </q-btn>
- </div>
- </div>
- <!--------------------------------------------------->
- <div v-show="type == 'confirm'" class="bg-white no-wrap">
- <div class="header row">
- <div class="caption col row items-center q-ml-md">
- <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" :name="iconName" size="28px"></q-icon>
- <div v-html="caption"></div>
- </div>
- <div class="close-icon column justify-center items-center">
- <q-btn v-close-popup flat round dense>
- <q-icon name="la la-times" size="18px"></q-icon>
- </q-btn>
- </div>
- </div>
- <div class="q-mx-md">
- <div v-html="message"></div>
- </div>
- <div class="buttons row justify-end q-pa-md">
- <q-btn v-close-popup class="q-px-md q-ml-sm" dense no-caps>
- Отмена
- </q-btn>
- <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">
- OK
- </q-btn>
- </div>
- </div>
- <!--------------------------------------------------->
- <div v-show="type == 'prompt'" class="bg-white no-wrap">
- <div class="header row">
- <div class="caption col row items-center q-ml-md">
- <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" :name="iconName" size="28px"></q-icon>
- <div v-html="caption"></div>
- </div>
- <div v-if="!noCancel" class="close-icon column justify-center items-center">
- <q-btn v-close-popup flat round dense>
- <q-icon name="la la-times" size="18px"></q-icon>
- </q-btn>
- </div>
- </div>
- <div class="q-mx-md">
- <div v-html="message"></div>
- <q-input ref="input" v-model="inputValue" class="q-mt-xs" outlined dense />
- <div class="error">
- <span v-show="error != ''">{{ error }}</span>
- </div>
- </div>
- <div class="buttons row justify-end q-pa-md">
- <q-btn v-if="!noCancel" v-close-popup class="q-px-md q-ml-sm" dense no-caps>
- Отмена
- </q-btn>
- <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">
- OK
- </q-btn>
- </div>
- </div>
- <!--------------------------------------------------->
- <div v-show="type == 'password'" class="bg-white no-wrap">
- <div class="header row">
- <div class="caption col row items-center q-ml-md">
- <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" :name="iconName" size="28px"></q-icon>
- <div v-html="caption"></div>
- </div>
- <div v-if="!noCancel" class="close-icon column justify-center items-center">
- <q-btn v-close-popup flat round dense>
- <q-icon name="la la-times" size="18px"></q-icon>
- </q-btn>
- </div>
- </div>
- <div class="q-mx-md">
- <div v-html="message"></div>
- <input type="text" name="username" autocomplete="username" :value="userName" hidden />
- <q-input ref="input" v-model="inputValue" type="password" autocomplete="current-password" class="q-mt-xs" outlined dense />
- <div class="error">
- <span v-show="error != ''">{{ error }}</span>
- </div>
- </div>
- <div class="buttons row justify-end q-pa-md">
- <q-btn v-if="!noCancel" v-close-popup class="q-px-md q-ml-sm" dense no-caps>
- Отмена
- </q-btn>
- <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">
- OK
- </q-btn>
- </div>
- </div>
- <!--------------------------------------------------->
- <div v-show="type == 'hotKey'" class="bg-white no-wrap">
- <div class="header row">
- <div class="caption col row items-center q-ml-md">
- <q-icon v-show="caption" class="q-mr-sm" :class="iconColor" :name="iconName" size="28px"></q-icon>
- <div v-html="caption"></div>
- </div>
- <div class="close-icon column justify-center items-center">
- <q-btn v-close-popup flat round dense>
- <q-icon name="la la-times" size="18px"></q-icon>
- </q-btn>
- </div>
- </div>
- <div class="q-mx-md">
- <div v-html="message"></div>
- <div class="q-my-md text-center">
- <div v-show="hotKeyCode == ''" class="text-grey-5">
- Нет
- </div>
- <div>{{ hotKeyCode }}</div>
- </div>
- </div>
- <div class="buttons row justify-end q-pa-md">
- <q-btn v-close-popup class="q-px-md q-ml-sm" dense no-caps>
- Отмена
- </q-btn>
- <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps :disabled="hotKeyCode == ''" @click="okClick">
- OK
- </q-btn>
- </div>
- </div>
- </q-dialog>
- </template>
- <script>
- //-----------------------------------------------------------------------------
- import vueComponent from '../vueComponent.js';
- import * as utils from '../../share/utils';
- const componentOptions = {
- watch: {
- inputValue: function(newValue) {
- this.validate(newValue);
- },
- }
- };
- class StdDialog {
- _options = componentOptions;
- caption = '';
- message = '';
- active = false;
- type = '';
- inputValue = '';
- error = '';
- iconColor = '';
- iconName = '';
- hotKeyCode = '';
- userName = '';
- noEscDismiss = false;
- noBackdropDismiss = false;
- noCancel = false;
- created() {
- if (this.$root.addKeyHook) {
- this.$root.addKeyHook(this.keyHook);
- }
- }
- init(message, caption, opts) {
- this.caption = caption;
- this.message = message;
- this.ok = false;
- this.type = '';
- this.inputValidator = null;
- this.inputValue = '';
- this.error = '';
- this.showed = false;
- this.noEscDismiss = (opts && opts.noEscDismiss) || false;
- this.noBackdropDismiss = (opts && opts.noBackdropDismiss) || false;
- this.noCancel = (opts && opts.noCancel) || false;
- this.iconColor = 'text-warning';
- if (opts && opts.color) {
- this.iconColor = `text-${opts.color}`;
- }
- this.iconName = 'las la-exclamation-circle';
- if (opts && opts.iconName) {
- this.iconName = opts.iconName;
- }
- this.hotKeyCode = '';
- if (opts && opts.hotKeyCode) {
- this.hotKeyCode = opts.hotKeyCode;
- }
- }
- onHide() {
- if (this.hideTrigger) {
- this.hideTrigger();
- this.hideTrigger = null;
- }
- this.showed = false;
- }
- onShow() {
- if (this.type == 'prompt' || this.type == 'password') {
- this.enableValidator = true;
- if (this.inputValue)
- this.validate(this.inputValue);
- this.$refs.input.focus();
- }
- this.showed = true;
- }
- validate(value) {
- if (!this.enableValidator)
- return false;
- if (this.inputValidator) {
- const result = this.inputValidator(value);
- if (result !== true) {
- this.error = result;
- return false;
- }
- }
- this.error = '';
- return true;
- }
- okClick() {
- if ((this.type == 'prompt' || this.type == 'password') && !this.validate(this.inputValue)) {
- this.$refs.dialog.shake();
- return;
- }
- if (this.type == 'hotKey' && this.hotKeyCode == '') {
- this.$refs.dialog.shake();
- return;
- }
- this.ok = true;
- this.$refs.dialog.hide();
- }
- alert(message, caption, opts) {
- return new Promise((resolve) => {
- this.init(message, caption, opts);
- this.hideTrigger = () => {
- if (this.ok) {
- resolve(true);
- } else {
- resolve(false);
- }
- };
- this.type = 'alert';
- this.active = true;
- });
- }
- confirm(message, caption, opts) {
- return new Promise((resolve) => {
- this.init(message, caption, opts);
- this.hideTrigger = () => {
- if (this.ok) {
- resolve(true);
- } else {
- resolve(false);
- }
- };
- this.type = 'confirm';
- this.active = true;
- });
- }
- prompt(message, caption, opts) {
- return new Promise((resolve) => {
- this.enableValidator = false;
- this.init(message, caption, opts);
- this.hideTrigger = () => {
- if (this.ok) {
- resolve({value: this.inputValue});
- } else {
- resolve(false);
- }
- };
- this.type = 'prompt';
- if (opts) {
- this.inputValidator = opts.inputValidator || null;
- this.inputValue = opts.inputValue || '';
- }
- this.active = true;
- });
- }
- password(message, caption, opts) {
- return new Promise((resolve) => {
- this.enableValidator = false;
- this.init(message, caption, opts);
- this.hideTrigger = () => {
- if (this.ok) {
- history.pushState({}, null);
- resolve({value: this.inputValue});
- } else {
- resolve(false);
- }
- };
- this.type = 'password';
- this.userName = '';
- if (opts) {
- this.inputValidator = opts.inputValidator || null;
- this.inputValue = opts.inputValue || '';
- this.userName = opts.userName || '';
- }
- this.active = true;
- });
- }
- getHotKey(message, caption, opts) {
- return new Promise((resolve) => {
- this.init(message, caption, opts);
- this.hideTrigger = () => {
- if (this.ok) {
- resolve(this.hotKeyCode);
- } else {
- resolve(false);
- }
- };
- this.type = 'hotKey';
- this.active = true;
- });
- }
- keyHook(event) {
- if (this.active && this.showed) {
- let handled = false;
- if (this.type == 'hotKey') {
- if (event.type == 'keydown') {
- this.hotKeyCode = utils.keyEventToCode(event);
- handled = true;
- }
- } else {
- if (event.key == 'Enter') {
- this.okClick();
- handled = true;
- }
- if (event.key == 'Escape' && !this.noEscDismiss) {
- this.$nextTick(() => {
- this.$refs.dialog.hide();
- });
- handled = true;
- }
- }
- if (handled) {
- event.stopPropagation();
- event.preventDefault();
- }
- }
- }
- }
- export default vueComponent(StdDialog);
- //-----------------------------------------------------------------------------
- </script>
- <style scoped>
- .header {
- height: 50px;
- }
- .caption {
- font-size: 110%;
- overflow: hidden;
- }
- .close-icon {
- width: 50px;
- }
- .buttons {
- height: 60px;
- }
- .error {
- height: 20px;
- font-size: 80%;
- color: red;
- }
- </style>
|