document.api.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. var DocumentApi = function(element,options) {
  2. this.onEvents = {};
  3. this.currentView = 'list';
  4. this.currentFolder = '.';
  5. this.views = [];
  6. this.selected = [];
  7. this.isProcessing = false;
  8. this.editorOptions = {
  9. btns: [
  10. ['strong', 'em', 'underline','del'],
  11. ['foreColor', 'backColor'],
  12. ['undo', 'redo'], // Only supported in Blink browsers
  13. ['formatting'],
  14. ['superscript', 'subscript'],
  15. ['link'],
  16. ['insertImage'],
  17. ['table'],
  18. ['justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull'],
  19. ['unorderedList', 'orderedList'],
  20. ['horizontalRule'],
  21. ['removeformat'],
  22. ['viewHTML']
  23. ],
  24. lang: 'fr',
  25. allowBigPaste : true,
  26. autogrow: true,
  27. semantic: false
  28. };
  29. this.dom = {
  30. container : $(element),
  31. editor : null,
  32. panels : {
  33. tree : null,
  34. detail : null,
  35. search : null,
  36. breadcrumb : null,
  37. files : null,
  38. workspace : null
  39. }
  40. }
  41. this.options = {
  42. rootLabel : 'C:', //Libellé de la racine
  43. root : '', //Racine visible des fichiers, on ne peut pas aller au dessus
  44. path : null, // dossier ouvert au lancement
  45. rightEdit : true,
  46. rightDelete : true,
  47. rightPermission : true,
  48. panels : {
  49. detail : {
  50. visible : true,
  51. buttons : []
  52. },
  53. view : {
  54. visible : true,
  55. buttons : []
  56. },
  57. tree : {
  58. visible : true
  59. },
  60. files : {
  61. visible : true
  62. },
  63. workspace : {
  64. visible : true
  65. },
  66. search : {
  67. visible : true,
  68. buttons : []
  69. },
  70. breadcrumb : {
  71. visible : true
  72. }
  73. },
  74. actions : {
  75. template : 'document_load_template',
  76. element_search : 'document_element_search',
  77. element_tree_search : 'document_element_tree_search',
  78. folder_create : 'document_folder_create',
  79. element_rename : 'document_element_rename',
  80. element_delete : 'document_element_delete',
  81. element_move : 'document_element_move',
  82. element_share_edit : 'document_element_share_edit',
  83. element_preview : 'document_element_preview',
  84. element_execute : 'document_element_execute',
  85. element_upload : 'document_element_upload',
  86. element_edit : 'document_element_edit',
  87. element_save : 'document_element_save',
  88. properties_show : 'document_properties_show',
  89. right_search : 'document_right_search',
  90. right_save : 'document_right_save',
  91. right_delete : 'document_right_delete',
  92. }
  93. };
  94. if(options) this.options = $.extend(this.options,options);
  95. }
  96. DocumentApi.prototype.load = function(callbacks){
  97. this.dom.container.html('Chargement...');
  98. var element = this.element;
  99. var object = this;
  100. object.options.panels.detail.buttons.push({
  101. buttonClass : 'btn-primary btn-download',
  102. icon : 'fas fa-arrow-alt-circle-down',
  103. label : 'TÉLÉCHARGER',
  104. visibility : {
  105. 'type': ['file','directory']
  106. }
  107. });
  108. object.options.panels.detail.buttons.push({
  109. lineClass : 'properties-button directory-button',
  110. buttonClass : 'btn',
  111. icon : 'fas fa-align-justify',
  112. label : 'PROPRIÉTÉS',
  113. visibility : {
  114. 'type': ['file','directory']
  115. }
  116. });
  117. object.options.panels.search.buttons.push({
  118. lineClass : 'upload-button',
  119. buttonClass : '',
  120. icon : 'far fa-file-alt',
  121. label : 'Envoyer un fichier',
  122. afterHtml : '<form class="box" method="post" action="action.php?action='+object.options.actions.element_upload+'" enctype="multipart/form-data"><input type="file" name="file[]" multiple /></form>'
  123. });
  124. object.options.panels.search.buttons.push({
  125. lineClass : '',
  126. buttonClass : 'add-new-folder',
  127. icon : 'far fa-folder',
  128. label : 'Créer un dossier',
  129. afterHtml : '<div class="new-folder-block hidden"> \
  130. <div class="input-group input-group-sm">\
  131. <input type="text" class="form-control form-control-sm folder-name" placeholder="Nom du dossier">\
  132. <div class="input-group-append">\
  133. <span class="btn btn-success"><i class="fas fa-check"></i></span>\
  134. </div>\
  135. </div>\
  136. </div>'
  137. });
  138. object.options.panels.search.buttons.push({
  139. lineClass : 'create-txt-button',
  140. buttonClass : '',
  141. icon : 'fas fa-pen text-secondary',
  142. label : 'Créer un fichier'
  143. });
  144. if(object.options.rightPermission){
  145. object.options.panels.detail.buttons.push({
  146. lineClass : 'share-button directory-button',
  147. buttonClass : 'btn',
  148. icon : 'fas fa-share',
  149. label : 'PERMISSIONS',
  150. visibility : {
  151. 'type': ['file','directory']
  152. }
  153. });
  154. object.options.panels.detail.buttons.push({
  155. html : '<div class="right-panel hidden"><hr/>\
  156. <h6>PARTAGES</h6>\
  157. <div class="element-right-form">\
  158. <input class="uid" class="form-control" type="text" data-type="user" data-types="user,rank" placeholder="Rang ou utilisateur..." /><hr/>\
  159. <label><input class="read" type="checkbox" data-type="checkbox"> Lecture</label>\
  160. <label><input class="edit" type="checkbox" data-type="checkbox"> Écriture</label>\
  161. <label><input class="recursive" type="checkbox" data-type="checkbox"> Récursif</label>\
  162. <div class="btn btn-add-share btn-light"><i class="fa fa-plus"></i> Ajouter</div>\
  163. </div>\
  164. <ul class="table element-rights" >\
  165. <li data-id="{{id}}" class="hidden p-2">\
  166. {{{uid}}} <br>\
  167. <small class="text-muted p-0">\
  168. <span class="p-2">{{#read}}<i class="fa fa-check text-success"></i>{{/read}}{{^read}}<i class="fa fa-times text-secondary"></i>{{/read}} Lecture </span>\
  169. <span class="p-2">{{#edit}}<i class="fa fa-check text-success"></i>{{/edit}}{{^edit}}<i class="fa fa-times text-secondary"></i>{{/edit}} Écriture </span>\
  170. <span class="p-2">{{#recursive}}<i class="fa fa-check text-success"></i>{{/recursive}}{{^recursive}}<i class="fa fa-times text-secondary"></i>{{/recursive}} Récursif </span>\
  171. <i title="Supprimer la permission" class="fa fa-trash pointer right btn-delete-right" ></i>\
  172. </small>\
  173. </li>\
  174. </ul>\
  175. <hr/></div>',
  176. visibility : {
  177. 'type': ['file','directory']
  178. }
  179. });
  180. }
  181. if(object.options.rightDelete){
  182. object.options.panels.detail.buttons.push({
  183. buttonClass : 'btn text-danger btn-element-delete',
  184. icon : 'far fa-trash-alt',
  185. label : 'SUPPRIMER',
  186. visibility : {
  187. 'type': ['file','directory']
  188. }
  189. });
  190. }
  191. object.dom.container.load('action.php?action='+object.options.actions.template,function(){
  192. object.dom.panels = {
  193. tree : $('.tree-panel',object.dom.container),
  194. detail : $('.detail-panel',object.dom.container),
  195. search : $('.search-module',object.dom.container),
  196. breadcrumb : $('.breadcrumb-module',object.dom.container),
  197. files : $('.file-module',object.dom.container),
  198. workspace : $('.file-panel',object.dom.container)
  199. };
  200. if(object.options.root) $('.document-container').attr('data-root',object.options.root);
  201. if(object.options.panels['tree'].visible === false) object.hidePanel('tree');
  202. if(object.options.panels['detail'].visible === false) object.hidePanel('detail');
  203. if(object.options.panels['search'].visible === false) object.hidePanel('search');
  204. if(object.options.panels['files'].visible === false) object.hidePanel('files');
  205. if(object.options.panels['workspace'].visible === false) object.hidePanel('workspace');
  206. if(object.options.panels['breadcrumb'].visible === false) object.hidePanel('breadcrumb');
  207. $('.root-folder span',object.dom.panels.tree).text(object.options.rootLabel);
  208. object.views.push({
  209. uid : 'list',
  210. icon : 'fas fa-align-justify',
  211. label : 'Vue liste',
  212. html : $('.file-view[data-view="list"]').get(0).outerHTML
  213. });
  214. object.views.push({
  215. uid : 'grid',
  216. icon : 'fas fa-th-large',
  217. label : 'Vue grille',
  218. html : $('.file-view[data-view="grid"]').get(0).outerHTML
  219. });
  220. //Chargement des vues disponibles
  221. object.loadViews();
  222. //Rendu de la vue courante & Chargement des fichiers
  223. object.renderView(object.options.path);
  224. //Chargement des boutons ajouter (dossier, fichier..)
  225. $('.document-create-dropdown .dropdown-item:not(.hidden)').remove();
  226. var tpl = $('.document-create-dropdown .dropdown-item.hidden').get(0).outerHTML;
  227. for(var k in object.options.panels.search.buttons){
  228. var data = object.options.panels.search.buttons[k];
  229. var button = $(Mustache.render(tpl,data));
  230. button.removeClass('hidden');
  231. $('.document-create-dropdown').append(button);
  232. }
  233. $('.root-folder',object.dom.container).addClass('folder-focused');
  234. $(document).off('keyup').on('keyup', function(e){
  235. switch(e.keyCode){
  236. case 46:
  237. if(!$('.file-view tr').hasClass('element-focused') || !object.options.rightDelete) break;
  238. object.element_delete();
  239. break;
  240. case 113:
  241. if(!object.options.rightEdit) break;
  242. if($('.file-view tr').hasClass('element-focused'))
  243. object.element_rename_edit($('.element-rename',object.selected[0].element));
  244. break;
  245. case 13:
  246. if($('.label-search',object.dom.panels.search).is(":focus"))
  247. object.element_search();
  248. break;
  249. }
  250. });
  251. $('.file-view:visible').sortable_table({
  252. onSort : function(){object.element_search();}
  253. });
  254. //RESET KEYWORD
  255. $(".label-search").on('keyup',function(){
  256. if ($(this).val() != ''){
  257. $('.doc-search-container').addClass('typing');
  258. }else{
  259. $('.doc-search-container').removeClass('typing');
  260. }
  261. });
  262. $(".search-clear").click(function(){
  263. $('.doc-search-container').removeClass('typing');
  264. $(".label-search").val('');
  265. $(".label-search").focus();
  266. object.element_search();
  267. });
  268. $('div.file-panel').on('click',function(e){
  269. if(e.target == (this)) {
  270. $('.file-view:visible tr').removeClass('element-focused');
  271. object.reset_preview();
  272. }
  273. });
  274. $(object.dom.panels.tree).panelResize({
  275. minWidth : 100,
  276. stored : true
  277. });
  278. $(object.dom.panels.detail).panelResize({
  279. minWidth : 100,
  280. direction : 'left',
  281. stored : true
  282. });
  283. // Mapping des events DOM / objet
  284. $('.root-folder',object.dom.panels.tree).click(function(event){object.element_search('.');});
  285. $('.btn-search',object.dom.panels.search).click(function(){object.element_search();});
  286. $('.add-new-folder',object.dom.panels.search).click(function(event){ object.new_folder(event); });
  287. $('.new-folder-block .input-group-append',object.dom.panels.search).click(function(){object.folder_create()});
  288. $('.detail-buttons',object.dom.panels.detail).on('click','.btn-download',function(){object.element_download(null,true)});
  289. $('.detail-buttons',object.dom.panels.detail).on('click','.share-button',function(){object.right_toggle()});
  290. $('.detail-buttons',object.dom.panels.detail).on('click','.properties-button',function(){object.properties_show()});
  291. $('.detail-buttons',object.dom.panels.detail).on('click','.element-right-form .btn-add-share',function(){object.right_save()});
  292. $('.detail-buttons',object.dom.panels.detail).on('click','.btn-delete-right',function(){object.right_delete(this)});
  293. $('.detail-buttons',object.dom.panels.detail).on('click','.btn-element-delete',function(){object.element_delete();});
  294. $('.tree-folders',object.dom.panels.tree).on('click','li',function(event){object.folder_toggle(this,event);});
  295. //editeur
  296. $('.dropdown-menu-button',object.dom.panels.search).click(function(){$('.file-element').removeClass('element-focused');});
  297. $('.create-txt-button',object.dom.panels.search).click(function(){object.element_edit();});
  298. $('.btn-editor-cancel',object.dom.panels.file).click(function(){ $('.file-editor',object.dom.panels.file).addClass('hidden'); });
  299. $('.btn-editor-save',object.dom.panels.file).click(function(){object.element_edit_save();});
  300. $('.btn-editor-expand',object.dom.panels.file).click(function(){object.element_edit_expand();});
  301. if(!object.options.rightEdit){
  302. $('.document-add-element',object.dom.panels.file).addClass('hidden');
  303. }else{
  304. object.init_upload();
  305. }
  306. object.triggerEvent('loaded');
  307. });
  308. }
  309. DocumentApi.prototype.triggerEvent = function (event,options){
  310. if(!this.onEvents[event]) return;
  311. for(var key in this.onEvents[event]){
  312. this.onEvents[event][key](options);
  313. }
  314. }
  315. DocumentApi.prototype.hidePanel = function (panel){
  316. var object = this;
  317. object.options.panels[panel].visible = false;
  318. if(object.dom.panels[panel]!=null) object.dom.panels[panel].addClass('hidden');
  319. }
  320. DocumentApi.prototype.showPanel = function (panel){
  321. var object = this;
  322. object.options.panels[panel].visible = true;
  323. if(object.dom.panels[panel]!=null) object.dom.panels[panel].removeClass('hidden');
  324. }
  325. //Récuperation d'une liste de element dans le tableau elements
  326. DocumentApi.prototype.element_search = function (folderPath){
  327. var object = this;
  328. if(object.isProcessing) return;
  329. $('.file-preloader',object.dom.panels.files).removeClass('hidden');
  330. var keyword = $('.label-search',object.dom.panels.search).val();
  331. //tri
  332. var sort = {};
  333. var sortElement = $('.file-view:visible th[data-sortable][data-sort]',object.dom.panels.files);
  334. if(sortElement.length!=0 && sortElement.attr('data-sort')!=""){
  335. sort.sortable = sortElement.attr('data-sortable');
  336. sort.sort = sortElement.attr('data-sort');
  337. }
  338. var data = {
  339. /*async: false,*/
  340. action: object.options.actions.element_tree_search,
  341. keyword : keyword,
  342. sort : sort,
  343. root :object.options.root,
  344. folder : folderPath ? folderPath : object.currentFolder
  345. }
  346. data.folder = data.folder == '.' ? '': data.folder;
  347. //gestion de l'arborescence de gauche (tree)
  348. $.action(data,function(r){
  349. var tree = $('ul.tree-folders',object.dom.panels.tree);
  350. var tpl = $('li:not(:visible)',tree).get(0).outerHTML;
  351. tree.find('li:visible').remove();
  352. //On n'affiche l'arborescence que si on est pas en mode recherche
  353. if(!data.keyword || data.keyword ==''){
  354. var selected = folderPath ? folderPath : '' ;
  355. if(selected.length>=2 && selected.substring(0,2) == './') selected = selected.substring(2);
  356. for(var k in r.tree){
  357. var path = r.tree[k];
  358. var parts = path.split('/');
  359. var recipient = tree.parent();
  360. for(var i in parts){
  361. var part = parts[i];
  362. var path = parts.slice(0,parseInt(i)+1).join('/');
  363. var element = $('> .folders-container > li[data-path="'+path+'"]',recipient);
  364. if(element.length ==0){
  365. var classes = '';
  366. if(selected==path) classes+= ' folder-focused ';
  367. if(selected && (selected.indexOf(path+'/')!==-1 || selected==path) ) classes+= ' folder-open ';
  368. var row = {
  369. path : path,
  370. label : part,
  371. className : classes
  372. };
  373. var element = $(Mustache.render(tpl,row));
  374. element.removeClass('hidden');
  375. recipient.find('> .folders-container').append(element);
  376. }
  377. recipient = element;
  378. }
  379. }
  380. }
  381. });
  382. data.action = object.options.actions.element_search;
  383. object.isProcessing = true;
  384. $('.file-view',object.dom.panels.files).fill(data,function(response){
  385. object.currentFolder = data.folder;
  386. $('.file-preloader',object.dom.panels.files).addClass('hidden');
  387. object.triggerEvent('searched',{data:data,response:response});
  388. object.isProcessing = false;
  389. var breadcrumb = null;
  390. if(data.folder) breadcrumb = data.folder.split('/');
  391. //Fil d'arianne
  392. object.breadcrumb_render(breadcrumb);
  393. //reset panel detail
  394. object.reset_preview();
  395. //EVENTS
  396. //Prévisualisation
  397. $('.file-view:visible .file-element',object.dom.panels.files).off('click').click(function(event){ object.element_preview(this, event); });
  398. //Ouverture fichier / dossier
  399. $('.file-view:visible .file-element',object.dom.panels.files).off('dblclick').dblclick(function(event){ object.element_execute(this); });
  400. //Renommage
  401. $('.file-view:visible .file-element .element-rename',object.dom.panels.files).click(function(event){
  402. event.stopPropagation();
  403. event.preventDefault();
  404. object.element_rename_edit(this);
  405. });
  406. //Validation du renommage
  407. $('.rename-input',object.dom.panels.files).bind('blur keyup', function(e){
  408. var input = $(this);
  409. var text = input.next('span');
  410. var label = text.text();
  411. if(e.type === 'keyup'){
  412. //adapte la largeur en temps réel de l'input à la largeur du texte avec un max de 320 px et un min de 45px
  413. var width = Math.min(Math.max( input.val().length *5.5 ,45),320);
  414. $(this).width(width+15);
  415. }
  416. if (e.type !== 'blur' && e.keyCode !== 13) return;
  417. var tr = input.closest('.file-element');
  418. var button = tr.find('.element-rename');
  419. var newLabel = input.val().replace(/\s+$/, '');
  420. //Les dossiers ne peuvent finir par un .
  421. if(newLabel.slice(-1) == '.' && tr.attr('data-type') =='directory') newLabel = newLabel.slice(0,-1);
  422. //Check si le label a changé ou non
  423. if(newLabel == label){
  424. text.text(label).removeClass('hidden');
  425. input.addClass('hidden');
  426. button.removeClass('hidden');
  427. return;
  428. }
  429. //Attribution nouveau label
  430. text.text(newLabel).removeClass('hidden');
  431. input.addClass('hidden');
  432. $.action({
  433. action : object.options.actions.element_rename,
  434. path : tr.attr('data-path'),
  435. label : newLabel
  436. }, function(r){
  437. button.removeClass('hidden');
  438. $('.tree-folders li[data-path="'+ tr.attr('data-path')+'"]').attr('data-path',r.element.path).find('span').text(newLabel);
  439. tr.attr('data-path', r.element.path);
  440. object.triggerEvent('renamed',{
  441. path : tr.attr('data-path'),
  442. old_label : label,
  443. label : newLabel
  444. });
  445. }, function(r){
  446. text.text(label);
  447. button.removeClass('hidden');
  448. });
  449. });
  450. //Maj du placeholder de la recherche
  451. $('.label-search').attr('placeholder','Rechercher'+(breadcrumb ? ' dans '+breadcrumb[breadcrumb.length-1]:''));
  452. if(!object.options.rightEdit){
  453. $('.element-rename',object.dom.panels.file).addClass('hidden');
  454. }else{
  455. //Drag & Drop fichiers
  456. var viewElement = $(".file-view:visible");
  457. var droppableContainer = viewElement.get(0).tagName == 'TABLE' ? ' tbody': '';
  458. $(".file-view:visible"+droppableContainer,object.dom.panels.files).sortable({
  459. distance: 5,
  460. opacity: 0.75,
  461. start: function(e, ui){
  462. var placeholder = ui.item.clone().addClass('original-placeholder').removeAttr('style');
  463. ui.item.after(placeholder);
  464. },
  465. stop: function(e, ui){
  466. $('.original-placeholder').remove();
  467. $(e.originalEvent.target).one('click', function(e){
  468. e.stopImmediatePropagation();
  469. });
  470. }
  471. }).disableSelection();
  472. $(".file-view:visible .file-element",object.dom.panels.files).droppable({
  473. tolerance: "pointer",
  474. hoverClass: "folder-receive-element",
  475. drop: function(event, ui) {
  476. var from = $(ui.draggable["0"]);
  477. var to = $(this);
  478. if(!from.attr('data-path')) return;
  479. if(to.attr('data-type') != 'directory') return;
  480. $.action({
  481. action : object.options.actions.element_move,
  482. from : from.attr('data-path'),
  483. to : to.attr('data-path'),
  484. }, function(response){
  485. if (response.element !== null && typeof response.element === 'object') {
  486. object.triggerEvent('moved',{from:from,to:to,response:response});
  487. from.remove();
  488. object.remove_treeline(from);
  489. object.reset_preview();
  490. } else {
  491. alert('Action impossible, un élément existe déjà avec ce nom dans le dossier.');
  492. }
  493. });
  494. }
  495. });
  496. //Drag & drop arborescence
  497. $("li",object.dom.panels.tree).droppable({
  498. tolerance: "pointer",
  499. greedy: true,
  500. over: function(e, ui){
  501. var to = $(this);
  502. var toPath = to.hasClass('root-folder') ? '.' : to.attr('data-path');
  503. to.addClass( to.hasClass('root-folder') ?'folder-focused':'folder-hover');
  504. },
  505. out: function(e, ui){
  506. var to = $(this);
  507. if( to.hasClass('root-folder') && to.hasClass('folder-focused'))
  508. to.removeClass('folder-focused');
  509. if(to.hasClass('folder-hover'))
  510. to.removeClass('folder-hover');
  511. },
  512. drop: function(event, ui) {
  513. var from = $(ui.draggable["0"]);
  514. var fromPath = from.attr('data-path');
  515. var to = $(this);
  516. var toPath = to.hasClass('root-folder') ? '.' : to.attr('data-path');
  517. if( to.hasClass('root-folder') && to.hasClass('folder-focused'))
  518. to.removeClass('folder-focused');
  519. if(to.hasClass('folder-hover'))
  520. to.removeClass('folder-hover');
  521. $('.tree-panel li').removeClass('folder-hover');
  522. if(fromPath == toPath) {
  523. $.message('error', 'Impossible de déplacer un dossier dans lui-même.');
  524. return;
  525. }
  526. $.action({
  527. action : object.options.actions.element_move,
  528. from : fromPath,
  529. to : toPath,
  530. }, function(response){
  531. if (response.element !== null && typeof response.element === 'object') {
  532. object.triggerEvent('moved',{from:from,to:to,response:response});
  533. // Suppression de la ligne dans le panneau arborescence
  534. object.remove_treeline(from);
  535. // Ajout d'une ligne dans le panneau de l'arborescence
  536. if(from.attr('data-type') == 'directory'){
  537. var fromLabel = $('span>span', from).text();
  538. var tpl = $('.tree-folders li:not(:visible)',object.dom.panels.tree).get(0).outerHTML;
  539. var toContainer = toPath == '.' ? $('ul.tree-folders',object.dom.panels.tree) : $('>ul.folders-container', to);
  540. toContainer.parent().addClass('folder-open');
  541. $('i.far.fa-folder',toContainer.parent()).removeClass('fa-folder').addClass('fa-folder-open');
  542. var li = $(Mustache.render(tpl,{label:fromLabel,path:toPath+'/'+fromLabel}));
  543. li.removeClass('hidden');
  544. toContainer.append(li);
  545. }
  546. // Suppression de la ligne dans le panneau d'élément
  547. from.remove();
  548. // Reset de la preview
  549. object.reset_preview();
  550. }
  551. else
  552. alert('Action impossible, un élément existe déjà avec ce nom dans le dossier.');
  553. });
  554. }
  555. });
  556. }
  557. });
  558. }
  559. //Draw du fil d'Ariane
  560. DocumentApi.prototype.breadcrumb_render = function (breadcrumb){
  561. var object = this;
  562. if(!object.dom.panels.breadcrumb) return;
  563. if(breadcrumb === null) breadcrumb = ['.'];
  564. if(breadcrumb[0]!='.') breadcrumb.unshift('.');
  565. $('ul li:visible',object.dom.panels.breadcrumb).remove();
  566. var tpl = $('li:not(:visible)',object.dom.panels.breadcrumb).get(0).outerHTML;
  567. for(var key in breadcrumb){
  568. var label = breadcrumb[key];
  569. if(label == '.') label = '<i class="fas fa-home"></i>';
  570. var currentpath = breadcrumb.slice(0,key);
  571. currentpath.push(breadcrumb[key]);
  572. currentpath = currentpath.join('/');
  573. var li = $(Mustache.render(tpl,{label:label,path:currentpath}));
  574. if(key == breadcrumb.length-1) li.attr('title', 'Dossier courant');
  575. $('ul',object.dom.panels.breadcrumb).append(li);
  576. li.removeClass('hidden');
  577. }
  578. $('ul > li',object.dom.panels.breadcrumb).click(function(){
  579. object.element_search( $(this).attr('data-path'));
  580. });
  581. }
  582. /** DOSSIER */
  583. DocumentApi.prototype.folder_toggle = function (element, event, force){
  584. var object = this;
  585. $('.label-search').val('').blur();
  586. var li = $(element).closest('li');
  587. if(li.hasClass('folder-focused')){
  588. var parent = li.parent().parent();
  589. object.element_search(parent.attr('data-path') == null ? '.' : parent.attr('data-path'));
  590. }else{
  591. object.element_search(li.attr('data-path'));
  592. }
  593. if(event) event.stopPropagation();
  594. }
  595. // Reset du panneau de preview de la GED
  596. DocumentApi.prototype.reset_preview = function(){
  597. var object = this;
  598. $('.detail-thumbnail',object.dom.panels.detail).removeAttr('style');
  599. $('> h1',object.dom.panels.detail).text('Aucun fichier sélectionné');
  600. $('> small > span',object.dom.panels.detail).text('-');
  601. $('.detail-buttons',object.dom.panels.detail).addClass('hidden');
  602. }
  603. // Preloader affiché durant l'upload
  604. DocumentApi.prototype.toggle_preloader_upload = function(){
  605. var object = this;
  606. var preloader = $('.preloader-upload-container');
  607. if(!preloader.is(':visible')) {
  608. preloader.removeClass('hidden');
  609. preloader.find('.preloader-upload-close').unbind('click').click(function(){
  610. object.toggle_preloader_upload();
  611. });
  612. $('body').css('overflow', 'hidden');
  613. } else {
  614. $('body').css('overflow', '');
  615. preloader.addClass('hidden');
  616. }
  617. }
  618. // Init de l'upload
  619. DocumentApi.prototype.init_upload = function(){
  620. var object = this;
  621. var form = $('.upload-button form',object.dom.container);
  622. var input = form.find('input[type="file"]');
  623. var zone = $('.file-panel');
  624. var droppedFiles = null;
  625. var noBlink = null;
  626. var fileQueue = [];
  627. //Upload des images dans le presse papier
  628. window.addEventListener("paste", function(event){
  629. var clipboardData = event.clipboardData || window.clipboardData;
  630. if(clipboardData.getData('Text').length) return;
  631. if(event.clipboardData == false || event.clipboardData.items == undefined) return;
  632. var items = event.clipboardData.items;
  633. if(items.length == 0) return;
  634. fileQueue = [];
  635. for(var i = 0; i < items.length; i++){
  636. var f = items[i];
  637. if (f.type.indexOf("image") == -1) return;
  638. var file = items[i].getAsFile();
  639. fileQueue.push({
  640. method : 'paste',
  641. file : file
  642. });
  643. }
  644. form.trigger('submit');
  645. }, false);
  646. input.addClass('hidden');
  647. $('.upload-button > div',object.dom.container).click(function(e){
  648. input.trigger('click');
  649. e.preventDefault();
  650. e.stopPropagation();
  651. });
  652. input.on('change', function (e) {
  653. fileQueue = [];
  654. for(var k = 0; k < this.files.length; k++){
  655. if(this.files[k].name == null) break;
  656. fileQueue.push({
  657. method : 'browse',
  658. file : this.files[k]
  659. });
  660. }
  661. form.trigger('submit');
  662. });
  663. zone.on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  664. e.preventDefault();
  665. e.stopPropagation();
  666. })
  667. .on('dragover dragenter', function (e) {
  668. clearTimeout(noBlink);
  669. $('.drag-overlay').removeClass('hidden');
  670. zone.addClass('drag-over');
  671. e.preventDefault();
  672. e.stopPropagation();
  673. })
  674. .on('dragleave dragend drop', function (e) {
  675. noBlink = setTimeout(function(){
  676. $('.drag-overlay').addClass('hidden');
  677. zone.removeClass('drag-over');
  678. },500);
  679. e.preventDefault();
  680. e.stopPropagation();
  681. })
  682. .on('drop', function (e) {
  683. fileQueue = [];
  684. if(!e.originalEvent.dataTransfer) return;
  685. droppedFiles = e.originalEvent.dataTransfer.files;
  686. for (var i=0, f; f=droppedFiles[i]; ++i) {
  687. if(f.name.indexOf(".")==-1 && f.size%4096 == 0) {
  688. $.message('error', 'Impossible d\'envoyer un dossier.');
  689. return;
  690. }
  691. fileQueue.push({
  692. method : 'drop',
  693. file : droppedFiles[i]
  694. });
  695. }
  696. form.trigger('submit');
  697. });
  698. form.on('submit', function (e) {
  699. $('.drag-overlay').addClass('hidden');
  700. e.preventDefault();
  701. var tpl = $('.upload-files li.hidden').get(0).outerHTML;
  702. $('.upload-files li:not(.hidden)').remove();
  703. var fileProcessed = [];
  704. object.toggle_preloader_upload();
  705. for( var k in fileQueue){
  706. var file = fileQueue[k].file;
  707. var line = $(Mustache.render(tpl,{
  708. label : file.name,
  709. sort : k,
  710. size : readable_size(file.size)
  711. }));
  712. line.removeClass('hidden');
  713. $('.upload-files').append(line);
  714. var ajaxData = new FormData();
  715. ajaxData.append(input.attr('name'), file);
  716. ajaxData.append('method', fileQueue[k].method);
  717. var path = object.currentFolder =='.' ? '' : object.currentFolder;
  718. if(path== '' && object.options.root!='' ) path = object.options.root;
  719. ajaxData.append('path', path);
  720. ajaxData.append('sort', k);
  721. $.ajax({
  722. url: form.attr('action'),
  723. type: form.attr('method'),
  724. data: ajaxData,
  725. dataType: 'json',
  726. cache: false,
  727. contentType: false,
  728. processData: false,
  729. complete: function (data) {},
  730. success: function (response) {
  731. if(!response.error)
  732. $('.upload-files li[data-sort="'+response.sort+'"] .upload-file-state').addClass('success').html('<i class="far fa-check-circle"></i> Succès');
  733. else
  734. $('.upload-files li[data-sort="'+response.sort+'"] .upload-file-state').addClass('error').html('<i class="fas fa-exclamation-circle"></i> Erreur :'+response.error);
  735. fileProcessed.push(response);
  736. $('.preloader-upload-container .upload-state span').text(fileProcessed.length+'/'+fileQueue.length+' fichiers envoyés');
  737. if(fileQueue.length==fileProcessed.length){
  738. object.triggerEvent('uploaded',{data:fileProcessed});
  739. $('.document-create-dropdown').removeClass('show');
  740. object.element_search();
  741. //On cache automatiquement le gestionnaire d'upload si aucune erreure.
  742. if($('.upload-file-state.error').length == 0){
  743. setTimeout(function(){
  744. var preloader = $('.preloader-upload-container');
  745. $('body').css('overflow', '');
  746. preloader.addClass('hidden');
  747. },1500);
  748. }
  749. }
  750. },
  751. error: function (data) {
  752. $('.document-create-dropdown').removeClass('show');
  753. $.message('error',data);
  754. },
  755. xhr: function() {
  756. var xhr = new window.XMLHttpRequest();
  757. xhr.sort = k;
  758. xhr.upload.addEventListener("progress", function(evt){
  759. if (evt.lengthComputable) {
  760. var percentComplete = (evt.loaded / evt.total) * 100;
  761. percentComplete = Math.round(percentComplete * 100) / 100;
  762. var progressBar = $('.upload-files li[data-sort="'+xhr.sort+'"] .progress-bar')
  763. .css('width',percentComplete+'%')
  764. .text(percentComplete+'%')
  765. .attr('aria-valuenow',percentComplete);
  766. }
  767. }, false);
  768. return xhr;
  769. }
  770. });
  771. }
  772. });
  773. }
  774. DocumentApi.prototype.folder_create = function(){
  775. var object = this;
  776. var path = object.currentFolder =='.' || object.currentFolder =='' ? '' : object.currentFolder+'/';
  777. if(path== '' && object.options.root!='' ) path = object.options.root+'/';
  778. path += $('.folder-name').val();
  779. var data = {
  780. action : object.options.actions.folder_create,
  781. path : path,
  782. folder : $('.folder-name',object.dom.panels.search).val()
  783. }
  784. $.action(data,function(response){
  785. object.triggerEvent('folder-created',{path:data.path,folder:data.folder,response:response});
  786. $('.folder-name',object.dom.panels.search).val('').focus();
  787. $('.new-folder-block',object.dom.panels.search).toggleClass('hidden');
  788. $('.dropdown-menu',object.dom.panels.search).trigger('click');
  789. object.element_search();
  790. });
  791. }
  792. DocumentApi.prototype.new_folder = function(event){
  793. var object = this;
  794. $('.new-folder-block',object.dom.panels.search).toggleClass('hidden');
  795. $('.new-folder-block input',object.dom.panels.search).focus().enter(function(){object.folder_create()});
  796. event.stopPropagation();
  797. }
  798. /** ELEMENT **/
  799. DocumentApi.prototype.element_rename_edit = function(button){
  800. var object = this;
  801. var button = $(button);
  802. var span = button.prev('span');
  803. var text = span.find('span');
  804. var tr = span.closest('.file-element');
  805. var label = span.find('span').text();
  806. var input = $('.rename-input',tr);
  807. //adapte la largeur de l'input à la largeur du texte avec un max de 320 px et un min de 45px
  808. var width = Math.min(Math.max(text.width(),45),320);
  809. button.addClass('hidden');
  810. text.addClass('hidden');
  811. input.removeClass('hidden')
  812. .focus()
  813. .val(label)
  814. .width(width+15)
  815. .click(function(event){
  816. event.stopPropagation();
  817. });
  818. }
  819. // Met à jour l'arborescence (supprime la line dans l'arborescence sur move ou rename)
  820. DocumentApi.prototype.remove_treeline = function(from){
  821. var parent = $('.folder-focused ul');
  822. if(parent.length == 0) parent = $('.tree-panel > ul');
  823. $('li',parent).each(function(i,li){
  824. var li = $(li);
  825. if(li.attr('data-path') == from.attr('data-path')) {
  826. li.remove();
  827. }
  828. });
  829. }
  830. //Récuperation d'une liste de element dans le tableau elements
  831. DocumentApi.prototype.element_preview = function(element, event){
  832. var object = this;
  833. var line = $(element);
  834. console.log('preview');
  835. if(!event.ctrlKey) object.selected = [];
  836. object.selected.push({
  837. element : line,
  838. path : line.attr('data-path'),
  839. type : line.attr('data-type'),
  840. extension : line.attr('data-extension'),
  841. id : line.attr('data-id')
  842. });
  843. console.log(object.selected);
  844. $('.file-view:visible .file-element',object.dom.panels.files).removeClass('element-focused');
  845. for(var k in object.selected){
  846. object.selected[k].element.addClass('element-focused');
  847. }
  848. if(line.hasClass('element-focused')) return;
  849. if(object.selected.length==1){
  850. $('.right-panel',object.dom.panels.detail).addClass('hidden');
  851. $('.detail-buttons',object.dom.panels.detail).removeClass('hidden');
  852. if(object.selected.type =='directory') {
  853. $('.detail-buttons > .directory-button',object.dom.panels.detail).removeClass('hidden');
  854. }else{
  855. $('.detail-buttons > .directory-button',object.dom.panels.detail).addClass('hidden');
  856. }
  857. $('.detail-buttons > li:visible',object.dom.panels.detail).remove();
  858. var buttonTemplate = $('.detail-buttons > li:eq(0).hidden').get(0).outerHTML;
  859. if(object.options.panels.detail.buttons){
  860. for(var i in object.options.panels.detail.buttons){
  861. var data = object.options.panels.detail.buttons[i];
  862. if(data.visibility && data.visibility.type && data.visibility.type.indexOf(object.selected.type) == -1 ) continue;
  863. if(data.visibility && data.visibility.extension && data.visibility.extension.indexOf(object.selected.extension) == -1 ) continue;
  864. var button = $(Mustache.render(buttonTemplate,data));
  865. button.removeClass('hidden');
  866. $('.detail-buttons').append(button);
  867. }
  868. }
  869. //autoblur sur le rename d'un fichier en cours si existant
  870. var currentRename = $('.file-view:visible .file-element .rename-input:visible');
  871. if(currentRename.length>0) currentRename.trigger('blur');
  872. $('.detail-thumbnail .thumbnail-preloader',object.dom.panels.detail).addClass('show');
  873. $.action({
  874. action : object.options.actions.element_preview,
  875. path : line.attr('data-path'),
  876. },function(response){
  877. setTimeout(function(){
  878. $('.detail-thumbnail .thumbnail-preloader',object.dom.panels.detail).removeClass('show');
  879. },100);
  880. $('.detail-thumbnail',object.dom.panels.detail).css('background-image','url('+response.row.thumbnail+')');
  881. var substrings = ['.jpg','.jpeg','.bmp','.gif','.png','.jfif'];
  882. $('.detail-thumbnail',object.dom.panels.detail).css('background-size','');
  883. if (new RegExp(substrings.join("|")).test(response.row.thumbnail))
  884. $('.detail-thumbnail',object.dom.panels.detail).css('background-size','contain');
  885. $('> h1',object.dom.panels.detail).html(response.row.label).text();
  886. var size = response.row.sizeReadable;
  887. if(response.row.type == 'directory') size = response.row.childNumber+' élement'+(response.row.childNumber>1?'s':'');
  888. $('> small > span',object.dom.panels.detail).text(size+", "+response.row.updatedRelative);
  889. object.triggerEvent('selected',object.selected);
  890. });
  891. }
  892. }
  893. //Suppression d'un élément de la GED
  894. DocumentApi.prototype.element_delete = function(){
  895. var object = this;
  896. if(object.selected.length == 0) return $.message('info', 'Sélectionnez d\'abord un élément à supprimer');
  897. if(!confirm('Êtes-vous sûr de vouloir supprimer '+(object.selected.length)+' élément ?')) return;
  898. for(var k in object.selected){
  899. var select = object.selected[k];
  900. if($('span .rename-input:visible',select.element).length) continue;
  901. $.action({
  902. action : object.options.actions.element_delete,
  903. path : select.path,
  904. },function(response){
  905. object.triggerEvent('deleted',{
  906. element : select.element,
  907. response : response,
  908. path : select.path,
  909. });
  910. select.element.remove();
  911. object.reset_preview();
  912. object.remove_treeline(select.element);
  913. });
  914. }
  915. }
  916. DocumentApi.prototype.element_share_edit = function(){
  917. var object = this;
  918. if(object.selected.length==0) return $.message('info', 'Sélectionnez d\'abord un élément à partager');
  919. var selected = object.selected[0];
  920. if($('span .rename-input:visible',selected.element).length) return;
  921. $.action({
  922. action : object.options.actions.element_share_edit,
  923. path : selected.path,
  924. });
  925. }
  926. //edition d'un fichier texte en ligne
  927. DocumentApi.prototype.element_edit = function(path){
  928. var object = this;
  929. $('.file-editor-name',object.dom.panels.file).val('Nouveau Fichier.txt');
  930. //reinitialisation
  931. $('.file-editor-content iframe',object.dom.panels.file).remove();
  932. $('.file-editor-input',object.dom.panels.file).removeClass('hidden');
  933. $('.file-editor-input',object.dom.panels.file).val('');
  934. var saveShortcut = function(event){
  935. if(event.ctrlKey && event.key == 's'){
  936. object.element_edit_save(true);
  937. event.preventDefault();
  938. event.stopPropagation();
  939. }
  940. }
  941. if(path){
  942. $('.file-editor-input',object.dom.panels.file).val('Chargement en cours...');
  943. $.action({
  944. action : object.options.actions.element_edit,
  945. path : path,
  946. },function(r){
  947. $('.file-editor-name',object.dom.panels.file).val(r.label);
  948. $('.file-editor-input',object.dom.panels.file).val(r.content);
  949. $('.file-editor-content iframe').remove();
  950. //sauvegarde sur le ctrl+s
  951. $('.file-editor-input',object.dom.panels.file).unbind('keydown').keydown(saveShortcut);
  952. if(r.wysiwyg){
  953. $('.file-editor-input').addClass('hidden');
  954. var iframe = $('<iframe width="100%" height="100%" frameBorder="0">');
  955. iframe.on('load',function(){
  956. object.dom.editor = $('<div id="editor">').html($('.file-editor-input').val());
  957. object.dom.editor.unbind('keydown').keydown(saveShortcut);
  958. var contextIframe = $(this).contents()[0];
  959. // Set the style on the head of the iframe.
  960. $('head', contextIframe).append('<link href="css/trumbowyg.min.css" rel="stylesheet"> \
  961. <style>html,body{padding:0!important;margin:0!important;}.trumbowyg-box{margin-top: 0;}.trumbowyg-button-pane{position:fixed;margin-top: -1px;}.trumbowyg-box .trumbowyg-editor{margin-top:20px;border:0px;}.trumbowyg-box{border:0px;}.trumbowyg-dropdown{position:fixed!important;width:300px!important;}</style>');
  962. // Set the content to be editable.
  963. $('body', contextIframe).append(object.dom.editor);
  964. object.dom.editor.trumbowyg(object.editorOptions);
  965. });
  966. $('.file-editor-content').append(iframe);
  967. }
  968. });
  969. }
  970. $('.file-editor',object.dom.panels.file).removeClass('hidden');
  971. $('.file-editor-input',object.dom.panels.file).focus();
  972. }
  973. //edition d'un fichier texte en ligne
  974. DocumentApi.prototype.element_edit_save = function(dontCloseEditor){
  975. var object = this;
  976. var content = $('.file-editor-input',object.dom.panels.file).val();
  977. if($('.file-editor-content iframe').length > 0){
  978. var contextIframe = $('.file-editor-content iframe').contents()[0];
  979. content = $('textarea[name="editor"]',contextIframe).val();
  980. }
  981. var path = object.currentFolder =='.' ? '' : object.currentFolder;
  982. if(path== '' && object.options.root!='' ) path = object.options.root;
  983. $.action({
  984. action : object.options.actions.element_save,
  985. label : $('.file-editor-name',object.dom.panels.file).val(),
  986. content : content,
  987. path : path,
  988. },function(r){
  989. //sauvegarde sans fermer
  990. if(dontCloseEditor){
  991. $.message('info','Fichier sauvegardé !');
  992. return;
  993. }
  994. //sauvegarde avec fermeture
  995. $('.file-editor',object.dom.panels.file).addClass('hidden');
  996. object.element_search();
  997. });
  998. }
  999. //Agrandissement de l'éditor en mode fullscreen
  1000. DocumentApi.prototype.element_edit_expand= function(){
  1001. var object = this;
  1002. var editor = $('.file-editor',object.dom.panels.file);
  1003. if(editor.hasClass('expanded')){
  1004. editor.removeClass('expanded');
  1005. $('.btn-editor-expand').attr('title','Agrandir');
  1006. $('.btn-editor-expand i').attr('class','fas fa-expand-arrows-alt');
  1007. }else{
  1008. $('.btn-editor-expand i').attr('class','fas fa-compress-arrows-alt');
  1009. $('.btn-editor-expand').attr('title','Réduire');
  1010. editor.addClass('expanded');
  1011. }
  1012. }
  1013. //Récuperation d'une liste de element dans le tableau elements
  1014. DocumentApi.prototype.element_execute = function(element){
  1015. var object = this;
  1016. var inputSearch = $('.label-search',object.dom.panels.search);
  1017. if(inputSearch.val().length) {
  1018. inputSearch.val('').blur();
  1019. $(".search-clear",object.dom.panels.search).removeAttr('style');
  1020. }
  1021. var line = $(element);
  1022. if(line.attr('data-type') == 'directory'){
  1023. object.element_search(line.attr('data-path'));
  1024. object.triggerEvent('opened',{
  1025. element : line,
  1026. path : line.attr('data-path')
  1027. });
  1028. return;
  1029. }
  1030. object.triggerEvent('executed',{
  1031. element : line,
  1032. path : line.attr('data-path')
  1033. });
  1034. var selected = object.selected[0];
  1035. var ext = selected.path.split('.').pop();
  1036. switch(ext){
  1037. case 'txt':
  1038. case 'html':
  1039. case 'htm':
  1040. object.element_edit(line.attr('data-path'));
  1041. break;
  1042. default:
  1043. object.element_download(line);
  1044. break;
  1045. }
  1046. }
  1047. // Téléchargement d'un élément de la GED
  1048. DocumentApi.prototype.element_download = function(line,forceDownload){
  1049. var object = this;
  1050. var line = line ? line : object.selected[0].element;
  1051. if(line.length == 0) return $.message('info', 'Sélectionnez d\'abord un élément à télécharger');
  1052. object.triggerEvent('downloaded',{
  1053. element : line,
  1054. path : line.attr('data-path')
  1055. });
  1056. var url = 'action.php?action='+object.options.actions.element_execute+'&path='+encodeURIComponent(btoa(line.attr('data-path')));
  1057. if(forceDownload) url+= '&download';
  1058. window.open(url, '_blank');
  1059. }
  1060. /** PROPERTIES **/
  1061. DocumentApi.prototype.properties_show = function(){
  1062. var object = this;
  1063. var modal = $('.document-properties-modal');
  1064. var tpl = $('.modal-body-template',modal).html();
  1065. modal.modal('show');
  1066. $('.modal-body',modal).html('Chargement...');
  1067. $.action({
  1068. action : object.options.actions.properties_show,
  1069. id : object.selected[0].id
  1070. },function(r){
  1071. $('.modal-body',modal).html(Mustache.render(tpl,r.row));
  1072. init_components(modal);
  1073. object.triggerEvent('properties-showed',{
  1074. modal : modal,
  1075. id : object.selected.id
  1076. });
  1077. });
  1078. }
  1079. /** ELEMENTRIGHT **/
  1080. //Récuperation d'une liste de elementright dans le tableau elementrights
  1081. DocumentApi.prototype.right_toggle = function(){
  1082. var object = this;
  1083. $('.right-panel',object.dom.panels.detail).toggleClass('hidden');
  1084. init_components('.element-right-form');
  1085. this.right_search();
  1086. }
  1087. //Récuperation d'une liste de elementright dans le tableau elementrights
  1088. DocumentApi.prototype.right_search = function(callback){
  1089. var object = this;
  1090. if(object.selected.length == 0) return;
  1091. $('.element-rights').fill({
  1092. action:object.options.actions.right_search,
  1093. id : object.selected[0].id
  1094. },function(){
  1095. if(callback!=null) callback();
  1096. });
  1097. }
  1098. //Ajout ou modification d'élément elementright
  1099. DocumentApi.prototype.right_save = function(){
  1100. var object = this;
  1101. var data = {
  1102. uid : $('.element-right-form input.uid').val(),
  1103. read : $('.element-right-form input.read').prop('checked') ? 1 :0,
  1104. edit : $('.element-right-form input.edit').prop('checked') ? 1 :0,
  1105. recursive : $('.element-right-form input.recursive').prop('checked') ? 1 :0
  1106. }
  1107. data.action = object.options.actions.right_save;
  1108. data.element = object.selected[0].id;
  1109. $.action(data,function(r){
  1110. $.message('success','Enregistré');
  1111. $('.uid').val('');
  1112. $('.read,.edit,.recursive').prop('checked',false);
  1113. init_components('.element-right-form');
  1114. object.right_search();
  1115. data.id = object.selected[0].id;
  1116. data.element = object.selected[0].element;
  1117. object.triggerEvent('right-saved',data);
  1118. });
  1119. }
  1120. //Suppression d'élement elementright
  1121. DocumentApi.prototype.right_delete = function(element){
  1122. var object = this;
  1123. if(!confirm('Êtes-vous sûr de vouloir supprimer ce droit ?')) return;
  1124. var line = $(element).closest('li');
  1125. $.action({
  1126. action : object.options.actions.right_delete,
  1127. id : line.attr('data-id')
  1128. },function(r){
  1129. object.triggerEvent('right-deleted',{id:line.attr('data-id'),element:line});
  1130. line.remove();
  1131. });
  1132. }
  1133. //Ajoute un callback sur un evenement de la ged particulier
  1134. DocumentApi.prototype.on = function(event,callback){
  1135. if(!this.onEvents[event])this.onEvents[event] = [];
  1136. this.onEvents[event].push(callback);
  1137. }
  1138. /* Gestion des vues */
  1139. //Change la vue courante
  1140. DocumentApi.prototype.view = function(view){
  1141. var object = this;
  1142. object.currentView = view;
  1143. object.renderView();
  1144. object.triggerEvent('view-loaded',{view:view});
  1145. }
  1146. //Ajoute une vue custom
  1147. DocumentApi.prototype.addView = function(data){
  1148. var object = this;
  1149. data.html = $(data.html);
  1150. data.html.attr('data-view',data.uid);
  1151. if(!data.html.hasClass('file-view')) data.html.addClass('file-view');
  1152. object.views.push(data);
  1153. }
  1154. //affiche le rendu de la vue courante (object.currentView)
  1155. DocumentApi.prototype.renderView = function(initPath){
  1156. var object = this;
  1157. if(!object.dom.panels.files) return;
  1158. $('.file-view',object.dom.panels.files).addClass('hidden');
  1159. var viewElement = $('.file-view[data-view="'+object.currentView+'"]');
  1160. $(object.dom.panels.files).attr('data-view',object.currentView);
  1161. $('.view-module > li',object.dom.panels.search).removeClass('selected');
  1162. $('.view-module > li[data-view="'+object.currentView+'"]',object.dom.panels.search).addClass('selected');
  1163. viewElement.removeClass('hidden');
  1164. object.element_search(initPath);
  1165. object.triggerEvent('rendered',{});
  1166. }
  1167. //charge toutes les vues disponibles dans le dom
  1168. DocumentApi.prototype.loadViews = function(){
  1169. var object = this;
  1170. //Chargement des bouttons de vue
  1171. $('.view-module > li:visible',object.dom.panels.search).remove();
  1172. var tpl = $('.view-module > li.hidden',object.dom.panels.search).get(0).outerHTML;
  1173. for(var key in object.views){
  1174. var view = object.views[key];
  1175. var button = $(Mustache.render(tpl, view));
  1176. button.removeClass('hidden');
  1177. $('.view-module',object.dom.panels.search).append(button);
  1178. button.click(function(){
  1179. object.view($(this).attr('data-view'));
  1180. });
  1181. if($('.file-view[data-view="'+view.uid+'"]',object.dom.panels.files).length==0)
  1182. object.dom.panels.files.append(view.html);
  1183. }
  1184. }