| Current Path : /home/juvelize/martine/plugins/system/jqsmoothtop/ |
| Current File : /home/juvelize/martine/plugins/system/jqsmoothtop/jqsmoothtop.php |
<?php
/**
* @version $Id: jqsmoothtop.php 2013-7-4 20:41:25Z eschneider $
* @package plg_sys_jqsmoothtop
* @version 1.0.0
* @copyright Copyright (C) 2013 Eric Schneider. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
class plgSystemjQSmoothTop extends JPlugin {
function onBeforeRender() {
// do we run in administrator?
$app = JFactory::getApplication();
if($app->isAdmin() && $this->params->get('runInAdmin',0)==0) return true;
$doc = JFactory::getDocument();
// we don't run in pages that aren't html
if($doc->getType() != 'html') return true;
// we don't run in modal pages or other incomplete pages
$nogo = array('component','raw');
if(in_array(JRequest::getString('tmpl'),$nogo)) return true;
// get our parameters from Joomla!
$params = json_decode ($this->params, true);
// figure out a couple of positions
$units = is_numeric ($params['horizontalPosition']) ? 'px' : '';
$params['horizontalLocation'] = $params['horizontalAlign'] == 'center' ? 'left : (jQuery(window).innerWidth() - jQSmoothTop.outerWidth()) / 2' : $params['horizontalAlign'] . " : " . $params['horizontalPosition'];
if ($params['horizontalAlign'] == 'center') {
$params['horizontalLocation'] =
<<<CENTERME
function centerMe(element) {
element.css({ left : (jQuery(window).innerWidth() - jQSmoothTop.outerWidth()) / 2 })
};
centerMe(jQSmoothTop);
jQuery(window).resize(function() { centerMe(jQSmoothTop) });
CENTERME;
} else {
$params['horizontalLocation'] = 'jQSmoothTop.css({ ' . $params['horizontalAlign'] . ' : ' . $params['horizontalPosition'] . '})';
}
$params['hangPosition'] = $params['showPosition'] + $params['hangDistance'];
$start_at_top = $this->params->get('topAlways') ? "\t// start at the top\n\tjQuery('body,html').scrollTop(0);\n" : "";
// the following section presents a paradox: do we format for readability here or a readable output?
$script =
<<<SCRIPT
jQuery(document).ready(function(){
$start_at_top
// make our jQSmoothTop tag
jQuery('body').append('<div id="jQSmoothTop" class="jQSmoothTop" style="position:fixed; display:block; visibility:visible; zoom:1; opacity:1; cursor:pointer; $params[verticalAlign]: $params[showPosition]px; z-index:1040;"><a href="#top" title="$params[linkTitle]"><span></span>$params[linkText]</a></div>');
// and assign it to a handy variable
var jQSmoothTop = jQuery('#jQSmoothTop');
// figure out the hide position
hidePosition = -$params[showPosition] - jQSmoothTop.outerHeight();
// do we need to align left, right or center (done partially in php)
$params[horizontalLocation];
// hide #back-top first
jQSmoothTop.css({ opacity : 0, $params[verticalAlign] : hidePosition });
// fade in #jQSmoothTop if we need to
jQuery(window).scroll(function () {
if (jQuery(this).scrollTop() >= $params[fadePosition] && jQSmoothTop.css('$params[verticalAlign]') == hidePosition + 'px' && !jQuery('body,html').is(':animated')) {
// show our tag
jQSmoothTop
.stop(true, true).animate(
{ $params[verticalAlign] : $params[hangPosition], opacity : $params[fadeMouseLeave], display : 'block' },
{ duration : $params[fadeSpeed], specialEasing: { $params[verticalAlign] : 'linear', opacity : 'easeInCubic' }}
)
.animate({ $params[verticalAlign] : $params[showPosition] }, $params[hangSpeed], 'linear');
} else if (jQuery(this).scrollTop() < $params[fadePosition] && jQSmoothTop.css('$params[verticalAlign]') == $params[showPosition] + 'px' && !jQuery('body,html').is(':animated')) {
// hide our tag
jQSmoothTop
.animate({ $params[verticalAlign] : $params[hangPosition] }, $params[hangSpeed], 'linear')
.animate(
{ $params[verticalAlign] : hidePosition, opacity : 0, display : 'none' },
{ duration : $params[fadeSpeed], specialEasing: { $params[verticalAlign] : 'linear', opacity : 'easeOutCubic' }, complete : function(){jQSmoothTop.stop(true, true)}}
);
}
});
// add some events
jQuery('#jQSmoothTop a')
// add a nifty hover effect
.mouseenter(function () {
if (jQSmoothTop.css('$params[verticalAlign]') == $params[showPosition] + 'px' && jQSmoothTop.css('opacity') == $params[fadeMouseLeave]){
jQSmoothTop.animate({ opacity : $params[fadeMouseEnter] }, 200, 'linear')};
})
.mouseleave(function () {
if (jQSmoothTop.css('$params[verticalAlign]') == $params[showPosition] + 'px' && jQSmoothTop.css('opacity') == $params[fadeMouseEnter]){
jQSmoothTop.animate({ opacity : $params[fadeMouseLeave] }, 200, 'linear')};
})
// scroll body to $params[scrollTo]px on click
.on('click', function (event) {
event.preventDefault(); // <---- This is the magic stuff
jQuery('#jQSmoothTop a').blur();
jQuery('body,html').animate({ scrollTop : $params[scrollTo] }, $params[scrollSpeed], $params[scrollEffect]);
});
});
SCRIPT;
// if DS (Directory Separator) is no defined, fix it!
!defined('DS') && define('DS', DIRECTORY_SEPARATOR);
// should we load jQuery?
if ($this->params->get('loadjQuery')) {
// use version appropriate means to load local copy of jQuery
if ($this->params->get('loadjQuery')==1 && version_compare(JVERSION,'3.0.0', '<') && !$app->get('jquery')) {
$doc->addScript(JURI::root( true ).DS.'plugins'.DS.'system'.DS.'jqsmoothtop'.DS.'js'.DS.'jquery-1.10.2.min.js', 'text/javascript');
JFactory::getApplication()->set('jquery', true);
// use jQuery noConflict?
if ($this->params->get('noConflict')==1) {
$doc->addScript(JURI::root( true ).DS.'plugins'.DS.'system'.DS.'jqsmoothtop'.DS.'js'.DS.'jquery.noconflict.js', 'text/javascript');
}
} else if ($this->params->get('loadjQuery')==2 && version_compare(JVERSION,'3.0.0', '<') && !$app->get('jquery')) {
// load jQuery from jQuery's CDN
$doc->addScript('http://code.jquery.com/jquery-1.10.2.min.js', 'text/javascript');
JFactory::getApplication()->set('jquery', true);
// use jQuery noConflict?
if ($this->params->get('noConflict')==1) {
$doc->addScript(JURI::root( true ).DS.'plugins'.DS.'system'.DS.'jqsmoothtop'.DS.'js'.DS.'jquery.noconflict.js', 'text/javascript');
}
} else {
JHtml::_('jquery.framework');
}
}
// load easing for transition effects beyond 'linear' and 'swing'
if ($this->params->get('loadjQueryEasing')==1) {
// load local copy of jQuery Easing
$doc->addScript(JURI::root( true ).DS.'plugins'.DS.'system'.DS.'jqsmoothtop'.DS.'js'.DS.'jquery.easing.min.js', "text/javascript");
} else if ($this->params->get('loadjQueryEasing')==2) {
// load jQuery Easing from CloudFlare's CDN
$doc->addScript("http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js", "text/javascript");
}
$doc->addScriptDeclaration ($script, 'text/javascript');
if($this->params->get('useCustomCSS',1)==1) $doc->addStyleDeclaration($this->params->get('customCSS'));
return true;
}
}