1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | import Component from '@ember/component'; import layout from '../templates/components/el-message-box'; import { computed, get, set } from "@ember/object"; import {inject as service} from '@ember/service'; export default Component.extend({ layout, messagesService: service('message-box'), messageObj: null, classNames: ['el-message-box__wrapper', 'ember-element-box', 'animated'], classNameBindings: ['getClassName'], attributeBindings: ['role', 'ariaModal:aria-modal', 'ariaLabel', 'tabindex', ], role: 'dialogue', ariaModal: true, tabindex: -1, ariaLabel: computed('title', function(){ return get(this, 'title') || 'dialog'; }), getClassName: computed('messageObj.{type,iconClass,customClass,dismiss}', function () { let classNames = ''; if (get(this, 'messageObj.dismiss')) { classNames += ` fadeOut50 `; }else{ classNames += ` fadeIn50 `; } return classNames; }), icon: computed('messageObj.{iconClass.type}', function(){ let typeMap = { success: 'success', info: 'info', warning: 'warning', error: 'error' }; let type = get(this,'messageObj.type'); return get(this, 'messageObj.iconClass') || (type && typeMap[type] ? `el-icon-${ typeMap[type] }` : ''); }), isIconCenter: computed.and('messageObj.center', 'icon'), isIconCenter2: computed('messageObj.{center,message}', 'icon', function(){ let icon = get(this, 'icon'); let center = get(this, 'messageObj.center'); let message = get(this, 'messageObj.message'); return icon && !center && message; }), confirmButtonClasses: computed('messageObj.confirmButtonClass', function(){ return `el-button--primary ${get(this, 'messageObj.confirmButtonClass')}`; }), cancelButtonClasses: computed('messageObj.cancelButtonClass', function(){ return `${get(this, 'messageObj.cancelButtonClass')}`; }), getClassNameForBox: computed('messageObj.{customClass,center,dismiss}', function () { let classNames = 'animated '; if (get(this, 'messageObj.customClass')){ classNames += ' ' + get(this, 'messageObj.customClass'); } if (get(this, 'messageObj.center')){ classNames += ' el-message-box--center'; } if (get(this, 'messageObj.dismiss')) { classNames += ` fadeOutUpElCustomBox `; }else{ classNames += ` fadeInDownElCustomBox `; } return classNames; }), didInsertElement(){ get(this, 'element').focus(); }, doClose() { // this.onClose && this.onClose(); get(this, 'messagesService').removeMessage(get(this, 'messageObj')); if (this.lockScroll) { setTimeout(this.restoreBodyStyle, 200); } set(this, 'opened', true); this.doAfterClose(); // setTimeout(() => { // if (this.action) this.callback(this.action, this); // }); }, doAfterClose(){ let currentMsg = get(this, 'messageObj'); if (currentMsg.resolve) { let action = get(this, 'action'); if (action === 'confirm') { if (get(currentMsg,'showInput')) { currentMsg.resolve( {value: get(currentMsg,'inputValue'), action}); } else { currentMsg.resolve(action); } } else if (currentMsg.reject && (action === 'cancel' || action === 'close')) { currentMsg.reject(action); } } }, handleAction(action) { if (get(this, 'messageObj.type') === 'prompt' && action === 'confirm' && !this.validate()) { return; } set(this,'action', action); if (typeof get(this, 'messageObj.beforeClose') === 'function') { // this.close = this.doClose(); get(this, 'messageObj.beforeClose')(action, get(this, 'messageObj'), this); } else { this.doClose(); } }, validate() { if (get(this, 'messageObj.type') === 'prompt') { const inputPattern = get(this, 'messageObj.inputPattern'); if (inputPattern && !inputPattern.test(get(this, 'messageObj.inputValue') || '')) { set(this, 'messageObj.editorErrorMessage', get(this, 'messageObj.inputErrorMessage')); set(this, 'inputErrorClass', 'invalid'); return false; } const inputValidator = get(this, 'messageObj.inputValidator') ; if (typeof inputValidator === 'function') { const validateResult = inputValidator(get(this, 'messageObj.inputValue')); if (validateResult === false) { set(this, 'messageObj.editorErrorMessage', get(this, 'messageObj.inputErrorMessage')); set(this, 'inputErrorClass', 'invalid'); return false; } if (typeof validateResult === 'string') { set(this, 'messageObj.editorErrorMessage', validateResult); set(this, 'inputErrorClass', 'invalid'); return false; } } } set(this, 'messageObj.editorErrorMessage', ''); set(this, 'inputErrorClass', ''); return true; }, actions: { handleWrapperClick() { if (get(this, 'closeOnClickModal')) { this.handleAction(get(this,'messageObj.distinguishCancelAndClose') ? 'close' : 'cancel'); } }, // handleInputEnter(e,f) { // console.log(e, f); // // if (get(this, 'inputType') !== 'textarea') { // // return this.handleAction('confirm'); // // } // }, // getFirstFocus() { // const btn = this.$el.querySelector('.el-message-box__btns .el-button'); // const title = this.$el.querySelector('.el-message-box__btns .el-message-box__title'); // return btn || title; // }, // getInputElement() { // const inputRefs = this.$refs.input.$refs; // return inputRefs.input || inputRefs.textarea; // }, close(){ this.handleAction(get(this,'messageObj.distinguishCancelAndClose') ? 'close' : 'cancel'); }, handleAction(action) { this.handleAction(action); }, }, click(e){ if(e.target.classList.contains('el-message-box__wrapper') && get(this, 'messageObj.closeOnClickModal')){ this.handleAction(get(this,'messageObj.distinguishCancelAndClose') ? 'close' : 'cancel'); } }, keyUp(e){ if(e.keyCode === 27 && get(this, 'messageObj.closeOnPressEscape')){ this.handleAction(get(this,'messageObj.distinguishCancelAndClose') ? 'close' : 'cancel'); } }, }); |