-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfmpCardDirective.js
More file actions
285 lines (247 loc) · 14 KB
/
fmpCardDirective.js
File metadata and controls
285 lines (247 loc) · 14 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
angular.module('fmp-card', [])
.directive("fmpCard", function($timeout, $window) {
var getCssVendorPrefix = function (operation){
var retval = "";
var userAgent = $window.navigator.userAgent.toLowerCase();
if (operation == "transform"){
if (userAgent.indexOf('chrome') > -1){
retval = "";
} else if (userAgent.indexOf('safari') > -1){
retval = "-webkit-";
} else if (userAgent.indexOf('msie') > -1){
retval = "";
} else if (userAgent.indexOf('opera') > -1){
retval = "";
} else if (userAgent.indexOf('firefox') > -1){
retval = "";
} else{
retval = "-webkit-";
}
} else if (operation == "transition"){
if (userAgent.indexOf('chrome') > -1){
retval = "";
} else if (userAgent.indexOf('safari') > -1){
retval = "-webkit-";
} else if (userAgent.indexOf('msie') > -1){
retval = "";
} else if (userAgent.indexOf('opera') > -1){
retval = "";
} else if (userAgent.indexOf('firefox') > -1){
retval = "";
} else{
retval = "-webkit-";
}
}
return retval;
};
var getFlipCardTransitionStartPointStyle = function (smallCard, largeCard){
//transition flip from small card to large card
var flipCardLeftInitialTransition = smallCard.offsetLeft - (largeCard.largeCardLeft + (largeCard.largeCardWidthSize - (largeCard.largeCardWidthSize / (largeCard.largeCardWidthSize/smallCard.clientWidth))) / 2);
var flipCardTopInitialTransition = smallCard.offsetTop - (largeCard.largeCardTop + (largeCard.largeCardHeightSize - (largeCard.largeCardHeightSize / (largeCard.largeCardHeightSize/smallCard.clientHeight))) / 2) + 2;
var flipCardInitialWidthScale = smallCard.clientWidth / largeCard.largeCardWidthSize;
var flipCardInitialHeightScale = smallCard.clientHeight / largeCard.largeCardHeightSize;
return "-webkit-transform: translate(" + flipCardLeftInitialTransition + "px, " +
flipCardTopInitialTransition + "px) " +
"scale(" + flipCardInitialWidthScale + "," + flipCardInitialHeightScale + ");";
};
var animateCardMovingIn = function(smallCard, largeCard, flipCard, transitionSpeed) {
//Set the large card in center of view-port
largeCard.largeCardWidthSize = smallCard.clientWidth * 2;
largeCard.largeCardHeightSize = smallCard.clientHeight * 2;
largeCard.largeCardLeft = $window.innerWidth/2 - largeCard.largeCardWidthSize/2;
largeCard.largeCardTop = $window.innerHeight/2 - largeCard.largeCardHeightSize/2;
var startFlipCardStyle = getFlipCardTransitionStartPointStyle(smallCard, largeCard);
var cssVendorPrefix = getCssVendorPrefix("transform");
var endFlipCardStyle = cssVendorPrefix + "transform: translate(0px, 0px) scale(1) rotateY(180deg);";
angular.element(flipCard).attr("style", startFlipCardStyle);
$timeout(function() {
angular.element(flipCard).attr("style", startFlipCardStyle + 'z-index:700;');
//Place large card in display
var newLargeCardStyle =
"left:" + largeCard.largeCardLeft + "px;" +
"top:" + largeCard.largeCardTop + "px;" +
"width:" + largeCard.largeCardWidthSize + "px;" +
"height:" + largeCard.largeCardHeightSize + "px;" +
"display:block;";
angular.element(largeCard).attr('style', newLargeCardStyle);
//After first preparation for animate do animation changes
$timeout(function () {
//Hide small card
var oldSmallCardStyle = angular.element(smallCard).attr("style");
angular.element(smallCard).attr("style",oldSmallCardStyle + "visibility: hidden;");
angular.element(largeCard).addClass('unflip');
var transitionStyleAddition = '';
if (transitionSpeed !== null && transitionSpeed !== undefined && transitionSpeed !== ""){
transitionStyleAddition = getCssVendorPrefix('transition') + 'transition:' + transitionSpeed + "s;";
}
angular.element(flipCard).attr("style", endFlipCardStyle + 'z-index:700;' + transitionStyleAddition);
}, 100);//Not 0 since sometimes animation not happening in browser
},100);
};
var animateCardMovingOutStrategy = function(smallCard, largeCard){
//Make small Card reappear
var oldSmallCardStyle = angular.element(smallCard).attr("style");
if (oldSmallCardStyle) {
var newSmallCardStyle = oldSmallCardStyle.replace("visibility: hidden;", "");
angular.element(smallCard).attr('style', newSmallCardStyle);
}
//Hide Large card after it shrank
var largeCardOldStyle = angular.element(largeCard).attr('style');
if (largeCardOldStyle) {
var newLargeCardStyle = largeCardOldStyle.replace("display:block", "display:none");
angular.element(largeCard).attr('style', newLargeCardStyle);
}
};
var animateCardMovingOut = function(smallCard, largeCard, flipCard, transitionSpeed) {
var transitions = "transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd";
angular.element(flipCard).one(transitions, function(){
angular.element(largeCard).removeClass('unflip');
angular.element(flipCard).unbind(transitions);
animateCardMovingOutStrategy(smallCard, largeCard);
});
var endFlipCardStyle = getFlipCardTransitionStartPointStyle(smallCard, largeCard);
if (transitionSpeed !== null && transitionSpeed !== undefined && transitions !== "") { //Change animation speed
endFlipCardStyle = endFlipCardStyle + getCssVendorPrefix('transition') + 'transition:' + transitionSpeed + "s;";
//angular.element(largeCard).removeClass('unflip');
if (transitionSpeed === 0) { //Immediate flip
angular.element(largeCard).removeClass('unflip');
angular.element(flipCard).unbind(transitions);
animateCardMovingOutStrategy(smallCard, largeCard);
}
}
angular.element(flipCard).attr("style", endFlipCardStyle);
};
return {
restrict: 'E',
transclude: true,
scope: {
smallCardWidth: "@",
smallCardHeight: "@",
image: "@",
frontCaption: "@",
suffix: "@",
onCardOpened: "&",
onCardClosed: "&",
cardControl: '='
},
link: function (scope, element) {
//Get all existing small cards to check if clicking outside of this card not clicking
//on another card. is so don't open it
scope.allSmallCardDOMElements = null;
//Small card representation
scope.smallCardDOMElement = element[0].querySelector('.fmp-card-small');
//Large card final point representation
scope.largeCardDOMElement = element[0].querySelector('.fmp-card-large');
//flipping card representation
scope.flipperCardDOMElement = element[0].querySelector('.fmp-flipper');
//Initialize card states to animate card moving back out final state
animateCardMovingOutStrategy(scope.smallCardDOMElement, scope.largeCardDOMElement);
////Set initial large card position and hide it
angular.element(scope.largeCardDOMElement).attr('style',"display:none;");
//Event when clicking outside current card which is opened
var onLargeCardSelected = function(e, transitionSpeed) {
var isClosingCard = false;
if (scope.onCardClosed){
isClosingCard = scope.onCardClosed();
}
if (isClosingCard !== false) { //During tests this received 'undefined'
animateCardMovingOut(scope.smallCardDOMElement, scope.largeCardDOMElement, scope.flipperCardDOMElement, transitionSpeed);
//scope.$digest();
}
};
//Event when clicking on a closed small card
var onSmallCardSelected = function(e, transitionSpeed){
var isCardAlreadyOpen = false;// if we have a different card already open then don't open another
if (!scope.allSmallCardDOMElements){
scope.allSmallCardDOMElements = angular.element(document.getElementsByClassName('fmp-card-small'));
}
//Check if we got another card already open
for (var i = 0; i < scope.allSmallCardDOMElements.length; i++) {
isCardAlreadyOpen = (scope.allSmallCardDOMElements[i].style.visibility == "hidden");
if (isCardAlreadyOpen) {
break;
}
}
if (!isCardAlreadyOpen) { //We don't have a different card already open
animateCardMovingIn(scope.smallCardDOMElement, scope.largeCardDOMElement, scope.flipperCardDOMElement, transitionSpeed);
if (scope.onCardOpened){
scope.onCardOpened();
}
if (e) {
e.stopPropagation();
}
}
};
scope.onSmallCardClicked = function(e){
//if Angular only
if (scope.clickEvent == 'click'){
onSmallCardSelected(e);
}
};
scope.onSmallCardTouched = function(e){
//if Ionic
if (scope.clickEvent == 'touch'){
onSmallCardSelected(e);
}
};
scope.onLargeCardClicked = function(e){
//if Angular only
if (scope.clickEvent == 'click'){
onLargeCardSelected(e);
}
};
scope.onLargeCardTouched = function(e){
//if Ionic
if (scope.clickEvent == 'touch'){
onLargeCardSelected(e);
}
};
var getRandomNumberForId = function(){
return parseInt((Math.random() * (1000000 - 1 + 1)), 10) + 1;
};
//If suffix input not given use a random number
scope.directiveSuffix = (scope.suffix)? scope.suffix : getRandomNumberForId();
var newSmallCardStyle = angular.element(scope.smallCardDOMElement).attr("style");
if (!newSmallCardStyle){
newSmallCardStyle = "";
}
//Set small card width if have input
if (scope.smallCardWidth){
newSmallCardStyle = newSmallCardStyle + "width: " + scope.smallCardWidth + ";";
}
//Set small card height if have input
if (scope.smallCardHeight){
newSmallCardStyle = newSmallCardStyle + "height: " + scope.smallCardHeight + ";";
}
angular.element(scope.smallCardDOMElement).attr("style", newSmallCardStyle);
//Check if ionic is installed and if so modify events to use on-touch instead of ng-click. faster
scope.clickEvent = 'click';
//noinspection JSUnresolvedVariable
if (typeof ionic !== 'undefined' && !isTesting) { //Might need to comment this out if fails build on angular only machine
scope.clickEvent = 'touch';
}
if (scope.cardControl) {
scope.cardControl.flipToLarge = function (transitionSpeed) {
onSmallCardSelected(null, transitionSpeed);
};
scope.cardControl.flipToSmall = function (transitionSpeed) {
onLargeCardSelected(null, transitionSpeed);
};
}
},
template:
'<!--suppress ALL --><div class="fmp-card-small" id="fmp-card-small-{{directiveSuffix}}" ng-click="onSmallCardClicked($event)" on-touch="onSmallCardTouched($event)">' +
'<div class="fmp-card fmp-card-small-image" ng-style="{\'background-image\':\'url(\'+ image +\')\'}">' +
'<div class="card-caption"><div ng-bind="frontCaption"></div></div>' +
'</div>' +
'</div>' +
'<!--suppress ALL --><div class="fmp-card-large" id="fmp-card-large-{{directiveSuffix}}" ng-click="onLargeCardClicked($event)" on-touch="onLargeCardTouched($event)">' +
'<div class="fmp-card fmp-flipper">' +
'<div class="fmp-card-front-large fmp-paper" ng-style="{\'background-image\':\'url(\'+ image +\')\'}">' +
//'<div ng-bind="frontCaption"></div>' +
'</div>' +
'<div class="fmp-card-back" ng-transclude></div>' +
'</div>' +
'</div>'
};
});