|
4 | 4 | angular.module('ezplus', []) |
5 | 5 | .directive('ezPlus', ezPlus); |
6 | 6 |
|
7 | | - function ezPlus() { |
| 7 | + function ezPlus($document) { |
8 | 8 | var service = { |
9 | 9 | restrict: 'A', |
10 | 10 | scope: { |
|
27 | 27 | link.$inject = ['$scope', '$element', '$attributes']; |
28 | 28 | function link($scope, $element, $attributes) { |
29 | 29 | var bootstrapped = false; |
| 30 | + var lastPlugin = null; |
| 31 | + var zoomIds = {}; |
30 | 32 | var options = { |
31 | 33 | onComplete: function () { |
32 | 34 | if ($scope.onComplete && $scope.onComplete()) { |
|
86 | 88 | showZoom(); |
87 | 89 | }); |
88 | 90 | $scope.$on('ezp-disableZoom', function (e, msg) { |
89 | | - var plugin = angular.element($element).data('ezPlus'); |
| 91 | + var plugin = getZoomPlugin(); |
90 | 92 | if (plugin) { |
91 | 93 | plugin.changeState('disable'); |
92 | 94 | } |
93 | 95 | }); |
94 | 96 | $scope.$on('ezp-enableZoom', function (e, msg) { |
95 | | - var plugin = angular.element($element).data('ezPlus'); |
| 97 | + var plugin = getZoomPlugin(); |
96 | 98 | if (plugin) { |
97 | 99 | plugin.changeState('enable'); |
98 | 100 | } |
|
102 | 104 | if (!bootstrapped) { |
103 | 105 | bootstrapped = true; |
104 | 106 | } else { |
105 | | - var plugin = angular.element($element).data('ezPlus'); |
| 107 | + var plugin = getZoomPlugin(); |
106 | 108 | plugin.destroy(); |
107 | 109 | angular.extend(options, $scope.ezpOptions); |
108 | 110 | if (plugin) { |
109 | | - angular.element($element).ezPlus(options); |
| 111 | + preparePlugin($element, options); |
110 | 112 | } |
111 | 113 | } |
112 | 114 | }, true); |
|
116 | 118 | var smallUrl = (image && image.small) || ''; |
117 | 119 | var largeUrl = (image && image.large) || ''; |
118 | 120 |
|
119 | | - var plugin = angular.element($element).data('ezPlus'); |
| 121 | + var initialUrl = null; |
| 122 | + var plugin = getZoomPlugin(); |
120 | 123 | if (plugin) { |
121 | 124 | if (image) { |
122 | 125 | hideZoom(); |
123 | 126 | if (loader) { |
124 | 127 | plugin.swaptheimage(loader, loader); |
125 | 128 | } |
126 | 129 |
|
127 | | - var initialUrl = getInitialUrl(smallUrl); |
| 130 | + initialUrl = getInitialUrl(options, smallUrl); |
128 | 131 | plugin.swaptheimage(initialUrl, largeUrl); |
129 | 132 | showZoom(); |
130 | 133 | } else { |
|
133 | 136 | } else { |
134 | 137 | if (image) { |
135 | 138 |
|
136 | | - var initialUrl = getInitialUrl(); |
| 139 | + initialUrl = getInitialUrl(options); |
137 | 140 | if (initialUrl) { |
138 | 141 | $element.attr('src', initialUrl); |
139 | 142 | } |
140 | 143 |
|
141 | 144 | $element.attr('data-zoom-image', largeUrl); |
142 | 145 |
|
143 | | - angular.element($element).ezPlus(options); |
| 146 | + preparePlugin($element, options); |
144 | 147 | } |
145 | 148 | } |
146 | 149 |
|
147 | | - function getInitialUrl(defaultUrl) { |
| 150 | + function getInitialUrl(options, defaultUrl) { |
148 | 151 | var initialUrl = defaultUrl; |
149 | 152 | if (options.initial === 'thumb') { |
150 | 153 | initialUrl = thumbUrl; |
|
157 | 160 | } |
158 | 161 | }); |
159 | 162 |
|
160 | | - $scope.$on('$destroy', function () { |
161 | | - var plugin = angular.element($element).data('ezPlus'); |
| 163 | + $scope.$on('$destroy', destroyPlugin); |
| 164 | + |
| 165 | + function destroyPlugin() { |
| 166 | + var plugin = getZoomPlugin(); |
162 | 167 | if (plugin) { |
163 | 168 | plugin.destroy(); |
164 | 169 | } |
165 | | - }); |
| 170 | + |
| 171 | + //in case the $destroy is called after the actual plugin element was removed, otherwise zoom containers |
| 172 | + //will be visible. |
| 173 | + for (var zoomId in zoomIds) { |
| 174 | + if (zoomIds.hasOwnProperty(zoomId)) { |
| 175 | + var zoomContainer = findZoomContainer(zoomId); |
| 176 | + zoomContainer.remove(); |
| 177 | + } |
| 178 | + } |
| 179 | + zoomIds = {}; |
| 180 | + } |
| 181 | + |
| 182 | + function findZoomContainer(uuid) { |
| 183 | + return angular.element($document).find('[uuid=' + uuid + ']'); |
| 184 | + } |
| 185 | + |
| 186 | + function preparePlugin(element, options) { |
| 187 | + var plugin = angular.element(element).ezPlus(options); |
| 188 | + lastPlugin = plugin && plugin.length > 0 ? getZoomPlugin(plugin[0]) : null; |
| 189 | + if (lastPlugin) { |
| 190 | + zoomIds[lastPlugin.options.zoomId] = true; |
| 191 | + } |
| 192 | + return lastPlugin; |
| 193 | + } |
| 194 | + |
| 195 | + function getZoomPlugin(element) { |
| 196 | + var plugin = angular.element(element ? element : $element).data('ezPlus'); |
| 197 | + return plugin; |
| 198 | + } |
166 | 199 |
|
167 | 200 | function hideZoom() { |
168 | 201 | var action = 'hide'; |
169 | | - var plugin = angular.element($element).data('ezPlus'); |
| 202 | + var plugin = getZoomPlugin(); |
170 | 203 | if (plugin) { |
171 | 204 | plugin.showHideZoomContainer(action); |
172 | 205 | /*plugin.showHideWindow(action); |
|
177 | 210 |
|
178 | 211 | function showZoom() { |
179 | 212 | var action = 'show'; |
180 | | - var plugin = angular.element($element).data('ezPlus'); |
| 213 | + var plugin = getZoomPlugin(); |
181 | 214 | if (plugin) { |
182 | 215 | /*plugin.showHideLens(action); |
183 | 216 | plugin.showHideTint(action); |
|
0 commit comments