o2.widget = o2.widget || {};
o2.widget.ImageLazyLoader = o2.ImageLazyLoader = new Class({
Implements: [Options, Events],
Extends: o2.widget.Common,
options: {
"style": "default",
"path": o2.session.path + "/widget/$ImageLazyLoader/"
},
initialize: function (node, html, options) {
this.node = node;
this.html = html;
this.setOptions(options);
this.isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
this.nodeWidth = this.node.getSize().x;
this.path = this.options.path || (o2.session.path + "/widget/$ImageLazyLoader/");
// this.cssPath = this.path + this.options.style + "/css.wcss";
//
// this._loadCss();
this.fireEvent("init");
},
load: function(callback){
debugger;
if( Browser.name === 'ie' && !this.isIE11 ){
this.parseOnerror();
this.node.set("html", this.html_new);
if(callback)callback();
}else{
this.loadResource(function () {
if(window.lozad){
this.parseHtml();
this.node.set("html", this.html_new);
var observer = lozad( this.node.querySelectorAll('img.lozad'), {
rootMargin: '1000px 0px', // syntax similar to that of CSS Margin
threshold: 0, // ratio of element convergence
enableAutoReload: true // it will reload the new image when validating attributes changes
});
observer.observe();
}else{
this.parseOnerror();
this.node.set("html", this.html_new);
}
if(callback)callback();
}.bind(this));
}
},
parseOnerror: function(){
var html = this.replaceOnAttribute(this.html);
var regexp_all = /(i?)(]+>)/gmi;
var images = this.html.match(regexp_all);
if(images){
if (images.length){
for (var i=0; i]+>)/gmi;
var images = this.html.match(regexp_all);
if(images){
if (images.length){
for (var i=0; i]+>)/gmi;
var as = html.match(regexp_a_all);
if(as){
if (as.length){
for (var i=0; i -1 ){
var a1 = this.removeAttribute(a, "href");
html = html.replace(a, a1);
}
}
}
}
return html;
},
getAttrRegExp: function( attr ){
return "\\s+" + attr + "\\s*=\\s*[\"|\'](.*?)[\"|\']";
},
getAttributeValue: function(str, attribute){
var regexp = new RegExp( this.getAttrRegExp(attribute) , "i");
var array = str.match( regexp );
return (o2.typeOf(array) === "array" && array.length === 2) ? array[1] : "";
},
addAttribute: function(str, attribute, value){
var regexp = new RegExp( "\\/*\\s*>" , "i");
return str.replace( regexp, ' ' + attribute + '="' + value + '"' + " />");
},
removeAttribute: function(str, attribute){
var regexp = new RegExp( this.getAttrRegExp(attribute) , "ig");
return str.replace( regexp, "" );
},
replaceStyles: function(str, object){
/*object 参数 {
"width" : "100px", //添加或替换
"height": "" //删除
}*/
var regexp = new RegExp( this.getAttrRegExp("style") , "i");
var array = str.match( regexp );
var newArray = [];
Object.each(object, function (value, key) {
if(value)newArray.push( key + ":" + value )
});
if( o2.typeOf(array) === "array" && array.length>1){
var styles = array[1].split(/\s*;\s*/gi);
for(var j=0; j 1){
array[1] = array[1].trim();
switch (array[0].trim() ) {
case "max-width": maxWidth = array[1]; break;
case "width": width = array[1]; break;
case "height": height = array[1]; break;
}
}
}
if( !width )width = this.getAttributeValue(imgStr, "data-width");
if( !height )height = this.getAttributeValue(imgStr, "data-height");
if( !width )width = this.getAttributeValue(imgStr, "width");
if( !height )height = this.getAttributeValue(imgStr, "height");
if( width && height ){
width = parseFloat( width );
height = parseFloat( height );
var mWidth, x, y;
if( maxWidth.indexOf("%") === maxWidth.length-1 ){
mWidth = this.nodeWidth * parseFloat(maxWidth) / 100;
}else{
mWidth = parseFloat(maxWidth);
}
if( mWidth && parseFloat(width) > mWidth ){
y = parseInt( height * ( mWidth / width ) );
x = parseInt( mWidth )
}else{
y = parseInt( height );
x = parseInt( width )
}
return {"x": x, "y": y};
}
return null;
},
loadResource: function (callback) {
var lozadPath = "../o2_lib/lozad/lozad.min.js";
var observerPath = "../o2_lib/IntersectionObserver/intersection-observer.min.js";
var observerPath_ie11 = "../o2_lib/IntersectionObserver/polyfill_ie11.min.js";
if( this.isIE11 ){
o2.load(observerPath_ie11, function () {
o2.load(lozadPath, function () { if(callback)callback(); }.bind(this));
}.bind(this));
}else if( window.IntersectionObserver && window.MutationObserver){
o2.load(lozadPath, function () { if(callback)callback(); }.bind(this));
}else{
o2.load([observerPath, observerPath_ie11], function () {
o2.load(lozadPath, function () { if(callback)callback(); }.bind(this));
}.bind(this));
}
}
});