plugins.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // Avoid `console` errors in browsers that lack a console.
  2. (function() {
  3. var method;
  4. var noop = function () {};
  5. var methods = [
  6. 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
  7. 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
  8. 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
  9. 'timeline', 'timelineEnd', 'timeStamp', 'trace', 'warn'
  10. ];
  11. var length = methods.length;
  12. var console = (window.console = window.console || {});
  13. while (length--) {
  14. method = methods[length];
  15. // Only stub undefined methods.
  16. if (!console[method]) {
  17. console[method] = noop;
  18. }
  19. }
  20. }());
  21. //Affiche un message 'message' de type 'type' pendant 'timeout' secondes
  22. $.message = function (type,message,timeout){
  23. message = message.replace(/<\/?[script|iframe|object][^>]*>/gim,'');
  24. $.toast({ type: type, content: message, timeout: timeout });
  25. }
  26. //Permet les notifications types toast sans dépendance de librairie/css/html
  27. $.toast = function (options) {
  28. var defaults = {
  29. title: null,
  30. content: '',
  31. type: 'info',
  32. timeout: 3000
  33. };
  34. var o = $.extend(defaults, options);
  35. var css = "word-wrap:break-word;color:#ffffff;border-radius:1px;margin-bottom:10px;text-align:center;width:100%;box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);position:relative;";
  36. var types = {
  37. error: { css: "background:#dc2626;", icon: 'fas fa-exclamation-triangle' },
  38. warning: { css: "background:#ffc107;color:#212529;", icon: 'fas fa-exclamation-circle' },
  39. info: { css: "background:#2AA7EA;", icon: 'fas fa-info-circle' },
  40. success: { css: "background:#28a745;", icon: 'fas fa-check' },
  41. }
  42. css += types[o.type].css;
  43. o.icon = types[o.type].icon;
  44. if ($('.toastContainer').length == 0)
  45. $('body').append('<div class="toastContainer" style="z-index:1100;position:fixed;right:15px;top:65px;width:350px;"></div>');
  46. var popupContainer = $('.toastContainer');
  47. var popup = $('<div style="' + css + '" class="noPrint toast-'+o.type+'"><i class="toastRemove fas fa-times hidden" style="position:absolute;top:5px;right:5px;cursor:pointer;" onclick="$(this).parent().remove();"; style="position:absolute;top:5px;right:5px;cursor:pointer;"></i><h1 class="toastTitle" style="margin:0;padding:10px;"></h1><i class="toastIcon" style="display:inline-block;vertical-align:top;width:30px;font-size:20px;margin:15px 0;"></i><p style="display: inline-block;margin-top: 10px;padding:0 10px 0 10px;width:300px;" class="toastContent"></p><div style="clear:both;"></div></div>');
  48. $(popupContainer).append(popup);
  49. if (o.title) $('.toastTitle', popup).html(o.title);
  50. $('.toastIcon', popup).attr('class', o.icon);
  51. if (!o.title) $('.toastTitle', popup).remove();
  52. $('.toastContent', popup).html(o.content);
  53. popup.removeClass('hidden');
  54. if (o.timeout != 0) {
  55. setTimeout(function () { popup.addClass('hidden'); }, o.timeout);
  56. } else {
  57. popup.find('.toastRemove').removeClass('hidden');
  58. }
  59. }
  60. $.page = function(element){
  61. var path = window.location.pathname.split('/') ;
  62. path = path[path.length-1];
  63. path = path.replace('.php','');
  64. return path;
  65. }
  66. $.getForm= function(element){
  67. return $(element).getForm();
  68. }
  69. $.setForm= function(element,data){
  70. var o = {};
  71. var obj = $(element);
  72. $('input,select,textarea',obj).each(function(i,element){
  73. if(element.id!=null && element.id!=""){
  74. if(data[element.id]!=null){
  75. if($(element).attr("type")=='checkbox' || $(element).attr("type")=='radio'){
  76. $(element).prop("checked",data[element.id]==1 || data[element.id]=='true' ?true:false);
  77. } else {
  78. $(element).val(data[element.id]);
  79. }
  80. }
  81. }
  82. });
  83. return o;
  84. }
  85. //Gestion des tableaux pour le formData
  86. $.form_data = function(formData, key,values){
  87. var hasFile = false;
  88. if(values instanceof File ) hasFile = true;
  89. if( typeof values == 'object' && !(values instanceof File) ){
  90. for(subkey in values)
  91. if($.form_data(formData,key + '[' + subkey + ']',values[subkey])) hasFile = true;
  92. }else{
  93. formData.append(key, values);
  94. }
  95. return hasFile;
  96. }
  97. $.action = function(data,success,error,progress) {
  98. var formData = new FormData();
  99. var defaultSuccess = data.defaultSuccess != undefined ? data.defaultSuccess : true;
  100. // if(data.defaultSuccess !== undefined) delete data.defaultSuccess;
  101. //hasFile determine si un fichier uploadé se trouve dans le tableau des data
  102. // Si c'est le cas, il envois les données en multipart, sinon il les envoie en json classique.
  103. var hasFile = false;
  104. $.each(data, function(key, value){
  105. if($.form_data(formData,key, value) == true) hasFile = true;
  106. });
  107. var request = {
  108. url : 'action.php',
  109. method : 'POST',
  110. success: function(response){
  111. if(response && !response.error){
  112. if(success!=null)success(response);
  113. } else {
  114. if(response.errorCode && response.errorCode=='401') window.location = 'action.php?action=logout';
  115. var errorMessage = 'ERREUR : '+"\n"+response.error;
  116. if(response.trace) errorMessage += "\n<div style='font-size:10px;max-height:200px;overflow:auto;text-align:left;'>"+response.trace+"</div>";
  117. if(defaultSuccess) $.message('error',errorMessage,0);
  118. if(error!=null) error(response);
  119. }
  120. },
  121. error : function(response){
  122. if(response.status == 200 && $.localhost() ){
  123. $('body').append('<div class="debugFrame" style="box-shadow: 0px 0px 10px rgba(0,0,0,0.5);z-index: 10001;padding: 5px;position: fixed;left: 0;top: 0;width: 40%;min-height: 100%;border-right: 2px solid #ff3b00;background: rgba(210, 222, 255, 0.9);overflow-y: auto;height: 100%; word-break: break-word;"><h3>Action debug</h3> <i style="position:absolute; right:10px; top:10px;font-size:1.5em;" onclick="$(this).parent().remove()" class="fas fa-times pointer"></i>'+response.responseText+'</div>');
  124. } else {
  125. if(response.readyState == 0 && error==null) return;
  126. if(error!=null){
  127. if(response.errorCode && response.errorCode=='401') window.location = 'action.php?action=logout';
  128. error(response);
  129. }else{
  130. $.message('error','Erreur indefinie, merci de contacter un administrateur',0);
  131. }
  132. }
  133. },
  134. xhr: function() {
  135. var xhr = new window.XMLHttpRequest();
  136. if(data.downloadResponse) xhr.responseType = 'blob';
  137. xhr.upload.addEventListener("progress", function(evt){
  138. if (evt.lengthComputable) {
  139. var percentComplete = (evt.loaded / evt.total) * 100;
  140. percentComplete = Math.round(percentComplete * 100) / 100;
  141. if(progress) progress(percentComplete,'upload');
  142. }
  143. }, false);
  144. xhr.addEventListener("progress", function(evt){
  145. if (evt.lengthComputable) {
  146. var percentComplete = evt.loaded / evt.total;
  147. if(progress) progress(percentComplete,'download');
  148. }
  149. }, false);
  150. xhr.addEventListener('readystatechange', function(e) {
  151. if(xhr.readyState == 4 && xhr.status == 200) {
  152. if(data.downloadResponse){
  153. var disposition = xhr.getResponseHeader('content-disposition');
  154. var matches = /"([^"]*)"/.exec(disposition);
  155. var filename = (matches != null && matches[1] ? matches[1] : 'file');
  156. var blob = new Blob([xhr.response], { type: xhr.response.type });
  157. var link = document.createElement('a');
  158. link.href = window.URL.createObjectURL(blob);
  159. link.download = filename;
  160. $('body').append(link);
  161. link.click();
  162. link.remove();
  163. window.URL.revokeObjectURL(link);
  164. }
  165. }
  166. });
  167. return xhr;
  168. }
  169. };
  170. if(!hasFile){
  171. request.data = data;
  172. }else{
  173. request.data = formData;
  174. request.processData = false;
  175. request.contentType = false;
  176. }
  177. $.ajax(request);
  178. }
  179. $.localhost = function(){
  180. return (document.location.hostname=='127.0.0.1' || document.location.hostname=='localhost');
  181. }
  182. $.hashData = function(name){
  183. var page = window.location.hash.substring(1);
  184. page += "&"+window.location.search.substring(1);
  185. data = {};
  186. if(page!='' && page!= null){
  187. options = page.split('&');
  188. var data = {};
  189. for(var key in options){
  190. infos = options[key].split('=');
  191. data[infos[0]] = infos[1];
  192. }
  193. }
  194. if(name == null) return data;
  195. if(typeof name === "object"){
  196. data = name;
  197. hashstring = '';
  198. for(var key in data)
  199. hashstring+= "&"+key+"="+data[key];
  200. hashstring = hashstring.substring(1);
  201. window.location.hash = hashstring;
  202. return;
  203. }
  204. return typeof data[name] == "undefined" ? '':data[name];
  205. }
  206. $.urlParam = function (name,value) {
  207. var parameters = window.location.href.match(/[\\?&]([^&#]*)=([^&#]*)/g);
  208. var data = {};
  209. for (var key in parameters) {
  210. var couple = parameters[key].substring(1, parameters[key].length).split('=');
  211. data[couple[0]] = couple[1];
  212. }
  213. if(name == null) return data;
  214. if (value == null)
  215. return data[name] ? data[name] : null;
  216. if (value != false) data[name] = value;
  217. var url = '?';
  218. for (var key in data) {
  219. if (value == false && key == name) continue;
  220. url += key + '=' + data[key]+'&';
  221. }
  222. window.history.pushState('', document.title, url.substring(0, url.length-1));
  223. }
  224. $.fn.extend({
  225. toJson : function(){
  226. return $.getForm(this);
  227. },
  228. getForm : function(){
  229. var o = {};
  230. var obj = $(this);
  231. for(var key in obj.data()){
  232. if(key!="action" && key != "id") continue;
  233. o[key] = obj.attr('data-'+key);
  234. }
  235. $('input,select,textarea',obj).each(function(i,element){
  236. if(element.id!=null && element.id!=""){
  237. if($(element).attr("type")=='checkbox' || $(element).attr("type")=='radio'){
  238. o[element.id] = ($(element).is(':checked')?1:0);
  239. }else{
  240. o[element.id] = $(element).val();
  241. }
  242. }
  243. });
  244. return o;
  245. },
  246. upload: function (options) {
  247. //var options = $.extend(defaults, options);
  248. return this.each(function () {
  249. var o = options;
  250. var droppedFiles = false;
  251. var div = $(this);
  252. var data = div.data();
  253. data.html =!div.attr('data-label') ? '': div.attr('data-label');
  254. data.label = data.html;
  255. div.html('');
  256. var o = $.extend(o, data);
  257. if(o.readonly == false){
  258. var form = $('<form class="box" method="post" action="' + o.action + '" enctype="multipart/form-data"><input type="file" name="'+(o.fileName?o.fileName:div.attr('id'))+'[]" multiple /></form>');
  259. div.append(form);
  260. var input = form.find('input[type="file"]');
  261. var zone = $('<div>' + o.label + '</div>');
  262. div.append(zone);
  263. //test if dnd is enabled n browser
  264. var div = document.createElement('div');
  265. var dragAndDropEnabled = (('draggable' in div) || ('ondragstart' in div && 'ondrop' in div)) && 'FormData' in window && 'FileReader' in window;
  266. //set elements styles
  267. input.attr('style', "width: 0.1px;height: 0.1px;opacity: 0;overflow: hidden; position: absolute;z-index: -1");
  268. zone.css('cursor', "pointer");
  269. //set events
  270. zone
  271. .on('click', function (e) {
  272. form.find('input[type="file"]').trigger('click');
  273. e.preventDefault();
  274. e.stopPropagation();
  275. })
  276. .on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  277. e.preventDefault();
  278. e.stopPropagation();
  279. })
  280. .on('dragover dragenter', function () {
  281. if (o.hover) form.addClass(o.hover);
  282. })
  283. .on('dragleave dragend drop', function () {
  284. if (o.hover) form.removeClass(o.hover);
  285. })
  286. .on('drop', function (e) {
  287. droppedFiles = e.originalEvent.dataTransfer.files;
  288. form.trigger('submit');
  289. });
  290. input.on('change', function (e) {
  291. form.trigger('submit');
  292. });
  293. form.on('submit', function (e) {
  294. if (o.start) o.start();
  295. if (dragAndDropEnabled) {
  296. e.preventDefault();
  297. var ajaxData = new FormData();
  298. if (droppedFiles) {
  299. $.each(droppedFiles, function (i, file) {
  300. var ext = file.name.split('.');
  301. ext = ext.pop();
  302. if(o.allowed && $.inArray(ext.toLowerCase(),o.allowed.split(','))===-1){
  303. $.message('error','Extension fichier '+ext+' non permise (autorisé:'+o.allowed+')',0);
  304. return;
  305. }
  306. if(o.size && file.size > o.size){
  307. $.message('error','Taille fichier '+file.size+' octets trop grande (autorisé:'+o.size+' octets)',0);
  308. return;
  309. }
  310. ajaxData.append(input.attr('name'), file);
  311. });
  312. }else{
  313. ajaxData = new FormData(form.get(0));
  314. for(var key in $('input',form).get(0).files){
  315. var file = $('input',form).get(0).files[key];
  316. if(file.name==null || typeof file !='object') continue;
  317. var ext = file.name.split('.');
  318. ext = ext.pop();
  319. if(o.allowed && $.inArray(ext.toLowerCase(),o.allowed.split(','))===-1){
  320. $.message('error','Extension fichier '+ext+' non permise (autorisé:'+o.allowed+')',0);
  321. $('input',form).val();
  322. return;
  323. }
  324. if(o.size && file.size > o.size){
  325. $.message('error','Taille fichier '+file.size+' octets trop grande (autorisé:'+o.size+' octets)',0);
  326. $('input',form).val();
  327. return;
  328. }
  329. }
  330. }
  331. if (o.addData){
  332. var addionalData = o.addData();
  333. for(var k in addionalData){
  334. ajaxData.append(k, addionalData[k]);
  335. }
  336. }
  337. droppedFiles = null;
  338. $.ajax({
  339. url: form.attr('action'),
  340. type: form.attr('method'),
  341. data: ajaxData,
  342. dataType: 'json',
  343. cache: false,
  344. contentType: false,
  345. processData: false,
  346. complete: function (data) {
  347. if (o.complete) o.complete(data.responseJSON);
  348. },
  349. success: function (data) {
  350. if (o.success) o.success(data);
  351. },
  352. error: function (data) {
  353. if (o.error) o.error(data);
  354. }
  355. });
  356. } else {
  357. var iframeName = 'uploadiframe' + new Date().getTime();
  358. iframe = $('<iframe name="' + iframeName + '" style="display: none;"></iframe>');
  359. $('body').append(iframe);
  360. form.attr('target', iframeName);
  361. iframe.one('load', function () {
  362. var data = JSON.parse(iframe.contents().find('body').text());
  363. if (!data.success) alert(data.error);
  364. form.removeAttr('target');
  365. iframe.remove();
  366. if (o.complete) o.complete();
  367. });
  368. }
  369. });
  370. }
  371. });
  372. },
  373. clear: function (){
  374. return this.each(function() {
  375. var obj = $(this);
  376. obj.find('input,select,textarea').val('').prop('checked',false).prop('selected',false);
  377. });
  378. },
  379. addLine: function (rows){
  380. return this.each(function() {
  381. var obj = $(this);
  382. var model = null;
  383. var container = null;
  384. if(obj.prop("tagName") == 'UL'){
  385. container = obj;
  386. model = container.find('li:first-child');
  387. container.find('li:visible').remove();
  388. }else if(obj.prop("tagName") == 'TABLE'){
  389. container = obj.find('tbody');
  390. model = container.find('tr:first-child');
  391. container.find('tr:visible').remove();
  392. }else{
  393. container = obj;
  394. childName = container.children().get(0).nodeName;
  395. model = container.find(childName+':first-child');
  396. container.find(childName+':visible:not(.nofill)').remove();
  397. }
  398. var tpl = model.get(0).outerHTML;
  399. //fix jquery backslahes break
  400. tpl = tpl.replace(/{{##/g,'{{/').replace(/{{\/(.*)}}=""/g,'{{/$1}}');
  401. //fix images url not found on template
  402. tpl = tpl.replace(/(<img\s[^>]*\s)(data-src)/g,'$1src');
  403. for(var key in rows){
  404. var line = $(Mustache.render(tpl,rows[key]));
  405. container.append(line);
  406. line.removeClass('hidden');
  407. }
  408. });
  409. },
  410. fill: function (option,callback,progress){
  411. return this.each(function() {
  412. var obj = $(this);
  413. var model = null;
  414. var container = null;
  415. option = $.extend({
  416. showing : function(item){
  417. //permet la personnalisation de l'apparition des lignes ( removeClass('hidden') par defaut)
  418. item.removeClass('hidden');
  419. }
  420. },option);
  421. var preloader = null;
  422. if(option.preloader){
  423. var preloader = $(option.preloader);
  424. preloader.css('position','absolute');
  425. preloader.css('left',(obj.offset().left+obj.width()/2)+'px');
  426. preloader.css('top',(obj.offset().top+30)+'px');
  427. $('body').append(preloader);
  428. }
  429. if(obj.prop("tagName") == 'UL'){
  430. container = obj;
  431. model = container.find('li:first-child');
  432. if(!option.export) container.children('li:visible').remove();
  433. } else if(obj.prop("tagName") == 'TABLE'){
  434. container = obj.find('tbody');
  435. model = container.find('tr:first-child');
  436. if(!option.export) container.children('tr:visible').remove();
  437. } else if(obj.prop("tagName") == "SELECT"){
  438. container = obj;
  439. model = container.find('option[value*="{{"]');
  440. if(model.length==0) model = $('<option class="hidden" value="{{value}}">{{label}}</option>');
  441. if(!option.export) container.find('option:not([value*="{{"])').remove();
  442. } else{
  443. container = obj;
  444. childName = container.children().get(0).nodeName;
  445. model = container.find(childName+':first-child');
  446. if(!option.export) container.find(childName+':visible:not(.nofill)').remove();
  447. }
  448. var tpl = model.get(0).outerHTML;
  449. //fix jquery backslashes break
  450. tpl = tpl.replace(/{{##/g,'{{/').replace(/{{\/(.*)}}=""/g,'{{/$1}}');
  451. //fix images url not found on template
  452. tpl = tpl.replace(/(<img\s[^>]*\s?)(data-src)/g,'$1src');
  453. var pagination = obj.nextAll('.pagination').length ? obj.nextAll('.pagination') : obj.parent().nextAll('.pagination');
  454. if(pagination.length!=0) option.page = $('li.active',pagination).attr('data-value');
  455. tpl = tpl.replace(/(data-style)/g, 'style');
  456. if(option.export)
  457. option.downloadResponse = true;
  458. //on clone l'objet option pour ne transmettre que des datas utiles
  459. data = $.extend({},option);
  460. delete data.showing;
  461. $.action(data,function(r){
  462. //On ne gere la pagination et l'affichage tableau que si on est pas en mode export
  463. if(!option.export){
  464. for(var key in r.rows){
  465. var line = $(Mustache.render(tpl,r.rows[key]));
  466. container.append(line);
  467. option.showing(line,key);
  468. }
  469. if(r.pagination){
  470. $('.page-item-previous,.page-item-next').remove();
  471. r.pagination.pages = Math.ceil(r.pagination.pages);
  472. var previewNumber = pagination.attr('data-range') != '' && pagination.attr('data-range') != null ? parseInt(pagination.attr('data-range')) : 5;
  473. previewNumber = previewNumber < 2 ? 2 : previewNumber;
  474. var template = pagination.find('li:not(:visible)');
  475. if(template.length>0 ){
  476. template = template.get(0).outerHTML;
  477. $('li:not(:eq(0))',pagination).remove();
  478. var current = parseInt(r.pagination.current);
  479. var previousPages = current-previewNumber;
  480. var nextPages = current+previewNumber;
  481. var dropDownTpl = '<div data-toggle="dropdown"><i class="fas fa-ellipsis-h pointer"></i></div><div class="dropdown-menu pagination-select">{{#choices}}<span class="dropdown-item">{{.}}</span>{{/choices}}</div>';
  482. for(i=0;i<r.pagination.pages;i++){
  483. var li = $(Mustache.render(template,{value:i,label:i+1}));
  484. if(
  485. (previousPages < i && i < nextPages)
  486. || i==0 || i==r.pagination.pages-1
  487. ){
  488. li.removeClass('hidden');
  489. if(i==current) li.addClass('active');
  490. }else{
  491. if(i==1 || i==r.pagination.pages-2){
  492. var dotli = li.clone()
  493. var start = i==1 ? 2: current+previewNumber+1;
  494. var end = i==1 ? (current+1)-previewNumber : r.pagination.pages-1;
  495. var choices = [];
  496. for(u=start; u<end+1;u++)
  497. choices.push(u);
  498. rightDropDown = $(Mustache.render(dropDownTpl,{choices:choices}));
  499. dotli.removeClass('hidden')
  500. .attr('title','Voir les pages '+(start)+' à '+(end))
  501. .removeAttr('onclick')
  502. .css('position','relative')
  503. .addClass('page-link').html(rightDropDown);
  504. pagination.append(dotli);
  505. }
  506. }
  507. pagination.append(li);
  508. }
  509. if(current!=0){
  510. var prev = $('<li class="page-item page-item-previous"><span class="page-link" ><span><i class="fas fa-angle-left"></i></span></span></li>');
  511. pagination.prepend(prev);
  512. prev.click(function(){
  513. pagination.find('li[data-value="'+(current-1)+'"]').trigger('click');
  514. });
  515. }
  516. if(current<r.pagination.pages-1){
  517. var next = $('<li class="page-item page-item-next"><span class="page-link" ><span><i class="fas fa-angle-right"></i></span></span></li>');
  518. pagination.append(next);
  519. next.click(function(){
  520. pagination.find('li[data-value="'+(current+1)+'"]').trigger('click');
  521. });
  522. }
  523. if(r.pagination.pages>1){
  524. pagination.find('.page-item:not(.hidden):first .page-link').css('border-radius','0.25rem 0 0 0.25rem');
  525. }else{
  526. pagination.find('.page-item:not(.hidden):first .page-link').css('border-radius','0.25rem 0.25rem 0.25rem 0.25rem');
  527. }
  528. $('.pagination-select span',pagination).click(function(){
  529. var selectedPage = (parseInt($(this).text())-1);
  530. pagination.find('li[data-value="'+selectedPage+'"]').trigger('click');
  531. });
  532. }
  533. }
  534. }
  535. if(preloader) preloader.remove();
  536. if(callback!=null)callback(r);
  537. },null,function(percent,type){
  538. if(progress) progress(percent,type);
  539. });
  540. });
  541. },
  542. filters: function (values){
  543. var obj = $(this);
  544. var box = obj.next('.filter-box');
  545. if(values){
  546. if(values.keyword && values.keyword!='')
  547. $('.filter-keyword',box).val(values.keyword);
  548. for(var key in values.advanced){
  549. var filter = values.advanced[key];
  550. filter_add($('.filter-add-button',box),filter);
  551. }
  552. return;
  553. }
  554. var formData = {
  555. keyword : $('.filter-keyword',box).val()
  556. };
  557. formData.advanced = [], formData.multiple = [], formData.custom = {};
  558. $('.filterRow:visible',box).each(function(i,row){
  559. var row = $(row);
  560. if(row.find('.filter-column').val()=='') return;
  561. var filter = {
  562. join : row.find('.filter-join').val(),
  563. column : row.find('.filter-column').val(),
  564. type : row.find('.filter-column option:selected').attr('data-filter-type'),
  565. operator : {},
  566. value : {}
  567. }
  568. if ($('.filter-value-block',row).length > 1) {
  569. $('.filter-value-block',row).each(function(j,block){
  570. var block = $(block);
  571. filter.operator[j] = block.find('.filter-operator').val();
  572. filter.value[j] = block.find('.filter-value').last().val();
  573. });
  574. formData.multiple.push(filter);
  575. } else {
  576. filter.operator = row.find('.filter-operator').val();
  577. values = row.find('.filter-value');
  578. if(values.length > 1){
  579. filter.value = [];
  580. for(var j=0; j<values.length; ++j){
  581. filter.value.push(values.eq(j).val());
  582. }
  583. values.eq(j).attr('data-custom') == 'true' ? formData.custom[filter.column] = filter : formData.advanced.push(filter);
  584. } else {
  585. filter.value = values.val();
  586. values.last().attr('data-custom') == 'true' ? formData.custom[filter.column] = filter : formData.advanced.push(filter);
  587. }
  588. }
  589. });
  590. return formData;
  591. },
  592. enter: function (option){
  593. return this.each(function() {
  594. var obj = $(this);
  595. obj.keydown(function(event){
  596. if(event.keyCode == 13){
  597. option();
  598. return false;
  599. }
  600. });
  601. });
  602. },
  603. sortable_table: function (option){
  604. if(option=='get'){
  605. var obj = $(this);
  606. var response = {};
  607. obj.find('thead th[data-sortable]').each(function(i,th){
  608. var th = $(th);
  609. if(th.attr('data-sort') && th.attr('data-sort')!=""){
  610. response = {
  611. sort : th.attr('data-sort'),
  612. sortable : th.attr('data-sortable')
  613. };
  614. }
  615. });
  616. return response;
  617. }
  618. return this.each(function() {
  619. var obj = $(this);
  620. obj.find('thead th[data-sortable]').click(function(){
  621. var th = $(this);
  622. var data = th.data();
  623. if(!data.sort || data.sort==''){
  624. data.sort = 'asc'
  625. }else if(data.sort == 'asc'){
  626. data.sort ='desc'
  627. }else{
  628. data.sort = '';
  629. }
  630. obj.find('thead th').removeClass('sort-asc').removeClass('sort-desc').removeAttr('data-sort');
  631. if(data.sort!='') th.addClass('sort-'+data.sort);
  632. th.attr('data-sort',data.sort);
  633. if(option.onSort) option.onSort();
  634. });
  635. });
  636. },
  637. autocomplete: function(o){
  638. return this.each(function() {
  639. var obj = $(this);
  640. if(o == 'off') return obj.typeahead('destroy');
  641. obj.attr('autocomplete','off');
  642. var option = obj.data();
  643. option = $.extend(option,o);
  644. obj.typeahead({
  645. items: (o.items) ? o.items : 5,
  646. minLength: o.suggest ? 0 : (o.minLength ? o.minLength : 2) ,
  647. fitToElement: false,
  648. autoSelect : false,
  649. selectOnBlur : false,
  650. displayText : function(item){
  651. return (o.skin) ? o.skin(item) : item.name || item;
  652. },
  653. source: function(keyword, response){
  654. if(o.onKeypress) o.onKeypress(obj);
  655. if(o.dynamicData) o.data = $.extend(o.data,o.dynamicData());
  656. $.action({
  657. action: option.action,
  658. keyword: obj.val(),
  659. data: o.data
  660. },function(r){
  661. if(r.rows != null)
  662. response(r.rows);
  663. });
  664. },
  665. highlighter: function(item){
  666. if(o.highlight) {
  667. return o.highlight(item)
  668. }
  669. else {
  670. var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
  671. return item.replace(new RegExp('('+query+')', 'ig'), function($1, match){
  672. return '<strong>'+match+'</strong>';
  673. });
  674. }
  675. },
  676. matcher: function(r){
  677. if(obj.val() == r.name)
  678. if(o.onClick)
  679. o.onClick(r,obj);
  680. return '<div>'+r.name+'</div>';
  681. },
  682. afterSelect: function(item) {
  683. obj.data('selected',true);
  684. if(o.onClick) o.onClick(item,obj);
  685. obj.trigger("change");
  686. }
  687. }).blur(function(){
  688. if(o.onBlur) o.onBlur(obj);
  689. });
  690. if(o.suggest){
  691. obj.on("click", function () {
  692. ev = $.Event("keydown");
  693. ev.keyCode = ev.which = 40;
  694. $(this).trigger(ev);
  695. return true
  696. });
  697. }
  698. obj.data('typeahead').next = function (event) {
  699. var active = this.$menu.find('.active').removeClass('active');
  700. var next = active.next();
  701. if (!next.length) {
  702. next = $(this.$menu.find($(this.options.item || this.theme.item).prop('tagName'))[0]);
  703. }
  704. while (next.hasClass('divider') || next.hasClass('dropdown-header')) {
  705. next = next.next();
  706. }
  707. next.addClass('active');
  708. var newVal = this.updater(next.data('value'));
  709. this.$element.val(newVal.name || newVal);
  710. };
  711. obj.data('typeahead').prev = function (event) {
  712. var active = this.$menu.find('.active').removeClass('active');
  713. var prev = active.prev();
  714. if (!prev.length){
  715. prev = this.$menu.find($(this.options.item || this.theme.item).prop('tagName')).last();
  716. }
  717. while (prev.hasClass('divider') || prev.hasClass('dropdown-header')) {
  718. prev = prev.prev();
  719. }
  720. prev.addClass('active');
  721. var newVal = this.updater(prev.data('value'));
  722. if (this.changeInputOnMove) this.$element.val(newVal.name || newVal);
  723. };
  724. });
  725. },
  726. location: function (options){
  727. return this.each(function() {
  728. try{
  729. //var options = $.extend({},options);
  730. var obj = $(this);
  731. obj.attr('autocomplete','off');
  732. var data = $('#algolia').data();
  733. var placesAutocomplete = places({
  734. appId: data.api,
  735. apiKey: data.key,
  736. container: obj.get(0)
  737. });
  738. placesAutocomplete.on('change', function(e) {
  739. var m = /[0-9]*/m.exec(e.suggestion.name)
  740. var infos = {
  741. latitude: e.suggestion.latlng.lat,
  742. longitude : e.suggestion.lng,
  743. types : e.suggestion.type,
  744. number : '',
  745. street : e.suggestion.name,
  746. city : e.suggestion.city,
  747. country : e.suggestion.country,
  748. code : e.suggestion.postcode,
  749. picture : '',
  750. map : ''
  751. };
  752. if(m && m.length > 0 ) infos.number = m[0] ;
  753. infos.address = (infos.street && infos.street.length && infos.number && infos.number.length) ? infos.number+' '+infos.street : (infos.street ? infos.street : '');
  754. if(options.select) options.select(infos);
  755. });
  756. }catch(e){
  757. console.warn('Composant Location en erreur, désactivé :'+e);
  758. }
  759. });
  760. },
  761. date: function (options){
  762. return this.each(function() {
  763. var obj = $(this);
  764. obj.on('paste', function(e){
  765. is_valid_date(e.originalEvent.clipboardData.getData('Text')) ? obj.val("") : e.preventDefault();
  766. });
  767. obj.datepicker({
  768. dateFormat: options.dateFormat,
  769. dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
  770. dayNamesMin: ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
  771. dayNamesShort: ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"],
  772. monthNames: ["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Décembre"],
  773. monthNamesShort: ["Jan","Fév","Mars","Avr","Mai","Juin","Juil","Aout","Sept","Oct","Nov","Déc"],
  774. firstDay: 1,
  775. minDate: options.beginDate,
  776. maxDate: options.endDate,
  777. changeMonth: true,
  778. yearRange: "-30:+30",
  779. changeYear: true,
  780. onSelect: function(dateText, inst) {
  781. obj.trigger("blur");
  782. }
  783. }).keypress(function(event){
  784. var length = obj.val().length;
  785. if(length == 2 || length == 5) obj.val(obj.val()+'/');
  786. }).blur(function(event){
  787. obj.removeClass('border border-danger');
  788. if(obj.val()=='') return;
  789. var segments = obj.val().split('/');
  790. if(segments.length!=3) return;
  791. if(segments[0] > 31 || segments[1] > 12) obj.addClass('border border-danger');
  792. }).attr('maxlength','10');
  793. obj.attr('placeholder',options.placeholder);
  794. obj.attr('pattern',"^(\\d{2}(?:\\d{2})?)\/(\d{2})\/(\\d{2}(?:\\d{2})?)$");
  795. obj.attr('title','Format '+options.dateFormat);
  796. });
  797. },
  798. hour: function (options){
  799. return this.each(function() {
  800. var obj = $(this);
  801. obj.on('paste', function(e){
  802. is_valid_hour(e.originalEvent.clipboardData.getData('Text')) ? obj.val("") : e.preventDefault();
  803. });
  804. obj.timepicker({
  805. scrollDefault: 'now',
  806. timeFormat: options.timeFormat,
  807. step: options.step,
  808. onSelect: function(timeText, inst) {
  809. obj.trigger("blur");
  810. }
  811. }).keypress(function(e) {
  812. if(!e.key.match(/[0-9:]/)) e.preventDefault();
  813. if(obj.val().length == 2) obj.val(obj.val()+':');
  814. }).blur(function(event){
  815. obj.removeClass('border border-danger')
  816. if(obj.val()=='') return;
  817. var segments = obj.val().split(':');
  818. if(segments.length!=2) return;
  819. if(segments[0] > 23 || segments[1] > 59) obj.addClass('border border-danger');
  820. }).attr('maxlength','5');
  821. obj.attr("placeholder",options.placeholder);
  822. obj.attr('pattern',"(\\d{2})\\:(\\d{2})");
  823. obj.attr('title',"Format hh:mm");
  824. });
  825. },
  826. raiseNumber: function(from,to) {
  827. return this.each(function(){
  828. var obj = $(this);
  829. obj.text(from);
  830. var interval = 800 / (to - from);
  831. var interval = setInterval(function(){
  832. var number = parseFloat(obj.text());
  833. if(number>=to) {
  834. clearInterval(interval);
  835. return;
  836. }
  837. obj.text(number+1);
  838. },interval);
  839. });
  840. }
  841. });