forked from NewSignature/jcoverflip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.jcoverflip.js
1464 lines (1173 loc) · 38 KB
/
jquery.jcoverflip.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* jCoverflip - Present your featured content elegantly.
* Version: 1.1.0
* Copyright 2010 New Signature
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*
* You can contact New Signature by electronic mail at [email protected]
* or- by U.S. Postal Service at 1100 H St. NW, Suite 940, Washington, DC 20005.
*/
( function( $ ){
//
// Helpers
//
var undefined; // safeguards against anyone who wants to change the value of undefined in the global space
var nofn = function(){};
var proxy = function( context, fn ){
return function( ){
if( $.isFunction( fn ) ){
return fn.apply( context, arguments );
} else {
return context[ fn ].apply( context, arguments );
}
};
};
/**
* Get an array of keys for an object
*/
var objectKeys = function( obj ){
var r = [];
for( var k in obj ){
r.push(k);
}
return r;
};
/**
* jQuery Plugin Factory
*
* A simple framework for creating jQuery plugins that have several 'methods'.
* The result will be a plugin that you can initialize like this:
* $('#foo').myplugin(param1, param2).show();
*
* Then you can call methods 'toggle' and 'bar' with you plugin like this:
* $('#foo').myplugin('toggle', param1).myplugin('bar', param1, parma2).fadeIn();
*
* The methods can alos act as getters and return values:
* var v = $('#foo').myplugin('current');
*
* 1) Most of the work is in creating the methods which is just an object of functions.
* 2) Any function name that starts with an underscore will not be exposed to the public.
* 3) When the plugin initializes, it will call the method named '_init'.
* 4) The 'this' object is for the each instance of the plugin.
* 5) To get the element that the plugin is for, use this.element to get it.
*
* @param $ - the jQuery instance to add the plugin to
* @param name - the name to call the plugin with
* @param methods - an object of functions to add as methods
* Any method name that starts with an underscore will be private, it will not be exposed.
* The method named '_init' will be called when initializing.
* @param getters - an array of methods that return values
*/
function jQueryPluginFactory( $, name, methods, getters ){
getters = getters instanceof Array ? getters : [];
var getters_obj = {};
for(var i=0; i<getters.length; i++){
getters_obj[getters[i]] = true;
}
// Create the object
var Plugin = function(element){
this.element = element;
};
Plugin.prototype = methods;
// Assign the plugin
$.fn[name] = function(){
var args = arguments;
var returnValue = this;
this.each(function() {
var $this = $(this);
var plugin = $this.data('plugin-'+name);
// Init the plugin if first time
if( !plugin ){
plugin = new Plugin($this);
$this.data('plugin-'+name, plugin);
if(plugin._init){
plugin._init.apply(plugin, args);
}
// call a method
} else if(typeof args[0] == 'string' && args[0].charAt(0) != '_' && typeof plugin[args[0]] == 'function'){
var methodArgs = Array.prototype.slice.call(args, 1);
var r = plugin[args[0]].apply(plugin, methodArgs);
// set the return value if method is a getter
if( args[0] in getters_obj ){
returnValue = r;
}
}
});
return returnValue; // returning the jQuery object
};
};
//
// Animation queue
//
animationqueue = { };
/**
* Animation Queue
*
* The AnimationQueue holds list of AnimationSets that each perform a set of animations. The sets
* run in ordering when the AnimationQueue is animating. As well, the running time is distributed
* across all the sets when the AnimationQueue is animating.
*
* As well, the AnimationQueue can be stopped and started again at any time. AnimationSets can be
* removed when stopped when they are not needed anymore.
*
*/
animationqueue.AnimationQueue = function(){
this.sets = [];
this.isRunning = false;
this.current = 0;
this.totalTime = 0;
this.elapsedTime = 0;
this.startTime = 1;
this.poll = null;
};
animationqueue.AnimationQueue.prototype = {
/**
* Add an AnimationSet to the end of the queue.
*
* @param animationSet
* The AnimationSet object to add to the end of the queue.
*/
queue: function( animationSet ){
this.sets.push( animationSet );
},
/**
* Start the animation.
*
* @param time
* The total running time of the animation in milliseconds.
*/
start: function( time, callback ){
this.totalTime = time;
this.elapsedTime = 0;
this.isRunning = true;
callback = $.isFunction( callback )? callback : nofn;
// calculate the how to divide the time between the sets
// Each set gets 1 part of the time except for the first which can get less if "paused."
var timeShare = 1; // total number of parts to divide the time by
var firstTimeShare = 1; // the part for the first item
if( this.sets.length > 0 ){
timeShare = this.sets.length;
firstTimeShare = this.sets[ 0 ].totalTime > 0? Math.max( 0, Math.min( 1, 1-(this.sets[ 0 ].elapsedTime / this.sets[ 0 ].totalTime) ) ): 1;
//timeShare += firstTimeShare;
}
var startTime = (new Date( )).getTime( );
if( this.sets.length > 0 ){
this.sets[ 0 ].start( firstTimeShare/timeShare*this.totalTime );
}
(function( self, totalTime, timeShare ){
function poll( ){
self.elapsedTime = (new Date( )).getTime( ) - startTime;
if( self.sets.length && !self.sets[ 0 ].isRunning ){
self.sets.shift( );
if( self.sets.length ){
self.sets[ 0 ].start( totalTime/timeShare );
}
}
if( self.elapsedTime >= self.totalTime && self.sets.length == 0 ){
callback( self.elapsedTime );
self.stop( );
}
}
self.poll = setInterval( poll, 16 );
} )( this, this.totalTime, timeShare );
},
/**
* Stop the animation.
*/
stop: function( ){
if( this.isRunning ){
this.isRunning = false;
var i = this.sets.length;
while( i-- ){
this.sets[ i ].stop( );
}
clearInterval( this.poll );
}
},
/**
* Remove an AnimationSet from the queue.
*
* @param animationSet
* The AnimationSet to remove from the queue.
*/
remove: function( animationSet ){
var i = this.sets.length;
while( i-- ){
if( this.sets[ i ] == animationSet ){
var newSets = this.sets.slice( 0, i );
this.animationSet = newSets.push( this.sets.slice( i + 1 ) );
}
}
},
/**
* Get an array of all the AnimationSets in order. The array is not live, but the AnimationSets are.
*
* @return array of AnimationSets
*/
get: function( ){
return this.sets.slice( 0 ); // .slice is to clone the array but not the objects
}
};
/**
*
*
*
*/
animationqueue.AnimationSet = function(){
this.steps = [ ];
this.isStepsSorted = true;
this.currentStep = -1;
this.animations = [ ];
this.isRunning = false;
this.totalTime = 0;
this.elapsedTime = 0;
this.poll = null;
this.data = { };
};
animationqueue.AnimationSet.prototype = {
/**
* Add an AnimationStep or Animation for this set.
*
* @param anim
* The AnimationStep or Animation to add.
*/
add: function( anim ){
if( anim instanceof animationqueue.AnimationStep ){
this.steps.push( anim );
this.isStepsSorted = false;
++this.currentStep;
} else if( anim instanceof animationqueue.Animation ){
this.animations.push( anim );
}
},
/**
* Start the set's animation.
*
* @param time
* The total running time of the animation.
*/
start: function( time ){
// Scale the previous elapsedTime to the new time
this.elapsedTime = this.totalTime == 0? 0: this.elapsedTime/this.totalTime*time;
this.totalTime = time;
// prepare this to run
if( !this.isStepsSorted ){
// Reverse sort
this.steps.sort( function( a, b ){
return b.moment - a.moment;
} );
this.isStepsSorted = true;
}
this.isRunning = true;
// Start the time up here right before any of the animation starts
this.startTime = (new Date()).getTime() - this.elapsedTime;
// Start up the animations
var i = this.animations.length;
while( i-- ){
this.animations[ i ].start( this.totalTime );
}
// The polling function: this will run the steps at the right time and
// check for when the animations are finished.
var self = this;
var animationsIndex = this.animations.length-1;
function poll( timeSince ){
self.elapsedTime = (new Date()).getTime() - self.startTime;
// Run any steps that should be run
while( self.currentStep >= 0 && self.steps[ self.currentStep ].getTime( self.totalTime ) <= self.elapsedTime ){
self.steps[ self.currentStep ].doIt( );
--self.currentStep;
}
// Check if all the animations are finished
if( self.elapsedTime >= self.totalTime && self.currentStep < 0){
while( animationsIndex >= 0 && !self.animations[ animationsIndex ].isRunning ){
--animationsIndex;
}
if( animationsIndex < 0 ){
// finished
self.reset( self.elapsedTime );
}
}
}
this.poll = setInterval( poll, 16 );
},
/**
* Stop the animation.
*/
stop: function( ){
if( this.isRunning ){
this.isRunning = false;
if( this.poll ){
clearInterval( this.poll );
}
var i = this.animations.length;
while( i-- ){
this.animations[ i ].stop( );
}
}
},
/**
* Set meta data for the set.
*
* @param key
* The key for the meta data.
*
* @param data
* The data.
*/
setData: function( key, data ){
this.data[ key ] = data;
},
/**
* Get meta data for the set.
*
* @param key
* The key used to save the meta data.
*
* @return The value of the meta data
*/
getData: function( key ){
return this.data[ key ];
},
/**
* Resets the set to beginning.
*/
reset: function( ){
this.stop( );
this.elapsedTime = 0;
this.currentStep = this.steps.length-1;
}
};
/**
* A single step to occur during a set's animation.
*
* This is useful for handling one off things during a set's animation such
* as switch the z-index.
*
* @param $element
* The jQuery object for the element(s) to update their CSS
*
* @param cssParams
* A key/value object of style properties. @see http://docs.jquery.com/CSS/css#properties
*
* @param moment
* A number from 0 to 1 for when as a percentage of the set's running time the
* step should happen.
*/
animationqueue.AnimationStep = function( $element, cssParams, moment ){
this.$element = $element;
this.cssParams = cssParams;
this.moment = Math.min( 1, Math.max( 0, moment ) );
};
animationqueue.AnimationStep.prototype = {
/**
* Get the time of execution
*
* @param totalTime
* The total of time for the set this belongs to in milliseconds.
*
* @return The time in milliseconds this step needs to execute.
*/
getTime: function( totalTime ){
return this.moment * totalTime;
},
/**
* Does the step action.
*/
doIt: function( ){
this.$element.css( this.cssParams );
}
};
/**
* A single animation object for a jQuery object.
*
* @param $element
* The jQuery object for the element(s) to animate
*
* @param animateParams
* The object of CSS values to animate. @see http://docs.jquery.com/Effects/animate
*/
animationqueue.Animation = function( $element, animateParams ){
this.$element = $element;
this.animateParams = $.isArray(animateParams) ? animateParams : [ animateParams ];
this.isRunning = false;
};
animationqueue.Animation.prototype = {
/**
* Start the animation.
*
* @param time
* The running time of the animation (and the step) in milliseconds.
*/
start: function( time ){
this.$element.stop( );
if( time === 0 ){
for( var i=0; i<this.animateParams.length; ++i ){
this.$element.css( this.animateParams[i] );
}
self.isRunning = false;
} else {
// calculate the duration of each animation param
if( this.animateParams.length > 1 ){
var totalDur = 0; // total duration calculated
var withoutDur = 0; // the number without a duration set
var i=this.animateParams.length;
while( i-- != 0 ){
var dur = this.animateParams[i].animationDuration;
if( !isNaN( dur ) && dur >= 0 ){
totalDur += dur;
} else {
++withoutDur;
delete this.animateParams[i].animationDuration;
}
}
var emptyDurVal = 0;
// case for when the total duration is less than 1, and there are animations that can fill in the gap
if( withoutDur && totalDur < 1 ){
emptyDurVal = (1-totalDur) / withoutDur; // the duration for those without value set
totalDur = 1;
}
i=this.animateParams.length;
// set the duration for each
while( i-- != 0 ){
if( !isNaN(this.animateParams[i].animationDuration) ){
this.animateParams[i].animationDuration = this.animateParams[i].animationDuration / totalDur;
} else {
this.animateParams[i].animationDuration = emptyDurVal;
}
}
// This is for the likely case that there is only one, to save time
} else {
this.animateParams[0].animationDuration = 1;
}
this.isRunning = true;
for( var i=0; i<this.animateParams.length-1; ++i ){
this.$element.animate( this.animateParams[i], time*this.animateParams[i].animationDuration );
}
this.$element.animate( this.animateParams[i], time*this.animateParams[i].animationDuration, proxy( this, function( ){
this.isRunning = false;
} ) );
}
},
/**
* Stop the animation.
*/
stop: function( ){
this.$element.stop( );
this.isRunning = false;
}
};
//
// The widget
//
//
// Static methods
$.jcoverflip = {
/**
* Used for wrapping the animation for an element for returned by beforeCss, afterCss and
* currentCss options.
*
* @param element
* The jQuery element to run the animation on.
*
* @param animate
* An object with CSS keys and values to animate to.
*
* @param steps
* An object with keys from 0 to 1 (0 to 100%) for how far along in the animation (0: start,
* 0.5: half way through, 1: end) with the value being an object of CSS keys and values to change.
* This is for discrete values that need to change such as z-index.
*
*/
animationElement: function( element, animate, steps ){
return { element: element, animate: animate, steps: steps };
},
/**
* Find the item element and index number that the element is associated.
*
* @param element
* The element that either is the item element or descendant element of the item element.
*
* @return
* null - if no item element is found
* { element: <the item element>, index: <the item index> }
*
*/
getItemFromElement: function( element ){
element = $( element );
var item = element.hasClass( 'ui-jcoverflip--item' )? element : element.parents( '.ui-jcoverflip--item' );
if( item.size( ) == 0 ){
return null;
} else {
return { element: item, index: item.data( 'jcoverflip__index' ) };
}
}
};
// The widget
var methods = {
_init: function( args ){
this.options = $.extend( defaults, args || {} );
// init some internal values
this.animationQueue = new animationqueue.AnimationQueue( );
this.isInit = false; // used for setting up the CSS
// Used to queue up overlapping goTo() calls since they come in async
this.goToPoll = { id: null };
this.goToQueue = [ ];
// Setup the elements
var items = this.items( );
// add classes
this.element.addClass( 'ui-jcoverflip' );
items.addClass( 'ui-jcoverflip--item' );
// Get the title for each item
var i = items.size( );
while( i-- ){
var el = items.eq( i );
// Tell the item what its index is
el.data( 'jcoverflip__index', i );
// Create the titles for the coverflow items
var title = this.options.titles.create( el );
title.css( { display: 'none' } ).addClass( 'ui-jcoverflip--title' ).appendTo( this.element );
el.data( 'jcoverflip__titleElement', title );
}
// Bind the click action for when the user clicks on the item to change the current
this.element.click( proxy( this, this._clickItem ) );
// Setup wrap around
if( this.options.wrapItemsAround ){
this.options.wrapCenter = this.options.wrapCenter === undefined? this.options.current : this.options.wrapCenter;
}
// setup the positioning of the elements, pass 0 for time, pass true to flag to init
this._goTo( this.options.current, 0, true );
// Add any addition controls (such as a scroll bar)
this.options.controls.create( this.element, this.length() );
},
/**
* The click event for an item. If the item is not current,
* then it calls the current() and stops the event.
*/
_clickItem: function( event ){
if( this.options.disabled == true ){
return;
}
var item = $.jcoverflip.getItemFromElement( event.target );
if( item !== null && item.index != this.current( ) ){
this.current( item.index, event );
event.preventDefault();
return false;
}
return true;
},
/**
* Parses the parameters for next and previous methods. Any of the parameters are optional.
*/
_nextAndPrevParameters: function( by, wrapAround, callback, originalEvent ){
// originalEvent is an object
if( typeof by == 'object' ){
originalEvent = by;
} else if( typeof wrapAround == 'object' ){
originalEvent = wrapAround;
} else if( typeof callback == 'object' ){
originalEvent = callback;
} else if( typeof originalEvent == 'object' ){
originalEvent = originalEvent;
} else {
originalEvent = { };
}
// callback is a function
if( $.isFunction( by ) ){
callback = by;
} else if( $.isFunction( wrapAround ) ){
callback = wrapAround;
} else if( $.isFunction( callback ) ){
callback = callback;
} else {
callback = nofn;
}
// wrapAround is boolean
if( typeof( by ) == 'boolean' ) {
wrapAround = by;
} else if( typeof( wrapAround ) == 'boolean' ){
wrapAround = wrapAround;
} else {
wrapAround = true;
}
// by is a number
by = isNaN( parseInt( by ) )? 1 : parseInt( by );
return { by: by, wrapAround: wrapAround, callback: callback, originalEvent: originalEvent };
},
/**
* Step to the right from the current.
*
* @param by
* (optional) An integer to step to the right by. Defaults to 1.
*
* @param wrapAround
* (optional) A boolean flag to wrap around if moving past the end. Defaults to true.
*
* @return
* New current number
*/
next: function( by, wrapAround, callback, originalEvent ){
if( this.options.disabled == true ){
return;
}
var params = this._nextAndPrevParameters( by, wrapAround, callback, originalEvent );
return this._nextAux( params.by, params.wrapAround, params.callback, params.originalEvent, 'next' );
},
_nextAux: function( by, wrapAround, callback, originalEvent, eventType ){
by = by === undefined && isNaN( by ) ? 1 : parseInt( by );
wrapAround = wrapAround !== false;
var current = this.current( );
var oldCurrent = current;
var length = this.length( );
if( wrapAround ){
current = (current + by) % length;
// If "current + by" is negative, then the result of "%" is between -(length-1) and -1.
// Add the length, if negative, to bring the index back to a valid number
current = current < 0 ? current + length : current;
} else {
current = Math.min( length-1, Math.max( 0, current + by ) );
}
if( current != this.current( ) ){
this.current( current, originalEvent );
}
if( eventType && oldCurrent != current ){
var event = $.Event( originalEvent );
event.type = this.widgetEventPrefix + eventType;
callback.call( this.element, event, { from: oldCurrent, to: current } );
this._trigger( eventType, originalEvent, { from: oldCurrent, to: current } );
}
return current;
},
/**
* Step to the left from the current.
*
* @param by
* (optional) An integer to step to the left by. Defaults to 1.
*
* @param wrapAround
* (optional) A boolean flag to wrap around if moving past the end. Defaults to true.
*
* @return
* New current number
*
*/
previous: function( by, wrapAround, callback, originalEvent ){
if( this.options.disabled == true ){
return;
}
var params = this._nextAndPrevParameters( by, wrapAround, callback, originalEvent );
return this._nextAux( -1*params.by, params.wrapAround, params.callback, params.originalEvent, 'previous' );
},
/**
* Go all the way to the left.
*/
first: function( callback, originalEvent ){
if( this.options.disabled == true ){
return;
}
if( typeof callback == 'object' ){
originalEvent = callback
} else if( typeof originalEvent == 'object' ){
originalEvent = originalEvent;
} else {
originalEvent = { };
}
callback = $.isFunction( callback ) ? callback : nofn;
var from = this.current( );
var to = this.current( 0, originalEvent );
if( from != to ){
var event = $.Event( originalEvent );
event.type = this.widgetEventPrefix + 'first';
callback.call( this.element, event, { from: from, to: to } );
this._trigger( 'first', originalEvent, { from: from, to: to } );
}
},
/**
* Go all the way to the right.
*/
last: function( callback, originalEvent ){
if( this.options.disabled == true ){
return;
}
if( typeof callback == 'object' ){
originalEvent = callback
} else if( typeof originalEvent == 'object' ){
originalEvent = originalEvent;
} else {
originalEvent = { };
}
callback = $.isFunction( callback ) ? callback : nofn;
var from = this.current( );
var to = this.current( this.length( ) - 1, originalEvent );
if( from != to ){
var event = $.Event( originalEvent );
event.type = this.widgetEventPrefix + 'last';
callback.call( this.element, event, { from: from, to: to } );
this._trigger( 'last', originalEvent, { from: from, to: to } );
}
},
/**
* Gets or sets the current item.
*
* @param originalEvent (optional)
* Pass an event object along to be assigned to the originalEvent for the event object passed
* along with the triggered events of start, stop and change.
*/
current: function( newCurrent, originalEvent ){
if( newCurrent !== undefined && !isNaN( newCurrent ) && !this.options.disabled && newCurrent != this.options.current ){
this._goTo( newCurrent, undefined, false, originalEvent );
}
return this.options.current;
},
destroy: function( ){
if( this.options.disabled == true ){
return;
}
// let others clean up first
this._trigger( 'destroy', {} );
// container element
this.element.removeClass( 'ui-jcoverflip' );
// titles
var items = this.items( );
var titleEl;
var i = items.length;
while( i-- ){
titleEl = items.eq( i ).data( 'jcoverflip__titleElement' );
this.options.titles.destroy( titleEl );
}
// items
// aggressively remove all inline styles
items
.removeClass( 'ui-jcoverflip--item' )
.find( '*' ).add( items.get( ) )
.each( function( ){
this.removeAttribute( 'style' );
} );
// controls
this.options.controls.destroy( this.element );
// default action
$.widget.prototype.destroy.apply( this, arguments );
},
enable: function( ){
this.options.disabled = false;
this._trigger( 'enable', {} );
},
disable: function( ){
this.options.disabled = true;
this._trigger( 'disable', {} );
},
option: function( name, value ){
// getter
if( typeof value == 'undefined' ){
return $.widget.prototype.option.apply( this, arguments );
}
// setter
// current
if( name == 'current' ){
return this.current( value );
}
// TODO: dynamic changing of the options: items, titles, controls
// items, titles, controls
if( name in { 'items': '', 'titles': '', 'controls': '' } ){
return this.options.items;
}
// beforeCss, afterCss, currentCss
if( name in { 'beforeCss': '', 'afterCss': '', 'currentCss': '' } ){
this.options[ name ] = value;
// force update positioning
this._goTo( this.current( ), 0, true );
}