ImageLazyLoader.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. o2.widget = o2.widget || {};
  2. o2.widget.ImageLazyLoader = o2.ImageLazyLoader = new Class({
  3. Implements: [Options, Events],
  4. Extends: o2.widget.Common,
  5. options: {
  6. "style": "default",
  7. "path": o2.session.path + "/widget/$ImageLazyLoader/"
  8. },
  9. initialize: function (node, html, options) {
  10. this.node = node;
  11. this.html = html;
  12. this.setOptions(options);
  13. this.isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
  14. this.nodeWidth = this.node.getSize().x;
  15. this.path = this.options.path || (o2.session.path + "/widget/$ImageLazyLoader/");
  16. // this.cssPath = this.path + this.options.style + "/css.wcss";
  17. //
  18. // this._loadCss();
  19. this.fireEvent("init");
  20. },
  21. load: function(callback){
  22. debugger;
  23. if( Browser.name === 'ie' && !this.isIE11 ){
  24. this.parseOnerror();
  25. this.node.set("html", this.html_new);
  26. if(callback)callback();
  27. }else{
  28. this.loadResource(function () {
  29. if(window.lozad){
  30. this.parseHtml();
  31. this.node.set("html", this.html_new);
  32. var observer = lozad( this.node.querySelectorAll('img.lozad'), {
  33. rootMargin: '1000px 0px', // syntax similar to that of CSS Margin
  34. threshold: 0, // ratio of element convergence
  35. enableAutoReload: true // it will reload the new image when validating attributes changes
  36. });
  37. observer.observe();
  38. }else{
  39. this.parseOnerror();
  40. this.node.set("html", this.html_new);
  41. }
  42. if(callback)callback();
  43. }.bind(this));
  44. }
  45. },
  46. parseOnerror: function(){
  47. var html = this.replaceOnAttribute(this.html);
  48. var regexp_all = /(i?)(<img)([^>]+>)/gmi;
  49. var images = this.html.match(regexp_all);
  50. if(images){
  51. if (images.length){
  52. for (var i=0; i<images.length; i++){
  53. var image = images[i];
  54. var image1 = this.removeAttribute(image, "onerror");
  55. image1 = this.addAttribute(image1, "onerror", "MWF.xDesktop.setImageSrc()");
  56. html = html.replace(image, image1);
  57. }
  58. }
  59. }
  60. html = this.replaceHrefJavascriptStr( html );
  61. this.html_new = html;
  62. },
  63. parseHtml: function(){
  64. var html = this.replaceOnAttribute(this.html);
  65. var regexp_all = /(i?)(<img)([^>]+>)/gmi;
  66. var images = this.html.match(regexp_all);
  67. if(images){
  68. if (images.length){
  69. for (var i=0; i<images.length; i++){
  70. var image = images[i];
  71. var image1 = this.removeAttribute(image, "onerror");
  72. image1 = this.addAttribute(image1, "onerror", "MWF.xDesktop.setImageSrc()");
  73. var src = this.getAttributeValue(image, "src");
  74. if( src.substr(0, 5) !== "data:" ){ //不是base64位
  75. var size = this.getSize(image);
  76. if( size ){
  77. image1 = this.removeAttribute(image1, "src");
  78. var id = this.getAttributeValue(image1, "data-id");
  79. var src2 = id ? MWF.xDesktop.getImageSrc(id) : src;
  80. image1 = this.addAttribute(image1, "data-src", src2);
  81. image1 = this.replaceStyles(image1, {
  82. "height": size.y+"px",
  83. "width": size.x+"px"
  84. });
  85. image1 = this.addAttribute(image1, "class", "lozad");
  86. }
  87. }
  88. html = html.replace(image, image1);
  89. }
  90. }
  91. }
  92. html = this.replaceHrefJavascriptStr( html );
  93. this.html_new = html;
  94. },
  95. replaceOnAttribute: function (htmlString){
  96. var tempDiv = document.createElement('div');
  97. tempDiv.innerHTML = htmlString;
  98. var elements = tempDiv.getElementsByTagName('*');
  99. for (var i = 0; i < elements.length; i++) {
  100. var element = elements[i];
  101. var attributeNames = element.getAttributeNames();
  102. for (var j = 0; j < attributeNames.length; j++) {
  103. var attributeName = attributeNames[j];
  104. if (attributeName.substr(0,2).toLowerCase() === 'on') {
  105. element.removeAttribute(attributeName);
  106. }
  107. }
  108. }
  109. return tempDiv.innerHTML;
  110. },
  111. replaceHrefJavascriptStr: function( html ){
  112. var regexp_a_all = /(i?)(<a)([^>]+>)/gmi;
  113. var as = html.match(regexp_a_all);
  114. if(as){
  115. if (as.length){
  116. for (var i=0; i<as.length; i++){
  117. var a = as[i];
  118. var href = this.getAttributeValue(a, "href");
  119. if( href.indexOf('javascript:') > -1 ){
  120. var a1 = this.removeAttribute(a, "href");
  121. html = html.replace(a, a1);
  122. }
  123. }
  124. }
  125. }
  126. return html;
  127. },
  128. getAttrRegExp: function( attr ){
  129. return "\\s+" + attr + "\\s*=\\s*[\"|\'](.*?)[\"|\']";
  130. },
  131. getAttributeValue: function(str, attribute){
  132. var regexp = new RegExp( this.getAttrRegExp(attribute) , "i");
  133. var array = str.match( regexp );
  134. return (o2.typeOf(array) === "array" && array.length === 2) ? array[1] : "";
  135. },
  136. addAttribute: function(str, attribute, value){
  137. var regexp = new RegExp( "\\/*\\s*>" , "i");
  138. return str.replace( regexp, ' ' + attribute + '="' + value + '"' + " />");
  139. },
  140. removeAttribute: function(str, attribute){
  141. var regexp = new RegExp( this.getAttrRegExp(attribute) , "ig");
  142. return str.replace( regexp, "" );
  143. },
  144. replaceStyles: function(str, object){
  145. /*object 参数 {
  146. "width" : "100px", //添加或替换
  147. "height": "" //删除
  148. }*/
  149. var regexp = new RegExp( this.getAttrRegExp("style") , "i");
  150. var array = str.match( regexp );
  151. var newArray = [];
  152. Object.each(object, function (value, key) {
  153. if(value)newArray.push( key + ":" + value )
  154. });
  155. if( o2.typeOf(array) === "array" && array.length>1){
  156. var styles = array[1].split(/\s*;\s*/gi);
  157. for(var j=0; j<styles.length; j++){
  158. var ar = styles[j].split(/\s*:\s*/gi);
  159. var key = ar[0].toLowerCase();
  160. if( !object.hasOwnProperty( key ) ){
  161. newArray.push( styles[j] );
  162. }
  163. }
  164. }
  165. if(o2.typeOf(array) === "array" && array[0]){ //原先有style
  166. if( newArray.length === 0 ){
  167. return str.replace(array[0], "")
  168. }else{
  169. return str.replace(array[0], " style=\""+ newArray.join("; ") + "\"")
  170. }
  171. }else{
  172. if( newArray.length === 0 ){
  173. return str;
  174. }else{
  175. return this.addAttribute(str, "style", newArray.join("; "));
  176. }
  177. }
  178. },
  179. getSize: function(imgStr){
  180. //获取占位图片高宽,先从style获取高宽,没有从data-width获取,并判断maxWidth
  181. var width="", height="", maxWidth="";
  182. var style = this.getAttributeValue(imgStr, "style");
  183. var styles = style.split(";");
  184. for(var j=0; j<styles.length; j++){
  185. var array = styles[j].split(":");
  186. if(array.length > 1){
  187. array[1] = array[1].trim();
  188. switch (array[0].trim() ) {
  189. case "max-width": maxWidth = array[1]; break;
  190. case "width": width = array[1]; break;
  191. case "height": height = array[1]; break;
  192. }
  193. }
  194. }
  195. if( !width )width = this.getAttributeValue(imgStr, "data-width");
  196. if( !height )height = this.getAttributeValue(imgStr, "data-height");
  197. if( !width )width = this.getAttributeValue(imgStr, "width");
  198. if( !height )height = this.getAttributeValue(imgStr, "height");
  199. if( width && height ){
  200. width = parseFloat( width );
  201. height = parseFloat( height );
  202. var mWidth, x, y;
  203. if( maxWidth.indexOf("%") === maxWidth.length-1 ){
  204. mWidth = this.nodeWidth * parseFloat(maxWidth) / 100;
  205. }else{
  206. mWidth = parseFloat(maxWidth);
  207. }
  208. if( mWidth && parseFloat(width) > mWidth ){
  209. y = parseInt( height * ( mWidth / width ) );
  210. x = parseInt( mWidth )
  211. }else{
  212. y = parseInt( height );
  213. x = parseInt( width )
  214. }
  215. return {"x": x, "y": y};
  216. }
  217. return null;
  218. },
  219. loadResource: function (callback) {
  220. var lozadPath = "../o2_lib/lozad/lozad.min.js";
  221. var observerPath = "../o2_lib/IntersectionObserver/intersection-observer.min.js";
  222. var observerPath_ie11 = "../o2_lib/IntersectionObserver/polyfill_ie11.min.js";
  223. if( this.isIE11 ){
  224. o2.load(observerPath_ie11, function () {
  225. o2.load(lozadPath, function () { if(callback)callback(); }.bind(this));
  226. }.bind(this));
  227. }else if( window.IntersectionObserver && window.MutationObserver){
  228. o2.load(lozadPath, function () { if(callback)callback(); }.bind(this));
  229. }else{
  230. o2.load([observerPath, observerPath_ie11], function () {
  231. o2.load(lozadPath, function () { if(callback)callback(); }.bind(this));
  232. }.bind(this));
  233. }
  234. }
  235. });