/*md5:e40ed5a66051755c233046bef2f19e2f*/
/**
* 缩略图+预览
* 设计规范:https://aone.alibaba-inc.com/req/31336436
*/
import Magix from 'magix';
import * as $ from '$';
import * as View from '../mx-util/view';
Magix.applyStyle('@index.less');
let Active; //优化大量预览
export default View.extend({
tmpl: '@index.html',
init(extra) {
let that = this;
that.assign(extra);
that.on('destroy', () => {
clearTimeout(that.timer);
$('#pic_preview_' + that.id).remove();
});
},
assign(extra) {
let that = this;
that.updater.snapshot();
// 缩略图尺寸
let maxWidth = +extra.maxWidth || 100,
maxHeight = +extra.maxHeight || 100;
// 语义化展示类型
let type;
// 兼容历史api,映射成语义方便处理)
// format,不配置默认展示为文字
// 2:图片
// 4:视频
// 5:文字链
// 3:flash
// 9:flash
// 10:html,iframe展示
// 11:直播(封面图)也是图片
if (extra.format) {
let format = +extra.format;
let map = {
'image': [2, 11],
'flash': [3, 9],
'video': [4],
'text': [5],
'iframe': [10]
}
for (let t in map) {
if (map[t].indexOf(format) > -1) {
type = t;
}
}
type = type || 'text';
} else {
type = extra.type || 'text';
}
// 是否需要预览,不需要则使用失败icon
let thumbnail = (extra.thumbnail + '' !== 'false');
// 占位icon,尺寸为宽高较小值的4/15,icon留白算成9/25
let holder = (thumbnail ? {
'image': '',
'flash': '',
'video': '',
'text': '',
'iframe': ''
} : {
'image': '',
'flash': '',
'video': '',
'text': '',
'iframe': ''
})[type];
let holderSize;
if (maxWidth / maxHeight > 1) {
holderSize = Math.ceil(maxHeight * 9 / 25);
} else {
holderSize = Math.ceil(maxWidth * 9 / 25);
}
that.updater.set({
tip: extra.tip || '',
thumbnail, //是否需要预览
preview: (extra.preview + '' !== 'false'), //是否需要预览
placement: extra.placement || 'right', //展示位置,左边 or 右边
border: (extra.border + '' === 'true'), // 是否需要border+背景色
type,
holder,
holderSize,
url: extra.url,
clickUrl: extra.clickUrl, //图点击跳转外链,没有可不配
width: +extra.width, // 预览展示尺寸,图片文案可不配置,其余必填
height: +extra.height,
rangeWidth: +extra.rangeWidth, // 预览图限制尺寸,默认屏幕大小
rangeHeight: +extra.rangeHeight,
maxWidth, // 缩略图尺寸,默认100
maxHeight,
previewData: $.extend(true, {}, extra.previewData),
previewView: extra.previewView || '',
videoAutoplay: extra.videoAutoplay + '' !== 'false',
videoControls: extra.videoControls + '' !== 'false',
videoMuted: extra.videoMuted + '' !== 'false',
videoPoster: extra.videoPoster
});
// altered是否有变化 true:有变化
let altered = this.updater.altered();
return altered;
},
render() {
let that = this;
that.updater.digest();
let { thumbnail } = that.updater.get();
if (thumbnail) {
if (window.IntersectionObserver) {
// 延迟加载预览图
let observer = new IntersectionObserver(changes => {
changes.forEach((t) => {
let target = t.target;
if (t.isIntersecting || (t.intersectionRatio > 0)) {
that.thumbnail();
observer.unobserve(target);
};
})
}, {
rootMargin: '8px 0px'
});
observer.observe(document.querySelector('#' + that.id));
that.capture('observer', {
destroy() {
observer.disconnect();
}
});
} else {
// 直接加载
that.thumbnail();
}
}
},
thumbnail() {
let { type, url, maxWidth, maxHeight, width, height, clickUrl, videoPoster } = this.updater.get();
let thumbnail = '';
switch (type) {
case 'image':
thumbnail = ``;
break;
case 'flash':
thumbnail = 'flash已下线';
break;
case 'video':
thumbnail = ``;
break;
case 'text':
thumbnail = $(`