main.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. var wikiEditor;
  2. var inEdition = false;
  3. //CHARGEMENT DE LA PAGE
  4. function init_plugin_wiki(){
  5. if($('#sideMenu').length) {
  6. $('#sideMenu').panelResize({
  7. minWidth : 300
  8. });
  9. }
  10. wiki_category_search(function(){
  11. var page = $.urlParam('page');
  12. var category = $.urlParam('category');
  13. if(page && page!=''){
  14. wiki_page_open(category,page);
  15. } else if(category && category!=''){
  16. wiki_category_open(category);
  17. } else{
  18. wiki_page_home();
  19. }
  20. $('#wiki-categories #categories').sortable({
  21. cursorAt: { top:15 },
  22. distance: 10,
  23. axis: "y",
  24. containment: "parent",
  25. update: function( event, ui ) {
  26. var sort = [];
  27. //tri d'une page au sein d'une catégorie
  28. $('#wiki-categories .category:visible').each(function(i,element){
  29. sort.push($(element).attr('data-id'));
  30. });
  31. $.action({
  32. action : 'wiki_category_sort',
  33. sort : sort
  34. });
  35. }
  36. });
  37. });
  38. window.onbeforeunload = function(){
  39. if(inEdition) return "Êtes-vous sûr de vouloir quitter la page sans sauvegarder ?";
  40. }
  41. //Champ recherche
  42. $('.wiki_search_item input').blur(function(){
  43. $('.wiki_search_item span').removeClass('hidden').animate({opacity: '1'}, 150);
  44. $(this).animate({opacity: '0'}, 150).addClass('hidden');
  45. }).keyup(function(e){
  46. if(e.keyCode != 13) return;
  47. wiki_search_item($(this));
  48. });
  49. //Smooth scrolling pour sommaire
  50. var editor = $('.module-wiki #editor');
  51. $('#wiki-summary').on('click', 'a[href^="#"]',function(e) {
  52. e.preventDefault();
  53. var href = $.attr(this, 'href');
  54. editor.animate({
  55. scrollTop: $(href).offset().top
  56. }, 500, function () {
  57. window.location.hash = href;
  58. });
  59. });
  60. //Night mode
  61. if($('#night-mode-check').prop('checked') == true)
  62. $('html.module-wiki').addClass('night-mode');
  63. wiki_document_upload();
  64. //Récuperation des images en presse papier et upload
  65. window.addEventListener("paste", function(event){
  66. var clipboardData = event.clipboardData || window.clipboardData;
  67. if(clipboardData.getData('Text').length) return;
  68. if(event.clipboardData == false || event.clipboardData.items == undefined) return;
  69. var items = event.clipboardData.items;
  70. if(items.length == 0) return;
  71. var ajaxData = new FormData();
  72. for (var i = 0; i < items.length; i++) {
  73. if (items[i].type.indexOf("image") == -1) continue;
  74. var file = items[i].getAsFile();
  75. ajaxData.append('file', file);
  76. }
  77. $.ajax({
  78. url: 'action.php?action=wiki_file_upload',
  79. type: 'POST',
  80. data: ajaxData,
  81. dataType: 'json',
  82. cache: false,
  83. contentType: false,
  84. processData: false,
  85. complete: function (data) {},
  86. success: function (response) {
  87. if(response.error) return $.message('error',response.error,0);
  88. var value = wikiEditor.value();
  89. for(var k in response.rows){
  90. var file = response.rows[k];
  91. value += "\n"+file.tag;
  92. }
  93. wikiEditor.value(value);
  94. },
  95. error: function (data) {
  96. $.message('error',data);
  97. }
  98. });
  99. }, false);
  100. }
  101. function toggle_night_mode(element){
  102. var wikiPage = $('html.module-wiki');
  103. var data = {action: 'wiki_night_mode'};
  104. if(element.checked){
  105. data.nightmode = true;
  106. wikiPage.addClass('night-mode');
  107. } else {
  108. wikiPage.removeClass('night-mode');
  109. }
  110. $.action(data, function(r){});
  111. }
  112. function wiki_search_item(input){
  113. if(inEdition && !confirm('Êtes vous sûr de vouloir quitter la page sans sauvegarder ?')) return;
  114. var input = $(input);
  115. // if(!input.val().length) return
  116. $('.search-title').length ? $('.category-recent').html('') : $('#editor').html('');
  117. $('.wiki-preloader').addClass('show');
  118. $.action({
  119. action : 'wiki_page_search',
  120. term : input.val(),
  121. },function(r){
  122. $('.wiki-preloader').removeClass('show');
  123. $('#editor').html(r.content);
  124. });
  125. }
  126. //Suppression d'un tag liée à la recherche en cours
  127. function wiki_tag_delete(element){
  128. var tag = $(element).closest('span.tag-item');
  129. // tag.remove();
  130. var searchInput = $('.wiki_search_item input');
  131. var tagKeyword = tag.text().trim();
  132. var keywords = searchInput.val();
  133. searchInput.val(keywords.replace(tagKeyword, '').trim());
  134. wiki_search_item(searchInput);
  135. }
  136. function wiki_change_url(category,page){
  137. var url = window.location.pathname+'?module=wiki';
  138. url = url.replace(/\/+/gi,'/');
  139. if(category) {
  140. url +='&category='+category;
  141. $('#wiki-categories .category').removeClass('category-open');
  142. $('#wiki-categories .category[data-category="'+category+'"]').addClass('category-open');
  143. }
  144. if(page){
  145. url += '&page='+page;
  146. $('#wiki-categories .page').removeClass('page-open');
  147. $('#wiki-categories .page[data-page="'+page+'"]').addClass('page-open');
  148. }
  149. url+= window.location.hash;
  150. window.history.replaceState(null, null, url);
  151. }
  152. //Enregistrement des configurations
  153. function wiki_setting_save(){
  154. var data = {}
  155. data.action = 'wiki_setting_save';
  156. data.fields = $('#wiki-setting-form').toJson();
  157. data.logo = $('#wiki_logo')[0].files[0];
  158. $.action(data,function(r){
  159. $.message('success','Enregistré');
  160. });
  161. }
  162. //Suppression image générale application
  163. function wiki_logo_delete(element){
  164. if(!confirm('Êtes-vous sûr de vouloir supprimer l\'image ?')) return;
  165. var imageComposer = $(element).parent().find("input[data-type='image']");
  166. $.action({
  167. action: 'wiki_logo_delete'
  168. }, function(r){
  169. imageComposer.wrap('<form>').closest('form').get(0).reset();
  170. imageComposer.unwrap();
  171. $(element).next('img').attr('src', $(imageComposer).attr('data-default-src'));
  172. $(element).remove();
  173. $.message('info', 'Image supprimée');
  174. });
  175. }
  176. /** COMMON **/
  177. function wiki_modal_open(data,callback){
  178. var modal = $('#wiki-modal');
  179. if(modal.length==0){
  180. var modal = $('<div id="wiki-modal" class=""><i onclick="wiki_modal_close();" class="fa fa-times close-button"></i><div class="wiki-content"></div></div>');
  181. $('body').append(modal);
  182. }
  183. modal.find('.wiki-content').load('action.php?action='+data.action,data,function(){
  184. if(callback) callback();
  185. init_components('#wiki-modal');
  186. modal.addClass('wiki-modal-open');
  187. });
  188. }
  189. function wiki_search(){
  190. var search = $('.wiki_search_item');
  191. if($('input', search).is(':visible')) return;
  192. $('span', search).animate({opacity: '0'}, 100).addClass('hidden');
  193. $('input', search).val('').removeClass('hidden').animate({opacity: '1'}, 100).focus();
  194. }
  195. function wiki_modal_close(){
  196. $('#wiki-modal').removeClass('wiki-modal-open');
  197. $('body').removeClass('modal-open');
  198. }
  199. /** CATEGORY **/
  200. function wiki_category_search(callback){
  201. $('#categories').fill({
  202. action:'wiki_category_search'
  203. },function(){
  204. if(callback!=null) callback();
  205. });
  206. }
  207. function wiki_category_edit(element,event){
  208. event.stopPropagation();
  209. var li = $(element).closest('li');
  210. wiki_modal_open({
  211. action : 'wiki_category_edit',
  212. id : li.attr('data-id')
  213. },function(){
  214. $('body').addClass('modal-open');
  215. $('#category-form #label').focus();
  216. });
  217. }
  218. function wiki_category_save(){
  219. var data = $('#category-form').toJson();
  220. $.action(data,function(r){
  221. $('body').removeClass('modal-open');
  222. wiki_category_search();
  223. wiki_modal_close();
  224. wiki_category_open(r.slug);
  225. });
  226. }
  227. function wiki_category_open(category,onlyTree,callback){
  228. if(inEdition && !confirm('Êtes vous sûr de vouloir quitter la page sans sauvegarder ?')) return;
  229. var tpl = $('#pageModel li').get(0).outerHTML;
  230. $.action({
  231. action : 'wiki_category_open',
  232. category : category
  233. },function(r){
  234. var line = $('#wiki-categories .category[data-category="'+category+'"]');
  235. $('#wiki-categories .category').removeClass('category-open').find('li').remove();
  236. line.addClass('category-open').removeClass('category-closed');
  237. $('#wiki-categories .category > ul').sortable({
  238. axis: "y",
  239. distance: 10,
  240. update: function( event, ui ) {
  241. var sort = [];
  242. //tri d'une page au sein d'une catégorie
  243. $('.page',this).each(function(i,element){
  244. sort.push($(element).attr('data-id'));
  245. });
  246. $.action({
  247. action : 'wiki_page_sort',
  248. sort : sort
  249. });
  250. }
  251. });
  252. $( "#wiki-categories .category" ).droppable({
  253. classes: {
  254. "ui-droppable-hover": "wiki-category-hover"
  255. },
  256. tolerance : 'fit',
  257. drop: function( event, ui ) {
  258. var category = $(this);
  259. var categorySlug= $(this).attr('data-category');
  260. var page = ui.draggable;
  261. if(isNaN(category.attr('data-id')) || isNaN(page.attr('data-id'))) return;
  262. //si on drop dans la catégorie dans laquelle la page est déja, on ne fait rien
  263. if(page.attr('data-category') == categorySlug) return;
  264. //Déplacement dans une autre catégorie
  265. $.action({
  266. action : 'wiki_page_move',
  267. category : category.attr('data-id'),
  268. page : page.attr('data-id'),
  269. },function(r){
  270. wiki_category_open(categorySlug,null,function(){
  271. $('[data-id="'+page.attr('data-id')+'"]',category).trigger('click');
  272. });
  273. var liPage = ui.draggable.remove();
  274. });
  275. }
  276. });
  277. if(!onlyTree) $('#editor').html(r.content);
  278. var ul = line.find('ul');
  279. ul.find('li').remove();
  280. var html = '';
  281. for(var key in r.pages){
  282. var page = r.pages[key];
  283. page.categorySlug = r.categorySlug;
  284. html+=Mustache.render(tpl,page);
  285. }
  286. ul.append(html);
  287. wiki_change_url(r.categorySlug);
  288. if(callback) callback();
  289. });
  290. }
  291. function wiki_category_delete(element,event){
  292. event.stopPropagation();
  293. if(!confirm('Êtes-vous sûr de vouloir supprimer cette catégorie ?')) return;
  294. var line = $(element).closest('li');
  295. $.action({
  296. action : 'wiki_category_delete',
  297. id : line.attr('data-id')
  298. },function(r){
  299. line.remove();
  300. $.message('info', 'Catégorie supprimée');
  301. });
  302. }
  303. function wiki_category_download(element,event){
  304. var line = $(element).closest('li');
  305. window.location = 'action.php?action=wiki_category_download&category='+line.attr('data-id');
  306. }
  307. /** PAGE **/
  308. function wiki_page_summary(element){
  309. var togglerIcon = $(element);
  310. wiki_page_summary_refresh();
  311. if($('#wiki-summary').hasClass('show')) {
  312. togglerIcon.removeClass('active');
  313. $('#wiki-summary').removeClass('show');
  314. } else {
  315. togglerIcon.addClass('active');
  316. $('#wiki-summary').addClass('show');
  317. }
  318. }
  319. function wiki_page_summary_refresh(){
  320. var summary = $('#wiki-summary ul');
  321. var htmlSummary = '';
  322. $('#editor').find('h1,h2,h3,h4,h5').each(function(i,title){
  323. var tag = title.tagName.toLowerCase();
  324. var title = $(title);
  325. var slug = title.text().trim().toLowerCase().replace(/[^a-z0-9]/ig,'-').replace(/[-]{2,}/ig,'-');
  326. title.find('.wiki-title-link').remove();
  327. var link = $('<i class="fas fa-link wiki-title-link"></i>');
  328. title.attr('id', slug).append(link);
  329. link
  330. .addClass('pointer')
  331. .attr('onclick' , "wiki_copy_anchor(this);")
  332. if(parseInt(tag.replace('h',''))<5) htmlSummary += '<li class="summary-'+tag+'"><a href="#'+slug+'">'+title.text()+'</a></li>';
  333. });
  334. summary.html(htmlSummary);
  335. }
  336. function wiki_page_open(category,page,event,callback){
  337. if(event) event.stopPropagation();
  338. if(inEdition && !confirm('Êtes vous sûr de vouloir quitter la page sans sauvegarder ?')) return;
  339. $.action({
  340. action : 'wiki_page_open',
  341. category : category,
  342. page : page,
  343. },function(r){
  344. wiki_category_open(r.categorySlug, true, function(){
  345. $('#editor').html(r.content);
  346. wiki_page_summary_refresh();
  347. wiki_change_url(r.categorySlug,r.pageSlug);
  348. if(window.location.hash!='' && $(window.location.hash).length>0)
  349. $(window.location.hash).get(0).scrollIntoView();
  350. $('.wiki-page-content .block-code').append('<div class="pointer text-light btn-copy" title="copier le code"><i class="fas fa-paste"></i></div>');
  351. $('.wiki-page-content .block-code .btn-copy').click(function(){
  352. copy_string($(this).parent().text());
  353. $.message('info','Copié !');
  354. });
  355. if(callback) callback();
  356. });
  357. });
  358. }
  359. function wiki_copy_anchor(anchor){
  360. var title = $(anchor).parent();
  361. var id = title.attr('id');
  362. var url = window.location.href.replace(/#.*/ig,'')+'#'+id;
  363. var temp = $('<input>');
  364. $('body').append(temp);
  365. temp.val(url).select();
  366. document.execCommand('copy');
  367. temp.remove();
  368. $.message('info','Adresse copiée dans le presse papier');
  369. }
  370. function wiki_page_home(){
  371. $.action({
  372. action : 'wiki_page_home',
  373. },function(r){
  374. $('#wiki-categories .category').removeClass('category-open').find('li').remove();
  375. wiki_change_url();
  376. $('#editor').html(r.content);
  377. });
  378. }
  379. function wiki_page_edit(callback){
  380. $('.wiki-page-content #content-text').removeClass('hidden');
  381. $('.wiki-page-content #content-html').addClass('hidden');
  382. var pageLabel = $('#page-label');
  383. pageLabel.attr('contenteditable','true').addClass('show');
  384. pageLabel.keydown(function(e){
  385. if(e.keyCode == 13) return false;
  386. });
  387. $('.page-option-menu').removeClass('shown');
  388. $('.page-editor-menu').addClass('hidden');
  389. $('.page-save-menu').removeClass('hidden');
  390. $('.page-option-save').addClass('show');
  391. inEdition = true;
  392. $('#editor').addClass('edition');
  393. wikiEditor = new SimpleMDE({
  394. element: $("#content-text")[0],
  395. spellChecker : false,
  396. promptURLs: true,
  397. autofocus : true,
  398. toolbar : [
  399. 'bold',"italic",{
  400. 'name' : 'underline',
  401. action: function customFunction(editor){
  402. var selected = editor.codemirror.getSelection();
  403. var newValue = '';
  404. if(selected.match(/^__.*__$/gi)){
  405. newValue = selected.replace(/^__(.*)__$/gi,"$1");
  406. }else{
  407. newValue = "__"+selected+"__";
  408. }
  409. editor.codemirror.replaceSelection(newValue);
  410. },
  411. className: "fas fa-underline",
  412. },"heading-1","heading-2","heading-3","|", "unordered-list", "ordered-list","code","link","image","quote","table","clean-block","horizontal-rule","|","preview","side-by-side","fullscreen"
  413. ],
  414. //showIcons: ["code", "table","heading-2","heading-3","clean-block","horizontal-rule"]
  415. });
  416. if(callback) callback();
  417. }
  418. function wiki_page_add(element, event){
  419. event.stopPropagation();
  420. var category = !element ? $('.category-open').attr('data-id') : $(element).closest('li.category').attr('data-id');
  421. $.action({
  422. action : 'wiki_page_save',
  423. category : category,
  424. content : 'Mon contenu ici...',
  425. },function(r){
  426. wiki_page_open(r.category.slug,r.page.slug,event,function(){
  427. wiki_page_edit(function(){
  428. $('.html.module-wiki #editor.edition .CodeMirror').click();
  429. });
  430. });
  431. });
  432. }
  433. //Ajout ou modification d'élément page
  434. function wiki_page_save(){
  435. var pageId = $('.wiki-breadcrumb').attr('data-page');
  436. $.action({
  437. action : 'wiki_page_save',
  438. id : pageId,
  439. label : $('#page-label').text(),
  440. content : wikiEditor.value(),
  441. },function(r){
  442. var editor = $('#editor');
  443. var currentPage = $('#categories li.category-open li[data-id="'+pageId+'"].page-open');
  444. $('.page-option-save, #page-label').removeClass('show');
  445. $('.page-option-menu').addClass('shown');
  446. editor.html(r.content).removeClass('hidden');
  447. $('#page-label').removeAttr('contenteditable','true');
  448. editor.removeClass('edition');
  449. currentPage.html(currentPage.children().get(0).outerHTML+' '+r.page.label).attr('data-page', r.page.slug);
  450. wiki_change_url(r.category.slug, r.page.slug);
  451. wiki_page_summary_refresh();
  452. inEdition = false;
  453. });
  454. }
  455. //Suppression d'élement page
  456. function wiki_page_delete(element){
  457. if(!confirm('Êtes vous sûr de vouloir supprimer cette page ?')) return;
  458. $.action({
  459. action : 'wiki_page_delete',
  460. id : $('.wiki-breadcrumb').attr('data-page')
  461. },function(r){
  462. $.message('info', 'Page supprimée');
  463. wiki_category_open(r.category.slug);
  464. });
  465. }
  466. function wiki_page_download(){
  467. window.location = 'action.php?action=wiki_page_download&page='+$('.wiki-breadcrumb').attr('data-page');
  468. }
  469. function wiki_document_upload(){
  470. var form = $('#upload-button form');
  471. var input = form.find('input[type="file"]');
  472. var zone = $('.container-fluid');
  473. var droppedFiles = null;
  474. var noBlink = null;
  475. input.addClass('hidden');
  476. $('#upload-button > div').click(function(e){
  477. input.trigger('click');
  478. e.preventDefault();
  479. e.stopPropagation();
  480. });
  481. input.on('change', function (e) {
  482. if(!$('#editor').hasClass('edition')) return;
  483. if(input.attr('data-label') == 1){
  484. //Ajout de fichiers avec libellés
  485. $("#file-modal").remove();
  486. if(!$('#file-modal').length) {
  487. var modal = '<div class="modal fade file-modal" id="file-modal" tabindex="-1" role="dialog" aria-labelledby="file-modal-label" aria-hidden="true"><div class="modal-dialog modal-lg" role="document"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="file-modal-label">Libellé par fichier</h5><button type="button" class="close" data-dismiss="modal" aria-label="Fermer"><span aria-hidden="true">&times;</span></button></div><div class="modal-body"><section id="file-form" class="file-form">';
  488. var tmpData = new FormData(form.get(0));
  489. //Obligé de passer par l'event pour IE
  490. if(!e.target.files) return;
  491. $(e.target.files).each(function(index, value) {
  492. modal += '<div class="form-group"><label class="form-control-label">'+value.name+'</label><input required onkeyup="if($(this).hasClass(\'label-required\')){$(this).removeClass(\'label-required\'); $(this).attr(\'placeholder\', \'Libellé\')}" type="text" value="" placeholder="Libellé" class="form-control" id="'+slugify(value.name)+'" name="'+slugify(value.name)+'"/></div>';
  493. });
  494. modal += '</section></div><div class="modal-footer"><div class="btn btn-light" data-dismiss="modal">Fermer</div><div class="btn btn-success" onclick="if(check_label_filled()){$(\'#upload-button form\').trigger(\'submit\'); $(\'#file-modal\').remove(); $(\'div.modal-backdrop\').remove(); $(\'body\').removeAttr(\'style\').removeClass(\'modal-open\'); $(\'#mainMenu, #mainMenu > button\').removeAttr(\'style\');}"><i class="fas fa-check"></i> Enregistrer</div></div></div></div></div>';
  495. $('div.document-container').after(modal);
  496. $('#file-modal:hidden').modal('show');
  497. }
  498. $("#file-modal").on("hidden.bs.modal", function () {
  499. input.val('');
  500. });
  501. } else {
  502. filesNb = $(this)[0].files.length;
  503. //Sans libellés
  504. form.trigger('submit');
  505. }
  506. });
  507. zone.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  508. e.preventDefault();
  509. e.stopPropagation();
  510. })
  511. .on('dragover dragenter', function (e) {
  512. if(!$('#editor').hasClass('edition')) return;
  513. clearTimeout(noBlink);
  514. $('#drag-overlay').css('display', 'block');
  515. zone.addClass('drag-over');
  516. e.preventDefault();
  517. e.stopPropagation();
  518. })
  519. .on('dragleave dragend drop', function (e) {
  520. if(!$('#editor').hasClass('edition')) return;
  521. noBlink = setTimeout(function(){
  522. $('#drag-overlay').css('display', 'none');
  523. zone.removeClass('drag-over');
  524. },500);
  525. e.preventDefault();
  526. e.stopPropagation();
  527. })
  528. .on('drop', function (e) {
  529. if(!$('#editor').hasClass('edition')) return;
  530. if(!e.originalEvent.dataTransfer) return;
  531. droppedFiles = e.originalEvent.dataTransfer.files;
  532. filesNb = droppedFiles.length;
  533. for (var i=0, f; f=droppedFiles[i]; ++i) {
  534. var ext = f.name.split('.').pop();
  535. if(!f.type && ext=='' ){
  536. $.message('error', 'Impossible d\'envoyer un dossier / un élément sans extension.');
  537. return;
  538. }
  539. if(!f.type && ext=='' && f.size%4096 == 0) {
  540. $.message('error', 'Impossible d\'envoyer un dossier.');
  541. return;
  542. }
  543. }
  544. if(input.attr('data-label') == 1){
  545. //Ajout de fichiers avec libellés
  546. $("#file-modal").remove();
  547. if(!$('#file-modal').length) {
  548. var modal = '<div class="modal fade file-modal" id="file-modal" tabindex="-1" role="dialog" aria-labelledby="file-modal-label" aria-hidden="true"><div class="modal-dialog modal-lg" role="document"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="file-modal-label">Libellé par fichier</h5><button type="button" class="close" data-dismiss="modal" aria-label="Fermer"><span aria-hidden="true">&times;</span></button></div><div class="modal-body"><section id="file-form" class="file-form">';
  549. $(droppedFiles).each(function(index, value) {
  550. modal += '<div class="form-group"><label class="form-control-label">'+value.name+'</label><input required onkeyup="if($(this).hasClass(\'label-required\')){$(this).removeClass(\'label-required\'); $(this).attr(\'placeholder\', \'Libellé\')}" type="text" value="" placeholder="Libellé" class="form-control" id="'+slugify(value.name)+'" name="'+slugify(value.name)+'"/></div>';
  551. });
  552. modal += '</section></div><div class="modal-footer"><div class="btn btn-light" data-dismiss="modal">Fermer</div><div class="btn btn-success" onclick="if(check_label_filled()){$(\'#upload-button form\').trigger(\'submit\'); $(\'#file-modal\').remove(); $(\'div.modal-backdrop\').remove(); $(\'body\').removeAttr(\'style\').removeClass(\'modal-open\'); $(\'#mainMenu, #mainMenu > button\').removeAttr(\'style\');}"><i class="fas fa-check"></i> Enregistrer</div></div></div></div></div>';
  553. $('div.document-container').after(modal);
  554. $('#file-modal:hidden').modal('show');
  555. }
  556. $("#file-modal").on("hidden.bs.modal", function () {
  557. input.val('');
  558. });
  559. } else {
  560. //Sans libellés
  561. form.trigger('submit');
  562. }
  563. });
  564. form.on('submit', function (e) {
  565. e.preventDefault();
  566. if(!$('#editor').hasClass('edition')) return;
  567. var ajaxData = new FormData();
  568. if (droppedFiles) {
  569. $.each(droppedFiles, function (i, file) {
  570. ajaxData.append(input.attr('name'), file);
  571. });
  572. } else {
  573. ajaxData = new FormData(form.get(0));
  574. }
  575. ajaxData.append('path', $('#file-elements').attr('data-current-path'));
  576. droppedFiles = null;
  577. //Gestion libellés de fichiers
  578. if(input.attr('data-label') == 1){
  579. var labels = $('#file-modal input');
  580. var labelData = {};
  581. labels.each(function(i, label){
  582. var label = $(label);
  583. labelData[label.attr('id')] = label.val();
  584. });
  585. var labelArr = JSON.stringify(labelData);
  586. ajaxData.append('labels', labelArr);
  587. }
  588. $.ajax({
  589. url: form.attr('action'),
  590. type: form.attr('method'),
  591. data: ajaxData,
  592. dataType: 'json',
  593. cache: false,
  594. contentType: false,
  595. processData: false,
  596. complete: function (data) {},
  597. success: function (response) {
  598. if(response.error) return $.message('error',response.error,0);
  599. var resultMsg = filesNb==1 ? '1 document envoyé' : filesNb+' documents envoyés';
  600. var value = wikiEditor.value();
  601. for(var k in response.rows){
  602. var file = response.rows[k];
  603. value += "\n"+file.tag;
  604. }
  605. wikiEditor.value(value);
  606. $.message('info',resultMsg);
  607. },
  608. error: function (data) {
  609. $.message('error',data);
  610. }
  611. });
  612. });
  613. }