main.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. if(category) {
  139. url +='&category='+category;
  140. $('#wiki-categories .category').removeClass('category-open');
  141. $('#wiki-categories .category[data-category="'+category+'"]').addClass('category-open');
  142. }
  143. if(page){
  144. url += '&page='+page;
  145. $('#wiki-categories .page').removeClass('page-open');
  146. $('#wiki-categories .page[data-page="'+page+'"]').addClass('page-open');
  147. }
  148. url+= window.location.hash;
  149. window.history.replaceState(null, null, url);
  150. }
  151. //Enregistrement des configurations
  152. function wiki_setting_save(){
  153. var data = {}
  154. data.action = 'wiki_setting_save';
  155. data.fields = $('#wiki-setting-form').toJson();
  156. data.logo = $('#wiki_logo')[0].files[0];
  157. $.action(data,function(r){
  158. $.message('success','Enregistré');
  159. });
  160. }
  161. //Suppression image générale application
  162. function wiki_logo_delete(element){
  163. if(!confirm('Êtes-vous sûr de vouloir supprimer l\'image ?')) return;
  164. var imageComposer = $(element).parent().find("input[data-type='image']");
  165. $.action({
  166. action: 'wiki_logo_delete'
  167. }, function(r){
  168. imageComposer.wrap('<form>').closest('form').get(0).reset();
  169. imageComposer.unwrap();
  170. $(element).next('img').attr('src', $(imageComposer).attr('data-default-src'));
  171. $(element).remove();
  172. $.message('info', 'Image supprimée');
  173. });
  174. }
  175. /** COMMON **/
  176. function wiki_modal_open(data,callback){
  177. var modal = $('#wiki-modal');
  178. if(modal.length==0){
  179. 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>');
  180. $('body').append(modal);
  181. }
  182. modal.find('.wiki-content').load('action.php?action='+data.action,data,function(){
  183. if(callback) callback();
  184. init_components('#wiki-modal');
  185. modal.addClass('wiki-modal-open');
  186. });
  187. }
  188. function wiki_search(){
  189. var search = $('.wiki_search_item');
  190. if($('input', search).is(':visible')) return;
  191. $('span', search).animate({opacity: '0'}, 100).addClass('hidden');
  192. $('input', search).val('').removeClass('hidden').animate({opacity: '1'}, 100).focus();
  193. }
  194. function wiki_modal_close(){
  195. $('#wiki-modal').removeClass('wiki-modal-open');
  196. $('body').removeClass('modal-open');
  197. }
  198. /** CATEGORY **/
  199. function wiki_category_search(callback){
  200. $('#categories').fill({
  201. action:'wiki_category_search'
  202. },function(){
  203. if(callback!=null) callback();
  204. });
  205. }
  206. function wiki_category_edit(element,event){
  207. event.stopPropagation();
  208. var li = $(element).closest('li');
  209. wiki_modal_open({
  210. action : 'wiki_category_edit',
  211. id : li.attr('data-id')
  212. },function(){
  213. $('body').addClass('modal-open');
  214. $('#category-form #label').focus();
  215. });
  216. }
  217. function wiki_category_save(){
  218. var data = $('#category-form').toJson();
  219. $.action(data,function(r){
  220. $('body').removeClass('modal-open');
  221. wiki_category_search();
  222. wiki_modal_close();
  223. wiki_category_open(r.slug);
  224. });
  225. }
  226. function wiki_category_open(category,onlyTree,callback){
  227. if(inEdition && !confirm('Êtes vous sûr de vouloir quitter la page sans sauvegarder ?')) return;
  228. var tpl = $('#pageModel li').get(0).outerHTML;
  229. $.action({
  230. action : 'wiki_category_open',
  231. category : category
  232. },function(r){
  233. var line = $('#wiki-categories .category[data-category="'+category+'"]');
  234. $('#wiki-categories .category').removeClass('category-open').find('li').remove();
  235. line.addClass('category-open').removeClass('category-closed');
  236. $('#wiki-categories .category > ul').sortable({
  237. axis: "y",
  238. distance: 10,
  239. update: function( event, ui ) {
  240. var sort = [];
  241. //tri d'une page au sein d'une catégorie
  242. $('.page',this).each(function(i,element){
  243. sort.push($(element).attr('data-id'));
  244. });
  245. $.action({
  246. action : 'wiki_page_sort',
  247. sort : sort
  248. });
  249. }
  250. });
  251. $( "#wiki-categories .category" ).droppable({
  252. classes: {
  253. "ui-droppable-hover": "wiki-category-hover"
  254. },
  255. tolerance : 'fit',
  256. drop: function( event, ui ) {
  257. var category = $(this);
  258. var categorySlug= $(this).attr('data-category');
  259. var page = ui.draggable;
  260. if(isNaN(category.attr('data-id')) || isNaN(page.attr('data-id'))) return;
  261. //si on drop dans la catégorie dans laquelle la page est déja, on ne fait rien
  262. if(page.attr('data-category') == categorySlug) return;
  263. //Déplacement dans une autre catégorie
  264. $.action({
  265. action : 'wiki_page_move',
  266. category : category.attr('data-id'),
  267. page : page.attr('data-id'),
  268. },function(r){
  269. wiki_category_open(categorySlug,null,function(){
  270. $('[data-id="'+page.attr('data-id')+'"]',category).trigger('click');
  271. });
  272. var liPage = ui.draggable.remove();
  273. });
  274. }
  275. });
  276. if(!onlyTree) $('#editor').html(r.content);
  277. var ul = line.find('ul');
  278. ul.find('li').remove();
  279. var html = '';
  280. for(var key in r.pages){
  281. var page = r.pages[key];
  282. page.categorySlug = r.categorySlug;
  283. html+=Mustache.render(tpl,page);
  284. }
  285. ul.append(html);
  286. wiki_change_url(r.categorySlug);
  287. if(callback) callback();
  288. });
  289. }
  290. function wiki_category_delete(element,event){
  291. event.stopPropagation();
  292. if(!confirm('Êtes-vous sûr de vouloir supprimer cette catégorie ?')) return;
  293. var line = $(element).closest('li');
  294. $.action({
  295. action : 'wiki_category_delete',
  296. id : line.attr('data-id')
  297. },function(r){
  298. line.remove();
  299. $.message('info', 'Catégorie supprimée');
  300. });
  301. }
  302. function wiki_category_download(element,event){
  303. var line = $(element).closest('li');
  304. window.location = 'action.php?action=wiki_category_download&category='+line.attr('data-id');
  305. }
  306. /** PAGE **/
  307. function wiki_page_summary(element){
  308. var togglerIcon = $(element);
  309. wiki_page_summary_refresh();
  310. if($('#wiki-summary').hasClass('show')) {
  311. togglerIcon.removeClass('active');
  312. $('#wiki-summary').removeClass('show');
  313. } else {
  314. togglerIcon.addClass('active');
  315. $('#wiki-summary').addClass('show');
  316. }
  317. }
  318. function wiki_page_summary_refresh(){
  319. var summary = $('#wiki-summary ul');
  320. var htmlSummary = '';
  321. $('#editor').find('h1,h2,h3,h4,h5').each(function(i,title){
  322. var tag = title.tagName.toLowerCase();
  323. var title = $(title);
  324. var slug = title.text().trim().toLowerCase().replace(/[^a-z0-9]/ig,'-').replace(/[-]{2,}/ig,'-');
  325. title.find('.wiki-title-link').remove();
  326. var link = $('<i class="fas fa-link wiki-title-link"></i>');
  327. title.attr('id', slug).append(link);
  328. link
  329. .addClass('pointer')
  330. .attr('onclick' , "wiki_copy_anchor(this);")
  331. if(parseInt(tag.replace('h',''))<5) htmlSummary += '<li class="summary-'+tag+'"><a href="#'+slug+'">'+title.text()+'</a></li>';
  332. });
  333. summary.html(htmlSummary);
  334. }
  335. function wiki_page_open(category,page,event,callback){
  336. if(event) event.stopPropagation();
  337. if(inEdition && !confirm('Êtes vous sûr de vouloir quitter la page sans sauvegarder ?')) return;
  338. $.action({
  339. action : 'wiki_page_open',
  340. category : category,
  341. page : page,
  342. },function(r){
  343. wiki_category_open(r.categorySlug, true, function(){
  344. $('#editor').html(r.content);
  345. wiki_page_summary_refresh();
  346. wiki_change_url(r.categorySlug,r.pageSlug);
  347. if(window.location.hash!='' && $(window.location.hash).length>0)
  348. $(window.location.hash).get(0).scrollIntoView();
  349. if(callback) callback();
  350. });
  351. });
  352. }
  353. function wiki_copy_anchor(anchor){
  354. var title = $(anchor).parent();
  355. var id = title.attr('id');
  356. var url = window.location.href.replace(/#.*/ig,'')+'#'+id;
  357. var temp = $('<input>');
  358. $('body').append(temp);
  359. temp.val(url).select();
  360. document.execCommand('copy');
  361. temp.remove();
  362. $.message('info','Adresse copiée dans le presse papier');
  363. }
  364. function wiki_page_home(){
  365. $.action({
  366. action : 'wiki_page_home',
  367. },function(r){
  368. $('#wiki-categories .category').removeClass('category-open').find('li').remove();
  369. wiki_change_url();
  370. $('#editor').html(r.content);
  371. });
  372. }
  373. function wiki_page_edit(callback){
  374. $('.wiki-page-content #content-text').removeClass('hidden');
  375. $('.wiki-page-content #content-html').addClass('hidden');
  376. var pageLabel = $('#page-label');
  377. pageLabel.attr('contenteditable','true').addClass('show');
  378. pageLabel.keydown(function(e){
  379. if(e.keyCode == 13) return false;
  380. });
  381. $('.page-option-menu').removeClass('shown');
  382. $('.page-editor-menu').addClass('hidden');
  383. $('.page-save-menu').removeClass('hidden');
  384. $('.page-option-save').addClass('show');
  385. inEdition = true;
  386. $('#editor').addClass('edition');
  387. wikiEditor = new SimpleMDE({
  388. element: $("#content-text")[0],
  389. spellChecker : false,
  390. promptURLs: true,
  391. autofocus : true,
  392. toolbar : [
  393. 'bold',"italic",{
  394. 'name' : 'underline',
  395. action: function customFunction(editor){
  396. var selected = editor.codemirror.getSelection();
  397. var newValue = '';
  398. if(selected.match(/^__.*__$/gi)){
  399. newValue = selected.replace(/^__(.*)__$/gi,"$1");
  400. }else{
  401. newValue = "__"+selected+"__";
  402. }
  403. editor.codemirror.replaceSelection(newValue);
  404. },
  405. className: "fas fa-underline",
  406. },"heading-1","heading-2","heading-3","|", "unordered-list", "ordered-list","code","link","image","quote","table","clean-block","horizontal-rule","|","preview","side-by-side","fullscreen"
  407. ],
  408. //showIcons: ["code", "table","heading-2","heading-3","clean-block","horizontal-rule"]
  409. });
  410. if(callback) callback();
  411. }
  412. function wiki_page_add(element, event){
  413. event.stopPropagation();
  414. var category = !element ? $('.category-open').attr('data-id') : $(element).closest('li.category').attr('data-id');
  415. $.action({
  416. action : 'wiki_page_save',
  417. category : category,
  418. content : 'Mon contenu ici...',
  419. },function(r){
  420. wiki_page_open(r.category.slug,r.page.slug,event,function(){
  421. wiki_page_edit(function(){
  422. $('.html.module-wiki #editor.edition .CodeMirror').click();
  423. });
  424. });
  425. });
  426. }
  427. //Ajout ou modification d'élément page
  428. function wiki_page_save(){
  429. var pageId = $('.wiki-breadcrumb').attr('data-page');
  430. $.action({
  431. action : 'wiki_page_save',
  432. id : pageId,
  433. label : $('#page-label').text(),
  434. content : wikiEditor.value(),
  435. },function(r){
  436. var editor = $('#editor');
  437. var currentPage = $('#categories li.category-open li[data-id="'+pageId+'"].page-open');
  438. $('.page-option-save, #page-label').removeClass('show');
  439. $('.page-option-menu').addClass('shown');
  440. editor.html(r.content).removeClass('hidden');
  441. $('#page-label').removeAttr('contenteditable','true');
  442. editor.removeClass('edition');
  443. currentPage.html(currentPage.children().get(0).outerHTML+' '+r.page.label).attr('data-page', r.page.slug);
  444. wiki_change_url(r.category.slug, r.page.slug);
  445. wiki_page_summary_refresh();
  446. inEdition = false;
  447. });
  448. }
  449. //Suppression d'élement page
  450. function wiki_page_delete(element){
  451. if(!confirm('Êtes vous sûr de vouloir supprimer cette page ?')) return;
  452. $.action({
  453. action : 'wiki_page_delete',
  454. id : $('.wiki-breadcrumb').attr('data-page')
  455. },function(r){
  456. $.message('info', 'Page supprimée');
  457. wiki_category_open(r.category.slug);
  458. });
  459. }
  460. function wiki_page_download(){
  461. window.location = 'action.php?action=wiki_page_download&page='+$('.wiki-breadcrumb').attr('data-page');
  462. }
  463. function wiki_document_upload(){
  464. var form = $('#upload-button form');
  465. var input = form.find('input[type="file"]');
  466. var zone = $('.container-fluid');
  467. var droppedFiles = null;
  468. var noBlink = null;
  469. input.addClass('hidden');
  470. $('#upload-button > div').click(function(e){
  471. input.trigger('click');
  472. e.preventDefault();
  473. e.stopPropagation();
  474. });
  475. input.on('change', function (e) {
  476. if(!$('#editor').hasClass('edition')) return;
  477. if(input.attr('data-label') == 1){
  478. //Ajout de fichiers avec libellés
  479. $("#file-modal").remove();
  480. if(!$('#file-modal').length) {
  481. 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">';
  482. var tmpData = new FormData(form.get(0));
  483. //Obligé de passer par l'event pour IE
  484. if(!e.target.files) return;
  485. $(e.target.files).each(function(index, value) {
  486. 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>';
  487. });
  488. 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>';
  489. $('div.document-container').after(modal);
  490. $('#file-modal:hidden').modal('show');
  491. }
  492. $("#file-modal").on("hidden.bs.modal", function () {
  493. input.val('');
  494. });
  495. } else {
  496. filesNb = $(this)[0].files.length;
  497. //Sans libellés
  498. form.trigger('submit');
  499. }
  500. });
  501. zone.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  502. e.preventDefault();
  503. e.stopPropagation();
  504. })
  505. .on('dragover dragenter', function (e) {
  506. if(!$('#editor').hasClass('edition')) return;
  507. clearTimeout(noBlink);
  508. $('#drag-overlay').css('display', 'block');
  509. zone.addClass('drag-over');
  510. e.preventDefault();
  511. e.stopPropagation();
  512. })
  513. .on('dragleave dragend drop', function (e) {
  514. if(!$('#editor').hasClass('edition')) return;
  515. noBlink = setTimeout(function(){
  516. $('#drag-overlay').css('display', 'none');
  517. zone.removeClass('drag-over');
  518. },500);
  519. e.preventDefault();
  520. e.stopPropagation();
  521. })
  522. .on('drop', function (e) {
  523. if(!$('#editor').hasClass('edition')) return;
  524. if(!e.originalEvent.dataTransfer) return;
  525. droppedFiles = e.originalEvent.dataTransfer.files;
  526. filesNb = droppedFiles.length;
  527. for (var i=0, f; f=droppedFiles[i]; ++i) {
  528. var ext = f.name.split('.').pop();
  529. if(!f.type && ext=='' ){
  530. $.message('error', 'Impossible d\'envoyer un dossier / un élément sans extension.');
  531. return;
  532. }
  533. if(!f.type && ext=='' && f.size%4096 == 0) {
  534. $.message('error', 'Impossible d\'envoyer un dossier.');
  535. return;
  536. }
  537. }
  538. if(input.attr('data-label') == 1){
  539. //Ajout de fichiers avec libellés
  540. $("#file-modal").remove();
  541. if(!$('#file-modal').length) {
  542. 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">';
  543. $(droppedFiles).each(function(index, value) {
  544. 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>';
  545. });
  546. 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>';
  547. $('div.document-container').after(modal);
  548. $('#file-modal:hidden').modal('show');
  549. }
  550. $("#file-modal").on("hidden.bs.modal", function () {
  551. input.val('');
  552. });
  553. } else {
  554. //Sans libellés
  555. form.trigger('submit');
  556. }
  557. });
  558. form.on('submit', function (e) {
  559. e.preventDefault();
  560. if(!$('#editor').hasClass('edition')) return;
  561. var ajaxData = new FormData();
  562. if (droppedFiles) {
  563. $.each(droppedFiles, function (i, file) {
  564. ajaxData.append(input.attr('name'), file);
  565. });
  566. } else {
  567. ajaxData = new FormData(form.get(0));
  568. }
  569. ajaxData.append('path', $('#file-elements').attr('data-current-path'));
  570. droppedFiles = null;
  571. //Gestion libellés de fichiers
  572. if(input.attr('data-label') == 1){
  573. var labels = $('#file-modal input');
  574. var labelData = {};
  575. labels.each(function(i, label){
  576. var label = $(label);
  577. labelData[label.attr('id')] = label.val();
  578. });
  579. var labelArr = JSON.stringify(labelData);
  580. ajaxData.append('labels', labelArr);
  581. }
  582. $.ajax({
  583. url: form.attr('action'),
  584. type: form.attr('method'),
  585. data: ajaxData,
  586. dataType: 'json',
  587. cache: false,
  588. contentType: false,
  589. processData: false,
  590. complete: function (data) {},
  591. success: function (response) {
  592. if(response.error) return $.message('error',response.error,0);
  593. var resultMsg = filesNb==1 ? '1 document envoyé' : filesNb+' documents envoyés';
  594. var value = wikiEditor.value();
  595. for(var k in response.rows){
  596. var file = response.rows[k];
  597. value += "\n"+file.tag;
  598. }
  599. wikiEditor.value(value);
  600. $.message('info',resultMsg);
  601. },
  602. error: function (data) {
  603. $.message('error',data);
  604. }
  605. });
  606. });
  607. }