main.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. var wikiEditor;
  2. //CHARGEMENT DE LA PAGE
  3. function init_plugin_wiki(){
  4. wiki_category_search(function(){
  5. var page = $.urlParam('page');
  6. var category = $.urlParam('category');
  7. if(page && page!=''){
  8. wiki_category_open(category,true,function(){
  9. wiki_page_open(category,page);
  10. });
  11. } else if(category && category!=''){
  12. wiki_category_open(category);
  13. } else{
  14. wiki_page_home();
  15. }
  16. });
  17. $(document).ready(function(e){
  18. //Champ recherche
  19. $('.wiki_search_item input').blur(function(){
  20. $('.wiki_search_item span').show().animate({opacity: '1'}, 150);
  21. $(this).animate({opacity: '0'}, 150).hide();
  22. }).keyup(function(e){
  23. if(e.keyCode != 13) return;
  24. wiki_search_item($(this));
  25. });
  26. //Smooth scrolling pour sommaire
  27. var docRoot = $('html, body');
  28. $('#wiki-summary').on('click', 'a[href^="#"]',function(e) {
  29. var href = $.attr(this, 'href');
  30. docRoot.animate({
  31. scrollTop: $(href).offset().top
  32. }, 500, function () {
  33. window.location.hash = href;
  34. });
  35. return false;
  36. });
  37. //Night mode
  38. if($('#night-mode-check').attr('checked') && $('#night-mode-check').attr('checked').length)
  39. $('html.module-wiki').addClass('night-mode');
  40. });
  41. wiki_document_upload();
  42. //Récuperation des images en presse papier et upload
  43. window.addEventListener("paste", function(event){
  44. var items = event.clipboardData.items;
  45. if(event.clipboardData == false || event.clipboardData.items == undefined) return;
  46. var items = event.clipboardData.items;
  47. if( items.length == 0) return;
  48. var ajaxData = new FormData();
  49. for (var i = 0; i < items.length; i++) {
  50. if (items[i].type.indexOf("image") == -1) continue;
  51. var file = items[i].getAsFile();
  52. ajaxData.append('file', file);
  53. }
  54. $.ajax({
  55. url: 'action.php?action=wiki_file_upload',
  56. type: 'POST',
  57. data: ajaxData,
  58. dataType: 'json',
  59. cache: false,
  60. contentType: false,
  61. processData: false,
  62. complete: function (data) {},
  63. success: function (response) {
  64. if(response.error) return $.message('error',response.error,0);
  65. var value = wikiEditor.value();
  66. for(var k in response.rows){
  67. var file = response.rows[k];
  68. value += "\n"+file.tag;
  69. }
  70. wikiEditor.value(value);
  71. },
  72. error: function (data) {
  73. $.message('error',data);
  74. }
  75. });
  76. }, false);
  77. }
  78. function toggle_night_mode(element){
  79. var wikiPage = $('html.module-wiki');
  80. var data = {action: 'wiki_night_mode'};
  81. if(element.checked){
  82. data.nightmode = true;
  83. wikiPage.addClass('night-mode');
  84. } else {
  85. wikiPage.removeClass('night-mode');
  86. }
  87. $.action(data, function(r){});
  88. }
  89. function wiki_search_item(input){
  90. var input = $(input);
  91. // if(!input.val().length) return
  92. $('.search-title').length ? $('.category-recent').html('') : $('#editor').html('');
  93. $('.wiki-preloader').addClass('show');
  94. $.action({
  95. action : 'wiki_page_search',
  96. term : input.val(),
  97. },function(r){
  98. $('.wiki-preloader').removeClass('show');
  99. $('#editor').html(r.content);
  100. });
  101. }
  102. //Suppression d'un tag liée à la recherche en cours
  103. function wiki_tag_delete(element){
  104. var tag = $(element).closest('span.tag-item');
  105. // tag.remove();
  106. var searchInput = $('.wiki_search_item input');
  107. var tagKeyword = tag.text().trim();
  108. var keywords = searchInput.val();
  109. searchInput.val(keywords.replace(tagKeyword, '').trim());
  110. wiki_search_item(searchInput);
  111. }
  112. function wiki_change_url(category,page){
  113. var url = window.location.pathname+'?module=wiki';
  114. if(category) {
  115. url +='&category='+category;
  116. $('#wiki-categories .category').removeClass('category-open');
  117. $('#wiki-categories .category[data-category="'+category+'"]').addClass('category-open');
  118. }
  119. if(page){
  120. url += '&page='+page;
  121. $('#wiki-categories .page').removeClass('page-open');
  122. $('#wiki-categories .page[data-page="'+page+'"]').addClass('page-open');
  123. }
  124. url+= window.location.hash;
  125. window.history.replaceState(null, null, url);
  126. }
  127. //Enregistrement des configurations
  128. function wiki_setting_save(){
  129. var data = {}
  130. data.action = 'wiki_setting_save';
  131. data.fields = $('#wiki-setting-form').toJson();
  132. data.logo = $('#wiki_logo')[0].files[0];
  133. $.action(data,function(r){
  134. $.message('success','Enregistré');
  135. });
  136. }
  137. //Suppression image générale application
  138. function wiki_logo_delete(element){
  139. if(!confirm('Êtes-vous sûr de vouloir supprimer l\'image ?')) return;
  140. var imageComposer = $(element).parent().find("input[data-type='image']");
  141. $.action({
  142. action: 'wiki_logo_delete'
  143. }, function(r){
  144. imageComposer.wrap('<form>').closest('form').get(0).reset();
  145. imageComposer.unwrap();
  146. $(element).next('img').attr('src', $(imageComposer).attr('data-default-src'));
  147. $(element).remove();
  148. $.message('info', 'Image supprimée');
  149. });
  150. }
  151. /** COMMON **/
  152. function wiki_modal_open(data,callback){
  153. var modal = $('#wiki-modal');
  154. if(modal.length==0){
  155. 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>');
  156. $('body').append(modal);
  157. }
  158. modal.find('.wiki-content').load('action.php?action='+data.action,data,function(){
  159. if(callback) callback();
  160. init_components('#wiki-modal');
  161. modal.addClass('wiki-modal-open');
  162. });
  163. }
  164. function wiki_search(){
  165. $('.wiki_search_item span').animate({opacity: '0'}, 150).hide();
  166. $('.wiki_search_item input').show().animate({opacity: '1'}, 150).focus();
  167. }
  168. function wiki_modal_close(){
  169. $('#wiki-modal').removeClass('wiki-modal-open');
  170. $('body').removeClass('modal-open');
  171. }
  172. /** CATEGORY **/
  173. function wiki_category_search(callback){
  174. $('#categories').fill({
  175. action:'wiki_category_search'
  176. },function(){
  177. if(callback!=null) callback();
  178. });
  179. }
  180. function wiki_category_edit(element,event){
  181. event.stopPropagation();
  182. var li = $(element).closest('li');
  183. wiki_modal_open({
  184. action : 'wiki_category_edit',
  185. id : li.attr('data-id')
  186. },function(){
  187. $('body').addClass('modal-open');
  188. $('#category-form #label').focus();
  189. });
  190. }
  191. function wiki_category_save(){
  192. var data = $('#category-form').toJson();
  193. $.action(data,function(r){
  194. $('body').removeClass('modal-open');
  195. wiki_category_search();
  196. wiki_modal_close();
  197. wiki_category_open(r.slug);
  198. });
  199. }
  200. function wiki_category_open(category,onlyTree,callback){
  201. var tpl = $('#pageModel li').get(0).outerHTML;
  202. $.action({
  203. action : 'wiki_category_open',
  204. category : category
  205. },function(r){
  206. var line = $('#wiki-categories .category[data-category="'+category+'"]');
  207. $('#wiki-categories .category').removeClass('category-open').find('li').remove();
  208. line.addClass('category-open').removeClass('category-closed');
  209. if(!onlyTree) $('#editor').html(r.content);
  210. var ul = line.find('ul');
  211. ul.find('li').remove();
  212. var html = '';
  213. for(var key in r.pages){
  214. var page = r.pages[key];
  215. page.categorySlug = r.categorySlug;
  216. html+=Mustache.render(tpl,page);
  217. }
  218. ul.append(html);
  219. wiki_change_url(r.categorySlug);
  220. if(callback) callback();
  221. });
  222. }
  223. function wiki_category_delete(element,event){
  224. event.stopPropagation();
  225. if(!confirm('Êtes-vous sûr de vouloir supprimer cette catégorie ?')) return;
  226. var line = $(element).closest('li');
  227. $.action({
  228. action : 'wiki_category_delete',
  229. id : line.attr('data-id')
  230. },function(r){
  231. line.remove();
  232. $.message('info', 'Catégorie supprimée');
  233. });
  234. }
  235. function wiki_category_download(element,event){
  236. var line = $(element).closest('li');
  237. window.location = 'action.php?action=wiki_category_download&category='+line.attr('data-id');
  238. }
  239. /** PAGE **/
  240. function wiki_page_summary(element){
  241. var togglerIcon = $(element);
  242. if($('#wiki-summary').hasClass('show')) {
  243. togglerIcon.removeClass('active');
  244. $('#wiki-summary').removeClass('show');
  245. } else {
  246. togglerIcon.addClass('active');
  247. $('#wiki-summary').addClass('show');
  248. }
  249. }
  250. function wiki_page_open(category,page,event,callback){
  251. if(event) event.stopPropagation();
  252. $.action({
  253. action : 'wiki_page_open',
  254. category : category,
  255. page : page,
  256. },function(r){
  257. wiki_category_open(r.categorySlug, true, function(){
  258. var summary = $('#wiki-summary ul');
  259. var htmlSummary = '';
  260. $('#editor').html(r.content);
  261. $('#editor').find('h1,h2,h3,h4,h5').each(function(i,title){
  262. var tag = title.tagName.toLowerCase();
  263. var title = $(title);
  264. var slug = title.text().trim().toLowerCase().replace(/[^a-z]/ig,'-').replace(/[-]{2,}/ig,'-');
  265. title.addClass('pointer').attr({
  266. 'onclick': "wiki_copy_anchor(this);",
  267. 'id': slug
  268. }).append('<i class="fas fa-link wiki-title-link"></i>');
  269. if(parseInt(tag.replace('h',''))<5) htmlSummary += '<li class="summary-'+tag+'"><a href="#'+slug+'">'+title.text()+'</a></li>';
  270. });
  271. summary.html(htmlSummary);
  272. wiki_change_url(r.categorySlug,r.pageSlug);
  273. if(window.location.hash!='' && $(window.location.hash).length>0)
  274. $(window.location.hash).get(0).scrollIntoView();
  275. });
  276. if(callback) callback();
  277. });
  278. }
  279. function wiki_copy_anchor(anchor){
  280. var id = anchor.id;
  281. var url = window.location.href.replace(/#.*/ig,'')+'#'+id;
  282. var temp = $('<input>');
  283. $('body').append(temp);
  284. temp.val(url).select();
  285. document.execCommand('copy');
  286. temp.remove();
  287. $.message('info','Adresse copiée dans le presse papier');
  288. }
  289. function wiki_page_home(){
  290. $.action({
  291. action : 'wiki_page_home',
  292. },function(r){
  293. $('#wiki-categories .category').removeClass('category-open').find('li').remove();
  294. wiki_change_url();
  295. $('#editor').html(r.content);
  296. });
  297. }
  298. function wiki_page_edit(){
  299. $('.wiki-page-content #content-text').show();
  300. $('.wiki-page-content #content-html').hide();
  301. var pageLabel = $('#page-label');
  302. pageLabel.attr('contenteditable','true').addClass('show');
  303. $('.page-option-menu').removeClass('shown');
  304. $('.page-editor-menu').addClass('hidden');
  305. $('.page-save-menu').removeClass('hidden');
  306. $('.page-option-save').addClass('show');
  307. $('#editor').addClass('edition');
  308. wikiEditor = new SimpleMDE({
  309. element: $("#content-text")[0],
  310. spellChecker : false,
  311. promptURLs: true,
  312. autofocus : true,
  313. showIcons: ["code", "table","heading-2","heading-3","clean-block","horizontal-rule"]
  314. });
  315. }
  316. function wiki_page_add(event){
  317. event.stopPropagation();
  318. $.action({
  319. action : 'wiki_page_save',
  320. category : $('.category-open').attr('data-id'),
  321. content : 'Mon contenu ici...',
  322. },function(r){
  323. wiki_category_open(r.category.slug,true,function(){
  324. wiki_page_open(r.category.slug,r.page.slug,event,function(){
  325. wiki_page_edit();
  326. });
  327. });
  328. });
  329. }
  330. //Ajout ou modification d'élément page
  331. function wiki_page_save(){
  332. $.action({
  333. action : 'wiki_page_save',
  334. id : $('.wiki-breadcrumb').attr('data-page'),
  335. label : $('#page-label').text(),
  336. content : wikiEditor.value(),
  337. },function(r){
  338. $('.page-option-save,#page-label').removeClass('show');
  339. $('.page-option-menu').addClass('shown');
  340. $('#editor').html(r.content).show();
  341. $('#page-label').removeAttr('contenteditable','true');
  342. $('#editor').removeClass('edition');
  343. });
  344. }
  345. //Suppression d'élement page
  346. function wiki_page_delete(element){
  347. if(!confirm('Êtes vous sûr de vouloir supprimer cette page ?')) return;
  348. $.action({
  349. action : 'wiki_page_delete',
  350. id : $('.wiki-breadcrumb').attr('data-page')
  351. },function(r){
  352. $.message('info', 'Page supprimée');
  353. wiki_category_open(r.category.slug);
  354. });
  355. }
  356. function wiki_page_download(){
  357. window.location = 'action.php?action=wiki_page_download&page='+$('.wiki-breadcrumb').attr('data-page');
  358. }
  359. function wiki_document_upload(){
  360. var form = $('#upload-button form');
  361. var input = form.find('input[type="file"]');
  362. var zone = $('.container-fluid');
  363. var droppedFiles = null;
  364. var noBlink = null;
  365. input.hide();
  366. $('#upload-button > div').click(function(e){
  367. input.trigger('click');
  368. e.preventDefault();
  369. e.stopPropagation();
  370. });
  371. input.on('change', function (e) {
  372. if(!$('#editor').hasClass('edition')) return;
  373. if(input.attr('data-label') == 1){
  374. //Ajout de fichiers avec libellés
  375. $("#file-modal").remove();
  376. if(!$('#file-modal').length) {
  377. 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">';
  378. var tmpData = new FormData(form.get(0));
  379. //Obligé de passer par l'event pour IE
  380. if(!e.target.files) return;
  381. $(e.target.files).each(function(index, value) {
  382. 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>';
  383. });
  384. 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>';
  385. $('div.document-container').after(modal);
  386. $('#file-modal:hidden').modal('show');
  387. }
  388. $("#file-modal").on("hidden.bs.modal", function () {
  389. input.val('');
  390. });
  391. } else {
  392. filesNb = $(this)[0].files.length;
  393. //Sans libellés
  394. form.trigger('submit');
  395. }
  396. });
  397. zone.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  398. e.preventDefault();
  399. e.stopPropagation();
  400. })
  401. .on('dragover dragenter', function (e) {
  402. if(!$('#editor').hasClass('edition')) return;
  403. clearTimeout(noBlink);
  404. $('#drag-overlay').css('display', 'block');
  405. zone.addClass('drag-over');
  406. e.preventDefault();
  407. e.stopPropagation();
  408. })
  409. .on('dragleave dragend drop', function (e) {
  410. if(!$('#editor').hasClass('edition')) return;
  411. noBlink = setTimeout(function(){
  412. $('#drag-overlay').css('display', 'none');
  413. zone.removeClass('drag-over');
  414. },500);
  415. e.preventDefault();
  416. e.stopPropagation();
  417. })
  418. .on('drop', function (e) {
  419. if(!$('#editor').hasClass('edition')) return;
  420. if(!e.originalEvent.dataTransfer) return;
  421. droppedFiles = e.originalEvent.dataTransfer.files;
  422. filesNb = droppedFiles.length;
  423. for (var i=0, f; f=droppedFiles[i]; ++i) {
  424. if(!f.type){
  425. $.message('error', 'Impossible d\'envoyer un dossier / un élément sans extension.');
  426. return;
  427. }
  428. if(!f.type && f.size%4096 == 0) {
  429. $.message('error', 'Impossible d\'envoyer un dossier.');
  430. return;
  431. }
  432. }
  433. if(input.attr('data-label') == 1){
  434. //Ajout de fichiers avec libellés
  435. $("#file-modal").remove();
  436. if(!$('#file-modal').length) {
  437. 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">';
  438. $(droppedFiles).each(function(index, value) {
  439. 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>';
  440. });
  441. 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>';
  442. $('div.document-container').after(modal);
  443. $('#file-modal:hidden').modal('show');
  444. }
  445. $("#file-modal").on("hidden.bs.modal", function () {
  446. input.val('');
  447. });
  448. } else {
  449. //Sans libellés
  450. form.trigger('submit');
  451. }
  452. });
  453. form.on('submit', function (e) {
  454. e.preventDefault();
  455. if(!$('#editor').hasClass('edition')) return;
  456. var ajaxData = new FormData();
  457. if (droppedFiles) {
  458. $.each(droppedFiles, function (i, file) {
  459. ajaxData.append(input.attr('name'), file);
  460. });
  461. } else {
  462. ajaxData = new FormData(form.get(0));
  463. }
  464. ajaxData.append('path', $('#file-elements').attr('data-current-path'));
  465. droppedFiles = null;
  466. //Gestion libellés de fichiers
  467. if(input.attr('data-label') == 1){
  468. var labels = $('#file-modal input');
  469. var labelData = {};
  470. labels.each(function(i, label){
  471. var label = $(label);
  472. labelData[label.attr('id')] = label.val();
  473. });
  474. var labelArr = JSON.stringify(labelData);
  475. ajaxData.append('labels', labelArr);
  476. }
  477. $.ajax({
  478. url: form.attr('action'),
  479. type: form.attr('method'),
  480. data: ajaxData,
  481. dataType: 'json',
  482. cache: false,
  483. contentType: false,
  484. processData: false,
  485. complete: function (data) {},
  486. success: function (response) {
  487. if(response.error) return $.message('error',response.error,0);
  488. var resultMsg = filesNb==1 ? '1 document envoyé' : filesNb+' documents envoyés';
  489. var value = wikiEditor.value();
  490. for(var k in response.rows){
  491. var file = response.rows[k];
  492. value += "\n"+file.tag;
  493. }
  494. wikiEditor.value(value);
  495. $.message('info',resultMsg);
  496. },
  497. error: function (data) {
  498. $.message('error',data);
  499. }
  500. });
  501. });
  502. }