document.api.js 45 KB

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