template.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /** template.js ***/
  2. /**
  3. $.imxNotify() - allows to display notification msg
  4. msg - message to display
  5. type - a bootstrap alert type message (alert, success, ...)
  6. **/
  7. $.imxNotify = function (msg, type) {
  8. let icon = 'fas fa-exclamation-triangle';
  9. type = type || 'success';
  10. switch (type) {
  11. case 'primary':
  12. break;
  13. case 'secondary':
  14. break;
  15. case 'success':
  16. icon = 'fas fa-check';
  17. break;
  18. case 'danger':
  19. break;
  20. case 'warning':
  21. icon = 'fas fa-exclamation-circle';
  22. break;
  23. case 'info':
  24. icon = 'fas fa-exclamation';
  25. break;
  26. case 'light':
  27. break;
  28. case 'dark':
  29. break;
  30. default:
  31. break;
  32. }
  33. $.notify({
  34. // options
  35. message: msg,
  36. icon: icon
  37. }, {
  38. // settings
  39. type: type,
  40. });
  41. };
  42. /**
  43. *
  44. * imxGetScript - Load a JavaScript file from the server using a GET HTTP request, then execute it.
  45. * alternative to $.getScript -
  46. *
  47. * url : url to the file to load
  48. * options : jQuery.ajax option - https://api.jquery.com/jQuery.Ajax/
  49. * eg : {cache:false}
  50. *
  51. */
  52. $.imxGetScript = function (url, options) {
  53. // Allow user to set any option except for dataType, cache, and url
  54. options = $.extend(options || { dataType: "script", cache: true }, {
  55. url: url
  56. });
  57. // Use $.ajax() since it is more flexible than $.getScript
  58. // Return the jqXHR object so we can chain callbacks
  59. return jQuery.ajax(options);
  60. };
  61. /**
  62. *
  63. * loadFileOnce - do as $.getScript but manages to load the file only once
  64. * url : url to the file to load
  65. * options : jQuery.ajax option - https://api.jquery.com/jQuery.Ajax/
  66. * eg : {cache:false}
  67. *
  68. */
  69. $.loadFileOnce = (function () {
  70. let loadedFiles = {};
  71. let func = function (aUrlFile, options) {
  72. if (!loadedFiles[aUrlFile])
  73. loadedFiles[aUrlFile] = $.imxGetScript(aUrlFile, options);
  74. return loadedFiles[aUrlFile];
  75. };
  76. return func;
  77. })();
  78. /**
  79. * show a Loader
  80. * @param aLoader@type {String} loader name
  81. * loader available:
  82. * - 'gooey'
  83. * - 'sasha'
  84. */
  85. var imxLoaderDisplayCalls = 0;
  86. $.imxLoaderDisplay = function (aLoader) {
  87. imxLoaderDisplayCalls++;
  88. $("#waitLoader").addClass('show' + aLoader);
  89. $("#waitLoader").addClass("display");
  90. };
  91. /**
  92. * hide the wait loader
  93. */
  94. $.imxLoaderHide = function (forceReset) {
  95. imxLoaderDisplayCalls--;
  96. if (forceReset == null) forceReset = false;// set to false if forceReset is undefined or null
  97. if (!forceReset && imxLoaderDisplayCalls > 0) return;
  98. else imxLoaderDisplayCalls = 0; //if for whatever reason imxLoaderDisplayCalls < 0,set it to 0
  99. $("#waitLoader").removeClass();
  100. $("#waitLoader").addClass("loader");
  101. };
  102. /*** FAVORIS Management ***/
  103. function addFavoris(idFormation) {
  104. $.ajax({
  105. type: 'POST',
  106. url: '//' + window.location.host + '/assets/templates/php/addFavoris.php',
  107. data: 'idFormation=' + idFormation,
  108. success: function (data) {
  109. if (data == 1) {
  110. alert("La fiche a bien été ajoutée à vos favoris.");
  111. }
  112. else if (data == 2) {
  113. alert("La fiche existe déjà dans vos favoris.");
  114. }
  115. else {
  116. alert("Erreur lors de l'ajout de votre fiche dans les favoris.");
  117. }
  118. }
  119. });
  120. };
  121. /**
  122. * removeClassRegex - remove every classes of jQuery (dom) element(s) whose class names match with 'regex'
  123. * @param {*} regex
  124. * @returns jQuery element(s)
  125. * @example $('#hello').removeClassRegex(/^color-/)
  126. */
  127. $.fn.removeClassRegex = function (regex) {
  128. return $(this).removeClass(function (index, classes) {
  129. return classes.split(/\s+/).filter(function (c) {
  130. return regex.test(c);
  131. }).join(' ');
  132. });
  133. };
  134. /**
  135. * removeClassStartingWith - remove every classes of jQuery (dom) element(s) whose class names start with 'filter'
  136. * @param string filter - a class name start
  137. * @returns jQuery object
  138. * @example $('#hello').removeClassStartingWith('color-')
  139. */
  140. $.fn.removeClassStartingWith = function removeClassStartingWith_custom(filter) {
  141. $(this).removeClass(function (index, className) {
  142. return (className.match(new RegExp("\\S*" + filter + "\\S*", 'g')) || []).join(' ')
  143. });
  144. return this;
  145. };
  146. /**
  147. * jQuery alterClass plugin
  148. *
  149. * Remove element classes with wildcard matching. Optionally add classes:
  150. * $( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' )
  151. *
  152. * Copyright (c) 2011 Pete Boere (the-echoplex.net)
  153. * Free under terms of the MIT license: http://www.opensource.org/licenses/mit-license.php
  154. *
  155. */
  156. $.fn.alterClass = function (removals, additions) {
  157. var self = this;
  158. if (removals.indexOf('*') === -1) {
  159. // Use native jQuery methods if there is no wildcard matching
  160. self.removeClass(removals);
  161. return !additions ? self : self.addClass(additions);
  162. }
  163. var patt = new RegExp('\\s' +
  164. removals.
  165. replace(/\*/g, '[A-Za-z0-9-_]+').
  166. split(' ').
  167. join('\\s|\\s') +
  168. '\\s', 'g');
  169. self.each(function (i, it) {
  170. var cn = ' ' + it.className + ' ';
  171. while (patt.test(cn)) {
  172. cn = cn.replace(patt, ' ');
  173. }
  174. it.className = $.trim(cn);
  175. });
  176. return !additions ? self : self.addClass(additions);
  177. };
  178. /**
  179. * proxies jQuery functions to emit events on show, hide, and remove events.
  180. *
  181. * https://github.com/hypesystem/showandtell.js
  182. * Copyright (c) 2017 Niels Abildgaard
  183. * Currently supported hides: hide, fadeOut, slideUp, remove, toggle, fadeToggle, slideToggle,
  184. * css (display), animate (height: hide, opacity: hide).
  185. *
  186. * Examples:
  187. * $("#myElement").on("showandtell.hide", function() {
  188. * alert("hide triggered");
  189. * }
  190. * $("#myElement").hide(); //Hides element, then triggers hide event
  191. *
  192. * Events:
  193. *
  194. * "showandtell.hide" on $().hide()
  195. * "showandtell.remove" on $().remove()
  196. * "showandtell.show" on $().show()
  197. * "showandtell.cssHide" on $().css('display:xxx') where xxx != "none"
  198. * "showandtell.cssShow" on $().css('display:none')
  199. *
  200. * Remarks Intersel:
  201. * - add functions Show() and Hide() called everytime events occur
  202. */
  203. (function ($) {
  204. // For any element that is shown or hidden, do these actions
  205. // fix chrome pb when a required input is hidden, chrome do not want to validate the form
  206. // so we remove "required" property on the hidden input, then reset it when shown again
  207. function Show() {
  208. // debugger;
  209. let requiredInput = $(this).find(":input[data-wasrequired=1]");
  210. requiredInput.prop("required", "required");
  211. };
  212. function Hide() {
  213. // debugger;
  214. let requiredInput = $(this).find(":input[required]");
  215. requiredInput.attr("data-wasrequired", "1");
  216. requiredInput.removeAttr("required");
  217. };
  218. function applyIfExistsOrNull(fun, self, args) {
  219. return typeof fun !== "undefined" ? fun.apply(self, args) : null;
  220. }
  221. function proxy$Functions(functionProxies) {
  222. $.each(functionProxies, function (old_function_name, proxy_settings) {
  223. var old_function = $.fn[old_function_name];
  224. $.fn[old_function_name] = function () {
  225. var pre_result = applyIfExistsOrNull(proxy_settings.pre, this, arguments);
  226. var result = old_function.apply(this, arguments);
  227. var postArguments = [result, pre_result, arguments];
  228. applyIfExistsOrNull(proxy_settings.post, this, postArguments);
  229. return result;
  230. }
  231. });
  232. }
  233. function checkIfElementWasHidden() {
  234. return $(this).map(function () { return $(this).is(":hidden"); });
  235. }
  236. //Only register if not already registered
  237. if (!$.showandtell) {
  238. //Indicate that showandtell has been registered.
  239. $.showandtell = true;
  240. proxy$Functions({
  241. hide: {
  242. pre: checkIfElementWasHidden,
  243. post: function (result, elementWasHidden, old_args) {
  244. $(this)
  245. .filter(function (idx) { return !elementWasHidden[idx]; })
  246. .each(function () {
  247. $(this).triggerHandler("showandtell.hide");
  248. Hide.apply(this);
  249. });
  250. }
  251. },
  252. show: {
  253. pre: checkIfElementWasHidden,
  254. post: function (result, elementWasHidden, old_args) {
  255. $(this)
  256. .filter(function (idx) { return elementWasHidden[idx]; })
  257. .each(function () {
  258. $(this).triggerHandler("showandtell.show");
  259. Show.apply(this);
  260. });
  261. }
  262. },
  263. remove: {
  264. //Remove has to trigger event before removing. After removal, there is no element to
  265. // trigger the event on.
  266. pre: function () {
  267. $(this)
  268. .each(function () {
  269. $(this).triggerHandler("showandtell.remove");
  270. Hide.apply(this);
  271. });
  272. }
  273. },
  274. css: {
  275. pre: checkIfElementWasHidden,
  276. post: function (result, elementWasHidden, old_args) {
  277. $(this)
  278. .filter(function (idx) {
  279. return !elementWasHidden[idx] && old_args[0] == "display" && old_args[1] == "none";
  280. }).each(function () {
  281. $(this).triggerHandler("showandtell.cssHide");
  282. Hide.apply(this);
  283. });
  284. $(this)
  285. .filter(function (idx) {
  286. return elementWasHidden[idx] && old_args[0] == "display" && old_args[1] != "none";
  287. }).each(function () {
  288. $(this).triggerHandler("showandtell.cssShow");
  289. Show.apply(this);
  290. });
  291. }
  292. }
  293. });
  294. }
  295. })(jQuery);