-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathangular-photoswipe.js
121 lines (101 loc) · 3.4 KB
/
angular-photoswipe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
angular-photoswipe v0.2.0
(c) 2016 Massimiliano Sartoretto <[email protected]>
License: MIT
*/
'format amd';
/* global define */
(function () {
'use strict';
function ngPhotoswipe(angular, Photoswipe) {
return angular
.module('ngPhotoswipe', [])
.directive('ngPhotoswipe', ngPhotoswipeDirective);
function ngPhotoswipeDirective($compile, $http, $templateCache) {
return {
restrict: 'AE',
replace: true,
scope: {
open: '=',
options: '=',
slides: '=',
slideSelector: '@',
template: '@',
onClose: '&'
},
link: linkFn
};
function linkFn(scope, iElement, iAttrs) {
scope.template = scope.template || 'views/ng-photoswipe.html';
$http
.get(scope.template, { cache: $templateCache })
.then(function(result) {
var template = angular.element(result.data);
iElement.append($compile(template)(scope));
});
scope.start = function () {
scope.open = true;
startGallery();
};
var startGallery = function () {
var pswpElement = document.querySelectorAll('.pswp')[0];
if (angular.isUndefined(scope.options.getThumbBoundsFn) &&
angular.isDefined(scope.slideSelector)) {
scope.options = angular.merge({}, {
getThumbBoundsFn: function(index) {
var thumbnail = document.querySelectorAll(scope.slideSelector)[index];
var pageYScroll = window.pageYOffset || document.documentElement.scrollTop;
var rect = thumbnail.getBoundingClientRect();
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
}
}, scope.options);
}
scope.gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default || false, scope.slides, scope.options);
scope.gallery.init();
scope.item = scope.gallery.currItem;
scope.gallery.listen('destroy', function () {
scope.safeApply(function () {
(scope.onClose || angular.noop)();
});
});
scope.gallery.listen('afterChange', function () {
scope.safeApply(function () {
scope.item = scope.gallery.currItem;
});
});
};
scope.$watch('open', function (nVal, oVal) {
if (nVal != oVal) {
if (nVal) {
startGallery();
}
} else if (!nVal && scope.gallery) {
scope.gallery.close();
scope.gallery = null;
}
});
scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
scope.$on('destroy', function () {
scope.gallery = null;
});
}
}
}
if (typeof define === 'function' && define.amd) {
define(['angular', 'photoswipe'], ngPhotoswipe);
} else if (typeof module !== 'undefined' && module && module.exports) {
ngPhotoswipe(angular, require('photoswipe'));
module.exports = 'ngPhotoswipe';
} else {
ngPhotoswipe(angular, (typeof global !== 'undefined' ? global : window).Photoswipe);
}
})();