zmp-vue
Version:
Build full featured iOS & Android apps using ZMP & Vue
223 lines (199 loc) • 7.43 kB
JavaScript
;
exports.__esModule = true;
exports.default = void 0;
var _vue = require("vue");
var _utils = require("../shared/utils");
var _mixins = require("../shared/mixins");
var _zmp = require("../shared/zmp");
var _link = _interopRequireDefault(require("./link"));
var _input = _interopRequireDefault(require("./input"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var _default = {
name: 'zmp-messagebar',
props: _extends({
sheetVisible: Boolean,
attachmentsVisible: Boolean,
top: Boolean,
resizable: {
type: Boolean,
default: true
},
bottomOffset: {
type: Number,
default: 0
},
topOffset: {
type: Number,
default: 0
},
maxHeight: Number,
resizePage: {
type: Boolean,
default: true
},
sendLink: String,
value: [String, Number, Array],
disabled: Boolean,
readonly: Boolean,
textareaId: [Number, String],
name: String,
placeholder: {
type: String,
default: 'Message'
},
init: {
type: Boolean,
default: true
}
}, _mixins.colorProps),
emits: ['change', 'input', 'focus', 'blur', 'submit', 'send', 'click', 'messagebar:attachmentdelete', 'messagebar:attachmentclick', 'messagebar:resizepage', 'update:value'],
setup: function setup(props, _ref) {
var emit = _ref.emit,
slots = _ref.slots;
var elRef = (0, _vue.ref)(null);
var areaElRef = (0, _vue.ref)(null);
var zmpMessagebar = null;
var updateSheetVisible = false;
var updateAttachmentsVisible = false;
var onChange = function onChange(event) {
emit('change', event);
};
var onInput = function onInput(event) {
emit('input', event);
emit('update:value', event.target.value);
};
var onFocus = function onFocus(event) {
emit('focus', event);
};
var onBlur = function onBlur(event) {
emit('blur', event);
};
var onClick = function onClick(event) {
var inputValue = areaElRef.value.$el;
var clear = zmpMessagebar ? function () {
zmpMessagebar.clear();
} : function () {};
emit('submit', inputValue, clear);
emit('send', inputValue, clear);
emit('click', event);
};
var onAttachmentDelete = function onAttachmentDelete(instance, attachmentEl, attachmentElIndex) {
emit('messagebar:attachmentdelete', instance, attachmentEl, attachmentElIndex);
};
var onAttachmentClick = function onAttachmentClick(instance, attachmentEl, attachmentElIndex) {
emit('messagebar:attachmentclick', instance, attachmentEl, attachmentElIndex);
};
var onResizePage = function onResizePage(instance) {
emit('messagebar:resizepage', instance);
};
(0, _vue.watch)(function () {
return props.sheetVisible;
}, function () {
if (!props.resizable || !zmpMessagebar) return;
updateSheetVisible = true;
});
(0, _vue.watch)(function () {
return props.attachmentsVisible;
}, function () {
if (!props.resizable || !zmpMessagebar) return;
updateAttachmentsVisible = true;
});
(0, _vue.onMounted)(function () {
if (!props.init) return;
if (!elRef.value) return;
var params = (0, _utils.noUndefinedProps)({
el: elRef.value,
top: props.top,
resizePage: props.resizePage,
bottomOffset: props.bottomOffset,
topOffset: props.topOffset,
maxHeight: props.maxHeight,
on: {
attachmentDelete: onAttachmentDelete,
attachmentClick: onAttachmentClick,
resizePage: onResizePage
}
});
(0, _zmp.zmpready)(function () {
zmpMessagebar = _zmp.zmp.messagebar.create(params);
});
});
(0, _vue.onUpdated)(function () {
if (!zmpMessagebar) return;
if (updateSheetVisible) {
updateSheetVisible = false;
zmpMessagebar.sheetVisible = props.sheetVisible;
zmpMessagebar.resizePage();
}
if (updateAttachmentsVisible) {
updateAttachmentsVisible = false;
zmpMessagebar.attachmentsVisible = props.attachmentsVisible;
zmpMessagebar.resizePage();
}
});
(0, _vue.onBeforeUnmount)(function () {
if (zmpMessagebar && zmpMessagebar.destroy) zmpMessagebar.destroy();
zmpMessagebar = null;
});
var classes = (0, _vue.computed)(function () {
return (0, _utils.classNames)('toolbar', 'messagebar', {
'messagebar-attachments-visible': props.attachmentsVisible,
'messagebar-sheet-visible': props.sheetVisible
}, (0, _mixins.colorClasses)(props));
});
return function () {
var valueProps = {};
if ('value' in props) valueProps.value = props.value;
var slotsDefault = slots.default,
slotsBeforeInner = slots['before-inner'],
slotsAfterInner = slots['after-inner'],
slotsSendLink = slots['send-link'],
slotsInnerStart = slots['inner-start'],
slotsInnerEnd = slots['inner-end'],
slotsBeforeArea = slots['before-area'],
slotsAfterArea = slots['after-area'];
var innerEndEls = [];
var messagebarAttachmentsEl;
var messagebarSheetEl;
if (slotsDefault) {
slotsDefault().forEach(function (vnode) {
if (typeof vnode === 'undefined') return;
var tag = vnode.type && vnode.type.name ? vnode.type.name : vnode.type;
if (tag && (tag.indexOf('messagebar-attachments') >= 0 || tag === 'ZMPMessagebarAttachments' || tag === 'zmp-messagebar-attachments')) {
messagebarAttachmentsEl = vnode;
} else if (tag && (tag.indexOf('messagebar-sheet') >= 0 || tag === 'ZMPMessagebarSheet' || tag === 'zmp-messagebar-sheet')) {
messagebarSheetEl = vnode;
} else {
innerEndEls.push(vnode);
}
});
}
return (0, _vue.h)('div', {
class: classes.value,
ref: elRef
}, [slotsBeforeInner && slotsBeforeInner(), (0, _vue.h)('div', {
class: 'toolbar-inner'
}, [slotsInnerStart && slotsInnerStart(), (0, _vue.h)('div', {
class: 'messagebar-area'
}, [slotsBeforeArea && slotsBeforeArea(), messagebarAttachmentsEl, (0, _vue.h)(_input.default, _extends({
id: props.textareaId,
ref: areaElRef,
type: 'textarea',
wrap: false,
placeholder: props.placeholder,
disabled: props.disabled,
name: props.name,
readonly: props.readonly,
resizable: props.resizable,
onInput: onInput,
onChange: onChange,
onFocus: onFocus,
onBlur: onBlur
}, valueProps)), slotsAfterArea && slotsAfterArea()]), (props.sendLink && props.sendLink.length > 0 || slotsSendLink) && (0, _vue.h)(_link.default, {
onClick: onClick
}, [slotsSendLink ? slotsSendLink() : props.sendLink]), slotsInnerEnd && slotsInnerEnd(), innerEndEls]), slotsAfterInner && slotsAfterInner(), messagebarSheetEl]);
};
}
};
exports.default = _default;