lib_head.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright (C) 2014-2021 Regis Houssin <regis.houssin@inodbox.com>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // or see http://www.gnu.org/
  16. //
  17. // \file /multicompany/core/js/lib_head.js
  18. // \brief File that include javascript functions (included if option use_javascript activated)
  19. //
  20. /*
  21. *
  22. */
  23. function setMulticompanyConstant(url, code, input, entity, strict, forcereload, userid, token) {
  24. var saved_url = url;
  25. $.post( url, {
  26. action: "set",
  27. name: code,
  28. entity: entity,
  29. token: token
  30. },
  31. function() {
  32. console.log("url request success forcereload="+forcereload);
  33. $("#set_" + code).hide();
  34. $("#del_" + code).show();
  35. $.each(input, function(type, data) {
  36. // Enable another element
  37. if (type == "enabled") {
  38. $.each(data, function(key, value) {
  39. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  40. $(newvalue).removeAttr("disabled");
  41. });
  42. // Disable another element
  43. } else if (type == "disabled") {
  44. $.each(data, function(key, value) {
  45. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  46. $(newvalue).attr("disabled", true);
  47. });
  48. // enable and disable another element
  49. } else if (type == "disabledenabled") {
  50. $.each(data, function(key, value) {
  51. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  52. $(newvalue).removeAttr("disabled");
  53. });
  54. // Show another element
  55. } else if (type == "showhide" || type == "show") {
  56. $.each(data, function(key, value) {
  57. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  58. $(newvalue).show();
  59. });
  60. } else if (type == "hideshow") {
  61. $.each(data, function(key, value) {
  62. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  63. $(newvalue).hide();
  64. });
  65. // Set another constant
  66. } else if (type == "set" || type == "del") {
  67. $.each(data, function(key, value) {
  68. if (type == "set") {
  69. $("#set_" + value).hide();
  70. $("#del_" + value).show();
  71. $.post( saved_url, {
  72. action: type,
  73. name: value,
  74. //name: key,
  75. //value: value,
  76. entity: entity,
  77. token: token
  78. });
  79. } else if (type == "del") {
  80. $("#del_" + value).hide();
  81. $("#set_" + value).show();
  82. $.post( saved_url, {
  83. action: type,
  84. name: value,
  85. entity: entity,
  86. token: token
  87. });
  88. }
  89. });
  90. // reload the current page
  91. } else if (type == "reload") {
  92. var url = window.location.pathname;
  93. location.href=url;
  94. }
  95. });
  96. if (forcereload) {
  97. location.reload();
  98. }
  99. }).fail(function(error) { location.reload(); }); /* When it fails, we always force reload to have setEventErrorMEssage in session visible */
  100. }
  101. /*
  102. *
  103. */
  104. function delMulticompanyConstant(url, code, input, entity, strict, forcereload, userid, token) {
  105. var saved_url = url;
  106. $.post( url, {
  107. action: "del",
  108. name: code,
  109. entity: entity,
  110. token: token
  111. },
  112. function() {
  113. console.log("url request success forcereload="+forcereload);
  114. $("#del_" + code).hide();
  115. $("#set_" + code).show();
  116. $.each(input, function(type, data) {
  117. // Enable another element
  118. if (type == "enabled" && strict != 1) {
  119. $.each(data, function(key, value) {
  120. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  121. $(newvalue).removeAttr("disabled");
  122. });
  123. // Disable another element
  124. } else if (type == "disabled") {
  125. $.each(data, function(key, value) {
  126. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  127. $(newvalue).attr("disabled", true);
  128. });
  129. // enable and disable another element
  130. } else if (type == "disabledenabled") {
  131. $.each(data, function(key, value) {
  132. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  133. $(newvalue).attr("disabled", true);
  134. });
  135. } else if (type == "showhide" || type == "hide") {
  136. $.each(data, function(key, value) {
  137. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  138. $(newvalue).hide();
  139. });
  140. } else if (type == "hideshow") {
  141. $.each(data, function(key, value) {
  142. var newvalue=(value.search("^#") < 0 ? "#" : "") + value;
  143. $(newvalue).show();
  144. });
  145. // Delete another constant
  146. } else if (type == "set" || type == "del") {
  147. $.each(data, function(key, value) {
  148. if (type == "set") {
  149. $("#set_" + value).hide();
  150. $("#del_" + value).show();
  151. $.post( saved_url, {
  152. action: type,
  153. name: value,
  154. //name: key,
  155. //value: value,
  156. entity: entity,
  157. token: token
  158. });
  159. } else if (type == "del") {
  160. $("#del_" + value).hide();
  161. $("#set_" + value).show();
  162. $.post( saved_url, {
  163. action: type,
  164. name: value,
  165. entity: entity,
  166. token: token
  167. });
  168. }
  169. });
  170. // reload the current page
  171. } else if (type == "reload") {
  172. var url = window.location.pathname;
  173. location.href=url;
  174. }
  175. });
  176. if (forcereload) {
  177. location.reload();
  178. }
  179. }).fail(function(error) { location.reload(); }); /* When it fails, we always force reload to have setEventErrorMEssage in session visible */
  180. }
  181. /*
  182. *
  183. */
  184. function confirmMulticompanyConstantAction(action, url, code, input, box, entity, yesButton, noButton, strict, userid, token) {
  185. var boxConfirm = box;
  186. $("#confirm_" + code)
  187. .attr("title", boxConfirm.title)
  188. .html(boxConfirm.content)
  189. .dialog({
  190. resizable: false,
  191. height: 180,
  192. width: 500,
  193. modal: true,
  194. buttons: [
  195. {
  196. id : 'yesButton_' + code,
  197. text : yesButton,
  198. click : function() {
  199. if (action == "set") {
  200. setMulticompanyConstant(url, code, input, entity, strict, 0, userid, token);
  201. } else if (action == "del") {
  202. delMulticompanyConstant(url, code, input, entity, strict, 0, userid, token);
  203. }
  204. // Close dialog
  205. $(this).dialog("close");
  206. // Execute another method
  207. if (boxConfirm.method) {
  208. var fnName = boxConfirm.method;
  209. if (window.hasOwnProperty(fnName)) {
  210. window[fnName]();
  211. }
  212. }
  213. }
  214. },
  215. {
  216. id : 'noButton_' + code,
  217. text : noButton,
  218. click : function() {
  219. $(this).dialog("close");
  220. }
  221. }
  222. ]
  223. });
  224. // For information dialog box only, hide the noButton
  225. if (boxConfirm.info) {
  226. $("#noButton_" + code).button().hide();
  227. }
  228. }