Advertisement
sammarks

Untitled

Dec 8th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * quadmail.pages.js - Quadmail pages plugin.
  3.  * @author Samuel Marks
  4.  */
  5.  
  6. QM(function() {
  7.   QM.fn.pages = function(method) {
  8.     var methods = {
  9.       init: function(options) {
  10.         this.pages.settings = $.extend({}, this.pages.defaults, options);
  11.  
  12.         return this.each(function() {
  13.           var $element = QM(this), element = this;
  14.          
  15.           // Hide all pages, and show the first page.
  16.           $element.children('.page').hide();
  17.           $element.children('.page:first-child').show();    
  18.          
  19.           // Add all the children pages to the pages array.
  20.           helpers.pages = $element.children('.page');
  21.           helpers.defaultPage = QM($element.children()[0]);
  22.           helpers.currentPage = QM($element.children()[0]);
  23.           helpers.currentPageIndex = 0;
  24.          
  25.           console.log(helpers);
  26.          
  27.           // Apply the CSS to parent and children pages.
  28.           if ($element.css('position') != 'absolute')
  29.             $element.css({'position': 'relative', 'overflow': 'hidden', 'height': helpers.findTallest().height(), 'padding-bottom': '10px'});
  30.           $element.children('.page').css({
  31.             'position': 'absolute',
  32.             'top': '0',
  33.             'left': '0'
  34.           });
  35.         });
  36.       },
  37.      
  38.       nextPage: function(callback) {
  39.         return this.each(function() {
  40.           var $element = QM(this), element = this;
  41.          
  42.           console.log(helpers);
  43.           var from = helpers.currentPage;
  44.           var nextIndex = helpers.currentPageIndex + 1;
  45.           if (nextIndex >= helpers.pages.length)
  46.             nextIndex = 0;
  47.           var to = helpers.pages[nextIndex];
  48.           helpers.transition(from, to, function() {
  49.             helpers.currentPage = to;
  50.             helpers.currentPageIndex += 1;
  51.             // Call the callback
  52.             if (callback != null) callback();
  53.           });
  54.         });
  55.       },
  56.      
  57.       previousPage: function(callback) {
  58.         var from = helpers.currentPage;
  59.         var nextIndex = helpers.currentPageIndex - 1;
  60.         if (nextIndex <= helpers.pages.length)
  61.           nextIndex = helpers.pages.length;
  62.         var to = helpers.pages[nextIndex];
  63.         helpers.transition(from, to, function() {
  64.           helpers.currentPage = to;
  65.           // Call the callback
  66.           if (callback != null) callback();
  67.         });
  68.       }
  69.     }
  70.  
  71.     var helpers = {
  72.       pages: new Array(),
  73.       defaultPage: '',
  74.       currentPage: '',
  75.       currentPageIndex: 1,
  76.       animating: false,
  77.       plugin: QM.fn.pages,
  78.      
  79.       transition: function(from, to, callback) {
  80.         if (helpers.animating == false)
  81.           helpers.animating = true;
  82.         var s = this.plugin.settings.speed;
  83.         var transition = this.plugin.settings.transition;
  84.         var f = QM(from);
  85.         var t = QM(to);
  86.        
  87.         console.log("[FROM]");
  88.         console.log(from);
  89.         console.log("[TO]");
  90.         console.log(to);
  91.         console.log("[SPEED] " + s);
  92.         console.log("[TRANSITION] " + transition);
  93.        
  94.         switch(transition) {
  95.           case 'fade':
  96.             f.fadeOut(s, function() { t.fadeIn(s); helpers.animating = false; if (callback != null) callback(); });
  97.             break;
  98.           case 'crossfade':
  99.             f.fadeOut(s);
  100.             t.fadeIn(s, function() { helpers.animating = false; if (callback != null) callback(); });
  101.             break;
  102.           case 'slide':
  103.             var fHeight = f.height();
  104.             var tHeight = t.height();
  105.             var containerHeight = QM(this).height();
  106.             t.show();
  107.             f.animate({'tp': (fHeight * -1)}, s, 'swing', function() { this.hide(); helpers.animating = false; if (callback != null) callback(); });
  108.             t.css('tp', containerHeight);
  109.             t.animate({'tp': 0}, s, 'swing');
  110.             break;
  111.         }
  112.       },
  113.      
  114.       findTallest: function() {
  115.         var tallestObj = null;
  116.         var tallestInt = 0;
  117.         for (var i = 0; i < helpers.pages.length; i++) {
  118.           var pg = QM(helpers.pages[i]);
  119.           if (pg.height() > tallestInt) {
  120.             tallestInt = pg.height();
  121.             tallestObj = pg;
  122.           }
  123.         }
  124.         return tallestObj;
  125.       }
  126.     }
  127.  
  128.     if (methods[method]) {
  129.       return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  130.     } else if (typeof method === 'object' || !method) {
  131.       return methods.init.apply(this, arguments);
  132.     } else {
  133.       QM.error("Method '" + method + "' does not exist in pages plugin!");
  134.     }
  135.   }
  136.  
  137.   // Default plugin settings.
  138.   QM.fn.pages.defaults = {
  139.     transition: 'fade',
  140.     speed: 500
  141.   }
  142.  
  143.   QM.fn.pages.settings = {}
  144. });
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement