var wikiEditor; //CHARGEMENT DE LA PAGE function init_plugin_wiki(){ wiki_category_search(function(){ var page = $.urlParam('page'); var category = $.urlParam('category'); if(page && page!=''){ wiki_category_open(category,true,function(){ wiki_page_open(category,page); }); } else if(category && category!=''){ wiki_category_open(category); } else{ wiki_page_home(); } }); $(document).ready(function(e){ //Champ recherche $('.wiki_search_item input').blur(function(){ $('.wiki_search_item span').show().animate({opacity: '1'}, 150); $(this).animate({opacity: '0'}, 150).hide(); }).keyup(function(e){ if(e.keyCode != 13) return; wiki_search_item($(this)); }); //Smooth scrolling pour sommaire var docRoot = $('html, body'); $('#wiki-summary').on('click', 'a[href^="#"]',function(e) { var href = $.attr(this, 'href'); docRoot.animate({ scrollTop: $(href).offset().top }, 500, function () { window.location.hash = href; }); return false; }); //Night mode if($('#night-mode-check').attr('checked') && $('#night-mode-check').attr('checked').length) $('html.module-wiki').addClass('night-mode'); }); wiki_document_upload(); //Récuperation des images en presse papier et upload window.addEventListener("paste", function(event){ var items = event.clipboardData.items; if(event.clipboardData == false || event.clipboardData.items == undefined) return; var items = event.clipboardData.items; if( items.length == 0) return; var ajaxData = new FormData(); for (var i = 0; i < items.length; i++) { if (items[i].type.indexOf("image") == -1) continue; var file = items[i].getAsFile(); ajaxData.append('file', file); } $.ajax({ url: 'action.php?action=wiki_file_upload', type: 'POST', data: ajaxData, dataType: 'json', cache: false, contentType: false, processData: false, complete: function (data) {}, success: function (response) { if(response.error) return $.message('error',response.error,0); var value = wikiEditor.value(); for(var k in response.rows){ var file = response.rows[k]; value += "\n"+file.tag; } wikiEditor.value(value); }, error: function (data) { $.message('error',data); } }); }, false); } function toggle_night_mode(element){ var wikiPage = $('html.module-wiki'); var data = {action: 'wiki_night_mode'}; if(element.checked){ data.nightmode = true; wikiPage.addClass('night-mode'); } else { wikiPage.removeClass('night-mode'); } $.action(data, function(r){}); } function wiki_search_item(input){ var input = $(input); // if(!input.val().length) return $('.search-title').length ? $('.category-recent').html('') : $('#editor').html(''); $('.wiki-preloader').addClass('show'); $.action({ action : 'wiki_page_search', term : input.val(), },function(r){ $('.wiki-preloader').removeClass('show'); $('#editor').html(r.content); }); } //Suppression d'un tag liée à la recherche en cours function wiki_tag_delete(element){ var tag = $(element).closest('span.tag-item'); // tag.remove(); var searchInput = $('.wiki_search_item input'); var tagKeyword = tag.text().trim(); var keywords = searchInput.val(); searchInput.val(keywords.replace(tagKeyword, '').trim()); wiki_search_item(searchInput); } function wiki_change_url(category,page){ var url = window.location.pathname+'?module=wiki'; if(category) { url +='&category='+category; $('#wiki-categories .category').removeClass('category-open'); $('#wiki-categories .category[data-category="'+category+'"]').addClass('category-open'); } if(page){ url += '&page='+page; $('#wiki-categories .page').removeClass('page-open'); $('#wiki-categories .page[data-page="'+page+'"]').addClass('page-open'); } url+= window.location.hash; window.history.replaceState(null, null, url); } //Enregistrement des configurations function wiki_setting_save(){ var data = {} data.action = 'wiki_setting_save'; data.fields = $('#wiki-setting-form').toJson(); data.logo = $('#wiki_logo')[0].files[0]; $.action(data,function(r){ $.message('success','Enregistré'); }); } //Suppression image générale application function wiki_logo_delete(element){ if(!confirm('Êtes-vous sûr de vouloir supprimer l\'image ?')) return; var imageComposer = $(element).parent().find("input[data-type='image']"); $.action({ action: 'wiki_logo_delete' }, function(r){ imageComposer.wrap('
').closest('form').get(0).reset(); imageComposer.unwrap(); $(element).next('img').attr('src', $(imageComposer).attr('data-default-src')); $(element).remove(); $.message('info', 'Image supprimée'); }); } /** COMMON **/ function wiki_modal_open(data,callback){ var modal = $('#wiki-modal'); if(modal.length==0){ var modal = $('
'); $('body').append(modal); } modal.find('.wiki-content').load('action.php?action='+data.action,data,function(){ if(callback) callback(); init_components('#wiki-modal'); modal.addClass('wiki-modal-open'); }); } function wiki_search(){ $('.wiki_search_item span').animate({opacity: '0'}, 150).hide(); $('.wiki_search_item input').show().animate({opacity: '1'}, 150).focus(); } function wiki_modal_close(){ $('#wiki-modal').removeClass('wiki-modal-open'); $('body').removeClass('modal-open'); } /** CATEGORY **/ function wiki_category_search(callback){ $('#categories').fill({ action:'wiki_category_search' },function(){ if(callback!=null) callback(); }); } function wiki_category_edit(element,event){ event.stopPropagation(); var li = $(element).closest('li'); wiki_modal_open({ action : 'wiki_category_edit', id : li.attr('data-id') },function(){ $('body').addClass('modal-open'); $('#category-form #label').focus(); }); } function wiki_category_save(){ var data = $('#category-form').toJson(); $.action(data,function(r){ $('body').removeClass('modal-open'); wiki_category_search(); wiki_modal_close(); wiki_category_open(r.slug); }); } function wiki_category_open(category,onlyTree,callback){ var tpl = $('#pageModel li').get(0).outerHTML; $.action({ action : 'wiki_category_open', category : category },function(r){ var line = $('#wiki-categories .category[data-category="'+category+'"]'); $('#wiki-categories .category').removeClass('category-open').find('li').remove(); line.addClass('category-open').removeClass('category-closed'); if(!onlyTree) $('#editor').html(r.content); var ul = line.find('ul'); ul.find('li').remove(); var html = ''; for(var key in r.pages){ var page = r.pages[key]; page.categorySlug = r.categorySlug; html+=Mustache.render(tpl,page); } ul.append(html); wiki_change_url(r.categorySlug); if(callback) callback(); }); } function wiki_category_delete(element,event){ event.stopPropagation(); if(!confirm('Êtes-vous sûr de vouloir supprimer cette catégorie ?')) return; var line = $(element).closest('li'); $.action({ action : 'wiki_category_delete', id : line.attr('data-id') },function(r){ line.remove(); $.message('info', 'Catégorie supprimée'); }); } function wiki_category_download(element,event){ var line = $(element).closest('li'); window.location = 'action.php?action=wiki_category_download&category='+line.attr('data-id'); } /** PAGE **/ function wiki_page_summary(element){ var togglerIcon = $(element); if($('#wiki-summary').hasClass('show')) { togglerIcon.removeClass('active'); $('#wiki-summary').removeClass('show'); } else { togglerIcon.addClass('active'); $('#wiki-summary').addClass('show'); } } function wiki_page_open(category,page,event,callback){ if(event) event.stopPropagation(); $.action({ action : 'wiki_page_open', category : category, page : page, },function(r){ wiki_category_open(r.categorySlug, true, function(){ var summary = $('#wiki-summary ul'); var htmlSummary = ''; $('#editor').html(r.content); $('#editor').find('h1,h2,h3,h4,h5').each(function(i,title){ var tag = title.tagName.toLowerCase(); var title = $(title); var slug = title.text().trim().toLowerCase().replace(/[^a-z]/ig,'-').replace(/[-]{2,}/ig,'-'); title.addClass('pointer').attr({ 'onclick': "wiki_copy_anchor(this);", 'id': slug }).append(''); if(parseInt(tag.replace('h',''))<5) htmlSummary += '
  • '+title.text()+'
  • '; }); summary.html(htmlSummary); wiki_change_url(r.categorySlug,r.pageSlug); if(window.location.hash!='' && $(window.location.hash).length>0) $(window.location.hash).get(0).scrollIntoView(); }); if(callback) callback(); }); } function wiki_copy_anchor(anchor){ var id = anchor.id; var url = window.location.href.replace(/#.*/ig,'')+'#'+id; var temp = $(''); $('body').append(temp); temp.val(url).select(); document.execCommand('copy'); temp.remove(); $.message('info','Adresse copiée dans le presse papier'); } function wiki_page_home(){ $.action({ action : 'wiki_page_home', },function(r){ $('#wiki-categories .category').removeClass('category-open').find('li').remove(); wiki_change_url(); $('#editor').html(r.content); }); } function wiki_page_edit(){ $('.wiki-page-content #content-text').show(); $('.wiki-page-content #content-html').hide(); var pageLabel = $('#page-label'); pageLabel.attr('contenteditable','true').addClass('show'); $('.page-option-menu').removeClass('shown'); $('.page-editor-menu').addClass('hidden'); $('.page-save-menu').removeClass('hidden'); $('.page-option-save').addClass('show'); $('#editor').addClass('edition'); wikiEditor = new SimpleMDE({ element: $("#content-text")[0], spellChecker : false, promptURLs: true, autofocus : true, showIcons: ["code", "table","heading-2","heading-3","clean-block","horizontal-rule"] }); } function wiki_page_add(event){ event.stopPropagation(); $.action({ action : 'wiki_page_save', category : $('.category-open').attr('data-id'), content : 'Mon contenu ici...', },function(r){ wiki_category_open(r.category.slug,true,function(){ wiki_page_open(r.category.slug,r.page.slug,event,function(){ wiki_page_edit(); }); }); }); } //Ajout ou modification d'élément page function wiki_page_save(){ $.action({ action : 'wiki_page_save', id : $('.wiki-breadcrumb').attr('data-page'), label : $('#page-label').text(), content : wikiEditor.value(), },function(r){ $('.page-option-save,#page-label').removeClass('show'); $('.page-option-menu').addClass('shown'); $('#editor').html(r.content).show(); $('#page-label').removeAttr('contenteditable','true'); $('#editor').removeClass('edition'); }); } //Suppression d'élement page function wiki_page_delete(element){ if(!confirm('Êtes vous sûr de vouloir supprimer cette page ?')) return; $.action({ action : 'wiki_page_delete', id : $('.wiki-breadcrumb').attr('data-page') },function(r){ $.message('info', 'Page supprimée'); wiki_category_open(r.category.slug); }); } function wiki_page_download(){ window.location = 'action.php?action=wiki_page_download&page='+$('.wiki-breadcrumb').attr('data-page'); } function wiki_document_upload(){ var form = $('#upload-button form'); var input = form.find('input[type="file"]'); var zone = $('.container-fluid'); var droppedFiles = null; var noBlink = null; input.hide(); $('#upload-button > div').click(function(e){ input.trigger('click'); e.preventDefault(); e.stopPropagation(); }); input.on('change', function (e) { if(!$('#editor').hasClass('edition')) return; if(input.attr('data-label') == 1){ //Ajout de fichiers avec libellés $("#file-modal").remove(); if(!$('#file-modal').length) { var modal = ''; $('div.document-container').after(modal); $('#file-modal:hidden').modal('show'); } $("#file-modal").on("hidden.bs.modal", function () { input.val(''); }); } else { filesNb = $(this)[0].files.length; //Sans libellés form.trigger('submit'); } }); zone.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) { e.preventDefault(); e.stopPropagation(); }) .on('dragover dragenter', function (e) { if(!$('#editor').hasClass('edition')) return; clearTimeout(noBlink); $('#drag-overlay').css('display', 'block'); zone.addClass('drag-over'); e.preventDefault(); e.stopPropagation(); }) .on('dragleave dragend drop', function (e) { if(!$('#editor').hasClass('edition')) return; noBlink = setTimeout(function(){ $('#drag-overlay').css('display', 'none'); zone.removeClass('drag-over'); },500); e.preventDefault(); e.stopPropagation(); }) .on('drop', function (e) { if(!$('#editor').hasClass('edition')) return; if(!e.originalEvent.dataTransfer) return; droppedFiles = e.originalEvent.dataTransfer.files; filesNb = droppedFiles.length; for (var i=0, f; f=droppedFiles[i]; ++i) { if(!f.type){ $.message('error', 'Impossible d\'envoyer un dossier / un élément sans extension.'); return; } if(!f.type && f.size%4096 == 0) { $.message('error', 'Impossible d\'envoyer un dossier.'); return; } } if(input.attr('data-label') == 1){ //Ajout de fichiers avec libellés $("#file-modal").remove(); if(!$('#file-modal').length) { var modal = ''; $('div.document-container').after(modal); $('#file-modal:hidden').modal('show'); } $("#file-modal").on("hidden.bs.modal", function () { input.val(''); }); } else { //Sans libellés form.trigger('submit'); } }); form.on('submit', function (e) { e.preventDefault(); if(!$('#editor').hasClass('edition')) return; var ajaxData = new FormData(); if (droppedFiles) { $.each(droppedFiles, function (i, file) { ajaxData.append(input.attr('name'), file); }); } else { ajaxData = new FormData(form.get(0)); } ajaxData.append('path', $('#file-elements').attr('data-current-path')); droppedFiles = null; //Gestion libellés de fichiers if(input.attr('data-label') == 1){ var labels = $('#file-modal input'); var labelData = {}; labels.each(function(i, label){ var label = $(label); labelData[label.attr('id')] = label.val(); }); var labelArr = JSON.stringify(labelData); ajaxData.append('labels', labelArr); } $.ajax({ url: form.attr('action'), type: form.attr('method'), data: ajaxData, dataType: 'json', cache: false, contentType: false, processData: false, complete: function (data) {}, success: function (response) { if(response.error) return $.message('error',response.error,0); var resultMsg = filesNb==1 ? '1 document envoyé' : filesNb+' documents envoyés'; var value = wikiEditor.value(); for(var k in response.rows){ var file = response.rows[k]; value += "\n"+file.tag; } wikiEditor.value(value); $.message('info',resultMsg); }, error: function (data) { $.message('error',data); } }); }); }