You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
1.6 KiB
87 lines
1.6 KiB
5 years ago
|
const config = require('../config');
|
||
|
Component({
|
||
|
options: {
|
||
|
styleIsolation: 'shared'
|
||
|
},
|
||
|
properties: {
|
||
|
data: {
|
||
|
type: Object,
|
||
|
value: {}
|
||
|
}
|
||
|
},
|
||
|
data: {
|
||
|
attr:{
|
||
|
src:'',
|
||
|
class:'',
|
||
|
style:''
|
||
|
},
|
||
|
size:{
|
||
|
w:0,
|
||
|
h:0
|
||
|
},
|
||
|
styleObj:{}
|
||
|
},
|
||
|
lifetimes:{
|
||
|
attached:function(){
|
||
|
const _ts = this;
|
||
|
let dataAttr = this.data.data.attr;
|
||
|
if(dataAttr.width){
|
||
|
_ts.data.size.w = +dataAttr.width / config.dpr;
|
||
|
};
|
||
|
|
||
|
if(dataAttr.height){
|
||
|
_ts.data.size.h = +dataAttr.height / config.dpr;
|
||
|
};
|
||
|
if(dataAttr.style){
|
||
|
let re = /;\s{0,}/ig;
|
||
|
dataAttr.style = dataAttr.style.replace(re,';');
|
||
|
dataAttr.style.split(';').forEach(item => {
|
||
|
let itemArr = item.split(':');
|
||
|
if(/^(width|height)$/i.test(itemArr[0])){
|
||
|
let num = parseInt(itemArr[1]) || 0,
|
||
|
key = '';
|
||
|
switch (itemArr[1].toLocaleLowerCase()) {
|
||
|
case 'width':
|
||
|
key = 'w';
|
||
|
break;
|
||
|
case 'height':
|
||
|
key = 'h';
|
||
|
break;
|
||
|
};
|
||
|
_ts.data.size[key] = num / config.dpr;
|
||
|
}else{
|
||
|
_ts.data.styleObj[itemArr[0]] = itemArr[1];
|
||
|
};
|
||
|
});
|
||
|
};
|
||
|
_ts.setData({
|
||
|
attr:{
|
||
|
src:dataAttr.src,
|
||
|
class:dataAttr.class,
|
||
|
style:_ts.setStyle(_ts.data.styleObj),
|
||
|
size:_ts.data.size
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
},
|
||
|
methods: {
|
||
|
setStyle:function(o){
|
||
|
let str = ``;
|
||
|
for(let key in o){
|
||
|
str += `${key}:${o[key]};`;
|
||
|
};
|
||
|
return str;
|
||
|
},
|
||
|
load:function(e){
|
||
|
const _ts = this;
|
||
|
if(!_ts.data.styleObj.width || !_ts.data.styleObj.height){
|
||
|
_ts.setData({
|
||
|
size:{
|
||
|
w:e.detail.width / config.dpr,
|
||
|
h:e.detail.height / config.dpr
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
})
|