/** template.js ***/ /** $.imxNotify() - allows to display notification msg msg - message to display type - a bootstrap alert type message (alert, success, ...) **/ $.imxNotify = function (msg, type) { let icon = 'fas fa-exclamation-triangle'; type = type || 'success'; switch (type) { case 'primary': break; case 'secondary': break; case 'success': icon = 'fas fa-check'; break; case 'danger': break; case 'warning': icon = 'fas fa-exclamation-circle'; break; case 'info': icon = 'fas fa-exclamation'; break; case 'light': break; case 'dark': break; default: break; } $.notify({ // options message: msg, icon: icon }, { // settings type: type, }); }; /** * * imxGetScript - Load a JavaScript file from the server using a GET HTTP request, then execute it. * alternative to $.getScript - * * url : url to the file to load * options : jQuery.ajax option - https://api.jquery.com/jQuery.Ajax/ * eg : {cache:false} * */ $.imxGetScript = function (url, options) { // Allow user to set any option except for dataType, cache, and url options = $.extend(options || { dataType: "script", cache: true }, { url: url }); // Use $.ajax() since it is more flexible than $.getScript // Return the jqXHR object so we can chain callbacks return jQuery.ajax(options); }; /** * * loadFileOnce - do as $.getScript but manages to load the file only once * url : url to the file to load * options : jQuery.ajax option - https://api.jquery.com/jQuery.Ajax/ * eg : {cache:false} * */ $.loadFileOnce = (function () { let loadedFiles = {}; let func = function (aUrlFile, options) { if (!loadedFiles[aUrlFile]) loadedFiles[aUrlFile] = $.imxGetScript(aUrlFile, options); return loadedFiles[aUrlFile]; }; return func; })(); /** * show a Loader * @param aLoader@type {String} loader name * loader available: * - 'gooey' * - 'sasha' */ var imxLoaderDisplayCalls = 0; $.imxLoaderDisplay = function (aLoader) { imxLoaderDisplayCalls++; $("#waitLoader").addClass('show' + aLoader); $("#waitLoader").addClass("display"); }; /** * hide the wait loader */ $.imxLoaderHide = function (forceReset) { imxLoaderDisplayCalls--; if (forceReset == null) forceReset = false;// set to false if forceReset is undefined or null if (!forceReset && imxLoaderDisplayCalls > 0) return; else imxLoaderDisplayCalls = 0; //if for whatever reason imxLoaderDisplayCalls < 0,set it to 0 $("#waitLoader").removeClass(); $("#waitLoader").addClass("loader"); }; /*** FAVORIS Management ***/ function addFavoris(idFormation) { $.ajax({ type: 'POST', url: '//' + window.location.host + '/assets/templates/php/addFavoris.php', data: 'idFormation=' + idFormation, success: function (data) { if (data == 1) { alert("La fiche a bien été ajoutée à vos favoris."); } else if (data == 2) { alert("La fiche existe déjà dans vos favoris."); } else { alert("Erreur lors de l'ajout de votre fiche dans les favoris."); } } }); }; /** * removeClassRegex - remove every classes of jQuery (dom) element(s) whose class names match with 'regex' * @param {*} regex * @returns jQuery element(s) * @example $('#hello').removeClassRegex(/^color-/) */ $.fn.removeClassRegex = function (regex) { return $(this).removeClass(function (index, classes) { return classes.split(/\s+/).filter(function (c) { return regex.test(c); }).join(' '); }); }; /** * removeClassStartingWith - remove every classes of jQuery (dom) element(s) whose class names start with 'filter' * @param string filter - a class name start * @returns jQuery object * @example $('#hello').removeClassStartingWith('color-') */ $.fn.removeClassStartingWith = function removeClassStartingWith_custom(filter) { $(this).removeClass(function (index, className) { return (className.match(new RegExp("\\S*" + filter + "\\S*", 'g')) || []).join(' ') }); return this; }; /** * jQuery alterClass plugin * * Remove element classes with wildcard matching. Optionally add classes: * $( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' ) * * Copyright (c) 2011 Pete Boere (the-echoplex.net) * Free under terms of the MIT license: http://www.opensource.org/licenses/mit-license.php * */ $.fn.alterClass = function (removals, additions) { var self = this; if (removals.indexOf('*') === -1) { // Use native jQuery methods if there is no wildcard matching self.removeClass(removals); return !additions ? self : self.addClass(additions); } var patt = new RegExp('\\s' + removals. replace(/\*/g, '[A-Za-z0-9-_]+'). split(' '). join('\\s|\\s') + '\\s', 'g'); self.each(function (i, it) { var cn = ' ' + it.className + ' '; while (patt.test(cn)) { cn = cn.replace(patt, ' '); } it.className = $.trim(cn); }); return !additions ? self : self.addClass(additions); }; /** * proxies jQuery functions to emit events on show, hide, and remove events. * * https://github.com/hypesystem/showandtell.js * Copyright (c) 2017 Niels Abildgaard * Currently supported hides: hide, fadeOut, slideUp, remove, toggle, fadeToggle, slideToggle, * css (display), animate (height: hide, opacity: hide). * * Examples: * $("#myElement").on("showandtell.hide", function() { * alert("hide triggered"); * } * $("#myElement").hide(); //Hides element, then triggers hide event * * Events: * * "showandtell.hide" on $().hide() * "showandtell.remove" on $().remove() * "showandtell.show" on $().show() * "showandtell.cssHide" on $().css('display:xxx') where xxx != "none" * "showandtell.cssShow" on $().css('display:none') * * Remarks Intersel: * - add functions Show() and Hide() called everytime events occur */ (function ($) { // For any element that is shown or hidden, do these actions // fix chrome pb when a required input is hidden, chrome do not want to validate the form // so we remove "required" property on the hidden input, then reset it when shown again function Show() { // debugger; let requiredInput = $(this).find(":input[data-wasrequired=1]"); requiredInput.prop("required", "required"); }; function Hide() { // debugger; let requiredInput = $(this).find(":input[required]"); requiredInput.attr("data-wasrequired", "1"); requiredInput.removeAttr("required"); }; function applyIfExistsOrNull(fun, self, args) { return typeof fun !== "undefined" ? fun.apply(self, args) : null; } function proxy$Functions(functionProxies) { $.each(functionProxies, function (old_function_name, proxy_settings) { var old_function = $.fn[old_function_name]; $.fn[old_function_name] = function () { var pre_result = applyIfExistsOrNull(proxy_settings.pre, this, arguments); var result = old_function.apply(this, arguments); var postArguments = [result, pre_result, arguments]; applyIfExistsOrNull(proxy_settings.post, this, postArguments); return result; } }); } function checkIfElementWasHidden() { return $(this).map(function () { return $(this).is(":hidden"); }); } //Only register if not already registered if (!$.showandtell) { //Indicate that showandtell has been registered. $.showandtell = true; proxy$Functions({ hide: { pre: checkIfElementWasHidden, post: function (result, elementWasHidden, old_args) { $(this) .filter(function (idx) { return !elementWasHidden[idx]; }) .each(function () { $(this).triggerHandler("showandtell.hide"); Hide.apply(this); }); } }, show: { pre: checkIfElementWasHidden, post: function (result, elementWasHidden, old_args) { $(this) .filter(function (idx) { return elementWasHidden[idx]; }) .each(function () { $(this).triggerHandler("showandtell.show"); Show.apply(this); }); } }, remove: { //Remove has to trigger event before removing. After removal, there is no element to // trigger the event on. pre: function () { $(this) .each(function () { $(this).triggerHandler("showandtell.remove"); Hide.apply(this); }); } }, css: { pre: checkIfElementWasHidden, post: function (result, elementWasHidden, old_args) { $(this) .filter(function (idx) { return !elementWasHidden[idx] && old_args[0] == "display" && old_args[1] == "none"; }).each(function () { $(this).triggerHandler("showandtell.cssHide"); Hide.apply(this); }); $(this) .filter(function (idx) { return elementWasHidden[idx] && old_args[0] == "display" && old_args[1] != "none"; }).each(function () { $(this).triggerHandler("showandtell.cssShow"); Show.apply(this); }); } } }); } })(jQuery);