forked from metafizzy/outlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.js
520 lines (437 loc) · 13 KB
/
item.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**
* Outlayer Item
**/
( function( window ) {
'use strict';
// ----- get style ----- //
var defView = document.defaultView;
var getStyle = defView && defView.getComputedStyle ?
function( elem ) {
return defView.getComputedStyle( elem, null );
} :
function( elem ) {
return elem.currentStyle;
};
// extend objects
function extend( a, b ) {
for ( var prop in b ) {
a[ prop ] = b[ prop ];
}
return a;
}
function isEmptyObj( obj ) {
for ( var prop in obj ) {
return false;
}
prop = null;
return true;
}
// http://jamesroberts.name/blog/2010/02/22/string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore/
function toDash( str ) {
return str.replace( /([A-Z])/g, function( $1 ){
return '-' + $1.toLowerCase();
});
}
// -------------------------- Outlayer definition -------------------------- //
function outlayerItemDefinition( EventEmitter, getSize, getStyleProperty ) {
// -------------------------- CSS3 support -------------------------- //
var transitionProperty = getStyleProperty('transition');
var transformProperty = getStyleProperty('transform');
var supportsCSS3 = transitionProperty && transformProperty;
var is3d = !!getStyleProperty('perspective');
var transitionEndEvent = {
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
OTransition: 'otransitionend',
transition: 'transitionend'
}[ transitionProperty ];
// properties that could have vendor prefix
var prefixableProperties = [
'transform',
'transition',
'transitionDuration',
'transitionProperty'
];
// cache all vendor properties
var vendorProperties = ( function() {
var cache = {};
for ( var i=0, len = prefixableProperties.length; i < len; i++ ) {
var prop = prefixableProperties[i];
var supportedProp = getStyleProperty( prop );
if ( supportedProp && supportedProp !== prop ) {
cache[ prop ] = supportedProp;
}
}
return cache;
})();
// -------------------------- Item -------------------------- //
function Item( element, layout ) {
if ( !element ) {
return;
}
this.element = element;
// parent layout class, i.e. Masonry, Isotope, or Packery
this.layout = layout;
this.position = {
x: 0,
y: 0
};
this._create();
}
// inherit EventEmitter
extend( Item.prototype, EventEmitter.prototype );
Item.prototype._create = function() {
// transition objects
this._transition = {
ingProperties: {},
clean: {},
onEnd: {}
};
this.css({
position: 'absolute'
});
};
// trigger specified handler for event type
Item.prototype.handleEvent = function( event ) {
var method = 'on' + event.type;
if ( this[ method ] ) {
this[ method ]( event );
}
};
Item.prototype.getSize = function() {
this.size = getSize( this.element );
};
/**
* apply CSS styles to element
* @param {Object} style
*/
Item.prototype.css = function( style ) {
var elemStyle = this.element.style;
for ( var prop in style ) {
// use vendor property if available
var supportedProp = vendorProperties[ prop ] || prop;
elemStyle[ supportedProp ] = style[ prop ];
}
};
// measure position, and sets it
Item.prototype.getPosition = function() {
var style = getStyle( this.element );
var layoutOptions = this.layout.options;
var isOriginLeft = layoutOptions.isOriginLeft;
var isOriginTop = layoutOptions.isOriginTop;
var x = parseInt( style[ isOriginLeft ? 'left' : 'right' ], 10 );
var y = parseInt( style[ isOriginTop ? 'top' : 'bottom' ], 10 );
// clean up 'auto' or other non-integer values
x = isNaN( x ) ? 0 : x;
y = isNaN( y ) ? 0 : y;
// remove padding from measurement
var layoutSize = this.layout.size;
x -= isOriginLeft ? layoutSize.paddingLeft : layoutSize.paddingRight;
y -= isOriginTop ? layoutSize.paddingTop : layoutSize.paddingBottom;
this.position.x = x;
this.position.y = y;
};
// set settled position, apply padding
Item.prototype.layoutPosition = function() {
var layoutSize = this.layout.size;
var layoutOptions = this.layout.options;
var style = {};
if ( layoutOptions.isOriginLeft ) {
style.left = ( this.position.x + layoutSize.paddingLeft ) + 'px';
// reset other property
style.right = '';
} else {
style.right = ( this.position.x + layoutSize.paddingRight ) + 'px';
style.left = '';
}
if ( layoutOptions.isOriginTop ) {
style.top = ( this.position.y + layoutSize.paddingTop ) + 'px';
style.bottom = '';
} else {
style.bottom = ( this.position.y + layoutSize.paddingBottom ) + 'px';
style.top = '';
}
this.css( style );
this.emitEvent( 'layout', [ this ] );
};
// transform translate function
var translate = is3d ?
function( x, y ) {
return 'translate3d(' + x + 'px, ' + y + 'px, 0)';
} :
function( x, y ) {
return 'translate(' + x + 'px, ' + y + 'px)';
};
Item.prototype._transitionTo = function( x, y ) {
this.getPosition();
// get current x & y from top/left
var curX = this.position.x;
var curY = this.position.y;
var compareX = parseInt( x, 10 );
var compareY = parseInt( y, 10 );
var didNotMove = compareX === this.position.x && compareY === this.position.y;
// save end position
this.setPosition( x, y );
// if did not move and not transitioning, just go to layout
if ( didNotMove && !this.isTransitioning ) {
this.layoutPosition();
return;
}
var transX = x - curX;
var transY = y - curY;
var transitionStyle = {};
// flip cooridinates if origin on right or bottom
var layoutOptions = this.layout.options;
transX = layoutOptions.isOriginLeft ? transX : -transX;
transY = layoutOptions.isOriginTop ? transY : -transY;
transitionStyle.transform = translate( transX, transY );
this.transition({
to: transitionStyle,
onTransitionEnd: {
transform: this.layoutPosition
},
isCleaning: true
});
};
// non transition + transform support
Item.prototype.goTo = function( x, y ) {
this.setPosition( x, y );
this.layoutPosition();
};
// use transition and transforms if supported
Item.prototype.moveTo = supportsCSS3 ?
Item.prototype._transitionTo : Item.prototype.goTo;
Item.prototype.setPosition = function( x, y ) {
this.position.x = parseInt( x, 10 );
this.position.y = parseInt( y, 10 );
};
// ----- transition ----- //
/**
* @param {Object} style - CSS
* @param {Function} onTransitionEnd
*/
// non transition, just trigger callback
Item.prototype._nonTransition = function( args ) {
this.css( args.to );
if ( args.isCleaning ) {
this._removeStyles( args.to );
}
for ( var prop in args.onTransitionEnd ) {
args.onTransitionEnd[ prop ].call( this );
}
};
/**
* proper transition
* @param {Object} args - arguments
* @param {Object} to - style to transition to
* @param {Object} from - style to start transition from
* @param {Boolean} isCleaning - removes transition styles after transition
* @param {Function} onTransitionEnd - callback
*/
Item.prototype._transition = function( args ) {
// redirect to nonTransition if no transition duration
if ( !parseFloat( this.layout.options.transitionDuration ) ) {
this._nonTransition( args );
return;
}
var _transition = this._transition;
// keep track of onTransitionEnd callback by css property
for ( var prop in args.onTransitionEnd ) {
_transition.onEnd[ prop ] = args.onTransitionEnd[ prop ];
}
// keep track of properties that are transitioning
for ( prop in args.to ) {
_transition.ingProperties[ prop ] = true;
// keep track of properties to clean up when transition is done
if ( args.isCleaning ) {
_transition.clean[ prop ] = true;
}
}
// set from styles
if ( args.from ) {
this.css( args.from );
// force redraw. http://blog.alexmaccaw.com/css-transitions
var h = this.element.offsetHeight;
// hack for JSHint to hush about unused var
h = null;
}
// enable transition
this.enableTransition( args.to );
// set styles that are transitioning
this.css( args.to );
this.isTransitioning = true;
};
var itemTransitionProperties = transformProperty && ( toDash( transformProperty ) +
',opacity' );
Item.prototype.enableTransition = function(/* style */) {
// only enable if not already transitioning
// bug in IE10 were re-setting transition style will prevent
// transitionend event from triggering
if ( this.isTransitioning ) {
return;
}
// make transition: foo, bar, baz from style object
// TODO uncomment this bit when IE10 bug is resolved
// var transitionValue = [];
// for ( var prop in style ) {
// // dash-ify camelCased properties like WebkitTransition
// transitionValue.push( toDash( prop ) );
// }
// enable transition styles
// HACK always enable transform,opacity for IE10
this.css({
transitionProperty: itemTransitionProperties,
transitionDuration: this.layout.options.transitionDuration
});
// listen for transition end event
this.element.addEventListener( transitionEndEvent, this, false );
};
Item.prototype.transition = Item.prototype[ transitionProperty ? '_transition' : '_nonTransition' ];
// ----- events ----- //
Item.prototype.onwebkitTransitionEnd = function( event ) {
this.ontransitionend( event );
};
Item.prototype.onotransitionend = function( event ) {
this.ontransitionend( event );
};
// properties that I munge to make my life easier
var dashedVendorProperties = {
'-webkit-transform': 'transform',
'-moz-transform': 'transform',
'-o-transform': 'transform'
};
Item.prototype.ontransitionend = function( event ) {
// disregard bubbled events from children
if ( event.target !== this.element ) {
return;
}
var _transition = this._transition;
// get property name of transitioned property, convert to prefix-free
var propertyName = dashedVendorProperties[ event.propertyName ] || event.propertyName;
// remove property that has completed transitioning
delete _transition.ingProperties[ propertyName ];
// check if any properties are still transitioning
if ( isEmptyObj( _transition.ingProperties ) ) {
// all properties have completed transitioning
this.disableTransition();
}
// clean style
if ( propertyName in _transition.clean ) {
// clean up style
this.element.style[ event.propertyName ] = '';
delete _transition.clean[ propertyName ];
}
// trigger onTransitionEnd callback
if ( propertyName in _transition.onEnd ) {
var onTransitionEnd = _transition.onEnd[ propertyName ];
onTransitionEnd.call( this );
delete _transition.onEnd[ propertyName ];
}
this.emitEvent( 'transitionEnd', [ this ] );
};
Item.prototype.disableTransition = function() {
this.removeTransitionStyles();
this.element.removeEventListener( transitionEndEvent, this, false );
this.isTransitioning = false;
};
/**
* removes style property from element
* @param {Object} style
**/
Item.prototype._removeStyles = function( style ) {
// clean up transition styles
var cleanStyle = {};
for ( var prop in style ) {
cleanStyle[ prop ] = '';
}
this.css( cleanStyle );
};
var cleanTransitionStyle = {
transitionProperty: '',
transitionDuration: ''
};
Item.prototype.removeTransitionStyles = function() {
// remove transition
this.css( cleanTransitionStyle );
};
// ----- show/hide/remove ----- //
// remove element from DOM
Item.prototype.removeElem = function() {
this.element.parentNode.removeChild( this.element );
this.emitEvent( 'remove', [ this ] );
};
Item.prototype.remove = function() {
// just remove element if no transition support or no transition
if ( !transitionProperty || !parseFloat( this.layout.options.transitionDuration ) ) {
this.removeElem();
return;
}
// start transition
var _this = this;
this.on( 'transitionEnd', function() {
_this.removeElem();
return true; // bind once
});
this.hide();
};
Item.prototype.reveal = function() {
delete this.isHidden;
// remove display: none
this.css({ display: '' });
var options = this.layout.options;
this.transition({
from: options.hiddenStyle,
to: options.visibleStyle,
isCleaning: true
});
};
Item.prototype.hide = function() {
// set flag
this.isHidden = true;
// remove display: none
this.css({ display: '' });
var options = this.layout.options;
this.transition({
from: options.visibleStyle,
to: options.hiddenStyle,
// keep hidden stuff hidden
isCleaning: true,
onTransitionEnd: {
opacity: function() {
this.css({ display: 'none' });
}
}
});
};
Item.prototype.destroy = function() {
this.css({
position: '',
left: '',
right: '',
top: '',
bottom: '',
transition: '',
transform: ''
});
};
return Item;
}
// -------------------------- transport -------------------------- //
if ( typeof define === 'function' && define.amd ) {
// AMD
define( [
'eventEmitter/EventEmitter',
'get-size/get-size',
'get-style-property/get-style-property'
],
outlayerItemDefinition );
} else {
// browser global
window.Outlayer = {};
window.Outlayer.Item = outlayerItemDefinition(
window.EventEmitter,
window.getSize,
window.getStyleProperty
);
}
})( window );