This repository has been archived by the owner on Nov 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
timelines.js
1143 lines (944 loc) · 39.1 KB
/
timelines.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
(function(window){
"use strict";
String.supplant = String["supplant"] || angular.noop;
/**
* AngularJS-GSAP Timeline module that supports a custom DSL for
* animation definitions using Greensock's TimelineMax API.
*
* NOTE: Currently this module has some dependencies upon jQuery() features
* in querySelector()...
*
* @usage
* <gs-timeline id="zoom" time-scale="1.4" resolve="preload(selectedTile)" >
* <gs-step target="#mask" style="zIndex:-10;className:''" duration="0.001" />
* <gs-step target="#details" style="zIndex:-11;className:''" duration="0.001" />
* <gs-step target="#green_status" style="zIndex:-13;className:''" duration="0.001" />
* <gs-step target="#mask" style="zIndex:90" duration="0.001" />
* <gs-step target="#details" style="zIndex:92; opacity:0.01; left:{{selectedTile.from.left}}; top:{{selectedTile.from.top}}; width:{{selectedTile.from.width}}; height:{{selectedTile.from.height}}" duration="0.01"/>
* <gs-step target="#details" style="opacity:1.0" duration="0.3" />
* <gs-step mark-position="fullThumb"/>
* <gs-step target="#details" style="delay:0.2; left:0; height:{{selectedTile.to.height}}; width:329" duration="0.5" />
* <gs-step mark-position="fullWidth"/>
* <gs-step target="#mask" style="opacity:0.80" duration="0.5" position="fullWidth-=0.3"/>
* <gs-step target="#details" style="opacity:1; top:18; height:512" duration="0.3" position="fullWidth+=0.1"/>
* <gs-step mark-position="slideIn"/>
* <gs-step target="#green_status" style="zIndex:91; opacity:1; top:21;" duration="0.001" position="slideIn"/>
* <gs-step target="#green_status" style="top:0" duration="0.2" position="slideIn"/>
* <gs-step target="#details > #title" style="height:131" duration="0.6" position="fullWidth" />
* <gs-step target="#details > #info" style="height:56" duration="0.5" position="fullWidth+=0.2" />
* <gs-step target="#details > #title > div.content" style="opacity:1.0" duration="0.8" position="fullWidth+=0.3" />
* <gs-step target="#details > #info > div.content" style="opacity:1" duration="0.4" position="fullWidth+=0.6" />
* <gs-step target="#details > #pause" style="opacity:1; scale:1.0" duration="0.4" position="fullWidth+=0.4" />
* </gs-timeline>
*
*/
angular.module('gsTimelines', [ 'ng' ])
.service( '$timeline', TimelineBuilder )
.service( '$$gsStates', TimelineStates )
.directive('gsTimeline', TimelineDirective )
.directive('gsStep', StepDirective )
.directive('gsPause', PauseDirective )
.directive('gsScale', ScaleDirective);
/**
* @ngdoc service
* @private
*
* Internal State management for Timelines
* Used to watch states and auto-trigger `timeline` animations (restart() or reverse())
*
* NOTE: currently this is a CRUDE architecture that does not account for hierarchical states
* and complex animation chains...
*/
function TimelineStates( $timeline, $log, $rootScope ) {
var self, registry = { };
return self = {
addTimeline : function addTimeline(data) {
var state = data.state;
if ( state && state.length ) {
registry[ state ] = data;
// Only watch the state in the scope that is a parent
// to root timeline.
if ( !data.parentController ) {
watchState( state );
}
}
}
};
// ******************************************
// Internal Accessor
// ******************************************
/**
* Get the scope for the specified `state`
*/
function scopeFor(state) {
var data = registry[state];
return data ? data.scope : undefined;
}
// ******************************************
// Internal Methods
// ******************************************
/**
* Watch the `state` variable and autostart the Timeline instance when state
* changes
*/
function watchState(state) {
$log.debug( "TimelineStates::watchState( state = '{0}' )".supplant([state]) );
// Ensure the key exists before $watch()
var parent = scopeFor(state).$parent;
parent.state = parent.state || undefined;
// Watch for the scope `state` change... to start or reverse the animations
// Watch for state changes and fire the associated timeline
var unwatch = parent.$watch('state', function(current, previous){
if ( state === "" ) return;
if ( current === previous ) return;
if ( current === undefined ) return;
$rootScope.$evalAsync(function(){
var shouldReverse = isStateReversal(current);
// Must use $timeline() to integrate the resolve processing...
$timeline(state).then( function(timeline){
// Pop special `-` reverse indicator (if present)
current = stripStateReversal(current);
$log.debug( ">> TimelineStates::triggerTimeline( state = '{0}' )".supplant([current]) );
if ( current === state ){
if ( shouldReverse ) timeline.reverse();
else timeline.restart();
}
});
});
});
// Auto unwatch when the scope is destroyed..
parent.$on( "$destroy", unwatch );
}
}
/**
* @ngdoc service
*
* Service to build a GSAP TimelineMax instance based on <gs-timeline> and nested <gs-step>
* directive settings...
*/
function TimelineBuilder( $log, $q ) {
var counter = 0,
targets = { },
cache = { },
self = {
state : findByState,
id : findById,
register : register,
makeTimeline : makeTimeline
},
// Add 1 or more event callbacks to the animation?
// events : ["onComplete", "onReverseComplete", "onUpdate"]
registerCallbacks = function(callbacks) {
return function(tl) {
if ( callbacks) {
var events = getKeys(callbacks);
events.forEach(function(key){
tl.eventCallback(key, callbacks[key] || angular.noop, ["{self}"] );
});
}
// publish/provide the TimelineMax instance
return tl;
}
},
// Chain step to resolve AFTER the timeline is ready
// but BEFORE the timeline is delivered externally
resolveBeforeUse= function(tl) {
var callback = tl.$$resolveWith || angular.noop;
return $q.when( callback() ).then(function(){
return tl;
});
},
// If the timeline is dirty and needs to rebuild,
// wait for that first.
waitForRebuild = function(tl) {
return (tl && tl.$$dirty) ? tl.$$dirty : tl;
},
// Special lookup or accessor function
$timeline = function ( id, callbacks ){
// Is this an implicit lookup?
if ( angular.isDefined(id) ){
id = stripStateReversal(id);
var promise = hasState(id) ? findByState(id) : findById(id);
return promise
.then( waitForRebuild )
.then( registerCallbacks(callbacks) )
.then( resolveBeforeUse );
}
// Not a lookup, so return the API
return self;
};
// Attach special direct search and make functions to the published
// API for `$timeline` service
angular.forEach(self,function(fn,key){
$timeline[key] = fn;
});
// Publish the service with its API
return $timeline;
// ******************************************************************
// Internal Methods
// ******************************************************************
/**
* Make or update a TimelineMax instance
*
* @param source TimelineController
* @param flushTargets Boolean to rebuild the query selectors
* @returns {*} GSAP TimelineMax instance
*/
function makeTimeline(source, flushTargets) {
source = source || { steps:[ ], children: [ ] };
targets = flushTargets ? { } : targets;
var querySelector = makeQuery( source, targets );
var timeline = source.timeline || new TimelineMax({paused: true, data: {id: source.id || counter++ }});
timeline.clear(true).timeScale( source.timeScale || 1.0 );
timeline.data.id = source.id || timeline.data.id;
source.timeline = timeline;
source.steps = source.steps || [ ];
source.children = source.children || [ ];
source.steps.forEach(function( step ) {
var element = querySelector( step.target );
var callback = keyValue(step, "callback", null );
var position = keyValue(step, "position", null );
var frameLabel = keyValue(step, "markPosition");
var styles = toJSON(updateZIndex(keyValue(step, "style" )));
var duration = getDuration(step, styles);
styles = updateEasing(updateBounds( styles ));
if ( callback ) timeline.addPause( position, callback, [timeline] );
else if ( frameLabel ) timeline.addLabel( frameLabel, position );
else if ( duration === 0 ) timeline.set(element, styles);
else if ( useTweenMax(styles) ) timeline.append( TweenMax.to(element, duration, styles), position );
else timeline.to(element, +duration, styles, position || timeline.totalDuration() );
});
source.children.forEach(function(it){
var child = it.timeline;
var position = keyValue(it, "position", null);
if ( child) {
child.paused(false); // Un-pause timelines when inserting in parent...
if ( !position ) timeline.append(child);
else timeline.insert(child, position);
}
});
return logBuild(source, targets, $log);
}
/**
* Get explicit duration value or calculate an implicit
* duration if certain keys are used.
*/
function getDuration(step, styles) {
var duration = keyValue(step, "duration", 0);
if ( duration === 0 ) {
var hasPosition = !!keyValue(step, "position");
var forceDuration = ( styles.zIndex || styles.className || styles.display );
forceDuration = forceDuration || hasPosition;
if ( forceDuration ) duration = "0.001";
}
return duration;
}
/**
* Some properties require the special powers
* of TweenMax instead of TweenLite...
*
* @param styles
* @returns {boolean|*}
*/
function useTweenMax(styles){
var needTweenMax = false;
if (angular.isDefined(styles.yoyo) ) {
needTweenMax = true;
styles.yoyo = Boolean(styles.yoyo);
}
if (angular.isDefined(styles.repeat) ) {
needTweenMax = true;
styles.repeat = +styles.repeat;
}
return needTweenMax;
}
/**
* Pre-toJSON scan for `z-index`; which is not a valid JSON.
* Replace with `zIndex` key words.
* @param styles
*/
function updateZIndex(styles) {
return angular.isString(styles)? styles.replace(/z-index/g,"zIndex") : styles;
}
/**
* For the special `bounds` style, extract/flatten the specifiers
*/
function updateBounds(styles) {
if ( styles.bounds ) {
"left top width height".split(" ").forEach(function(key) {
var value = styles.bounds[key];
if ( angular.isDefined( value ))
{
styles[key] = value;
}
});
}
delete styles.bounds;
return styles;
}
/**
* Special lookup of the easing instance in the GSAP globals.
* This is used to replace the easing string property chain reference
* with an object instance
*/
function updateEasing(styles) {
var warning = 'TimelineBuilder::makeTimeline() - ignoring invalid easing `{0}` ';
var invalid = true;
var easing = styles.ease || "";
try {
if ( easing.length ) {
var inst = window;
var keys = easing.split(".");
keys.forEach(function(key){
exitOnInvalid(key, inst);
inst = inst[key];
});
invalid = !!inst ? false : true;
styles.ease = inst;
}
}
catch (e ) { /* suppress errors */ }
finally { if (invalid){ delete styles.ease; }}
return styles;
/**
* If full property chain is not valid, then do not continue
* with the ease translation lookup.
*/
function exitOnInvalid(key, inst) {
if ( angular.isUndefined(inst[key]) ) {
$log.warn( warning.supplant([easing]));
throw( new Error('invalid ease') );
}
}
}
/**
* Provide a async lookup to return a timeline after
* all $digest changes have completed.
*
* @param id
* @returns {Deferred.promise|*}
*/
function findById( id ) {
var deferred = $q.defer();
var timeline = cache[id];
if ( timeline ) deferred.resolve(timeline);
else deferred.reject( "Timeline( id == '{0}' ) was not found.".supplant([ id ]) );
return deferred.promise;
}
/**
* Provide a async lookup to return a timeline after
* all $digest changes have completed.
*
* @param state
* @returns {Deferred.promise|*}
*/
function findByState( state ) {
var deferred = $q.defer();
var timeline;
angular.forEach(cache, function(it) {
if ( angular.isDefined(it.$$state) ){
if ( it.$$state == state )
{
timeline = it;
}
}
});
if ( timeline ) deferred.resolve(timeline);
else deferred.reject( "Timeline( state == '{0}' ) was not found.".supplant([ state ]) );
return deferred.promise;
}
/**
* Scan available timelines to see if any have a matching state
* @param state
* @returns {boolean|*}
*/
function hasState(state) {
var found = false;
angular.forEach(cache, function(it) {
if ( angular.isDefined(it.$$state) ){
if ( it.$$state == state )
{
found = true;
}
}
});
return found;
}
/**
* Register timeline for easy lookups later...
*/
function register(timeline, id, state) {
if ( timeline && id && id.length ) {
cache[ id ] = timeline;
if ( angular.isDefined(state) )
{
timeline.$$state = state;
}
}
}
}
/**
* Timeline Controller
* Each controller manages its own timeline with its nested child steps.
* However, a Timeline controller may be a child of a parent Timeline controller.
*
* @param $scope
* @constructor
*/
function TimeLineController($scope, $element, $q, $timeout, $timeline, $log) {
var self = this,
timeline = null,
children = [ ],
steps = [ ],
pendingRebuild = null,
bouncedRebuild = null,
debounce = $debounce( $timeout ),
parentController = $element.parent().controller('gsTimeline');
self.addStep = onStepChanged; // Used by StepDirective
self.addCallback = onAddCallback; // Used by PauseDirectives
self.addChild = onAddTimeline; // Used by TimelineDirective
self.addResolve = onAddResolve; // Used by TimelineDirective
// Rebuild when the timeScale changes..
$scope.$watch('timeScale', function(current,previous) {
if ( current !== previous ) {
$log.debug( ( previous != "" ) ?
'timeScale( {0} -> {1} )'.supplant([previous, current]) :
'timeScale( {0} )'.supplant([current])
);
asyncRebuild();
}
});
/**
* Deferred external accessor to `timeline` to support asyncRebuild(s).
*/
self.timeline = function() {
return pendingRebuild ? pendingRebuild.promise : $q.when(timeline);
};
// ******************************************************************
// Internal Builder Methods
// ******************************************************************
/**
* Create a `debounced` async timeline build process; this allows
* all step changes and child timeline additions to complete before
* rebuilding.
*/
function asyncRebuild() {
pendingRebuild = pendingRebuild || $q.defer();
bouncedRebuild = bouncedRebuild || debounce( rebuildTimeline );
// Keep debouncing...
bouncedRebuild();
// Temporarily mark this as dirty...
if ( timeline != null ) timeline.$$dirty = pendingRebuild.promise;
/**
* Rebuild the timeline when the steps or children timelines are changed...
*/
function rebuildTimeline() {
try {
// No rebuilding while active...
if ( timeline && timeline.isActive() ) {
timeline.kill();
}
// Build or update the TimelineMax instance
timeline = $timeline.makeTimeline({
id : $scope.id,
timeline : timeline,
steps : steps,
children : children,
target : findTimelineTarget(),
timeScale: +$scope.timeScale || 1.0
});
// Register for easy lookups later...
$timeline.register( timeline, $scope.id, $scope.state );
// Add to parent as child timeline (if parent exists)
if ( parentController ) {
parentController.addChild( timeline, $scope.position );
}
// Then resolve promise (for external requests)
delete $timeline.$$dirty;
pendingRebuild.resolve( timeline );
}
catch( e ) { pendingRebuild.reject(e); }
finally { pendingRebuild = null; }
}
}
/**
* Scan up the parent controller ancestor chain to find the fallback
* target, if it is not specified on the current scope.
* @returns {*}
*/
function findTimelineTarget() {
var target = $scope.target;
if ( !angular.isDefined(target) || (target == "") ) {
var parent = $element.parent();
var timelineCntrl = parent.controller('gsTimeline');
var isRoot = !timelineCntrl;
while ( timelineCntrl ) {
if ( hasValidTarget(parent) ) {
target = parent.attr("target");
break;
}
parent = angular.element(parent.parent());
timelineCntrl = parent.controller('gsTimeline');
}
}
return target;
/**
* Has the `target="<dom selector>"` attribute been specified?
* @param element
* @returns {boolean|*}
*/
function hasValidTarget(element) {
var target = element.attr("target");
return (angular.isDefined(target) && (target != ""));
}
}
/**
* Add a child timeline instance to this timeline parent
*
* @param timeline Child TimelineMax instance
* @param position start offset in the parent timeline
*/
function onAddTimeline(timeline, position) {
try {
// If not already registered...
if ( children.indexOf(timeline) < 0 ) {
children.push({
timeline : timeline,
position : position
});
}
} finally {
// Perform an async rebuild of the timeline; async to
// respond to all changes as a single batch response.
asyncRebuild();
}
}
/**
* Add hidden resolve callback to the timeline; which will be
* resolved PRIOR to the resolve of the `$timeline( id )` promise.
*
* @param callback
*/
function onAddResolve( callback ) {
timeline = timeline || $timeline.makeTimeline();
timeline.$$resolveWith = callback;
}
/**
* When properties of a child step changes,
* we need to rebuild the timeline...
* @param step
*/
function onStepChanged(step) {
try{
if ( steps.indexOf(step) < 0 ) {
// If not already registered...
steps.push(step);
}
} finally {
// Perform an async rebuild of the timeline; async to
// respond to all changes as a single batch response.
asyncRebuild();
}
}
/**
* Add a special step that will pause the timeline progress
* until the callback finishes or resolves.
*
* @param fn Function created in the `gs-pause` Directive
* @param position
*/
function onAddCallback(fn, position) {
self.addStep({
callback : wrapCallback(fn),
position : position,
params : ["{self}"]
});
/**
* Create wrapper function that will pause the timeline
* while the callback is in-progress, then resume once
* the callback finishes. If a promise is returned, then
* the promise must be resolved before the timeline will continue
*
* @param fn
* @returns {Function}
*/
function wrapCallback(fn) {
return function (tl) {
$q.when( fn() )
.then( function() {
tl.resume();
});
}
}
}
}
/**
* @ngdoc directive
* Timeline Directive contains 0..n steps and 0..n child timelines
*
* @param state String constant used to auto-trigger animation with the parent scope `state` matches
* @param timeScale Float value configures time scale of timeline instance
* @param target String element identifier of the fallback target element for each step
* @param resolve Expression that is evaluated before each timeline is ready to run
* @param position String identifier that specifies the position of this timeline within a parent timeline
*
* @returns {{restrict: string, controller: string, link: Function}}
* @constructor
*/
function TimelineDirective($parse, $$gsStates) {
var counter = 1;
return {
restrict: "E",
scope : {
timeScale : "@?"
// state : "@?",
// target : "@?target",
// position : "@?position",
// resolve : "&?resolve",
},
require : "^?gsTimeline",
controller : TimeLineController,
link : function (scope, element, attr, controller )
{
// Manually access these static properties
scope.id = attr.id || attr.state || ("timeline_" + counter++);
scope.position = attr.position || 0;
scope.timeScale = scope.timeScale || 1.0;
scope.state = attr.state;
scope.target = attr.target;
prepareResolve();
prepareStateWatch();
// ******************************************************************
// Internal Methods
// ******************************************************************
/**
* Prepare the `resolve` expression to be evaluated AFTER $digest() and BEFORE
* the timeline instances are reconstructed...
*/
function prepareResolve() {
if ( angular.isDefined(attr.resolve) )
{
var context = scope.$parent;
var fn = $parse(attr["resolve"], /* interceptorFn */ null, /* expensiveChecks */ true);
controller.addResolve( function(){
// fn() may return a promise or value;
// both are wrapped in $q.when(<value>)
return fn(context);
});
}
}
/**
* Each timeline that has a state should be registered...
*/
function prepareStateWatch() {
if ( angular.isDefined(attr.state) ) {
// Register this timeline with Timeline State management
$$gsStates.addTimeline({
scope : scope,
state : attr.state,
controller : controller,
parentController : element.parent().controller('gsTimeline')
});
}
}
}
};
}
/**
* @ngdoc directive
* Step Directive
*
* Steps can only be defined as children of a Timeline. Steps are used to label frames, set styles,
* or animate 1..n sets of properties for a specific duration.
*
* @returns {{restrict: string, scope: {style: string, duration: string, position: string, markPosition: string, clazz: string}, require: string[], link: LinkStepDirective}}
* @constructor
*
* @example:
*
* <gs-step className=""
* duration="0.3"
* position="0.1"
* style="opacity:1; left:{{source.left}}; top:{{source.top}}; width:{{source.width}}; height:{{source.height}};" >
* </gs-step>
*/
function StepDirective() {
return {
restrict : "E",
scope : {
className : "@?",
duration : "@?",
markPosition : "@?",
position : "@?",
style : "@"
},
require : "^gsTimeline",
compile : function compile(tElement, tAttrs, transclude) {
// Convert all commas to semi-colons (for later conversion to JSON)
if ( angular.isDefined(tAttrs.style) ) {
tAttrs.style = tAttrs.style.replace(/,/g,";");
}
return function LinkStepDirective(scope, element, attr, ctrl) {
scope.target = attr.target;
scope.$watch('style', function onChangeStep() {
ctrl.addStep(scope);
});
}
}
};
}
/**
* @ngdoc directive
* Step Directive
*
* Steps can only be defined as children of a Timeline. Steps are used to label frames, set styles,
* or animate 1..n sets of properties for a specific duration.
*
* @returns {{restrict: string, scope: {style: string, duration: string, position: string, markPosition: string, clazz: string}, require: string[], link: LinkStepDirective}}
* @constructor
*
* @example:
*
* <gs-step className=""
* duration="0.3"
* position="0.1"
* style="opacity:1; left:{{source.left}}; top:{{source.top}}; width:{{source.width}}; height:{{source.height}};" >
* </gs-step>
*/
function PauseDirective() {
return {
restrict : "E",
scope : {
resolve : "&",
position : "@"
},
require : "^gsTimeline",
link : function LinkStepDirective(scope, element, attr, ctrl) {
ctrl.addCallback(scope.resolve, scope.position );
}
};
}
/**
* @ngdoc directive
* Scale Directive
*
* Scale the attached element to the window inner bounds or to a fixed scale value
*
* Startup viewport scaling for UX; this will increase
* the stage size to fill the window area with
* PROPORTIONAL_FIT_INSIDE
*
*/
function ScaleDirective($window, $timeout) {
return {
restrict : "A",
link : function LinkStepDirective(scope, element, attr) {
var fixedScale = attr['gsScale'] ? parseFloat(attr['gsScale']) : NaN;
var isLocked = !isNaN(fixedScale);
var timeline = new TimelineMax();
adjustScaling();
if ( !isLocked ) {
// Watch resize; remove watcher during tear down.
scope.$on("$destroy", watchResize( adjustScaling ) );
}
/**
* Will autoAdjust the scale so the target element will proportionally
* fit INSIDE the window; unless an explicit size has been defined
*
* gs-scale === adjust scale to window size
* gs-scale="1.0" === lock scale to 1x (ignore window size)
* gs-scale="3" === lock scale to 3x (ignore window size)
*
*/
function adjustScaling() {
var win = {
width : $window.innerWidth-20,
height: $window.innerHeight-20
},
stage = {
width : element[0].clientWidth,
height: element[0].clientHeight
},
scaling = Math.min(
win.height/stage.height,
win.width/stage.width
),
selector = '#' + attr.id;
// Scale and FadeIn entire stage for better UX
timeline.clear(true)
.set(selector, { scale:isLocked ? fixedScale : scaling, transformOrigin:"0 0 0" } )
.to(selector, 0.5, {opacity:1});
}
}
};
/**
* Auto-adjust scaling when window resizes... minimize
* the # of events to debounce until idle for 30 frames
*/
function watchResize( targetFn ) {
var win = angular.element($window);
var debounce = $debounce($timeout, true);
var onResizeFn = debounce(targetFn, 30);
win.bind('resize', onResizeFn);
// Publish unwatch method...
return function(){
win.unbind('resize', onResizeFn);
}
}
}
// ******************************************************************
// Internal Debug Methods
// ******************************************************************
/**
* Output the build steps and details to the console...
*
* @param source
* @param targets
* @param $log
* @returns {*|TimelineMax}
*/
function logBuild( source, targets, $log ) {
$log.debug( ">> TimelineBuilder::makeTimeline() invoked by $timeline('{data.id}')".supplant(source.timeline) );
source.steps.forEach(function( step ) {
var frameLabel = keyValue(step, "markPosition");
var position = keyValue(step, "position", "");
var styles = toJSON(keyValue(step, "style"));
var hasDuration = !!keyValue(step, "duration");
var duration = hasDuration ? keyValue(step, "duration") : 0;
if ( frameLabel ) $log.debug( "addLabel( '{0}' )" .supplant( [frameLabel] ));
else if ( hasDuration ) $log.debug( "timeline.set( '{0}', {1}, {2}, '{3}' )" .supplant( [step.target, duration, JSON.stringify(styles), position ] ));
else $log.debug( "timeline.set( '{0}', '{1}' )" .supplant( [step.target, JSON.stringify(styles)] ));
});
source.children.forEach(function(it){
if ( it.timeline ) {
$log.debug( "$timeline('{0.data.id}').addChild( '{1.data.id}' )".supplant([source.timeline, it.timeline]) );
}
});
return source.timeline;
}
// ******************************************************************
// Internal DOM Query Method
// ******************************************************************
/**
* Find DOM element associated with query selector
* Use the fallback timeline target if the step target `selector` is not
* specified.
*
* @param selector
* @returns {*}
*/