fd-gulp-convert-encoding
Version:
convert file to assigned charset
184 lines (157 loc) • 5.87 kB
JavaScript
define('detail.modules.description.View',
['jQuery', 'Class', 'detail.core.Event', 'lofty.util.Lazyload', 'lofty.ui.Scroller'],
function($, Class, Event, LazyLoad, Scroller) {
return Class({
init: function(view, config) {
this.onEvent = $.os.supportsTouch ? 'tap' : 'click';
this.div = view;
this.config = config;
this.ratio = this.config.ratio || window.devicePixelRatio;
this.detailWidth = document.documentElement.clientWidth - 30;
this.detailHeight = document.documentElement.clientHeight - 101;
this.div = view;
this.config = config;
this.container = $('div.d-lazyload-container', this.div);
this._setContentHeight();
this._loadDesc();
this._bindCloseEvent();
},
_setContentHeight: function(){
$('div.d-content', this.div).css('height', this.detailHeight + 'px');
},
_bindCloseEvent: function(){
$('div.d-header', this.div).on(this.onEvent, function(e){
e.preventDefault();
Event.trigger('closeDescriptionDialog');
});
this.div.on('swipeRight', function(e){
e.preventDefault();
Event.trigger('closeDescriptionDialog');
});
},
_loadDesc: function(){
if (this.config.detailUrl){
this._initLazyLoadDescriptionTFS();
} else {
this._initLazyLoadDescription();
}
},
_initLazyLoadDescriptionTFS: function(){
var that = this,
descURLTFS = this.config.detailUrl;
//新老地址处理
if (typeof(descURLTFS) === "string" && descURLTFS.indexOf("/tfscom/") >= 0 && descURLTFS.indexOf("?t=") < 0){
descURLTFS = descURLTFS + '?t=' + new Date().getTime();
}
$.ajax(descURLTFS, {
dataType: 'script',
timeout: 3000,
success: function(){
if (descURLTFS.indexOf("/tfscom/") >= 0){
if (window.offer_details && window.offer_details.content){
that._descriptionSuccessCallback(window.offer_details.content);
} else {
that._initLazyLoadDescription();
}
} else {
if (window.desc){
that._descriptionSuccessCallback(window.desc);
} else {
that._initLazyLoadDescription();
}
}
},
error: function(){
that._initLazyLoadDescription();
}
});
},
_initLazyLoadDescription:function(){
var descURL = this.config.oldDetailUrl,
offerId = this.config.offerId,
memberId = this.config.memberId,
params={
'offerId': offerId,
'memberId': memberId
},
that = this;
//获取offer详情的数据
$.ajax(descURL + "?" + $.paramSpecial(params), {
dataType: 'jsonp',
success: function(o){
if (o.success === true){
that._descriptionSuccessCallback(o.offerDesc);
} else {
that.container.html('详情加载失败');
}
},
error: function(){
that.container.html('详情加载失败');
}
});
},
_descriptionSuccessCallback: function(o){
var heightStyle = '',
widthStyle = '',
imgRegx = /(<img[^>]*)(src *= *("[^"]*"|'[^']*'|[^ >]*))/g,
heightRegx = /(height*=*("[^"]*"|'[^']*'|[^ >]*))/g,
widthRegx = /(width*=*("[^"]*"|'[^']*'|[^ >]*))/g,
html = '', imgCount, count = 0, imgs, tempEl,
SPACEBALL = 'http://img.china.alibaba.com/cms/upload/other/lazyload.png',
that = this;
html = o || '';
imgCount = html.match(imgRegx) ? html.match(imgRegx).length : 0;
html = html.replace(imgRegx, function(m, p1, p2, p3) {
count++;
//if (count < 1) return m; // 前 1 个图片不处理
if (m.indexOf('width') !== -1){
widthStyle = m.match(widthRegx);
} else {
widthStyle = '';
}
if (m.indexOf('height') !== -1){
heightStyle = m.match(heightRegx);
} else {
heightStyle = '';
}
return [
p1,
'src="' + SPACEBALL + '" data-lazyload-src=',
p3,
" ",
widthStyle,
" ",
heightStyle
].join('');
});
/* ref: http://blog.stevenlevithan.com/archives/faster-than-innerhtml */
this.container.html(html);
var imgs = $('img', this.container);
for(var i = 0, l = imgs.length; i < l; i++){
//显示图片,一开始的时候图片都是隐藏的
imgs.eq(i).css('visibility', 'visible');
}
//图片加载后缩放到detail页宽度
imgs.bind('load',function(){
$(this).css('visibility', 'visible');
if (this.width > that.detailWidth){
$(this).css('width', '100%');
$(this).css('height', 'auto');
}
});
this._initScroller();
},
_initScroller: function(){
setTimeout(function(){
var myScroll = new Scroller({
container: '#d-content',
checkDOMChanges: true
});
var lazy = new LazyLoad({
container: '#d-content',
scroller: myScroll
});
}, 200);
}
});
});