Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * quadmail.pages.js - Quadmail pages plugin.
- * @author Samuel Marks
- */
- QM(function() {
- QM.fn.pages = function(method) {
- var methods = {
- init: function(options) {
- this.pages.settings = $.extend({}, this.pages.defaults, options);
- return this.each(function() {
- var $element = QM(this), element = this;
- // Hide all pages, and show the first page.
- $element.children('.page').hide();
- $element.children('.page:first-child').show();
- // Add all the children pages to the pages array.
- helpers.pages = $element.children('.page');
- helpers.defaultPage = QM($element.children()[0]);
- helpers.currentPage = QM($element.children()[0]);
- helpers.currentPageIndex = 0;
- console.log(helpers);
- // Apply the CSS to parent and children pages.
- if ($element.css('position') != 'absolute')
- $element.css({'position': 'relative', 'overflow': 'hidden', 'height': helpers.findTallest().height(), 'padding-bottom': '10px'});
- $element.children('.page').css({
- 'position': 'absolute',
- 'top': '0',
- 'left': '0'
- });
- });
- },
- nextPage: function(callback) {
- return this.each(function() {
- var $element = QM(this), element = this;
- console.log(helpers);
- var from = helpers.currentPage;
- var nextIndex = helpers.currentPageIndex + 1;
- if (nextIndex >= helpers.pages.length)
- nextIndex = 0;
- var to = helpers.pages[nextIndex];
- helpers.transition(from, to, function() {
- helpers.currentPage = to;
- helpers.currentPageIndex += 1;
- // Call the callback
- if (callback != null) callback();
- });
- });
- },
- previousPage: function(callback) {
- var from = helpers.currentPage;
- var nextIndex = helpers.currentPageIndex - 1;
- if (nextIndex <= helpers.pages.length)
- nextIndex = helpers.pages.length;
- var to = helpers.pages[nextIndex];
- helpers.transition(from, to, function() {
- helpers.currentPage = to;
- // Call the callback
- if (callback != null) callback();
- });
- }
- }
- var helpers = {
- pages: new Array(),
- defaultPage: '',
- currentPage: '',
- currentPageIndex: 1,
- animating: false,
- plugin: QM.fn.pages,
- transition: function(from, to, callback) {
- if (helpers.animating == false)
- helpers.animating = true;
- var s = this.plugin.settings.speed;
- var transition = this.plugin.settings.transition;
- var f = QM(from);
- var t = QM(to);
- console.log("[FROM]");
- console.log(from);
- console.log("[TO]");
- console.log(to);
- console.log("[SPEED] " + s);
- console.log("[TRANSITION] " + transition);
- switch(transition) {
- case 'fade':
- f.fadeOut(s, function() { t.fadeIn(s); helpers.animating = false; if (callback != null) callback(); });
- break;
- case 'crossfade':
- f.fadeOut(s);
- t.fadeIn(s, function() { helpers.animating = false; if (callback != null) callback(); });
- break;
- case 'slide':
- var fHeight = f.height();
- var tHeight = t.height();
- var containerHeight = QM(this).height();
- t.show();
- f.animate({'tp': (fHeight * -1)}, s, 'swing', function() { this.hide(); helpers.animating = false; if (callback != null) callback(); });
- t.css('tp', containerHeight);
- t.animate({'tp': 0}, s, 'swing');
- break;
- }
- },
- findTallest: function() {
- var tallestObj = null;
- var tallestInt = 0;
- for (var i = 0; i < helpers.pages.length; i++) {
- var pg = QM(helpers.pages[i]);
- if (pg.height() > tallestInt) {
- tallestInt = pg.height();
- tallestObj = pg;
- }
- }
- return tallestObj;
- }
- }
- if (methods[method]) {
- return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
- } else if (typeof method === 'object' || !method) {
- return methods.init.apply(this, arguments);
- } else {
- QM.error("Method '" + method + "' does not exist in pages plugin!");
- }
- }
- // Default plugin settings.
- QM.fn.pages.defaults = {
- transition: 'fade',
- speed: 500
- }
- QM.fn.pages.settings = {}
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement