ample_assets 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module AmpleAssets
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,581 @@
1
+ (function($) {
2
+
3
+ var defaults = {
4
+ auto : true,
5
+ interval : 3500,
6
+ counter : 0,
7
+ continuous : false,
8
+ duration : 200,
9
+ rewind: false,
10
+ debug : false,
11
+ previous : false,
12
+ next : false,
13
+ nav: false,
14
+ el : false,
15
+ parent : 'ul',
16
+ children : 'li',
17
+ width : 250,
18
+ height : 250,
19
+ distance : 0,
20
+ easing: 'easeInOutQuart',
21
+ orientation: 'horizontal',
22
+ key_orientation: 'horizontal',
23
+ active_class: 'on',
24
+ per_page : 1,
25
+ keyboard_nav : false,
26
+ enabled : true
27
+ };
28
+
29
+ $.amplePanels = function(e, o) {
30
+ this.options = $.extend({}, defaults, o || {});
31
+ this.options.el = e;
32
+ this.init();
33
+ };
34
+
35
+ $.amplePanels.fn = $.amplePanels.prototype = {
36
+
37
+ /**
38
+ * constructor function
39
+ */
40
+ init: function() {
41
+ this.log('init()')
42
+ this.setup();
43
+ this.buttons();
44
+ },
45
+
46
+ /**
47
+ * add styles to head, wrap container element with mask
48
+ */
49
+ setup : function( ) {
50
+ this.log('setup()');
51
+ this.options.id = $(this.options.el).attr('id');
52
+ this.children = $(this.options.el).find(this.options.children);
53
+ this.options.total = this.children.length;
54
+ switch(this.options.orientation) {
55
+ case 'vertical': this.vertical(); break;
56
+ default: this.horizontal(); break;
57
+ }
58
+ this.clone_els();
59
+ this.events();
60
+ this.nav();
61
+ this.start();
62
+ },
63
+
64
+ /**
65
+ * setup horizontal orientation
66
+ */
67
+ horizontal : function() {
68
+ this.log('horizontal()');
69
+ $('<style>.ampn #' + $(this.options.el).attr('id') + ' ' + this.options.parent + ', .ampn #' + $(this.options.el).attr('id') + ' ' + this.options.children + ' { list-style-type:none; } .ampn #' + $(this.options.el).attr('id') + ' ' + this.options.children + ' { float: left; margin-right: ' + this.options.distance + 'px; }</style>').appendTo('head');
70
+ var width = (this.options.width + this.options.distance) * this.options.per_page - this.options.distance;
71
+ if(this.width_percentage()) {
72
+ width = this.options.width;
73
+ }
74
+ $(this.options.el).wrap($('<div id="' + this.options.id + '-wrapper" class="ampn"></div>').css({ 'width': width, 'height': this.options.height, overflow: 'hidden' }));
75
+ this.set_width();
76
+ },
77
+
78
+ /**
79
+ * setup vertical orientation
80
+ */
81
+ vertical : function() {
82
+ this.log('vertical()');
83
+ var width = this.width_percentage() ? this.options.width : String(parseInt(this.options.width)) + 'px';
84
+ var height = String(parseInt(this.options.height)) + 'px';
85
+ $('<style>.ampn #' + $(this.options.el).attr('id') + ' ' + this.options.children + ' { width: ' + width + '; height: ' + height + '; overflow: hidden; margin-bottom: ' + this.options.distance + 'px; }</style>').appendTo('head');
86
+ var height = (this.options.height + this.options.distance) * this.options.per_page - this.options.distance;
87
+ $(this.options.el).wrap($('<div id="' + this.options.id + '-wrapper" class="ampn"></div>').css({ 'width': this.options.width, 'height': height, overflow: 'hidden' }));
88
+ this.set_height();
89
+ },
90
+
91
+ /**
92
+ * for continuous instances, prepend last original item and append first original item
93
+ */
94
+ clone_els : function() {
95
+ if(this.options.continuous) {
96
+ this.log('clone_els()')
97
+ var width = this.options.width;
98
+ if(this.width_percentage()) {
99
+ width = $(this.children).first().width();
100
+ }
101
+ var distance = "-" + (width + this.options.distance) +"px";
102
+ $.each($(this.options.children), function(i,el){
103
+ $(el).attr('data-slide-item',i);
104
+ });
105
+ $(this.options.el).prepend($(this.options.children + ":last-child", this.options.el).clone().css(( this.options.orientation=='horizontal' ? 'margin-left' : 'margin-top' ), distance));
106
+ for(var i = 1; i < ( this.options.continuous ? this.options.total : 2 ); i++) {
107
+ $(this.options.el).append($(this.options.children + ":eq(" + i + ")", this.options.el).clone());
108
+ }
109
+ this.set_width();
110
+ }
111
+ },
112
+
113
+ /**
114
+ * add events to buttons, keys, etc.
115
+ */
116
+ events: function( ) {
117
+ var ref = this;
118
+ this.key_down();
119
+ if(this.options.previous) {
120
+ $(this.options.previous).bind('click',function(){
121
+ ref.stop();
122
+ ref.previous();
123
+ });
124
+ }
125
+ if(this.options.next) {
126
+ $(this.options.next).bind('click',function(){
127
+ ref.stop();
128
+ ref.next();
129
+ });
130
+ }
131
+ },
132
+
133
+ /**
134
+ * build navigation
135
+ */
136
+ nav: function( ) {
137
+ if(this.options.nav) {
138
+ if(this.options.continuous) {
139
+ this.paged_nav();
140
+ } else {
141
+ this.itemized_nav();
142
+ }
143
+ }
144
+ },
145
+
146
+ /**
147
+ * build paged navigation
148
+ */
149
+ paged_nav: function() {
150
+ this.log('paged_nav()')
151
+ var ref = this;
152
+ var previous = $('<a href="#"></a>')
153
+ .addClass('previous')
154
+ .text('Previous')
155
+ .click(function() {
156
+ ref.stop();
157
+ ref.previous();
158
+ });
159
+ var next = $('<a href="#"></a>')
160
+ .addClass('next')
161
+ .text('Next')
162
+ .click(function() {
163
+ ref.stop();
164
+ ref.next();
165
+ });
166
+ var nav = $('<div></div>')
167
+ .append( previous )
168
+ .append( '<span data-role="current-slide">' + 1 + '</span> of ' )
169
+ .append( '<span data-role="total-slides">' + this.options.total + '</span>' )
170
+ .append( next );
171
+ $(this.options.nav).append(nav);
172
+ this.activate();
173
+ },
174
+
175
+ /**
176
+ * build itemized navigation
177
+ */
178
+ itemized_nav: function() {
179
+ this.log('itemized_nav()')
180
+ var ref = this;
181
+ var nav = $('<ul></ul>');
182
+ $.each($(this.options.el).find(this.options.children), function(i,el){
183
+ nav.append('<li><a href="#" data-role="' + i + '">' + i + '</a></li>');
184
+ });
185
+ $(this.options.nav).append(nav);
186
+ $(this.options.nav).find('a').click(function(){
187
+ ref.goto($(this).attr('data-role'));
188
+ return false;
189
+ })
190
+ this.activate();
191
+ },
192
+
193
+ /**
194
+ * enable / disable navigation buttons
195
+ */
196
+ buttons: function() {
197
+ if( !this.options.continuous ) {
198
+ $( this.options.previous ).attr('disabled', ( this.options.counter == 0 ? true : false ) );
199
+ $( this.options.next ).attr('disabled', ( this.options.counter == this.last_page() ? true : false ) );
200
+ }
201
+ },
202
+
203
+ /**
204
+ * update container el's width
205
+ */
206
+ set_width: function( ) {
207
+ this.log('set_width()')
208
+ this.children = $(this.options.el).find(this.options.children);
209
+ var total = this.children.length;
210
+ var width = 0;
211
+ if(this.width_percentage()) {
212
+ $.each(this.children, function(i,el) {
213
+ width += $(el).children().first().width();
214
+ });
215
+ } else {
216
+ width = (this.options.width + this.options.distance) * total;
217
+ }
218
+ $(this.options.el).css('width', width);
219
+ },
220
+
221
+ /**
222
+ * update container el's height
223
+ */
224
+ set_height: function( ) {
225
+ this.log('set_height()')
226
+ var total = $(this.options.el).find(this.options.children).length;
227
+ $(this.options.el).css('height',( (this.options.height + this.options.distance) * total) );
228
+ },
229
+
230
+ /**
231
+ * scroll to
232
+ */
233
+ goto: function( i ) {
234
+ if(!this.options.enabled) {
235
+ return false;
236
+ }
237
+ this.options.counter = parseInt(i);
238
+ this.stop();
239
+ switch(this.options.orientation) {
240
+ case 'vertical': this.slide_vertically(); break;
241
+ default: this.slide_horizontally(); break;
242
+ }
243
+ },
244
+
245
+ enable: function() {
246
+ this.options.enabled = true;
247
+ },
248
+
249
+ disable: function() {
250
+ this.options.enabled = false;
251
+ },
252
+
253
+ /**
254
+ * next page
255
+ */
256
+ next: function( ) {
257
+ if(!this.options.enabled) {
258
+ return false;
259
+ }
260
+ if(!this.options.continuous) {
261
+ if(!this.options.rewind && this.options.counter == this.last_page()) { return false; }
262
+ }
263
+ this.count('next');
264
+ switch(this.options.orientation) {
265
+ case 'vertical': this.slide_vertically(); break;
266
+ default: this.slide_horizontally(); break;
267
+ }
268
+ return false;
269
+ },
270
+
271
+ /**
272
+ * previous page
273
+ */
274
+ previous: function( ) {
275
+ if(!this.options.enabled) {
276
+ return false;
277
+ }
278
+ if(!this.options.continuous) {
279
+ if(!this.options.rewind && this.options.counter == 0) { return false; }
280
+ }
281
+ this.count('previous');
282
+ switch(this.options.orientation) {
283
+ case 'vertical': this.slide_vertically(); break;
284
+ default: this.slide_horizontally(); break;
285
+ }
286
+ return false;
287
+ },
288
+
289
+ /**
290
+ * return true if width option is a percentage value
291
+ */
292
+ width_percentage: function( ) {
293
+ return String(this.options.width).slice(-1)=='%' ? true : false
294
+ },
295
+
296
+ /**
297
+ * remove active class from navigation els
298
+ */
299
+ deactivate: function( ) {
300
+ $(this.options.nav).find('li').removeClass(this.options.active_class);
301
+ },
302
+
303
+ /**
304
+ * apply active class to navigation els
305
+ */
306
+ activate: function( ) {
307
+ if(this.options.nav) {
308
+ this.log('activate()');
309
+ var active;
310
+ if(this.options.continuous) {
311
+ $(this.options.nav).find('span[data-role=current-slide]').text(this.active);
312
+ } else {
313
+ this.deactivate();
314
+ active = $(this.options.nav).find('li').get(this.options.counter);
315
+ $(active).addClass(this.options.active_class);
316
+ }
317
+ }
318
+ },
319
+
320
+ /**
321
+ * increment / decrement page counter
322
+ */
323
+ count: function( dir ) {
324
+ this.dir = dir;
325
+ var last_page = this.last_page();
326
+ switch(dir) {
327
+ case 'previous':
328
+ this.options.counter = this.options.counter == 0 ? parseInt(last_page) : parseInt(this.options.counter) - 1;
329
+ break;
330
+ case 'next':
331
+ this.options.counter = this.options.counter == parseInt(last_page) ? 0 : parseInt(this.options.counter) + 1;
332
+ break;
333
+ };
334
+ },
335
+
336
+ /**
337
+ * return the page counter for last page
338
+ */
339
+ last_page: function() {
340
+ return Math.ceil ( $(this.options.el).find(this.options.children).length / this.options.per_page ) - 1;
341
+ },
342
+
343
+ /**
344
+ * slide panel horizontally
345
+ */
346
+ slide_horizontally: function( ) {
347
+
348
+ var counter = this.options.counter;
349
+ if(this.options.continuous && counter>this.options.total) {
350
+ counter = -1;
351
+ }
352
+
353
+ $( this.options.el ).trigger('slide_horizontal', [counter, this.dir]);
354
+
355
+ var left = 0;
356
+ if( this.width_percentage() ) {
357
+ for(var i=0; i<(this.options.counter); i++) {
358
+ left += ($(this.children[i]).first().width() + this.options.distance ) * this.options.per_page;
359
+ }
360
+ if( counter == -1 ) {
361
+ left = ($(this.children).first().width() + this.options.distance ) * this.options.per_page * counter;
362
+ }
363
+ } else {
364
+ left = (this.options.width + this.options.distance ) * this.options.per_page * counter;
365
+ }
366
+ var properties = { marginLeft: left * -1 + 'px' };
367
+ var options = { duration: this.options.duration };
368
+ if(jQuery().easing) {
369
+ options['easing'] = this.options.easing;
370
+ }
371
+ if(this.rewinding()) {
372
+ options['duration'] = this.options.duration * 2;
373
+ }
374
+ var ref = this;
375
+ if(this.options.continuous) {
376
+ options['complete'] = function(){
377
+ $(ref.options.el).trigger('complete')
378
+ ref.reset();
379
+ }
380
+ } else {
381
+ options['complete'] = function(){
382
+ $(ref.options.el).trigger('complete')
383
+ }
384
+ }
385
+ $( this.options.el ).animate( properties, options );
386
+ this.buttons();
387
+ this.current(counter);
388
+ this.activate();
389
+ },
390
+
391
+ /**
392
+ * slide panel vertically
393
+ */
394
+ slide_vertically: function( ) {
395
+
396
+ var counter = this.options.counter;
397
+ if( this.options.continuous ) {
398
+ counter += 1;
399
+ if(this.dir == 'previous' && counter > this.options.total) {
400
+ counter = -1;
401
+ }
402
+ }
403
+
404
+ $( this.options.el ).trigger('slide_vertical', counter);
405
+
406
+ var top = ( this.options.height + this.options.distance ) * this.options.per_page * counter;
407
+ var properties = { marginTop: top * -1 + 'px' };
408
+ var options = { duration: this.options.duration };
409
+ if(jQuery().easing) {
410
+ options['easing'] = this.options.easing;
411
+ }
412
+ if(this.rewinding()) {
413
+ options['duration'] = this.options.duration * 2;
414
+ }
415
+ var ref = this;
416
+ if(this.options.continuous) {
417
+ options['complete'] = function(){
418
+ $(ref.options.el).trigger('complete')
419
+ ref.reset();
420
+ }
421
+ } else {
422
+ options['complete'] = function(){
423
+ $(ref.options.el).trigger('complete', counter)
424
+ }
425
+ }
426
+
427
+ $( this.options.el ).animate( properties, options );
428
+ this.buttons();
429
+ this.current(counter);
430
+ this.activate();
431
+ },
432
+
433
+ current: function(counter) {
434
+ var i = $(this.options.el).find(this.options.children + ':eq(' + (counter+1) + ')').attr('data-slide-item');
435
+ this.active = parseInt(i) + 1
436
+ },
437
+
438
+ /**
439
+ * if instance is continuous, reset position after animation
440
+ */
441
+ reset: function() {
442
+ if(this.options.orientation == 'horizontal') {
443
+ if( this.options.total == this.options.counter ) {
444
+ $( this.options.el ).css({ 'margin-left': 0 });
445
+ this.options.counter = 0;
446
+ } else if( this.options.counter > this.options.total ) {
447
+ this.log('reset')
448
+ if(this.options.continuous && this.width_percentage()) {
449
+ var width = 0, total = this.options.total;
450
+ var distance = this.options.distance, per_page = this.options.per_page;
451
+ $.each(this.children,function(i,el){
452
+ if(i > 0 && i < total) {
453
+ width += ( $(el).width() + distance ) * per_page;
454
+ }
455
+ });
456
+ $( this.options.el ).css({ 'margin-left': width * -1 });
457
+ } else {
458
+ $( this.options.el ).css({ 'margin-left': (( this.options.width + this.options.distance ) * this.options.per_page * ( this.options.total - 1 )) * -1 });
459
+ }
460
+ this.options.counter = this.options.total - 1;
461
+ }
462
+ } else if(this.options.orientation == 'vertical') {
463
+ if( this.options.total == this.options.counter ) {
464
+ $( this.options.el ).css({ 'margin-top': (( this.options.height + this.options.distance ) * this.options.per_page) * -1 });
465
+ this.options.counter = 0;
466
+ } else if( this.options.counter > this.options.total ) {
467
+ $( this.options.el ).css({ 'margin-top': (( this.options.height + this.options.distance ) * this.options.per_page) * this.options.total * -1 });
468
+ this.options.counter = this.options.total - 1;
469
+ }
470
+ }
471
+ },
472
+
473
+ /**
474
+ * on keydown event handler
475
+ */
476
+ key_down: function() {
477
+ if(!this.options.keyboard_nav) return;
478
+ var ref = this;
479
+ var previous = this.options.key_orientation == 'vertical' ? 38 : 37;
480
+ var next = this.options.key_orientation == 'vertical' ? 40 : 39;
481
+
482
+ $(document).keydown(function(e){
483
+ switch(e.keyCode) {
484
+ case previous:
485
+ ref.stop();
486
+ ref.previous();
487
+ break;
488
+ case next:
489
+ ref.stop();
490
+ ref.next();
491
+ break;
492
+ }
493
+ });
494
+ },
495
+
496
+ /**
497
+ * is this animation a rewind?
498
+ */
499
+ rewinding : function() {
500
+ var rewinding = false;
501
+ switch(this.dir) {
502
+ case 'next':
503
+ rewinding = this.options.counter == 0 ? true : false;
504
+ break;
505
+ case 'previous':
506
+ rewinding = this.options.counter == this.last_page() ? true : false;
507
+ break;
508
+ }
509
+ return this.options.rewind && rewinding ? true : false;
510
+ },
511
+
512
+ /**
513
+ * push new els onto container el
514
+ */
515
+ append : function( el ) {
516
+ this.log('append()')
517
+ $(this.options.el).append(el);
518
+ this.update();
519
+ },
520
+
521
+ update : function( ) {
522
+ this.log('update()')
523
+ this.set_width();
524
+ this.buttons();
525
+ },
526
+
527
+ empty : function( ) {
528
+ this.log('empty()')
529
+ this.goto(0);
530
+ $(this.options.el).empty();
531
+ this.update();
532
+ },
533
+
534
+ /**
535
+ * start auto advance
536
+ */
537
+ start : function() {
538
+ this.log('start()');
539
+ if(this.options.auto) {
540
+ var ref = this;
541
+ this.interval = setInterval(function() {
542
+ ref.next();
543
+ }, this.options.interval);
544
+ }
545
+ },
546
+
547
+ /**
548
+ * stop auto advance
549
+ */
550
+ stop : function() {
551
+ if(!this.options.enabled) {
552
+ return false;
553
+ }
554
+ this.log('stop()');
555
+ clearInterval(this.interval)
556
+ },
557
+
558
+ /**
559
+ * talk to me
560
+ */
561
+ log : function( msg ) {
562
+ if (this.options.debug) {
563
+ msg = typeof(msg)=='object' ? msg : 'AMPLE: '+msg;
564
+ !window.console ? alert(msg) : console.log(msg);
565
+ }
566
+ }
567
+
568
+ };
569
+
570
+ $.fn.amplePanels = function(o) {
571
+ if (typeof o == 'string') {
572
+ var instance = $(this).data('amplePanels'), args = Array.prototype.slice.call(arguments, 1);
573
+ return instance[o].apply(instance, args);
574
+ } else {
575
+ return this.each(function() {
576
+ $(this).data('amplePanels', new $.amplePanels(this, o));
577
+ });
578
+ }
579
+ };
580
+
581
+ })(jQuery);