main.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. //handle target
  2. var target = false;
  3. var refreshInterval = null;
  4. var resizeTarget = false;
  5. //CHARGEMENT DE LA PAGE
  6. $(function(){
  7. if($.urlParam('section') == 'list.widget.share') dashboard_widget_share_search();
  8. if(($.page()!='' && $.page()!='index') || $.urlParam('module')!=null) return;
  9. dashboard_dashboardwidget_search();
  10. $('#dashboard-view li').click(function(){
  11. $('#dashboard-view li').removeAttr('data-selected');
  12. $(this).attr('data-selected',1);
  13. dashboard_dashboardwidget_search();
  14. });
  15. $('#widget-list').change(function(){
  16. var option = $('#widget-list option:selected').data();
  17. option.text = $('#widget-list option:selected').text();
  18. var description = $('.widgetDescription');
  19. description.removeClass('hidden');
  20. $('h3 span', description).text($(this).val() == '' ? 'Sélectionnez un widget' : option.text);
  21. $('h3 i', description).attr('class',option.icon);
  22. $('p', description).text(option.description);
  23. $('.widgetWidth', description).html(option.width=='' ? '' : '<span class="text-muted font-weight-bold mr-2">LARGEUR</span> '+Math.round(option.width*100 /12)+'%');
  24. $('.widgetColor', description).html(option.background=='' ? '' : '<span class="text-muted font-weight-bold mr-2">COULEUR</span> <small style="background-color:'+option.background+'"></small><span>'+option.background+'</span> ');
  25. });
  26. if(!$('.dashboard-container').hasClass('readonly')){
  27. $('#dashboard').sortable({
  28. distance: 15,
  29. cancel :'[data-mandatory="1"]',
  30. tolerance: "pointer",
  31. handle: ".widget_header",
  32. opacity: 0.8,
  33. placeholder: {
  34. element: function(clone, ui) {
  35. var classes = clone.attr('class').split(' ');
  36. for(var key in classes){
  37. var number = classes[key].match(/col-xl-([0-9])/i);
  38. if(number != null)
  39. colSize = number[1] > 9 ? 9 : number[1];
  40. }
  41. return $('<div class="ui_sortable_placeholder widget_placeholder col-xl-'+colSize+' col-md-'+colSize*2+' col-lg-'+colSize*2+'"></div>');
  42. },
  43. update: function() {
  44. return;
  45. }
  46. },
  47. update:function(event,ui){
  48. var data = dashboard_dashboardwidget_save_position();
  49. }
  50. });
  51. $( ".widget" ).disableSelection();
  52. }
  53. });
  54. function init_setting_dashboard(){
  55. dashboard_dashboard_search();
  56. dashboard_widget_share_search();
  57. }
  58. //Enregistrement des configurations
  59. function dashboard_setting_save(){
  60. $.action({
  61. action : 'dashboard_setting_save',
  62. fields : $('#dashboard-setting-form').toJson()
  63. },function(){
  64. $.message('success','Enregistré');
  65. });
  66. }
  67. /** DASHBOARD **/
  68. //Récuperation d'une liste de dashboard dans le tableau #dashboards
  69. function dashboard_dashboard_search(callback){
  70. $('#dashboards').fill({
  71. action:'dashboard_dashboard_search'
  72. },function(){
  73. if(callback!=null) callback();
  74. });
  75. }
  76. //Ajout ou modification d'élément dashboard
  77. function dashboard_dashboard_save(){
  78. var form = $('#dashboard-form');
  79. var data = form.toJson();
  80. $.action(data,function(r){
  81. form.attr('data-id','');
  82. reset_inputs(form);
  83. $('#icon').val('far fa-bookmark');
  84. init_components(form);
  85. $.message('success','Enregistré');
  86. dashboard_dashboard_search();
  87. });
  88. }
  89. //Récuperation ou edition d'élément dashboard
  90. function dashboard_dashboard_edit(element){
  91. var line = $(element).closest('tr');
  92. $.action({
  93. action:'dashboard_dashboard_edit',
  94. id:line.attr('data-id')
  95. },function(r){
  96. $.setForm('#dashboard-form',r);
  97. $('#dashboard-form').attr('data-id',r.id);
  98. init_components('#dashboard-form');
  99. });
  100. }
  101. //Suppression d'élement dashboard
  102. function dashboard_dashboard_delete(element){
  103. if(!confirm('Êtes-vous sûr de vouloir supprimer ce dashboard ?')) return;
  104. var line = $(element).closest('tr');
  105. $.action({
  106. action : 'dashboard_dashboard_delete',
  107. id : line.attr('data-id')
  108. },function(r){
  109. line.remove();
  110. $.message('info','Dashboard supprimé');
  111. });
  112. }
  113. /** DASHBOARDWIDGET **/
  114. function configure_widget(element){
  115. var widget = $(element).closest('.widget');
  116. var configureUrl = widget.attr('data-configure');
  117. if(configureUrl.substring(0,11) == 'setting.php'){
  118. window.open(configureUrl);
  119. }else{
  120. var modal = $('#configureWidgetModal');
  121. modal.attr('data-widget',widget.attr('data-id')).modal('show');
  122. $('.pluginContent', modal).load(widget.attr('data-configure'),{
  123. id : widget.attr('data-id')
  124. },function(){
  125. init_components();
  126. var callback = widget.attr('data-configure-init');
  127. if(window[callback]!=null) window[callback](widget,modal);
  128. });
  129. }
  130. }
  131. function dashboard_dashboardwidget_save_configuration(){
  132. var modal = $('#configureWidgetModal');
  133. var widget = $('.widget[data-id="'+modal.attr('data-widget')+'"]');
  134. modal.modal('hide');
  135. var callback = widget.attr('data-configure-callback');
  136. if(window[callback]!=null) window[callback](widget,modal);
  137. }
  138. //Récuperation d'une liste de dashboardwidget dans le tableau #dashboardwidgets
  139. function dashboard_dashboardwidget_search(callback){
  140. var dashboard = $('#dashboard-view li[data-selected]').attr('data-id');
  141. if(!dashboard || dashboard=='') return;
  142. $.action({
  143. action : 'dashboard_dashboardwidget_search',
  144. dashboard : dashboard
  145. },function(r){
  146. $('#dashboard .widget:visible').remove();
  147. for(var i in r.rows){
  148. var widget = r.rows[i];
  149. dashboard_dashboardwidget_append(widget,function(widget){
  150. dashboard_dashboardwidget_load(widget);
  151. });
  152. }
  153. clearInterval(refreshInterval);
  154. refreshInterval = setInterval(function(){
  155. dashboard_dashboardwidget_refresh();
  156. },3000);
  157. if(!$('.dashboard-container').hasClass('readonly')){
  158. //Gestion du resize de largeur
  159. $('.widget_resize').draggable({
  160. axis: "x",
  161. cancel :'[data-mandatory="1"]',
  162. start : function(event,ui){
  163. var widget = ui.helper.closest('.widget');
  164. $('.widget_content',widget).append('<div class="readonly-veil"></div>');
  165. },
  166. drag: function(event,ui) {
  167. var widget = ui.helper.closest('.widget');
  168. var width = widget.attr('data-width');
  169. var percent = ui.position.left * 100 / ui.originalPosition.left;
  170. var newWidth = Math.round(width * percent/100);
  171. newWidth = newWidth<1 ? 1 : newWidth;
  172. newWidth = newWidth>12 ? 12 : newWidth;
  173. //Gestion taille widget max
  174. if(widget.attr('data-maxWidth') && widget.attr('data-maxWidth').length){
  175. maxWidth = widget.attr('data-maxWidth');
  176. newWidth = newWidth>maxWidth ? maxWidth : newWidth;
  177. }
  178. //Gestion taille widget min
  179. if(widget.attr('data-minWidth') && widget.attr('data-minWidth').length){
  180. minWidth = widget.attr('data-minWidth');
  181. newWidth = newWidth<minWidth ? minWidth : newWidth;
  182. }
  183. widget.removeClass('col-xl-1 col-xl-2 col-xl-3 col-xl-4 col-xl-5 col-xl-6 col-xl-7 col-xl-8 col-xl-9 col-xl-10 col-xl-11 col-xl-12');
  184. widget.addClass('col-xl-'+newWidth);
  185. widget.removeClass('col-lg-1 col-lg-2 col-lg-3 col-lg-4 col-lg-5 col-lg-6 col-lg-7 col-lg-8 col-lg-9 col-lg-10 col-lg-11 col-lg-12');
  186. widget.addClass('col-lg-'+newWidth*2);
  187. widget.removeClass('col-md-1 col-md-2 col-md-3 col-md-4 col-md-5 col-md-6 col-md-7 col-md-8 col-md-9 col-md-10 col-md-11 col-md-12');
  188. widget.addClass('col-md-'+newWidth*2);
  189. },
  190. stop: function(event,ui){
  191. var resizebar = ui.helper;
  192. var widget = resizebar.closest('.widget');
  193. $('.widget .readonly-veil').remove();
  194. var width = widget.attr('data-width');
  195. var newWidth = widget.attr('class').match(/col-xl-([0-9]*)/)
  196. newWidth = newWidth[1];
  197. if(newWidth!=width){
  198. $.action({
  199. action : 'dashboard_dashboardwidget_resize',
  200. widget : widget.attr('data-id'),
  201. width: newWidth
  202. },function(r){
  203. widget.attr('data-width',newWidth);
  204. });
  205. }
  206. resizebar.removeAttr('style');
  207. }
  208. });
  209. }
  210. });
  211. }
  212. function dashboard_dashboardwidget_refresh(){
  213. $.action({
  214. action : 'dashboard_dashboardwidget_refresh',
  215. dashboard : $('#dashboard-view li[data-selected]').attr('data-id')
  216. },function(r){
  217. for(var id in r.rows){
  218. var widget = r.rows[id];
  219. if(widget.widget){
  220. var header = $('.widget[data-id="'+id+'"]').find('.widget_header');
  221. if(widget.widget.title) header.find('span').text(widget.widget.title);
  222. if(widget.widget.icon) header.find('i').attr('class','fa '+widget.widget.icon);
  223. if(widget.widget.background) header.css('backgroundColor',widget.widget.background);
  224. }
  225. if(widget.callback){
  226. if(window[widget.callback]!=null) window[widget.callback]($('.widget[data-id="'+id+'"]'),widget.data);
  227. }
  228. }
  229. });
  230. }
  231. //Mise à jour des infos d'un élement widget à partir d'un object data
  232. function dashboard_dashboardwidget_render(widget,data){
  233. widget.attr('data-id',data.id);
  234. widget.removeClass (function (index, css) {
  235. return (css.match (/(^|\s)col-xl-\S+/g) || []).join(' ');
  236. });
  237. var width = data.width!=0 ? data.width : data.defaultWidth;
  238. widget.attr('data-id',data.id)
  239. .attr('data-load',data.load)
  240. .attr('data-configure',data.configure)
  241. .attr('data-configure-callback',data.configure_callback)
  242. .attr('data-configure-init',data.configure_init)
  243. .attr('data-delete',data.delete)
  244. .attr('data-mandatory',data.mandatory && data.mandatory==1 ?1:0)
  245. .attr('data-model',data.model)
  246. .attr('data-width',width)
  247. .addClass('col-xl-'+width)
  248. .addClass('col-lg-'+width*2)
  249. .addClass('col-md-'+width*2)
  250. .find('.widget_header')
  251. .css('background',data.background)
  252. .find('i:eq(0)').attr('class',data.icon);
  253. if(data.minWidth && data.minWidth!=0) widget.attr('data-minWidth', data.minWidth);
  254. if(data.maxWidth && data.maxWidth!=0) widget.attr('data-maxWidth', data.maxWidth);
  255. widget.find('.widget_header span:eq(0)').html(data.title);
  256. widget.find('.widget_content').html(data.content);
  257. var options = '';
  258. for(var k in data.options){
  259. var option = data.options[k];
  260. options+='<li onclick="'+option.function+'" '+(option.title ? 'title="'+option.title+'"':'')+'><i class="fa '+option.icon+'"></i> '+(option.label ? option.label:'')+'</li>';
  261. }
  262. if(data.configure) options+="<li title='Configurer' onclick='configure_widget(this);'><i class='fa fa-wrench'></i></li>";
  263. options+="<li title='Supprimer' onclick='dashboard_dashboardwidget_delete(this);'><i class='fa fa-times'></i></li>";
  264. widget.find('.widget_options').html(options);
  265. widget.data('data',data);
  266. widget.removeClass('hidden');
  267. return widget;
  268. }
  269. //Modification d'un widget existant
  270. function dashboard_dashboardwidget_update(data){
  271. var widget = $('.widget[data-id="'+data.id+'"]');
  272. var data = $.extend(widget.data('data'), data);
  273. dashboard_dashboardwidget_render(widget,data);
  274. }
  275. //Ajout ou modification d'élément dashboardwidget
  276. function dashboard_dashboardwidget_save(){
  277. var data = $('#dashboardwidget-form').toJson();
  278. $.action(data,function(r){
  279. $('#dashboardwidget-form').attr('data-id','');
  280. dashboard_dashboardwidget_search();
  281. $.message('success','Enregistré');
  282. });
  283. }
  284. //Chargement du contenu php du widget
  285. function dashboard_dashboardwidget_load(widget){
  286. $.getJSON(widget.load,{
  287. id : widget.id,
  288. model : widget.model,
  289. content:'',
  290. },function(r){
  291. if(r.error && r.error.code == 403){
  292. $('.widget[data-id="'+widget.id+'"]').remove();
  293. return;
  294. }
  295. dashboard_dashboardwidget_update(r);
  296. var data = $.extend($('.widget[data-id="'+widget.id+'"]').data('data'), r.widget);
  297. var init = 'widget_'+data.model.replace(/[^a-z0-9]/i,'_')+'_init';
  298. if(window[init]!=null) window[init]();
  299. });
  300. }
  301. //Ajout (manuel par l'user) d'un widget
  302. function dashboard_dashboardwidget_add(){
  303. $.action({
  304. action : 'dashboard_dashboardwidget_add',
  305. dashboard : $('#dashboard-view li[data-selected]').attr('data-id'),
  306. widget : $('#widget-list').val()
  307. },function(r){
  308. if(r.message) $.message('info',r.message);
  309. $('#dashboard_dashboardwidget_appendModal').modal('hide');
  310. dashboard_dashboardwidget_search();
  311. });
  312. }
  313. //Ajout (depuis le code) d'un widget
  314. function dashboard_dashboardwidget_append(data,callback){
  315. var tpl = $('#dashboard .widget:hidden').get(0).outerHTML;
  316. var widget = $(tpl);
  317. $('#dashboard').append(widget);
  318. if(data.css!=null){
  319. for(k in data.css){
  320. var css = data.css[k];
  321. if($('link[href="'+css+'"]').length!=0) continue;
  322. //on supprime tout autre css ayant la même base mais des versions plus vielles
  323. var baseCss = css.replace(/\?v=.*/gm,'');
  324. $('link[href^="'+baseCss+'"]').remove();
  325. var cssFile = document.createElement('link');
  326. cssFile.setAttribute("rel","stylesheet");
  327. cssFile.setAttribute("type","text/css");
  328. cssFile.setAttribute("href", css);
  329. document.getElementsByTagName("body")[0].appendChild(cssFile);
  330. }
  331. }
  332. dashboard_dashboardwidget_render(widget,data);
  333. if(data.js!=null){
  334. dashboard_load_js(data.js,0,function(){
  335. if(callback) callback(data);
  336. });
  337. }else{
  338. if(callback) callback(data);
  339. }
  340. }
  341. function dashboard_load_js(files,iteration,callback){
  342. var js = files[iteration];
  343. if($('script[src="'+js+'"]').length!=0 || js==null) {
  344. if(files.length > iteration) dashboard_load_js(files,iteration+1,callback);
  345. if((files.length-1) == iteration) if(callback) callback();
  346. return;
  347. }
  348. //on supprime tout autre js ayant la même base mais des versions plus vielles
  349. var baseJs = js.replace(/\?v=.*/gm,'');
  350. $('script[src^="'+baseJs+'?"]').remove();
  351. var jsFile = document.createElement('script');
  352. jsFile.setAttribute("type","text/javascript");
  353. document.getElementsByTagName("body")[0].appendChild(jsFile);
  354. jsFile.onload = function() {
  355. if(files.length > iteration) dashboard_load_js(files,iteration+1,callback);
  356. if((files.length-1) == iteration) if(callback) callback();
  357. };
  358. jsFile.src = js;
  359. }
  360. //Récuperation ou edition d'élément dashboardwidget
  361. function dashboard_dashboardwidget_edit(widget){
  362. var line = $(element).closest('tr');
  363. $.action({action:'dashboard_dashboardwidget_edit',id:line.attr('data-id')},function(r){
  364. $.setForm('#dashboardwidget-form',r);
  365. $('#dashboardwidget-form').attr('data-id',r.id);
  366. });
  367. }
  368. //Suppression d'élement dashboardwidget
  369. function dashboard_dashboardwidget_delete(element){
  370. var element = $(element).closest('.widget');
  371. var data = element.data('data');
  372. $.action({
  373. action : 'dashboard_dashboardwidget_delete',
  374. dashboard : $('#dashboard-view li[data-selected]').attr('data-id'),
  375. widget : data.id,
  376. },function(r){
  377. element.remove();
  378. if(r.message) $.message('info',r.message);
  379. else $.message('info', 'Widget supprimé');
  380. if(data.delete != null){
  381. $.getJSON(data.delete,$.extend(data,{content:''}));
  382. }
  383. });
  384. }
  385. //Enregistrement de toutes les positions de widget
  386. function dashboard_dashboardwidget_save_position(){
  387. var positions = [];
  388. $('.widget:visible').each(function(i,element){
  389. positions.push({id:$(element).attr('data-id'),position:$(element).index()});
  390. });
  391. $.action({
  392. action : 'dashboard_dashboardwidget_save_position',
  393. dashboard : $('#dashboard-view li[data-selected]').attr('data-id'),
  394. positions : positions,
  395. },function(r){
  396. });
  397. }
  398. /** DASHBOARDWIDGETSHARE **/
  399. //Récuperation d'une liste de dashboardwidgetshare dans le tableau #dashboardwidgetshares
  400. function dashboard_widget_share_search(callback){
  401. $('#dashboard-widget-shares').fill({
  402. action: 'dashboard_widget_share_search'
  403. },function(response){
  404. if(callback!=null) callback();
  405. });
  406. }
  407. //Ajout ou modification d'élément dashboardwidgetshare
  408. function dashboard_widget_share_save(){
  409. var data = $('#dashboard-widget-share-form').toJson();
  410. var target = $('#uid').data('values');
  411. if(target){
  412. data.entity = target[0].entity;
  413. data.uid = target[0].uid;
  414. }
  415. $.action(data,function(r){
  416. $('#dashboard-widget-share-form').attr('data-id','');
  417. $('#dashboard-widget-share-form').clear();
  418. dashboard_widget_share_search();
  419. $.message('success','Enregistré');
  420. });
  421. }
  422. //Récuperation ou edition d'élément dashboardwidgetshare
  423. function dashboard_widget_share_edit(element){
  424. var line = $(element).closest('tr');
  425. $.action({
  426. action: 'dashboard_widget_share_edit',
  427. id: line.attr('data-id')
  428. },function(r){
  429. $.setForm('#dashboard-widget-share-form',r);
  430. init_components('#dashboard-widget-share-form');
  431. $('#dashboard-widget-share-form').attr('data-id',r.id);
  432. });
  433. }
  434. //Suppression d'élement dashboardwidgetshare
  435. function dashboard_widget_share_delete(element){
  436. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  437. var line = $(element).closest('tr');
  438. $.action({
  439. action: 'dashboard_widget_share_delete',
  440. id: line.attr('data-id')
  441. },function(r){
  442. line.remove();
  443. $.message('info','Item supprimé');
  444. });
  445. }