Browse Source

BI-137665 fix: 代码优化

research/test
jian 12 months ago
parent
commit
ea52d833b8
  1. 429
      packages/fineui/src/base/foundation/message.js
  2. 2
      packages/fineui/typescript/base/foundation/message.ts

429
packages/fineui/src/base/foundation/message.js

@ -21,254 +21,249 @@ import {
} from "../../core"; } from "../../core";
import { Toast } from "../single"; import { Toast } from "../single";
export const Msg = (() => { let $mask, $pop;
let $mask, $pop;
const messageShows = []; const messageShows = [];
const toastStack = []; const toastStack = [];
const defaultConfig = { const defaultConfig = {
buttonHeight: 24, buttonHeight: 24,
}; };
return { export const Msg = {
alert(title, message, callback, config = defaultConfig) { alert(title, message, callback, config = defaultConfig) {
this._show(false, title, message, callback, config); this._show(false, title, message, callback, config);
}, },
confirm(title, message, callback, config = defaultConfig) { confirm(title, message, callback, config = defaultConfig) {
this._show(true, title, message, callback, config); this._show(true, title, message, callback, config);
}, },
prompt(title, message, value, callback, min_width) { toast(message, options, context) {
// Msg.prompt(title, message, value, callback, min_width); isString(options) && (options = { level: options });
}, options = options || {};
toast(message, options, context) { context = context || Widget._renderEngine.createElement("body");
isString(options) && (options = { level: options }); const level = options.level || "common";
options = options || {}; const autoClose = isNull(options.autoClose) ? true : options.autoClose;
context = context || Widget._renderEngine.createElement("body"); const callback = isFunction(options.callback) ? options.callback : emptyFn;
const level = options.level || "common"; const toast = createWidget({
const autoClose = isNull(options.autoClose) ? true : options.autoClose; type: "bi.toast",
const callback = isFunction(options.callback) ? options.callback : emptyFn; cls: "bi-message-animate bi-message-leave",
const toast = createWidget({ level,
type: "bi.toast", autoClose,
cls: "bi-message-animate bi-message-leave", closable: options.closable,
level, text: message,
autoClose, listeners: [
closable: options.closable, {
text: message, eventName: Toast.EVENT_DESTORY,
listeners: [ action() {
{ remove(toastStack, toast.element);
eventName: Toast.EVENT_DESTORY, let _height = SIZE_CONSANTS.TOAST_TOP;
action() { each(toastStack, (i, element) => {
remove(toastStack, toast.element); element.css({ top: _height });
let _height = SIZE_CONSANTS.TOAST_TOP; _height += element.outerHeight() + 10;
each(toastStack, (i, element) => { });
element.css({ top: _height }); callback();
_height += element.outerHeight() + 10; },
}); }
callback(); ],
}, });
} let height = SIZE_CONSANTS.TOAST_TOP;
], each(toastStack, (i, element) => {
}); height += element.outerHeight() + 10;
let height = SIZE_CONSANTS.TOAST_TOP; });
each(toastStack, (i, element) => { createWidget({
height += element.outerHeight() + 10; type: "bi.absolute",
}); element: context,
createWidget({ items: [
type: "bi.absolute", {
element: context, el: toast,
items: [ left: "50%",
{ top: height,
el: toast, }
left: "50%", ],
top: height, });
} toastStack.push(toast.element);
], toast.element.css({ "margin-left": (-1 * toast.element.outerWidth()) / 2 });
}); toast.element.removeClass("bi-message-leave").addClass("bi-message-enter");
toastStack.push(toast.element);
toast.element.css({ "margin-left": (-1 * toast.element.outerWidth()) / 2 });
toast.element.removeClass("bi-message-leave").addClass("bi-message-enter");
autoClose && autoClose &&
delay(() => { delay(() => {
toast.element.removeClass("bi-message-enter").addClass("bi-message-leave"); toast.element.removeClass("bi-message-enter").addClass("bi-message-leave");
toast.destroy?.(); toast.destroy?.();
}, 5000); }, 5000);
return function() { return function() {
toast.element.removeClass("bi-message-enter").addClass("bi-message-leave"); toast.element.removeClass("bi-message-enter").addClass("bi-message-leave");
toast.destroy?.(); toast.destroy?.();
}; };
}, },
_show(hasCancel, title, message, callback, config) { _show(hasCancel, title, message, callback, config) {
isNull($mask) && isNull($mask) &&
($mask = Widget._renderEngine ($mask = Widget._renderEngine
.createElement("<div class=\"bi-z-index-mask\">") .createElement("<div class=\"bi-z-index-mask\">")
.css({ .css({
position: "absolute", position: "absolute",
zIndex: zIndex_tip - 2, zIndex: zIndex_tip - 2,
top: 0, top: 0,
left: 0, left: 0,
right: 0, right: 0,
bottom: 0, bottom: 0,
opacity: 0.5, opacity: 0.5,
}) })
.appendTo("body")); .appendTo("body"));
$pop = Widget._renderEngine $pop = Widget._renderEngine
.createElement("<div class=\"bi-message-depend\">") .createElement("<div class=\"bi-message-depend\">")
.css({ .css({
position: "absolute", position: "absolute",
zIndex: zIndex_tip - 1, zIndex: zIndex_tip - 1,
top: 0, top: 0,
left: 0, left: 0,
right: 0, right: 0,
bottom: 0, bottom: 0,
}) })
.appendTo("body"); .appendTo("body");
function close() { function close() {
messageShows[messageShows.length - 1].destroy(); messageShows[messageShows.length - 1].destroy();
messageShows.pop(); messageShows.pop();
if (messageShows.length === 0) { if (messageShows.length === 0) {
$mask.remove(); $mask.remove();
$mask = null; $mask = null;
}
} }
}
const controlItems = []; const controlItems = [];
if (hasCancel === true) { if (hasCancel === true) {
controlItems.push({
el: {
type: "bi.button",
height: config.buttonHeight,
text: i18nText("BI-Basic_Cancel"),
light: true,
handler() {
close();
if (isFunction(callback)) {
callback.apply(null, [false]);
}
},
},
});
}
controlItems.push({ controlItems.push({
el: { el: {
type: "bi.button", type: "bi.button",
height: config.buttonHeight, height: config.buttonHeight,
text: i18nText("BI-Basic_OK"), text: i18nText("BI-Basic_Cancel"),
light: true,
handler() { handler() {
close(); close();
if (isFunction(callback)) { if (isFunction(callback)) {
callback.apply(null, [true]); callback.apply(null, [false]);
} }
}, },
}, },
}); });
const conf = { }
element: $pop, controlItems.push({
type: "bi.center_adapt", el: {
items: [ type: "bi.button",
{ height: config.buttonHeight,
type: "bi.border", text: i18nText("BI-Basic_OK"),
attributes: { handler() {
tabIndex: 1, close();
}, if (isFunction(callback)) {
mounted() { callback.apply(null, [true]);
this.element.keyup(e => { }
if (e.keyCode === KeyCode.ENTER) { },
close(); },
});
const conf = {
element: $pop,
type: "bi.center_adapt",
items: [
{
type: "bi.border",
attributes: {
tabIndex: 1,
},
mounted() {
this.element.keyup(e => {
if (e.keyCode === KeyCode.ENTER) {
close();
if (isFunction(callback)) {
callback.apply(null, [true]);
}
} else if (e.keyCode === KeyCode.ESCAPE) {
close();
if (hasCancel === true) {
if (isFunction(callback)) { if (isFunction(callback)) {
callback.apply(null, [true]); callback.apply(null, [false]);
}
} else if (e.keyCode === KeyCode.ESCAPE) {
close();
if (hasCancel === true) {
if (isFunction(callback)) {
callback.apply(null, [false]);
}
} }
} }
});
try {
this.element.focus();
} catch (e) {
} }
}, });
cls: "bi-card", try {
items: { this.element.focus();
north: { } catch (e) {
el: { }
type: "bi.border", },
cls: "bi-message-title bi-background", cls: "bi-card",
items: { items: {
center: { north: {
el: { el: {
type: "bi.label", type: "bi.border",
cls: "bi-font-bold", cls: "bi-message-title bi-background",
text: title || i18nText("BI-Basic_Prompt"), items: {
textAlign: "left", center: {
hgap: 20, el: {
height: 40, type: "bi.label",
}, cls: "bi-font-bold",
text: title || i18nText("BI-Basic_Prompt"),
textAlign: "left",
hgap: 20,
height: 40,
}, },
east: { },
el: { east: {
type: "bi.icon_button", el: {
cls: "bi-message-close close-font", type: "bi.icon_button",
// height: 50, cls: "bi-message-close close-font",
handler() { // height: 50,
close(); handler() {
if (isFunction(callback)) { close();
callback.apply(null, [false]); if (isFunction(callback)) {
} callback.apply(null, [false]);
}, }
}, },
width: 56,
}, },
width: 56,
}, },
}, },
height: 40,
},
center: {
el: isPlainObject(message)
? message
: {
type: "bi.label",
vgap: 10,
hgap: 20,
whiteSpace: "normal",
text: message,
},
}, },
south: { height: 40,
el: { },
type: "bi.absolute", center: {
items: [ el: isPlainObject(message)
{ ? message
el: { : {
type: "bi.right_vertical_adapt", type: "bi.label",
lgap: 10, vgap: 10,
items: controlItems, hgap: 20,
}, whiteSpace: "normal",
top: 0, text: message,
left: 20,
right: 20,
bottom: 0,
}
],
}, },
height: 44, },
south: {
el: {
type: "bi.absolute",
items: [
{
el: {
type: "bi.right_vertical_adapt",
lgap: 10,
items: controlItems,
},
top: 0,
left: 20,
right: 20,
bottom: 0,
}
],
}, },
height: 44,
}, },
width: 450, },
height: 200, width: 450,
} height: 200,
], }
}; ],
};
messageShows[messageShows.length] = createWidget(conf); messageShows[messageShows.length] = createWidget(conf);
}, },
}; };
})();

2
packages/fineui/typescript/base/foundation/message.ts

@ -14,7 +14,5 @@ export declare namespace Msg {
[key: string]: any [key: string]: any
}, callback?: (result: boolean) => void): void }, callback?: (result: boolean) => void): void
function prompt(title: string, message?: string, value?: any, callback?: (result: string) => void, minWidth?: number): void
function toast(message: string | Obj, options?: toastOptions | string, context?: HTMLElement): Function function toast(message: string | Obj, options?: toastOptions | string, context?: HTMLElement): Function
} }

Loading…
Cancel
Save