lib_foot.js.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. $(".ulselectedfields").removeClass("open");
  97. $(this).parent().parent().find(\'dd ul\').toggleClass("open");
  98. if ($(this).parent().parent().find(\'dd ul\').hasClass("open")) {
  99. lastopendropdown = $(this).parent().parent().find(\'dd ul\');
  100. //console.log(lastopendropdown);
  101. } else {
  102. // We closed the dropdown for hamburger selectfields
  103. if ($("input:hidden[name=formfilteraction]").val() == "listafterchangingselectedfields") {
  104. console.log("resubmit the form saved into lastopendropdown after clicking on hamburger");
  105. //$(".dropdown dt a").parents(\'form:first\').submit();
  106. //$(".dropdown dt a").closest("form").submit();
  107. lastopendropdown.closest("form").submit();
  108. }
  109. }
  110. // Note: Did not find a way to get exact height (value is update at exit) so i calculate a generic from nb of lines
  111. heigthofcontent = 21 * $(this).parent().parent().find(\'dd div ul li\').length;
  112. if (heigthofcontent > 300) heigthofcontent = 300; // limited by max-height on css .dropdown dd ul
  113. posbottom = $(this).parent().parent().find(\'dd\').offset().top + heigthofcontent + 8;
  114. var scrollBottom = $(window).scrollTop() + $(window).height();
  115. diffoutsidebottom = (posbottom - scrollBottom);
  116. console.log("heigthofcontent="+heigthofcontent+", diffoutsidebottom (posbottom="+posbottom+" - scrollBottom="+scrollBottom+") = "+diffoutsidebottom);
  117. if (diffoutsidebottom > 0)
  118. {
  119. pix = "-"+(diffoutsidebottom+8)+"px";
  120. console.log("We reposition top by "+pix);
  121. $(this).parent().parent().find(\'dd\').css("top", pix);
  122. }
  123. });
  124. // Click on a link into the popup "link to" or other dropdown that ask to close drop down on element click, so close dropdown
  125. $(".dropdowncloseonclick").on(\'click\', function () {
  126. console.log("Link has class dropdowncloseonclick, so we close/hide the popup ul");
  127. //$(this).parent().parent().hide(); // $(this).parent().parent() is ul
  128. $(this).parent().parent().removeClass("open"); // $(this).parent().parent() is ul
  129. });
  130. // Click outside of any dropdown
  131. $(document).bind(\'click\', function (e) {
  132. var $clicked = $(e.target); // This is element we click on
  133. if (!$clicked.parents().hasClass("dropdown")) {
  134. //console.log("close dropdown dd ul - we click outside");
  135. //$(".dropdown dd ul").hide();
  136. $(".dropdown dd ul").removeClass("open");
  137. if ($("input:hidden[name=formfilteraction]").val() == "listafterchangingselectedfields") {
  138. console.log("resubmit form saved into lastopendropdown after clicking outside of dropdown and having change selectlist from selectlist field of hamburger dropdown");
  139. //$(".dropdown dt a").parents(\'form:first\').submit();
  140. //$(".dropdown dt a").closest("form").submit();
  141. lastopendropdown.closest("form").submit();
  142. }
  143. }
  144. });
  145. });
  146. ';
  147. }
  148. // Wrapper to manage document_preview
  149. if ($conf->browser->layout != 'phone') {
  150. print "\n/* JS CODE TO ENABLE document_preview */\n"; // Function document_preview is into header
  151. print ' jQuery(document).ready(function () {
  152. jQuery(".documentpreview").click(function () {
  153. console.log("We click on preview for element with href="+$(this).attr(\'href\')+" mime="+$(this).attr(\'mime\'));
  154. document_preview($(this).attr(\'href\'), $(this).attr(\'mime\'), \''.dol_escape_js($langs->transnoentities("Preview")).'\');
  155. return false;
  156. });
  157. });'."\n";
  158. }
  159. // Code to manage reposition
  160. print "\n/* JS CODE TO ENABLE reposition management (does not work if a redirect is done after action of submission) */\n";
  161. print '
  162. jQuery(document).ready(function() {
  163. /* If page_y set, we set scollbar with it */
  164. page_y=getParameterByName(\'page_y\', 0); /* search in GET parameter */
  165. if (page_y == 0) page_y = jQuery("#page_y").text(); /* search in POST parameter that is filed at bottom of page */
  166. if (page_y > 0)
  167. {
  168. console.log("page_y found is "+page_y);
  169. $(\'html, body\').scrollTop(page_y);
  170. }
  171. /* Set handler to add page_y param on output (click on href links or submit button) */
  172. jQuery(".reposition").click(function() {
  173. var page_y = $(document).scrollTop();
  174. if (page_y > 0)
  175. {
  176. if (this.href)
  177. {
  178. console.log("We click on tag with .reposition class. this.ref was "+this.href);
  179. var hrefarray = this.href.split("#", 2);
  180. hrefarray[0]=hrefarray[0].replace(/&page_y=(\d+)/, \'\'); /* remove page_y param if already present */
  181. this.href=hrefarray[0]+\'&page_y=\'+page_y;
  182. console.log("We click on tag with .reposition class. this.ref is now "+this.href);
  183. }
  184. else
  185. {
  186. 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);
  187. jQuery("input[type=hidden][name=page_y]").val(page_y);
  188. }
  189. }
  190. });
  191. });'."\n";
  192. // Code to manage Copy To Clipboard click
  193. print "\n/* JS CODE TO ENABLE ClipBoard copy paste */\n";
  194. print '
  195. jQuery(document).ready(function() {
  196. jQuery(\'.clipboardCPShowOnHover\').hover(
  197. function() {
  198. console.log("We hover a value with a copy paste feature");
  199. $(this).children(".clipboardCPButton, .clipboardCPText").show();
  200. },
  201. function() {
  202. console.log("We hover out the value with a copy paste feature");
  203. $(this).children(".clipboardCPButton, .clipboardCPText").hide();
  204. }
  205. );
  206. jQuery(\'.clipboardCPValue, .clipboardCPButton, .clipboardCPValueToPrint\').click(function() {
  207. console.log("We click on a clipboardCPButton or clipboardCPValueToPrint class and we want to copy content of clipboardCPValue class");
  208. if (window.getSelection) {
  209. jqobj=$(this).parent().children(".clipboardCPValue");
  210. console.log(jqobj.html());
  211. selection = window.getSelection(); /* get the object used for selection */
  212. selection.removeAllRanges(); /* clear current selection */
  213. /* We select the value to print using the parentNode.firstChild */
  214. /* We should use the class clipboardCPValue but it may have several element with copy/paste so class to select is not enough */
  215. range = document.createRange();
  216. range.selectNodeContents(this.parentNode.firstChild);
  217. selection.addRange(range); /* make the new selection with the value to copy */
  218. /* copy selection into clipboard */
  219. var succeed;
  220. try {
  221. console.log("We set the style display to unset for the span so the copy will work");
  222. jqobj.css("display", "unset"); /* Because copy does not work on "block" object */
  223. succeed = document.execCommand(\'copy\');
  224. console.log("We set the style display back to inline-block");
  225. jqobj.css("display", "inline-block");
  226. } catch(e) {
  227. succeed = false;
  228. }
  229. /* Remove the selection to avoid to see the hidden field to copy selected */
  230. window.getSelection().removeAllRanges();
  231. }
  232. /* Show message */
  233. /* TODO Show message into a top left corner or center of screen */
  234. var lastchild = this.parentNode.lastChild; /* .parentNode is clipboardCP and last child is clipboardCPText */
  235. var tmp = lastchild.innerHTML
  236. if (succeed) {
  237. lastchild.innerHTML = \'<div class="clipboardCPTextDivInside opacitymedium">'.dol_escape_js($langs->trans('CopiedToClipboard')).'</div>\';
  238. } else {
  239. lastchild.innerHTML = \'<div class="clipboardCPTextDivInside opacitymedium">'.dol_escape_js($langs->trans('Error')).'</div>\';
  240. }
  241. setTimeout(() => { lastchild.innerHTML = tmp; }, 1000);
  242. });
  243. });'."\n";
  244. // Code to manage clicktodial
  245. print "\n/* JS CODE TO ENABLE clicktodial call of an URL */\n";
  246. print '
  247. jQuery(document).ready(function() {
  248. jQuery(".cssforclicktodial").click(function() {
  249. event.preventDefault();
  250. console.log("We click on a cssforclicktodial class with url="+this.href);
  251. $.ajax({
  252. url: this.href,
  253. type: \'GET\',
  254. data: { token: \''.newToken().'\' }
  255. }).done(function(xhr, textStatus, errorThrown) {
  256. /* do nothing */
  257. }).fail(function(xhr, textStatus, errorThrown) {
  258. alert("Error: "+textStatus);
  259. });
  260. return false;
  261. });
  262. });'."\n";
  263. // Code to manage the confirm dialog box
  264. print "\n/* JS CODE TO ENABLE DIALOG CONFIRM POPUP ON ACTION BUTTON */\n";
  265. print '
  266. jQuery(document).ready(function() {
  267. $(document).on("click", \'.butActionConfirm\', function(event) {
  268. event.preventDefault();
  269. // I don\'t use jquery $(this).data(\'confirm-url\'); to get $(this).attr(\'data-confirm-url\'); because .data() can doesn\'t work with ajax
  270. var confirmUrl = $(this).attr(\'data-confirm-url\');
  271. var confirmTitle = $(this).attr(\'data-confirm-title\');
  272. var confirmContent = $(this).attr(\'data-confirm-content\');
  273. var confirmActionBtnLabel = $(this).attr(\'data-confirm-action-btn-label\');
  274. var confirmCancelBtnLabel = $(this).attr(\'data-confirm-cancel-btn-label\');
  275. var confirmModal = $(this).attr(\'data-confirm-modal\');
  276. if(confirmModal == undefined){ confirmModal = false; }
  277. var confirmId = \'confirm-dialog-box\';
  278. if($(this).attr(\'id\') != undefined){ var confirmId = confirmId + "-" + $(this).attr(\'id\'); }
  279. if($("#" + confirmId) != undefined) { $(\'#\' + confirmId).remove(); }
  280. // Create modal box
  281. var $confirmBox = $(\'<div/>\', {
  282. id: confirmId,
  283. title: confirmTitle
  284. }).appendTo(\'body\');
  285. $confirmBox.dialog({
  286. autoOpen: true,
  287. modal: confirmModal,
  288. //width: Math.min($( window ).width() - 50, 1700),
  289. width: \'auto\',
  290. dialogClass: \'confirm-dialog-box\',
  291. buttons: [
  292. {
  293. text: confirmActionBtnLabel,
  294. "class": \'ui-state-information\',
  295. click: function () {
  296. window.location.replace(confirmUrl);
  297. }
  298. },
  299. {
  300. text: confirmCancelBtnLabel,
  301. "class": \'ui-state-information\',
  302. click: function () {
  303. $(this).dialog("close");
  304. }
  305. }
  306. ],
  307. close: function( event, ui ) {
  308. $(\'#\'+confirmBox).remove();
  309. },
  310. open: function( event, ui ) {
  311. $confirmBox.html(confirmContent);
  312. }
  313. });
  314. });
  315. });
  316. '."\n";