-
Notifications
You must be signed in to change notification settings - Fork 4
/
angular-flipsnap.js
132 lines (103 loc) · 5.04 KB
/
angular-flipsnap.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
122
123
124
125
126
127
128
129
130
131
132
(function () {
'use strict';
angular
.module('flipsnap', [])
.directive('flipsnap', ['$window', '$parse', '$timeout', function($window, $parse, $timeout) {
return {
transclude: true,
restrict: 'A',
scope: { },
template: '<div ng-transclude></div>',
link: function($scope, $element, $attrs) {
if(!$window.Flipsnap) {
$window.Flipsnap = function(matcher, options) { };
}
var $flipsnap = angular.element($element.children()[0]);
var $parent = $scope.$parent;
$attrs.flipsnapOptions = $attrs.flipsnapOptions || {};
$attrs.flipsnapId = $attrs.flipsnapId || 'flipsnap';
$attrs.flipsnap = $attrs.flipsnap || 'flipsnap';
var options = $parse($attrs.flipsnapOptions)($parent);
var name = $attrs.flipsnap;
var id = $attrs.flipsnapId;
var hiddenReset = $flipsnap[0].hidden;
$flipsnap[0].hidden = true;
var repeatWatchers = {};
var optionsWatch;
function hasAttribute(element, attrName) {
var attr = element.attr(attrName);
return (attr !== undefined) && (attr !== null);
}
function isFlipsnapPane(element) {
return hasAttribute(element, 'flipsnap-pane');
}
function isNgRepeat(element) {
return hasAttribute(element, 'ng-repeat');
}
function uniqueWatch(target) {
if(!repeatWatchers[target]) {
repeatWatchers[target] = $parent.$watch(target, function() {
$timeout(function() {
completeLayout();
generateFlipsnap();
});
}, true);
}
}
function completeLayout() {
$flipsnap[0].hidden = hiddenReset;
var totalWidth = 0;
if($flipsnap.children().length > 0) {
angular.forEach($flipsnap.children(), function(child) {
child = angular.element(child);
if(isFlipsnapPane(child)) {
totalWidth += child[0].offsetWidth;
child.css('float', 'left');
}
if(isNgRepeat(child)) {
var target = child.attr('ng-repeat').match(/in\s+(.+)/)[1];
uniqueWatch(target);
}
});
} else if($flipsnap[0].innerHTML.match(/ngRepeat/)) {
var target = $flipsnap[0].innerHTML.match(/in\s+(.+)\s-->/)[1];
uniqueWatch(target);
}
$element.css('overflow', 'hidden');
$flipsnap.css('width', (totalWidth)+'px');
$flipsnap.attr('id', id);
optionsWatch = optionsWatch || $parent.$watch($attrs.flipsnapOptions, generateFlipsnap, true);
}
function generateFlipsnap() {
var oldFlipsnap = $parent[name];
var newFlipsnap = $window.Flipsnap('#'+id, options);
newFlipsnap.element.addEventListener('mousemove', function(e) {
e.stopPropagation();
});
newFlipsnap.element.addEventListener('touchmove', function(e) {
e.stopPropagation();
});
$parent[name] = newFlipsnap;
updateFlipsnap();
if(oldFlipsnap) {
oldFlipsnap.destroy();
}
}
function updateFlipsnap() {
if($parent[name]) {
$parent[name].canMoveNext = $parent[name].hasNext();
$parent[name].canMovePrev = $parent[name].hasPrev();
}
}
$flipsnap.on('fspointmove', function() {
if($parent.$$phase || $parent.$root.$$phase) {
updateFlipsnap();
} else {
$scope.$apply(updateFlipsnap);
}
});
$timeout(completeLayout);
}
};
}]);
})();