Skip to content

Commit d5ab042

Browse files
committed
refactored code
1 parent 6bdae1b commit d5ab042

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

directives/map-lazy-load.js

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,52 @@
2222
*/
2323
(function() {
2424
'use strict';
25+
var $timeout, $compile, src, savedHtml;
2526

26-
angular.module('ngMap').directive('mapLazyLoad', ['$compile', '$timeout', function($compile, $timeout) {
27-
'use strict';
28-
var directiveDefinitionObject = {
29-
compile: function(tElement, tAttrs) {
30-
(!tAttrs.mapLazyLoad) && console.error('requires src with map-lazy-load');
31-
var savedHtml = tElement.html(), src = tAttrs.mapLazyLoad;
32-
/**
33-
* if already loaded, stop processing it
34-
*/
35-
if (document.querySelector('script[src="'+src+'?callback=lazyLoadCallback"]')) {
36-
return false;
37-
}
38-
39-
tElement.html(''); // will compile again after script is loaded
40-
return {
41-
pre: function(scope, element, attrs) {
42-
window.lazyLoadCallback = function() {
43-
console.log('script loaded,' + src);
44-
$timeout(function() { /* give some time to load */
45-
element.html(savedHtml);
46-
$compile(element.contents())(scope);
47-
}, 100);
48-
};
49-
if(window.google === undefined || window.google.maps === undefined) {
50-
var scriptEl = document.createElement('script');
51-
scriptEl.src = src + (src.indexOf('?') > -1 ? '&' : '?') + 'callback=lazyLoadCallback';
52-
document.body.appendChild(scriptEl);
53-
} else {
54-
element.html(savedHtml);
55-
$compile(element.contents())(scope);
56-
}
57-
}
58-
};
59-
}
27+
var preLinkFunc = function(scope, element, attrs) {
28+
window.lazyLoadCallback = function() {
29+
console.log('script loaded,' + src);
30+
$timeout(function() { /* give some time to load */
31+
element.html(savedHtml);
32+
$compile(element.contents())(scope);
33+
}, 100);
6034
};
61-
return directiveDefinitionObject;
62-
}]);
35+
36+
if(window.google === undefined || window.google.maps === undefined) {
37+
var scriptEl = document.createElement('script');
38+
scriptEl.src = src + (src.indexOf('?') > -1 ? '&' : '?') + 'callback=lazyLoadCallback';
39+
document.body.appendChild(scriptEl);
40+
} else {
41+
element.html(savedHtml);
42+
$compile(element.contents())(scope);
43+
}
44+
};
45+
46+
var compileFunc = function(tElement, tAttrs) {
47+
48+
(!tAttrs.mapLazyLoad) && console.error('requires src with map-lazy-load');
49+
savedHtml = tElement.html();
50+
src = tAttrs.mapLazyLoad;
51+
52+
/**
53+
* if already loaded, stop processing it
54+
*/
55+
if (document.querySelector('script[src="'+src+'?callback=lazyLoadCallback"]')) {
56+
return false;
57+
}
58+
59+
tElement.html(''); // will compile again after script is loaded
60+
return {
61+
pre: preLinkFunc
62+
};
63+
};
64+
65+
var mapLazyLoad = function(_$compile_, _$timeout_) {
66+
$compile = _$compile_, $timeout = _$timeout_;
67+
return {
68+
compile: compileFunc
69+
}
70+
};
71+
72+
angular.module('ngMap').directive('mapLazyLoad', mapLazyLoad);
6373
})();

0 commit comments

Comments
 (0)