Advertisement
Guest User

Modified Slide Panel Plugin

a guest
Nov 1st, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;(function ( $, window, document, undefined ) {
  2.    
  3.     var defaults = {
  4.         orientation: 'left',
  5.         mode: 'push',
  6.         static: false
  7.     };
  8.  
  9.     // The actual plugin constructor
  10.     function Slidepanel( $element, options ) {
  11.         this.$element = $element;
  12.         this.options = $.extend( {}, defaults, options) ;
  13.         this._defaults = defaults;
  14.         this.init();
  15.     }
  16.  
  17.     Slidepanel.prototype.init = function () {
  18.        
  19.         var base = this;
  20.  
  21.         if($('#slidepanel').length == 0){
  22.             var panel_html = '<div id="slidepanel" class="cb_slide_panel"><div class="wrapper"><a href="#" class="close">Close</a><div class="inner"><div class="wrapper"></div></div></div></div>';
  23.             $(panel_html).hide().appendTo($('body'));    
  24.         }
  25.  
  26.         this.$panel = $('#slidepanel');
  27.         this.$body = $('body');
  28.         this.$body_position = this.$body.css('position');
  29.  
  30.         //hide the panel and set orientation class for display
  31.         this.$panel.hide().addClass('panel_' + this.options.orientation);
  32.        
  33.         //set current trigger link to false for the current panel
  34.         this.$panel.data('slidepanel-current', false);
  35.         this.$panel.data('slidepanel-loaded', false);
  36.        
  37.  
  38.         //reset any defined a positions
  39.         this.$panel.css('left', '').css('right', '').css('top', '').css('bottom', '');
  40.  
  41.         //set a default top value for left and right orientations
  42.         //and set the starting position based on element width
  43.         if(this.options.orientation == 'left' || this.options.orientation == 'right') {
  44.             var options = {};
  45.             options['top'] = 0;
  46.             options[this.options.orientation] = -this.$panel.width();
  47.             this.$panel.css(options);
  48.         }
  49.  
  50.         //set a default left value for top and bottom orientations
  51.         //and set the starting position based on element height
  52.         if(this.options.orientation == 'top' || this.options.orientation == 'bottom') {
  53.             var options = {};
  54.             options['left'] = 0;
  55.             options[this.options.orientation] = -this.$panel.height();
  56.             this.$panel.css(options);
  57.         }
  58.  
  59.         //bind click event to trigger ajax load of html content
  60.         //and panel display to any elements that have the attribute rel="panel"
  61.         $(this.$element).on('click', function(e) {
  62.             e.preventDefault();
  63.              
  64.             //if the request mode is static
  65.             if(base.options.static) {
  66.                 //show the panel
  67.                 base.expand();
  68.             }
  69.             // if the reques mode is ajax
  70.             else {
  71.                 //load the external html
  72.                 base.load();
  73.             };
  74.         });
  75.  
  76.         //listen for a click on the close buttons for this panel
  77.         $('.close', this.$panel).click(function(e) {
  78.             e.preventDefault();
  79.             base.collapse();
  80.         });
  81.        
  82.     };
  83.  
  84.     Slidepanel.prototype.load = function() {
  85.             var base = this;
  86.             //if the current trigger element is the element that just triggered a load
  87.             if(this.$panel.data('slidepanel-current') == this.$element) {
  88.                 //collapse the current panel
  89.                 this.collapse();
  90.                 return;
  91.             } else {
  92.                 //show the slide panel
  93.                 this.expand();
  94.                 //get the target url
  95.                 var href = $(this.$element).attr('href');
  96.  
  97.                 //prevent an ajax request if the current URL is the the target URL
  98.                 if(this.$panel.data('slidepanel-loaded') !== href){
  99.                     //load the content from the target url, and update the panel html
  100.                     // $('.inner .wrapper', this.$panel).html('').load(href, function() {
  101.                     //     //remove the loading indicator
  102.                     //     base.$panel.removeClass('loading');
  103.                     //     //set the current loaded URL to the target URL
  104.                     //     base.$panel.data('slidepanel-loaded', href);
  105.                     // });
  106.                     $('.inner .wrapper', this.$panel).html($('#panel-content').html());
  107.                     base.$panel.removeClass('loading');
  108.                     base.$panel.data('slidepanel-loaded', href);
  109.                 //  the current URL is already loaded
  110.                 } else {
  111.                     //remove the loading indicator
  112.                     this.$panel.removeClass('loading');
  113.                 }
  114.             }
  115.             //set the current source element to this element that triggered the load
  116.             this.$panel.data('slidepanel-current', this.$element);
  117.     };
  118.  
  119.  
  120.     Slidepanel.prototype.expand = function() {
  121.         var base = this;
  122.                 //set the css properties to animatate
  123.  
  124.         var panel_options = {};
  125.         var body_options = {};
  126.         panel_options.visible = 'show';
  127.         panel_options[this.options.orientation] = 0;
  128.         body_options[this.options.orientation] = (this.options.orientation == 'top' || this.options.orientation == 'bottom') ? this.$panel.height() : this.$panel.width();
  129.        
  130.         //if the animation mode is set to push, we move the body in relation to the panel
  131.         //else the panel is overlayed on top of the body
  132.         if(this.options.mode == 'push'){
  133.             //animate the body position in relation to the panel dimensions
  134.             this.$body.css('position', 'absolute').animate(body_options, 250);
  135.         }
  136.  
  137.         //animate the panel into view
  138.         this.$panel.addClass('loading').animate(panel_options, 250, function() {
  139.             //show the panel's close button
  140.             $('.close', base.$panel).fadeIn(250);
  141.         });
  142.     };
  143.  
  144.     Slidepanel.prototype.collapse = function() {
  145.         //hide the close button for this panel
  146.         $('.close', this.$panel).hide();
  147.  
  148.         //set the css properties to animatate
  149.         var panel_options = {};
  150.         var body_options = {};
  151.         panel_options.visible = 'hide';
  152.         panel_options[this.options.orientation] = -(this.$panel.width() + 40);
  153.         body_options[this.options.orientation] = 0;
  154.        
  155.         //if the animation mode is push, move the document body back to it's original position
  156.         if(this.options.mode == 'push'){
  157.             this.$body.css('position', this.$body_position).animate(body_options, 250);
  158.         }
  159.         //animate the panel out of view
  160.         this.$panel.animate(panel_options, 250).data('slidepanel-current', false);
  161.     };
  162.  
  163.     $.fn['slidepanel'] = function ( options ) {
  164.         return this.each(function () {
  165.             if (!$.data(this, 'plugin_slidepanel')) {
  166.                 $.data(this, 'plugin_slidepanel', new Slidepanel( this, options ));
  167.             }
  168.         });
  169.     }
  170.  
  171. })(jQuery, window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement