lib_foot.js.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/js/lib_foot.js.php
  20. * \brief File that include javascript functions (included if option use_javascript activated)
  21. */
  22. if (!defined('NOREQUIRESOC')) {
  23. define('NOREQUIRESOC', '1');
  24. }
  25. if (!defined('NOCSRFCHECK')) {
  26. define('NOCSRFCHECK', 1);
  27. }
  28. if (!defined('NOTOKENRENEWAL')) {
  29. define('NOTOKENRENEWAL', 1);
  30. }
  31. if (!defined('NOLOGIN')) {
  32. define('NOLOGIN', 1);
  33. }
  34. if (!defined('NOREQUIREMENU')) {
  35. define('NOREQUIREMENU', 1);
  36. }
  37. if (!defined('NOREQUIREHTML')) {
  38. define('NOREQUIREHTML', 1);
  39. }
  40. if (!defined('NOREQUIREAJAX')) {
  41. define('NOREQUIREAJAX', '1');
  42. }
  43. session_cache_limiter('public');
  44. require_once '../../main.inc.php';
  45. /*
  46. * View
  47. */
  48. // Define javascript type
  49. top_httphead('text/javascript; charset=UTF-8');
  50. // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
  51. if (empty($dolibarr_nocache)) {
  52. header('Cache-Control: max-age=10800, public, must-revalidate');
  53. } else {
  54. header('Cache-Control: no-cache');
  55. }
  56. //var_dump($conf);
  57. // Wrapper to show tooltips (html or onclick popup)
  58. print "\n/* JS CODE TO ENABLE Tooltips on all object with class classfortooltip */\n";
  59. print "jQuery(document).ready(function () {\n";
  60. if (empty($conf->dol_no_mouse_hover)) {
  61. print 'jQuery(".classfortooltip").tooltip({
  62. show: { collision: "flipfit", effect:"toggle", delay:50, duration: 20 },
  63. hide: { delay: 250, duration: 20 },
  64. tooltipClass: "mytooltip",
  65. content: function () {
  66. console.log("Return title for popup");
  67. return $(this).prop("title"); /* To force to get title as is */
  68. }
  69. });'."\n";
  70. }
  71. print '
  72. jQuery(".classfortooltiponclicktext").dialog({
  73. closeOnEscape: true, classes: { "ui-dialog": "highlight" },
  74. maxHeight: window.innerHeight-60, width: '.($conf->browser->layout == 'phone' ? max($_SESSION['dol_screenwidth'] - 20, 320) : 700).',
  75. modal: true,
  76. autoOpen: false
  77. }).css("z-index: 5000");
  78. jQuery(".classfortooltiponclick").click(function () {
  79. console.log("We click on tooltip for element with dolid="+$(this).attr(\'dolid\'));
  80. if ($(this).attr(\'dolid\')) {
  81. obj=$("#idfortooltiponclick_"+$(this).attr(\'dolid\')); /* obj is a div component */
  82. obj.dialog("open");
  83. return false;
  84. }
  85. });'."\n";
  86. print "});\n";
  87. // Wrapper to manage dropdown
  88. if (!defined('JS_JQUERY_DISABLE_DROPDOWN')) {
  89. print "\n/* JS CODE TO ENABLE dropdown (hamburger, linkto, ...) */\n";
  90. print ' jQuery(document).ready(function () {
  91. var lastopendropdown = null;
  92. // Click onto the link "link to" or "hamburger", toggle dropdown
  93. $(document).on(\'click\', \'.dropdown dt a\', function () {
  94. console.log("toggle dropdown dt a");
  95. //$(this).parent().parent().find(\'dd ul\').slideToggle(\'fast\');
  96. $(this).parent().parent().find(\'dd ul\').toggleClass("open");
  97. if ($(this).parent().parent().find(\'dd ul\').hasClass("open")) {
  98. lastopendropdown = $(this).parent().parent().find(\'dd ul\');
  99. //console.log(lastopendropdown);
  100. } else {
  101. // We closed the dropdown for hamburger selectfields
  102. if ($("input:hidden[name=formfilteraction]").val() == "listafterchangingselectedfields") {
  103. console.log("resubmit the form saved into lastopendropdown after clicking on hamburger");
  104. //$(".dropdown dt a").parents(\'form:first\').submit();
  105. //$(".dropdown dt a").closest("form").submit();
  106. lastopendropdown.closest("form").submit();
  107. }
  108. }
  109. // Note: Did not find a way to get exact height (value is update at exit) so i calculate a generic from nb of lines
  110. heigthofcontent = 21 * $(this).parent().parent().find(\'dd div ul li\').length;
  111. if (heigthofcontent > 300) heigthofcontent = 300; // limited by max-height on css .dropdown dd ul
  112. posbottom = $(this).parent().parent().find(\'dd\').offset().top + heigthofcontent + 8;
  113. var scrollBottom = $(window).scrollTop() + $(window).height();
  114. diffoutsidebottom = (posbottom - scrollBottom);
  115. console.log("heigthofcontent="+heigthofcontent+", diffoutsidebottom (posbottom="+posbottom+" - scrollBottom="+scrollBottom+") = "+diffoutsidebottom);
  116. if (diffoutsidebottom > 0)
  117. {
  118. pix = "-"+(diffoutsidebottom+8)+"px";
  119. console.log("We reposition top by "+pix);
  120. $(this).parent().parent().find(\'dd\').css("top", pix);
  121. }
  122. });
  123. // Click on a link into the popup "link to" or other dropdown that ask to close drop down on element click, so close dropdown
  124. $(".dropdowncloseonclick").on(\'click\', function () {
  125. console.log("Link has class dropdowncloseonclick, so we close/hide the popup ul");
  126. //$(this).parent().parent().hide(); // $(this).parent().parent() is ul
  127. $(this).parent().parent().removeClass("open"); // $(this).parent().parent() is ul
  128. });
  129. // Click outside of any dropdown
  130. $(document).bind(\'click\', function (e) {
  131. var $clicked = $(e.target); // This is element we click on
  132. if (!$clicked.parents().hasClass("dropdown")) {
  133. //console.log("close dropdown dd ul - we click outside");
  134. //$(".dropdown dd ul").hide();
  135. $(".dropdown dd ul").removeClass("open");
  136. if ($("input:hidden[name=formfilteraction]").val() == "listafterchangingselectedfields") {
  137. console.log("resubmit form saved into lastopendropdown after clicking outside of dropdown and having change selectlist from selectlist field of hamburger dropdown");
  138. //$(".dropdown dt a").parents(\'form:first\').submit();
  139. //$(".dropdown dt a").closest("form").submit();
  140. lastopendropdown.closest("form").submit();
  141. }
  142. }
  143. });
  144. });
  145. ';
  146. }
  147. // Wrapper to manage document_preview
  148. if ($conf->browser->layout != 'phone') {
  149. print "\n/* JS CODE TO ENABLE document_preview */\n"; // Function document_preview is into header
  150. print ' jQuery(document).ready(function () {
  151. jQuery(".documentpreview").click(function () {
  152. console.log("We click on preview for element with href="+$(this).attr(\'href\')+" mime="+$(this).attr(\'mime\'));
  153. document_preview($(this).attr(\'href\'), $(this).attr(\'mime\'), \''.dol_escape_js($langs->transnoentities("Preview")).'\');
  154. return false;
  155. });
  156. });'."\n";
  157. }
  158. // Code to manage reposition
  159. print "\n/* JS CODE TO ENABLE reposition management (does not work if a redirect is done after action of submission) */\n";
  160. print '
  161. jQuery(document).ready(function() {
  162. /* If page_y set, we set scollbar with it */
  163. page_y=getParameterByName(\'page_y\', 0); /* search in GET parameter */
  164. if (page_y == 0) page_y = jQuery("#page_y").text(); /* search in POST parameter that is filed at bottom of page */
  165. if (page_y > 0)
  166. {
  167. console.log("page_y found is "+page_y);
  168. $(\'html, body\').scrollTop(page_y);
  169. }
  170. /* Set handler to add page_y param on output (click on href links or submit button) */
  171. jQuery(".reposition").click(function() {
  172. var page_y = $(document).scrollTop();
  173. if (page_y > 0)
  174. {
  175. if (this.href)
  176. {
  177. console.log("We click on tag with .reposition class. this.ref was "+this.href);
  178. var hrefarray = this.href.split("#", 2);
  179. hrefarray[0]=hrefarray[0].replace(/&page_y=(\d+)/, \'\'); /* remove page_y param if already present */
  180. this.href=hrefarray[0]+\'&page_y=\'+page_y;
  181. console.log("We click on tag with .reposition class. this.ref is now "+this.href);
  182. }
  183. else
  184. {
  185. console.log("We click on tag with .reposition class but element is not an <a> html tag, so we try to update input form field with name=page_y with value "+page_y);
  186. jQuery("input[type=hidden][name=page_y]").val(page_y);
  187. }
  188. }
  189. });
  190. });'."\n";
  191. // Code to manage Copy To Clipboard click
  192. print "\n/* JS CODE TO ENABLE ClipBoard copy paste */\n";
  193. print '
  194. jQuery(document).ready(function() {
  195. jQuery(\'.clipboardCPShowOnHover\').hover(
  196. function() {
  197. console.log("We hover a value with a copy paste feature");
  198. $(this).children(".clipboardCPButton, .clipboardCPText").show();
  199. },
  200. function() {
  201. console.log("We hover out the value with a copy paste feature");
  202. $(this).children(".clipboardCPButton, .clipboardCPText").hide();
  203. }
  204. );
  205. jQuery(\'.clipboardCPValue, .clipboardCPButton, .clipboardCPValueToPrint\').click(function() {
  206. console.log("We click on a clipboardCPButton or clipboardCPValueToPrint class and we want to copy content of clipboardCPValue class");
  207. if (window.getSelection) {
  208. jqobj=$(this).parent().children(".clipboardCPValue");
  209. console.log(jqobj.html());
  210. selection = window.getSelection(); /* get the object used for selection */
  211. selection.removeAllRanges(); /* clear current selection */
  212. /* We select the value to print using the parentNode.firstChild */
  213. /* We should use the class clipboardCPValue but it may have several element with copy/paste so class to select is not enough */
  214. range = document.createRange();
  215. range.selectNodeContents(this.parentNode.firstChild);
  216. selection.addRange(range); /* make the new selection with the value to copy */
  217. /* copy selection into clipboard */
  218. var succeed;
  219. try {
  220. console.log("We set the style display to unset for the span so the copy will work");
  221. jqobj.css("display", "unset"); /* Because copy does not work on "block" object */
  222. succeed = document.execCommand(\'copy\');
  223. console.log("We set the style display back to inline-block");
  224. jqobj.css("display", "inline-block");
  225. } catch(e) {
  226. succeed = false;
  227. }
  228. /* Remove the selection to avoid to see the hidden field to copy selected */
  229. window.getSelection().removeAllRanges();
  230. }
  231. /* Show message */
  232. /* TODO Show message into a top left corner or center of screen */
  233. var lastchild = this.parentNode.lastChild; /* .parentNode is clipboardCP and last child is clipboardCPText */
  234. var tmp = lastchild.innerHTML
  235. if (succeed) {
  236. lastchild.innerHTML = \'<div class="clipboardCPTextDivInside opacitymedium">'.dol_escape_js($langs->trans('CopiedToClipboard')).'</div>\';
  237. } else {
  238. lastchild.innerHTML = \'<div class="clipboardCPTextDivInside opacitymedium">'.dol_escape_js($langs->trans('Error')).'</div>\';
  239. }
  240. setTimeout(() => { lastchild.innerHTML = tmp; }, 1000);
  241. });
  242. });'."\n";
  243. // Code to manage clicktodial
  244. print "\n/* JS CODE TO ENABLE clicktodial call of an URL */\n";
  245. print '
  246. jQuery(document).ready(function() {
  247. jQuery(".cssforclicktodial").click(function() {
  248. event.preventDefault();
  249. console.log("We click on a cssforclicktodial class with url="+this.href);
  250. $.ajax({
  251. url: this.href,
  252. type: \'GET\',
  253. data: { token: \''.newToken().'\' }
  254. }).done(function(xhr, textStatus, errorThrown) {
  255. /* do nothing */
  256. }).fail(function(xhr, textStatus, errorThrown) {
  257. alert("Error: "+textStatus);
  258. });
  259. return false;
  260. });
  261. });'."\n";
  262. // Code to manage the confirm dialog box
  263. print "\n/* JS CODE TO ENABLE DIALOG CONFIRM POPUP ON ACTION BUTTON */\n";
  264. print '
  265. jQuery(document).ready(function() {
  266. $(document).on("click", \'.butActionConfirm\', function(event) {
  267. event.preventDefault();
  268. // I don\'t use jquery $(this).data(\'confirm-url\'); to get $(this).attr(\'data-confirm-url\'); because .data() can doesn\'t work with ajax
  269. var confirmUrl = $(this).attr(\'data-confirm-url\');
  270. var confirmTitle = $(this).attr(\'data-confirm-title\');
  271. var confirmContent = $(this).attr(\'data-confirm-content\');
  272. var confirmActionBtnLabel = $(this).attr(\'data-confirm-action-btn-label\');
  273. var confirmCancelBtnLabel = $(this).attr(\'data-confirm-cancel-btn-label\');
  274. var confirmModal = $(this).attr(\'data-confirm-modal\');
  275. if(confirmModal == undefined){ confirmModal = false; }
  276. var confirmId = \'confirm-dialog-box\';
  277. if($(this).attr(\'id\') != undefined){ var confirmId = confirmId + "-" + $(this).attr(\'id\'); }
  278. if($("#" + confirmId) != undefined) { $(\'#\' + confirmId).remove(); }
  279. // Create modal box
  280. var $confirmBox = $(\'<div/>\', {
  281. id: confirmId,
  282. title: confirmTitle
  283. }).appendTo(\'body\');
  284. $confirmBox.dialog({
  285. autoOpen: true,
  286. modal: confirmModal,
  287. //width: Math.min($( window ).width() - 50, 1700),
  288. width: \'auto\',
  289. dialogClass: \'confirm-dialog-box\',
  290. buttons: [
  291. {
  292. text: confirmActionBtnLabel,
  293. "class": \'ui-state-information\',
  294. click: function () {
  295. window.location.replace(confirmUrl);
  296. }
  297. },
  298. {
  299. text: confirmCancelBtnLabel,
  300. "class": \'ui-state-information\',
  301. click: function () {
  302. $(this).dialog("close");
  303. }
  304. }
  305. ],
  306. close: function( event, ui ) {
  307. $(\'#\'+confirmBox).remove();
  308. },
  309. open: function( event, ui ) {
  310. $confirmBox.html(confirmContent);
  311. }
  312. });
  313. });
  314. });
  315. '."\n";