|
22 | 22 | */
|
23 | 23 | (function() {
|
24 | 24 | 'use strict';
|
| 25 | + var $timeout, $compile, src, savedHtml; |
25 | 26 |
|
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); |
60 | 34 | };
|
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); |
63 | 73 | })();
|
0 commit comments