2
0
forked from Wavyzz/dolibarr

Reorganize javascript at end of page.

This commit is contained in:
Laurent Destailleur
2017-11-09 01:00:43 +01:00
parent ead08c17d8
commit c8a551e7de
4 changed files with 232 additions and 180 deletions

View File

@@ -0,0 +1,142 @@
<?php
/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* or see http://www.gnu.org/
*/
/**
* \file htdocs/core/js/lib_foot.js.php
* \brief File that include javascript functions (included if option use_javascript activated)
*/
//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Not disabled cause need to do translations
if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK',1);
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1);
if (! defined('NOLOGIN')) define('NOLOGIN',1);
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU',1);
if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML',1);
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
session_cache_limiter(FALSE);
require_once '../../main.inc.php';
// Define javascript type
top_httphead('text/javascript; charset=UTF-8');
// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
if (empty($dolibarr_nocache)) header('Cache-Control: max-age=3600, public, must-revalidate');
else header('Cache-Control: no-cache');
//var_dump($conf);
print '/* Reposition management (does not work if a redirect is done after action of submission) */'."\n";
print 'jQuery(document).ready(function() {'."\n";
print '/* If page_y set, we set scollbar with it */'."\n";
print "page_y=getParameterByName('page_y', 0); if (page_y > 0) $('html, body').scrollTop(page_y);\n";
print '/* Set handler to add page_y param on some a href links */'."\n";
print 'jQuery(".reposition").click(function() {
var page_y = $(document).scrollTop();
this.href=this.href+\'&page_y=\'+page_y;
console.log("this.ref is now "+this.href)
});'."\n";
print '});'."\n";
// Wrapper to show tooltips (html or onclick popup)
if (empty($conf->dol_no_mouse_hover))
{
print "\n/* JS CODE TO ENABLE Tooltips on all object with class classfortooltip */\n";
print '
jQuery(document).ready(function () {
jQuery(".classfortooltip").tooltip({
show: { collision: "flipfit", effect:\'toggle\', delay:50 },
hide: { effect:\'toggle\', delay: 50 },
tooltipClass: "mytooltip",
content: function () {
return $(this).prop(\'title\'); /* To force to get title as is */
}
});
jQuery(".classfortooltiponclicktext").dialog({ closeOnEscape: true, classes: { "ui-dialog": "highlight" }, maxHeight: window.innerHeight-60, width: '.($conf->browser->layout == 'phone' ? 400 : 700).', autoOpen: false }).css("z-index: 5000");
jQuery(".classfortooltiponclick").click(function () {
console.log("We click on tooltip for element with dolid="+$(this).attr(\'dolid\'));
if ($(this).attr(\'dolid\'))
{
obj=$("#idfortooltiponclick_"+$(this).attr(\'dolid\')); /* obj is a div component */
obj.dialog("open");
}
});
});
' . "\n";
}
// Wrapper to manage dropdown
if (! defined('JS_JQUERY_DISABLE_DROPDOWN'))
{
print "\n/* JS CODE TO ENABLE dropdown */\n";
print '
jQuery(document).ready(function () {
$(".dropdown dt a").on(\'click\', function () {
//console.log($(this).parent().parent().find(\'dd ul\'));
$(this).parent().parent().find(\'dd ul\').slideToggle(\'fast\');
// Note: Did not find a way to get exact height (value is update at exit) so i calculate a generic from nb of lines
heigthofcontent = 21 * $(this).parent().parent().find(\'dd div ul li\').length;
if (heigthofcontent > 300) heigthofcontent = 300; // limited by max-height on css .dropdown dd ul
posbottom = $(this).parent().parent().find(\'dd\').offset().top + heigthofcontent + 8;
//console.log(posbottom);
var scrollBottom = $(window).scrollTop() + $(window).height();
//console.log(scrollBottom);
diffoutsidebottom = (posbottom - scrollBottom);
console.log("heigthofcontent="+heigthofcontent+", diffoutsidebottom (posbottom="+posbottom+" - scrollBottom="+scrollBottom+") = "+diffoutsidebottom);
if (diffoutsidebottom > 0)
{
pix = "-"+(diffoutsidebottom+8)+"px";
console.log("We reposition top by "+pix);
$(this).parent().parent().find(\'dd\').css("top", pix);
}
// $(".dropdown dd ul").slideToggle(\'fast\');
});
$(".dropdowncloseonclick").on(\'click\', function () {
console.log("Link has class dropdowncloseonclick, so we close/hide the popup ul");
$(this).parent().parent().hide();
});
$(document).bind(\'click\', function (e) {
var $clicked = $(e.target);
if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
});
});
';
}
// Wrapper to manage document_preview
if ($conf->browser->layout != 'phone')
{
print "\n/* JS CODE TO ENABLE document_preview */\n";
print '
jQuery(document).ready(function () {
jQuery(".documentpreview").click(function () {
console.log("We click on preview for element with href="+$(this).attr(\'href\')+" mime="+$(this).attr(\'mime\'));
document_preview($(this).attr(\'href\'), $(this).attr(\'mime\'), \''.dol_escape_js($langs->transnoentities("Preview")).'\');
return false;
});
});
console.log("end of js code to manage preview");
' . "\n";
}

View File

@@ -853,7 +853,7 @@ function document_preview(file, type, title)
if ($.inArray(type, ValidImageTypes) < 0) { if ($.inArray(type, ValidImageTypes) < 0) {
var width='85%'; var width='85%';
var object_width='100%'; var object_width='100%';
var height = $( window ).height()*0.90; var height = ($( window ).height() - 60) * 0.90;
var object_height='98%'; var object_height='98%';
show_preview('notimage'); show_preview('notimage');

View File

@@ -6304,112 +6304,101 @@ function printCommonFooter($zone='private')
if ($zone == 'private') print "\n".'<!-- Common footer for private page -->'."\n"; if ($zone == 'private') print "\n".'<!-- Common footer for private page -->'."\n";
else print "\n".'<!-- Common footer for public page -->'."\n"; else print "\n".'<!-- Common footer for public page -->'."\n";
if (! empty($conf->global->MAIN_HTML_FOOTER)) print $conf->global->MAIN_HTML_FOOTER."\n"; $parameters=array();
$reshook=$hookmanager->executeHooks('printCommonFooter',$parameters); // Note that $action and $object may have been modified by some hooks
print "\n"; if (empty($reshook))
if (! empty($conf->use_javascript_ajax))
{ {
print '<!-- Reposition management (does not work if a redirect is done after action of submission) -->'."\n"; if (! empty($conf->global->MAIN_HTML_FOOTER)) print $conf->global->MAIN_HTML_FOOTER."\n";
print '<script type="text/javascript" language="javascript">jQuery(document).ready(function() {'."\n";
print "\n";
print '<!-- If page_y set, we set scollbar with it -->'."\n"; if (! empty($conf->use_javascript_ajax))
print "page_y=getParameterByName('page_y', 0);";
print "if (page_y > 0) $('html, body').scrollTop(page_y);\n";
print '<!-- Set handler to add page_y param on some a href links -->'."\n";
print 'jQuery(".reposition").click(function() {
var page_y = $(document).scrollTop();
/*alert(page_y);*/
this.href=this.href+\'&page_y=\'+page_y;
});'."\n";
print '});'."\n";
if (empty($conf->dol_use_jmobile))
{ {
print '<!-- Set handler to switch left menu page (menuhider) -->'."\n"; print '<script type="text/javascript" language="javascript">'."\n";
print 'jQuery(".menuhider").click(function() {';
print ' console.log("We click on .menuhider");'."\n";
//print " $('.side-nav').animate({width:'toggle'},200);\n"; // OK with eldy theme but not with md
print " $('.side-nav').toggle()\n";
print " $('.login_block').toggle()\n";
print '});'."\n";
}
print '</script>'."\n"; if (empty($conf->dol_use_jmobile))
} {
print "\n";
// Google Analytics (need Google module) print '/* Set handler to switch left menu page (menuhider) */'."\n";
if (! empty($conf->google->enabled) && ! empty($conf->global->MAIN_GOOGLE_AN_ID)) print 'jQuery(".menuhider").click(function() {';
{ print ' console.log("We click on .menuhider");'."\n";
if (($conf->dol_use_jmobile != 4)) //print " $('.side-nav').animate({width:'toggle'},200);\n"; // OK with eldy theme but not with md
{ print " $('.side-nav').toggle()\n";
print "\n"; print " $('.login_block').toggle()\n";
print '<script type="text/javascript">'."\n"; print '});'."\n";
print ' var _gaq = _gaq || [];'."\n"; }
print ' _gaq.push([\'_setAccount\', \''.$conf->global->MAIN_GOOGLE_AN_ID.'\']);'."\n";
print ' _gaq.push([\'_trackPageview\']);'."\n"; // Google Analytics (need Google module)
print ''."\n"; if (! empty($conf->google->enabled) && ! empty($conf->global->MAIN_GOOGLE_AN_ID))
print ' (function() {'."\n"; {
print ' var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;'."\n"; if (($conf->dol_use_jmobile != 4))
print ' ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';'."\n"; {
print ' var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);'."\n"; print "\n";
print ' })();'."\n"; print "/* Set google analtics tag */\n";
print '</script>'."\n"; print ' var _gaq = _gaq || [];'."\n";
print ' _gaq.push([\'_setAccount\', \''.$conf->global->MAIN_GOOGLE_AN_ID.'\']);'."\n";
print ' _gaq.push([\'_trackPageview\']);'."\n";
print ''."\n";
print ' (function() {'."\n";
print ' var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;'."\n";
print ' ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';'."\n";
print ' var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);'."\n";
print ' })();'."\n";
}
}
// End of tuning
if (! empty($_SERVER['MAIN_SHOW_TUNING_INFO']) || ! empty($conf->global->MAIN_SHOW_TUNING_INFO))
{
print "\n";
print "/* Add memory info */\n";
print 'window.console && console.log("';
if (! empty($conf->global->MEMCACHED_SERVER)) print 'MEMCACHED_SERVER='.$conf->global->MEMCACHED_SERVER.' - ';
print 'MAIN_OPTIMIZE_SPEED='.(isset($conf->global->MAIN_OPTIMIZE_SPEED)?$conf->global->MAIN_OPTIMIZE_SPEED:'off');
if (! empty($micro_start_time)) // Works only if MAIN_SHOW_TUNING_INFO is defined at $_SERVER level. Not in global variable.
{
$micro_end_time = microtime(true);
print ' - Build time: '.ceil(1000*($micro_end_time-$micro_start_time)).' ms';
}
if (function_exists("memory_get_usage"))
{
print ' - Mem: '.memory_get_usage();
}
if (function_exists("xdebug_memory_usage"))
{
print ' - XDebug time: '.ceil(1000*xdebug_time_index()).' ms';
print ' - XDebug mem: '.xdebug_memory_usage();
print ' - XDebug mem peak: '.xdebug_peak_memory_usage();
}
if (function_exists("zend_loader_file_encoded"))
{
print ' - Zend encoded file: '.(zend_loader_file_encoded()?'yes':'no');
}
print '");'."\n";
}
print "\n".'</script>'."\n";
} }
}
// End of tuning
if (! empty($_SERVER['MAIN_SHOW_TUNING_INFO']) || ! empty($conf->global->MAIN_SHOW_TUNING_INFO))
{
print "\n".'<script type="text/javascript">'."\n";
print 'window.console && console.log("';
if (! empty($conf->global->MEMCACHED_SERVER)) print 'MEMCACHED_SERVER='.$conf->global->MEMCACHED_SERVER.' - ';
print 'MAIN_OPTIMIZE_SPEED='.(isset($conf->global->MAIN_OPTIMIZE_SPEED)?$conf->global->MAIN_OPTIMIZE_SPEED:'off');
if (! empty($micro_start_time)) // Works only if MAIN_SHOW_TUNING_INFO is defined at $_SERVER level. Not in global variable.
{
$micro_end_time = microtime(true);
print ' - Build time: '.ceil(1000*($micro_end_time-$micro_start_time)).' ms';
}
if (function_exists("memory_get_usage"))
{
print ' - Mem: '.memory_get_usage();
}
if (function_exists("xdebug_memory_usage"))
{
print ' - XDebug time: '.ceil(1000*xdebug_time_index()).' ms';
print ' - XDebug mem: '.xdebug_memory_usage();
print ' - XDebug mem peak: '.xdebug_peak_memory_usage();
}
if (function_exists("zend_loader_file_encoded"))
{
print ' - Zend encoded file: '.(zend_loader_file_encoded()?'yes':'no');
}
print '");'."\n";
print '</script>'."\n";
// Add Xdebug coverage of code // Add Xdebug coverage of code
if (defined('XDEBUGCOVERAGE')) if (defined('XDEBUGCOVERAGE'))
{ {
print_r(xdebug_get_code_coverage()); print_r(xdebug_get_code_coverage());
} }
}
// If there is some logs in buffer to show
// If there is some logs in buffer to show if (count($conf->logbuffer))
if (count($conf->logbuffer))
{
print "\n";
print "<!-- Start of log output\n";
//print '<div class="hidden">'."\n";
foreach($conf->logbuffer as $logline)
{ {
print $logline."<br>\n"; print "\n";
print "<!-- Start of log output\n";
//print '<div class="hidden">'."\n";
foreach($conf->logbuffer as $logline)
{
print $logline."<br>\n";
}
//print '</div>'."\n";
print "End of log output -->\n";
} }
//print '</div>'."\n";
print "End of log output -->\n";
} }
$parameters=array();
$reshook=$hookmanager->executeHooks('printCommonFooter',$parameters); // Note that $action and $object may have been modified by some hooks
} }
/** /**

View File

@@ -1928,101 +1928,23 @@ if (! function_exists("llxFooter"))
print '</div> <!-- End div class="fiche" -->'."\n"; // End div fiche print '</div> <!-- End div class="fiche" -->'."\n"; // End div fiche
if (empty($conf->dol_hide_leftmenu)) print '</div> <!-- End div id-right -->'; // End div id-right if (empty($conf->dol_hide_leftmenu)) print '</div> <!-- End div id-right -->'."\n"; // End div id-right
if (empty($conf->dol_hide_leftmenu) && empty($conf->dol_use_jmobile)) print '</div> <!-- End div id-container -->'."\n"; // End div container
print "\n"; print "\n";
if ($comment) print '<!-- '.$comment.' -->'."\n"; if ($comment) print '<!-- '.$comment.' -->'."\n";
printCommonFooter($zone); printCommonFooter($zone);
if (empty($conf->dol_hide_leftmenu) && empty($conf->dol_use_jmobile)) print '</div> <!-- End div id-container -->'."\n"; // End div container
if (! empty($delayedhtmlcontent)) print $delayedhtmlcontent; if (! empty($delayedhtmlcontent)) print $delayedhtmlcontent;
// TODO Move this in lib_head.js.php if (! empty($conf->use_javascript_ajax))
// Wrapper to show tooltips (html or onclick popup)
if (! empty($conf->use_javascript_ajax) && empty($conf->dol_no_mouse_hover))
{ {
print "\n<!-- JS CODE TO ENABLE Tooltips on all object with class classfortooltip -->\n"; print "\n".'<!-- Includes JS Footer of Dolibarr -->'."\n";
print '<script type="text/javascript"> print '<script type="text/javascript" src="/dolibarr/htdocs/core/js/lib_foot.js.php?lang=fr_FR&amp;version=7.0.0-alpha" async></script>'."\n";
jQuery(document).ready(function () {
jQuery(".classfortooltip").tooltip({
show: { collision: "flipfit", effect:\'toggle\', delay:50 },
hide: { effect:\'toggle\', delay: 50 },
tooltipClass: "mytooltip",
content: function () {
return $(this).prop(\'title\'); /* To force to get title as is */
}
});
jQuery(".classfortooltiponclicktext").dialog({ closeOnEscape: true, classes: { "ui-dialog": "highlight" }, maxHeight: window.innerHeight-60, width: '.($conf->browser->layout == 'phone' ? 400 : 700).', autoOpen: false }).css("z-index: 5000");
jQuery(".classfortooltiponclick").click(function () {
console.log("We click on tooltip for element with dolid="+$(this).attr(\'dolid\'));
if ($(this).attr(\'dolid\'))
{
obj=$("#idfortooltiponclick_"+$(this).attr(\'dolid\')); /* obj is a div component */
obj.dialog("open");
}
});
});
</script>' . "\n";
} }
// Wrapper to manage document_preview
if (! empty($conf->use_javascript_ajax) && ($conf->browser->layout != 'phone'))
{
print "\n<!-- JS CODE TO ENABLE document_preview -->\n";
print '<script type="text/javascript">
jQuery(document).ready(function () {
jQuery(".documentpreview").click(function () {
console.log("We click on preview for element with href="+$(this).attr(\'href\')+" mime="+$(this).attr(\'mime\'));
document_preview($(this).attr(\'href\'), $(this).attr(\'mime\'), \''.dol_escape_js($langs->transnoentities("Preview")).'\');
return false;
});
});
</script>' . "\n";
}
// Wrapper to manage dropdown
if (! empty($conf->use_javascript_ajax) && ! defined('JS_JQUERY_DISABLE_DROPDOWN'))
{
print "\n<!-- JS CODE TO ENABLE dropdown -->\n";
print '<script type="text/javascript">
jQuery(document).ready(function () {
$(".dropdown dt a").on(\'click\', function () {
//console.log($(this).parent().parent().find(\'dd ul\'));
$(this).parent().parent().find(\'dd ul\').slideToggle(\'fast\');
// Note: Did not find a way to get exact height (value is update at exit) so i calculate a generic from nb of lines
heigthofcontent = 21 * $(this).parent().parent().find(\'dd div ul li\').length;
if (heigthofcontent > 300) heigthofcontent = 300; // limited by max-height on css .dropdown dd ul
posbottom = $(this).parent().parent().find(\'dd\').offset().top + heigthofcontent + 8;
//console.log(posbottom);
var scrollBottom = $(window).scrollTop() + $(window).height();
//console.log(scrollBottom);
diffoutsidebottom = (posbottom - scrollBottom);
console.log("heigthofcontent="+heigthofcontent+", diffoutsidebottom (posbottom="+posbottom+" - scrollBottom="+scrollBottom+") = "+diffoutsidebottom);
if (diffoutsidebottom > 0)
{
pix = "-"+(diffoutsidebottom+8)+"px";
console.log("We reposition top by "+pix);
$(this).parent().parent().find(\'dd\').css("top", pix);
}
// $(".dropdown dd ul").slideToggle(\'fast\');
});
$(".dropdowncloseonclick").on(\'click\', function () {
console.log("Link has class dropdowncloseonclick, so we close/hide the popup ul");
$(this).parent().parent().hide();
});
$(document).bind(\'click\', function (e) {
var $clicked = $(e.target);
if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
});
});
</script>';
}
// Wrapper to add log when clicking on download or preview // Wrapper to add log when clicking on download or preview
if (! empty($conf->blockedlog->enabled) && is_object($object) && $object->id > 0 && $object->statut > 0) if (! empty($conf->blockedlog->enabled) && is_object($object) && $object->id > 0 && $object->statut > 0)
{ {
@@ -2056,7 +1978,6 @@ if (! function_exists("llxFooter"))
} }
} }
// A div for the address popup // A div for the address popup
print "\n<!-- A div to allow dialog popup -->\n"; print "\n<!-- A div to allow dialog popup -->\n";
print '<div id="dialogforpopup" style="display: none;"></div>'."\n"; print '<div id="dialogforpopup" style="display: none;"></div>'."\n";